xref: /freebsd/sys/dev/ral/rt2661.c (revision 396c556d77189a5c474d35cec6f44a762e310b7d)
1 /*	$FreeBSD$	*/
2 
3 /*-
4  * Copyright (c) 2006
5  *	Damien Bergamini <damien.bergamini@free.fr>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include <sys/cdefs.h>
21 __FBSDID("$FreeBSD$");
22 
23 /*-
24  * Ralink Technology RT2561, RT2561S and RT2661 chipset driver
25  * http://www.ralinktech.com/
26  */
27 
28 #include <sys/param.h>
29 #include <sys/sysctl.h>
30 #include <sys/sockio.h>
31 #include <sys/mbuf.h>
32 #include <sys/kernel.h>
33 #include <sys/socket.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/lock.h>
37 #include <sys/mutex.h>
38 #include <sys/module.h>
39 #include <sys/bus.h>
40 #include <sys/endian.h>
41 #include <sys/firmware.h>
42 
43 #include <machine/bus.h>
44 #include <machine/resource.h>
45 #include <sys/rman.h>
46 
47 #include <net/bpf.h>
48 #include <net/if.h>
49 #include <net/if_var.h>
50 #include <net/if_arp.h>
51 #include <net/ethernet.h>
52 #include <net/if_dl.h>
53 #include <net/if_media.h>
54 #include <net/if_types.h>
55 
56 #include <net80211/ieee80211_var.h>
57 #include <net80211/ieee80211_radiotap.h>
58 #include <net80211/ieee80211_regdomain.h>
59 #include <net80211/ieee80211_ratectl.h>
60 
61 #include <netinet/in.h>
62 #include <netinet/in_systm.h>
63 #include <netinet/in_var.h>
64 #include <netinet/ip.h>
65 #include <netinet/if_ether.h>
66 
67 #include <dev/ral/rt2661reg.h>
68 #include <dev/ral/rt2661var.h>
69 
70 #define RAL_DEBUG
71 #ifdef RAL_DEBUG
72 #define DPRINTF(sc, fmt, ...) do {				\
73 	if (sc->sc_debug > 0)					\
74 		printf(fmt, __VA_ARGS__);			\
75 } while (0)
76 #define DPRINTFN(sc, n, fmt, ...) do {				\
77 	if (sc->sc_debug >= (n))				\
78 		printf(fmt, __VA_ARGS__);			\
79 } while (0)
80 #else
81 #define DPRINTF(sc, fmt, ...)
82 #define DPRINTFN(sc, n, fmt, ...)
83 #endif
84 
85 static struct ieee80211vap *rt2661_vap_create(struct ieee80211com *,
86 			    const char [IFNAMSIZ], int, enum ieee80211_opmode,
87 			    int, const uint8_t [IEEE80211_ADDR_LEN],
88 			    const uint8_t [IEEE80211_ADDR_LEN]);
89 static void		rt2661_vap_delete(struct ieee80211vap *);
90 static void		rt2661_dma_map_addr(void *, bus_dma_segment_t *, int,
91 			    int);
92 static int		rt2661_alloc_tx_ring(struct rt2661_softc *,
93 			    struct rt2661_tx_ring *, int);
94 static void		rt2661_reset_tx_ring(struct rt2661_softc *,
95 			    struct rt2661_tx_ring *);
96 static void		rt2661_free_tx_ring(struct rt2661_softc *,
97 			    struct rt2661_tx_ring *);
98 static int		rt2661_alloc_rx_ring(struct rt2661_softc *,
99 			    struct rt2661_rx_ring *, int);
100 static void		rt2661_reset_rx_ring(struct rt2661_softc *,
101 			    struct rt2661_rx_ring *);
102 static void		rt2661_free_rx_ring(struct rt2661_softc *,
103 			    struct rt2661_rx_ring *);
104 static int		rt2661_newstate(struct ieee80211vap *,
105 			    enum ieee80211_state, int);
106 static uint16_t		rt2661_eeprom_read(struct rt2661_softc *, uint8_t);
107 static void		rt2661_rx_intr(struct rt2661_softc *);
108 static void		rt2661_tx_intr(struct rt2661_softc *);
109 static void		rt2661_tx_dma_intr(struct rt2661_softc *,
110 			    struct rt2661_tx_ring *);
111 static void		rt2661_mcu_beacon_expire(struct rt2661_softc *);
112 static void		rt2661_mcu_wakeup(struct rt2661_softc *);
113 static void		rt2661_mcu_cmd_intr(struct rt2661_softc *);
114 static void		rt2661_scan_start(struct ieee80211com *);
115 static void		rt2661_scan_end(struct ieee80211com *);
116 static void		rt2661_getradiocaps(struct ieee80211com *, int, int *,
117 			    struct ieee80211_channel[]);
118 static void		rt2661_set_channel(struct ieee80211com *);
119 static void		rt2661_setup_tx_desc(struct rt2661_softc *,
120 			    struct rt2661_tx_desc *, uint32_t, uint16_t, int,
121 			    int, const bus_dma_segment_t *, int, int);
122 static int		rt2661_tx_data(struct rt2661_softc *, struct mbuf *,
123 			    struct ieee80211_node *, int);
124 static int		rt2661_tx_mgt(struct rt2661_softc *, struct mbuf *,
125 			    struct ieee80211_node *);
126 static int		rt2661_transmit(struct ieee80211com *, struct mbuf *);
127 static void		rt2661_start(struct rt2661_softc *);
128 static int		rt2661_raw_xmit(struct ieee80211_node *, struct mbuf *,
129 			    const struct ieee80211_bpf_params *);
130 static void		rt2661_watchdog(void *);
131 static void		rt2661_parent(struct ieee80211com *);
132 static void		rt2661_bbp_write(struct rt2661_softc *, uint8_t,
133 			    uint8_t);
134 static uint8_t		rt2661_bbp_read(struct rt2661_softc *, uint8_t);
135 static void		rt2661_rf_write(struct rt2661_softc *, uint8_t,
136 			    uint32_t);
137 static int		rt2661_tx_cmd(struct rt2661_softc *, uint8_t,
138 			    uint16_t);
139 static void		rt2661_select_antenna(struct rt2661_softc *);
140 static void		rt2661_enable_mrr(struct rt2661_softc *);
141 static void		rt2661_set_txpreamble(struct rt2661_softc *);
142 static void		rt2661_set_basicrates(struct rt2661_softc *,
143 			    const struct ieee80211_rateset *);
144 static void		rt2661_select_band(struct rt2661_softc *,
145 			    struct ieee80211_channel *);
146 static void		rt2661_set_chan(struct rt2661_softc *,
147 			    struct ieee80211_channel *);
148 static void		rt2661_set_bssid(struct rt2661_softc *,
149 			    const uint8_t *);
150 static void		rt2661_set_macaddr(struct rt2661_softc *,
151 			   const uint8_t *);
152 static void		rt2661_update_promisc(struct ieee80211com *);
153 static int		rt2661_wme_update(struct ieee80211com *) __unused;
154 static void		rt2661_update_slot(struct ieee80211com *);
155 static const char	*rt2661_get_rf(int);
156 static void		rt2661_read_eeprom(struct rt2661_softc *,
157 			    uint8_t macaddr[IEEE80211_ADDR_LEN]);
158 static int		rt2661_bbp_init(struct rt2661_softc *);
159 static void		rt2661_init_locked(struct rt2661_softc *);
160 static void		rt2661_init(void *);
161 static void             rt2661_stop_locked(struct rt2661_softc *);
162 static void		rt2661_stop(void *);
163 static int		rt2661_load_microcode(struct rt2661_softc *);
164 #ifdef notyet
165 static void		rt2661_rx_tune(struct rt2661_softc *);
166 static void		rt2661_radar_start(struct rt2661_softc *);
167 static int		rt2661_radar_stop(struct rt2661_softc *);
168 #endif
169 static int		rt2661_prepare_beacon(struct rt2661_softc *,
170 			    struct ieee80211vap *);
171 static void		rt2661_enable_tsf_sync(struct rt2661_softc *);
172 static void		rt2661_enable_tsf(struct rt2661_softc *);
173 static int		rt2661_get_rssi(struct rt2661_softc *, uint8_t);
174 
175 static const struct {
176 	uint32_t	reg;
177 	uint32_t	val;
178 } rt2661_def_mac[] = {
179 	RT2661_DEF_MAC
180 };
181 
182 static const struct {
183 	uint8_t	reg;
184 	uint8_t	val;
185 } rt2661_def_bbp[] = {
186 	RT2661_DEF_BBP
187 };
188 
189 static const struct rfprog {
190 	uint8_t		chan;
191 	uint32_t	r1, r2, r3, r4;
192 }  rt2661_rf5225_1[] = {
193 	RT2661_RF5225_1
194 }, rt2661_rf5225_2[] = {
195 	RT2661_RF5225_2
196 };
197 
198 static const uint8_t rt2661_chan_2ghz[] =
199 	{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };
200 static const uint8_t rt2661_chan_5ghz[] =
201 	{ 36, 40, 44, 48, 52, 56, 60, 64,
202 	  100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140,
203 	  149, 153, 157, 161, 165 };
204 
205 int
206 rt2661_attach(device_t dev, int id)
207 {
208 	struct rt2661_softc *sc = device_get_softc(dev);
209 	struct ieee80211com *ic = &sc->sc_ic;
210 	uint32_t val;
211 	int error, ac, ntries;
212 
213 	sc->sc_id = id;
214 	sc->sc_dev = dev;
215 
216 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
217 	    MTX_DEF | MTX_RECURSE);
218 
219 	callout_init_mtx(&sc->watchdog_ch, &sc->sc_mtx, 0);
220 	mbufq_init(&sc->sc_snd, ifqmaxlen);
221 
222 	/* wait for NIC to initialize */
223 	for (ntries = 0; ntries < 1000; ntries++) {
224 		if ((val = RAL_READ(sc, RT2661_MAC_CSR0)) != 0)
225 			break;
226 		DELAY(1000);
227 	}
228 	if (ntries == 1000) {
229 		device_printf(sc->sc_dev,
230 		    "timeout waiting for NIC to initialize\n");
231 		error = EIO;
232 		goto fail1;
233 	}
234 
235 	/* retrieve RF rev. no and various other things from EEPROM */
236 	rt2661_read_eeprom(sc, ic->ic_macaddr);
237 
238 	device_printf(dev, "MAC/BBP RT%X, RF %s\n", val,
239 	    rt2661_get_rf(sc->rf_rev));
240 
241 	/*
242 	 * Allocate Tx and Rx rings.
243 	 */
244 	for (ac = 0; ac < 4; ac++) {
245 		error = rt2661_alloc_tx_ring(sc, &sc->txq[ac],
246 		    RT2661_TX_RING_COUNT);
247 		if (error != 0) {
248 			device_printf(sc->sc_dev,
249 			    "could not allocate Tx ring %d\n", ac);
250 			goto fail2;
251 		}
252 	}
253 
254 	error = rt2661_alloc_tx_ring(sc, &sc->mgtq, RT2661_MGT_RING_COUNT);
255 	if (error != 0) {
256 		device_printf(sc->sc_dev, "could not allocate Mgt ring\n");
257 		goto fail2;
258 	}
259 
260 	error = rt2661_alloc_rx_ring(sc, &sc->rxq, RT2661_RX_RING_COUNT);
261 	if (error != 0) {
262 		device_printf(sc->sc_dev, "could not allocate Rx ring\n");
263 		goto fail3;
264 	}
265 
266 	ic->ic_softc = sc;
267 	ic->ic_name = device_get_nameunit(dev);
268 	ic->ic_opmode = IEEE80211_M_STA;
269 	ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
270 
271 	/* set device capabilities */
272 	ic->ic_caps =
273 		  IEEE80211_C_STA		/* station mode */
274 		| IEEE80211_C_IBSS		/* ibss, nee adhoc, mode */
275 		| IEEE80211_C_HOSTAP		/* hostap mode */
276 		| IEEE80211_C_MONITOR		/* monitor mode */
277 		| IEEE80211_C_AHDEMO		/* adhoc demo mode */
278 		| IEEE80211_C_WDS		/* 4-address traffic works */
279 		| IEEE80211_C_MBSS		/* mesh point link mode */
280 		| IEEE80211_C_SHPREAMBLE	/* short preamble supported */
281 		| IEEE80211_C_SHSLOT		/* short slot time supported */
282 		| IEEE80211_C_WPA		/* capable of WPA1+WPA2 */
283 		| IEEE80211_C_BGSCAN		/* capable of bg scanning */
284 #ifdef notyet
285 		| IEEE80211_C_TXFRAG		/* handle tx frags */
286 		| IEEE80211_C_WME		/* 802.11e */
287 #endif
288 		;
289 
290 	rt2661_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
291 	    ic->ic_channels);
292 
293 	ieee80211_ifattach(ic);
294 #if 0
295 	ic->ic_wme.wme_update = rt2661_wme_update;
296 #endif
297 	ic->ic_scan_start = rt2661_scan_start;
298 	ic->ic_scan_end = rt2661_scan_end;
299 	ic->ic_getradiocaps = rt2661_getradiocaps;
300 	ic->ic_set_channel = rt2661_set_channel;
301 	ic->ic_updateslot = rt2661_update_slot;
302 	ic->ic_update_promisc = rt2661_update_promisc;
303 	ic->ic_raw_xmit = rt2661_raw_xmit;
304 	ic->ic_transmit = rt2661_transmit;
305 	ic->ic_parent = rt2661_parent;
306 	ic->ic_vap_create = rt2661_vap_create;
307 	ic->ic_vap_delete = rt2661_vap_delete;
308 
309 	ieee80211_radiotap_attach(ic,
310 	    &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
311 		RT2661_TX_RADIOTAP_PRESENT,
312 	    &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
313 		RT2661_RX_RADIOTAP_PRESENT);
314 
315 #ifdef RAL_DEBUG
316 	SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
317 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
318 	    "debug", CTLFLAG_RW, &sc->sc_debug, 0, "debug msgs");
319 #endif
320 	if (bootverbose)
321 		ieee80211_announce(ic);
322 
323 	return 0;
324 
325 fail3:	rt2661_free_tx_ring(sc, &sc->mgtq);
326 fail2:	while (--ac >= 0)
327 		rt2661_free_tx_ring(sc, &sc->txq[ac]);
328 fail1:	mtx_destroy(&sc->sc_mtx);
329 	return error;
330 }
331 
332 int
333 rt2661_detach(void *xsc)
334 {
335 	struct rt2661_softc *sc = xsc;
336 	struct ieee80211com *ic = &sc->sc_ic;
337 
338 	RAL_LOCK(sc);
339 	rt2661_stop_locked(sc);
340 	RAL_UNLOCK(sc);
341 
342 	ieee80211_ifdetach(ic);
343 	mbufq_drain(&sc->sc_snd);
344 
345 	rt2661_free_tx_ring(sc, &sc->txq[0]);
346 	rt2661_free_tx_ring(sc, &sc->txq[1]);
347 	rt2661_free_tx_ring(sc, &sc->txq[2]);
348 	rt2661_free_tx_ring(sc, &sc->txq[3]);
349 	rt2661_free_tx_ring(sc, &sc->mgtq);
350 	rt2661_free_rx_ring(sc, &sc->rxq);
351 
352 	mtx_destroy(&sc->sc_mtx);
353 
354 	return 0;
355 }
356 
357 static struct ieee80211vap *
358 rt2661_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
359     enum ieee80211_opmode opmode, int flags,
360     const uint8_t bssid[IEEE80211_ADDR_LEN],
361     const uint8_t mac[IEEE80211_ADDR_LEN])
362 {
363 	struct rt2661_softc *sc = ic->ic_softc;
364 	struct rt2661_vap *rvp;
365 	struct ieee80211vap *vap;
366 
367 	switch (opmode) {
368 	case IEEE80211_M_STA:
369 	case IEEE80211_M_IBSS:
370 	case IEEE80211_M_AHDEMO:
371 	case IEEE80211_M_MONITOR:
372 	case IEEE80211_M_HOSTAP:
373 	case IEEE80211_M_MBSS:
374 		/* XXXRP: TBD */
375 		if (!TAILQ_EMPTY(&ic->ic_vaps)) {
376 			device_printf(sc->sc_dev, "only 1 vap supported\n");
377 			return NULL;
378 		}
379 		if (opmode == IEEE80211_M_STA)
380 			flags |= IEEE80211_CLONE_NOBEACONS;
381 		break;
382 	case IEEE80211_M_WDS:
383 		if (TAILQ_EMPTY(&ic->ic_vaps) ||
384 		    ic->ic_opmode != IEEE80211_M_HOSTAP) {
385 			device_printf(sc->sc_dev,
386 			    "wds only supported in ap mode\n");
387 			return NULL;
388 		}
389 		/*
390 		 * Silently remove any request for a unique
391 		 * bssid; WDS vap's always share the local
392 		 * mac address.
393 		 */
394 		flags &= ~IEEE80211_CLONE_BSSID;
395 		break;
396 	default:
397 		device_printf(sc->sc_dev, "unknown opmode %d\n", opmode);
398 		return NULL;
399 	}
400 	rvp = malloc(sizeof(struct rt2661_vap), M_80211_VAP, M_WAITOK | M_ZERO);
401 	vap = &rvp->ral_vap;
402 	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
403 
404 	/* override state transition machine */
405 	rvp->ral_newstate = vap->iv_newstate;
406 	vap->iv_newstate = rt2661_newstate;
407 #if 0
408 	vap->iv_update_beacon = rt2661_beacon_update;
409 #endif
410 
411 	ieee80211_ratectl_init(vap);
412 	/* complete setup */
413 	ieee80211_vap_attach(vap, ieee80211_media_change,
414 	    ieee80211_media_status, mac);
415 	if (TAILQ_FIRST(&ic->ic_vaps) == vap)
416 		ic->ic_opmode = opmode;
417 	return vap;
418 }
419 
420 static void
421 rt2661_vap_delete(struct ieee80211vap *vap)
422 {
423 	struct rt2661_vap *rvp = RT2661_VAP(vap);
424 
425 	ieee80211_ratectl_deinit(vap);
426 	ieee80211_vap_detach(vap);
427 	free(rvp, M_80211_VAP);
428 }
429 
430 void
431 rt2661_shutdown(void *xsc)
432 {
433 	struct rt2661_softc *sc = xsc;
434 
435 	rt2661_stop(sc);
436 }
437 
438 void
439 rt2661_suspend(void *xsc)
440 {
441 	struct rt2661_softc *sc = xsc;
442 
443 	rt2661_stop(sc);
444 }
445 
446 void
447 rt2661_resume(void *xsc)
448 {
449 	struct rt2661_softc *sc = xsc;
450 
451 	if (sc->sc_ic.ic_nrunning > 0)
452 		rt2661_init(sc);
453 }
454 
455 static void
456 rt2661_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
457 {
458 	if (error != 0)
459 		return;
460 
461 	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
462 
463 	*(bus_addr_t *)arg = segs[0].ds_addr;
464 }
465 
466 static int
467 rt2661_alloc_tx_ring(struct rt2661_softc *sc, struct rt2661_tx_ring *ring,
468     int count)
469 {
470 	int i, error;
471 
472 	ring->count = count;
473 	ring->queued = 0;
474 	ring->cur = ring->next = ring->stat = 0;
475 
476 	error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 4, 0,
477 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
478 	    count * RT2661_TX_DESC_SIZE, 1, count * RT2661_TX_DESC_SIZE,
479 	    0, NULL, NULL, &ring->desc_dmat);
480 	if (error != 0) {
481 		device_printf(sc->sc_dev, "could not create desc DMA tag\n");
482 		goto fail;
483 	}
484 
485 	error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc,
486 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_map);
487 	if (error != 0) {
488 		device_printf(sc->sc_dev, "could not allocate DMA memory\n");
489 		goto fail;
490 	}
491 
492 	error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc,
493 	    count * RT2661_TX_DESC_SIZE, rt2661_dma_map_addr, &ring->physaddr,
494 	    0);
495 	if (error != 0) {
496 		device_printf(sc->sc_dev, "could not load desc DMA map\n");
497 		goto fail;
498 	}
499 
500 	ring->data = malloc(count * sizeof (struct rt2661_tx_data), M_DEVBUF,
501 	    M_NOWAIT | M_ZERO);
502 	if (ring->data == NULL) {
503 		device_printf(sc->sc_dev, "could not allocate soft data\n");
504 		error = ENOMEM;
505 		goto fail;
506 	}
507 
508 	error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
509 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES,
510 	    RT2661_MAX_SCATTER, MCLBYTES, 0, NULL, NULL, &ring->data_dmat);
511 	if (error != 0) {
512 		device_printf(sc->sc_dev, "could not create data DMA tag\n");
513 		goto fail;
514 	}
515 
516 	for (i = 0; i < count; i++) {
517 		error = bus_dmamap_create(ring->data_dmat, 0,
518 		    &ring->data[i].map);
519 		if (error != 0) {
520 			device_printf(sc->sc_dev, "could not create DMA map\n");
521 			goto fail;
522 		}
523 	}
524 
525 	return 0;
526 
527 fail:	rt2661_free_tx_ring(sc, ring);
528 	return error;
529 }
530 
531 static void
532 rt2661_reset_tx_ring(struct rt2661_softc *sc, struct rt2661_tx_ring *ring)
533 {
534 	struct rt2661_tx_desc *desc;
535 	struct rt2661_tx_data *data;
536 	int i;
537 
538 	for (i = 0; i < ring->count; i++) {
539 		desc = &ring->desc[i];
540 		data = &ring->data[i];
541 
542 		if (data->m != NULL) {
543 			bus_dmamap_sync(ring->data_dmat, data->map,
544 			    BUS_DMASYNC_POSTWRITE);
545 			bus_dmamap_unload(ring->data_dmat, data->map);
546 			m_freem(data->m);
547 			data->m = NULL;
548 		}
549 
550 		if (data->ni != NULL) {
551 			ieee80211_free_node(data->ni);
552 			data->ni = NULL;
553 		}
554 
555 		desc->flags = 0;
556 	}
557 
558 	bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_PREWRITE);
559 
560 	ring->queued = 0;
561 	ring->cur = ring->next = ring->stat = 0;
562 }
563 
564 static void
565 rt2661_free_tx_ring(struct rt2661_softc *sc, struct rt2661_tx_ring *ring)
566 {
567 	struct rt2661_tx_data *data;
568 	int i;
569 
570 	if (ring->desc != NULL) {
571 		bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
572 		    BUS_DMASYNC_POSTWRITE);
573 		bus_dmamap_unload(ring->desc_dmat, ring->desc_map);
574 		bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
575 	}
576 
577 	if (ring->desc_dmat != NULL)
578 		bus_dma_tag_destroy(ring->desc_dmat);
579 
580 	if (ring->data != NULL) {
581 		for (i = 0; i < ring->count; i++) {
582 			data = &ring->data[i];
583 
584 			if (data->m != NULL) {
585 				bus_dmamap_sync(ring->data_dmat, data->map,
586 				    BUS_DMASYNC_POSTWRITE);
587 				bus_dmamap_unload(ring->data_dmat, data->map);
588 				m_freem(data->m);
589 			}
590 
591 			if (data->ni != NULL)
592 				ieee80211_free_node(data->ni);
593 
594 			if (data->map != NULL)
595 				bus_dmamap_destroy(ring->data_dmat, data->map);
596 		}
597 
598 		free(ring->data, M_DEVBUF);
599 	}
600 
601 	if (ring->data_dmat != NULL)
602 		bus_dma_tag_destroy(ring->data_dmat);
603 }
604 
605 static int
606 rt2661_alloc_rx_ring(struct rt2661_softc *sc, struct rt2661_rx_ring *ring,
607     int count)
608 {
609 	struct rt2661_rx_desc *desc;
610 	struct rt2661_rx_data *data;
611 	bus_addr_t physaddr;
612 	int i, error;
613 
614 	ring->count = count;
615 	ring->cur = ring->next = 0;
616 
617 	error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 4, 0,
618 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
619 	    count * RT2661_RX_DESC_SIZE, 1, count * RT2661_RX_DESC_SIZE,
620 	    0, NULL, NULL, &ring->desc_dmat);
621 	if (error != 0) {
622 		device_printf(sc->sc_dev, "could not create desc DMA tag\n");
623 		goto fail;
624 	}
625 
626 	error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc,
627 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_map);
628 	if (error != 0) {
629 		device_printf(sc->sc_dev, "could not allocate DMA memory\n");
630 		goto fail;
631 	}
632 
633 	error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc,
634 	    count * RT2661_RX_DESC_SIZE, rt2661_dma_map_addr, &ring->physaddr,
635 	    0);
636 	if (error != 0) {
637 		device_printf(sc->sc_dev, "could not load desc DMA map\n");
638 		goto fail;
639 	}
640 
641 	ring->data = malloc(count * sizeof (struct rt2661_rx_data), M_DEVBUF,
642 	    M_NOWAIT | M_ZERO);
643 	if (ring->data == NULL) {
644 		device_printf(sc->sc_dev, "could not allocate soft data\n");
645 		error = ENOMEM;
646 		goto fail;
647 	}
648 
649 	/*
650 	 * Pre-allocate Rx buffers and populate Rx ring.
651 	 */
652 	error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
653 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES,
654 	    1, MCLBYTES, 0, NULL, NULL, &ring->data_dmat);
655 	if (error != 0) {
656 		device_printf(sc->sc_dev, "could not create data DMA tag\n");
657 		goto fail;
658 	}
659 
660 	for (i = 0; i < count; i++) {
661 		desc = &sc->rxq.desc[i];
662 		data = &sc->rxq.data[i];
663 
664 		error = bus_dmamap_create(ring->data_dmat, 0, &data->map);
665 		if (error != 0) {
666 			device_printf(sc->sc_dev, "could not create DMA map\n");
667 			goto fail;
668 		}
669 
670 		data->m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
671 		if (data->m == NULL) {
672 			device_printf(sc->sc_dev,
673 			    "could not allocate rx mbuf\n");
674 			error = ENOMEM;
675 			goto fail;
676 		}
677 
678 		error = bus_dmamap_load(ring->data_dmat, data->map,
679 		    mtod(data->m, void *), MCLBYTES, rt2661_dma_map_addr,
680 		    &physaddr, 0);
681 		if (error != 0) {
682 			device_printf(sc->sc_dev,
683 			    "could not load rx buf DMA map");
684 			goto fail;
685 		}
686 
687 		desc->flags = htole32(RT2661_RX_BUSY);
688 		desc->physaddr = htole32(physaddr);
689 	}
690 
691 	bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_PREWRITE);
692 
693 	return 0;
694 
695 fail:	rt2661_free_rx_ring(sc, ring);
696 	return error;
697 }
698 
699 static void
700 rt2661_reset_rx_ring(struct rt2661_softc *sc, struct rt2661_rx_ring *ring)
701 {
702 	int i;
703 
704 	for (i = 0; i < ring->count; i++)
705 		ring->desc[i].flags = htole32(RT2661_RX_BUSY);
706 
707 	bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_PREWRITE);
708 
709 	ring->cur = ring->next = 0;
710 }
711 
712 static void
713 rt2661_free_rx_ring(struct rt2661_softc *sc, struct rt2661_rx_ring *ring)
714 {
715 	struct rt2661_rx_data *data;
716 	int i;
717 
718 	if (ring->desc != NULL) {
719 		bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
720 		    BUS_DMASYNC_POSTWRITE);
721 		bus_dmamap_unload(ring->desc_dmat, ring->desc_map);
722 		bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
723 	}
724 
725 	if (ring->desc_dmat != NULL)
726 		bus_dma_tag_destroy(ring->desc_dmat);
727 
728 	if (ring->data != NULL) {
729 		for (i = 0; i < ring->count; i++) {
730 			data = &ring->data[i];
731 
732 			if (data->m != NULL) {
733 				bus_dmamap_sync(ring->data_dmat, data->map,
734 				    BUS_DMASYNC_POSTREAD);
735 				bus_dmamap_unload(ring->data_dmat, data->map);
736 				m_freem(data->m);
737 			}
738 
739 			if (data->map != NULL)
740 				bus_dmamap_destroy(ring->data_dmat, data->map);
741 		}
742 
743 		free(ring->data, M_DEVBUF);
744 	}
745 
746 	if (ring->data_dmat != NULL)
747 		bus_dma_tag_destroy(ring->data_dmat);
748 }
749 
750 static int
751 rt2661_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
752 {
753 	struct rt2661_vap *rvp = RT2661_VAP(vap);
754 	struct ieee80211com *ic = vap->iv_ic;
755 	struct rt2661_softc *sc = ic->ic_softc;
756 	int error;
757 
758 	if (nstate == IEEE80211_S_INIT && vap->iv_state == IEEE80211_S_RUN) {
759 		uint32_t tmp;
760 
761 		/* abort TSF synchronization */
762 		tmp = RAL_READ(sc, RT2661_TXRX_CSR9);
763 		RAL_WRITE(sc, RT2661_TXRX_CSR9, tmp & ~0x00ffffff);
764 	}
765 
766 	error = rvp->ral_newstate(vap, nstate, arg);
767 
768 	if (error == 0 && nstate == IEEE80211_S_RUN) {
769 		struct ieee80211_node *ni = vap->iv_bss;
770 
771 		if (vap->iv_opmode != IEEE80211_M_MONITOR) {
772 			rt2661_enable_mrr(sc);
773 			rt2661_set_txpreamble(sc);
774 			rt2661_set_basicrates(sc, &ni->ni_rates);
775 			rt2661_set_bssid(sc, ni->ni_bssid);
776 		}
777 
778 		if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
779 		    vap->iv_opmode == IEEE80211_M_IBSS ||
780 		    vap->iv_opmode == IEEE80211_M_MBSS) {
781 			error = rt2661_prepare_beacon(sc, vap);
782 			if (error != 0)
783 				return error;
784 		}
785 		if (vap->iv_opmode != IEEE80211_M_MONITOR)
786 			rt2661_enable_tsf_sync(sc);
787 		else
788 			rt2661_enable_tsf(sc);
789 	}
790 	return error;
791 }
792 
793 /*
794  * Read 16 bits at address 'addr' from the serial EEPROM (either 93C46 or
795  * 93C66).
796  */
797 static uint16_t
798 rt2661_eeprom_read(struct rt2661_softc *sc, uint8_t addr)
799 {
800 	uint32_t tmp;
801 	uint16_t val;
802 	int n;
803 
804 	/* clock C once before the first command */
805 	RT2661_EEPROM_CTL(sc, 0);
806 
807 	RT2661_EEPROM_CTL(sc, RT2661_S);
808 	RT2661_EEPROM_CTL(sc, RT2661_S | RT2661_C);
809 	RT2661_EEPROM_CTL(sc, RT2661_S);
810 
811 	/* write start bit (1) */
812 	RT2661_EEPROM_CTL(sc, RT2661_S | RT2661_D);
813 	RT2661_EEPROM_CTL(sc, RT2661_S | RT2661_D | RT2661_C);
814 
815 	/* write READ opcode (10) */
816 	RT2661_EEPROM_CTL(sc, RT2661_S | RT2661_D);
817 	RT2661_EEPROM_CTL(sc, RT2661_S | RT2661_D | RT2661_C);
818 	RT2661_EEPROM_CTL(sc, RT2661_S);
819 	RT2661_EEPROM_CTL(sc, RT2661_S | RT2661_C);
820 
821 	/* write address (A5-A0 or A7-A0) */
822 	n = (RAL_READ(sc, RT2661_E2PROM_CSR) & RT2661_93C46) ? 5 : 7;
823 	for (; n >= 0; n--) {
824 		RT2661_EEPROM_CTL(sc, RT2661_S |
825 		    (((addr >> n) & 1) << RT2661_SHIFT_D));
826 		RT2661_EEPROM_CTL(sc, RT2661_S |
827 		    (((addr >> n) & 1) << RT2661_SHIFT_D) | RT2661_C);
828 	}
829 
830 	RT2661_EEPROM_CTL(sc, RT2661_S);
831 
832 	/* read data Q15-Q0 */
833 	val = 0;
834 	for (n = 15; n >= 0; n--) {
835 		RT2661_EEPROM_CTL(sc, RT2661_S | RT2661_C);
836 		tmp = RAL_READ(sc, RT2661_E2PROM_CSR);
837 		val |= ((tmp & RT2661_Q) >> RT2661_SHIFT_Q) << n;
838 		RT2661_EEPROM_CTL(sc, RT2661_S);
839 	}
840 
841 	RT2661_EEPROM_CTL(sc, 0);
842 
843 	/* clear Chip Select and clock C */
844 	RT2661_EEPROM_CTL(sc, RT2661_S);
845 	RT2661_EEPROM_CTL(sc, 0);
846 	RT2661_EEPROM_CTL(sc, RT2661_C);
847 
848 	return val;
849 }
850 
851 static void
852 rt2661_tx_intr(struct rt2661_softc *sc)
853 {
854 	struct ieee80211_ratectl_tx_status *txs = &sc->sc_txs;
855 	struct rt2661_tx_ring *txq;
856 	struct rt2661_tx_data *data;
857 	uint32_t val;
858 	int error, qid;
859 
860 	txs->flags = IEEE80211_RATECTL_TX_FAIL_LONG;
861 	for (;;) {
862 		struct ieee80211_node *ni;
863 		struct mbuf *m;
864 
865 		val = RAL_READ(sc, RT2661_STA_CSR4);
866 		if (!(val & RT2661_TX_STAT_VALID))
867 			break;
868 
869 		/* retrieve the queue in which this frame was sent */
870 		qid = RT2661_TX_QID(val);
871 		txq = (qid <= 3) ? &sc->txq[qid] : &sc->mgtq;
872 
873 		/* retrieve rate control algorithm context */
874 		data = &txq->data[txq->stat];
875 		m = data->m;
876 		data->m = NULL;
877 		ni = data->ni;
878 		data->ni = NULL;
879 
880 		/* if no frame has been sent, ignore */
881 		if (ni == NULL)
882 			continue;
883 
884 		switch (RT2661_TX_RESULT(val)) {
885 		case RT2661_TX_SUCCESS:
886 			txs->status = IEEE80211_RATECTL_TX_SUCCESS;
887 			txs->long_retries = RT2661_TX_RETRYCNT(val);
888 
889 			DPRINTFN(sc, 10, "data frame sent successfully after "
890 			    "%d retries\n", txs->long_retries);
891 			if (data->rix != IEEE80211_FIXED_RATE_NONE)
892 				ieee80211_ratectl_tx_complete(ni, txs);
893 			error = 0;
894 			break;
895 
896 		case RT2661_TX_RETRY_FAIL:
897 			txs->status = IEEE80211_RATECTL_TX_FAIL_LONG;
898 			txs->long_retries = RT2661_TX_RETRYCNT(val);
899 
900 			DPRINTFN(sc, 9, "%s\n",
901 			    "sending data frame failed (too much retries)");
902 			if (data->rix != IEEE80211_FIXED_RATE_NONE)
903 				ieee80211_ratectl_tx_complete(ni, txs);
904 			error = 1;
905 			break;
906 
907 		default:
908 			/* other failure */
909 			device_printf(sc->sc_dev,
910 			    "sending data frame failed 0x%08x\n", val);
911 			error = 1;
912 		}
913 
914 		DPRINTFN(sc, 15, "tx done q=%d idx=%u\n", qid, txq->stat);
915 
916 		txq->queued--;
917 		if (++txq->stat >= txq->count)	/* faster than % count */
918 			txq->stat = 0;
919 
920 		ieee80211_tx_complete(ni, m, error);
921 	}
922 
923 	sc->sc_tx_timer = 0;
924 
925 	rt2661_start(sc);
926 }
927 
928 static void
929 rt2661_tx_dma_intr(struct rt2661_softc *sc, struct rt2661_tx_ring *txq)
930 {
931 	struct rt2661_tx_desc *desc;
932 	struct rt2661_tx_data *data;
933 
934 	bus_dmamap_sync(txq->desc_dmat, txq->desc_map, BUS_DMASYNC_POSTREAD);
935 
936 	for (;;) {
937 		desc = &txq->desc[txq->next];
938 		data = &txq->data[txq->next];
939 
940 		if ((le32toh(desc->flags) & RT2661_TX_BUSY) ||
941 		    !(le32toh(desc->flags) & RT2661_TX_VALID))
942 			break;
943 
944 		bus_dmamap_sync(txq->data_dmat, data->map,
945 		    BUS_DMASYNC_POSTWRITE);
946 		bus_dmamap_unload(txq->data_dmat, data->map);
947 
948 		/* descriptor is no longer valid */
949 		desc->flags &= ~htole32(RT2661_TX_VALID);
950 
951 		DPRINTFN(sc, 15, "tx dma done q=%p idx=%u\n", txq, txq->next);
952 
953 		if (++txq->next >= txq->count)	/* faster than % count */
954 			txq->next = 0;
955 	}
956 
957 	bus_dmamap_sync(txq->desc_dmat, txq->desc_map, BUS_DMASYNC_PREWRITE);
958 }
959 
960 static void
961 rt2661_rx_intr(struct rt2661_softc *sc)
962 {
963 	struct ieee80211com *ic = &sc->sc_ic;
964 	struct rt2661_rx_desc *desc;
965 	struct rt2661_rx_data *data;
966 	bus_addr_t physaddr;
967 	struct ieee80211_frame *wh;
968 	struct ieee80211_node *ni;
969 	struct mbuf *mnew, *m;
970 	int error;
971 
972 	bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
973 	    BUS_DMASYNC_POSTREAD);
974 
975 	for (;;) {
976 		int8_t rssi, nf;
977 
978 		desc = &sc->rxq.desc[sc->rxq.cur];
979 		data = &sc->rxq.data[sc->rxq.cur];
980 
981 		if (le32toh(desc->flags) & RT2661_RX_BUSY)
982 			break;
983 
984 		if ((le32toh(desc->flags) & RT2661_RX_PHY_ERROR) ||
985 		    (le32toh(desc->flags) & RT2661_RX_CRC_ERROR)) {
986 			/*
987 			 * This should not happen since we did not request
988 			 * to receive those frames when we filled TXRX_CSR0.
989 			 */
990 			DPRINTFN(sc, 5, "PHY or CRC error flags 0x%08x\n",
991 			    le32toh(desc->flags));
992 			counter_u64_add(ic->ic_ierrors, 1);
993 			goto skip;
994 		}
995 
996 		if ((le32toh(desc->flags) & RT2661_RX_CIPHER_MASK) != 0) {
997 			counter_u64_add(ic->ic_ierrors, 1);
998 			goto skip;
999 		}
1000 
1001 		/*
1002 		 * Try to allocate a new mbuf for this ring element and load it
1003 		 * before processing the current mbuf. If the ring element
1004 		 * cannot be loaded, drop the received packet and reuse the old
1005 		 * mbuf. In the unlikely case that the old mbuf can't be
1006 		 * reloaded either, explicitly panic.
1007 		 */
1008 		mnew = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1009 		if (mnew == NULL) {
1010 			counter_u64_add(ic->ic_ierrors, 1);
1011 			goto skip;
1012 		}
1013 
1014 		bus_dmamap_sync(sc->rxq.data_dmat, data->map,
1015 		    BUS_DMASYNC_POSTREAD);
1016 		bus_dmamap_unload(sc->rxq.data_dmat, data->map);
1017 
1018 		error = bus_dmamap_load(sc->rxq.data_dmat, data->map,
1019 		    mtod(mnew, void *), MCLBYTES, rt2661_dma_map_addr,
1020 		    &physaddr, 0);
1021 		if (error != 0) {
1022 			m_freem(mnew);
1023 
1024 			/* try to reload the old mbuf */
1025 			error = bus_dmamap_load(sc->rxq.data_dmat, data->map,
1026 			    mtod(data->m, void *), MCLBYTES,
1027 			    rt2661_dma_map_addr, &physaddr, 0);
1028 			if (error != 0) {
1029 				/* very unlikely that it will fail... */
1030 				panic("%s: could not load old rx mbuf",
1031 				    device_get_name(sc->sc_dev));
1032 			}
1033 			counter_u64_add(ic->ic_ierrors, 1);
1034 			goto skip;
1035 		}
1036 
1037 		/*
1038 	 	 * New mbuf successfully loaded, update Rx ring and continue
1039 		 * processing.
1040 		 */
1041 		m = data->m;
1042 		data->m = mnew;
1043 		desc->physaddr = htole32(physaddr);
1044 
1045 		/* finalize mbuf */
1046 		m->m_pkthdr.len = m->m_len =
1047 		    (le32toh(desc->flags) >> 16) & 0xfff;
1048 
1049 		rssi = rt2661_get_rssi(sc, desc->rssi);
1050 		/* Error happened during RSSI conversion. */
1051 		if (rssi < 0)
1052 			rssi = -30;	/* XXX ignored by net80211 */
1053 		nf = RT2661_NOISE_FLOOR;
1054 
1055 		if (ieee80211_radiotap_active(ic)) {
1056 			struct rt2661_rx_radiotap_header *tap = &sc->sc_rxtap;
1057 			uint32_t tsf_lo, tsf_hi;
1058 
1059 			/* get timestamp (low and high 32 bits) */
1060 			tsf_hi = RAL_READ(sc, RT2661_TXRX_CSR13);
1061 			tsf_lo = RAL_READ(sc, RT2661_TXRX_CSR12);
1062 
1063 			tap->wr_tsf =
1064 			    htole64(((uint64_t)tsf_hi << 32) | tsf_lo);
1065 			tap->wr_flags = 0;
1066 			tap->wr_rate = ieee80211_plcp2rate(desc->rate,
1067 			    (desc->flags & htole32(RT2661_RX_OFDM)) ?
1068 				IEEE80211_T_OFDM : IEEE80211_T_CCK);
1069 			tap->wr_antsignal = nf + rssi;
1070 			tap->wr_antnoise = nf;
1071 		}
1072 		sc->sc_flags |= RAL_INPUT_RUNNING;
1073 		RAL_UNLOCK(sc);
1074 		wh = mtod(m, struct ieee80211_frame *);
1075 
1076 		/* send the frame to the 802.11 layer */
1077 		ni = ieee80211_find_rxnode(ic,
1078 		    (struct ieee80211_frame_min *)wh);
1079 		if (ni != NULL) {
1080 			(void) ieee80211_input(ni, m, rssi, nf);
1081 			ieee80211_free_node(ni);
1082 		} else
1083 			(void) ieee80211_input_all(ic, m, rssi, nf);
1084 
1085 		RAL_LOCK(sc);
1086 		sc->sc_flags &= ~RAL_INPUT_RUNNING;
1087 
1088 skip:		desc->flags |= htole32(RT2661_RX_BUSY);
1089 
1090 		DPRINTFN(sc, 15, "rx intr idx=%u\n", sc->rxq.cur);
1091 
1092 		sc->rxq.cur = (sc->rxq.cur + 1) % RT2661_RX_RING_COUNT;
1093 	}
1094 
1095 	bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1096 	    BUS_DMASYNC_PREWRITE);
1097 }
1098 
1099 /* ARGSUSED */
1100 static void
1101 rt2661_mcu_beacon_expire(struct rt2661_softc *sc)
1102 {
1103 	/* do nothing */
1104 }
1105 
1106 static void
1107 rt2661_mcu_wakeup(struct rt2661_softc *sc)
1108 {
1109 	RAL_WRITE(sc, RT2661_MAC_CSR11, 5 << 16);
1110 
1111 	RAL_WRITE(sc, RT2661_SOFT_RESET_CSR, 0x7);
1112 	RAL_WRITE(sc, RT2661_IO_CNTL_CSR, 0x18);
1113 	RAL_WRITE(sc, RT2661_PCI_USEC_CSR, 0x20);
1114 
1115 	/* send wakeup command to MCU */
1116 	rt2661_tx_cmd(sc, RT2661_MCU_CMD_WAKEUP, 0);
1117 }
1118 
1119 static void
1120 rt2661_mcu_cmd_intr(struct rt2661_softc *sc)
1121 {
1122 	RAL_READ(sc, RT2661_M2H_CMD_DONE_CSR);
1123 	RAL_WRITE(sc, RT2661_M2H_CMD_DONE_CSR, 0xffffffff);
1124 }
1125 
1126 void
1127 rt2661_intr(void *arg)
1128 {
1129 	struct rt2661_softc *sc = arg;
1130 	uint32_t r1, r2;
1131 
1132 	RAL_LOCK(sc);
1133 
1134 	/* disable MAC and MCU interrupts */
1135 	RAL_WRITE(sc, RT2661_INT_MASK_CSR, 0xffffff7f);
1136 	RAL_WRITE(sc, RT2661_MCU_INT_MASK_CSR, 0xffffffff);
1137 
1138 	/* don't re-enable interrupts if we're shutting down */
1139 	if (!(sc->sc_flags & RAL_RUNNING)) {
1140 		RAL_UNLOCK(sc);
1141 		return;
1142 	}
1143 
1144 	r1 = RAL_READ(sc, RT2661_INT_SOURCE_CSR);
1145 	RAL_WRITE(sc, RT2661_INT_SOURCE_CSR, r1);
1146 
1147 	r2 = RAL_READ(sc, RT2661_MCU_INT_SOURCE_CSR);
1148 	RAL_WRITE(sc, RT2661_MCU_INT_SOURCE_CSR, r2);
1149 
1150 	if (r1 & RT2661_MGT_DONE)
1151 		rt2661_tx_dma_intr(sc, &sc->mgtq);
1152 
1153 	if (r1 & RT2661_RX_DONE)
1154 		rt2661_rx_intr(sc);
1155 
1156 	if (r1 & RT2661_TX0_DMA_DONE)
1157 		rt2661_tx_dma_intr(sc, &sc->txq[0]);
1158 
1159 	if (r1 & RT2661_TX1_DMA_DONE)
1160 		rt2661_tx_dma_intr(sc, &sc->txq[1]);
1161 
1162 	if (r1 & RT2661_TX2_DMA_DONE)
1163 		rt2661_tx_dma_intr(sc, &sc->txq[2]);
1164 
1165 	if (r1 & RT2661_TX3_DMA_DONE)
1166 		rt2661_tx_dma_intr(sc, &sc->txq[3]);
1167 
1168 	if (r1 & RT2661_TX_DONE)
1169 		rt2661_tx_intr(sc);
1170 
1171 	if (r2 & RT2661_MCU_CMD_DONE)
1172 		rt2661_mcu_cmd_intr(sc);
1173 
1174 	if (r2 & RT2661_MCU_BEACON_EXPIRE)
1175 		rt2661_mcu_beacon_expire(sc);
1176 
1177 	if (r2 & RT2661_MCU_WAKEUP)
1178 		rt2661_mcu_wakeup(sc);
1179 
1180 	/* re-enable MAC and MCU interrupts */
1181 	RAL_WRITE(sc, RT2661_INT_MASK_CSR, 0x0000ff10);
1182 	RAL_WRITE(sc, RT2661_MCU_INT_MASK_CSR, 0);
1183 
1184 	RAL_UNLOCK(sc);
1185 }
1186 
1187 static uint8_t
1188 rt2661_plcp_signal(int rate)
1189 {
1190 	switch (rate) {
1191 	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1192 	case 12:	return 0xb;
1193 	case 18:	return 0xf;
1194 	case 24:	return 0xa;
1195 	case 36:	return 0xe;
1196 	case 48:	return 0x9;
1197 	case 72:	return 0xd;
1198 	case 96:	return 0x8;
1199 	case 108:	return 0xc;
1200 
1201 	/* CCK rates (NB: not IEEE std, device-specific) */
1202 	case 2:		return 0x0;
1203 	case 4:		return 0x1;
1204 	case 11:	return 0x2;
1205 	case 22:	return 0x3;
1206 	}
1207 	return 0xff;		/* XXX unsupported/unknown rate */
1208 }
1209 
1210 static void
1211 rt2661_setup_tx_desc(struct rt2661_softc *sc, struct rt2661_tx_desc *desc,
1212     uint32_t flags, uint16_t xflags, int len, int rate,
1213     const bus_dma_segment_t *segs, int nsegs, int ac)
1214 {
1215 	struct ieee80211com *ic = &sc->sc_ic;
1216 	uint16_t plcp_length;
1217 	int i, remainder;
1218 
1219 	desc->flags = htole32(flags);
1220 	desc->flags |= htole32(len << 16);
1221 	desc->flags |= htole32(RT2661_TX_BUSY | RT2661_TX_VALID);
1222 
1223 	desc->xflags = htole16(xflags);
1224 	desc->xflags |= htole16(nsegs << 13);
1225 
1226 	desc->wme = htole16(
1227 	    RT2661_QID(ac) |
1228 	    RT2661_AIFSN(2) |
1229 	    RT2661_LOGCWMIN(4) |
1230 	    RT2661_LOGCWMAX(10));
1231 
1232 	/*
1233 	 * Remember in which queue this frame was sent. This field is driver
1234 	 * private data only. It will be made available by the NIC in STA_CSR4
1235 	 * on Tx interrupts.
1236 	 */
1237 	desc->qid = ac;
1238 
1239 	/* setup PLCP fields */
1240 	desc->plcp_signal  = rt2661_plcp_signal(rate);
1241 	desc->plcp_service = 4;
1242 
1243 	len += IEEE80211_CRC_LEN;
1244 	if (ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) {
1245 		desc->flags |= htole32(RT2661_TX_OFDM);
1246 
1247 		plcp_length = len & 0xfff;
1248 		desc->plcp_length_hi = plcp_length >> 6;
1249 		desc->plcp_length_lo = plcp_length & 0x3f;
1250 	} else {
1251 		plcp_length = howmany(16 * len, rate);
1252 		if (rate == 22) {
1253 			remainder = (16 * len) % 22;
1254 			if (remainder != 0 && remainder < 7)
1255 				desc->plcp_service |= RT2661_PLCP_LENGEXT;
1256 		}
1257 		desc->plcp_length_hi = plcp_length >> 8;
1258 		desc->plcp_length_lo = plcp_length & 0xff;
1259 
1260 		if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1261 			desc->plcp_signal |= 0x08;
1262 	}
1263 
1264 	/* RT2x61 supports scatter with up to 5 segments */
1265 	for (i = 0; i < nsegs; i++) {
1266 		desc->addr[i] = htole32(segs[i].ds_addr);
1267 		desc->len [i] = htole16(segs[i].ds_len);
1268 	}
1269 }
1270 
1271 static int
1272 rt2661_tx_mgt(struct rt2661_softc *sc, struct mbuf *m0,
1273     struct ieee80211_node *ni)
1274 {
1275 	struct ieee80211vap *vap = ni->ni_vap;
1276 	struct ieee80211com *ic = ni->ni_ic;
1277 	struct rt2661_tx_desc *desc;
1278 	struct rt2661_tx_data *data;
1279 	struct ieee80211_frame *wh;
1280 	struct ieee80211_key *k;
1281 	bus_dma_segment_t segs[RT2661_MAX_SCATTER];
1282 	uint16_t dur;
1283 	uint32_t flags = 0;	/* XXX HWSEQ */
1284 	int nsegs, rate, error;
1285 
1286 	desc = &sc->mgtq.desc[sc->mgtq.cur];
1287 	data = &sc->mgtq.data[sc->mgtq.cur];
1288 
1289 	rate = ni->ni_txparms->mgmtrate;
1290 
1291 	wh = mtod(m0, struct ieee80211_frame *);
1292 
1293 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1294 		k = ieee80211_crypto_encap(ni, m0);
1295 		if (k == NULL) {
1296 			m_freem(m0);
1297 			return ENOBUFS;
1298 		}
1299 	}
1300 
1301 	error = bus_dmamap_load_mbuf_sg(sc->mgtq.data_dmat, data->map, m0,
1302 	    segs, &nsegs, 0);
1303 	if (error != 0) {
1304 		device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1305 		    error);
1306 		m_freem(m0);
1307 		return error;
1308 	}
1309 
1310 	if (ieee80211_radiotap_active_vap(vap)) {
1311 		struct rt2661_tx_radiotap_header *tap = &sc->sc_txtap;
1312 
1313 		tap->wt_flags = 0;
1314 		tap->wt_rate = rate;
1315 
1316 		ieee80211_radiotap_tx(vap, m0);
1317 	}
1318 
1319 	data->m = m0;
1320 	data->ni = ni;
1321 	/* management frames are not taken into account for amrr */
1322 	data->rix = IEEE80211_FIXED_RATE_NONE;
1323 
1324 	wh = mtod(m0, struct ieee80211_frame *);
1325 
1326 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1327 		flags |= RT2661_TX_NEED_ACK;
1328 
1329 		dur = ieee80211_ack_duration(ic->ic_rt,
1330 		    rate, ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1331 		*(uint16_t *)wh->i_dur = htole16(dur);
1332 
1333 		/* tell hardware to add timestamp in probe responses */
1334 		if ((wh->i_fc[0] &
1335 		    (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
1336 		    (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
1337 			flags |= RT2661_TX_TIMESTAMP;
1338 	}
1339 
1340 	rt2661_setup_tx_desc(sc, desc, flags, 0 /* XXX HWSEQ */,
1341 	    m0->m_pkthdr.len, rate, segs, nsegs, RT2661_QID_MGT);
1342 
1343 	bus_dmamap_sync(sc->mgtq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1344 	bus_dmamap_sync(sc->mgtq.desc_dmat, sc->mgtq.desc_map,
1345 	    BUS_DMASYNC_PREWRITE);
1346 
1347 	DPRINTFN(sc, 10, "sending mgt frame len=%u idx=%u rate=%u\n",
1348 	    m0->m_pkthdr.len, sc->mgtq.cur, rate);
1349 
1350 	/* kick mgt */
1351 	sc->mgtq.queued++;
1352 	sc->mgtq.cur = (sc->mgtq.cur + 1) % RT2661_MGT_RING_COUNT;
1353 	RAL_WRITE(sc, RT2661_TX_CNTL_CSR, RT2661_KICK_MGT);
1354 
1355 	return 0;
1356 }
1357 
1358 static int
1359 rt2661_sendprot(struct rt2661_softc *sc, int ac,
1360     const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate)
1361 {
1362 	struct ieee80211com *ic = ni->ni_ic;
1363 	struct rt2661_tx_ring *txq = &sc->txq[ac];
1364 	const struct ieee80211_frame *wh;
1365 	struct rt2661_tx_desc *desc;
1366 	struct rt2661_tx_data *data;
1367 	struct mbuf *mprot;
1368 	int protrate, ackrate, pktlen, flags, isshort, error;
1369 	uint16_t dur;
1370 	bus_dma_segment_t segs[RT2661_MAX_SCATTER];
1371 	int nsegs;
1372 
1373 	KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY,
1374 	    ("protection %d", prot));
1375 
1376 	wh = mtod(m, const struct ieee80211_frame *);
1377 	pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
1378 
1379 	protrate = ieee80211_ctl_rate(ic->ic_rt, rate);
1380 	ackrate = ieee80211_ack_rate(ic->ic_rt, rate);
1381 
1382 	isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0;
1383 	dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort)
1384 	    + ieee80211_ack_duration(ic->ic_rt, rate, isshort);
1385 	flags = RT2661_TX_MORE_FRAG;
1386 	if (prot == IEEE80211_PROT_RTSCTS) {
1387 		/* NB: CTS is the same size as an ACK */
1388 		dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort);
1389 		flags |= RT2661_TX_NEED_ACK;
1390 		mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur);
1391 	} else {
1392 		mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur);
1393 	}
1394 	if (mprot == NULL) {
1395 		/* XXX stat + msg */
1396 		return ENOBUFS;
1397 	}
1398 
1399 	data = &txq->data[txq->cur];
1400 	desc = &txq->desc[txq->cur];
1401 
1402 	error = bus_dmamap_load_mbuf_sg(txq->data_dmat, data->map, mprot, segs,
1403 	    &nsegs, 0);
1404 	if (error != 0) {
1405 		device_printf(sc->sc_dev,
1406 		    "could not map mbuf (error %d)\n", error);
1407 		m_freem(mprot);
1408 		return error;
1409 	}
1410 
1411 	data->m = mprot;
1412 	data->ni = ieee80211_ref_node(ni);
1413 	/* ctl frames are not taken into account for amrr */
1414 	data->rix = IEEE80211_FIXED_RATE_NONE;
1415 
1416 	rt2661_setup_tx_desc(sc, desc, flags, 0, mprot->m_pkthdr.len,
1417 	    protrate, segs, 1, ac);
1418 
1419 	bus_dmamap_sync(txq->data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1420 	bus_dmamap_sync(txq->desc_dmat, txq->desc_map, BUS_DMASYNC_PREWRITE);
1421 
1422 	txq->queued++;
1423 	txq->cur = (txq->cur + 1) % RT2661_TX_RING_COUNT;
1424 
1425 	return 0;
1426 }
1427 
1428 static int
1429 rt2661_tx_data(struct rt2661_softc *sc, struct mbuf *m0,
1430     struct ieee80211_node *ni, int ac)
1431 {
1432 	struct ieee80211vap *vap = ni->ni_vap;
1433 	struct ieee80211com *ic = &sc->sc_ic;
1434 	struct rt2661_tx_ring *txq = &sc->txq[ac];
1435 	struct rt2661_tx_desc *desc;
1436 	struct rt2661_tx_data *data;
1437 	struct ieee80211_frame *wh;
1438 	const struct ieee80211_txparam *tp = ni->ni_txparms;
1439 	struct ieee80211_key *k;
1440 	const struct chanAccParams *cap;
1441 	struct mbuf *mnew;
1442 	bus_dma_segment_t segs[RT2661_MAX_SCATTER];
1443 	uint16_t dur;
1444 	uint32_t flags;
1445 	int error, nsegs, rate, noack = 0;
1446 
1447 	wh = mtod(m0, struct ieee80211_frame *);
1448 
1449 	if (m0->m_flags & M_EAPOL) {
1450 		rate = tp->mgmtrate;
1451 	} else if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1452 		rate = tp->mcastrate;
1453 	} else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) {
1454 		rate = tp->ucastrate;
1455 	} else {
1456 		(void) ieee80211_ratectl_rate(ni, NULL, 0);
1457 		rate = ni->ni_txrate;
1458 	}
1459 	rate &= IEEE80211_RATE_VAL;
1460 
1461 	if (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_QOS) {
1462 		cap = &ic->ic_wme.wme_chanParams;
1463 		noack = cap->cap_wmeParams[ac].wmep_noackPolicy;
1464 	}
1465 
1466 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1467 		k = ieee80211_crypto_encap(ni, m0);
1468 		if (k == NULL) {
1469 			m_freem(m0);
1470 			return ENOBUFS;
1471 		}
1472 
1473 		/* packet header may have moved, reset our local pointer */
1474 		wh = mtod(m0, struct ieee80211_frame *);
1475 	}
1476 
1477 	flags = 0;
1478 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1479 		int prot = IEEE80211_PROT_NONE;
1480 		if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold)
1481 			prot = IEEE80211_PROT_RTSCTS;
1482 		else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
1483 		    ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM)
1484 			prot = ic->ic_protmode;
1485 		if (prot != IEEE80211_PROT_NONE) {
1486 			error = rt2661_sendprot(sc, ac, m0, ni, prot, rate);
1487 			if (error) {
1488 				m_freem(m0);
1489 				return error;
1490 			}
1491 			flags |= RT2661_TX_LONG_RETRY | RT2661_TX_IFS;
1492 		}
1493 	}
1494 
1495 	data = &txq->data[txq->cur];
1496 	desc = &txq->desc[txq->cur];
1497 
1498 	error = bus_dmamap_load_mbuf_sg(txq->data_dmat, data->map, m0, segs,
1499 	    &nsegs, 0);
1500 	if (error != 0 && error != EFBIG) {
1501 		device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1502 		    error);
1503 		m_freem(m0);
1504 		return error;
1505 	}
1506 	if (error != 0) {
1507 		mnew = m_defrag(m0, M_NOWAIT);
1508 		if (mnew == NULL) {
1509 			device_printf(sc->sc_dev,
1510 			    "could not defragment mbuf\n");
1511 			m_freem(m0);
1512 			return ENOBUFS;
1513 		}
1514 		m0 = mnew;
1515 
1516 		error = bus_dmamap_load_mbuf_sg(txq->data_dmat, data->map, m0,
1517 		    segs, &nsegs, 0);
1518 		if (error != 0) {
1519 			device_printf(sc->sc_dev,
1520 			    "could not map mbuf (error %d)\n", error);
1521 			m_freem(m0);
1522 			return error;
1523 		}
1524 
1525 		/* packet header have moved, reset our local pointer */
1526 		wh = mtod(m0, struct ieee80211_frame *);
1527 	}
1528 
1529 	if (ieee80211_radiotap_active_vap(vap)) {
1530 		struct rt2661_tx_radiotap_header *tap = &sc->sc_txtap;
1531 
1532 		tap->wt_flags = 0;
1533 		tap->wt_rate = rate;
1534 
1535 		ieee80211_radiotap_tx(vap, m0);
1536 	}
1537 
1538 	data->m = m0;
1539 	data->ni = ni;
1540 
1541 	/* remember link conditions for rate adaptation algorithm */
1542 	if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE) {
1543 		data->rix = ni->ni_txrate;
1544 		/* XXX probably need last rssi value and not avg */
1545 		data->rssi = ic->ic_node_getrssi(ni);
1546 	} else
1547 		data->rix = IEEE80211_FIXED_RATE_NONE;
1548 
1549 	if (!noack && !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1550 		flags |= RT2661_TX_NEED_ACK;
1551 
1552 		dur = ieee80211_ack_duration(ic->ic_rt,
1553 		    rate, ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1554 		*(uint16_t *)wh->i_dur = htole16(dur);
1555 	}
1556 
1557 	rt2661_setup_tx_desc(sc, desc, flags, 0, m0->m_pkthdr.len, rate, segs,
1558 	    nsegs, ac);
1559 
1560 	bus_dmamap_sync(txq->data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1561 	bus_dmamap_sync(txq->desc_dmat, txq->desc_map, BUS_DMASYNC_PREWRITE);
1562 
1563 	DPRINTFN(sc, 10, "sending data frame len=%u idx=%u rate=%u\n",
1564 	    m0->m_pkthdr.len, txq->cur, rate);
1565 
1566 	/* kick Tx */
1567 	txq->queued++;
1568 	txq->cur = (txq->cur + 1) % RT2661_TX_RING_COUNT;
1569 	RAL_WRITE(sc, RT2661_TX_CNTL_CSR, 1 << ac);
1570 
1571 	return 0;
1572 }
1573 
1574 static int
1575 rt2661_transmit(struct ieee80211com *ic, struct mbuf *m)
1576 {
1577 	struct rt2661_softc *sc = ic->ic_softc;
1578 	int error;
1579 
1580 	RAL_LOCK(sc);
1581 	if ((sc->sc_flags & RAL_RUNNING) == 0) {
1582 		RAL_UNLOCK(sc);
1583 		return (ENXIO);
1584 	}
1585 	error = mbufq_enqueue(&sc->sc_snd, m);
1586 	if (error) {
1587 		RAL_UNLOCK(sc);
1588 		return (error);
1589 	}
1590 	rt2661_start(sc);
1591 	RAL_UNLOCK(sc);
1592 
1593 	return (0);
1594 }
1595 
1596 static void
1597 rt2661_start(struct rt2661_softc *sc)
1598 {
1599 	struct mbuf *m;
1600 	struct ieee80211_node *ni;
1601 	int ac;
1602 
1603 	RAL_LOCK_ASSERT(sc);
1604 
1605 	/* prevent management frames from being sent if we're not ready */
1606 	if (!(sc->sc_flags & RAL_RUNNING) || sc->sc_invalid)
1607 		return;
1608 
1609 	while ((m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
1610 		ac = M_WME_GETAC(m);
1611 		if (sc->txq[ac].queued >= RT2661_TX_RING_COUNT - 1) {
1612 			/* there is no place left in this ring */
1613 			mbufq_prepend(&sc->sc_snd, m);
1614 			break;
1615 		}
1616 		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1617 		if (rt2661_tx_data(sc, m, ni, ac) != 0) {
1618 			if_inc_counter(ni->ni_vap->iv_ifp,
1619 			    IFCOUNTER_OERRORS, 1);
1620 			ieee80211_free_node(ni);
1621 			break;
1622 		}
1623 		sc->sc_tx_timer = 5;
1624 	}
1625 }
1626 
1627 static int
1628 rt2661_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
1629 	const struct ieee80211_bpf_params *params)
1630 {
1631 	struct ieee80211com *ic = ni->ni_ic;
1632 	struct rt2661_softc *sc = ic->ic_softc;
1633 
1634 	RAL_LOCK(sc);
1635 
1636 	/* prevent management frames from being sent if we're not ready */
1637 	if (!(sc->sc_flags & RAL_RUNNING)) {
1638 		RAL_UNLOCK(sc);
1639 		m_freem(m);
1640 		return ENETDOWN;
1641 	}
1642 	if (sc->mgtq.queued >= RT2661_MGT_RING_COUNT) {
1643 		RAL_UNLOCK(sc);
1644 		m_freem(m);
1645 		return ENOBUFS;		/* XXX */
1646 	}
1647 
1648 	/*
1649 	 * Legacy path; interpret frame contents to decide
1650 	 * precisely how to send the frame.
1651 	 * XXX raw path
1652 	 */
1653 	if (rt2661_tx_mgt(sc, m, ni) != 0)
1654 		goto bad;
1655 	sc->sc_tx_timer = 5;
1656 
1657 	RAL_UNLOCK(sc);
1658 
1659 	return 0;
1660 bad:
1661 	RAL_UNLOCK(sc);
1662 	return EIO;		/* XXX */
1663 }
1664 
1665 static void
1666 rt2661_watchdog(void *arg)
1667 {
1668 	struct rt2661_softc *sc = (struct rt2661_softc *)arg;
1669 
1670 	RAL_LOCK_ASSERT(sc);
1671 
1672 	KASSERT(sc->sc_flags & RAL_RUNNING, ("not running"));
1673 
1674 	if (sc->sc_invalid)		/* card ejected */
1675 		return;
1676 
1677 	if (sc->sc_tx_timer > 0 && --sc->sc_tx_timer == 0) {
1678 		device_printf(sc->sc_dev, "device timeout\n");
1679 		rt2661_init_locked(sc);
1680 		counter_u64_add(sc->sc_ic.ic_oerrors, 1);
1681 		/* NB: callout is reset in rt2661_init() */
1682 		return;
1683 	}
1684 	callout_reset(&sc->watchdog_ch, hz, rt2661_watchdog, sc);
1685 }
1686 
1687 static void
1688 rt2661_parent(struct ieee80211com *ic)
1689 {
1690 	struct rt2661_softc *sc = ic->ic_softc;
1691 	int startall = 0;
1692 
1693 	RAL_LOCK(sc);
1694 	if (ic->ic_nrunning > 0) {
1695 		if ((sc->sc_flags & RAL_RUNNING) == 0) {
1696 			rt2661_init_locked(sc);
1697 			startall = 1;
1698 		} else
1699 			rt2661_update_promisc(ic);
1700 	} else if (sc->sc_flags & RAL_RUNNING)
1701 		rt2661_stop_locked(sc);
1702 	RAL_UNLOCK(sc);
1703 	if (startall)
1704 		ieee80211_start_all(ic);
1705 }
1706 
1707 static void
1708 rt2661_bbp_write(struct rt2661_softc *sc, uint8_t reg, uint8_t val)
1709 {
1710 	uint32_t tmp;
1711 	int ntries;
1712 
1713 	for (ntries = 0; ntries < 100; ntries++) {
1714 		if (!(RAL_READ(sc, RT2661_PHY_CSR3) & RT2661_BBP_BUSY))
1715 			break;
1716 		DELAY(1);
1717 	}
1718 	if (ntries == 100) {
1719 		device_printf(sc->sc_dev, "could not write to BBP\n");
1720 		return;
1721 	}
1722 
1723 	tmp = RT2661_BBP_BUSY | (reg & 0x7f) << 8 | val;
1724 	RAL_WRITE(sc, RT2661_PHY_CSR3, tmp);
1725 
1726 	DPRINTFN(sc, 15, "BBP R%u <- 0x%02x\n", reg, val);
1727 }
1728 
1729 static uint8_t
1730 rt2661_bbp_read(struct rt2661_softc *sc, uint8_t reg)
1731 {
1732 	uint32_t val;
1733 	int ntries;
1734 
1735 	for (ntries = 0; ntries < 100; ntries++) {
1736 		if (!(RAL_READ(sc, RT2661_PHY_CSR3) & RT2661_BBP_BUSY))
1737 			break;
1738 		DELAY(1);
1739 	}
1740 	if (ntries == 100) {
1741 		device_printf(sc->sc_dev, "could not read from BBP\n");
1742 		return 0;
1743 	}
1744 
1745 	val = RT2661_BBP_BUSY | RT2661_BBP_READ | reg << 8;
1746 	RAL_WRITE(sc, RT2661_PHY_CSR3, val);
1747 
1748 	for (ntries = 0; ntries < 100; ntries++) {
1749 		val = RAL_READ(sc, RT2661_PHY_CSR3);
1750 		if (!(val & RT2661_BBP_BUSY))
1751 			return val & 0xff;
1752 		DELAY(1);
1753 	}
1754 
1755 	device_printf(sc->sc_dev, "could not read from BBP\n");
1756 	return 0;
1757 }
1758 
1759 static void
1760 rt2661_rf_write(struct rt2661_softc *sc, uint8_t reg, uint32_t val)
1761 {
1762 	uint32_t tmp;
1763 	int ntries;
1764 
1765 	for (ntries = 0; ntries < 100; ntries++) {
1766 		if (!(RAL_READ(sc, RT2661_PHY_CSR4) & RT2661_RF_BUSY))
1767 			break;
1768 		DELAY(1);
1769 	}
1770 	if (ntries == 100) {
1771 		device_printf(sc->sc_dev, "could not write to RF\n");
1772 		return;
1773 	}
1774 
1775 	tmp = RT2661_RF_BUSY | RT2661_RF_21BIT | (val & 0x1fffff) << 2 |
1776 	    (reg & 3);
1777 	RAL_WRITE(sc, RT2661_PHY_CSR4, tmp);
1778 
1779 	/* remember last written value in sc */
1780 	sc->rf_regs[reg] = val;
1781 
1782 	DPRINTFN(sc, 15, "RF R[%u] <- 0x%05x\n", reg & 3, val & 0x1fffff);
1783 }
1784 
1785 static int
1786 rt2661_tx_cmd(struct rt2661_softc *sc, uint8_t cmd, uint16_t arg)
1787 {
1788 	if (RAL_READ(sc, RT2661_H2M_MAILBOX_CSR) & RT2661_H2M_BUSY)
1789 		return EIO;	/* there is already a command pending */
1790 
1791 	RAL_WRITE(sc, RT2661_H2M_MAILBOX_CSR,
1792 	    RT2661_H2M_BUSY | RT2661_TOKEN_NO_INTR << 16 | arg);
1793 
1794 	RAL_WRITE(sc, RT2661_HOST_CMD_CSR, RT2661_KICK_CMD | cmd);
1795 
1796 	return 0;
1797 }
1798 
1799 static void
1800 rt2661_select_antenna(struct rt2661_softc *sc)
1801 {
1802 	uint8_t bbp4, bbp77;
1803 	uint32_t tmp;
1804 
1805 	bbp4  = rt2661_bbp_read(sc,  4);
1806 	bbp77 = rt2661_bbp_read(sc, 77);
1807 
1808 	/* TBD */
1809 
1810 	/* make sure Rx is disabled before switching antenna */
1811 	tmp = RAL_READ(sc, RT2661_TXRX_CSR0);
1812 	RAL_WRITE(sc, RT2661_TXRX_CSR0, tmp | RT2661_DISABLE_RX);
1813 
1814 	rt2661_bbp_write(sc,  4, bbp4);
1815 	rt2661_bbp_write(sc, 77, bbp77);
1816 
1817 	/* restore Rx filter */
1818 	RAL_WRITE(sc, RT2661_TXRX_CSR0, tmp);
1819 }
1820 
1821 /*
1822  * Enable multi-rate retries for frames sent at OFDM rates.
1823  * In 802.11b/g mode, allow fallback to CCK rates.
1824  */
1825 static void
1826 rt2661_enable_mrr(struct rt2661_softc *sc)
1827 {
1828 	struct ieee80211com *ic = &sc->sc_ic;
1829 	uint32_t tmp;
1830 
1831 	tmp = RAL_READ(sc, RT2661_TXRX_CSR4);
1832 
1833 	tmp &= ~RT2661_MRR_CCK_FALLBACK;
1834 	if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan))
1835 		tmp |= RT2661_MRR_CCK_FALLBACK;
1836 	tmp |= RT2661_MRR_ENABLED;
1837 
1838 	RAL_WRITE(sc, RT2661_TXRX_CSR4, tmp);
1839 }
1840 
1841 static void
1842 rt2661_set_txpreamble(struct rt2661_softc *sc)
1843 {
1844 	struct ieee80211com *ic = &sc->sc_ic;
1845 	uint32_t tmp;
1846 
1847 	tmp = RAL_READ(sc, RT2661_TXRX_CSR4);
1848 
1849 	tmp &= ~RT2661_SHORT_PREAMBLE;
1850 	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
1851 		tmp |= RT2661_SHORT_PREAMBLE;
1852 
1853 	RAL_WRITE(sc, RT2661_TXRX_CSR4, tmp);
1854 }
1855 
1856 static void
1857 rt2661_set_basicrates(struct rt2661_softc *sc,
1858     const struct ieee80211_rateset *rs)
1859 {
1860 	struct ieee80211com *ic = &sc->sc_ic;
1861 	uint32_t mask = 0;
1862 	uint8_t rate;
1863 	int i;
1864 
1865 	for (i = 0; i < rs->rs_nrates; i++) {
1866 		rate = rs->rs_rates[i];
1867 
1868 		if (!(rate & IEEE80211_RATE_BASIC))
1869 			continue;
1870 
1871 		mask |= 1 << ieee80211_legacy_rate_lookup(ic->ic_rt,
1872 		    IEEE80211_RV(rate));
1873 	}
1874 
1875 	RAL_WRITE(sc, RT2661_TXRX_CSR5, mask);
1876 
1877 	DPRINTF(sc, "Setting basic rate mask to 0x%x\n", mask);
1878 }
1879 
1880 /*
1881  * Reprogram MAC/BBP to switch to a new band.  Values taken from the reference
1882  * driver.
1883  */
1884 static void
1885 rt2661_select_band(struct rt2661_softc *sc, struct ieee80211_channel *c)
1886 {
1887 	uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104;
1888 	uint32_t tmp;
1889 
1890 	/* update all BBP registers that depend on the band */
1891 	bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c;
1892 	bbp35 = 0x50; bbp97 = 0x48; bbp98  = 0x48;
1893 	if (IEEE80211_IS_CHAN_5GHZ(c)) {
1894 		bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c;
1895 		bbp35 += 0x10; bbp97 += 0x10; bbp98  += 0x10;
1896 	}
1897 	if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1898 	    (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1899 		bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10;
1900 	}
1901 
1902 	rt2661_bbp_write(sc,  17, bbp17);
1903 	rt2661_bbp_write(sc,  96, bbp96);
1904 	rt2661_bbp_write(sc, 104, bbp104);
1905 
1906 	if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1907 	    (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1908 		rt2661_bbp_write(sc, 75, 0x80);
1909 		rt2661_bbp_write(sc, 86, 0x80);
1910 		rt2661_bbp_write(sc, 88, 0x80);
1911 	}
1912 
1913 	rt2661_bbp_write(sc, 35, bbp35);
1914 	rt2661_bbp_write(sc, 97, bbp97);
1915 	rt2661_bbp_write(sc, 98, bbp98);
1916 
1917 	tmp = RAL_READ(sc, RT2661_PHY_CSR0);
1918 	tmp &= ~(RT2661_PA_PE_2GHZ | RT2661_PA_PE_5GHZ);
1919 	if (IEEE80211_IS_CHAN_2GHZ(c))
1920 		tmp |= RT2661_PA_PE_2GHZ;
1921 	else
1922 		tmp |= RT2661_PA_PE_5GHZ;
1923 	RAL_WRITE(sc, RT2661_PHY_CSR0, tmp);
1924 }
1925 
1926 static void
1927 rt2661_set_chan(struct rt2661_softc *sc, struct ieee80211_channel *c)
1928 {
1929 	struct ieee80211com *ic = &sc->sc_ic;
1930 	const struct rfprog *rfprog;
1931 	uint8_t bbp3, bbp94 = RT2661_BBPR94_DEFAULT;
1932 	int8_t power;
1933 	u_int i, chan;
1934 
1935 	chan = ieee80211_chan2ieee(ic, c);
1936 	KASSERT(chan != 0 && chan != IEEE80211_CHAN_ANY, ("chan 0x%x", chan));
1937 
1938 	/* select the appropriate RF settings based on what EEPROM says */
1939 	rfprog = (sc->rfprog == 0) ? rt2661_rf5225_1 : rt2661_rf5225_2;
1940 
1941 	/* find the settings for this channel (we know it exists) */
1942 	for (i = 0; rfprog[i].chan != chan; i++);
1943 
1944 	power = sc->txpow[i];
1945 	if (power < 0) {
1946 		bbp94 += power;
1947 		power = 0;
1948 	} else if (power > 31) {
1949 		bbp94 += power - 31;
1950 		power = 31;
1951 	}
1952 
1953 	/*
1954 	 * If we are switching from the 2GHz band to the 5GHz band or
1955 	 * vice-versa, BBP registers need to be reprogrammed.
1956 	 */
1957 	if (c->ic_flags != sc->sc_curchan->ic_flags) {
1958 		rt2661_select_band(sc, c);
1959 		rt2661_select_antenna(sc);
1960 	}
1961 	sc->sc_curchan = c;
1962 
1963 	rt2661_rf_write(sc, RAL_RF1, rfprog[i].r1);
1964 	rt2661_rf_write(sc, RAL_RF2, rfprog[i].r2);
1965 	rt2661_rf_write(sc, RAL_RF3, rfprog[i].r3 | power << 7);
1966 	rt2661_rf_write(sc, RAL_RF4, rfprog[i].r4 | sc->rffreq << 10);
1967 
1968 	DELAY(200);
1969 
1970 	rt2661_rf_write(sc, RAL_RF1, rfprog[i].r1);
1971 	rt2661_rf_write(sc, RAL_RF2, rfprog[i].r2);
1972 	rt2661_rf_write(sc, RAL_RF3, rfprog[i].r3 | power << 7 | 1);
1973 	rt2661_rf_write(sc, RAL_RF4, rfprog[i].r4 | sc->rffreq << 10);
1974 
1975 	DELAY(200);
1976 
1977 	rt2661_rf_write(sc, RAL_RF1, rfprog[i].r1);
1978 	rt2661_rf_write(sc, RAL_RF2, rfprog[i].r2);
1979 	rt2661_rf_write(sc, RAL_RF3, rfprog[i].r3 | power << 7);
1980 	rt2661_rf_write(sc, RAL_RF4, rfprog[i].r4 | sc->rffreq << 10);
1981 
1982 	/* enable smart mode for MIMO-capable RFs */
1983 	bbp3 = rt2661_bbp_read(sc, 3);
1984 
1985 	bbp3 &= ~RT2661_SMART_MODE;
1986 	if (sc->rf_rev == RT2661_RF_5325 || sc->rf_rev == RT2661_RF_2529)
1987 		bbp3 |= RT2661_SMART_MODE;
1988 
1989 	rt2661_bbp_write(sc, 3, bbp3);
1990 
1991 	if (bbp94 != RT2661_BBPR94_DEFAULT)
1992 		rt2661_bbp_write(sc, 94, bbp94);
1993 
1994 	/* 5GHz radio needs a 1ms delay here */
1995 	if (IEEE80211_IS_CHAN_5GHZ(c))
1996 		DELAY(1000);
1997 }
1998 
1999 static void
2000 rt2661_set_bssid(struct rt2661_softc *sc, const uint8_t *bssid)
2001 {
2002 	uint32_t tmp;
2003 
2004 	tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24;
2005 	RAL_WRITE(sc, RT2661_MAC_CSR4, tmp);
2006 
2007 	tmp = bssid[4] | bssid[5] << 8 | RT2661_ONE_BSSID << 16;
2008 	RAL_WRITE(sc, RT2661_MAC_CSR5, tmp);
2009 }
2010 
2011 static void
2012 rt2661_set_macaddr(struct rt2661_softc *sc, const uint8_t *addr)
2013 {
2014 	uint32_t tmp;
2015 
2016 	tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24;
2017 	RAL_WRITE(sc, RT2661_MAC_CSR2, tmp);
2018 
2019 	tmp = addr[4] | addr[5] << 8;
2020 	RAL_WRITE(sc, RT2661_MAC_CSR3, tmp);
2021 }
2022 
2023 static void
2024 rt2661_update_promisc(struct ieee80211com *ic)
2025 {
2026 	struct rt2661_softc *sc = ic->ic_softc;
2027 	uint32_t tmp;
2028 
2029 	tmp = RAL_READ(sc, RT2661_TXRX_CSR0);
2030 
2031 	tmp &= ~RT2661_DROP_NOT_TO_ME;
2032 	if (ic->ic_promisc == 0)
2033 		tmp |= RT2661_DROP_NOT_TO_ME;
2034 
2035 	RAL_WRITE(sc, RT2661_TXRX_CSR0, tmp);
2036 
2037 	DPRINTF(sc, "%s promiscuous mode\n",
2038 	    (ic->ic_promisc > 0) ?  "entering" : "leaving");
2039 }
2040 
2041 /*
2042  * Update QoS (802.11e) settings for each h/w Tx ring.
2043  */
2044 static int
2045 rt2661_wme_update(struct ieee80211com *ic)
2046 {
2047 	struct rt2661_softc *sc = ic->ic_softc;
2048 	const struct wmeParams *wmep;
2049 
2050 	wmep = ic->ic_wme.wme_chanParams.cap_wmeParams;
2051 
2052 	/* XXX: not sure about shifts. */
2053 	/* XXX: the reference driver plays with AC_VI settings too. */
2054 
2055 	/* update TxOp */
2056 	RAL_WRITE(sc, RT2661_AC_TXOP_CSR0,
2057 	    wmep[WME_AC_BE].wmep_txopLimit << 16 |
2058 	    wmep[WME_AC_BK].wmep_txopLimit);
2059 	RAL_WRITE(sc, RT2661_AC_TXOP_CSR1,
2060 	    wmep[WME_AC_VI].wmep_txopLimit << 16 |
2061 	    wmep[WME_AC_VO].wmep_txopLimit);
2062 
2063 	/* update CWmin */
2064 	RAL_WRITE(sc, RT2661_CWMIN_CSR,
2065 	    wmep[WME_AC_BE].wmep_logcwmin << 12 |
2066 	    wmep[WME_AC_BK].wmep_logcwmin <<  8 |
2067 	    wmep[WME_AC_VI].wmep_logcwmin <<  4 |
2068 	    wmep[WME_AC_VO].wmep_logcwmin);
2069 
2070 	/* update CWmax */
2071 	RAL_WRITE(sc, RT2661_CWMAX_CSR,
2072 	    wmep[WME_AC_BE].wmep_logcwmax << 12 |
2073 	    wmep[WME_AC_BK].wmep_logcwmax <<  8 |
2074 	    wmep[WME_AC_VI].wmep_logcwmax <<  4 |
2075 	    wmep[WME_AC_VO].wmep_logcwmax);
2076 
2077 	/* update Aifsn */
2078 	RAL_WRITE(sc, RT2661_AIFSN_CSR,
2079 	    wmep[WME_AC_BE].wmep_aifsn << 12 |
2080 	    wmep[WME_AC_BK].wmep_aifsn <<  8 |
2081 	    wmep[WME_AC_VI].wmep_aifsn <<  4 |
2082 	    wmep[WME_AC_VO].wmep_aifsn);
2083 
2084 	return 0;
2085 }
2086 
2087 static void
2088 rt2661_update_slot(struct ieee80211com *ic)
2089 {
2090 	struct rt2661_softc *sc = ic->ic_softc;
2091 	uint8_t slottime;
2092 	uint32_t tmp;
2093 
2094 	slottime = IEEE80211_GET_SLOTTIME(ic);
2095 
2096 	tmp = RAL_READ(sc, RT2661_MAC_CSR9);
2097 	tmp = (tmp & ~0xff) | slottime;
2098 	RAL_WRITE(sc, RT2661_MAC_CSR9, tmp);
2099 }
2100 
2101 static const char *
2102 rt2661_get_rf(int rev)
2103 {
2104 	switch (rev) {
2105 	case RT2661_RF_5225:	return "RT5225";
2106 	case RT2661_RF_5325:	return "RT5325 (MIMO XR)";
2107 	case RT2661_RF_2527:	return "RT2527";
2108 	case RT2661_RF_2529:	return "RT2529 (MIMO XR)";
2109 	default:		return "unknown";
2110 	}
2111 }
2112 
2113 static void
2114 rt2661_read_eeprom(struct rt2661_softc *sc, uint8_t macaddr[IEEE80211_ADDR_LEN])
2115 {
2116 	uint16_t val;
2117 	int i;
2118 
2119 	/* read MAC address */
2120 	val = rt2661_eeprom_read(sc, RT2661_EEPROM_MAC01);
2121 	macaddr[0] = val & 0xff;
2122 	macaddr[1] = val >> 8;
2123 
2124 	val = rt2661_eeprom_read(sc, RT2661_EEPROM_MAC23);
2125 	macaddr[2] = val & 0xff;
2126 	macaddr[3] = val >> 8;
2127 
2128 	val = rt2661_eeprom_read(sc, RT2661_EEPROM_MAC45);
2129 	macaddr[4] = val & 0xff;
2130 	macaddr[5] = val >> 8;
2131 
2132 	val = rt2661_eeprom_read(sc, RT2661_EEPROM_ANTENNA);
2133 	/* XXX: test if different from 0xffff? */
2134 	sc->rf_rev   = (val >> 11) & 0x1f;
2135 	sc->hw_radio = (val >> 10) & 0x1;
2136 	sc->rx_ant   = (val >> 4)  & 0x3;
2137 	sc->tx_ant   = (val >> 2)  & 0x3;
2138 	sc->nb_ant   = val & 0x3;
2139 
2140 	DPRINTF(sc, "RF revision=%d\n", sc->rf_rev);
2141 
2142 	val = rt2661_eeprom_read(sc, RT2661_EEPROM_CONFIG2);
2143 	sc->ext_5ghz_lna = (val >> 6) & 0x1;
2144 	sc->ext_2ghz_lna = (val >> 4) & 0x1;
2145 
2146 	DPRINTF(sc, "External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n",
2147 	    sc->ext_2ghz_lna, sc->ext_5ghz_lna);
2148 
2149 	val = rt2661_eeprom_read(sc, RT2661_EEPROM_RSSI_2GHZ_OFFSET);
2150 	if ((val & 0xff) != 0xff)
2151 		sc->rssi_2ghz_corr = (int8_t)(val & 0xff);	/* signed */
2152 
2153 	/* Only [-10, 10] is valid */
2154 	if (sc->rssi_2ghz_corr < -10 || sc->rssi_2ghz_corr > 10)
2155 		sc->rssi_2ghz_corr = 0;
2156 
2157 	val = rt2661_eeprom_read(sc, RT2661_EEPROM_RSSI_5GHZ_OFFSET);
2158 	if ((val & 0xff) != 0xff)
2159 		sc->rssi_5ghz_corr = (int8_t)(val & 0xff);	/* signed */
2160 
2161 	/* Only [-10, 10] is valid */
2162 	if (sc->rssi_5ghz_corr < -10 || sc->rssi_5ghz_corr > 10)
2163 		sc->rssi_5ghz_corr = 0;
2164 
2165 	/* adjust RSSI correction for external low-noise amplifier */
2166 	if (sc->ext_2ghz_lna)
2167 		sc->rssi_2ghz_corr -= 14;
2168 	if (sc->ext_5ghz_lna)
2169 		sc->rssi_5ghz_corr -= 14;
2170 
2171 	DPRINTF(sc, "RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n",
2172 	    sc->rssi_2ghz_corr, sc->rssi_5ghz_corr);
2173 
2174 	val = rt2661_eeprom_read(sc, RT2661_EEPROM_FREQ_OFFSET);
2175 	if ((val >> 8) != 0xff)
2176 		sc->rfprog = (val >> 8) & 0x3;
2177 	if ((val & 0xff) != 0xff)
2178 		sc->rffreq = val & 0xff;
2179 
2180 	DPRINTF(sc, "RF prog=%d\nRF freq=%d\n", sc->rfprog, sc->rffreq);
2181 
2182 	/* read Tx power for all a/b/g channels */
2183 	for (i = 0; i < 19; i++) {
2184 		val = rt2661_eeprom_read(sc, RT2661_EEPROM_TXPOWER + i);
2185 		sc->txpow[i * 2] = (int8_t)(val >> 8);		/* signed */
2186 		DPRINTF(sc, "Channel=%d Tx power=%d\n",
2187 		    rt2661_rf5225_1[i * 2].chan, sc->txpow[i * 2]);
2188 		sc->txpow[i * 2 + 1] = (int8_t)(val & 0xff);	/* signed */
2189 		DPRINTF(sc, "Channel=%d Tx power=%d\n",
2190 		    rt2661_rf5225_1[i * 2 + 1].chan, sc->txpow[i * 2 + 1]);
2191 	}
2192 
2193 	/* read vendor-specific BBP values */
2194 	for (i = 0; i < 16; i++) {
2195 		val = rt2661_eeprom_read(sc, RT2661_EEPROM_BBP_BASE + i);
2196 		if (val == 0 || val == 0xffff)
2197 			continue;	/* skip invalid entries */
2198 		sc->bbp_prom[i].reg = val >> 8;
2199 		sc->bbp_prom[i].val = val & 0xff;
2200 		DPRINTF(sc, "BBP R%d=%02x\n", sc->bbp_prom[i].reg,
2201 		    sc->bbp_prom[i].val);
2202 	}
2203 }
2204 
2205 static int
2206 rt2661_bbp_init(struct rt2661_softc *sc)
2207 {
2208 	int i, ntries;
2209 	uint8_t val;
2210 
2211 	/* wait for BBP to be ready */
2212 	for (ntries = 0; ntries < 100; ntries++) {
2213 		val = rt2661_bbp_read(sc, 0);
2214 		if (val != 0 && val != 0xff)
2215 			break;
2216 		DELAY(100);
2217 	}
2218 	if (ntries == 100) {
2219 		device_printf(sc->sc_dev, "timeout waiting for BBP\n");
2220 		return EIO;
2221 	}
2222 
2223 	/* initialize BBP registers to default values */
2224 	for (i = 0; i < nitems(rt2661_def_bbp); i++) {
2225 		rt2661_bbp_write(sc, rt2661_def_bbp[i].reg,
2226 		    rt2661_def_bbp[i].val);
2227 	}
2228 
2229 	/* write vendor-specific BBP values (from EEPROM) */
2230 	for (i = 0; i < 16; i++) {
2231 		if (sc->bbp_prom[i].reg == 0)
2232 			continue;
2233 		rt2661_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
2234 	}
2235 
2236 	return 0;
2237 }
2238 
2239 static void
2240 rt2661_init_locked(struct rt2661_softc *sc)
2241 {
2242 	struct ieee80211com *ic = &sc->sc_ic;
2243 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2244 	uint32_t tmp, sta[3];
2245 	int i, error, ntries;
2246 
2247 	RAL_LOCK_ASSERT(sc);
2248 
2249 	if ((sc->sc_flags & RAL_FW_LOADED) == 0) {
2250 		error = rt2661_load_microcode(sc);
2251 		if (error != 0) {
2252 			device_printf(sc->sc_dev,
2253 			    "%s: could not load 8051 microcode, error %d\n",
2254 			    __func__, error);
2255 			return;
2256 		}
2257 		sc->sc_flags |= RAL_FW_LOADED;
2258 	}
2259 
2260 	rt2661_stop_locked(sc);
2261 
2262 	/* initialize Tx rings */
2263 	RAL_WRITE(sc, RT2661_AC1_BASE_CSR, sc->txq[1].physaddr);
2264 	RAL_WRITE(sc, RT2661_AC0_BASE_CSR, sc->txq[0].physaddr);
2265 	RAL_WRITE(sc, RT2661_AC2_BASE_CSR, sc->txq[2].physaddr);
2266 	RAL_WRITE(sc, RT2661_AC3_BASE_CSR, sc->txq[3].physaddr);
2267 
2268 	/* initialize Mgt ring */
2269 	RAL_WRITE(sc, RT2661_MGT_BASE_CSR, sc->mgtq.physaddr);
2270 
2271 	/* initialize Rx ring */
2272 	RAL_WRITE(sc, RT2661_RX_BASE_CSR, sc->rxq.physaddr);
2273 
2274 	/* initialize Tx rings sizes */
2275 	RAL_WRITE(sc, RT2661_TX_RING_CSR0,
2276 	    RT2661_TX_RING_COUNT << 24 |
2277 	    RT2661_TX_RING_COUNT << 16 |
2278 	    RT2661_TX_RING_COUNT <<  8 |
2279 	    RT2661_TX_RING_COUNT);
2280 
2281 	RAL_WRITE(sc, RT2661_TX_RING_CSR1,
2282 	    RT2661_TX_DESC_WSIZE << 16 |
2283 	    RT2661_TX_RING_COUNT <<  8 |	/* XXX: HCCA ring unused */
2284 	    RT2661_MGT_RING_COUNT);
2285 
2286 	/* initialize Rx rings */
2287 	RAL_WRITE(sc, RT2661_RX_RING_CSR,
2288 	    RT2661_RX_DESC_BACK  << 16 |
2289 	    RT2661_RX_DESC_WSIZE <<  8 |
2290 	    RT2661_RX_RING_COUNT);
2291 
2292 	/* XXX: some magic here */
2293 	RAL_WRITE(sc, RT2661_TX_DMA_DST_CSR, 0xaa);
2294 
2295 	/* load base addresses of all 5 Tx rings (4 data + 1 mgt) */
2296 	RAL_WRITE(sc, RT2661_LOAD_TX_RING_CSR, 0x1f);
2297 
2298 	/* load base address of Rx ring */
2299 	RAL_WRITE(sc, RT2661_RX_CNTL_CSR, 2);
2300 
2301 	/* initialize MAC registers to default values */
2302 	for (i = 0; i < nitems(rt2661_def_mac); i++)
2303 		RAL_WRITE(sc, rt2661_def_mac[i].reg, rt2661_def_mac[i].val);
2304 
2305 	rt2661_set_macaddr(sc, vap ? vap->iv_myaddr : ic->ic_macaddr);
2306 
2307 	/* set host ready */
2308 	RAL_WRITE(sc, RT2661_MAC_CSR1, 3);
2309 	RAL_WRITE(sc, RT2661_MAC_CSR1, 0);
2310 
2311 	/* wait for BBP/RF to wakeup */
2312 	for (ntries = 0; ntries < 1000; ntries++) {
2313 		if (RAL_READ(sc, RT2661_MAC_CSR12) & 8)
2314 			break;
2315 		DELAY(1000);
2316 	}
2317 	if (ntries == 1000) {
2318 		printf("timeout waiting for BBP/RF to wakeup\n");
2319 		rt2661_stop_locked(sc);
2320 		return;
2321 	}
2322 
2323 	if (rt2661_bbp_init(sc) != 0) {
2324 		rt2661_stop_locked(sc);
2325 		return;
2326 	}
2327 
2328 	/* select default channel */
2329 	sc->sc_curchan = ic->ic_curchan;
2330 	rt2661_select_band(sc, sc->sc_curchan);
2331 	rt2661_select_antenna(sc);
2332 	rt2661_set_chan(sc, sc->sc_curchan);
2333 
2334 	/* update Rx filter */
2335 	tmp = RAL_READ(sc, RT2661_TXRX_CSR0) & 0xffff;
2336 
2337 	tmp |= RT2661_DROP_PHY_ERROR | RT2661_DROP_CRC_ERROR;
2338 	if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2339 		tmp |= RT2661_DROP_CTL | RT2661_DROP_VER_ERROR |
2340 		       RT2661_DROP_ACKCTS;
2341 		if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
2342 		    ic->ic_opmode != IEEE80211_M_MBSS)
2343 			tmp |= RT2661_DROP_TODS;
2344 		if (ic->ic_promisc == 0)
2345 			tmp |= RT2661_DROP_NOT_TO_ME;
2346 	}
2347 
2348 	RAL_WRITE(sc, RT2661_TXRX_CSR0, tmp);
2349 
2350 	/* clear STA registers */
2351 	RAL_READ_REGION_4(sc, RT2661_STA_CSR0, sta, nitems(sta));
2352 
2353 	/* initialize ASIC */
2354 	RAL_WRITE(sc, RT2661_MAC_CSR1, 4);
2355 
2356 	/* clear any pending interrupt */
2357 	RAL_WRITE(sc, RT2661_INT_SOURCE_CSR, 0xffffffff);
2358 
2359 	/* enable interrupts */
2360 	RAL_WRITE(sc, RT2661_INT_MASK_CSR, 0x0000ff10);
2361 	RAL_WRITE(sc, RT2661_MCU_INT_MASK_CSR, 0);
2362 
2363 	/* kick Rx */
2364 	RAL_WRITE(sc, RT2661_RX_CNTL_CSR, 1);
2365 
2366 	sc->sc_flags |= RAL_RUNNING;
2367 
2368 	callout_reset(&sc->watchdog_ch, hz, rt2661_watchdog, sc);
2369 }
2370 
2371 static void
2372 rt2661_init(void *priv)
2373 {
2374 	struct rt2661_softc *sc = priv;
2375 	struct ieee80211com *ic = &sc->sc_ic;
2376 
2377 	RAL_LOCK(sc);
2378 	rt2661_init_locked(sc);
2379 	RAL_UNLOCK(sc);
2380 
2381 	if (sc->sc_flags & RAL_RUNNING)
2382 		ieee80211_start_all(ic);		/* start all vap's */
2383 }
2384 
2385 void
2386 rt2661_stop_locked(struct rt2661_softc *sc)
2387 {
2388 	volatile int *flags = &sc->sc_flags;
2389 	uint32_t tmp;
2390 
2391 	while (*flags & RAL_INPUT_RUNNING)
2392 		msleep(sc, &sc->sc_mtx, 0, "ralrunning", hz/10);
2393 
2394 	callout_stop(&sc->watchdog_ch);
2395 	sc->sc_tx_timer = 0;
2396 
2397 	if (sc->sc_flags & RAL_RUNNING) {
2398 		sc->sc_flags &= ~RAL_RUNNING;
2399 
2400 		/* abort Tx (for all 5 Tx rings) */
2401 		RAL_WRITE(sc, RT2661_TX_CNTL_CSR, 0x1f << 16);
2402 
2403 		/* disable Rx (value remains after reset!) */
2404 		tmp = RAL_READ(sc, RT2661_TXRX_CSR0);
2405 		RAL_WRITE(sc, RT2661_TXRX_CSR0, tmp | RT2661_DISABLE_RX);
2406 
2407 		/* reset ASIC */
2408 		RAL_WRITE(sc, RT2661_MAC_CSR1, 3);
2409 		RAL_WRITE(sc, RT2661_MAC_CSR1, 0);
2410 
2411 		/* disable interrupts */
2412 		RAL_WRITE(sc, RT2661_INT_MASK_CSR, 0xffffffff);
2413 		RAL_WRITE(sc, RT2661_MCU_INT_MASK_CSR, 0xffffffff);
2414 
2415 		/* clear any pending interrupt */
2416 		RAL_WRITE(sc, RT2661_INT_SOURCE_CSR, 0xffffffff);
2417 		RAL_WRITE(sc, RT2661_MCU_INT_SOURCE_CSR, 0xffffffff);
2418 
2419 		/* reset Tx and Rx rings */
2420 		rt2661_reset_tx_ring(sc, &sc->txq[0]);
2421 		rt2661_reset_tx_ring(sc, &sc->txq[1]);
2422 		rt2661_reset_tx_ring(sc, &sc->txq[2]);
2423 		rt2661_reset_tx_ring(sc, &sc->txq[3]);
2424 		rt2661_reset_tx_ring(sc, &sc->mgtq);
2425 		rt2661_reset_rx_ring(sc, &sc->rxq);
2426 	}
2427 }
2428 
2429 void
2430 rt2661_stop(void *priv)
2431 {
2432 	struct rt2661_softc *sc = priv;
2433 
2434 	RAL_LOCK(sc);
2435 	rt2661_stop_locked(sc);
2436 	RAL_UNLOCK(sc);
2437 }
2438 
2439 static int
2440 rt2661_load_microcode(struct rt2661_softc *sc)
2441 {
2442 	const struct firmware *fp;
2443 	const char *imagename;
2444 	int ntries, error;
2445 
2446 	RAL_LOCK_ASSERT(sc);
2447 
2448 	switch (sc->sc_id) {
2449 	case 0x0301: imagename = "rt2561sfw"; break;
2450 	case 0x0302: imagename = "rt2561fw"; break;
2451 	case 0x0401: imagename = "rt2661fw"; break;
2452 	default:
2453 		device_printf(sc->sc_dev, "%s: unexpected pci device id 0x%x, "
2454 		    "don't know how to retrieve firmware\n",
2455 		    __func__, sc->sc_id);
2456 		return EINVAL;
2457 	}
2458 	RAL_UNLOCK(sc);
2459 	fp = firmware_get(imagename);
2460 	RAL_LOCK(sc);
2461 	if (fp == NULL) {
2462 		device_printf(sc->sc_dev,
2463 		    "%s: unable to retrieve firmware image %s\n",
2464 		    __func__, imagename);
2465 		return EINVAL;
2466 	}
2467 
2468 	/*
2469 	 * Load 8051 microcode into NIC.
2470 	 */
2471 	/* reset 8051 */
2472 	RAL_WRITE(sc, RT2661_MCU_CNTL_CSR, RT2661_MCU_RESET);
2473 
2474 	/* cancel any pending Host to MCU command */
2475 	RAL_WRITE(sc, RT2661_H2M_MAILBOX_CSR, 0);
2476 	RAL_WRITE(sc, RT2661_M2H_CMD_DONE_CSR, 0xffffffff);
2477 	RAL_WRITE(sc, RT2661_HOST_CMD_CSR, 0);
2478 
2479 	/* write 8051's microcode */
2480 	RAL_WRITE(sc, RT2661_MCU_CNTL_CSR, RT2661_MCU_RESET | RT2661_MCU_SEL);
2481 	RAL_WRITE_REGION_1(sc, RT2661_MCU_CODE_BASE, fp->data, fp->datasize);
2482 	RAL_WRITE(sc, RT2661_MCU_CNTL_CSR, RT2661_MCU_RESET);
2483 
2484 	/* kick 8051's ass */
2485 	RAL_WRITE(sc, RT2661_MCU_CNTL_CSR, 0);
2486 
2487 	/* wait for 8051 to initialize */
2488 	for (ntries = 0; ntries < 500; ntries++) {
2489 		if (RAL_READ(sc, RT2661_MCU_CNTL_CSR) & RT2661_MCU_READY)
2490 			break;
2491 		DELAY(100);
2492 	}
2493 	if (ntries == 500) {
2494 		device_printf(sc->sc_dev,
2495 		    "%s: timeout waiting for MCU to initialize\n", __func__);
2496 		error = EIO;
2497 	} else
2498 		error = 0;
2499 
2500 	firmware_put(fp, FIRMWARE_UNLOAD);
2501 	return error;
2502 }
2503 
2504 #ifdef notyet
2505 /*
2506  * Dynamically tune Rx sensitivity (BBP register 17) based on average RSSI and
2507  * false CCA count.  This function is called periodically (every seconds) when
2508  * in the RUN state.  Values taken from the reference driver.
2509  */
2510 static void
2511 rt2661_rx_tune(struct rt2661_softc *sc)
2512 {
2513 	uint8_t bbp17;
2514 	uint16_t cca;
2515 	int lo, hi, dbm;
2516 
2517 	/*
2518 	 * Tuning range depends on operating band and on the presence of an
2519 	 * external low-noise amplifier.
2520 	 */
2521 	lo = 0x20;
2522 	if (IEEE80211_IS_CHAN_5GHZ(sc->sc_curchan))
2523 		lo += 0x08;
2524 	if ((IEEE80211_IS_CHAN_2GHZ(sc->sc_curchan) && sc->ext_2ghz_lna) ||
2525 	    (IEEE80211_IS_CHAN_5GHZ(sc->sc_curchan) && sc->ext_5ghz_lna))
2526 		lo += 0x10;
2527 	hi = lo + 0x20;
2528 
2529 	/* retrieve false CCA count since last call (clear on read) */
2530 	cca = RAL_READ(sc, RT2661_STA_CSR1) & 0xffff;
2531 
2532 	if (dbm >= -35) {
2533 		bbp17 = 0x60;
2534 	} else if (dbm >= -58) {
2535 		bbp17 = hi;
2536 	} else if (dbm >= -66) {
2537 		bbp17 = lo + 0x10;
2538 	} else if (dbm >= -74) {
2539 		bbp17 = lo + 0x08;
2540 	} else {
2541 		/* RSSI < -74dBm, tune using false CCA count */
2542 
2543 		bbp17 = sc->bbp17; /* current value */
2544 
2545 		hi -= 2 * (-74 - dbm);
2546 		if (hi < lo)
2547 			hi = lo;
2548 
2549 		if (bbp17 > hi) {
2550 			bbp17 = hi;
2551 
2552 		} else if (cca > 512) {
2553 			if (++bbp17 > hi)
2554 				bbp17 = hi;
2555 		} else if (cca < 100) {
2556 			if (--bbp17 < lo)
2557 				bbp17 = lo;
2558 		}
2559 	}
2560 
2561 	if (bbp17 != sc->bbp17) {
2562 		rt2661_bbp_write(sc, 17, bbp17);
2563 		sc->bbp17 = bbp17;
2564 	}
2565 }
2566 
2567 /*
2568  * Enter/Leave radar detection mode.
2569  * This is for 802.11h additional regulatory domains.
2570  */
2571 static void
2572 rt2661_radar_start(struct rt2661_softc *sc)
2573 {
2574 	uint32_t tmp;
2575 
2576 	/* disable Rx */
2577 	tmp = RAL_READ(sc, RT2661_TXRX_CSR0);
2578 	RAL_WRITE(sc, RT2661_TXRX_CSR0, tmp | RT2661_DISABLE_RX);
2579 
2580 	rt2661_bbp_write(sc, 82, 0x20);
2581 	rt2661_bbp_write(sc, 83, 0x00);
2582 	rt2661_bbp_write(sc, 84, 0x40);
2583 
2584 	/* save current BBP registers values */
2585 	sc->bbp18 = rt2661_bbp_read(sc, 18);
2586 	sc->bbp21 = rt2661_bbp_read(sc, 21);
2587 	sc->bbp22 = rt2661_bbp_read(sc, 22);
2588 	sc->bbp16 = rt2661_bbp_read(sc, 16);
2589 	sc->bbp17 = rt2661_bbp_read(sc, 17);
2590 	sc->bbp64 = rt2661_bbp_read(sc, 64);
2591 
2592 	rt2661_bbp_write(sc, 18, 0xff);
2593 	rt2661_bbp_write(sc, 21, 0x3f);
2594 	rt2661_bbp_write(sc, 22, 0x3f);
2595 	rt2661_bbp_write(sc, 16, 0xbd);
2596 	rt2661_bbp_write(sc, 17, sc->ext_5ghz_lna ? 0x44 : 0x34);
2597 	rt2661_bbp_write(sc, 64, 0x21);
2598 
2599 	/* restore Rx filter */
2600 	RAL_WRITE(sc, RT2661_TXRX_CSR0, tmp);
2601 }
2602 
2603 static int
2604 rt2661_radar_stop(struct rt2661_softc *sc)
2605 {
2606 	uint8_t bbp66;
2607 
2608 	/* read radar detection result */
2609 	bbp66 = rt2661_bbp_read(sc, 66);
2610 
2611 	/* restore BBP registers values */
2612 	rt2661_bbp_write(sc, 16, sc->bbp16);
2613 	rt2661_bbp_write(sc, 17, sc->bbp17);
2614 	rt2661_bbp_write(sc, 18, sc->bbp18);
2615 	rt2661_bbp_write(sc, 21, sc->bbp21);
2616 	rt2661_bbp_write(sc, 22, sc->bbp22);
2617 	rt2661_bbp_write(sc, 64, sc->bbp64);
2618 
2619 	return bbp66 == 1;
2620 }
2621 #endif
2622 
2623 static int
2624 rt2661_prepare_beacon(struct rt2661_softc *sc, struct ieee80211vap *vap)
2625 {
2626 	struct ieee80211com *ic = vap->iv_ic;
2627 	struct rt2661_tx_desc desc;
2628 	struct mbuf *m0;
2629 	int rate;
2630 
2631 	if ((m0 = ieee80211_beacon_alloc(vap->iv_bss))== NULL) {
2632 		device_printf(sc->sc_dev, "could not allocate beacon frame\n");
2633 		return ENOBUFS;
2634 	}
2635 
2636 	/* send beacons at the lowest available rate */
2637 	rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan) ? 12 : 2;
2638 
2639 	rt2661_setup_tx_desc(sc, &desc, RT2661_TX_TIMESTAMP, RT2661_TX_HWSEQ,
2640 	    m0->m_pkthdr.len, rate, NULL, 0, RT2661_QID_MGT);
2641 
2642 	/* copy the first 24 bytes of Tx descriptor into NIC memory */
2643 	RAL_WRITE_REGION_1(sc, RT2661_HW_BEACON_BASE0, (uint8_t *)&desc, 24);
2644 
2645 	/* copy beacon header and payload into NIC memory */
2646 	RAL_WRITE_REGION_1(sc, RT2661_HW_BEACON_BASE0 + 24,
2647 	    mtod(m0, uint8_t *), m0->m_pkthdr.len);
2648 
2649 	m_freem(m0);
2650 
2651 	return 0;
2652 }
2653 
2654 /*
2655  * Enable TSF synchronization and tell h/w to start sending beacons for IBSS
2656  * and HostAP operating modes.
2657  */
2658 static void
2659 rt2661_enable_tsf_sync(struct rt2661_softc *sc)
2660 {
2661 	struct ieee80211com *ic = &sc->sc_ic;
2662 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2663 	uint32_t tmp;
2664 
2665 	if (vap->iv_opmode != IEEE80211_M_STA) {
2666 		/*
2667 		 * Change default 16ms TBTT adjustment to 8ms.
2668 		 * Must be done before enabling beacon generation.
2669 		 */
2670 		RAL_WRITE(sc, RT2661_TXRX_CSR10, 1 << 12 | 8);
2671 	}
2672 
2673 	tmp = RAL_READ(sc, RT2661_TXRX_CSR9) & 0xff000000;
2674 
2675 	/* set beacon interval (in 1/16ms unit) */
2676 	tmp |= vap->iv_bss->ni_intval * 16;
2677 
2678 	tmp |= RT2661_TSF_TICKING | RT2661_ENABLE_TBTT;
2679 	if (vap->iv_opmode == IEEE80211_M_STA)
2680 		tmp |= RT2661_TSF_MODE(1);
2681 	else
2682 		tmp |= RT2661_TSF_MODE(2) | RT2661_GENERATE_BEACON;
2683 
2684 	RAL_WRITE(sc, RT2661_TXRX_CSR9, tmp);
2685 }
2686 
2687 static void
2688 rt2661_enable_tsf(struct rt2661_softc *sc)
2689 {
2690 	RAL_WRITE(sc, RT2661_TXRX_CSR9,
2691 	      (RAL_READ(sc, RT2661_TXRX_CSR9) & 0xff000000)
2692 	    | RT2661_TSF_TICKING | RT2661_TSF_MODE(2));
2693 }
2694 
2695 /*
2696  * Retrieve the "Received Signal Strength Indicator" from the raw values
2697  * contained in Rx descriptors.  The computation depends on which band the
2698  * frame was received.  Correction values taken from the reference driver.
2699  */
2700 static int
2701 rt2661_get_rssi(struct rt2661_softc *sc, uint8_t raw)
2702 {
2703 	int lna, agc, rssi;
2704 
2705 	lna = (raw >> 5) & 0x3;
2706 	agc = raw & 0x1f;
2707 
2708 	if (lna == 0) {
2709 		/*
2710 		 * No mapping available.
2711 		 *
2712 		 * NB: Since RSSI is relative to noise floor, -1 is
2713 		 *     adequate for caller to know error happened.
2714 		 */
2715 		return -1;
2716 	}
2717 
2718 	rssi = (2 * agc) - RT2661_NOISE_FLOOR;
2719 
2720 	if (IEEE80211_IS_CHAN_2GHZ(sc->sc_curchan)) {
2721 		rssi += sc->rssi_2ghz_corr;
2722 
2723 		if (lna == 1)
2724 			rssi -= 64;
2725 		else if (lna == 2)
2726 			rssi -= 74;
2727 		else if (lna == 3)
2728 			rssi -= 90;
2729 	} else {
2730 		rssi += sc->rssi_5ghz_corr;
2731 
2732 		if (lna == 1)
2733 			rssi -= 64;
2734 		else if (lna == 2)
2735 			rssi -= 86;
2736 		else if (lna == 3)
2737 			rssi -= 100;
2738 	}
2739 	return rssi;
2740 }
2741 
2742 static void
2743 rt2661_scan_start(struct ieee80211com *ic)
2744 {
2745 	struct rt2661_softc *sc = ic->ic_softc;
2746 	uint32_t tmp;
2747 
2748 	/* abort TSF synchronization */
2749 	tmp = RAL_READ(sc, RT2661_TXRX_CSR9);
2750 	RAL_WRITE(sc, RT2661_TXRX_CSR9, tmp & ~0xffffff);
2751 	rt2661_set_bssid(sc, ieee80211broadcastaddr);
2752 }
2753 
2754 static void
2755 rt2661_scan_end(struct ieee80211com *ic)
2756 {
2757 	struct rt2661_softc *sc = ic->ic_softc;
2758 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2759 
2760 	rt2661_enable_tsf_sync(sc);
2761 	/* XXX keep local copy */
2762 	rt2661_set_bssid(sc, vap->iv_bss->ni_bssid);
2763 }
2764 
2765 static void
2766 rt2661_getradiocaps(struct ieee80211com *ic,
2767     int maxchans, int *nchans, struct ieee80211_channel chans[])
2768 {
2769 	struct rt2661_softc *sc = ic->ic_softc;
2770 	uint8_t bands[IEEE80211_MODE_BYTES];
2771 
2772 	memset(bands, 0, sizeof(bands));
2773 	setbit(bands, IEEE80211_MODE_11B);
2774 	setbit(bands, IEEE80211_MODE_11G);
2775 	ieee80211_add_channel_list_2ghz(chans, maxchans, nchans,
2776 	    rt2661_chan_2ghz, nitems(rt2661_chan_2ghz), bands, 0);
2777 
2778 	if (sc->rf_rev == RT2661_RF_5225 || sc->rf_rev == RT2661_RF_5325) {
2779 		setbit(bands, IEEE80211_MODE_11A);
2780 		ieee80211_add_channel_list_5ghz(chans, maxchans, nchans,
2781 		    rt2661_chan_5ghz, nitems(rt2661_chan_5ghz), bands, 0);
2782 	}
2783 }
2784 
2785 static void
2786 rt2661_set_channel(struct ieee80211com *ic)
2787 {
2788 	struct rt2661_softc *sc = ic->ic_softc;
2789 
2790 	RAL_LOCK(sc);
2791 	rt2661_set_chan(sc, ic->ic_curchan);
2792 	RAL_UNLOCK(sc);
2793 
2794 }
2795