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.
20 #include <sys/cdefs.h>
21 __FBSDID("$FreeBSD$");
24 * Ralink Technology RT2560 chipset driver
25 * http://www.ralinktech.com/
28 #include <sys/param.h>
29 #include <sys/sysctl.h>
30 #include <sys/sockio.h>
32 #include <sys/kernel.h>
33 #include <sys/socket.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
37 #include <sys/mutex.h>
38 #include <sys/module.h>
40 #include <sys/endian.h>
42 #include <machine/bus.h>
43 #include <machine/resource.h>
48 #include <net/if_arp.h>
49 #include <net/ethernet.h>
50 #include <net/if_dl.h>
51 #include <net/if_media.h>
52 #include <net/if_types.h>
54 #include <net80211/ieee80211_var.h>
55 #include <net80211/ieee80211_radiotap.h>
56 #include <net80211/ieee80211_regdomain.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/ral/if_ralrate.h>
65 #include <dev/ral/rt2560reg.h>
66 #include <dev/ral/rt2560var.h>
68 #define RT2560_RSSI(sc, rssi) \
69 ((rssi) > (RT2560_NOISE_FLOOR + (sc)->rssi_corr) ? \
70 ((rssi) - RT2560_NOISE_FLOOR - (sc)->rssi_corr) : 0)
73 #define DPRINTF(x) do { if (ral_debug > 0) printf x; } while (0)
74 #define DPRINTFN(n, x) do { if (ral_debug >= (n)) printf x; } while (0)
78 #define DPRINTFN(n, x)
81 static void rt2560_dma_map_addr(void *, bus_dma_segment_t *, int,
83 static int rt2560_alloc_tx_ring(struct rt2560_softc *,
84 struct rt2560_tx_ring *, int);
85 static void rt2560_reset_tx_ring(struct rt2560_softc *,
86 struct rt2560_tx_ring *);
87 static void rt2560_free_tx_ring(struct rt2560_softc *,
88 struct rt2560_tx_ring *);
89 static int rt2560_alloc_rx_ring(struct rt2560_softc *,
90 struct rt2560_rx_ring *, int);
91 static void rt2560_reset_rx_ring(struct rt2560_softc *,
92 struct rt2560_rx_ring *);
93 static void rt2560_free_rx_ring(struct rt2560_softc *,
94 struct rt2560_rx_ring *);
95 static struct ieee80211_node *rt2560_node_alloc(
96 struct ieee80211_node_table *);
97 static int rt2560_media_change(struct ifnet *);
98 static void rt2560_iter_func(void *, struct ieee80211_node *);
99 static void rt2560_update_rssadapt(void *);
100 static int rt2560_newstate(struct ieee80211com *,
101 enum ieee80211_state, int);
102 static uint16_t rt2560_eeprom_read(struct rt2560_softc *, uint8_t);
103 static void rt2560_encryption_intr(struct rt2560_softc *);
104 static void rt2560_tx_intr(struct rt2560_softc *);
105 static void rt2560_prio_intr(struct rt2560_softc *);
106 static void rt2560_decryption_intr(struct rt2560_softc *);
107 static void rt2560_rx_intr(struct rt2560_softc *);
108 static void rt2560_beacon_update(struct ieee80211com *, int item);
109 static void rt2560_beacon_expire(struct rt2560_softc *);
110 static void rt2560_wakeup_expire(struct rt2560_softc *);
111 static uint8_t rt2560_rxrate(struct rt2560_rx_desc *);
112 static int rt2560_ack_rate(struct ieee80211com *, int);
113 static void rt2560_scan_start(struct ieee80211com *);
114 static void rt2560_scan_end(struct ieee80211com *);
115 static void rt2560_set_channel(struct ieee80211com *);
116 static uint16_t rt2560_txtime(int, int, uint32_t);
117 static uint8_t rt2560_plcp_signal(int);
118 static void rt2560_setup_tx_desc(struct rt2560_softc *,
119 struct rt2560_tx_desc *, uint32_t, int, int, int,
121 static int rt2560_tx_bcn(struct rt2560_softc *, struct mbuf *,
122 struct ieee80211_node *);
123 static int rt2560_tx_mgt(struct rt2560_softc *, struct mbuf *,
124 struct ieee80211_node *);
125 static struct mbuf *rt2560_get_rts(struct rt2560_softc *,
126 struct ieee80211_frame *, uint16_t);
127 static int rt2560_tx_data(struct rt2560_softc *, struct mbuf *,
128 struct ieee80211_node *);
129 static void rt2560_start(struct ifnet *);
130 static void rt2560_watchdog(void *);
131 static int rt2560_reset(struct ifnet *);
132 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_update_plcp(struct rt2560_softc *);
145 static void rt2560_update_slot(struct ifnet *);
146 static void rt2560_set_basicrates(struct rt2560_softc *);
147 static void rt2560_update_led(struct rt2560_softc *, int, int);
148 static void rt2560_set_bssid(struct rt2560_softc *, const uint8_t *);
149 static void rt2560_set_macaddr(struct rt2560_softc *, uint8_t *);
150 static void rt2560_get_macaddr(struct rt2560_softc *, uint8_t *);
151 static void rt2560_update_promisc(struct rt2560_softc *);
152 static const char *rt2560_get_rf(int);
153 static void rt2560_read_config(struct rt2560_softc *);
154 static int rt2560_bbp_init(struct rt2560_softc *);
155 static void rt2560_set_txantenna(struct rt2560_softc *, int);
156 static void rt2560_set_rxantenna(struct rt2560_softc *, int);
157 static void rt2560_init(void *);
158 static int rt2560_raw_xmit(struct ieee80211_node *, struct mbuf *,
159 const struct ieee80211_bpf_params *);
161 static const struct {
164 } rt2560_def_mac[] = {
168 static const struct {
171 } rt2560_def_bbp[] = {
175 static const uint32_t rt2560_rf2522_r2[] = RT2560_RF2522_R2;
176 static const uint32_t rt2560_rf2523_r2[] = RT2560_RF2523_R2;
177 static const uint32_t rt2560_rf2524_r2[] = RT2560_RF2524_R2;
178 static const uint32_t rt2560_rf2525_r2[] = RT2560_RF2525_R2;
179 static const uint32_t rt2560_rf2525_hi_r2[] = RT2560_RF2525_HI_R2;
180 static const uint32_t rt2560_rf2525e_r2[] = RT2560_RF2525E_R2;
181 static const uint32_t rt2560_rf2526_r2[] = RT2560_RF2526_R2;
182 static const uint32_t rt2560_rf2526_hi_r2[] = RT2560_RF2526_HI_R2;
184 static const struct {
187 } rt2560_rf5222[] = {
192 rt2560_attach(device_t dev, int id)
194 struct rt2560_softc *sc = device_get_softc(dev);
195 struct ieee80211com *ic = &sc->sc_ic;
201 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
202 MTX_DEF | MTX_RECURSE);
204 callout_init_mtx(&sc->watchdog_ch, &sc->sc_mtx, 0);
205 callout_init(&sc->rssadapt_ch, CALLOUT_MPSAFE);
207 /* retrieve RT2560 rev. no */
208 sc->asic_rev = RAL_READ(sc, RT2560_CSR0);
210 /* retrieve MAC address */
211 rt2560_get_macaddr(sc, ic->ic_myaddr);
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_ETHER);
254 device_printf(sc->sc_dev, "can not if_alloc()\n");
259 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
260 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
261 ifp->if_init = rt2560_init;
262 ifp->if_ioctl = rt2560_ioctl;
263 ifp->if_start = rt2560_start;
264 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
265 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
266 IFQ_SET_READY(&ifp->if_snd);
269 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
270 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
271 ic->ic_state = IEEE80211_S_INIT;
273 /* set device capabilities */
275 IEEE80211_C_IBSS | /* IBSS mode supported */
276 IEEE80211_C_MONITOR | /* monitor mode supported */
277 IEEE80211_C_HOSTAP | /* HostAp mode supported */
278 IEEE80211_C_TXPMGT | /* tx power management */
279 IEEE80211_C_SHPREAMBLE | /* short preamble supported */
280 IEEE80211_C_SHSLOT | /* short slot time supported */
281 IEEE80211_C_BGSCAN | /* bg scanning support */
282 IEEE80211_C_WPA; /* 802.11i */
285 setbit(&bands, IEEE80211_MODE_11B);
286 setbit(&bands, IEEE80211_MODE_11G);
287 if (sc->rf_rev == RT2560_RF_5222)
288 setbit(&bands, IEEE80211_MODE_11A);
289 ieee80211_init_channels(ic, 0, CTRY_DEFAULT, bands, 0, 1);
291 ieee80211_ifattach(ic);
292 ic->ic_scan_start = rt2560_scan_start;
293 ic->ic_scan_end = rt2560_scan_end;
294 ic->ic_set_channel = rt2560_set_channel;
295 ic->ic_node_alloc = rt2560_node_alloc;
296 ic->ic_updateslot = rt2560_update_slot;
297 ic->ic_reset = rt2560_reset;
298 /* enable s/w bmiss handling in sta mode */
299 ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS;
301 /* override state transition machine */
302 sc->sc_newstate = ic->ic_newstate;
303 ic->ic_newstate = rt2560_newstate;
304 ic->ic_raw_xmit = rt2560_raw_xmit;
305 ic->ic_update_beacon = rt2560_beacon_update;
306 ieee80211_media_init(ic, rt2560_media_change, ieee80211_media_status);
308 bpfattach2(ifp, DLT_IEEE802_11_RADIO,
309 sizeof (struct ieee80211_frame) + sizeof (sc->sc_txtap),
312 sc->sc_rxtap_len = sizeof sc->sc_rxtap;
313 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
314 sc->sc_rxtap.wr_ihdr.it_present = htole32(RT2560_RX_RADIOTAP_PRESENT);
316 sc->sc_txtap_len = sizeof sc->sc_txtap;
317 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
318 sc->sc_txtap.wt_ihdr.it_present = htole32(RT2560_TX_RADIOTAP_PRESENT);
321 * Add a few sysctl knobs.
325 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
326 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
327 "txantenna", CTLFLAG_RW, &sc->tx_ant, 0, "tx antenna (0=auto)");
329 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
330 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
331 "rxantenna", CTLFLAG_RW, &sc->rx_ant, 0, "rx antenna (0=auto)");
333 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
334 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "dwell",
335 CTLFLAG_RW, &sc->dwelltime, 0,
336 "channel dwell time (ms) for AP/station scanning");
339 ieee80211_announce(ic);
343 fail6: rt2560_free_rx_ring(sc, &sc->rxq);
344 fail5: rt2560_free_tx_ring(sc, &sc->bcnq);
345 fail4: rt2560_free_tx_ring(sc, &sc->prioq);
346 fail3: rt2560_free_tx_ring(sc, &sc->atimq);
347 fail2: rt2560_free_tx_ring(sc, &sc->txq);
348 fail1: mtx_destroy(&sc->sc_mtx);
354 rt2560_detach(void *xsc)
356 struct rt2560_softc *sc = xsc;
357 struct ieee80211com *ic = &sc->sc_ic;
358 struct ifnet *ifp = ic->ic_ifp;
361 callout_stop(&sc->rssadapt_ch);
364 ieee80211_ifdetach(ic);
366 rt2560_free_tx_ring(sc, &sc->txq);
367 rt2560_free_tx_ring(sc, &sc->atimq);
368 rt2560_free_tx_ring(sc, &sc->prioq);
369 rt2560_free_tx_ring(sc, &sc->bcnq);
370 rt2560_free_rx_ring(sc, &sc->rxq);
374 mtx_destroy(&sc->sc_mtx);
380 rt2560_resume(void *xsc)
382 struct rt2560_softc *sc = xsc;
383 struct ifnet *ifp = sc->sc_ic.ic_ifp;
385 if (ifp->if_flags & IFF_UP) {
386 ifp->if_init(ifp->if_softc);
387 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
393 rt2560_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
398 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
400 *(bus_addr_t *)arg = segs[0].ds_addr;
404 rt2560_alloc_tx_ring(struct rt2560_softc *sc, struct rt2560_tx_ring *ring,
411 ring->cur = ring->next = 0;
412 ring->cur_encrypt = ring->next_encrypt = 0;
414 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 4, 0,
415 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
416 count * RT2560_TX_DESC_SIZE, 1, count * RT2560_TX_DESC_SIZE,
417 0, NULL, NULL, &ring->desc_dmat);
419 device_printf(sc->sc_dev, "could not create desc DMA tag\n");
423 error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc,
424 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_map);
426 device_printf(sc->sc_dev, "could not allocate DMA memory\n");
430 error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc,
431 count * RT2560_TX_DESC_SIZE, rt2560_dma_map_addr, &ring->physaddr,
434 device_printf(sc->sc_dev, "could not load desc DMA map\n");
438 ring->data = malloc(count * sizeof (struct rt2560_tx_data), M_DEVBUF,
440 if (ring->data == NULL) {
441 device_printf(sc->sc_dev, "could not allocate soft data\n");
446 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
447 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
448 MCLBYTES, RT2560_MAX_SCATTER, MCLBYTES, 0, NULL, NULL,
451 device_printf(sc->sc_dev, "could not create data DMA tag\n");
455 for (i = 0; i < count; i++) {
456 error = bus_dmamap_create(ring->data_dmat, 0,
459 device_printf(sc->sc_dev, "could not create DMA map\n");
466 fail: rt2560_free_tx_ring(sc, ring);
471 rt2560_reset_tx_ring(struct rt2560_softc *sc, struct rt2560_tx_ring *ring)
473 struct rt2560_tx_desc *desc;
474 struct rt2560_tx_data *data;
477 for (i = 0; i < ring->count; i++) {
478 desc = &ring->desc[i];
479 data = &ring->data[i];
481 if (data->m != NULL) {
482 bus_dmamap_sync(ring->data_dmat, data->map,
483 BUS_DMASYNC_POSTWRITE);
484 bus_dmamap_unload(ring->data_dmat, data->map);
489 if (data->ni != NULL) {
490 ieee80211_free_node(data->ni);
497 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_PREWRITE);
500 ring->cur = ring->next = 0;
501 ring->cur_encrypt = ring->next_encrypt = 0;
505 rt2560_free_tx_ring(struct rt2560_softc *sc, struct rt2560_tx_ring *ring)
507 struct rt2560_tx_data *data;
510 if (ring->desc != NULL) {
511 bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
512 BUS_DMASYNC_POSTWRITE);
513 bus_dmamap_unload(ring->desc_dmat, ring->desc_map);
514 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
517 if (ring->desc_dmat != NULL)
518 bus_dma_tag_destroy(ring->desc_dmat);
520 if (ring->data != NULL) {
521 for (i = 0; i < ring->count; i++) {
522 data = &ring->data[i];
524 if (data->m != NULL) {
525 bus_dmamap_sync(ring->data_dmat, data->map,
526 BUS_DMASYNC_POSTWRITE);
527 bus_dmamap_unload(ring->data_dmat, data->map);
531 if (data->ni != NULL)
532 ieee80211_free_node(data->ni);
534 if (data->map != NULL)
535 bus_dmamap_destroy(ring->data_dmat, data->map);
538 free(ring->data, M_DEVBUF);
541 if (ring->data_dmat != NULL)
542 bus_dma_tag_destroy(ring->data_dmat);
546 rt2560_alloc_rx_ring(struct rt2560_softc *sc, struct rt2560_rx_ring *ring,
549 struct rt2560_rx_desc *desc;
550 struct rt2560_rx_data *data;
555 ring->cur = ring->next = 0;
556 ring->cur_decrypt = 0;
558 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 4, 0,
559 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
560 count * RT2560_RX_DESC_SIZE, 1, count * RT2560_RX_DESC_SIZE,
561 0, NULL, NULL, &ring->desc_dmat);
563 device_printf(sc->sc_dev, "could not create desc DMA tag\n");
567 error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc,
568 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_map);
570 device_printf(sc->sc_dev, "could not allocate DMA memory\n");
574 error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc,
575 count * RT2560_RX_DESC_SIZE, rt2560_dma_map_addr, &ring->physaddr,
578 device_printf(sc->sc_dev, "could not load desc DMA map\n");
582 ring->data = malloc(count * sizeof (struct rt2560_rx_data), M_DEVBUF,
584 if (ring->data == NULL) {
585 device_printf(sc->sc_dev, "could not allocate soft data\n");
591 * Pre-allocate Rx buffers and populate Rx ring.
593 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
594 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES,
595 1, MCLBYTES, 0, NULL, NULL, &ring->data_dmat);
597 device_printf(sc->sc_dev, "could not create data DMA tag\n");
601 for (i = 0; i < count; i++) {
602 desc = &sc->rxq.desc[i];
603 data = &sc->rxq.data[i];
605 error = bus_dmamap_create(ring->data_dmat, 0, &data->map);
607 device_printf(sc->sc_dev, "could not create DMA map\n");
611 data->m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
612 if (data->m == NULL) {
613 device_printf(sc->sc_dev,
614 "could not allocate rx mbuf\n");
619 error = bus_dmamap_load(ring->data_dmat, data->map,
620 mtod(data->m, void *), MCLBYTES, rt2560_dma_map_addr,
623 device_printf(sc->sc_dev,
624 "could not load rx buf DMA map");
628 desc->flags = htole32(RT2560_RX_BUSY);
629 desc->physaddr = htole32(physaddr);
632 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_PREWRITE);
636 fail: rt2560_free_rx_ring(sc, ring);
641 rt2560_reset_rx_ring(struct rt2560_softc *sc, struct rt2560_rx_ring *ring)
645 for (i = 0; i < ring->count; i++) {
646 ring->desc[i].flags = htole32(RT2560_RX_BUSY);
647 ring->data[i].drop = 0;
650 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_PREWRITE);
652 ring->cur = ring->next = 0;
653 ring->cur_decrypt = 0;
657 rt2560_free_rx_ring(struct rt2560_softc *sc, struct rt2560_rx_ring *ring)
659 struct rt2560_rx_data *data;
662 if (ring->desc != NULL) {
663 bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
664 BUS_DMASYNC_POSTWRITE);
665 bus_dmamap_unload(ring->desc_dmat, ring->desc_map);
666 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
669 if (ring->desc_dmat != NULL)
670 bus_dma_tag_destroy(ring->desc_dmat);
672 if (ring->data != NULL) {
673 for (i = 0; i < ring->count; i++) {
674 data = &ring->data[i];
676 if (data->m != NULL) {
677 bus_dmamap_sync(ring->data_dmat, data->map,
678 BUS_DMASYNC_POSTREAD);
679 bus_dmamap_unload(ring->data_dmat, data->map);
683 if (data->map != NULL)
684 bus_dmamap_destroy(ring->data_dmat, data->map);
687 free(ring->data, M_DEVBUF);
690 if (ring->data_dmat != NULL)
691 bus_dma_tag_destroy(ring->data_dmat);
694 static struct ieee80211_node *
695 rt2560_node_alloc(struct ieee80211_node_table *nt)
697 struct rt2560_node *rn;
699 rn = malloc(sizeof (struct rt2560_node), M_80211_NODE,
702 return (rn != NULL) ? &rn->ni : NULL;
706 rt2560_media_change(struct ifnet *ifp)
708 struct rt2560_softc *sc = ifp->if_softc;
711 error = ieee80211_media_change(ifp);
713 if (error == ENETRESET) {
714 if ((ifp->if_flags & IFF_UP) &&
715 (ifp->if_drv_flags & IFF_DRV_RUNNING))
722 * This function is called for each node present in the node station table.
725 rt2560_iter_func(void *arg, struct ieee80211_node *ni)
727 struct rt2560_node *rn = (struct rt2560_node *)ni;
729 ral_rssadapt_updatestats(&rn->rssadapt);
733 * This function is called periodically (every 100ms) in RUN state to update
734 * the rate adaptation statistics.
737 rt2560_update_rssadapt(void *arg)
739 struct rt2560_softc *sc = arg;
740 struct ieee80211com *ic = &sc->sc_ic;
744 ieee80211_iterate_nodes(&ic->ic_sta, rt2560_iter_func, arg);
745 callout_reset(&sc->rssadapt_ch, hz / 10, rt2560_update_rssadapt, sc);
751 rt2560_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
753 struct rt2560_softc *sc = ic->ic_ifp->if_softc;
754 enum ieee80211_state ostate;
755 struct ieee80211_node *ni;
759 ostate = ic->ic_state;
762 case IEEE80211_S_INIT:
763 callout_stop(&sc->rssadapt_ch);
765 if (ostate == IEEE80211_S_RUN) {
766 /* abort TSF synchronization */
767 RAL_WRITE(sc, RT2560_CSR14, 0);
769 /* turn association led off */
770 rt2560_update_led(sc, 0, 0);
773 case IEEE80211_S_RUN:
776 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
777 rt2560_update_plcp(sc);
778 rt2560_set_basicrates(sc);
779 rt2560_set_bssid(sc, ni->ni_bssid);
782 if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
783 ic->ic_opmode == IEEE80211_M_IBSS) {
784 m = ieee80211_beacon_alloc(ni, &sc->sc_bo);
786 device_printf(sc->sc_dev,
787 "could not allocate beacon\n");
792 ieee80211_ref_node(ni);
793 error = rt2560_tx_bcn(sc, m, ni);
798 /* turn assocation led on */
799 rt2560_update_led(sc, 1, 0);
801 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
802 callout_reset(&sc->rssadapt_ch, hz / 10,
803 rt2560_update_rssadapt, sc);
805 rt2560_enable_tsf_sync(sc);
808 case IEEE80211_S_SCAN:
809 case IEEE80211_S_AUTH:
810 case IEEE80211_S_ASSOC:
815 return (error != 0) ? error : sc->sc_newstate(ic, nstate, arg);
819 * Read 16 bits at address 'addr' from the serial EEPROM (either 93C46 or
823 rt2560_eeprom_read(struct rt2560_softc *sc, uint8_t addr)
829 /* clock C once before the first command */
830 RT2560_EEPROM_CTL(sc, 0);
832 RT2560_EEPROM_CTL(sc, RT2560_S);
833 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C);
834 RT2560_EEPROM_CTL(sc, RT2560_S);
836 /* write start bit (1) */
837 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D);
838 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D | RT2560_C);
840 /* write READ opcode (10) */
841 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D);
842 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D | RT2560_C);
843 RT2560_EEPROM_CTL(sc, RT2560_S);
844 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C);
846 /* write address (A5-A0 or A7-A0) */
847 n = (RAL_READ(sc, RT2560_CSR21) & RT2560_93C46) ? 5 : 7;
848 for (; n >= 0; n--) {
849 RT2560_EEPROM_CTL(sc, RT2560_S |
850 (((addr >> n) & 1) << RT2560_SHIFT_D));
851 RT2560_EEPROM_CTL(sc, RT2560_S |
852 (((addr >> n) & 1) << RT2560_SHIFT_D) | RT2560_C);
855 RT2560_EEPROM_CTL(sc, RT2560_S);
857 /* read data Q15-Q0 */
859 for (n = 15; n >= 0; n--) {
860 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C);
861 tmp = RAL_READ(sc, RT2560_CSR21);
862 val |= ((tmp & RT2560_Q) >> RT2560_SHIFT_Q) << n;
863 RT2560_EEPROM_CTL(sc, RT2560_S);
866 RT2560_EEPROM_CTL(sc, 0);
868 /* clear Chip Select and clock C */
869 RT2560_EEPROM_CTL(sc, RT2560_S);
870 RT2560_EEPROM_CTL(sc, 0);
871 RT2560_EEPROM_CTL(sc, RT2560_C);
877 * Some frames were processed by the hardware cipher engine and are ready for
881 rt2560_encryption_intr(struct rt2560_softc *sc)
883 struct rt2560_tx_desc *desc;
886 /* retrieve last descriptor index processed by cipher engine */
887 hw = RAL_READ(sc, RT2560_SECCSR1) - sc->txq.physaddr;
888 hw /= RT2560_TX_DESC_SIZE;
890 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
891 BUS_DMASYNC_POSTREAD);
893 while (sc->txq.next_encrypt != hw) {
894 if (sc->txq.next_encrypt == sc->txq.cur_encrypt) {
895 printf("hw encrypt %d, cur_encrypt %d\n", hw,
896 sc->txq.cur_encrypt);
900 desc = &sc->txq.desc[sc->txq.next_encrypt];
902 if ((le32toh(desc->flags) & RT2560_TX_BUSY) ||
903 (le32toh(desc->flags) & RT2560_TX_CIPHER_BUSY))
906 /* for TKIP, swap eiv field to fix a bug in ASIC */
907 if ((le32toh(desc->flags) & RT2560_TX_CIPHER_MASK) ==
908 RT2560_TX_CIPHER_TKIP)
909 desc->eiv = bswap32(desc->eiv);
911 /* mark the frame ready for transmission */
912 desc->flags |= htole32(RT2560_TX_VALID);
913 desc->flags |= htole32(RT2560_TX_BUSY);
915 DPRINTFN(15, ("encryption done idx=%u\n",
916 sc->txq.next_encrypt));
918 sc->txq.next_encrypt =
919 (sc->txq.next_encrypt + 1) % RT2560_TX_RING_COUNT;
922 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
923 BUS_DMASYNC_PREWRITE);
926 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_TX);
930 rt2560_tx_intr(struct rt2560_softc *sc)
932 struct ieee80211com *ic = &sc->sc_ic;
933 struct ifnet *ifp = ic->ic_ifp;
934 struct rt2560_tx_desc *desc;
935 struct rt2560_tx_data *data;
936 struct rt2560_node *rn;
938 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
939 BUS_DMASYNC_POSTREAD);
942 desc = &sc->txq.desc[sc->txq.next];
943 data = &sc->txq.data[sc->txq.next];
945 if ((le32toh(desc->flags) & RT2560_TX_BUSY) ||
946 (le32toh(desc->flags) & RT2560_TX_CIPHER_BUSY) ||
947 !(le32toh(desc->flags) & RT2560_TX_VALID))
950 rn = (struct rt2560_node *)data->ni;
952 switch (le32toh(desc->flags) & RT2560_TX_RESULT_MASK) {
953 case RT2560_TX_SUCCESS:
954 DPRINTFN(10, ("data frame sent successfully\n"));
955 if (data->id.id_node != NULL) {
956 ral_rssadapt_raise_rate(ic, &rn->rssadapt,
962 case RT2560_TX_SUCCESS_RETRY:
963 DPRINTFN(9, ("data frame sent after %u retries\n",
964 (le32toh(desc->flags) >> 5) & 0x7));
968 case RT2560_TX_FAIL_RETRY:
969 DPRINTFN(9, ("sending data frame failed (too much "
971 if (data->id.id_node != NULL) {
972 ral_rssadapt_lower_rate(ic, data->ni,
973 &rn->rssadapt, &data->id);
978 case RT2560_TX_FAIL_INVALID:
979 case RT2560_TX_FAIL_OTHER:
981 device_printf(sc->sc_dev, "sending data frame failed "
982 "0x%08x\n", le32toh(desc->flags));
986 bus_dmamap_sync(sc->txq.data_dmat, data->map,
987 BUS_DMASYNC_POSTWRITE);
988 bus_dmamap_unload(sc->txq.data_dmat, data->map);
991 ieee80211_free_node(data->ni);
994 /* descriptor is no longer valid */
995 desc->flags &= ~htole32(RT2560_TX_VALID);
997 DPRINTFN(15, ("tx done idx=%u\n", sc->txq.next));
1000 sc->txq.next = (sc->txq.next + 1) % RT2560_TX_RING_COUNT;
1003 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
1004 BUS_DMASYNC_PREWRITE);
1006 if (sc->prioq.queued == 0 && sc->txq.queued == 0)
1007 sc->sc_tx_timer = 0;
1009 if (sc->txq.queued < RT2560_TX_RING_COUNT - 1) {
1010 sc->sc_flags &= ~RT2560_F_DATA_OACTIVE;
1012 (RT2560_F_DATA_OACTIVE | RT2560_F_PRIO_OACTIVE)) == 0)
1013 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1019 rt2560_prio_intr(struct rt2560_softc *sc)
1021 struct ieee80211com *ic = &sc->sc_ic;
1022 struct ifnet *ifp = ic->ic_ifp;
1023 struct rt2560_tx_desc *desc;
1024 struct rt2560_tx_data *data;
1025 struct ieee80211_node *ni;
1029 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map,
1030 BUS_DMASYNC_POSTREAD);
1033 desc = &sc->prioq.desc[sc->prioq.next];
1034 data = &sc->prioq.data[sc->prioq.next];
1036 flags = le32toh(desc->flags);
1037 if ((flags & RT2560_TX_BUSY) || (flags & RT2560_TX_VALID) == 0)
1040 switch (flags & RT2560_TX_RESULT_MASK) {
1041 case RT2560_TX_SUCCESS:
1042 DPRINTFN(10, ("mgt frame sent successfully\n"));
1045 case RT2560_TX_SUCCESS_RETRY:
1046 DPRINTFN(9, ("mgt frame sent after %u retries\n",
1047 (flags >> 5) & 0x7));
1050 case RT2560_TX_FAIL_RETRY:
1051 DPRINTFN(9, ("sending mgt frame failed (too much "
1055 case RT2560_TX_FAIL_INVALID:
1056 case RT2560_TX_FAIL_OTHER:
1058 device_printf(sc->sc_dev, "sending mgt frame failed "
1063 bus_dmamap_sync(sc->prioq.data_dmat, data->map,
1064 BUS_DMASYNC_POSTWRITE);
1065 bus_dmamap_unload(sc->prioq.data_dmat, data->map);
1072 /* descriptor is no longer valid */
1073 desc->flags &= ~htole32(RT2560_TX_VALID);
1075 DPRINTFN(15, ("prio done idx=%u\n", sc->prioq.next));
1078 sc->prioq.next = (sc->prioq.next + 1) % RT2560_PRIO_RING_COUNT;
1080 if (m->m_flags & M_TXCB)
1081 ieee80211_process_callback(ni, m,
1082 (flags & RT2560_TX_RESULT_MASK) &~
1083 (RT2560_TX_SUCCESS | RT2560_TX_SUCCESS_RETRY));
1085 ieee80211_free_node(ni);
1088 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map,
1089 BUS_DMASYNC_PREWRITE);
1091 if (sc->prioq.queued == 0 && sc->txq.queued == 0)
1092 sc->sc_tx_timer = 0;
1094 if (sc->prioq.queued < RT2560_PRIO_RING_COUNT) {
1095 sc->sc_flags &= ~RT2560_F_PRIO_OACTIVE;
1097 (RT2560_F_DATA_OACTIVE | RT2560_F_PRIO_OACTIVE)) == 0)
1098 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1104 * Some frames were processed by the hardware cipher engine and are ready for
1105 * transmission to the IEEE802.11 layer.
1108 rt2560_decryption_intr(struct rt2560_softc *sc)
1110 struct ieee80211com *ic = &sc->sc_ic;
1111 struct ifnet *ifp = ic->ic_ifp;
1112 struct rt2560_rx_desc *desc;
1113 struct rt2560_rx_data *data;
1114 bus_addr_t physaddr;
1115 struct ieee80211_frame *wh;
1116 struct ieee80211_node *ni;
1117 struct rt2560_node *rn;
1118 struct mbuf *mnew, *m;
1121 /* retrieve last decriptor index processed by cipher engine */
1122 hw = RAL_READ(sc, RT2560_SECCSR0) - sc->rxq.physaddr;
1123 hw /= RT2560_RX_DESC_SIZE;
1125 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1126 BUS_DMASYNC_POSTREAD);
1128 for (; sc->rxq.cur_decrypt != hw;) {
1129 desc = &sc->rxq.desc[sc->rxq.cur_decrypt];
1130 data = &sc->rxq.data[sc->rxq.cur_decrypt];
1132 if ((le32toh(desc->flags) & RT2560_RX_BUSY) ||
1133 (le32toh(desc->flags) & RT2560_RX_CIPHER_BUSY))
1141 if ((le32toh(desc->flags) & RT2560_RX_CIPHER_MASK) != 0 &&
1142 (le32toh(desc->flags) & RT2560_RX_ICV_ERROR)) {
1148 * Try to allocate a new mbuf for this ring element and load it
1149 * before processing the current mbuf. If the ring element
1150 * cannot be loaded, drop the received packet and reuse the old
1151 * mbuf. In the unlikely case that the old mbuf can't be
1152 * reloaded either, explicitly panic.
1154 mnew = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1160 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
1161 BUS_DMASYNC_POSTREAD);
1162 bus_dmamap_unload(sc->rxq.data_dmat, data->map);
1164 error = bus_dmamap_load(sc->rxq.data_dmat, data->map,
1165 mtod(mnew, void *), MCLBYTES, rt2560_dma_map_addr,
1170 /* try to reload the old mbuf */
1171 error = bus_dmamap_load(sc->rxq.data_dmat, data->map,
1172 mtod(data->m, void *), MCLBYTES,
1173 rt2560_dma_map_addr, &physaddr, 0);
1175 /* very unlikely that it will fail... */
1176 panic("%s: could not load old rx mbuf",
1177 device_get_name(sc->sc_dev));
1184 * New mbuf successfully loaded, update Rx ring and continue
1189 desc->physaddr = htole32(physaddr);
1192 m->m_pkthdr.rcvif = ifp;
1193 m->m_pkthdr.len = m->m_len =
1194 (le32toh(desc->flags) >> 16) & 0xfff;
1196 if (bpf_peers_present(sc->sc_drvbpf)) {
1197 struct rt2560_rx_radiotap_header *tap = &sc->sc_rxtap;
1198 uint32_t tsf_lo, tsf_hi;
1200 /* get timestamp (low and high 32 bits) */
1201 tsf_hi = RAL_READ(sc, RT2560_CSR17);
1202 tsf_lo = RAL_READ(sc, RT2560_CSR16);
1205 htole64(((uint64_t)tsf_hi << 32) | tsf_lo);
1207 tap->wr_rate = rt2560_rxrate(desc);
1208 tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
1209 tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
1210 tap->wr_antenna = sc->rx_ant;
1211 tap->wr_antsignal = RT2560_RSSI(sc, desc->rssi);
1213 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m);
1216 sc->sc_flags |= RT2560_F_INPUT_RUNNING;
1218 wh = mtod(m, struct ieee80211_frame *);
1219 ni = ieee80211_find_rxnode(ic,
1220 (struct ieee80211_frame_min *)wh);
1222 /* send the frame to the 802.11 layer */
1223 ieee80211_input(ic, m, ni, RT2560_RSSI(sc, desc->rssi),
1224 RT2560_NOISE_FLOOR, 0);
1226 /* give rssi to the rate adatation algorithm */
1227 rn = (struct rt2560_node *)ni;
1228 ral_rssadapt_input(ic, ni, &rn->rssadapt,
1229 RT2560_RSSI(sc, desc->rssi));
1231 /* node is no longer needed */
1232 ieee80211_free_node(ni);
1235 sc->sc_flags &= ~RT2560_F_INPUT_RUNNING;
1236 skip: desc->flags = htole32(RT2560_RX_BUSY);
1238 DPRINTFN(15, ("decryption done idx=%u\n", sc->rxq.cur_decrypt));
1240 sc->rxq.cur_decrypt =
1241 (sc->rxq.cur_decrypt + 1) % RT2560_RX_RING_COUNT;
1244 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1245 BUS_DMASYNC_PREWRITE);
1249 * Some frames were received. Pass them to the hardware cipher engine before
1250 * sending them to the 802.11 layer.
1253 rt2560_rx_intr(struct rt2560_softc *sc)
1255 struct rt2560_rx_desc *desc;
1256 struct rt2560_rx_data *data;
1258 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1259 BUS_DMASYNC_POSTREAD);
1262 desc = &sc->rxq.desc[sc->rxq.cur];
1263 data = &sc->rxq.data[sc->rxq.cur];
1265 if ((le32toh(desc->flags) & RT2560_RX_BUSY) ||
1266 (le32toh(desc->flags) & RT2560_RX_CIPHER_BUSY))
1271 if ((le32toh(desc->flags) & RT2560_RX_PHY_ERROR) ||
1272 (le32toh(desc->flags) & RT2560_RX_CRC_ERROR)) {
1274 * This should not happen since we did not request
1275 * to receive those frames when we filled RXCSR0.
1277 DPRINTFN(5, ("PHY or CRC error flags 0x%08x\n",
1278 le32toh(desc->flags)));
1282 if (((le32toh(desc->flags) >> 16) & 0xfff) > MCLBYTES) {
1283 DPRINTFN(5, ("bad length\n"));
1287 /* mark the frame for decryption */
1288 desc->flags |= htole32(RT2560_RX_CIPHER_BUSY);
1290 DPRINTFN(15, ("rx done idx=%u\n", sc->rxq.cur));
1292 sc->rxq.cur = (sc->rxq.cur + 1) % RT2560_RX_RING_COUNT;
1295 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1296 BUS_DMASYNC_PREWRITE);
1299 RAL_WRITE(sc, RT2560_SECCSR0, RT2560_KICK_DECRYPT);
1303 rt2560_beacon_update(struct ieee80211com *ic, int item)
1305 struct rt2560_softc *sc = ic->ic_ifp->if_softc;
1306 struct ieee80211_beacon_offsets *bo = &sc->sc_bo;
1308 setbit(bo->bo_flags, item);
1312 * This function is called periodically in IBSS mode when a new beacon must be
1316 rt2560_beacon_expire(struct rt2560_softc *sc)
1318 struct ieee80211com *ic = &sc->sc_ic;
1319 struct rt2560_tx_data *data;
1321 if (ic->ic_opmode != IEEE80211_M_IBSS &&
1322 ic->ic_opmode != IEEE80211_M_HOSTAP)
1325 data = &sc->bcnq.data[sc->bcnq.next];
1327 * Don't send beacon if bsschan isn't set
1329 if (data->ni == NULL)
1332 bus_dmamap_sync(sc->bcnq.data_dmat, data->map, BUS_DMASYNC_POSTWRITE);
1333 bus_dmamap_unload(sc->bcnq.data_dmat, data->map);
1335 ieee80211_beacon_update(data->ni, &sc->sc_bo, data->m, 1);
1337 if (bpf_peers_present(ic->ic_rawbpf))
1338 bpf_mtap(ic->ic_rawbpf, data->m);
1340 rt2560_tx_bcn(sc, data->m, data->ni);
1342 DPRINTFN(15, ("beacon expired\n"));
1344 sc->bcnq.next = (sc->bcnq.next + 1) % RT2560_BEACON_RING_COUNT;
1349 rt2560_wakeup_expire(struct rt2560_softc *sc)
1351 DPRINTFN(2, ("wakeup expired\n"));
1355 rt2560_intr(void *arg)
1357 struct rt2560_softc *sc = arg;
1358 struct ifnet *ifp = sc->sc_ifp;
1363 /* disable interrupts */
1364 RAL_WRITE(sc, RT2560_CSR8, 0xffffffff);
1366 /* don't re-enable interrupts if we're shutting down */
1367 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1372 r = RAL_READ(sc, RT2560_CSR7);
1373 RAL_WRITE(sc, RT2560_CSR7, r);
1375 if (r & RT2560_BEACON_EXPIRE)
1376 rt2560_beacon_expire(sc);
1378 if (r & RT2560_WAKEUP_EXPIRE)
1379 rt2560_wakeup_expire(sc);
1381 if (r & RT2560_ENCRYPTION_DONE)
1382 rt2560_encryption_intr(sc);
1384 if (r & RT2560_TX_DONE)
1387 if (r & RT2560_PRIO_DONE)
1388 rt2560_prio_intr(sc);
1390 if (r & RT2560_DECRYPTION_DONE)
1391 rt2560_decryption_intr(sc);
1393 if (r & RT2560_RX_DONE) {
1395 rt2560_encryption_intr(sc);
1398 /* re-enable interrupts */
1399 RAL_WRITE(sc, RT2560_CSR8, RT2560_INTR_MASK);
1404 /* quickly determine if a given rate is CCK or OFDM */
1405 #define RAL_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
1407 #define RAL_ACK_SIZE 14 /* 10 + 4(FCS) */
1408 #define RAL_CTS_SIZE 14 /* 10 + 4(FCS) */
1410 #define RAL_SIFS 10 /* us */
1412 #define RT2560_TXRX_TURNAROUND 10 /* us */
1415 * This function is only used by the Rx radiotap code.
1418 rt2560_rxrate(struct rt2560_rx_desc *desc)
1420 if (le32toh(desc->flags) & RT2560_RX_OFDM) {
1421 /* reverse function of rt2560_plcp_signal */
1422 switch (desc->rate) {
1423 case 0xb: return 12;
1424 case 0xf: return 18;
1425 case 0xa: return 24;
1426 case 0xe: return 36;
1427 case 0x9: return 48;
1428 case 0xd: return 72;
1429 case 0x8: return 96;
1430 case 0xc: return 108;
1433 if (desc->rate == 10)
1435 if (desc->rate == 20)
1437 if (desc->rate == 55)
1439 if (desc->rate == 110)
1442 return 2; /* should not get there */
1446 * Return the expected ack rate for a frame transmitted at rate `rate'.
1447 * XXX: this should depend on the destination node basic rate set.
1450 rt2560_ack_rate(struct ieee80211com *ic, int rate)
1459 return (ic->ic_curmode == IEEE80211_MODE_11B) ? 4 : rate;
1475 /* default to 1Mbps */
1480 * Compute the duration (in us) needed to transmit `len' bytes at rate `rate'.
1481 * The function automatically determines the operating mode depending on the
1482 * given rate. `flags' indicates whether short preamble is in use or not.
1485 rt2560_txtime(int len, int rate, uint32_t flags)
1489 if (RAL_RATE_IS_OFDM(rate)) {
1490 /* IEEE Std 802.11a-1999, pp. 37 */
1491 txtime = (8 + 4 * len + 3 + rate - 1) / rate;
1492 txtime = 16 + 4 + 4 * txtime + 6;
1494 /* IEEE Std 802.11b-1999, pp. 28 */
1495 txtime = (16 * len + rate - 1) / rate;
1496 if (rate != 2 && (flags & IEEE80211_F_SHPREAMBLE))
1506 rt2560_plcp_signal(int rate)
1509 /* CCK rates (returned values are device-dependent) */
1512 case 11: return 0x2;
1513 case 22: return 0x3;
1515 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1516 case 12: return 0xb;
1517 case 18: return 0xf;
1518 case 24: return 0xa;
1519 case 36: return 0xe;
1520 case 48: return 0x9;
1521 case 72: return 0xd;
1522 case 96: return 0x8;
1523 case 108: return 0xc;
1525 /* unsupported rates (should not get there) */
1526 default: return 0xff;
1531 rt2560_setup_tx_desc(struct rt2560_softc *sc, struct rt2560_tx_desc *desc,
1532 uint32_t flags, int len, int rate, int encrypt, bus_addr_t physaddr)
1534 struct ieee80211com *ic = &sc->sc_ic;
1535 uint16_t plcp_length;
1538 desc->flags = htole32(flags);
1539 desc->flags |= htole32(len << 16);
1541 desc->physaddr = htole32(physaddr);
1542 desc->wme = htole16(
1544 RT2560_LOGCWMIN(3) |
1545 RT2560_LOGCWMAX(8));
1547 /* setup PLCP fields */
1548 desc->plcp_signal = rt2560_plcp_signal(rate);
1549 desc->plcp_service = 4;
1551 len += IEEE80211_CRC_LEN;
1552 if (RAL_RATE_IS_OFDM(rate)) {
1553 desc->flags |= htole32(RT2560_TX_OFDM);
1555 plcp_length = len & 0xfff;
1556 desc->plcp_length_hi = plcp_length >> 6;
1557 desc->plcp_length_lo = plcp_length & 0x3f;
1559 plcp_length = (16 * len + rate - 1) / rate;
1561 remainder = (16 * len) % 22;
1562 if (remainder != 0 && remainder < 7)
1563 desc->plcp_service |= RT2560_PLCP_LENGEXT;
1565 desc->plcp_length_hi = plcp_length >> 8;
1566 desc->plcp_length_lo = plcp_length & 0xff;
1568 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1569 desc->plcp_signal |= 0x08;
1573 desc->flags |= htole32(RT2560_TX_VALID);
1574 desc->flags |= encrypt ? htole32(RT2560_TX_CIPHER_BUSY)
1575 : htole32(RT2560_TX_BUSY);
1579 rt2560_tx_bcn(struct rt2560_softc *sc, struct mbuf *m0,
1580 struct ieee80211_node *ni)
1582 struct ieee80211com *ic = &sc->sc_ic;
1583 struct rt2560_tx_desc *desc;
1584 struct rt2560_tx_data *data;
1585 bus_dma_segment_t segs[RT2560_MAX_SCATTER];
1586 int nsegs, rate, error;
1588 desc = &sc->bcnq.desc[sc->bcnq.cur];
1589 data = &sc->bcnq.data[sc->bcnq.cur];
1591 rate = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? 12 : 2;
1593 error = bus_dmamap_load_mbuf_sg(sc->bcnq.data_dmat, data->map, m0,
1594 segs, &nsegs, BUS_DMA_NOWAIT);
1596 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1602 if (bpf_peers_present(sc->sc_drvbpf)) {
1603 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1606 tap->wt_rate = rate;
1607 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1608 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1609 tap->wt_antenna = sc->tx_ant;
1611 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
1617 rt2560_setup_tx_desc(sc, desc, RT2560_TX_IFS_NEWBACKOFF |
1618 RT2560_TX_TIMESTAMP, m0->m_pkthdr.len, rate, 0, segs->ds_addr);
1620 DPRINTFN(10, ("sending beacon frame len=%u idx=%u rate=%u\n",
1621 m0->m_pkthdr.len, sc->bcnq.cur, rate));
1623 bus_dmamap_sync(sc->bcnq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1624 bus_dmamap_sync(sc->bcnq.desc_dmat, sc->bcnq.desc_map,
1625 BUS_DMASYNC_PREWRITE);
1627 sc->bcnq.cur = (sc->bcnq.cur + 1) % RT2560_BEACON_RING_COUNT;
1633 rt2560_tx_mgt(struct rt2560_softc *sc, struct mbuf *m0,
1634 struct ieee80211_node *ni)
1636 struct ieee80211com *ic = &sc->sc_ic;
1637 struct rt2560_tx_desc *desc;
1638 struct rt2560_tx_data *data;
1639 struct ieee80211_frame *wh;
1640 bus_dma_segment_t segs[RT2560_MAX_SCATTER];
1643 int nsegs, rate, error;
1645 desc = &sc->prioq.desc[sc->prioq.cur];
1646 data = &sc->prioq.data[sc->prioq.cur];
1648 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
1650 error = bus_dmamap_load_mbuf_sg(sc->prioq.data_dmat, data->map, m0,
1653 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1659 if (bpf_peers_present(sc->sc_drvbpf)) {
1660 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1663 tap->wt_rate = rate;
1664 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1665 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1666 tap->wt_antenna = sc->tx_ant;
1668 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
1674 wh = mtod(m0, struct ieee80211_frame *);
1676 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1677 flags |= RT2560_TX_ACK;
1679 dur = rt2560_txtime(RAL_ACK_SIZE, rate, ic->ic_flags) +
1681 *(uint16_t *)wh->i_dur = htole16(dur);
1683 /* tell hardware to add timestamp for probe responses */
1684 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
1685 IEEE80211_FC0_TYPE_MGT &&
1686 (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
1687 IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1688 flags |= RT2560_TX_TIMESTAMP;
1691 rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate, 0,
1694 bus_dmamap_sync(sc->prioq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1695 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map,
1696 BUS_DMASYNC_PREWRITE);
1698 DPRINTFN(10, ("sending mgt frame len=%u idx=%u rate=%u\n",
1699 m0->m_pkthdr.len, sc->prioq.cur, rate));
1703 sc->prioq.cur = (sc->prioq.cur + 1) % RT2560_PRIO_RING_COUNT;
1704 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_PRIO);
1710 rt2560_tx_raw(struct rt2560_softc *sc, struct mbuf *m0,
1711 struct ieee80211_node *ni, const struct ieee80211_bpf_params *params)
1713 struct ieee80211com *ic = &sc->sc_ic;
1714 struct rt2560_tx_desc *desc;
1715 struct rt2560_tx_data *data;
1716 bus_dma_segment_t segs[RT2560_MAX_SCATTER];
1718 int nsegs, rate, error;
1720 desc = &sc->prioq.desc[sc->prioq.cur];
1721 data = &sc->prioq.data[sc->prioq.cur];
1723 rate = params->ibp_rate0 & IEEE80211_RATE_VAL;
1730 error = bus_dmamap_load_mbuf_sg(sc->prioq.data_dmat, data->map, m0,
1733 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1739 if (bpf_peers_present(sc->sc_drvbpf)) {
1740 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1743 tap->wt_rate = rate;
1744 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1745 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1746 tap->wt_antenna = sc->tx_ant;
1748 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
1755 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
1756 flags |= RT2560_TX_ACK;
1758 /* XXX need to setup descriptor ourself */
1759 rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len,
1760 rate, (params->ibp_flags & IEEE80211_BPF_CRYPTO) != 0,
1763 bus_dmamap_sync(sc->prioq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1764 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map,
1765 BUS_DMASYNC_PREWRITE);
1767 DPRINTFN(10, ("sending raw frame len=%u idx=%u rate=%u\n",
1768 m0->m_pkthdr.len, sc->prioq.cur, rate));
1772 sc->prioq.cur = (sc->prioq.cur + 1) % RT2560_PRIO_RING_COUNT;
1773 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_PRIO);
1779 * Build a RTS control frame.
1781 static struct mbuf *
1782 rt2560_get_rts(struct rt2560_softc *sc, struct ieee80211_frame *wh,
1785 struct ieee80211_frame_rts *rts;
1788 MGETHDR(m, M_DONTWAIT, MT_DATA);
1790 sc->sc_ic.ic_stats.is_tx_nobuf++;
1791 device_printf(sc->sc_dev, "could not allocate RTS frame\n");
1795 rts = mtod(m, struct ieee80211_frame_rts *);
1797 rts->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_CTL |
1798 IEEE80211_FC0_SUBTYPE_RTS;
1799 rts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1800 *(uint16_t *)rts->i_dur = htole16(dur);
1801 IEEE80211_ADDR_COPY(rts->i_ra, wh->i_addr1);
1802 IEEE80211_ADDR_COPY(rts->i_ta, wh->i_addr2);
1804 m->m_pkthdr.len = m->m_len = sizeof (struct ieee80211_frame_rts);
1810 rt2560_tx_data(struct rt2560_softc *sc, struct mbuf *m0,
1811 struct ieee80211_node *ni)
1813 struct ieee80211com *ic = &sc->sc_ic;
1814 struct rt2560_tx_desc *desc;
1815 struct rt2560_tx_data *data;
1816 struct rt2560_node *rn;
1817 struct ieee80211_frame *wh;
1818 struct ieee80211_key *k;
1820 bus_dma_segment_t segs[RT2560_MAX_SCATTER];
1823 int nsegs, rate, error;
1825 wh = mtod(m0, struct ieee80211_frame *);
1827 if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE) {
1828 rate = ic->ic_fixed_rate;
1830 struct ieee80211_rateset *rs;
1833 rn = (struct rt2560_node *)ni;
1834 ni->ni_txrate = ral_rssadapt_choose(&rn->rssadapt, rs, wh,
1835 m0->m_pkthdr.len, NULL, 0);
1836 rate = rs->rs_rates[ni->ni_txrate];
1838 rate &= IEEE80211_RATE_VAL;
1840 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1841 k = ieee80211_crypto_encap(ic, ni, m0);
1847 /* packet header may have moved, reset our local pointer */
1848 wh = mtod(m0, struct ieee80211_frame *);
1852 * IEEE Std 802.11-1999, pp 82: "A STA shall use an RTS/CTS exchange
1853 * for directed frames only when the length of the MPDU is greater
1854 * than the length threshold indicated by [...]" ic_rtsthreshold.
1856 if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
1857 m0->m_pkthdr.len > ic->ic_rtsthreshold) {
1860 int rtsrate, ackrate;
1862 rtsrate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
1863 ackrate = rt2560_ack_rate(ic, rate);
1865 dur = rt2560_txtime(m0->m_pkthdr.len + 4, rate, ic->ic_flags) +
1866 rt2560_txtime(RAL_CTS_SIZE, rtsrate, ic->ic_flags) +
1867 rt2560_txtime(RAL_ACK_SIZE, ackrate, ic->ic_flags) +
1870 m = rt2560_get_rts(sc, wh, dur);
1872 desc = &sc->txq.desc[sc->txq.cur_encrypt];
1873 data = &sc->txq.data[sc->txq.cur_encrypt];
1875 error = bus_dmamap_load_mbuf_sg(sc->txq.data_dmat, data->map,
1876 m, segs, &nsegs, 0);
1878 device_printf(sc->sc_dev,
1879 "could not map mbuf (error %d)\n", error);
1885 /* avoid multiple free() of the same node for each fragment */
1886 ieee80211_ref_node(ni);
1891 /* RTS frames are not taken into account for rssadapt */
1892 data->id.id_node = NULL;
1894 rt2560_setup_tx_desc(sc, desc, RT2560_TX_ACK |
1895 RT2560_TX_MORE_FRAG, m->m_pkthdr.len, rtsrate, 1,
1898 bus_dmamap_sync(sc->txq.data_dmat, data->map,
1899 BUS_DMASYNC_PREWRITE);
1902 sc->txq.cur_encrypt =
1903 (sc->txq.cur_encrypt + 1) % RT2560_TX_RING_COUNT;
1906 * IEEE Std 802.11-1999: when an RTS/CTS exchange is used, the
1907 * asynchronous data frame shall be transmitted after the CTS
1908 * frame and a SIFS period.
1910 flags |= RT2560_TX_LONG_RETRY | RT2560_TX_IFS_SIFS;
1913 data = &sc->txq.data[sc->txq.cur_encrypt];
1914 desc = &sc->txq.desc[sc->txq.cur_encrypt];
1916 error = bus_dmamap_load_mbuf_sg(sc->txq.data_dmat, data->map, m0,
1918 if (error != 0 && error != EFBIG) {
1919 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1925 mnew = m_defrag(m0, M_DONTWAIT);
1927 device_printf(sc->sc_dev,
1928 "could not defragment mbuf\n");
1934 error = bus_dmamap_load_mbuf_sg(sc->txq.data_dmat, data->map,
1935 m0, segs, &nsegs, 0);
1937 device_printf(sc->sc_dev,
1938 "could not map mbuf (error %d)\n", error);
1943 /* packet header may have moved, reset our local pointer */
1944 wh = mtod(m0, struct ieee80211_frame *);
1947 if (bpf_peers_present(sc->sc_drvbpf)) {
1948 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1951 tap->wt_rate = rate;
1952 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1953 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1954 tap->wt_antenna = sc->tx_ant;
1956 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
1962 /* remember link conditions for rate adaptation algorithm */
1963 if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) {
1964 data->id.id_len = m0->m_pkthdr.len;
1965 data->id.id_rateidx = ni->ni_txrate;
1966 data->id.id_node = ni;
1967 data->id.id_rssi = ni->ni_rssi;
1969 data->id.id_node = NULL;
1971 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1972 flags |= RT2560_TX_ACK;
1974 dur = rt2560_txtime(RAL_ACK_SIZE, rt2560_ack_rate(ic, rate),
1975 ic->ic_flags) + RAL_SIFS;
1976 *(uint16_t *)wh->i_dur = htole16(dur);
1979 rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate, 1,
1982 bus_dmamap_sync(sc->txq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1983 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
1984 BUS_DMASYNC_PREWRITE);
1986 DPRINTFN(10, ("sending data frame len=%u idx=%u rate=%u\n",
1987 m0->m_pkthdr.len, sc->txq.cur_encrypt, rate));
1991 sc->txq.cur_encrypt = (sc->txq.cur_encrypt + 1) % RT2560_TX_RING_COUNT;
1992 RAL_WRITE(sc, RT2560_SECCSR1, RT2560_KICK_ENCRYPT);
1998 rt2560_start(struct ifnet *ifp)
2000 struct rt2560_softc *sc = ifp->if_softc;
2001 struct ieee80211com *ic = &sc->sc_ic;
2003 struct ether_header *eh;
2004 struct ieee80211_node *ni;
2008 /* prevent management frames from being sent if we're not ready */
2009 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2015 IF_POLL(&ic->ic_mgtq, m0);
2017 if (sc->prioq.queued >= RT2560_PRIO_RING_COUNT) {
2018 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2019 sc->sc_flags |= RT2560_F_PRIO_OACTIVE;
2022 IF_DEQUEUE(&ic->ic_mgtq, m0);
2024 ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
2025 m0->m_pkthdr.rcvif = NULL;
2027 if (bpf_peers_present(ic->ic_rawbpf))
2028 bpf_mtap(ic->ic_rawbpf, m0);
2030 if (rt2560_tx_mgt(sc, m0, ni) != 0) {
2031 ieee80211_free_node(ni);
2035 if (ic->ic_state != IEEE80211_S_RUN)
2037 IFQ_DRV_DEQUEUE(&ifp->if_snd, m0);
2040 if (sc->txq.queued >= RT2560_TX_RING_COUNT - 1) {
2041 IFQ_DRV_PREPEND(&ifp->if_snd, m0);
2042 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2043 sc->sc_flags |= RT2560_F_DATA_OACTIVE;
2047 * Cancel any background scan.
2049 if (ic->ic_flags & IEEE80211_F_SCAN)
2050 ieee80211_cancel_scan(ic);
2052 if (m0->m_len < sizeof (struct ether_header) &&
2053 !(m0 = m_pullup(m0, sizeof (struct ether_header))))
2056 eh = mtod(m0, struct ether_header *);
2057 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
2062 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
2063 (m0->m_flags & M_PWR_SAV) == 0) {
2065 * Station in power save mode; pass the frame
2066 * to the 802.11 layer and continue. We'll get
2067 * the frame back when the time is right.
2069 ieee80211_pwrsave(ni, m0);
2071 * If we're in power save mode 'cuz of a bg
2072 * scan cancel it so the traffic can flow.
2073 * The packet we just queued will automatically
2074 * get sent when we drop out of power save.
2077 if (ic->ic_flags & IEEE80211_F_SCAN)
2078 ieee80211_cancel_scan(ic);
2079 ieee80211_free_node(ni);
2085 m0 = ieee80211_encap(ic, m0, ni);
2087 ieee80211_free_node(ni);
2091 if (bpf_peers_present(ic->ic_rawbpf))
2092 bpf_mtap(ic->ic_rawbpf, m0);
2094 if (rt2560_tx_data(sc, m0, ni) != 0) {
2095 ieee80211_free_node(ni);
2101 sc->sc_tx_timer = 5;
2102 ic->ic_lastdata = ticks;
2109 rt2560_watchdog(void *arg)
2111 struct rt2560_softc *sc = arg;
2112 struct ifnet *ifp = sc->sc_ifp;
2114 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
2117 rt2560_encryption_intr(sc);
2120 if (sc->sc_tx_timer > 0) {
2121 if (--sc->sc_tx_timer == 0) {
2122 device_printf(sc->sc_dev, "device timeout\n");
2125 /* watchdog timeout is set in rt2560_init() */
2129 callout_reset(&sc->watchdog_ch, hz, rt2560_watchdog, sc);
2133 * This function allows for fast channel switching in monitor mode (used by
2134 * net-mgmt/kismet). In IBSS mode, we must explicitly reset the interface to
2135 * generate a new beacon frame.
2138 rt2560_reset(struct ifnet *ifp)
2140 struct rt2560_softc *sc = ifp->if_softc;
2141 struct ieee80211com *ic = &sc->sc_ic;
2143 if (ic->ic_opmode != IEEE80211_M_MONITOR)
2146 rt2560_set_chan(sc, ic->ic_curchan);
2152 rt2560_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
2154 struct rt2560_softc *sc = ifp->if_softc;
2155 struct ieee80211com *ic = &sc->sc_ic;
2162 if (ifp->if_flags & IFF_UP) {
2164 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2165 rt2560_update_promisc(sc);
2170 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2177 error = ieee80211_ioctl(ic, cmd, data);
2180 if (error == ENETRESET) {
2181 if ((ifp->if_flags & IFF_UP) &&
2182 (ifp->if_drv_flags & IFF_DRV_RUNNING) &&
2183 (ic->ic_roaming != IEEE80211_ROAMING_MANUAL))
2193 rt2560_bbp_write(struct rt2560_softc *sc, uint8_t reg, uint8_t val)
2198 for (ntries = 0; ntries < 100; ntries++) {
2199 if (!(RAL_READ(sc, RT2560_BBPCSR) & RT2560_BBP_BUSY))
2203 if (ntries == 100) {
2204 device_printf(sc->sc_dev, "could not write to BBP\n");
2208 tmp = RT2560_BBP_WRITE | RT2560_BBP_BUSY | reg << 8 | val;
2209 RAL_WRITE(sc, RT2560_BBPCSR, tmp);
2211 DPRINTFN(15, ("BBP R%u <- 0x%02x\n", reg, val));
2215 rt2560_bbp_read(struct rt2560_softc *sc, uint8_t reg)
2220 for (ntries = 0; ntries < 100; ntries++) {
2221 if (!(RAL_READ(sc, RT2560_BBPCSR) & RT2560_BBP_BUSY))
2225 if (ntries == 100) {
2226 device_printf(sc->sc_dev, "could not read from BBP\n");
2230 val = RT2560_BBP_BUSY | reg << 8;
2231 RAL_WRITE(sc, RT2560_BBPCSR, val);
2233 for (ntries = 0; ntries < 100; ntries++) {
2234 val = RAL_READ(sc, RT2560_BBPCSR);
2235 if (!(val & RT2560_BBP_BUSY))
2240 device_printf(sc->sc_dev, "could not read from BBP\n");
2245 rt2560_rf_write(struct rt2560_softc *sc, uint8_t reg, uint32_t val)
2250 for (ntries = 0; ntries < 100; ntries++) {
2251 if (!(RAL_READ(sc, RT2560_RFCSR) & RT2560_RF_BUSY))
2255 if (ntries == 100) {
2256 device_printf(sc->sc_dev, "could not write to RF\n");
2260 tmp = RT2560_RF_BUSY | RT2560_RF_20BIT | (val & 0xfffff) << 2 |
2262 RAL_WRITE(sc, RT2560_RFCSR, tmp);
2264 /* remember last written value in sc */
2265 sc->rf_regs[reg] = val;
2267 DPRINTFN(15, ("RF R[%u] <- 0x%05x\n", reg & 0x3, val & 0xfffff));
2271 rt2560_set_chan(struct rt2560_softc *sc, struct ieee80211_channel *c)
2273 struct ieee80211com *ic = &sc->sc_ic;
2277 chan = ieee80211_chan2ieee(ic, c);
2278 if (chan == 0 || chan == IEEE80211_CHAN_ANY)
2281 if (IEEE80211_IS_CHAN_2GHZ(c))
2282 power = min(sc->txpow[chan - 1], 31);
2286 /* adjust txpower using ifconfig settings */
2287 power -= (100 - ic->ic_txpowlimit) / 8;
2289 DPRINTFN(2, ("setting channel to %u, txpower to %u\n", chan, power));
2291 switch (sc->rf_rev) {
2292 case RT2560_RF_2522:
2293 rt2560_rf_write(sc, RAL_RF1, 0x00814);
2294 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2522_r2[chan - 1]);
2295 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
2298 case RT2560_RF_2523:
2299 rt2560_rf_write(sc, RAL_RF1, 0x08804);
2300 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2523_r2[chan - 1]);
2301 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x38044);
2302 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2305 case RT2560_RF_2524:
2306 rt2560_rf_write(sc, RAL_RF1, 0x0c808);
2307 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2524_r2[chan - 1]);
2308 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
2309 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2312 case RT2560_RF_2525:
2313 rt2560_rf_write(sc, RAL_RF1, 0x08808);
2314 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525_hi_r2[chan - 1]);
2315 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2316 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2318 rt2560_rf_write(sc, RAL_RF1, 0x08808);
2319 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525_r2[chan - 1]);
2320 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2321 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2324 case RT2560_RF_2525E:
2325 rt2560_rf_write(sc, RAL_RF1, 0x08808);
2326 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525e_r2[chan - 1]);
2327 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2328 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00286 : 0x00282);
2331 case RT2560_RF_2526:
2332 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2526_hi_r2[chan - 1]);
2333 rt2560_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
2334 rt2560_rf_write(sc, RAL_RF1, 0x08804);
2336 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2526_r2[chan - 1]);
2337 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2338 rt2560_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
2342 case RT2560_RF_5222:
2343 for (i = 0; rt2560_rf5222[i].chan != chan; i++);
2345 rt2560_rf_write(sc, RAL_RF1, rt2560_rf5222[i].r1);
2346 rt2560_rf_write(sc, RAL_RF2, rt2560_rf5222[i].r2);
2347 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
2348 rt2560_rf_write(sc, RAL_RF4, rt2560_rf5222[i].r4);
2351 printf("unknown ral rev=%d\n", sc->rf_rev);
2354 if (ic->ic_state != IEEE80211_S_SCAN) {
2355 /* set Japan filter bit for channel 14 */
2356 tmp = rt2560_bbp_read(sc, 70);
2358 tmp &= ~RT2560_JAPAN_FILTER;
2360 tmp |= RT2560_JAPAN_FILTER;
2362 rt2560_bbp_write(sc, 70, tmp);
2364 /* clear CRC errors */
2365 RAL_READ(sc, RT2560_CNT0);
2370 rt2560_set_channel(struct ieee80211com *ic)
2372 struct ifnet *ifp = ic->ic_ifp;
2373 struct rt2560_softc *sc = ifp->if_softc;
2376 rt2560_set_chan(sc, ic->ic_curchan);
2383 * Disable RF auto-tuning.
2386 rt2560_disable_rf_tune(struct rt2560_softc *sc)
2390 if (sc->rf_rev != RT2560_RF_2523) {
2391 tmp = sc->rf_regs[RAL_RF1] & ~RAL_RF1_AUTOTUNE;
2392 rt2560_rf_write(sc, RAL_RF1, tmp);
2395 tmp = sc->rf_regs[RAL_RF3] & ~RAL_RF3_AUTOTUNE;
2396 rt2560_rf_write(sc, RAL_RF3, tmp);
2398 DPRINTFN(2, ("disabling RF autotune\n"));
2403 * Refer to IEEE Std 802.11-1999 pp. 123 for more information on TSF
2407 rt2560_enable_tsf_sync(struct rt2560_softc *sc)
2409 struct ieee80211com *ic = &sc->sc_ic;
2410 uint16_t logcwmin, preload;
2413 /* first, disable TSF synchronization */
2414 RAL_WRITE(sc, RT2560_CSR14, 0);
2416 tmp = 16 * ic->ic_bss->ni_intval;
2417 RAL_WRITE(sc, RT2560_CSR12, tmp);
2419 RAL_WRITE(sc, RT2560_CSR13, 0);
2422 preload = (ic->ic_opmode == IEEE80211_M_STA) ? 384 : 1024;
2423 tmp = logcwmin << 16 | preload;
2424 RAL_WRITE(sc, RT2560_BCNOCSR, tmp);
2426 /* finally, enable TSF synchronization */
2427 tmp = RT2560_ENABLE_TSF | RT2560_ENABLE_TBCN;
2428 if (ic->ic_opmode == IEEE80211_M_STA)
2429 tmp |= RT2560_ENABLE_TSF_SYNC(1);
2431 tmp |= RT2560_ENABLE_TSF_SYNC(2) |
2432 RT2560_ENABLE_BEACON_GENERATOR;
2433 RAL_WRITE(sc, RT2560_CSR14, tmp);
2435 DPRINTF(("enabling TSF synchronization\n"));
2439 rt2560_update_plcp(struct rt2560_softc *sc)
2441 struct ieee80211com *ic = &sc->sc_ic;
2443 /* no short preamble for 1Mbps */
2444 RAL_WRITE(sc, RT2560_PLCP1MCSR, 0x00700400);
2446 if (!(ic->ic_flags & IEEE80211_F_SHPREAMBLE)) {
2447 /* values taken from the reference driver */
2448 RAL_WRITE(sc, RT2560_PLCP2MCSR, 0x00380401);
2449 RAL_WRITE(sc, RT2560_PLCP5p5MCSR, 0x00150402);
2450 RAL_WRITE(sc, RT2560_PLCP11MCSR, 0x000b8403);
2452 /* same values as above or'ed 0x8 */
2453 RAL_WRITE(sc, RT2560_PLCP2MCSR, 0x00380409);
2454 RAL_WRITE(sc, RT2560_PLCP5p5MCSR, 0x0015040a);
2455 RAL_WRITE(sc, RT2560_PLCP11MCSR, 0x000b840b);
2458 DPRINTF(("updating PLCP for %s preamble\n",
2459 (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ? "short" : "long"));
2463 * This function can be called by ieee80211_set_shortslottime(). Refer to
2464 * IEEE Std 802.11-1999 pp. 85 to know how these values are computed.
2467 rt2560_update_slot(struct ifnet *ifp)
2469 struct rt2560_softc *sc = ifp->if_softc;
2470 struct ieee80211com *ic = &sc->sc_ic;
2472 uint16_t tx_sifs, tx_pifs, tx_difs, eifs;
2475 #ifndef FORCE_SLOTTIME
2476 slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
2479 * Setting slot time according to "short slot time" capability
2480 * in beacon/probe_resp seems to cause problem to acknowledge
2481 * certain AP's data frames transimitted at CCK/DS rates: the
2482 * problematic AP keeps retransmitting data frames, probably
2483 * because MAC level acks are not received by hardware.
2484 * So we cheat a little bit here by claiming we are capable of
2485 * "short slot time" but setting hardware slot time to the normal
2486 * slot time. ral(4) does not seem to have trouble to receive
2487 * frames transmitted using short slot time even if hardware
2488 * slot time is set to normal slot time. If we didn't use this
2489 * trick, we would have to claim that short slot time is not
2490 * supported; this would give relative poor RX performance
2491 * (-1Mb~-2Mb lower) and the _whole_ BSS would stop using short
2497 /* update the MAC slot boundaries */
2498 tx_sifs = RAL_SIFS - RT2560_TXRX_TURNAROUND;
2499 tx_pifs = tx_sifs + slottime;
2500 tx_difs = tx_sifs + 2 * slottime;
2501 eifs = (ic->ic_curmode == IEEE80211_MODE_11B) ? 364 : 60;
2503 tmp = RAL_READ(sc, RT2560_CSR11);
2504 tmp = (tmp & ~0x1f00) | slottime << 8;
2505 RAL_WRITE(sc, RT2560_CSR11, tmp);
2507 tmp = tx_pifs << 16 | tx_sifs;
2508 RAL_WRITE(sc, RT2560_CSR18, tmp);
2510 tmp = eifs << 16 | tx_difs;
2511 RAL_WRITE(sc, RT2560_CSR19, tmp);
2513 DPRINTF(("setting slottime to %uus\n", slottime));
2517 rt2560_set_basicrates(struct rt2560_softc *sc)
2519 struct ieee80211com *ic = &sc->sc_ic;
2521 /* update basic rate set */
2522 if (ic->ic_curmode == IEEE80211_MODE_11B) {
2523 /* 11b basic rates: 1, 2Mbps */
2524 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x3);
2525 } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan)) {
2526 /* 11a basic rates: 6, 12, 24Mbps */
2527 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x150);
2529 /* 11g basic rates: 1, 2, 5.5, 11, 6, 12, 24Mbps */
2530 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x15f);
2535 rt2560_update_led(struct rt2560_softc *sc, int led1, int led2)
2539 /* set ON period to 70ms and OFF period to 30ms */
2540 tmp = led1 << 16 | led2 << 17 | 70 << 8 | 30;
2541 RAL_WRITE(sc, RT2560_LEDCSR, tmp);
2545 rt2560_set_bssid(struct rt2560_softc *sc, const uint8_t *bssid)
2549 tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24;
2550 RAL_WRITE(sc, RT2560_CSR5, tmp);
2552 tmp = bssid[4] | bssid[5] << 8;
2553 RAL_WRITE(sc, RT2560_CSR6, tmp);
2555 DPRINTF(("setting BSSID to %6D\n", bssid, ":"));
2559 rt2560_set_macaddr(struct rt2560_softc *sc, uint8_t *addr)
2563 tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24;
2564 RAL_WRITE(sc, RT2560_CSR3, tmp);
2566 tmp = addr[4] | addr[5] << 8;
2567 RAL_WRITE(sc, RT2560_CSR4, tmp);
2569 DPRINTF(("setting MAC address to %6D\n", addr, ":"));
2573 rt2560_get_macaddr(struct rt2560_softc *sc, uint8_t *addr)
2577 tmp = RAL_READ(sc, RT2560_CSR3);
2578 addr[0] = tmp & 0xff;
2579 addr[1] = (tmp >> 8) & 0xff;
2580 addr[2] = (tmp >> 16) & 0xff;
2581 addr[3] = (tmp >> 24);
2583 tmp = RAL_READ(sc, RT2560_CSR4);
2584 addr[4] = tmp & 0xff;
2585 addr[5] = (tmp >> 8) & 0xff;
2589 rt2560_update_promisc(struct rt2560_softc *sc)
2591 struct ifnet *ifp = sc->sc_ic.ic_ifp;
2594 tmp = RAL_READ(sc, RT2560_RXCSR0);
2596 tmp &= ~RT2560_DROP_NOT_TO_ME;
2597 if (!(ifp->if_flags & IFF_PROMISC))
2598 tmp |= RT2560_DROP_NOT_TO_ME;
2600 RAL_WRITE(sc, RT2560_RXCSR0, tmp);
2602 DPRINTF(("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
2603 "entering" : "leaving"));
2607 rt2560_get_rf(int rev)
2610 case RT2560_RF_2522: return "RT2522";
2611 case RT2560_RF_2523: return "RT2523";
2612 case RT2560_RF_2524: return "RT2524";
2613 case RT2560_RF_2525: return "RT2525";
2614 case RT2560_RF_2525E: return "RT2525e";
2615 case RT2560_RF_2526: return "RT2526";
2616 case RT2560_RF_5222: return "RT5222";
2617 default: return "unknown";
2622 rt2560_read_config(struct rt2560_softc *sc)
2627 val = rt2560_eeprom_read(sc, RT2560_EEPROM_CONFIG0);
2628 sc->rf_rev = (val >> 11) & 0x7;
2629 sc->hw_radio = (val >> 10) & 0x1;
2630 sc->led_mode = (val >> 6) & 0x7;
2631 sc->rx_ant = (val >> 4) & 0x3;
2632 sc->tx_ant = (val >> 2) & 0x3;
2633 sc->nb_ant = val & 0x3;
2635 /* read default values for BBP registers */
2636 for (i = 0; i < 16; i++) {
2637 val = rt2560_eeprom_read(sc, RT2560_EEPROM_BBP_BASE + i);
2638 if (val == 0 || val == 0xffff)
2641 sc->bbp_prom[i].reg = val >> 8;
2642 sc->bbp_prom[i].val = val & 0xff;
2645 /* read Tx power for all b/g channels */
2646 for (i = 0; i < 14 / 2; i++) {
2647 val = rt2560_eeprom_read(sc, RT2560_EEPROM_TXPOWER + i);
2648 sc->txpow[i * 2] = val & 0xff;
2649 sc->txpow[i * 2 + 1] = val >> 8;
2651 for (i = 0; i < 14; ++i) {
2652 if (sc->txpow[i] > 31)
2656 val = rt2560_eeprom_read(sc, RT2560_EEPROM_CALIBRATE);
2657 if ((val & 0xff) == 0xff)
2658 sc->rssi_corr = RT2560_DEFAULT_RSSI_CORR;
2660 sc->rssi_corr = val & 0xff;
2661 DPRINTF(("rssi correction %d, calibrate 0x%02x\n",
2662 sc->rssi_corr, val));
2667 rt2560_scan_start(struct ieee80211com *ic)
2669 struct ifnet *ifp = ic->ic_ifp;
2670 struct rt2560_softc *sc = ifp->if_softc;
2672 /* abort TSF synchronization */
2673 RAL_WRITE(sc, RT2560_CSR14, 0);
2674 rt2560_set_bssid(sc, ifp->if_broadcastaddr);
2678 rt2560_scan_end(struct ieee80211com *ic)
2680 struct ifnet *ifp = ic->ic_ifp;
2681 struct rt2560_softc *sc = ifp->if_softc;
2683 rt2560_enable_tsf_sync(sc);
2684 /* XXX keep local copy */
2685 rt2560_set_bssid(sc, ic->ic_bss->ni_bssid);
2689 rt2560_bbp_init(struct rt2560_softc *sc)
2691 #define N(a) (sizeof (a) / sizeof ((a)[0]))
2694 /* wait for BBP to be ready */
2695 for (ntries = 0; ntries < 100; ntries++) {
2696 if (rt2560_bbp_read(sc, RT2560_BBP_VERSION) != 0)
2700 if (ntries == 100) {
2701 device_printf(sc->sc_dev, "timeout waiting for BBP\n");
2705 /* initialize BBP registers to default values */
2706 for (i = 0; i < N(rt2560_def_bbp); i++) {
2707 rt2560_bbp_write(sc, rt2560_def_bbp[i].reg,
2708 rt2560_def_bbp[i].val);
2711 /* initialize BBP registers to values stored in EEPROM */
2712 for (i = 0; i < 16; i++) {
2713 if (sc->bbp_prom[i].reg == 0 && sc->bbp_prom[i].val == 0)
2715 rt2560_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
2717 rt2560_bbp_write(sc, 17, 0x48); /* XXX restore bbp17 */
2724 rt2560_set_txantenna(struct rt2560_softc *sc, int antenna)
2729 tx = rt2560_bbp_read(sc, RT2560_BBP_TX) & ~RT2560_BBP_ANTMASK;
2731 tx |= RT2560_BBP_ANTA;
2732 else if (antenna == 2)
2733 tx |= RT2560_BBP_ANTB;
2735 tx |= RT2560_BBP_DIVERSITY;
2737 /* need to force I/Q flip for RF 2525e, 2526 and 5222 */
2738 if (sc->rf_rev == RT2560_RF_2525E || sc->rf_rev == RT2560_RF_2526 ||
2739 sc->rf_rev == RT2560_RF_5222)
2740 tx |= RT2560_BBP_FLIPIQ;
2742 rt2560_bbp_write(sc, RT2560_BBP_TX, tx);
2744 /* update values for CCK and OFDM in BBPCSR1 */
2745 tmp = RAL_READ(sc, RT2560_BBPCSR1) & ~0x00070007;
2746 tmp |= (tx & 0x7) << 16 | (tx & 0x7);
2747 RAL_WRITE(sc, RT2560_BBPCSR1, tmp);
2751 rt2560_set_rxantenna(struct rt2560_softc *sc, int antenna)
2755 rx = rt2560_bbp_read(sc, RT2560_BBP_RX) & ~RT2560_BBP_ANTMASK;
2757 rx |= RT2560_BBP_ANTA;
2758 else if (antenna == 2)
2759 rx |= RT2560_BBP_ANTB;
2761 rx |= RT2560_BBP_DIVERSITY;
2763 /* need to force no I/Q flip for RF 2525e and 2526 */
2764 if (sc->rf_rev == RT2560_RF_2525E || sc->rf_rev == RT2560_RF_2526)
2765 rx &= ~RT2560_BBP_FLIPIQ;
2767 rt2560_bbp_write(sc, RT2560_BBP_RX, rx);
2771 rt2560_init(void *priv)
2773 #define N(a) (sizeof (a) / sizeof ((a)[0]))
2774 struct rt2560_softc *sc = priv;
2775 struct ieee80211com *ic = &sc->sc_ic;
2776 struct ifnet *ifp = ic->ic_ifp;
2785 /* setup tx rings */
2786 tmp = RT2560_PRIO_RING_COUNT << 24 |
2787 RT2560_ATIM_RING_COUNT << 16 |
2788 RT2560_TX_RING_COUNT << 8 |
2789 RT2560_TX_DESC_SIZE;
2791 /* rings must be initialized in this exact order */
2792 RAL_WRITE(sc, RT2560_TXCSR2, tmp);
2793 RAL_WRITE(sc, RT2560_TXCSR3, sc->txq.physaddr);
2794 RAL_WRITE(sc, RT2560_TXCSR5, sc->prioq.physaddr);
2795 RAL_WRITE(sc, RT2560_TXCSR4, sc->atimq.physaddr);
2796 RAL_WRITE(sc, RT2560_TXCSR6, sc->bcnq.physaddr);
2799 tmp = RT2560_RX_RING_COUNT << 8 | RT2560_RX_DESC_SIZE;
2801 RAL_WRITE(sc, RT2560_RXCSR1, tmp);
2802 RAL_WRITE(sc, RT2560_RXCSR2, sc->rxq.physaddr);
2804 /* initialize MAC registers to default values */
2805 for (i = 0; i < N(rt2560_def_mac); i++)
2806 RAL_WRITE(sc, rt2560_def_mac[i].reg, rt2560_def_mac[i].val);
2808 IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
2809 rt2560_set_macaddr(sc, ic->ic_myaddr);
2811 /* set basic rate set (will be updated later) */
2812 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x153);
2814 rt2560_update_slot(ifp);
2815 rt2560_update_plcp(sc);
2816 rt2560_update_led(sc, 0, 0);
2818 RAL_WRITE(sc, RT2560_CSR1, RT2560_RESET_ASIC);
2819 RAL_WRITE(sc, RT2560_CSR1, RT2560_HOST_READY);
2821 if (rt2560_bbp_init(sc) != 0) {
2827 rt2560_set_txantenna(sc, sc->tx_ant);
2828 rt2560_set_rxantenna(sc, sc->rx_ant);
2830 /* set default BSS channel */
2831 rt2560_set_chan(sc, ic->ic_curchan);
2834 tmp = RT2560_DROP_PHY_ERROR | RT2560_DROP_CRC_ERROR;
2835 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2836 tmp |= RT2560_DROP_CTL | RT2560_DROP_VERSION_ERROR;
2837 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2838 tmp |= RT2560_DROP_TODS;
2839 if (!(ifp->if_flags & IFF_PROMISC))
2840 tmp |= RT2560_DROP_NOT_TO_ME;
2842 RAL_WRITE(sc, RT2560_RXCSR0, tmp);
2844 /* clear old FCS and Rx FIFO errors */
2845 RAL_READ(sc, RT2560_CNT0);
2846 RAL_READ(sc, RT2560_CNT4);
2848 /* clear any pending interrupts */
2849 RAL_WRITE(sc, RT2560_CSR7, 0xffffffff);
2851 /* enable interrupts */
2852 RAL_WRITE(sc, RT2560_CSR8, RT2560_INTR_MASK);
2854 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2855 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2857 callout_reset(&sc->watchdog_ch, hz, rt2560_watchdog, sc);
2859 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2860 if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
2861 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2863 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2870 rt2560_stop(void *arg)
2872 struct rt2560_softc *sc = arg;
2873 struct ieee80211com *ic = &sc->sc_ic;
2874 struct ifnet *ifp = ic->ic_ifp;
2875 volatile int *flags = &sc->sc_flags;
2877 while (*flags & RT2560_F_INPUT_RUNNING) {
2878 tsleep(sc, 0, "ralrunning", hz/10);
2883 callout_stop(&sc->watchdog_ch);
2885 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
2886 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2887 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2890 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_ABORT_TX);
2893 RAL_WRITE(sc, RT2560_RXCSR0, RT2560_DISABLE_RX);
2895 /* reset ASIC (imply reset BBP) */
2896 RAL_WRITE(sc, RT2560_CSR1, RT2560_RESET_ASIC);
2897 RAL_WRITE(sc, RT2560_CSR1, 0);
2899 /* disable interrupts */
2900 RAL_WRITE(sc, RT2560_CSR8, 0xffffffff);
2902 /* reset Tx and Rx rings */
2903 rt2560_reset_tx_ring(sc, &sc->txq);
2904 rt2560_reset_tx_ring(sc, &sc->atimq);
2905 rt2560_reset_tx_ring(sc, &sc->prioq);
2906 rt2560_reset_tx_ring(sc, &sc->bcnq);
2907 rt2560_reset_rx_ring(sc, &sc->rxq);
2909 sc->sc_tx_timer = 0;
2910 sc->sc_flags &= ~(RT2560_F_PRIO_OACTIVE | RT2560_F_DATA_OACTIVE);
2916 rt2560_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2917 const struct ieee80211_bpf_params *params)
2919 struct ieee80211com *ic = ni->ni_ic;
2920 struct ifnet *ifp = ic->ic_ifp;
2921 struct rt2560_softc *sc = ifp->if_softc;
2925 /* prevent management frames from being sent if we're not ready */
2926 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2929 ieee80211_free_node(ni);
2932 if (sc->prioq.queued >= RT2560_PRIO_RING_COUNT) {
2933 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2936 ieee80211_free_node(ni);
2937 return ENOBUFS; /* XXX */
2940 if (bpf_peers_present(ic->ic_rawbpf))
2941 bpf_mtap(ic->ic_rawbpf, m);
2945 if (params == NULL) {
2947 * Legacy path; interpret frame contents to decide
2948 * precisely how to send the frame.
2950 if (rt2560_tx_mgt(sc, m, ni) != 0)
2954 * Caller supplied explicit parameters to use in
2955 * sending the frame.
2957 if (rt2560_tx_raw(sc, m, ni, params))
2960 sc->sc_tx_timer = 5;
2967 ieee80211_free_node(ni);
2969 return EIO; /* XXX */