1 /* $FreeBSD: head/sys/dev/ral/rt2560.c 195618 2009-07-11 15:02:45Z rpaulo $ */
4 * Copyright (c) 2005, 2006
5 * Damien Bergamini <damien.bergamini@free.fr>
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.
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.
19 * $FreeBSD: head/sys/dev/ral/rt2560.c 195618 2009-07-11 15:02:45Z rpaulo $
25 * Ralink Technology RT2560 chipset driver
26 * http://www.ralinktech.com/
29 #include <sys/param.h>
30 #include <sys/sysctl.h>
31 #include <sys/sockio.h>
33 #include <sys/kernel.h>
34 #include <sys/socket.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
38 #include <sys/mutex.h>
39 #include <sys/module.h>
41 #include <sys/endian.h>
46 #include <net/if_arp.h>
47 #include <net/ethernet.h>
48 #include <net/if_dl.h>
49 #include <net/if_media.h>
50 #include <net/if_types.h>
51 #include <net/ifq_var.h>
53 #include <netproto/802_11/ieee80211_var.h>
54 #include <netproto/802_11/ieee80211_radiotap.h>
55 #include <netproto/802_11/ieee80211_regdomain.h>
56 #include <netproto/802_11/ieee80211_ratectl.h>
58 #include <netinet/in.h>
59 #include <netinet/in_systm.h>
60 #include <netinet/in_var.h>
61 #include <netinet/ip.h>
62 #include <netinet/if_ether.h>
64 #include <dev/netif/ral/rt2560reg.h>
65 #include <dev/netif/ral/rt2560var.h>
67 #define RT2560_RSSI(sc, rssi) \
68 ((rssi) > (RT2560_NOISE_FLOOR + (sc)->rssi_corr) ? \
69 ((rssi) - RT2560_NOISE_FLOOR - (sc)->rssi_corr) : 0)
73 #define DPRINTF(sc, fmt, ...) do { \
74 if (sc->sc_debug > 0) \
75 kprintf(fmt, __VA_ARGS__); \
77 #define DPRINTFN(sc, n, fmt, ...) do { \
78 if (sc->sc_debug >= (n)) \
79 kprintf(fmt, __VA_ARGS__); \
82 #define DPRINTF(sc, fmt, ...)
83 #define DPRINTFN(sc, n, fmt, ...)
86 static struct ieee80211vap *rt2560_vap_create(struct ieee80211com *,
87 const char name[IFNAMSIZ], int unit, int opmode,
88 int flags, const uint8_t bssid[IEEE80211_ADDR_LEN],
89 const uint8_t mac[IEEE80211_ADDR_LEN]);
90 static void rt2560_vap_delete(struct ieee80211vap *);
91 static void rt2560_dma_map_addr(void *, bus_dma_segment_t *, int,
93 static int rt2560_alloc_tx_ring(struct rt2560_softc *,
94 struct rt2560_tx_ring *, int);
95 static void rt2560_reset_tx_ring(struct rt2560_softc *,
96 struct rt2560_tx_ring *);
97 static void rt2560_free_tx_ring(struct rt2560_softc *,
98 struct rt2560_tx_ring *);
99 static int rt2560_alloc_rx_ring(struct rt2560_softc *,
100 struct rt2560_rx_ring *, int);
101 static void rt2560_reset_rx_ring(struct rt2560_softc *,
102 struct rt2560_rx_ring *);
103 static void rt2560_free_rx_ring(struct rt2560_softc *,
104 struct rt2560_rx_ring *);
105 static int rt2560_newstate(struct ieee80211vap *,
106 enum ieee80211_state, int);
107 static uint16_t rt2560_eeprom_read(struct rt2560_softc *, uint8_t);
108 static void rt2560_encryption_intr(struct rt2560_softc *);
109 static void rt2560_tx_intr(struct rt2560_softc *);
110 static void rt2560_prio_intr(struct rt2560_softc *);
111 static void rt2560_decryption_intr(struct rt2560_softc *);
112 static void rt2560_rx_intr(struct rt2560_softc *);
113 static void rt2560_beacon_update(struct ieee80211vap *, int item);
114 static void rt2560_beacon_expire(struct rt2560_softc *);
115 static void rt2560_wakeup_expire(struct rt2560_softc *);
116 static void rt2560_scan_start(struct ieee80211com *);
117 static void rt2560_scan_end(struct ieee80211com *);
118 static void rt2560_set_channel(struct ieee80211com *);
119 static void rt2560_setup_tx_desc(struct rt2560_softc *,
120 struct rt2560_tx_desc *, uint32_t, int, int, int,
122 static int rt2560_tx_bcn(struct rt2560_softc *, struct mbuf *,
123 struct ieee80211_node *);
124 static int rt2560_tx_mgt(struct rt2560_softc *, struct mbuf *,
125 struct ieee80211_node *);
126 static int rt2560_tx_data(struct rt2560_softc *, struct mbuf *,
127 struct ieee80211_node *);
128 static void rt2560_start_locked(struct ifnet *);
129 static void rt2560_start(struct ifnet *);
130 static void rt2560_watchdog_callout(void *);
131 static int rt2560_ioctl(struct ifnet *, u_long, caddr_t,
133 static void rt2560_bbp_write(struct rt2560_softc *, uint8_t,
135 static uint8_t rt2560_bbp_read(struct rt2560_softc *, uint8_t);
136 static void rt2560_rf_write(struct rt2560_softc *, uint8_t,
138 static void rt2560_set_chan(struct rt2560_softc *,
139 struct ieee80211_channel *);
141 static void rt2560_disable_rf_tune(struct rt2560_softc *);
143 static void rt2560_enable_tsf_sync(struct rt2560_softc *);
144 static void rt2560_enable_tsf(struct rt2560_softc *);
145 static void rt2560_update_plcp(struct rt2560_softc *);
146 static void rt2560_update_slot(struct ifnet *);
147 static void rt2560_set_basicrates(struct rt2560_softc *);
148 static void rt2560_update_led(struct rt2560_softc *, int, int);
149 static void rt2560_set_bssid(struct rt2560_softc *, const uint8_t *);
150 static void rt2560_set_macaddr(struct rt2560_softc *, uint8_t *);
151 static void rt2560_get_macaddr(struct rt2560_softc *, uint8_t *);
152 static void rt2560_update_promisc(struct ifnet *);
153 static const char *rt2560_get_rf(int);
154 static void rt2560_read_config(struct rt2560_softc *);
155 static int rt2560_bbp_init(struct rt2560_softc *);
156 static void rt2560_set_txantenna(struct rt2560_softc *, int);
157 static void rt2560_set_rxantenna(struct rt2560_softc *, int);
158 static void rt2560_init_locked(struct rt2560_softc *);
159 static void rt2560_init(void *);
160 static void rt2560_stop_locked(struct rt2560_softc *);
161 static int rt2560_raw_xmit(struct ieee80211_node *, struct mbuf *,
162 const struct ieee80211_bpf_params *);
164 static const struct {
167 } rt2560_def_mac[] = {
171 static const struct {
174 } rt2560_def_bbp[] = {
178 static const uint32_t rt2560_rf2522_r2[] = RT2560_RF2522_R2;
179 static const uint32_t rt2560_rf2523_r2[] = RT2560_RF2523_R2;
180 static const uint32_t rt2560_rf2524_r2[] = RT2560_RF2524_R2;
181 static const uint32_t rt2560_rf2525_r2[] = RT2560_RF2525_R2;
182 static const uint32_t rt2560_rf2525_hi_r2[] = RT2560_RF2525_HI_R2;
183 static const uint32_t rt2560_rf2525e_r2[] = RT2560_RF2525E_R2;
184 static const uint32_t rt2560_rf2526_r2[] = RT2560_RF2526_R2;
185 static const uint32_t rt2560_rf2526_hi_r2[] = RT2560_RF2526_HI_R2;
187 static const struct {
190 } rt2560_rf5222[] = {
195 rt2560_attach(device_t dev, int id)
197 struct rt2560_softc *sc = device_get_softc(dev);
198 struct ieee80211com *ic;
202 uint8_t macaddr[IEEE80211_ADDR_LEN];
203 struct sysctl_ctx_list *ctx;
204 struct sysctl_oid *tree;
208 callout_init(&sc->watchdog_ch);
210 /* retrieve RT2560 rev. no */
211 sc->asic_rev = RAL_READ(sc, RT2560_CSR0);
213 /* retrieve RF rev. no and various other things from EEPROM */
214 rt2560_read_config(sc);
216 device_printf(dev, "MAC/BBP RT2560 (rev 0x%02x), RF %s\n",
217 sc->asic_rev, rt2560_get_rf(sc->rf_rev));
220 * Allocate Tx and Rx rings.
222 error = rt2560_alloc_tx_ring(sc, &sc->txq, RT2560_TX_RING_COUNT);
224 device_printf(sc->sc_dev, "could not allocate Tx ring\n");
228 error = rt2560_alloc_tx_ring(sc, &sc->atimq, RT2560_ATIM_RING_COUNT);
230 device_printf(sc->sc_dev, "could not allocate ATIM ring\n");
234 error = rt2560_alloc_tx_ring(sc, &sc->prioq, RT2560_PRIO_RING_COUNT);
236 device_printf(sc->sc_dev, "could not allocate Prio ring\n");
240 error = rt2560_alloc_tx_ring(sc, &sc->bcnq, RT2560_BEACON_RING_COUNT);
242 device_printf(sc->sc_dev, "could not allocate Beacon ring\n");
246 error = rt2560_alloc_rx_ring(sc, &sc->rxq, RT2560_RX_RING_COUNT);
248 device_printf(sc->sc_dev, "could not allocate Rx ring\n");
252 ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
254 device_printf(sc->sc_dev, "can not if_alloc()\n");
259 /* retrieve MAC address */
260 rt2560_get_macaddr(sc, macaddr);
263 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
264 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
265 ifp->if_init = rt2560_init;
266 ifp->if_ioctl = rt2560_ioctl;
267 ifp->if_start = rt2560_start;
268 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
269 ifq_set_ready(&ifp->if_snd);
272 ic->ic_opmode = IEEE80211_M_STA;
273 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
275 /* set device capabilities */
277 IEEE80211_C_STA /* station mode */
278 | IEEE80211_C_IBSS /* ibss, nee adhoc, mode */
279 | IEEE80211_C_HOSTAP /* hostap mode */
280 | IEEE80211_C_MONITOR /* monitor mode */
281 | IEEE80211_C_AHDEMO /* adhoc demo mode */
282 | IEEE80211_C_WDS /* 4-address traffic works */
283 | IEEE80211_C_MBSS /* mesh point link mode */
284 | IEEE80211_C_SHPREAMBLE /* short preamble supported */
285 | IEEE80211_C_SHSLOT /* short slot time supported */
286 | IEEE80211_C_WPA /* capable of WPA1+WPA2 */
287 | IEEE80211_C_BGSCAN /* capable of bg scanning */
289 | IEEE80211_C_TXFRAG /* handle tx frags */
294 setbit(&bands, IEEE80211_MODE_11B);
295 setbit(&bands, IEEE80211_MODE_11G);
296 if (sc->rf_rev == RT2560_RF_5222)
297 setbit(&bands, IEEE80211_MODE_11A);
298 ieee80211_init_channels(ic, NULL, &bands);
300 ieee80211_ifattach(ic, macaddr);
301 ic->ic_raw_xmit = rt2560_raw_xmit;
302 ic->ic_updateslot = rt2560_update_slot;
303 ic->ic_update_promisc = rt2560_update_promisc;
304 ic->ic_scan_start = rt2560_scan_start;
305 ic->ic_scan_end = rt2560_scan_end;
306 ic->ic_set_channel = rt2560_set_channel;
308 ic->ic_vap_create = rt2560_vap_create;
309 ic->ic_vap_delete = rt2560_vap_delete;
311 ieee80211_radiotap_attach(ic,
312 &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
313 RT2560_TX_RADIOTAP_PRESENT,
314 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
315 RT2560_RX_RADIOTAP_PRESENT);
318 * Add a few sysctl knobs.
320 ctx = &sc->sc_sysctl_ctx;
321 sysctl_ctx_init(ctx);
322 tree = SYSCTL_ADD_NODE(ctx, SYSCTL_STATIC_CHILDREN(_hw),
324 device_get_nameunit(sc->sc_dev),
327 device_printf(sc->sc_dev, "can't add sysctl node\n");
332 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
333 "debug", CTLFLAG_RW, &sc->sc_debug, 0, "debug msgs");
335 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
336 "txantenna", CTLFLAG_RW, &sc->tx_ant, 0, "tx antenna (0=auto)");
338 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
339 "rxantenna", CTLFLAG_RW, &sc->rx_ant, 0, "rx antenna (0=auto)");
342 ieee80211_announce(ic);
346 fail6: rt2560_free_rx_ring(sc, &sc->rxq);
347 fail5: rt2560_free_tx_ring(sc, &sc->bcnq);
348 fail4: rt2560_free_tx_ring(sc, &sc->prioq);
349 fail3: rt2560_free_tx_ring(sc, &sc->atimq);
350 fail2: rt2560_free_tx_ring(sc, &sc->txq);
357 rt2560_detach(void *xsc)
359 struct rt2560_softc *sc = xsc;
360 struct ifnet *ifp = sc->sc_ifp;
361 struct ieee80211com *ic = ifp->if_l2com;
365 ieee80211_ifdetach(ic);
367 rt2560_free_tx_ring(sc, &sc->txq);
368 rt2560_free_tx_ring(sc, &sc->atimq);
369 rt2560_free_tx_ring(sc, &sc->prioq);
370 rt2560_free_tx_ring(sc, &sc->bcnq);
371 rt2560_free_rx_ring(sc, &sc->rxq);
378 static struct ieee80211vap *
379 rt2560_vap_create(struct ieee80211com *ic,
380 const char name[IFNAMSIZ], int unit, int opmode, int flags,
381 const uint8_t bssid[IEEE80211_ADDR_LEN],
382 const uint8_t mac[IEEE80211_ADDR_LEN])
384 struct ifnet *ifp = ic->ic_ifp;
385 struct rt2560_vap *rvp;
386 struct ieee80211vap *vap;
389 case IEEE80211_M_STA:
390 case IEEE80211_M_IBSS:
391 case IEEE80211_M_AHDEMO:
392 case IEEE80211_M_MONITOR:
393 case IEEE80211_M_HOSTAP:
394 case IEEE80211_M_MBSS:
396 if (!TAILQ_EMPTY(&ic->ic_vaps)) {
397 if_printf(ifp, "only 1 vap supported\n");
400 if (opmode == IEEE80211_M_STA)
401 flags |= IEEE80211_CLONE_NOBEACONS;
403 case IEEE80211_M_WDS:
404 if (TAILQ_EMPTY(&ic->ic_vaps) ||
405 ic->ic_opmode != IEEE80211_M_HOSTAP) {
406 if_printf(ifp, "wds only supported in ap mode\n");
410 * Silently remove any request for a unique
411 * bssid; WDS vap's always share the local
414 flags &= ~IEEE80211_CLONE_BSSID;
417 if_printf(ifp, "unknown opmode %d\n", opmode);
420 rvp = (struct rt2560_vap *) kmalloc(sizeof(struct rt2560_vap),
421 M_80211_VAP, M_INTWAIT | M_ZERO);
425 ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac);
427 /* override state transition machine */
428 rvp->ral_newstate = vap->iv_newstate;
429 vap->iv_newstate = rt2560_newstate;
430 vap->iv_update_beacon = rt2560_beacon_update;
432 ieee80211_ratectl_init(vap);
434 ieee80211_vap_attach(vap, ieee80211_media_change, ieee80211_media_status);
435 if (TAILQ_FIRST(&ic->ic_vaps) == vap)
436 ic->ic_opmode = opmode;
441 rt2560_vap_delete(struct ieee80211vap *vap)
443 struct rt2560_vap *rvp = RT2560_VAP(vap);
445 ieee80211_ratectl_deinit(vap);
446 ieee80211_vap_detach(vap);
447 kfree(rvp, M_80211_VAP);
451 rt2560_resume(void *xsc)
453 struct rt2560_softc *sc = xsc;
454 struct ifnet *ifp = sc->sc_ifp;
456 if (ifp->if_flags & IFF_UP)
461 rt2560_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
466 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
468 *(bus_addr_t *)arg = segs[0].ds_addr;
472 rt2560_alloc_tx_ring(struct rt2560_softc *sc, struct rt2560_tx_ring *ring,
479 ring->cur = ring->next = 0;
480 ring->cur_encrypt = ring->next_encrypt = 0;
482 error = bus_dma_tag_create(ring->desc_dmat, 4, 0,
483 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
484 count * RT2560_TX_DESC_SIZE, 1, count * RT2560_TX_DESC_SIZE,
485 0, &ring->desc_dmat);
487 device_printf(sc->sc_dev, "could not create desc DMA tag\n");
491 error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc,
492 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_map);
494 device_printf(sc->sc_dev, "could not allocate DMA memory\n");
498 error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc,
499 count * RT2560_TX_DESC_SIZE, rt2560_dma_map_addr, &ring->physaddr,
502 device_printf(sc->sc_dev, "could not load desc DMA map\n");
506 ring->data = kmalloc(count * sizeof (struct rt2560_tx_data), M_DEVBUF,
508 if (ring->data == NULL) {
509 device_printf(sc->sc_dev, "could not allocate soft data\n");
514 error = bus_dma_tag_create(ring->data_dmat, 1, 0,
515 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
516 MCLBYTES, RT2560_MAX_SCATTER, MCLBYTES, 0, &ring->data_dmat);
518 device_printf(sc->sc_dev, "could not create data DMA tag\n");
522 for (i = 0; i < count; i++) {
523 error = bus_dmamap_create(ring->data_dmat, 0,
526 device_printf(sc->sc_dev, "could not create DMA map\n");
533 fail: rt2560_free_tx_ring(sc, ring);
538 rt2560_reset_tx_ring(struct rt2560_softc *sc, struct rt2560_tx_ring *ring)
540 struct rt2560_tx_desc *desc;
541 struct rt2560_tx_data *data;
544 for (i = 0; i < ring->count; i++) {
545 desc = &ring->desc[i];
546 data = &ring->data[i];
548 if (data->m != NULL) {
549 bus_dmamap_sync(ring->data_dmat, data->map,
550 BUS_DMASYNC_POSTWRITE);
551 bus_dmamap_unload(ring->data_dmat, data->map);
556 if (data->ni != NULL) {
557 ieee80211_free_node(data->ni);
564 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_PREWRITE);
567 ring->cur = ring->next = 0;
568 ring->cur_encrypt = ring->next_encrypt = 0;
572 rt2560_free_tx_ring(struct rt2560_softc *sc, struct rt2560_tx_ring *ring)
574 struct rt2560_tx_data *data;
577 if (ring->desc != NULL) {
578 bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
579 BUS_DMASYNC_POSTWRITE);
580 bus_dmamap_unload(ring->desc_dmat, ring->desc_map);
581 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
584 if (ring->desc_dmat != NULL)
585 bus_dma_tag_destroy(ring->desc_dmat);
587 if (ring->data != NULL) {
588 for (i = 0; i < ring->count; i++) {
589 data = &ring->data[i];
591 if (data->m != NULL) {
592 bus_dmamap_sync(ring->data_dmat, data->map,
593 BUS_DMASYNC_POSTWRITE);
594 bus_dmamap_unload(ring->data_dmat, data->map);
598 if (data->ni != NULL)
599 ieee80211_free_node(data->ni);
601 if (data->map != NULL)
602 bus_dmamap_destroy(ring->data_dmat, data->map);
605 kfree(ring->data, M_DEVBUF);
608 if (ring->data_dmat != NULL)
609 bus_dma_tag_destroy(ring->data_dmat);
613 rt2560_alloc_rx_ring(struct rt2560_softc *sc, struct rt2560_rx_ring *ring,
616 struct rt2560_rx_desc *desc;
617 struct rt2560_rx_data *data;
622 ring->cur = ring->next = 0;
623 ring->cur_decrypt = 0;
625 error = bus_dma_tag_create(ring->desc_dmat, 4, 0,
626 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
627 count * RT2560_RX_DESC_SIZE, 1, count * RT2560_RX_DESC_SIZE,
628 0, &ring->desc_dmat);
630 device_printf(sc->sc_dev, "could not create desc DMA tag\n");
634 error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc,
635 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_map);
637 device_printf(sc->sc_dev, "could not allocate DMA memory\n");
641 error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc,
642 count * RT2560_RX_DESC_SIZE, rt2560_dma_map_addr, &ring->physaddr,
645 device_printf(sc->sc_dev, "could not load desc DMA map\n");
649 ring->data = kmalloc(count * sizeof (struct rt2560_rx_data), M_DEVBUF,
651 if (ring->data == NULL) {
652 device_printf(sc->sc_dev, "could not allocate soft data\n");
658 * Pre-allocate Rx buffers and populate Rx ring.
660 error = bus_dma_tag_create(ring->data_dmat, 1, 0,
661 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES,
662 1, MCLBYTES, 0, &ring->data_dmat);
664 device_printf(sc->sc_dev, "could not create data DMA tag\n");
668 for (i = 0; i < count; i++) {
669 desc = &sc->rxq.desc[i];
670 data = &sc->rxq.data[i];
672 error = bus_dmamap_create(ring->data_dmat, 0, &data->map);
674 device_printf(sc->sc_dev, "could not create DMA map\n");
678 data->m = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
679 if (data->m == NULL) {
680 device_printf(sc->sc_dev,
681 "could not allocate rx mbuf\n");
686 error = bus_dmamap_load(ring->data_dmat, data->map,
687 mtod(data->m, void *), MCLBYTES, rt2560_dma_map_addr,
690 device_printf(sc->sc_dev,
691 "could not load rx buf DMA map");
695 desc->flags = htole32(RT2560_RX_BUSY);
696 desc->physaddr = htole32(physaddr);
699 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_PREWRITE);
703 fail: rt2560_free_rx_ring(sc, ring);
708 rt2560_reset_rx_ring(struct rt2560_softc *sc, struct rt2560_rx_ring *ring)
712 for (i = 0; i < ring->count; i++) {
713 ring->desc[i].flags = htole32(RT2560_RX_BUSY);
714 ring->data[i].drop = 0;
717 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_PREWRITE);
719 ring->cur = ring->next = 0;
720 ring->cur_decrypt = 0;
724 rt2560_free_rx_ring(struct rt2560_softc *sc, struct rt2560_rx_ring *ring)
726 struct rt2560_rx_data *data;
729 if (ring->desc != NULL) {
730 bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
731 BUS_DMASYNC_POSTWRITE);
732 bus_dmamap_unload(ring->desc_dmat, ring->desc_map);
733 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
736 if (ring->desc_dmat != NULL)
737 bus_dma_tag_destroy(ring->desc_dmat);
739 if (ring->data != NULL) {
740 for (i = 0; i < ring->count; i++) {
741 data = &ring->data[i];
743 if (data->m != NULL) {
744 bus_dmamap_sync(ring->data_dmat, data->map,
745 BUS_DMASYNC_POSTREAD);
746 bus_dmamap_unload(ring->data_dmat, data->map);
750 if (data->map != NULL)
751 bus_dmamap_destroy(ring->data_dmat, data->map);
754 kfree(ring->data, M_DEVBUF);
757 if (ring->data_dmat != NULL)
758 bus_dma_tag_destroy(ring->data_dmat);
762 rt2560_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
764 struct rt2560_vap *rvp = RT2560_VAP(vap);
765 struct ifnet *ifp = vap->iv_ic->ic_ifp;
766 struct rt2560_softc *sc = ifp->if_softc;
769 if (nstate == IEEE80211_S_INIT && vap->iv_state == IEEE80211_S_RUN) {
770 /* abort TSF synchronization */
771 RAL_WRITE(sc, RT2560_CSR14, 0);
773 /* turn association led off */
774 rt2560_update_led(sc, 0, 0);
777 error = rvp->ral_newstate(vap, nstate, arg);
779 if (error == 0 && nstate == IEEE80211_S_RUN) {
780 struct ieee80211_node *ni = vap->iv_bss;
783 if (vap->iv_opmode != IEEE80211_M_MONITOR) {
784 rt2560_update_plcp(sc);
785 rt2560_set_basicrates(sc);
786 rt2560_set_bssid(sc, ni->ni_bssid);
789 if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
790 vap->iv_opmode == IEEE80211_M_IBSS ||
791 vap->iv_opmode == IEEE80211_M_MBSS) {
792 m = ieee80211_beacon_alloc(ni, &rvp->ral_bo);
794 if_printf(ifp, "could not allocate beacon\n");
797 ieee80211_ref_node(ni);
798 error = rt2560_tx_bcn(sc, m, ni);
803 /* turn assocation led on */
804 rt2560_update_led(sc, 1, 0);
806 if (vap->iv_opmode != IEEE80211_M_MONITOR)
807 rt2560_enable_tsf_sync(sc);
809 rt2560_enable_tsf(sc);
815 * Read 16 bits at address 'addr' from the serial EEPROM (either 93C46 or
819 rt2560_eeprom_read(struct rt2560_softc *sc, uint8_t addr)
825 /* clock C once before the first command */
826 RT2560_EEPROM_CTL(sc, 0);
828 RT2560_EEPROM_CTL(sc, RT2560_S);
829 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C);
830 RT2560_EEPROM_CTL(sc, RT2560_S);
832 /* write start bit (1) */
833 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D);
834 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D | RT2560_C);
836 /* write READ opcode (10) */
837 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D);
838 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D | RT2560_C);
839 RT2560_EEPROM_CTL(sc, RT2560_S);
840 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C);
842 /* write address (A5-A0 or A7-A0) */
843 n = (RAL_READ(sc, RT2560_CSR21) & RT2560_93C46) ? 5 : 7;
844 for (; n >= 0; n--) {
845 RT2560_EEPROM_CTL(sc, RT2560_S |
846 (((addr >> n) & 1) << RT2560_SHIFT_D));
847 RT2560_EEPROM_CTL(sc, RT2560_S |
848 (((addr >> n) & 1) << RT2560_SHIFT_D) | RT2560_C);
851 RT2560_EEPROM_CTL(sc, RT2560_S);
853 /* read data Q15-Q0 */
855 for (n = 15; n >= 0; n--) {
856 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C);
857 tmp = RAL_READ(sc, RT2560_CSR21);
858 val |= ((tmp & RT2560_Q) >> RT2560_SHIFT_Q) << n;
859 RT2560_EEPROM_CTL(sc, RT2560_S);
862 RT2560_EEPROM_CTL(sc, 0);
864 /* clear Chip Select and clock C */
865 RT2560_EEPROM_CTL(sc, RT2560_S);
866 RT2560_EEPROM_CTL(sc, 0);
867 RT2560_EEPROM_CTL(sc, RT2560_C);
873 * Some frames were processed by the hardware cipher engine and are ready for
877 rt2560_encryption_intr(struct rt2560_softc *sc)
879 struct rt2560_tx_desc *desc;
882 /* retrieve last descriptor index processed by cipher engine */
883 hw = RAL_READ(sc, RT2560_SECCSR1) - sc->txq.physaddr;
884 hw /= RT2560_TX_DESC_SIZE;
886 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
887 BUS_DMASYNC_POSTREAD);
889 while (sc->txq.next_encrypt != hw) {
890 if (sc->txq.next_encrypt == sc->txq.cur_encrypt) {
891 kprintf("hw encrypt %d, cur_encrypt %d\n", hw,
892 sc->txq.cur_encrypt);
896 desc = &sc->txq.desc[sc->txq.next_encrypt];
898 if ((le32toh(desc->flags) & RT2560_TX_BUSY) ||
899 (le32toh(desc->flags) & RT2560_TX_CIPHER_BUSY))
902 /* for TKIP, swap eiv field to fix a bug in ASIC */
903 if ((le32toh(desc->flags) & RT2560_TX_CIPHER_MASK) ==
904 RT2560_TX_CIPHER_TKIP)
905 desc->eiv = bswap32(desc->eiv);
907 /* mark the frame ready for transmission */
908 desc->flags |= htole32(RT2560_TX_VALID);
909 desc->flags |= htole32(RT2560_TX_BUSY);
911 DPRINTFN(sc, 15, "encryption done idx=%u\n",
912 sc->txq.next_encrypt);
914 sc->txq.next_encrypt =
915 (sc->txq.next_encrypt + 1) % RT2560_TX_RING_COUNT;
918 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
919 BUS_DMASYNC_PREWRITE);
922 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_TX);
926 rt2560_tx_intr(struct rt2560_softc *sc)
928 struct ifnet *ifp = sc->sc_ifp;
929 struct rt2560_tx_desc *desc;
930 struct rt2560_tx_data *data;
934 struct ieee80211vap *vap;
935 struct ieee80211_node *ni;
937 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
938 BUS_DMASYNC_POSTREAD);
941 desc = &sc->txq.desc[sc->txq.next];
942 data = &sc->txq.data[sc->txq.next];
944 flags = le32toh(desc->flags);
945 if ((flags & RT2560_TX_BUSY) ||
946 (flags & RT2560_TX_CIPHER_BUSY) ||
947 !(flags & RT2560_TX_VALID))
954 switch (flags & RT2560_TX_RESULT_MASK) {
955 case RT2560_TX_SUCCESS:
958 DPRINTFN(sc, 10, "%s\n", "data frame sent successfully");
959 if (data->rix != IEEE80211_FIXED_RATE_NONE)
960 ieee80211_ratectl_tx_complete(vap, ni,
961 IEEE80211_RATECTL_TX_SUCCESS,
966 case RT2560_TX_SUCCESS_RETRY:
967 retrycnt = RT2560_TX_RETRYCNT(flags);
969 DPRINTFN(sc, 9, "data frame sent after %u retries\n",
971 if (data->rix != IEEE80211_FIXED_RATE_NONE)
972 ieee80211_ratectl_tx_complete(vap, ni,
973 IEEE80211_RATECTL_TX_SUCCESS,
978 case RT2560_TX_FAIL_RETRY:
979 retrycnt = RT2560_TX_RETRYCNT(flags);
981 DPRINTFN(sc, 9, "data frame failed after %d retries\n",
983 if (data->rix != IEEE80211_FIXED_RATE_NONE)
984 ieee80211_ratectl_tx_complete(vap, ni,
985 IEEE80211_RATECTL_TX_FAILURE,
990 case RT2560_TX_FAIL_INVALID:
991 case RT2560_TX_FAIL_OTHER:
993 device_printf(sc->sc_dev, "sending data frame failed "
998 bus_dmamap_sync(sc->txq.data_dmat, data->map,
999 BUS_DMASYNC_POSTWRITE);
1000 bus_dmamap_unload(sc->txq.data_dmat, data->map);
1003 ieee80211_free_node(data->ni);
1007 /* descriptor is no longer valid */
1008 desc->flags &= ~htole32(RT2560_TX_VALID);
1010 DPRINTFN(sc, 15, "tx done idx=%u\n", sc->txq.next);
1013 sc->txq.next = (sc->txq.next + 1) % RT2560_TX_RING_COUNT;
1016 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
1017 BUS_DMASYNC_PREWRITE);
1019 if (sc->prioq.queued == 0 && sc->txq.queued == 0)
1020 sc->sc_tx_timer = 0;
1022 if (sc->txq.queued < RT2560_TX_RING_COUNT - 1) {
1023 sc->sc_flags &= ~RT2560_F_DATA_OACTIVE;
1025 (RT2560_F_DATA_OACTIVE | RT2560_F_PRIO_OACTIVE)) == 0)
1026 ifp->if_flags &= ~IFF_OACTIVE;
1027 rt2560_start_locked(ifp);
1032 rt2560_prio_intr(struct rt2560_softc *sc)
1034 struct ifnet *ifp = sc->sc_ifp;
1035 struct rt2560_tx_desc *desc;
1036 struct rt2560_tx_data *data;
1037 struct ieee80211_node *ni;
1041 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map,
1042 BUS_DMASYNC_POSTREAD);
1045 desc = &sc->prioq.desc[sc->prioq.next];
1046 data = &sc->prioq.data[sc->prioq.next];
1048 flags = le32toh(desc->flags);
1049 if ((flags & RT2560_TX_BUSY) || (flags & RT2560_TX_VALID) == 0)
1052 switch (flags & RT2560_TX_RESULT_MASK) {
1053 case RT2560_TX_SUCCESS:
1054 DPRINTFN(sc, 10, "%s\n", "mgt frame sent successfully");
1057 case RT2560_TX_SUCCESS_RETRY:
1058 DPRINTFN(sc, 9, "mgt frame sent after %u retries\n",
1059 (flags >> 5) & 0x7);
1062 case RT2560_TX_FAIL_RETRY:
1063 DPRINTFN(sc, 9, "%s\n",
1064 "sending mgt frame failed (too much retries)");
1067 case RT2560_TX_FAIL_INVALID:
1068 case RT2560_TX_FAIL_OTHER:
1070 device_printf(sc->sc_dev, "sending mgt frame failed "
1075 bus_dmamap_sync(sc->prioq.data_dmat, data->map,
1076 BUS_DMASYNC_POSTWRITE);
1077 bus_dmamap_unload(sc->prioq.data_dmat, data->map);
1084 /* descriptor is no longer valid */
1085 desc->flags &= ~htole32(RT2560_TX_VALID);
1087 DPRINTFN(sc, 15, "prio done idx=%u\n", sc->prioq.next);
1090 sc->prioq.next = (sc->prioq.next + 1) % RT2560_PRIO_RING_COUNT;
1092 if (m->m_flags & M_TXCB)
1093 ieee80211_process_callback(ni, m,
1094 (flags & RT2560_TX_RESULT_MASK) &~
1095 (RT2560_TX_SUCCESS | RT2560_TX_SUCCESS_RETRY));
1097 ieee80211_free_node(ni);
1100 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map,
1101 BUS_DMASYNC_PREWRITE);
1103 if (sc->prioq.queued == 0 && sc->txq.queued == 0)
1104 sc->sc_tx_timer = 0;
1106 if (sc->prioq.queued < RT2560_PRIO_RING_COUNT) {
1107 sc->sc_flags &= ~RT2560_F_PRIO_OACTIVE;
1109 (RT2560_F_DATA_OACTIVE | RT2560_F_PRIO_OACTIVE)) == 0)
1110 ifp->if_flags &= ~IFF_OACTIVE;
1111 rt2560_start_locked(ifp);
1116 * Some frames were processed by the hardware cipher engine and are ready for
1117 * handoff to the IEEE802.11 layer.
1120 rt2560_decryption_intr(struct rt2560_softc *sc)
1122 struct ifnet *ifp = sc->sc_ifp;
1123 struct ieee80211com *ic = ifp->if_l2com;
1124 struct rt2560_rx_desc *desc;
1125 struct rt2560_rx_data *data;
1126 bus_addr_t physaddr;
1127 struct ieee80211_frame *wh;
1128 struct ieee80211_node *ni;
1129 struct mbuf *mnew, *m;
1133 /* retrieve last decriptor index processed by cipher engine */
1134 hw = RAL_READ(sc, RT2560_SECCSR0) - sc->rxq.physaddr;
1135 hw /= RT2560_RX_DESC_SIZE;
1137 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1138 BUS_DMASYNC_POSTREAD);
1140 for (; sc->rxq.cur_decrypt != hw;) {
1141 desc = &sc->rxq.desc[sc->rxq.cur_decrypt];
1142 data = &sc->rxq.data[sc->rxq.cur_decrypt];
1144 if ((le32toh(desc->flags) & RT2560_RX_BUSY) ||
1145 (le32toh(desc->flags) & RT2560_RX_CIPHER_BUSY))
1153 if ((le32toh(desc->flags) & RT2560_RX_CIPHER_MASK) != 0 &&
1154 (le32toh(desc->flags) & RT2560_RX_ICV_ERROR)) {
1160 * Try to allocate a new mbuf for this ring element and load it
1161 * before processing the current mbuf. If the ring element
1162 * cannot be loaded, drop the received packet and reuse the old
1163 * mbuf. In the unlikely case that the old mbuf can't be
1164 * reloaded either, explicitly panic.
1166 mnew = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
1172 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
1173 BUS_DMASYNC_POSTREAD);
1174 bus_dmamap_unload(sc->rxq.data_dmat, data->map);
1176 error = bus_dmamap_load(sc->rxq.data_dmat, data->map,
1177 mtod(mnew, void *), MCLBYTES, rt2560_dma_map_addr,
1182 /* try to reload the old mbuf */
1183 error = bus_dmamap_load(sc->rxq.data_dmat, data->map,
1184 mtod(data->m, void *), MCLBYTES,
1185 rt2560_dma_map_addr, &physaddr, 0);
1187 /* very unlikely that it will fail... */
1188 panic("%s: could not load old rx mbuf",
1189 device_get_name(sc->sc_dev));
1196 * New mbuf successfully loaded, update Rx ring and continue
1201 desc->physaddr = htole32(physaddr);
1204 m->m_pkthdr.rcvif = ifp;
1205 m->m_pkthdr.len = m->m_len =
1206 (le32toh(desc->flags) >> 16) & 0xfff;
1208 rssi = RT2560_RSSI(sc, desc->rssi);
1209 nf = RT2560_NOISE_FLOOR;
1210 if (ieee80211_radiotap_active(ic)) {
1211 struct rt2560_rx_radiotap_header *tap = &sc->sc_rxtap;
1212 uint32_t tsf_lo, tsf_hi;
1214 /* get timestamp (low and high 32 bits) */
1215 tsf_hi = RAL_READ(sc, RT2560_CSR17);
1216 tsf_lo = RAL_READ(sc, RT2560_CSR16);
1219 htole64(((uint64_t)tsf_hi << 32) | tsf_lo);
1221 tap->wr_rate = ieee80211_plcp2rate(desc->rate,
1222 (desc->flags & htole32(RT2560_RX_OFDM)) ?
1223 IEEE80211_T_OFDM : IEEE80211_T_CCK);
1224 tap->wr_antenna = sc->rx_ant;
1225 tap->wr_antsignal = nf + rssi;
1226 tap->wr_antnoise = nf;
1229 sc->sc_flags |= RT2560_F_INPUT_RUNNING;
1230 wh = mtod(m, struct ieee80211_frame *);
1231 ni = ieee80211_find_rxnode(ic,
1232 (struct ieee80211_frame_min *)wh);
1234 (void) ieee80211_input(ni, m, rssi, nf);
1235 ieee80211_free_node(ni);
1237 (void) ieee80211_input_all(ic, m, rssi, nf);
1239 sc->sc_flags &= ~RT2560_F_INPUT_RUNNING;
1240 skip: desc->flags = htole32(RT2560_RX_BUSY);
1242 DPRINTFN(sc, 15, "decryption done idx=%u\n", sc->rxq.cur_decrypt);
1244 sc->rxq.cur_decrypt =
1245 (sc->rxq.cur_decrypt + 1) % RT2560_RX_RING_COUNT;
1248 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1249 BUS_DMASYNC_PREWRITE);
1253 * Some frames were received. Pass them to the hardware cipher engine before
1254 * sending them to the 802.11 layer.
1257 rt2560_rx_intr(struct rt2560_softc *sc)
1259 struct rt2560_rx_desc *desc;
1260 struct rt2560_rx_data *data;
1262 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1263 BUS_DMASYNC_POSTREAD);
1266 desc = &sc->rxq.desc[sc->rxq.cur];
1267 data = &sc->rxq.data[sc->rxq.cur];
1269 if ((le32toh(desc->flags) & RT2560_RX_BUSY) ||
1270 (le32toh(desc->flags) & RT2560_RX_CIPHER_BUSY))
1275 if ((le32toh(desc->flags) & RT2560_RX_PHY_ERROR) ||
1276 (le32toh(desc->flags) & RT2560_RX_CRC_ERROR)) {
1278 * This should not happen since we did not request
1279 * to receive those frames when we filled RXCSR0.
1281 DPRINTFN(sc, 5, "PHY or CRC error flags 0x%08x\n",
1282 le32toh(desc->flags));
1286 if (((le32toh(desc->flags) >> 16) & 0xfff) > MCLBYTES) {
1287 DPRINTFN(sc, 5, "%s\n", "bad length");
1291 /* mark the frame for decryption */
1292 desc->flags |= htole32(RT2560_RX_CIPHER_BUSY);
1294 DPRINTFN(sc, 15, "rx done idx=%u\n", sc->rxq.cur);
1296 sc->rxq.cur = (sc->rxq.cur + 1) % RT2560_RX_RING_COUNT;
1299 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1300 BUS_DMASYNC_PREWRITE);
1303 RAL_WRITE(sc, RT2560_SECCSR0, RT2560_KICK_DECRYPT);
1307 rt2560_beacon_update(struct ieee80211vap *vap, int item)
1309 struct rt2560_vap *rvp = RT2560_VAP(vap);
1310 struct ieee80211_beacon_offsets *bo = &rvp->ral_bo;
1312 setbit(bo->bo_flags, item);
1316 * This function is called periodically in IBSS mode when a new beacon must be
1320 rt2560_beacon_expire(struct rt2560_softc *sc)
1322 struct ifnet *ifp = sc->sc_ifp;
1323 struct ieee80211com *ic = ifp->if_l2com;
1324 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1325 struct rt2560_vap *rvp = RT2560_VAP(vap);
1326 struct rt2560_tx_data *data;
1328 if (ic->ic_opmode != IEEE80211_M_IBSS &&
1329 ic->ic_opmode != IEEE80211_M_HOSTAP &&
1330 ic->ic_opmode != IEEE80211_M_MBSS)
1333 data = &sc->bcnq.data[sc->bcnq.next];
1335 * Don't send beacon if bsschan isn't set
1337 if (data->ni == NULL)
1340 bus_dmamap_sync(sc->bcnq.data_dmat, data->map, BUS_DMASYNC_POSTWRITE);
1341 bus_dmamap_unload(sc->bcnq.data_dmat, data->map);
1343 /* XXX 1 =>'s mcast frames which means all PS sta's will wakeup! */
1344 ieee80211_beacon_update(data->ni, &rvp->ral_bo, data->m, 1);
1346 rt2560_tx_bcn(sc, data->m, data->ni);
1348 DPRINTFN(sc, 15, "%s", "beacon expired\n");
1350 sc->bcnq.next = (sc->bcnq.next + 1) % RT2560_BEACON_RING_COUNT;
1355 rt2560_wakeup_expire(struct rt2560_softc *sc)
1357 DPRINTFN(sc, 2, "%s", "wakeup expired\n");
1361 rt2560_intr(void *arg)
1363 struct rt2560_softc *sc = arg;
1364 struct ifnet *ifp = sc->sc_ifp;
1367 /* disable interrupts */
1368 RAL_WRITE(sc, RT2560_CSR8, 0xffffffff);
1370 /* don't re-enable interrupts if we're shutting down */
1371 if (!(ifp->if_flags & IFF_RUNNING)) {
1375 r = RAL_READ(sc, RT2560_CSR7);
1376 RAL_WRITE(sc, RT2560_CSR7, r);
1378 if (r & RT2560_BEACON_EXPIRE)
1379 rt2560_beacon_expire(sc);
1381 if (r & RT2560_WAKEUP_EXPIRE)
1382 rt2560_wakeup_expire(sc);
1384 if (r & RT2560_ENCRYPTION_DONE)
1385 rt2560_encryption_intr(sc);
1387 if (r & RT2560_TX_DONE)
1390 if (r & RT2560_PRIO_DONE)
1391 rt2560_prio_intr(sc);
1393 if (r & RT2560_DECRYPTION_DONE)
1394 rt2560_decryption_intr(sc);
1396 if (r & RT2560_RX_DONE) {
1398 rt2560_encryption_intr(sc);
1401 /* re-enable interrupts */
1402 RAL_WRITE(sc, RT2560_CSR8, RT2560_INTR_MASK);
1405 #define RAL_SIFS 10 /* us */
1407 #define RT2560_TXRX_TURNAROUND 10 /* us */
1410 rt2560_plcp_signal(int rate)
1413 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1414 case 12: return 0xb;
1415 case 18: return 0xf;
1416 case 24: return 0xa;
1417 case 36: return 0xe;
1418 case 48: return 0x9;
1419 case 72: return 0xd;
1420 case 96: return 0x8;
1421 case 108: return 0xc;
1423 /* CCK rates (NB: not IEEE std, device-specific) */
1426 case 11: return 0x2;
1427 case 22: return 0x3;
1429 return 0xff; /* XXX unsupported/unknown rate */
1433 rt2560_setup_tx_desc(struct rt2560_softc *sc, struct rt2560_tx_desc *desc,
1434 uint32_t flags, int len, int rate, int encrypt, bus_addr_t physaddr)
1436 struct ifnet *ifp = sc->sc_ifp;
1437 struct ieee80211com *ic = ifp->if_l2com;
1438 uint16_t plcp_length;
1441 desc->flags = htole32(flags);
1442 desc->flags |= htole32(len << 16);
1444 desc->physaddr = htole32(physaddr);
1445 desc->wme = htole16(
1447 RT2560_LOGCWMIN(3) |
1448 RT2560_LOGCWMAX(8));
1450 /* setup PLCP fields */
1451 desc->plcp_signal = rt2560_plcp_signal(rate);
1452 desc->plcp_service = 4;
1454 len += IEEE80211_CRC_LEN;
1455 if (ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) {
1456 desc->flags |= htole32(RT2560_TX_OFDM);
1458 plcp_length = len & 0xfff;
1459 desc->plcp_length_hi = plcp_length >> 6;
1460 desc->plcp_length_lo = plcp_length & 0x3f;
1462 plcp_length = (16 * len + rate - 1) / rate;
1464 remainder = (16 * len) % 22;
1465 if (remainder != 0 && remainder < 7)
1466 desc->plcp_service |= RT2560_PLCP_LENGEXT;
1468 desc->plcp_length_hi = plcp_length >> 8;
1469 desc->plcp_length_lo = plcp_length & 0xff;
1471 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1472 desc->plcp_signal |= 0x08;
1476 desc->flags |= htole32(RT2560_TX_VALID);
1477 desc->flags |= encrypt ? htole32(RT2560_TX_CIPHER_BUSY)
1478 : htole32(RT2560_TX_BUSY);
1482 rt2560_tx_bcn(struct rt2560_softc *sc, struct mbuf *m0,
1483 struct ieee80211_node *ni)
1485 struct ieee80211vap *vap = ni->ni_vap;
1486 struct rt2560_tx_desc *desc;
1487 struct rt2560_tx_data *data;
1488 bus_dma_segment_t segs[RT2560_MAX_SCATTER];
1489 int nsegs, rate, error;
1491 desc = &sc->bcnq.desc[sc->bcnq.cur];
1492 data = &sc->bcnq.data[sc->bcnq.cur];
1494 /* XXX maybe a separate beacon rate? */
1495 rate = vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)].mgmtrate;
1497 error = bus_dmamap_load_mbuf_segment(sc->bcnq.data_dmat, data->map, m0,
1498 segs, 1, &nsegs, BUS_DMA_NOWAIT);
1500 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1506 if (ieee80211_radiotap_active_vap(vap)) {
1507 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1510 tap->wt_rate = rate;
1511 tap->wt_antenna = sc->tx_ant;
1513 ieee80211_radiotap_tx(vap, m0);
1519 rt2560_setup_tx_desc(sc, desc, RT2560_TX_IFS_NEWBACKOFF |
1520 RT2560_TX_TIMESTAMP, m0->m_pkthdr.len, rate, 0, segs->ds_addr);
1522 DPRINTFN(sc, 10, "sending beacon frame len=%u idx=%u rate=%u\n",
1523 m0->m_pkthdr.len, sc->bcnq.cur, rate);
1525 bus_dmamap_sync(sc->bcnq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1526 bus_dmamap_sync(sc->bcnq.desc_dmat, sc->bcnq.desc_map,
1527 BUS_DMASYNC_PREWRITE);
1529 sc->bcnq.cur = (sc->bcnq.cur + 1) % RT2560_BEACON_RING_COUNT;
1535 rt2560_tx_mgt(struct rt2560_softc *sc, struct mbuf *m0,
1536 struct ieee80211_node *ni)
1538 struct ieee80211vap *vap = ni->ni_vap;
1539 struct ieee80211com *ic = ni->ni_ic;
1540 struct rt2560_tx_desc *desc;
1541 struct rt2560_tx_data *data;
1542 struct ieee80211_frame *wh;
1543 struct ieee80211_key *k;
1544 bus_dma_segment_t segs[RT2560_MAX_SCATTER];
1547 int nsegs, rate, error;
1549 desc = &sc->prioq.desc[sc->prioq.cur];
1550 data = &sc->prioq.data[sc->prioq.cur];
1552 rate = vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)].mgmtrate;
1554 wh = mtod(m0, struct ieee80211_frame *);
1556 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1557 k = ieee80211_crypto_encap(ni, m0);
1564 error = bus_dmamap_load_mbuf_segment(sc->prioq.data_dmat, data->map, m0,
1565 segs, 1, &nsegs, 0);
1567 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1573 if (ieee80211_radiotap_active_vap(vap)) {
1574 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1577 tap->wt_rate = rate;
1578 tap->wt_antenna = sc->tx_ant;
1580 ieee80211_radiotap_tx(vap, m0);
1585 /* management frames are not taken into account for amrr */
1586 data->rix = IEEE80211_FIXED_RATE_NONE;
1588 wh = mtod(m0, struct ieee80211_frame *);
1590 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1591 flags |= RT2560_TX_ACK;
1593 dur = ieee80211_ack_duration(ic->ic_rt,
1594 rate, ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1595 *(uint16_t *)wh->i_dur = htole16(dur);
1597 /* tell hardware to add timestamp for probe responses */
1598 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
1599 IEEE80211_FC0_TYPE_MGT &&
1600 (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
1601 IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1602 flags |= RT2560_TX_TIMESTAMP;
1605 rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate, 0,
1608 bus_dmamap_sync(sc->prioq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1609 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map,
1610 BUS_DMASYNC_PREWRITE);
1612 DPRINTFN(sc, 10, "sending mgt frame len=%u idx=%u rate=%u\n",
1613 m0->m_pkthdr.len, sc->prioq.cur, rate);
1617 sc->prioq.cur = (sc->prioq.cur + 1) % RT2560_PRIO_RING_COUNT;
1618 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_PRIO);
1624 rt2560_sendprot(struct rt2560_softc *sc,
1625 const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate)
1627 struct ieee80211com *ic = ni->ni_ic;
1628 const struct ieee80211_frame *wh;
1629 struct rt2560_tx_desc *desc;
1630 struct rt2560_tx_data *data;
1632 int protrate, ackrate, pktlen, flags, isshort, error;
1634 bus_dma_segment_t segs[RT2560_MAX_SCATTER];
1637 KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY,
1638 ("protection %d", prot));
1640 wh = mtod(m, const struct ieee80211_frame *);
1641 pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
1643 protrate = ieee80211_ctl_rate(ic->ic_rt, rate);
1644 ackrate = ieee80211_ack_rate(ic->ic_rt, rate);
1646 isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0;
1647 dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort)
1648 + ieee80211_ack_duration(ic->ic_rt, rate, isshort);
1649 flags = RT2560_TX_MORE_FRAG;
1650 if (prot == IEEE80211_PROT_RTSCTS) {
1651 /* NB: CTS is the same size as an ACK */
1652 dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort);
1653 flags |= RT2560_TX_ACK;
1654 mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur);
1656 mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur);
1658 if (mprot == NULL) {
1659 /* XXX stat + msg */
1663 desc = &sc->txq.desc[sc->txq.cur_encrypt];
1664 data = &sc->txq.data[sc->txq.cur_encrypt];
1666 error = bus_dmamap_load_mbuf_segment(sc->txq.data_dmat, data->map,
1667 mprot, segs, 1, &nsegs, 0);
1669 device_printf(sc->sc_dev,
1670 "could not map mbuf (error %d)\n", error);
1676 data->ni = ieee80211_ref_node(ni);
1677 /* ctl frames are not taken into account for amrr */
1678 data->rix = IEEE80211_FIXED_RATE_NONE;
1680 rt2560_setup_tx_desc(sc, desc, flags, mprot->m_pkthdr.len, protrate, 1,
1683 bus_dmamap_sync(sc->txq.data_dmat, data->map,
1684 BUS_DMASYNC_PREWRITE);
1687 sc->txq.cur_encrypt = (sc->txq.cur_encrypt + 1) % RT2560_TX_RING_COUNT;
1693 rt2560_tx_raw(struct rt2560_softc *sc, struct mbuf *m0,
1694 struct ieee80211_node *ni, const struct ieee80211_bpf_params *params)
1696 struct ieee80211vap *vap = ni->ni_vap;
1697 struct ieee80211com *ic = ni->ni_ic;
1698 struct rt2560_tx_desc *desc;
1699 struct rt2560_tx_data *data;
1700 bus_dma_segment_t segs[RT2560_MAX_SCATTER];
1702 int nsegs, rate, error;
1704 desc = &sc->prioq.desc[sc->prioq.cur];
1705 data = &sc->prioq.data[sc->prioq.cur];
1707 rate = params->ibp_rate0;
1708 if (!ieee80211_isratevalid(ic->ic_rt, rate)) {
1709 /* XXX fall back to mcast/mgmt rate? */
1715 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
1716 flags |= RT2560_TX_ACK;
1717 if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) {
1718 error = rt2560_sendprot(sc, m0, ni,
1719 params->ibp_flags & IEEE80211_BPF_RTS ?
1720 IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY,
1726 flags |= RT2560_TX_LONG_RETRY | RT2560_TX_IFS_SIFS;
1729 error = bus_dmamap_load_mbuf_segment(sc->prioq.data_dmat, data->map, m0,
1730 segs, 1, &nsegs, 0);
1732 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1738 if (ieee80211_radiotap_active_vap(vap)) {
1739 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1742 tap->wt_rate = rate;
1743 tap->wt_antenna = sc->tx_ant;
1745 ieee80211_radiotap_tx(ni->ni_vap, m0);
1751 /* XXX need to setup descriptor ourself */
1752 rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len,
1753 rate, (params->ibp_flags & IEEE80211_BPF_CRYPTO) != 0,
1756 bus_dmamap_sync(sc->prioq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1757 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map,
1758 BUS_DMASYNC_PREWRITE);
1760 DPRINTFN(sc, 10, "sending raw frame len=%u idx=%u rate=%u\n",
1761 m0->m_pkthdr.len, sc->prioq.cur, rate);
1765 sc->prioq.cur = (sc->prioq.cur + 1) % RT2560_PRIO_RING_COUNT;
1766 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_PRIO);
1772 rt2560_tx_data(struct rt2560_softc *sc, struct mbuf *m0,
1773 struct ieee80211_node *ni)
1775 struct ieee80211vap *vap = ni->ni_vap;
1776 struct ieee80211com *ic = ni->ni_ic;
1777 struct rt2560_tx_desc *desc;
1778 struct rt2560_tx_data *data;
1779 struct ieee80211_frame *wh;
1780 const struct ieee80211_txparam *tp;
1781 struct ieee80211_key *k;
1783 bus_dma_segment_t segs[RT2560_MAX_SCATTER];
1786 int nsegs, rate, error;
1788 wh = mtod(m0, struct ieee80211_frame *);
1790 tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
1791 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1792 rate = tp->mcastrate;
1793 } else if (m0->m_flags & M_EAPOL) {
1794 rate = tp->mgmtrate;
1795 } else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) {
1796 rate = tp->ucastrate;
1798 (void) ieee80211_ratectl_rate(ni, NULL, 0);
1799 rate = ni->ni_txrate;
1802 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1803 k = ieee80211_crypto_encap(ni, m0);
1809 /* packet header may have moved, reset our local pointer */
1810 wh = mtod(m0, struct ieee80211_frame *);
1814 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1815 int prot = IEEE80211_PROT_NONE;
1816 if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold)
1817 prot = IEEE80211_PROT_RTSCTS;
1818 else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
1819 ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM)
1820 prot = ic->ic_protmode;
1821 if (prot != IEEE80211_PROT_NONE) {
1822 error = rt2560_sendprot(sc, m0, ni, prot, rate);
1827 flags |= RT2560_TX_LONG_RETRY | RT2560_TX_IFS_SIFS;
1831 data = &sc->txq.data[sc->txq.cur_encrypt];
1832 desc = &sc->txq.desc[sc->txq.cur_encrypt];
1834 error = bus_dmamap_load_mbuf_segment(sc->txq.data_dmat, data->map, m0,
1835 segs, 1, &nsegs, 0);
1836 if (error != 0 && error != EFBIG) {
1837 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1843 mnew = m_defrag(m0, MB_DONTWAIT);
1845 device_printf(sc->sc_dev,
1846 "could not defragment mbuf\n");
1852 error = bus_dmamap_load_mbuf_segment(sc->txq.data_dmat, data->map,
1853 m0, segs, 1, &nsegs, 0);
1855 device_printf(sc->sc_dev,
1856 "could not map mbuf (error %d)\n", error);
1861 /* packet header may have moved, reset our local pointer */
1862 wh = mtod(m0, struct ieee80211_frame *);
1865 if (ieee80211_radiotap_active_vap(vap)) {
1866 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1869 tap->wt_rate = rate;
1870 tap->wt_antenna = sc->tx_ant;
1872 ieee80211_radiotap_tx(vap, m0);
1878 /* remember link conditions for rate adaptation algorithm */
1879 if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE) {
1880 data->rix = ni->ni_txrate;
1881 /* XXX probably need last rssi value and not avg */
1882 data->rssi = ic->ic_node_getrssi(ni);
1884 data->rix = IEEE80211_FIXED_RATE_NONE;
1886 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1887 flags |= RT2560_TX_ACK;
1889 dur = ieee80211_ack_duration(ic->ic_rt,
1890 rate, ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1891 *(uint16_t *)wh->i_dur = htole16(dur);
1894 rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate, 1,
1897 bus_dmamap_sync(sc->txq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1898 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
1899 BUS_DMASYNC_PREWRITE);
1901 DPRINTFN(sc, 10, "sending data frame len=%u idx=%u rate=%u\n",
1902 m0->m_pkthdr.len, sc->txq.cur_encrypt, rate);
1906 sc->txq.cur_encrypt = (sc->txq.cur_encrypt + 1) % RT2560_TX_RING_COUNT;
1907 RAL_WRITE(sc, RT2560_SECCSR1, RT2560_KICK_ENCRYPT);
1913 rt2560_start_locked(struct ifnet *ifp)
1915 struct rt2560_softc *sc = ifp->if_softc;
1917 struct ieee80211_node *ni;
1920 IF_DEQUEUE(&ifp->if_snd, m);
1923 if (sc->txq.queued >= RT2560_TX_RING_COUNT - 1) {
1924 IF_PREPEND(&ifp->if_snd, m);
1925 ifp->if_flags |= IFF_OACTIVE;
1926 sc->sc_flags |= RT2560_F_DATA_OACTIVE;
1929 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1930 if (rt2560_tx_data(sc, m, ni) != 0) {
1931 ieee80211_free_node(ni);
1936 sc->sc_tx_timer = 5;
1941 rt2560_start(struct ifnet *ifp)
1943 rt2560_start_locked(ifp);
1947 rt2560_watchdog_callout(void *arg)
1949 struct rt2560_softc *sc = arg;
1950 struct ifnet *ifp = sc->sc_ifp;
1952 KASSERT(ifp->if_flags & IFF_RUNNING, ("not running"));
1954 if (sc->sc_invalid) /* card ejected */
1957 rt2560_encryption_intr(sc);
1960 if (sc->sc_tx_timer > 0 && --sc->sc_tx_timer == 0) {
1961 if_printf(ifp, "device timeout\n");
1962 rt2560_init_locked(sc);
1964 /* NB: callout is reset in rt2560_init() */
1967 callout_reset(&sc->watchdog_ch, hz, rt2560_watchdog_callout, sc);
1971 rt2560_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *ucred)
1973 struct rt2560_softc *sc = ifp->if_softc;
1974 struct ieee80211com *ic = ifp->if_l2com;
1975 struct ifreq *ifr = (struct ifreq *) data;
1976 int error = 0, startall = 0;
1980 if (ifp->if_flags & IFF_UP) {
1981 if ((ifp->if_flags & IFF_RUNNING) == 0) {
1982 rt2560_init_locked(sc);
1985 rt2560_update_promisc(ifp);
1987 if (ifp->if_flags & IFF_RUNNING)
1988 rt2560_stop_locked(sc);
1991 ieee80211_start_all(ic);
1994 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
1997 error = ether_ioctl(ifp, cmd, data);
2007 rt2560_bbp_write(struct rt2560_softc *sc, uint8_t reg, uint8_t val)
2012 for (ntries = 0; ntries < 100; ntries++) {
2013 if (!(RAL_READ(sc, RT2560_BBPCSR) & RT2560_BBP_BUSY))
2017 if (ntries == 100) {
2018 device_printf(sc->sc_dev, "could not write to BBP\n");
2022 tmp = RT2560_BBP_WRITE | RT2560_BBP_BUSY | reg << 8 | val;
2023 RAL_WRITE(sc, RT2560_BBPCSR, tmp);
2025 DPRINTFN(sc, 15, "BBP R%u <- 0x%02x\n", reg, val);
2029 rt2560_bbp_read(struct rt2560_softc *sc, uint8_t reg)
2034 for (ntries = 0; ntries < 100; ntries++) {
2035 if (!(RAL_READ(sc, RT2560_BBPCSR) & RT2560_BBP_BUSY))
2039 if (ntries == 100) {
2040 device_printf(sc->sc_dev, "could not read from BBP\n");
2044 val = RT2560_BBP_BUSY | reg << 8;
2045 RAL_WRITE(sc, RT2560_BBPCSR, val);
2047 for (ntries = 0; ntries < 100; ntries++) {
2048 val = RAL_READ(sc, RT2560_BBPCSR);
2049 if (!(val & RT2560_BBP_BUSY))
2054 device_printf(sc->sc_dev, "could not read from BBP\n");
2059 rt2560_rf_write(struct rt2560_softc *sc, uint8_t reg, uint32_t val)
2064 for (ntries = 0; ntries < 100; ntries++) {
2065 if (!(RAL_READ(sc, RT2560_RFCSR) & RT2560_RF_BUSY))
2069 if (ntries == 100) {
2070 device_printf(sc->sc_dev, "could not write to RF\n");
2074 tmp = RT2560_RF_BUSY | RT2560_RF_20BIT | (val & 0xfffff) << 2 |
2076 RAL_WRITE(sc, RT2560_RFCSR, tmp);
2078 /* remember last written value in sc */
2079 sc->rf_regs[reg] = val;
2081 DPRINTFN(sc, 15, "RF R[%u] <- 0x%05x\n", reg & 0x3, val & 0xfffff);
2085 rt2560_set_chan(struct rt2560_softc *sc, struct ieee80211_channel *c)
2087 struct ifnet *ifp = sc->sc_ifp;
2088 struct ieee80211com *ic = ifp->if_l2com;
2092 chan = ieee80211_chan2ieee(ic, c);
2093 KASSERT(chan != 0 && chan != IEEE80211_CHAN_ANY, ("chan 0x%x", chan));
2095 if (IEEE80211_IS_CHAN_2GHZ(c))
2096 power = min(sc->txpow[chan - 1], 31);
2100 /* adjust txpower using ifconfig settings */
2101 power -= (100 - ic->ic_txpowlimit) / 8;
2103 DPRINTFN(sc, 2, "setting channel to %u, txpower to %u\n", chan, power);
2105 switch (sc->rf_rev) {
2106 case RT2560_RF_2522:
2107 rt2560_rf_write(sc, RAL_RF1, 0x00814);
2108 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2522_r2[chan - 1]);
2109 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
2112 case RT2560_RF_2523:
2113 rt2560_rf_write(sc, RAL_RF1, 0x08804);
2114 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2523_r2[chan - 1]);
2115 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x38044);
2116 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2119 case RT2560_RF_2524:
2120 rt2560_rf_write(sc, RAL_RF1, 0x0c808);
2121 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2524_r2[chan - 1]);
2122 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
2123 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2126 case RT2560_RF_2525:
2127 rt2560_rf_write(sc, RAL_RF1, 0x08808);
2128 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525_hi_r2[chan - 1]);
2129 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2130 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2132 rt2560_rf_write(sc, RAL_RF1, 0x08808);
2133 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525_r2[chan - 1]);
2134 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2135 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2138 case RT2560_RF_2525E:
2139 rt2560_rf_write(sc, RAL_RF1, 0x08808);
2140 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525e_r2[chan - 1]);
2141 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2142 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00286 : 0x00282);
2145 case RT2560_RF_2526:
2146 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2526_hi_r2[chan - 1]);
2147 rt2560_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
2148 rt2560_rf_write(sc, RAL_RF1, 0x08804);
2150 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2526_r2[chan - 1]);
2151 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2152 rt2560_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
2156 case RT2560_RF_5222:
2157 for (i = 0; rt2560_rf5222[i].chan != chan; i++);
2159 rt2560_rf_write(sc, RAL_RF1, rt2560_rf5222[i].r1);
2160 rt2560_rf_write(sc, RAL_RF2, rt2560_rf5222[i].r2);
2161 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
2162 rt2560_rf_write(sc, RAL_RF4, rt2560_rf5222[i].r4);
2165 kprintf("unknown ral rev=%d\n", sc->rf_rev);
2169 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
2170 /* set Japan filter bit for channel 14 */
2171 tmp = rt2560_bbp_read(sc, 70);
2173 tmp &= ~RT2560_JAPAN_FILTER;
2175 tmp |= RT2560_JAPAN_FILTER;
2177 rt2560_bbp_write(sc, 70, tmp);
2179 /* clear CRC errors */
2180 RAL_READ(sc, RT2560_CNT0);
2185 rt2560_set_channel(struct ieee80211com *ic)
2187 struct ifnet *ifp = ic->ic_ifp;
2188 struct rt2560_softc *sc = ifp->if_softc;
2190 rt2560_set_chan(sc, ic->ic_curchan);
2196 * Disable RF auto-tuning.
2199 rt2560_disable_rf_tune(struct rt2560_softc *sc)
2203 if (sc->rf_rev != RT2560_RF_2523) {
2204 tmp = sc->rf_regs[RAL_RF1] & ~RAL_RF1_AUTOTUNE;
2205 rt2560_rf_write(sc, RAL_RF1, tmp);
2208 tmp = sc->rf_regs[RAL_RF3] & ~RAL_RF3_AUTOTUNE;
2209 rt2560_rf_write(sc, RAL_RF3, tmp);
2211 DPRINTFN(sc, 2, "%s", "disabling RF autotune\n");
2216 * Refer to IEEE Std 802.11-1999 pp. 123 for more information on TSF
2220 rt2560_enable_tsf_sync(struct rt2560_softc *sc)
2222 struct ifnet *ifp = sc->sc_ifp;
2223 struct ieee80211com *ic = ifp->if_l2com;
2224 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2225 uint16_t logcwmin, preload;
2228 /* first, disable TSF synchronization */
2229 RAL_WRITE(sc, RT2560_CSR14, 0);
2231 tmp = 16 * vap->iv_bss->ni_intval;
2232 RAL_WRITE(sc, RT2560_CSR12, tmp);
2234 RAL_WRITE(sc, RT2560_CSR13, 0);
2237 preload = (vap->iv_opmode == IEEE80211_M_STA) ? 384 : 1024;
2238 tmp = logcwmin << 16 | preload;
2239 RAL_WRITE(sc, RT2560_BCNOCSR, tmp);
2241 /* finally, enable TSF synchronization */
2242 tmp = RT2560_ENABLE_TSF | RT2560_ENABLE_TBCN;
2243 if (ic->ic_opmode == IEEE80211_M_STA)
2244 tmp |= RT2560_ENABLE_TSF_SYNC(1);
2246 tmp |= RT2560_ENABLE_TSF_SYNC(2) |
2247 RT2560_ENABLE_BEACON_GENERATOR;
2248 RAL_WRITE(sc, RT2560_CSR14, tmp);
2250 DPRINTF(sc, "%s", "enabling TSF synchronization\n");
2254 rt2560_enable_tsf(struct rt2560_softc *sc)
2256 RAL_WRITE(sc, RT2560_CSR14, 0);
2257 RAL_WRITE(sc, RT2560_CSR14,
2258 RT2560_ENABLE_TSF_SYNC(2) | RT2560_ENABLE_TSF);
2262 rt2560_update_plcp(struct rt2560_softc *sc)
2264 struct ifnet *ifp = sc->sc_ifp;
2265 struct ieee80211com *ic = ifp->if_l2com;
2267 /* no short preamble for 1Mbps */
2268 RAL_WRITE(sc, RT2560_PLCP1MCSR, 0x00700400);
2270 if (!(ic->ic_flags & IEEE80211_F_SHPREAMBLE)) {
2271 /* values taken from the reference driver */
2272 RAL_WRITE(sc, RT2560_PLCP2MCSR, 0x00380401);
2273 RAL_WRITE(sc, RT2560_PLCP5p5MCSR, 0x00150402);
2274 RAL_WRITE(sc, RT2560_PLCP11MCSR, 0x000b8403);
2276 /* same values as above or'ed 0x8 */
2277 RAL_WRITE(sc, RT2560_PLCP2MCSR, 0x00380409);
2278 RAL_WRITE(sc, RT2560_PLCP5p5MCSR, 0x0015040a);
2279 RAL_WRITE(sc, RT2560_PLCP11MCSR, 0x000b840b);
2282 DPRINTF(sc, "updating PLCP for %s preamble\n",
2283 (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ? "short" : "long");
2287 * This function can be called by ieee80211_set_shortslottime(). Refer to
2288 * IEEE Std 802.11-1999 pp. 85 to know how these values are computed.
2291 rt2560_update_slot(struct ifnet *ifp)
2293 struct rt2560_softc *sc = ifp->if_softc;
2294 struct ieee80211com *ic = ifp->if_l2com;
2296 uint16_t tx_sifs, tx_pifs, tx_difs, eifs;
2299 #ifndef FORCE_SLOTTIME
2300 slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
2303 * Setting slot time according to "short slot time" capability
2304 * in beacon/probe_resp seems to cause problem to acknowledge
2305 * certain AP's data frames transimitted at CCK/DS rates: the
2306 * problematic AP keeps retransmitting data frames, probably
2307 * because MAC level acks are not received by hardware.
2308 * So we cheat a little bit here by claiming we are capable of
2309 * "short slot time" but setting hardware slot time to the normal
2310 * slot time. ral(4) does not seem to have trouble to receive
2311 * frames transmitted using short slot time even if hardware
2312 * slot time is set to normal slot time. If we didn't use this
2313 * trick, we would have to claim that short slot time is not
2314 * supported; this would give relative poor RX performance
2315 * (-1Mb~-2Mb lower) and the _whole_ BSS would stop using short
2321 /* update the MAC slot boundaries */
2322 tx_sifs = RAL_SIFS - RT2560_TXRX_TURNAROUND;
2323 tx_pifs = tx_sifs + slottime;
2324 tx_difs = tx_sifs + 2 * slottime;
2325 eifs = (ic->ic_curmode == IEEE80211_MODE_11B) ? 364 : 60;
2327 tmp = RAL_READ(sc, RT2560_CSR11);
2328 tmp = (tmp & ~0x1f00) | slottime << 8;
2329 RAL_WRITE(sc, RT2560_CSR11, tmp);
2331 tmp = tx_pifs << 16 | tx_sifs;
2332 RAL_WRITE(sc, RT2560_CSR18, tmp);
2334 tmp = eifs << 16 | tx_difs;
2335 RAL_WRITE(sc, RT2560_CSR19, tmp);
2337 DPRINTF(sc, "setting slottime to %uus\n", slottime);
2341 rt2560_set_basicrates(struct rt2560_softc *sc)
2343 struct ifnet *ifp = sc->sc_ifp;
2344 struct ieee80211com *ic = ifp->if_l2com;
2346 /* update basic rate set */
2347 if (ic->ic_curmode == IEEE80211_MODE_11B) {
2348 /* 11b basic rates: 1, 2Mbps */
2349 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x3);
2350 } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan)) {
2351 /* 11a basic rates: 6, 12, 24Mbps */
2352 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x150);
2354 /* 11g basic rates: 1, 2, 5.5, 11, 6, 12, 24Mbps */
2355 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x15f);
2360 rt2560_update_led(struct rt2560_softc *sc, int led1, int led2)
2364 /* set ON period to 70ms and OFF period to 30ms */
2365 tmp = led1 << 16 | led2 << 17 | 70 << 8 | 30;
2366 RAL_WRITE(sc, RT2560_LEDCSR, tmp);
2370 rt2560_set_bssid(struct rt2560_softc *sc, const uint8_t *bssid)
2374 tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24;
2375 RAL_WRITE(sc, RT2560_CSR5, tmp);
2377 tmp = bssid[4] | bssid[5] << 8;
2378 RAL_WRITE(sc, RT2560_CSR6, tmp);
2380 DPRINTF(sc, "setting BSSID to %6D\n", bssid, ":");
2384 rt2560_set_macaddr(struct rt2560_softc *sc, uint8_t *addr)
2388 tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24;
2389 RAL_WRITE(sc, RT2560_CSR3, tmp);
2391 tmp = addr[4] | addr[5] << 8;
2392 RAL_WRITE(sc, RT2560_CSR4, tmp);
2394 DPRINTF(sc, "setting MAC address to %6D\n", addr, ":");
2398 rt2560_get_macaddr(struct rt2560_softc *sc, uint8_t *addr)
2402 tmp = RAL_READ(sc, RT2560_CSR3);
2403 addr[0] = tmp & 0xff;
2404 addr[1] = (tmp >> 8) & 0xff;
2405 addr[2] = (tmp >> 16) & 0xff;
2406 addr[3] = (tmp >> 24);
2408 tmp = RAL_READ(sc, RT2560_CSR4);
2409 addr[4] = tmp & 0xff;
2410 addr[5] = (tmp >> 8) & 0xff;
2414 rt2560_update_promisc(struct ifnet *ifp)
2416 struct rt2560_softc *sc = ifp->if_softc;
2419 tmp = RAL_READ(sc, RT2560_RXCSR0);
2421 tmp &= ~RT2560_DROP_NOT_TO_ME;
2422 if (!(ifp->if_flags & IFF_PROMISC))
2423 tmp |= RT2560_DROP_NOT_TO_ME;
2425 RAL_WRITE(sc, RT2560_RXCSR0, tmp);
2427 DPRINTF(sc, "%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
2428 "entering" : "leaving");
2432 rt2560_get_rf(int rev)
2435 case RT2560_RF_2522: return "RT2522";
2436 case RT2560_RF_2523: return "RT2523";
2437 case RT2560_RF_2524: return "RT2524";
2438 case RT2560_RF_2525: return "RT2525";
2439 case RT2560_RF_2525E: return "RT2525e";
2440 case RT2560_RF_2526: return "RT2526";
2441 case RT2560_RF_5222: return "RT5222";
2442 default: return "unknown";
2447 rt2560_read_config(struct rt2560_softc *sc)
2452 val = rt2560_eeprom_read(sc, RT2560_EEPROM_CONFIG0);
2453 sc->rf_rev = (val >> 11) & 0x7;
2454 sc->hw_radio = (val >> 10) & 0x1;
2455 sc->led_mode = (val >> 6) & 0x7;
2456 sc->rx_ant = (val >> 4) & 0x3;
2457 sc->tx_ant = (val >> 2) & 0x3;
2458 sc->nb_ant = val & 0x3;
2460 /* read default values for BBP registers */
2461 for (i = 0; i < 16; i++) {
2462 val = rt2560_eeprom_read(sc, RT2560_EEPROM_BBP_BASE + i);
2463 if (val == 0 || val == 0xffff)
2466 sc->bbp_prom[i].reg = val >> 8;
2467 sc->bbp_prom[i].val = val & 0xff;
2470 /* read Tx power for all b/g channels */
2471 for (i = 0; i < 14 / 2; i++) {
2472 val = rt2560_eeprom_read(sc, RT2560_EEPROM_TXPOWER + i);
2473 sc->txpow[i * 2] = val & 0xff;
2474 sc->txpow[i * 2 + 1] = val >> 8;
2476 for (i = 0; i < 14; ++i) {
2477 if (sc->txpow[i] > 31)
2481 val = rt2560_eeprom_read(sc, RT2560_EEPROM_CALIBRATE);
2482 if ((val & 0xff) == 0xff)
2483 sc->rssi_corr = RT2560_DEFAULT_RSSI_CORR;
2485 sc->rssi_corr = val & 0xff;
2486 DPRINTF(sc, "rssi correction %d, calibrate 0x%02x\n",
2487 sc->rssi_corr, val);
2492 rt2560_scan_start(struct ieee80211com *ic)
2494 struct ifnet *ifp = ic->ic_ifp;
2495 struct rt2560_softc *sc = ifp->if_softc;
2497 /* abort TSF synchronization */
2498 RAL_WRITE(sc, RT2560_CSR14, 0);
2499 rt2560_set_bssid(sc, ifp->if_broadcastaddr);
2503 rt2560_scan_end(struct ieee80211com *ic)
2505 struct ifnet *ifp = ic->ic_ifp;
2506 struct rt2560_softc *sc = ifp->if_softc;
2507 struct ieee80211vap *vap = ic->ic_scan->ss_vap;
2509 rt2560_enable_tsf_sync(sc);
2510 /* XXX keep local copy */
2511 rt2560_set_bssid(sc, vap->iv_bss->ni_bssid);
2515 rt2560_bbp_init(struct rt2560_softc *sc)
2517 #define N(a) (sizeof (a) / sizeof ((a)[0]))
2520 /* wait for BBP to be ready */
2521 for (ntries = 0; ntries < 100; ntries++) {
2522 if (rt2560_bbp_read(sc, RT2560_BBP_VERSION) != 0)
2526 if (ntries == 100) {
2527 device_printf(sc->sc_dev, "timeout waiting for BBP\n");
2531 /* initialize BBP registers to default values */
2532 for (i = 0; i < N(rt2560_def_bbp); i++) {
2533 rt2560_bbp_write(sc, rt2560_def_bbp[i].reg,
2534 rt2560_def_bbp[i].val);
2537 /* initialize BBP registers to values stored in EEPROM */
2538 for (i = 0; i < 16; i++) {
2539 if (sc->bbp_prom[i].reg == 0 && sc->bbp_prom[i].val == 0)
2541 rt2560_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
2543 rt2560_bbp_write(sc, 17, 0x48); /* XXX restore bbp17 */
2550 rt2560_set_txantenna(struct rt2560_softc *sc, int antenna)
2555 tx = rt2560_bbp_read(sc, RT2560_BBP_TX) & ~RT2560_BBP_ANTMASK;
2557 tx |= RT2560_BBP_ANTA;
2558 else if (antenna == 2)
2559 tx |= RT2560_BBP_ANTB;
2561 tx |= RT2560_BBP_DIVERSITY;
2563 /* need to force I/Q flip for RF 2525e, 2526 and 5222 */
2564 if (sc->rf_rev == RT2560_RF_2525E || sc->rf_rev == RT2560_RF_2526 ||
2565 sc->rf_rev == RT2560_RF_5222)
2566 tx |= RT2560_BBP_FLIPIQ;
2568 rt2560_bbp_write(sc, RT2560_BBP_TX, tx);
2570 /* update values for CCK and OFDM in BBPCSR1 */
2571 tmp = RAL_READ(sc, RT2560_BBPCSR1) & ~0x00070007;
2572 tmp |= (tx & 0x7) << 16 | (tx & 0x7);
2573 RAL_WRITE(sc, RT2560_BBPCSR1, tmp);
2577 rt2560_set_rxantenna(struct rt2560_softc *sc, int antenna)
2581 rx = rt2560_bbp_read(sc, RT2560_BBP_RX) & ~RT2560_BBP_ANTMASK;
2583 rx |= RT2560_BBP_ANTA;
2584 else if (antenna == 2)
2585 rx |= RT2560_BBP_ANTB;
2587 rx |= RT2560_BBP_DIVERSITY;
2589 /* need to force no I/Q flip for RF 2525e and 2526 */
2590 if (sc->rf_rev == RT2560_RF_2525E || sc->rf_rev == RT2560_RF_2526)
2591 rx &= ~RT2560_BBP_FLIPIQ;
2593 rt2560_bbp_write(sc, RT2560_BBP_RX, rx);
2597 rt2560_init_locked(struct rt2560_softc *sc)
2599 #define N(a) (sizeof (a) / sizeof ((a)[0]))
2600 struct ifnet *ifp = sc->sc_ifp;
2601 struct ieee80211com *ic = ifp->if_l2com;
2605 rt2560_stop_locked(sc);
2607 /* setup tx rings */
2608 tmp = RT2560_PRIO_RING_COUNT << 24 |
2609 RT2560_ATIM_RING_COUNT << 16 |
2610 RT2560_TX_RING_COUNT << 8 |
2611 RT2560_TX_DESC_SIZE;
2613 /* rings must be initialized in this exact order */
2614 RAL_WRITE(sc, RT2560_TXCSR2, tmp);
2615 RAL_WRITE(sc, RT2560_TXCSR3, sc->txq.physaddr);
2616 RAL_WRITE(sc, RT2560_TXCSR5, sc->prioq.physaddr);
2617 RAL_WRITE(sc, RT2560_TXCSR4, sc->atimq.physaddr);
2618 RAL_WRITE(sc, RT2560_TXCSR6, sc->bcnq.physaddr);
2621 tmp = RT2560_RX_RING_COUNT << 8 | RT2560_RX_DESC_SIZE;
2623 RAL_WRITE(sc, RT2560_RXCSR1, tmp);
2624 RAL_WRITE(sc, RT2560_RXCSR2, sc->rxq.physaddr);
2626 /* initialize MAC registers to default values */
2627 for (i = 0; i < N(rt2560_def_mac); i++)
2628 RAL_WRITE(sc, rt2560_def_mac[i].reg, rt2560_def_mac[i].val);
2630 rt2560_set_macaddr(sc, IF_LLADDR(ifp));
2632 /* set basic rate set (will be updated later) */
2633 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x153);
2635 rt2560_update_slot(ifp);
2636 rt2560_update_plcp(sc);
2637 rt2560_update_led(sc, 0, 0);
2639 RAL_WRITE(sc, RT2560_CSR1, RT2560_RESET_ASIC);
2640 RAL_WRITE(sc, RT2560_CSR1, RT2560_HOST_READY);
2642 if (rt2560_bbp_init(sc) != 0) {
2647 rt2560_set_txantenna(sc, sc->tx_ant);
2648 rt2560_set_rxantenna(sc, sc->rx_ant);
2650 /* set default BSS channel */
2651 rt2560_set_chan(sc, ic->ic_curchan);
2654 tmp = RT2560_DROP_PHY_ERROR | RT2560_DROP_CRC_ERROR;
2655 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2656 tmp |= RT2560_DROP_CTL | RT2560_DROP_VERSION_ERROR;
2657 if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
2658 ic->ic_opmode != IEEE80211_M_MBSS)
2659 tmp |= RT2560_DROP_TODS;
2660 if (!(ifp->if_flags & IFF_PROMISC))
2661 tmp |= RT2560_DROP_NOT_TO_ME;
2663 RAL_WRITE(sc, RT2560_RXCSR0, tmp);
2665 /* clear old FCS and Rx FIFO errors */
2666 RAL_READ(sc, RT2560_CNT0);
2667 RAL_READ(sc, RT2560_CNT4);
2669 /* clear any pending interrupts */
2670 RAL_WRITE(sc, RT2560_CSR7, 0xffffffff);
2672 /* enable interrupts */
2673 RAL_WRITE(sc, RT2560_CSR8, RT2560_INTR_MASK);
2675 ifp->if_flags &= ~IFF_OACTIVE;
2676 ifp->if_flags |= IFF_RUNNING;
2678 callout_reset(&sc->watchdog_ch, hz, rt2560_watchdog_callout, sc);
2683 rt2560_init(void *priv)
2685 struct rt2560_softc *sc = priv;
2686 struct ifnet *ifp = sc->sc_ifp;
2687 struct ieee80211com *ic = ifp->if_l2com;
2689 rt2560_init_locked(sc);
2691 if (ifp->if_flags & IFF_RUNNING)
2692 ieee80211_start_all(ic); /* start all vap's */
2696 rt2560_stop_locked(struct rt2560_softc *sc)
2698 struct ifnet *ifp = sc->sc_ifp;
2699 volatile int *flags = &sc->sc_flags;
2701 while (*flags & RT2560_F_INPUT_RUNNING)
2702 zsleep(sc, &wlan_global_serializer, 0, "ralrunning", hz/10);
2704 callout_stop(&sc->watchdog_ch);
2705 sc->sc_tx_timer = 0;
2707 if (ifp->if_flags & IFF_RUNNING) {
2708 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2711 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_ABORT_TX);
2714 RAL_WRITE(sc, RT2560_RXCSR0, RT2560_DISABLE_RX);
2716 /* reset ASIC (imply reset BBP) */
2717 RAL_WRITE(sc, RT2560_CSR1, RT2560_RESET_ASIC);
2718 RAL_WRITE(sc, RT2560_CSR1, 0);
2720 /* disable interrupts */
2721 RAL_WRITE(sc, RT2560_CSR8, 0xffffffff);
2723 /* reset Tx and Rx rings */
2724 rt2560_reset_tx_ring(sc, &sc->txq);
2725 rt2560_reset_tx_ring(sc, &sc->atimq);
2726 rt2560_reset_tx_ring(sc, &sc->prioq);
2727 rt2560_reset_tx_ring(sc, &sc->bcnq);
2728 rt2560_reset_rx_ring(sc, &sc->rxq);
2730 sc->sc_flags &= ~(RT2560_F_PRIO_OACTIVE | RT2560_F_DATA_OACTIVE);
2734 rt2560_stop(void *arg)
2736 struct rt2560_softc *sc = arg;
2738 rt2560_stop_locked(sc);
2742 rt2560_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2743 const struct ieee80211_bpf_params *params)
2745 struct ieee80211com *ic = ni->ni_ic;
2746 struct ifnet *ifp = ic->ic_ifp;
2747 struct rt2560_softc *sc = ifp->if_softc;
2749 /* prevent management frames from being sent if we're not ready */
2750 if (!(ifp->if_flags & IFF_RUNNING)) {
2752 ieee80211_free_node(ni);
2755 if (sc->prioq.queued >= RT2560_PRIO_RING_COUNT) {
2756 ifp->if_flags |= IFF_OACTIVE;
2757 sc->sc_flags |= RT2560_F_PRIO_OACTIVE;
2759 ieee80211_free_node(ni);
2760 return ENOBUFS; /* XXX */
2765 if (params == NULL) {
2767 * Legacy path; interpret frame contents to decide
2768 * precisely how to send the frame.
2770 if (rt2560_tx_mgt(sc, m, ni) != 0)
2774 * Caller supplied explicit parameters to use in
2775 * sending the frame.
2777 if (rt2560_tx_raw(sc, m, ni, params))
2780 sc->sc_tx_timer = 5;
2785 ieee80211_free_node(ni);
2786 return EIO; /* XXX */