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>
57 #include <net80211/ieee80211_amrr.h>
59 #include <netinet/in.h>
60 #include <netinet/in_systm.h>
61 #include <netinet/in_var.h>
62 #include <netinet/ip.h>
63 #include <netinet/if_ether.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)
74 #define DPRINTF(sc, fmt, ...) do { \
75 if (sc->sc_debug > 0) \
76 printf(fmt, __VA_ARGS__); \
78 #define DPRINTFN(sc, n, fmt, ...) do { \
79 if (sc->sc_debug >= (n)) \
80 printf(fmt, __VA_ARGS__); \
83 #define DPRINTF(sc, fmt, ...)
84 #define DPRINTFN(sc, n, fmt, ...)
87 static struct ieee80211vap *rt2560_vap_create(struct ieee80211com *,
88 const char name[IFNAMSIZ], int unit, int opmode,
89 int flags, const uint8_t bssid[IEEE80211_ADDR_LEN],
90 const uint8_t mac[IEEE80211_ADDR_LEN]);
91 static void rt2560_vap_delete(struct ieee80211vap *);
92 static void rt2560_dma_map_addr(void *, bus_dma_segment_t *, int,
94 static int rt2560_alloc_tx_ring(struct rt2560_softc *,
95 struct rt2560_tx_ring *, int);
96 static void rt2560_reset_tx_ring(struct rt2560_softc *,
97 struct rt2560_tx_ring *);
98 static void rt2560_free_tx_ring(struct rt2560_softc *,
99 struct rt2560_tx_ring *);
100 static int rt2560_alloc_rx_ring(struct rt2560_softc *,
101 struct rt2560_rx_ring *, int);
102 static void rt2560_reset_rx_ring(struct rt2560_softc *,
103 struct rt2560_rx_ring *);
104 static void rt2560_free_rx_ring(struct rt2560_softc *,
105 struct rt2560_rx_ring *);
106 static struct ieee80211_node *rt2560_node_alloc(struct ieee80211vap *,
107 const uint8_t [IEEE80211_ADDR_LEN]);
108 static void rt2560_newassoc(struct ieee80211_node *, int);
109 static int rt2560_newstate(struct ieee80211vap *,
110 enum ieee80211_state, int);
111 static uint16_t rt2560_eeprom_read(struct rt2560_softc *, uint8_t);
112 static void rt2560_encryption_intr(struct rt2560_softc *);
113 static void rt2560_tx_intr(struct rt2560_softc *);
114 static void rt2560_prio_intr(struct rt2560_softc *);
115 static void rt2560_decryption_intr(struct rt2560_softc *);
116 static void rt2560_rx_intr(struct rt2560_softc *);
117 static void rt2560_beacon_update(struct ieee80211vap *, int item);
118 static void rt2560_beacon_expire(struct rt2560_softc *);
119 static void rt2560_wakeup_expire(struct rt2560_softc *);
120 static void rt2560_scan_start(struct ieee80211com *);
121 static void rt2560_scan_end(struct ieee80211com *);
122 static void rt2560_set_channel(struct ieee80211com *);
123 static void rt2560_setup_tx_desc(struct rt2560_softc *,
124 struct rt2560_tx_desc *, uint32_t, int, int, int,
126 static int rt2560_tx_bcn(struct rt2560_softc *, struct mbuf *,
127 struct ieee80211_node *);
128 static int rt2560_tx_mgt(struct rt2560_softc *, struct mbuf *,
129 struct ieee80211_node *);
130 static int rt2560_tx_data(struct rt2560_softc *, struct mbuf *,
131 struct ieee80211_node *);
132 static void rt2560_start_locked(struct ifnet *);
133 static void rt2560_start(struct ifnet *);
134 static void rt2560_watchdog(void *);
135 static int rt2560_ioctl(struct ifnet *, u_long, caddr_t);
136 static void rt2560_bbp_write(struct rt2560_softc *, uint8_t,
138 static uint8_t rt2560_bbp_read(struct rt2560_softc *, uint8_t);
139 static void rt2560_rf_write(struct rt2560_softc *, uint8_t,
141 static void rt2560_set_chan(struct rt2560_softc *,
142 struct ieee80211_channel *);
144 static void rt2560_disable_rf_tune(struct rt2560_softc *);
146 static void rt2560_enable_tsf_sync(struct rt2560_softc *);
147 static void rt2560_enable_tsf(struct rt2560_softc *);
148 static void rt2560_update_plcp(struct rt2560_softc *);
149 static void rt2560_update_slot(struct ifnet *);
150 static void rt2560_set_basicrates(struct rt2560_softc *);
151 static void rt2560_update_led(struct rt2560_softc *, int, int);
152 static void rt2560_set_bssid(struct rt2560_softc *, const uint8_t *);
153 static void rt2560_set_macaddr(struct rt2560_softc *, uint8_t *);
154 static void rt2560_get_macaddr(struct rt2560_softc *, uint8_t *);
155 static void rt2560_update_promisc(struct ifnet *);
156 static const char *rt2560_get_rf(int);
157 static void rt2560_read_config(struct rt2560_softc *);
158 static int rt2560_bbp_init(struct rt2560_softc *);
159 static void rt2560_set_txantenna(struct rt2560_softc *, int);
160 static void rt2560_set_rxantenna(struct rt2560_softc *, int);
161 static void rt2560_init_locked(struct rt2560_softc *);
162 static void rt2560_init(void *);
163 static void rt2560_stop_locked(struct rt2560_softc *);
164 static int rt2560_raw_xmit(struct ieee80211_node *, struct mbuf *,
165 const struct ieee80211_bpf_params *);
167 static const struct {
170 } rt2560_def_mac[] = {
174 static const struct {
177 } rt2560_def_bbp[] = {
181 static const uint32_t rt2560_rf2522_r2[] = RT2560_RF2522_R2;
182 static const uint32_t rt2560_rf2523_r2[] = RT2560_RF2523_R2;
183 static const uint32_t rt2560_rf2524_r2[] = RT2560_RF2524_R2;
184 static const uint32_t rt2560_rf2525_r2[] = RT2560_RF2525_R2;
185 static const uint32_t rt2560_rf2525_hi_r2[] = RT2560_RF2525_HI_R2;
186 static const uint32_t rt2560_rf2525e_r2[] = RT2560_RF2525E_R2;
187 static const uint32_t rt2560_rf2526_r2[] = RT2560_RF2526_R2;
188 static const uint32_t rt2560_rf2526_hi_r2[] = RT2560_RF2526_HI_R2;
190 static const struct {
193 } rt2560_rf5222[] = {
198 rt2560_attach(device_t dev, int id)
200 struct rt2560_softc *sc = device_get_softc(dev);
201 struct ieee80211com *ic;
205 uint8_t macaddr[IEEE80211_ADDR_LEN];
209 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
210 MTX_DEF | MTX_RECURSE);
212 callout_init_mtx(&sc->watchdog_ch, &sc->sc_mtx, 0);
214 /* retrieve RT2560 rev. no */
215 sc->asic_rev = RAL_READ(sc, RT2560_CSR0);
217 /* retrieve RF rev. no and various other things from EEPROM */
218 rt2560_read_config(sc);
220 device_printf(dev, "MAC/BBP RT2560 (rev 0x%02x), RF %s\n",
221 sc->asic_rev, rt2560_get_rf(sc->rf_rev));
224 * Allocate Tx and Rx rings.
226 error = rt2560_alloc_tx_ring(sc, &sc->txq, RT2560_TX_RING_COUNT);
228 device_printf(sc->sc_dev, "could not allocate Tx ring\n");
232 error = rt2560_alloc_tx_ring(sc, &sc->atimq, RT2560_ATIM_RING_COUNT);
234 device_printf(sc->sc_dev, "could not allocate ATIM ring\n");
238 error = rt2560_alloc_tx_ring(sc, &sc->prioq, RT2560_PRIO_RING_COUNT);
240 device_printf(sc->sc_dev, "could not allocate Prio ring\n");
244 error = rt2560_alloc_tx_ring(sc, &sc->bcnq, RT2560_BEACON_RING_COUNT);
246 device_printf(sc->sc_dev, "could not allocate Beacon ring\n");
250 error = rt2560_alloc_rx_ring(sc, &sc->rxq, RT2560_RX_RING_COUNT);
252 device_printf(sc->sc_dev, "could not allocate Rx ring\n");
256 ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
258 device_printf(sc->sc_dev, "can not if_alloc()\n");
263 /* retrieve MAC address */
264 rt2560_get_macaddr(sc, macaddr);
267 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
268 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
269 ifp->if_init = rt2560_init;
270 ifp->if_ioctl = rt2560_ioctl;
271 ifp->if_start = rt2560_start;
272 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
273 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
274 IFQ_SET_READY(&ifp->if_snd);
277 ic->ic_opmode = IEEE80211_M_STA;
278 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
280 /* set device capabilities */
282 IEEE80211_C_STA /* station mode */
283 | IEEE80211_C_IBSS /* ibss, nee adhoc, mode */
284 | IEEE80211_C_HOSTAP /* hostap mode */
285 | IEEE80211_C_MONITOR /* monitor mode */
286 | IEEE80211_C_AHDEMO /* adhoc demo mode */
287 | IEEE80211_C_WDS /* 4-address traffic works */
288 | IEEE80211_C_MBSS /* mesh point link mode */
289 | IEEE80211_C_SHPREAMBLE /* short preamble supported */
290 | IEEE80211_C_SHSLOT /* short slot time supported */
291 | IEEE80211_C_WPA /* capable of WPA1+WPA2 */
292 | IEEE80211_C_BGSCAN /* capable of bg scanning */
294 | IEEE80211_C_TXFRAG /* handle tx frags */
299 setbit(&bands, IEEE80211_MODE_11B);
300 setbit(&bands, IEEE80211_MODE_11G);
301 if (sc->rf_rev == RT2560_RF_5222)
302 setbit(&bands, IEEE80211_MODE_11A);
303 ieee80211_init_channels(ic, NULL, &bands);
305 ieee80211_ifattach(ic, macaddr);
306 ic->ic_newassoc = rt2560_newassoc;
307 ic->ic_raw_xmit = rt2560_raw_xmit;
308 ic->ic_updateslot = rt2560_update_slot;
309 ic->ic_update_promisc = rt2560_update_promisc;
310 ic->ic_node_alloc = rt2560_node_alloc;
311 ic->ic_scan_start = rt2560_scan_start;
312 ic->ic_scan_end = rt2560_scan_end;
313 ic->ic_set_channel = rt2560_set_channel;
315 ic->ic_vap_create = rt2560_vap_create;
316 ic->ic_vap_delete = rt2560_vap_delete;
318 ieee80211_radiotap_attach(ic,
319 &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
320 RT2560_TX_RADIOTAP_PRESENT,
321 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
322 RT2560_RX_RADIOTAP_PRESENT);
325 * Add a few sysctl knobs.
328 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
329 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
330 "debug", CTLFLAG_RW, &sc->sc_debug, 0, "debug msgs");
332 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
333 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
334 "txantenna", CTLFLAG_RW, &sc->tx_ant, 0, "tx antenna (0=auto)");
336 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
337 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
338 "rxantenna", CTLFLAG_RW, &sc->rx_ant, 0, "rx antenna (0=auto)");
341 ieee80211_announce(ic);
345 fail6: rt2560_free_rx_ring(sc, &sc->rxq);
346 fail5: rt2560_free_tx_ring(sc, &sc->bcnq);
347 fail4: rt2560_free_tx_ring(sc, &sc->prioq);
348 fail3: rt2560_free_tx_ring(sc, &sc->atimq);
349 fail2: rt2560_free_tx_ring(sc, &sc->txq);
350 fail1: mtx_destroy(&sc->sc_mtx);
356 rt2560_detach(void *xsc)
358 struct rt2560_softc *sc = xsc;
359 struct ifnet *ifp = sc->sc_ifp;
360 struct ieee80211com *ic = ifp->if_l2com;
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);
379 static struct ieee80211vap *
380 rt2560_vap_create(struct ieee80211com *ic,
381 const char name[IFNAMSIZ], int unit, int opmode, int flags,
382 const uint8_t bssid[IEEE80211_ADDR_LEN],
383 const uint8_t mac[IEEE80211_ADDR_LEN])
385 struct ifnet *ifp = ic->ic_ifp;
386 struct rt2560_vap *rvp;
387 struct ieee80211vap *vap;
390 case IEEE80211_M_STA:
391 case IEEE80211_M_IBSS:
392 case IEEE80211_M_AHDEMO:
393 case IEEE80211_M_MONITOR:
394 case IEEE80211_M_HOSTAP:
395 case IEEE80211_M_MBSS:
397 if (!TAILQ_EMPTY(&ic->ic_vaps)) {
398 if_printf(ifp, "only 1 vap supported\n");
401 if (opmode == IEEE80211_M_STA)
402 flags |= IEEE80211_CLONE_NOBEACONS;
404 case IEEE80211_M_WDS:
405 if (TAILQ_EMPTY(&ic->ic_vaps) ||
406 ic->ic_opmode != IEEE80211_M_HOSTAP) {
407 if_printf(ifp, "wds only supported in ap mode\n");
411 * Silently remove any request for a unique
412 * bssid; WDS vap's always share the local
415 flags &= ~IEEE80211_CLONE_BSSID;
418 if_printf(ifp, "unknown opmode %d\n", opmode);
421 rvp = (struct rt2560_vap *) malloc(sizeof(struct rt2560_vap),
422 M_80211_VAP, M_NOWAIT | M_ZERO);
426 ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac);
428 /* override state transition machine */
429 rvp->ral_newstate = vap->iv_newstate;
430 vap->iv_newstate = rt2560_newstate;
431 vap->iv_update_beacon = rt2560_beacon_update;
433 ieee80211_amrr_init(&rvp->amrr, vap,
434 IEEE80211_AMRR_MIN_SUCCESS_THRESHOLD,
435 IEEE80211_AMRR_MAX_SUCCESS_THRESHOLD,
439 ieee80211_vap_attach(vap, ieee80211_media_change, ieee80211_media_status);
440 if (TAILQ_FIRST(&ic->ic_vaps) == vap)
441 ic->ic_opmode = opmode;
446 rt2560_vap_delete(struct ieee80211vap *vap)
448 struct rt2560_vap *rvp = RT2560_VAP(vap);
450 ieee80211_amrr_cleanup(&rvp->amrr);
451 ieee80211_vap_detach(vap);
452 free(rvp, M_80211_VAP);
456 rt2560_resume(void *xsc)
458 struct rt2560_softc *sc = xsc;
459 struct ifnet *ifp = sc->sc_ifp;
461 if (ifp->if_flags & IFF_UP)
466 rt2560_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
471 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
473 *(bus_addr_t *)arg = segs[0].ds_addr;
477 rt2560_alloc_tx_ring(struct rt2560_softc *sc, struct rt2560_tx_ring *ring,
484 ring->cur = ring->next = 0;
485 ring->cur_encrypt = ring->next_encrypt = 0;
487 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 4, 0,
488 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
489 count * RT2560_TX_DESC_SIZE, 1, count * RT2560_TX_DESC_SIZE,
490 0, NULL, NULL, &ring->desc_dmat);
492 device_printf(sc->sc_dev, "could not create desc DMA tag\n");
496 error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc,
497 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_map);
499 device_printf(sc->sc_dev, "could not allocate DMA memory\n");
503 error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc,
504 count * RT2560_TX_DESC_SIZE, rt2560_dma_map_addr, &ring->physaddr,
507 device_printf(sc->sc_dev, "could not load desc DMA map\n");
511 ring->data = malloc(count * sizeof (struct rt2560_tx_data), M_DEVBUF,
513 if (ring->data == NULL) {
514 device_printf(sc->sc_dev, "could not allocate soft data\n");
519 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
520 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
521 MCLBYTES, RT2560_MAX_SCATTER, MCLBYTES, 0, NULL, NULL,
524 device_printf(sc->sc_dev, "could not create data DMA tag\n");
528 for (i = 0; i < count; i++) {
529 error = bus_dmamap_create(ring->data_dmat, 0,
532 device_printf(sc->sc_dev, "could not create DMA map\n");
539 fail: rt2560_free_tx_ring(sc, ring);
544 rt2560_reset_tx_ring(struct rt2560_softc *sc, struct rt2560_tx_ring *ring)
546 struct rt2560_tx_desc *desc;
547 struct rt2560_tx_data *data;
550 for (i = 0; i < ring->count; i++) {
551 desc = &ring->desc[i];
552 data = &ring->data[i];
554 if (data->m != NULL) {
555 bus_dmamap_sync(ring->data_dmat, data->map,
556 BUS_DMASYNC_POSTWRITE);
557 bus_dmamap_unload(ring->data_dmat, data->map);
562 if (data->ni != NULL) {
563 ieee80211_free_node(data->ni);
570 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_PREWRITE);
573 ring->cur = ring->next = 0;
574 ring->cur_encrypt = ring->next_encrypt = 0;
578 rt2560_free_tx_ring(struct rt2560_softc *sc, struct rt2560_tx_ring *ring)
580 struct rt2560_tx_data *data;
583 if (ring->desc != NULL) {
584 bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
585 BUS_DMASYNC_POSTWRITE);
586 bus_dmamap_unload(ring->desc_dmat, ring->desc_map);
587 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
590 if (ring->desc_dmat != NULL)
591 bus_dma_tag_destroy(ring->desc_dmat);
593 if (ring->data != NULL) {
594 for (i = 0; i < ring->count; i++) {
595 data = &ring->data[i];
597 if (data->m != NULL) {
598 bus_dmamap_sync(ring->data_dmat, data->map,
599 BUS_DMASYNC_POSTWRITE);
600 bus_dmamap_unload(ring->data_dmat, data->map);
604 if (data->ni != NULL)
605 ieee80211_free_node(data->ni);
607 if (data->map != NULL)
608 bus_dmamap_destroy(ring->data_dmat, data->map);
611 free(ring->data, M_DEVBUF);
614 if (ring->data_dmat != NULL)
615 bus_dma_tag_destroy(ring->data_dmat);
619 rt2560_alloc_rx_ring(struct rt2560_softc *sc, struct rt2560_rx_ring *ring,
622 struct rt2560_rx_desc *desc;
623 struct rt2560_rx_data *data;
628 ring->cur = ring->next = 0;
629 ring->cur_decrypt = 0;
631 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 4, 0,
632 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
633 count * RT2560_RX_DESC_SIZE, 1, count * RT2560_RX_DESC_SIZE,
634 0, NULL, NULL, &ring->desc_dmat);
636 device_printf(sc->sc_dev, "could not create desc DMA tag\n");
640 error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc,
641 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_map);
643 device_printf(sc->sc_dev, "could not allocate DMA memory\n");
647 error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc,
648 count * RT2560_RX_DESC_SIZE, rt2560_dma_map_addr, &ring->physaddr,
651 device_printf(sc->sc_dev, "could not load desc DMA map\n");
655 ring->data = malloc(count * sizeof (struct rt2560_rx_data), M_DEVBUF,
657 if (ring->data == NULL) {
658 device_printf(sc->sc_dev, "could not allocate soft data\n");
664 * Pre-allocate Rx buffers and populate Rx ring.
666 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
667 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES,
668 1, MCLBYTES, 0, NULL, NULL, &ring->data_dmat);
670 device_printf(sc->sc_dev, "could not create data DMA tag\n");
674 for (i = 0; i < count; i++) {
675 desc = &sc->rxq.desc[i];
676 data = &sc->rxq.data[i];
678 error = bus_dmamap_create(ring->data_dmat, 0, &data->map);
680 device_printf(sc->sc_dev, "could not create DMA map\n");
684 data->m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
685 if (data->m == NULL) {
686 device_printf(sc->sc_dev,
687 "could not allocate rx mbuf\n");
692 error = bus_dmamap_load(ring->data_dmat, data->map,
693 mtod(data->m, void *), MCLBYTES, rt2560_dma_map_addr,
696 device_printf(sc->sc_dev,
697 "could not load rx buf DMA map");
701 desc->flags = htole32(RT2560_RX_BUSY);
702 desc->physaddr = htole32(physaddr);
705 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_PREWRITE);
709 fail: rt2560_free_rx_ring(sc, ring);
714 rt2560_reset_rx_ring(struct rt2560_softc *sc, struct rt2560_rx_ring *ring)
718 for (i = 0; i < ring->count; i++) {
719 ring->desc[i].flags = htole32(RT2560_RX_BUSY);
720 ring->data[i].drop = 0;
723 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_PREWRITE);
725 ring->cur = ring->next = 0;
726 ring->cur_decrypt = 0;
730 rt2560_free_rx_ring(struct rt2560_softc *sc, struct rt2560_rx_ring *ring)
732 struct rt2560_rx_data *data;
735 if (ring->desc != NULL) {
736 bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
737 BUS_DMASYNC_POSTWRITE);
738 bus_dmamap_unload(ring->desc_dmat, ring->desc_map);
739 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
742 if (ring->desc_dmat != NULL)
743 bus_dma_tag_destroy(ring->desc_dmat);
745 if (ring->data != NULL) {
746 for (i = 0; i < ring->count; i++) {
747 data = &ring->data[i];
749 if (data->m != NULL) {
750 bus_dmamap_sync(ring->data_dmat, data->map,
751 BUS_DMASYNC_POSTREAD);
752 bus_dmamap_unload(ring->data_dmat, data->map);
756 if (data->map != NULL)
757 bus_dmamap_destroy(ring->data_dmat, data->map);
760 free(ring->data, M_DEVBUF);
763 if (ring->data_dmat != NULL)
764 bus_dma_tag_destroy(ring->data_dmat);
767 static struct ieee80211_node *
768 rt2560_node_alloc(struct ieee80211vap *vap,
769 const uint8_t mac[IEEE80211_ADDR_LEN])
771 struct rt2560_node *rn;
773 rn = malloc(sizeof (struct rt2560_node), M_80211_NODE,
776 return (rn != NULL) ? &rn->ni : NULL;
780 rt2560_newassoc(struct ieee80211_node *ni, int isnew)
782 struct ieee80211vap *vap = ni->ni_vap;
784 ieee80211_amrr_node_init(&RT2560_VAP(vap)->amrr,
785 &RT2560_NODE(ni)->amrr, ni);
789 rt2560_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
791 struct rt2560_vap *rvp = RT2560_VAP(vap);
792 struct ifnet *ifp = vap->iv_ic->ic_ifp;
793 struct rt2560_softc *sc = ifp->if_softc;
796 if (nstate == IEEE80211_S_INIT && vap->iv_state == IEEE80211_S_RUN) {
797 /* abort TSF synchronization */
798 RAL_WRITE(sc, RT2560_CSR14, 0);
800 /* turn association led off */
801 rt2560_update_led(sc, 0, 0);
804 error = rvp->ral_newstate(vap, nstate, arg);
806 if (error == 0 && nstate == IEEE80211_S_RUN) {
807 struct ieee80211_node *ni = vap->iv_bss;
810 if (vap->iv_opmode != IEEE80211_M_MONITOR) {
811 rt2560_update_plcp(sc);
812 rt2560_set_basicrates(sc);
813 rt2560_set_bssid(sc, ni->ni_bssid);
816 if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
817 vap->iv_opmode == IEEE80211_M_IBSS ||
818 vap->iv_opmode == IEEE80211_M_MBSS) {
819 m = ieee80211_beacon_alloc(ni, &rvp->ral_bo);
821 if_printf(ifp, "could not allocate beacon\n");
824 ieee80211_ref_node(ni);
825 error = rt2560_tx_bcn(sc, m, ni);
830 /* turn assocation led on */
831 rt2560_update_led(sc, 1, 0);
833 if (vap->iv_opmode != IEEE80211_M_MONITOR)
834 rt2560_enable_tsf_sync(sc);
836 rt2560_enable_tsf(sc);
842 * Read 16 bits at address 'addr' from the serial EEPROM (either 93C46 or
846 rt2560_eeprom_read(struct rt2560_softc *sc, uint8_t addr)
852 /* clock C once before the first command */
853 RT2560_EEPROM_CTL(sc, 0);
855 RT2560_EEPROM_CTL(sc, RT2560_S);
856 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C);
857 RT2560_EEPROM_CTL(sc, RT2560_S);
859 /* write start bit (1) */
860 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D);
861 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D | RT2560_C);
863 /* write READ opcode (10) */
864 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D);
865 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D | RT2560_C);
866 RT2560_EEPROM_CTL(sc, RT2560_S);
867 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C);
869 /* write address (A5-A0 or A7-A0) */
870 n = (RAL_READ(sc, RT2560_CSR21) & RT2560_93C46) ? 5 : 7;
871 for (; n >= 0; n--) {
872 RT2560_EEPROM_CTL(sc, RT2560_S |
873 (((addr >> n) & 1) << RT2560_SHIFT_D));
874 RT2560_EEPROM_CTL(sc, RT2560_S |
875 (((addr >> n) & 1) << RT2560_SHIFT_D) | RT2560_C);
878 RT2560_EEPROM_CTL(sc, RT2560_S);
880 /* read data Q15-Q0 */
882 for (n = 15; n >= 0; n--) {
883 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C);
884 tmp = RAL_READ(sc, RT2560_CSR21);
885 val |= ((tmp & RT2560_Q) >> RT2560_SHIFT_Q) << n;
886 RT2560_EEPROM_CTL(sc, RT2560_S);
889 RT2560_EEPROM_CTL(sc, 0);
891 /* clear Chip Select and clock C */
892 RT2560_EEPROM_CTL(sc, RT2560_S);
893 RT2560_EEPROM_CTL(sc, 0);
894 RT2560_EEPROM_CTL(sc, RT2560_C);
900 * Some frames were processed by the hardware cipher engine and are ready for
904 rt2560_encryption_intr(struct rt2560_softc *sc)
906 struct rt2560_tx_desc *desc;
909 /* retrieve last descriptor index processed by cipher engine */
910 hw = RAL_READ(sc, RT2560_SECCSR1) - sc->txq.physaddr;
911 hw /= RT2560_TX_DESC_SIZE;
913 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
914 BUS_DMASYNC_POSTREAD);
916 while (sc->txq.next_encrypt != hw) {
917 if (sc->txq.next_encrypt == sc->txq.cur_encrypt) {
918 printf("hw encrypt %d, cur_encrypt %d\n", hw,
919 sc->txq.cur_encrypt);
923 desc = &sc->txq.desc[sc->txq.next_encrypt];
925 if ((le32toh(desc->flags) & RT2560_TX_BUSY) ||
926 (le32toh(desc->flags) & RT2560_TX_CIPHER_BUSY))
929 /* for TKIP, swap eiv field to fix a bug in ASIC */
930 if ((le32toh(desc->flags) & RT2560_TX_CIPHER_MASK) ==
931 RT2560_TX_CIPHER_TKIP)
932 desc->eiv = bswap32(desc->eiv);
934 /* mark the frame ready for transmission */
935 desc->flags |= htole32(RT2560_TX_VALID);
936 desc->flags |= htole32(RT2560_TX_BUSY);
938 DPRINTFN(sc, 15, "encryption done idx=%u\n",
939 sc->txq.next_encrypt);
941 sc->txq.next_encrypt =
942 (sc->txq.next_encrypt + 1) % RT2560_TX_RING_COUNT;
945 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
946 BUS_DMASYNC_PREWRITE);
949 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_TX);
953 rt2560_tx_intr(struct rt2560_softc *sc)
955 struct ifnet *ifp = sc->sc_ifp;
956 struct rt2560_tx_desc *desc;
957 struct rt2560_tx_data *data;
958 struct rt2560_node *rn;
963 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
964 BUS_DMASYNC_POSTREAD);
967 desc = &sc->txq.desc[sc->txq.next];
968 data = &sc->txq.data[sc->txq.next];
970 flags = le32toh(desc->flags);
971 if ((flags & RT2560_TX_BUSY) ||
972 (flags & RT2560_TX_CIPHER_BUSY) ||
973 !(flags & RT2560_TX_VALID))
976 rn = (struct rt2560_node *)data->ni;
979 switch (flags & RT2560_TX_RESULT_MASK) {
980 case RT2560_TX_SUCCESS:
981 DPRINTFN(sc, 10, "%s\n", "data frame sent successfully");
982 if (data->rix != IEEE80211_FIXED_RATE_NONE)
983 ieee80211_amrr_tx_complete(&rn->amrr,
984 IEEE80211_AMRR_SUCCESS, 0);
988 case RT2560_TX_SUCCESS_RETRY:
989 retrycnt = RT2560_TX_RETRYCNT(flags);
991 DPRINTFN(sc, 9, "data frame sent after %u retries\n",
993 if (data->rix != IEEE80211_FIXED_RATE_NONE)
994 ieee80211_amrr_tx_complete(&rn->amrr,
995 IEEE80211_AMRR_SUCCESS, retrycnt);
999 case RT2560_TX_FAIL_RETRY:
1000 retrycnt = RT2560_TX_RETRYCNT(flags);
1002 DPRINTFN(sc, 9, "data frame failed after %d retries\n",
1004 if (data->rix != IEEE80211_FIXED_RATE_NONE)
1005 ieee80211_amrr_tx_complete(&rn->amrr,
1006 IEEE80211_AMRR_FAILURE, retrycnt);
1010 case RT2560_TX_FAIL_INVALID:
1011 case RT2560_TX_FAIL_OTHER:
1013 device_printf(sc->sc_dev, "sending data frame failed "
1018 bus_dmamap_sync(sc->txq.data_dmat, data->map,
1019 BUS_DMASYNC_POSTWRITE);
1020 bus_dmamap_unload(sc->txq.data_dmat, data->map);
1023 ieee80211_free_node(data->ni);
1026 /* descriptor is no longer valid */
1027 desc->flags &= ~htole32(RT2560_TX_VALID);
1029 DPRINTFN(sc, 15, "tx done idx=%u\n", sc->txq.next);
1032 sc->txq.next = (sc->txq.next + 1) % RT2560_TX_RING_COUNT;
1035 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
1036 BUS_DMASYNC_PREWRITE);
1038 if (sc->prioq.queued == 0 && sc->txq.queued == 0)
1039 sc->sc_tx_timer = 0;
1041 if (sc->txq.queued < RT2560_TX_RING_COUNT - 1) {
1042 sc->sc_flags &= ~RT2560_F_DATA_OACTIVE;
1044 (RT2560_F_DATA_OACTIVE | RT2560_F_PRIO_OACTIVE)) == 0)
1045 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1046 rt2560_start_locked(ifp);
1051 rt2560_prio_intr(struct rt2560_softc *sc)
1053 struct ifnet *ifp = sc->sc_ifp;
1054 struct rt2560_tx_desc *desc;
1055 struct rt2560_tx_data *data;
1056 struct ieee80211_node *ni;
1060 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map,
1061 BUS_DMASYNC_POSTREAD);
1064 desc = &sc->prioq.desc[sc->prioq.next];
1065 data = &sc->prioq.data[sc->prioq.next];
1067 flags = le32toh(desc->flags);
1068 if ((flags & RT2560_TX_BUSY) || (flags & RT2560_TX_VALID) == 0)
1071 switch (flags & RT2560_TX_RESULT_MASK) {
1072 case RT2560_TX_SUCCESS:
1073 DPRINTFN(sc, 10, "%s\n", "mgt frame sent successfully");
1076 case RT2560_TX_SUCCESS_RETRY:
1077 DPRINTFN(sc, 9, "mgt frame sent after %u retries\n",
1078 (flags >> 5) & 0x7);
1081 case RT2560_TX_FAIL_RETRY:
1082 DPRINTFN(sc, 9, "%s\n",
1083 "sending mgt frame failed (too much retries)");
1086 case RT2560_TX_FAIL_INVALID:
1087 case RT2560_TX_FAIL_OTHER:
1089 device_printf(sc->sc_dev, "sending mgt frame failed "
1094 bus_dmamap_sync(sc->prioq.data_dmat, data->map,
1095 BUS_DMASYNC_POSTWRITE);
1096 bus_dmamap_unload(sc->prioq.data_dmat, data->map);
1103 /* descriptor is no longer valid */
1104 desc->flags &= ~htole32(RT2560_TX_VALID);
1106 DPRINTFN(sc, 15, "prio done idx=%u\n", sc->prioq.next);
1109 sc->prioq.next = (sc->prioq.next + 1) % RT2560_PRIO_RING_COUNT;
1111 if (m->m_flags & M_TXCB)
1112 ieee80211_process_callback(ni, m,
1113 (flags & RT2560_TX_RESULT_MASK) &~
1114 (RT2560_TX_SUCCESS | RT2560_TX_SUCCESS_RETRY));
1116 ieee80211_free_node(ni);
1119 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map,
1120 BUS_DMASYNC_PREWRITE);
1122 if (sc->prioq.queued == 0 && sc->txq.queued == 0)
1123 sc->sc_tx_timer = 0;
1125 if (sc->prioq.queued < RT2560_PRIO_RING_COUNT) {
1126 sc->sc_flags &= ~RT2560_F_PRIO_OACTIVE;
1128 (RT2560_F_DATA_OACTIVE | RT2560_F_PRIO_OACTIVE)) == 0)
1129 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1130 rt2560_start_locked(ifp);
1135 * Some frames were processed by the hardware cipher engine and are ready for
1136 * handoff to the IEEE802.11 layer.
1139 rt2560_decryption_intr(struct rt2560_softc *sc)
1141 struct ifnet *ifp = sc->sc_ifp;
1142 struct ieee80211com *ic = ifp->if_l2com;
1143 struct rt2560_rx_desc *desc;
1144 struct rt2560_rx_data *data;
1145 bus_addr_t physaddr;
1146 struct ieee80211_frame *wh;
1147 struct ieee80211_node *ni;
1148 struct mbuf *mnew, *m;
1152 /* retrieve last decriptor index processed by cipher engine */
1153 hw = RAL_READ(sc, RT2560_SECCSR0) - sc->rxq.physaddr;
1154 hw /= RT2560_RX_DESC_SIZE;
1156 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1157 BUS_DMASYNC_POSTREAD);
1159 for (; sc->rxq.cur_decrypt != hw;) {
1160 desc = &sc->rxq.desc[sc->rxq.cur_decrypt];
1161 data = &sc->rxq.data[sc->rxq.cur_decrypt];
1163 if ((le32toh(desc->flags) & RT2560_RX_BUSY) ||
1164 (le32toh(desc->flags) & RT2560_RX_CIPHER_BUSY))
1172 if ((le32toh(desc->flags) & RT2560_RX_CIPHER_MASK) != 0 &&
1173 (le32toh(desc->flags) & RT2560_RX_ICV_ERROR)) {
1179 * Try to allocate a new mbuf for this ring element and load it
1180 * before processing the current mbuf. If the ring element
1181 * cannot be loaded, drop the received packet and reuse the old
1182 * mbuf. In the unlikely case that the old mbuf can't be
1183 * reloaded either, explicitly panic.
1185 mnew = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1191 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
1192 BUS_DMASYNC_POSTREAD);
1193 bus_dmamap_unload(sc->rxq.data_dmat, data->map);
1195 error = bus_dmamap_load(sc->rxq.data_dmat, data->map,
1196 mtod(mnew, void *), MCLBYTES, rt2560_dma_map_addr,
1201 /* try to reload the old mbuf */
1202 error = bus_dmamap_load(sc->rxq.data_dmat, data->map,
1203 mtod(data->m, void *), MCLBYTES,
1204 rt2560_dma_map_addr, &physaddr, 0);
1206 /* very unlikely that it will fail... */
1207 panic("%s: could not load old rx mbuf",
1208 device_get_name(sc->sc_dev));
1215 * New mbuf successfully loaded, update Rx ring and continue
1220 desc->physaddr = htole32(physaddr);
1223 m->m_pkthdr.rcvif = ifp;
1224 m->m_pkthdr.len = m->m_len =
1225 (le32toh(desc->flags) >> 16) & 0xfff;
1227 rssi = RT2560_RSSI(sc, desc->rssi);
1228 nf = RT2560_NOISE_FLOOR;
1229 if (ieee80211_radiotap_active(ic)) {
1230 struct rt2560_rx_radiotap_header *tap = &sc->sc_rxtap;
1231 uint32_t tsf_lo, tsf_hi;
1233 /* get timestamp (low and high 32 bits) */
1234 tsf_hi = RAL_READ(sc, RT2560_CSR17);
1235 tsf_lo = RAL_READ(sc, RT2560_CSR16);
1238 htole64(((uint64_t)tsf_hi << 32) | tsf_lo);
1240 tap->wr_rate = ieee80211_plcp2rate(desc->rate,
1241 (desc->flags & htole32(RT2560_RX_OFDM)) ?
1242 IEEE80211_T_OFDM : IEEE80211_T_CCK);
1243 tap->wr_antenna = sc->rx_ant;
1244 tap->wr_antsignal = nf + rssi;
1245 tap->wr_antnoise = nf;
1248 sc->sc_flags |= RT2560_F_INPUT_RUNNING;
1250 wh = mtod(m, struct ieee80211_frame *);
1251 ni = ieee80211_find_rxnode(ic,
1252 (struct ieee80211_frame_min *)wh);
1254 (void) ieee80211_input(ni, m, rssi, nf);
1255 ieee80211_free_node(ni);
1257 (void) ieee80211_input_all(ic, m, rssi, nf);
1260 sc->sc_flags &= ~RT2560_F_INPUT_RUNNING;
1261 skip: desc->flags = htole32(RT2560_RX_BUSY);
1263 DPRINTFN(sc, 15, "decryption done idx=%u\n", sc->rxq.cur_decrypt);
1265 sc->rxq.cur_decrypt =
1266 (sc->rxq.cur_decrypt + 1) % RT2560_RX_RING_COUNT;
1269 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1270 BUS_DMASYNC_PREWRITE);
1274 * Some frames were received. Pass them to the hardware cipher engine before
1275 * sending them to the 802.11 layer.
1278 rt2560_rx_intr(struct rt2560_softc *sc)
1280 struct rt2560_rx_desc *desc;
1281 struct rt2560_rx_data *data;
1283 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1284 BUS_DMASYNC_POSTREAD);
1287 desc = &sc->rxq.desc[sc->rxq.cur];
1288 data = &sc->rxq.data[sc->rxq.cur];
1290 if ((le32toh(desc->flags) & RT2560_RX_BUSY) ||
1291 (le32toh(desc->flags) & RT2560_RX_CIPHER_BUSY))
1296 if ((le32toh(desc->flags) & RT2560_RX_PHY_ERROR) ||
1297 (le32toh(desc->flags) & RT2560_RX_CRC_ERROR)) {
1299 * This should not happen since we did not request
1300 * to receive those frames when we filled RXCSR0.
1302 DPRINTFN(sc, 5, "PHY or CRC error flags 0x%08x\n",
1303 le32toh(desc->flags));
1307 if (((le32toh(desc->flags) >> 16) & 0xfff) > MCLBYTES) {
1308 DPRINTFN(sc, 5, "%s\n", "bad length");
1312 /* mark the frame for decryption */
1313 desc->flags |= htole32(RT2560_RX_CIPHER_BUSY);
1315 DPRINTFN(sc, 15, "rx done idx=%u\n", sc->rxq.cur);
1317 sc->rxq.cur = (sc->rxq.cur + 1) % RT2560_RX_RING_COUNT;
1320 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1321 BUS_DMASYNC_PREWRITE);
1324 RAL_WRITE(sc, RT2560_SECCSR0, RT2560_KICK_DECRYPT);
1328 rt2560_beacon_update(struct ieee80211vap *vap, int item)
1330 struct rt2560_vap *rvp = RT2560_VAP(vap);
1331 struct ieee80211_beacon_offsets *bo = &rvp->ral_bo;
1333 setbit(bo->bo_flags, item);
1337 * This function is called periodically in IBSS mode when a new beacon must be
1341 rt2560_beacon_expire(struct rt2560_softc *sc)
1343 struct ifnet *ifp = sc->sc_ifp;
1344 struct ieee80211com *ic = ifp->if_l2com;
1345 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1346 struct rt2560_vap *rvp = RT2560_VAP(vap);
1347 struct rt2560_tx_data *data;
1349 if (ic->ic_opmode != IEEE80211_M_IBSS &&
1350 ic->ic_opmode != IEEE80211_M_HOSTAP &&
1351 ic->ic_opmode != IEEE80211_M_MBSS)
1354 data = &sc->bcnq.data[sc->bcnq.next];
1356 * Don't send beacon if bsschan isn't set
1358 if (data->ni == NULL)
1361 bus_dmamap_sync(sc->bcnq.data_dmat, data->map, BUS_DMASYNC_POSTWRITE);
1362 bus_dmamap_unload(sc->bcnq.data_dmat, data->map);
1364 /* XXX 1 =>'s mcast frames which means all PS sta's will wakeup! */
1365 ieee80211_beacon_update(data->ni, &rvp->ral_bo, data->m, 1);
1367 rt2560_tx_bcn(sc, data->m, data->ni);
1369 DPRINTFN(sc, 15, "%s", "beacon expired\n");
1371 sc->bcnq.next = (sc->bcnq.next + 1) % RT2560_BEACON_RING_COUNT;
1376 rt2560_wakeup_expire(struct rt2560_softc *sc)
1378 DPRINTFN(sc, 2, "%s", "wakeup expired\n");
1382 rt2560_intr(void *arg)
1384 struct rt2560_softc *sc = arg;
1385 struct ifnet *ifp = sc->sc_ifp;
1390 /* disable interrupts */
1391 RAL_WRITE(sc, RT2560_CSR8, 0xffffffff);
1393 /* don't re-enable interrupts if we're shutting down */
1394 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1399 r = RAL_READ(sc, RT2560_CSR7);
1400 RAL_WRITE(sc, RT2560_CSR7, r);
1402 if (r & RT2560_BEACON_EXPIRE)
1403 rt2560_beacon_expire(sc);
1405 if (r & RT2560_WAKEUP_EXPIRE)
1406 rt2560_wakeup_expire(sc);
1408 if (r & RT2560_ENCRYPTION_DONE)
1409 rt2560_encryption_intr(sc);
1411 if (r & RT2560_TX_DONE)
1414 if (r & RT2560_PRIO_DONE)
1415 rt2560_prio_intr(sc);
1417 if (r & RT2560_DECRYPTION_DONE)
1418 rt2560_decryption_intr(sc);
1420 if (r & RT2560_RX_DONE) {
1422 rt2560_encryption_intr(sc);
1425 /* re-enable interrupts */
1426 RAL_WRITE(sc, RT2560_CSR8, RT2560_INTR_MASK);
1431 #define RAL_SIFS 10 /* us */
1433 #define RT2560_TXRX_TURNAROUND 10 /* us */
1436 rt2560_plcp_signal(int rate)
1439 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1440 case 12: return 0xb;
1441 case 18: return 0xf;
1442 case 24: return 0xa;
1443 case 36: return 0xe;
1444 case 48: return 0x9;
1445 case 72: return 0xd;
1446 case 96: return 0x8;
1447 case 108: return 0xc;
1449 /* CCK rates (NB: not IEEE std, device-specific) */
1452 case 11: return 0x2;
1453 case 22: return 0x3;
1455 return 0xff; /* XXX unsupported/unknown rate */
1459 rt2560_setup_tx_desc(struct rt2560_softc *sc, struct rt2560_tx_desc *desc,
1460 uint32_t flags, int len, int rate, int encrypt, bus_addr_t physaddr)
1462 struct ifnet *ifp = sc->sc_ifp;
1463 struct ieee80211com *ic = ifp->if_l2com;
1464 uint16_t plcp_length;
1467 desc->flags = htole32(flags);
1468 desc->flags |= htole32(len << 16);
1470 desc->physaddr = htole32(physaddr);
1471 desc->wme = htole16(
1473 RT2560_LOGCWMIN(3) |
1474 RT2560_LOGCWMAX(8));
1476 /* setup PLCP fields */
1477 desc->plcp_signal = rt2560_plcp_signal(rate);
1478 desc->plcp_service = 4;
1480 len += IEEE80211_CRC_LEN;
1481 if (ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) {
1482 desc->flags |= htole32(RT2560_TX_OFDM);
1484 plcp_length = len & 0xfff;
1485 desc->plcp_length_hi = plcp_length >> 6;
1486 desc->plcp_length_lo = plcp_length & 0x3f;
1488 plcp_length = (16 * len + rate - 1) / rate;
1490 remainder = (16 * len) % 22;
1491 if (remainder != 0 && remainder < 7)
1492 desc->plcp_service |= RT2560_PLCP_LENGEXT;
1494 desc->plcp_length_hi = plcp_length >> 8;
1495 desc->plcp_length_lo = plcp_length & 0xff;
1497 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1498 desc->plcp_signal |= 0x08;
1502 desc->flags |= htole32(RT2560_TX_VALID);
1503 desc->flags |= encrypt ? htole32(RT2560_TX_CIPHER_BUSY)
1504 : htole32(RT2560_TX_BUSY);
1508 rt2560_tx_bcn(struct rt2560_softc *sc, struct mbuf *m0,
1509 struct ieee80211_node *ni)
1511 struct ieee80211vap *vap = ni->ni_vap;
1512 struct rt2560_tx_desc *desc;
1513 struct rt2560_tx_data *data;
1514 bus_dma_segment_t segs[RT2560_MAX_SCATTER];
1515 int nsegs, rate, error;
1517 desc = &sc->bcnq.desc[sc->bcnq.cur];
1518 data = &sc->bcnq.data[sc->bcnq.cur];
1520 /* XXX maybe a separate beacon rate? */
1521 rate = vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)].mgmtrate;
1523 error = bus_dmamap_load_mbuf_sg(sc->bcnq.data_dmat, data->map, m0,
1524 segs, &nsegs, BUS_DMA_NOWAIT);
1526 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1532 if (ieee80211_radiotap_active_vap(vap)) {
1533 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1536 tap->wt_rate = rate;
1537 tap->wt_antenna = sc->tx_ant;
1539 ieee80211_radiotap_tx(vap, m0);
1545 rt2560_setup_tx_desc(sc, desc, RT2560_TX_IFS_NEWBACKOFF |
1546 RT2560_TX_TIMESTAMP, m0->m_pkthdr.len, rate, 0, segs->ds_addr);
1548 DPRINTFN(sc, 10, "sending beacon frame len=%u idx=%u rate=%u\n",
1549 m0->m_pkthdr.len, sc->bcnq.cur, rate);
1551 bus_dmamap_sync(sc->bcnq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1552 bus_dmamap_sync(sc->bcnq.desc_dmat, sc->bcnq.desc_map,
1553 BUS_DMASYNC_PREWRITE);
1555 sc->bcnq.cur = (sc->bcnq.cur + 1) % RT2560_BEACON_RING_COUNT;
1561 rt2560_tx_mgt(struct rt2560_softc *sc, struct mbuf *m0,
1562 struct ieee80211_node *ni)
1564 struct ieee80211vap *vap = ni->ni_vap;
1565 struct ieee80211com *ic = ni->ni_ic;
1566 struct rt2560_tx_desc *desc;
1567 struct rt2560_tx_data *data;
1568 struct ieee80211_frame *wh;
1569 struct ieee80211_key *k;
1570 bus_dma_segment_t segs[RT2560_MAX_SCATTER];
1573 int nsegs, rate, error;
1575 desc = &sc->prioq.desc[sc->prioq.cur];
1576 data = &sc->prioq.data[sc->prioq.cur];
1578 rate = vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)].mgmtrate;
1580 wh = mtod(m0, struct ieee80211_frame *);
1582 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1583 k = ieee80211_crypto_encap(ni, m0);
1590 error = bus_dmamap_load_mbuf_sg(sc->prioq.data_dmat, data->map, m0,
1593 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1599 if (ieee80211_radiotap_active_vap(vap)) {
1600 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1603 tap->wt_rate = rate;
1604 tap->wt_antenna = sc->tx_ant;
1606 ieee80211_radiotap_tx(vap, m0);
1611 /* management frames are not taken into account for amrr */
1612 data->rix = IEEE80211_FIXED_RATE_NONE;
1614 wh = mtod(m0, struct ieee80211_frame *);
1616 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1617 flags |= RT2560_TX_ACK;
1619 dur = ieee80211_ack_duration(ic->ic_rt,
1620 rate, ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1621 *(uint16_t *)wh->i_dur = htole16(dur);
1623 /* tell hardware to add timestamp for probe responses */
1624 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
1625 IEEE80211_FC0_TYPE_MGT &&
1626 (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
1627 IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1628 flags |= RT2560_TX_TIMESTAMP;
1631 rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate, 0,
1634 bus_dmamap_sync(sc->prioq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1635 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map,
1636 BUS_DMASYNC_PREWRITE);
1638 DPRINTFN(sc, 10, "sending mgt frame len=%u idx=%u rate=%u\n",
1639 m0->m_pkthdr.len, sc->prioq.cur, rate);
1643 sc->prioq.cur = (sc->prioq.cur + 1) % RT2560_PRIO_RING_COUNT;
1644 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_PRIO);
1650 rt2560_sendprot(struct rt2560_softc *sc,
1651 const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate)
1653 struct ieee80211com *ic = ni->ni_ic;
1654 const struct ieee80211_frame *wh;
1655 struct rt2560_tx_desc *desc;
1656 struct rt2560_tx_data *data;
1658 int protrate, ackrate, pktlen, flags, isshort, error;
1660 bus_dma_segment_t segs[RT2560_MAX_SCATTER];
1663 KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY,
1664 ("protection %d", prot));
1666 wh = mtod(m, const struct ieee80211_frame *);
1667 pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
1669 protrate = ieee80211_ctl_rate(ic->ic_rt, rate);
1670 ackrate = ieee80211_ack_rate(ic->ic_rt, rate);
1672 isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0;
1673 dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort)
1674 + ieee80211_ack_duration(ic->ic_rt, rate, isshort);
1675 flags = RT2560_TX_MORE_FRAG;
1676 if (prot == IEEE80211_PROT_RTSCTS) {
1677 /* NB: CTS is the same size as an ACK */
1678 dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort);
1679 flags |= RT2560_TX_ACK;
1680 mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur);
1682 mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur);
1684 if (mprot == NULL) {
1685 /* XXX stat + msg */
1689 desc = &sc->txq.desc[sc->txq.cur_encrypt];
1690 data = &sc->txq.data[sc->txq.cur_encrypt];
1692 error = bus_dmamap_load_mbuf_sg(sc->txq.data_dmat, data->map,
1693 mprot, segs, &nsegs, 0);
1695 device_printf(sc->sc_dev,
1696 "could not map mbuf (error %d)\n", error);
1702 data->ni = ieee80211_ref_node(ni);
1703 /* ctl frames are not taken into account for amrr */
1704 data->rix = IEEE80211_FIXED_RATE_NONE;
1706 rt2560_setup_tx_desc(sc, desc, flags, mprot->m_pkthdr.len, protrate, 1,
1709 bus_dmamap_sync(sc->txq.data_dmat, data->map,
1710 BUS_DMASYNC_PREWRITE);
1713 sc->txq.cur_encrypt = (sc->txq.cur_encrypt + 1) % RT2560_TX_RING_COUNT;
1719 rt2560_tx_raw(struct rt2560_softc *sc, struct mbuf *m0,
1720 struct ieee80211_node *ni, const struct ieee80211_bpf_params *params)
1722 struct ieee80211vap *vap = ni->ni_vap;
1723 struct ieee80211com *ic = ni->ni_ic;
1724 struct rt2560_tx_desc *desc;
1725 struct rt2560_tx_data *data;
1726 bus_dma_segment_t segs[RT2560_MAX_SCATTER];
1728 int nsegs, rate, error;
1730 desc = &sc->prioq.desc[sc->prioq.cur];
1731 data = &sc->prioq.data[sc->prioq.cur];
1733 rate = params->ibp_rate0;
1734 if (!ieee80211_isratevalid(ic->ic_rt, rate)) {
1735 /* XXX fall back to mcast/mgmt rate? */
1741 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
1742 flags |= RT2560_TX_ACK;
1743 if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) {
1744 error = rt2560_sendprot(sc, m0, ni,
1745 params->ibp_flags & IEEE80211_BPF_RTS ?
1746 IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY,
1752 flags |= RT2560_TX_LONG_RETRY | RT2560_TX_IFS_SIFS;
1755 error = bus_dmamap_load_mbuf_sg(sc->prioq.data_dmat, data->map, m0,
1758 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1764 if (ieee80211_radiotap_active_vap(vap)) {
1765 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1768 tap->wt_rate = rate;
1769 tap->wt_antenna = sc->tx_ant;
1771 ieee80211_radiotap_tx(ni->ni_vap, m0);
1777 /* XXX need to setup descriptor ourself */
1778 rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len,
1779 rate, (params->ibp_flags & IEEE80211_BPF_CRYPTO) != 0,
1782 bus_dmamap_sync(sc->prioq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1783 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map,
1784 BUS_DMASYNC_PREWRITE);
1786 DPRINTFN(sc, 10, "sending raw frame len=%u idx=%u rate=%u\n",
1787 m0->m_pkthdr.len, sc->prioq.cur, rate);
1791 sc->prioq.cur = (sc->prioq.cur + 1) % RT2560_PRIO_RING_COUNT;
1792 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_PRIO);
1798 rt2560_tx_data(struct rt2560_softc *sc, struct mbuf *m0,
1799 struct ieee80211_node *ni)
1801 struct ieee80211vap *vap = ni->ni_vap;
1802 struct ieee80211com *ic = ni->ni_ic;
1803 struct rt2560_tx_desc *desc;
1804 struct rt2560_tx_data *data;
1805 struct ieee80211_frame *wh;
1806 const struct ieee80211_txparam *tp;
1807 struct ieee80211_key *k;
1809 bus_dma_segment_t segs[RT2560_MAX_SCATTER];
1812 int nsegs, rate, error;
1814 wh = mtod(m0, struct ieee80211_frame *);
1816 tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
1817 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1818 rate = tp->mcastrate;
1819 } else if (m0->m_flags & M_EAPOL) {
1820 rate = tp->mgmtrate;
1821 } else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) {
1822 rate = tp->ucastrate;
1824 (void) ieee80211_amrr_choose(ni, &RT2560_NODE(ni)->amrr);
1825 rate = ni->ni_txrate;
1828 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1829 k = ieee80211_crypto_encap(ni, m0);
1835 /* packet header may have moved, reset our local pointer */
1836 wh = mtod(m0, struct ieee80211_frame *);
1840 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1841 int prot = IEEE80211_PROT_NONE;
1842 if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold)
1843 prot = IEEE80211_PROT_RTSCTS;
1844 else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
1845 ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM)
1846 prot = ic->ic_protmode;
1847 if (prot != IEEE80211_PROT_NONE) {
1848 error = rt2560_sendprot(sc, m0, ni, prot, rate);
1853 flags |= RT2560_TX_LONG_RETRY | RT2560_TX_IFS_SIFS;
1857 data = &sc->txq.data[sc->txq.cur_encrypt];
1858 desc = &sc->txq.desc[sc->txq.cur_encrypt];
1860 error = bus_dmamap_load_mbuf_sg(sc->txq.data_dmat, data->map, m0,
1862 if (error != 0 && error != EFBIG) {
1863 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1869 mnew = m_defrag(m0, M_DONTWAIT);
1871 device_printf(sc->sc_dev,
1872 "could not defragment mbuf\n");
1878 error = bus_dmamap_load_mbuf_sg(sc->txq.data_dmat, data->map,
1879 m0, segs, &nsegs, 0);
1881 device_printf(sc->sc_dev,
1882 "could not map mbuf (error %d)\n", error);
1887 /* packet header may have moved, reset our local pointer */
1888 wh = mtod(m0, struct ieee80211_frame *);
1891 if (ieee80211_radiotap_active_vap(vap)) {
1892 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1895 tap->wt_rate = rate;
1896 tap->wt_antenna = sc->tx_ant;
1898 ieee80211_radiotap_tx(vap, m0);
1904 /* remember link conditions for rate adaptation algorithm */
1905 if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE) {
1906 data->rix = ni->ni_txrate;
1907 /* XXX probably need last rssi value and not avg */
1908 data->rssi = ic->ic_node_getrssi(ni);
1910 data->rix = IEEE80211_FIXED_RATE_NONE;
1912 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1913 flags |= RT2560_TX_ACK;
1915 dur = ieee80211_ack_duration(ic->ic_rt,
1916 rate, ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1917 *(uint16_t *)wh->i_dur = htole16(dur);
1920 rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate, 1,
1923 bus_dmamap_sync(sc->txq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1924 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
1925 BUS_DMASYNC_PREWRITE);
1927 DPRINTFN(sc, 10, "sending data frame len=%u idx=%u rate=%u\n",
1928 m0->m_pkthdr.len, sc->txq.cur_encrypt, rate);
1932 sc->txq.cur_encrypt = (sc->txq.cur_encrypt + 1) % RT2560_TX_RING_COUNT;
1933 RAL_WRITE(sc, RT2560_SECCSR1, RT2560_KICK_ENCRYPT);
1939 rt2560_start_locked(struct ifnet *ifp)
1941 struct rt2560_softc *sc = ifp->if_softc;
1943 struct ieee80211_node *ni;
1945 RAL_LOCK_ASSERT(sc);
1948 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1951 if (sc->txq.queued >= RT2560_TX_RING_COUNT - 1) {
1952 IFQ_DRV_PREPEND(&ifp->if_snd, m);
1953 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1954 sc->sc_flags |= RT2560_F_DATA_OACTIVE;
1957 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1958 if (rt2560_tx_data(sc, m, ni) != 0) {
1959 ieee80211_free_node(ni);
1964 sc->sc_tx_timer = 5;
1969 rt2560_start(struct ifnet *ifp)
1971 struct rt2560_softc *sc = ifp->if_softc;
1974 rt2560_start_locked(ifp);
1979 rt2560_watchdog(void *arg)
1981 struct rt2560_softc *sc = arg;
1982 struct ifnet *ifp = sc->sc_ifp;
1984 RAL_LOCK_ASSERT(sc);
1986 KASSERT(ifp->if_drv_flags & IFF_DRV_RUNNING, ("not running"));
1988 if (sc->sc_invalid) /* card ejected */
1991 rt2560_encryption_intr(sc);
1994 if (sc->sc_tx_timer > 0 && --sc->sc_tx_timer == 0) {
1995 if_printf(ifp, "device timeout\n");
1996 rt2560_init_locked(sc);
1998 /* NB: callout is reset in rt2560_init() */
2001 callout_reset(&sc->watchdog_ch, hz, rt2560_watchdog, sc);
2005 rt2560_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
2007 struct rt2560_softc *sc = ifp->if_softc;
2008 struct ieee80211com *ic = ifp->if_l2com;
2009 struct ifreq *ifr = (struct ifreq *) data;
2010 int error = 0, startall = 0;
2015 if (ifp->if_flags & IFF_UP) {
2016 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2017 rt2560_init_locked(sc);
2020 rt2560_update_promisc(ifp);
2022 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2023 rt2560_stop_locked(sc);
2027 ieee80211_start_all(ic);
2030 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
2033 error = ether_ioctl(ifp, cmd, data);
2043 rt2560_bbp_write(struct rt2560_softc *sc, uint8_t reg, uint8_t val)
2048 for (ntries = 0; ntries < 100; ntries++) {
2049 if (!(RAL_READ(sc, RT2560_BBPCSR) & RT2560_BBP_BUSY))
2053 if (ntries == 100) {
2054 device_printf(sc->sc_dev, "could not write to BBP\n");
2058 tmp = RT2560_BBP_WRITE | RT2560_BBP_BUSY | reg << 8 | val;
2059 RAL_WRITE(sc, RT2560_BBPCSR, tmp);
2061 DPRINTFN(sc, 15, "BBP R%u <- 0x%02x\n", reg, val);
2065 rt2560_bbp_read(struct rt2560_softc *sc, uint8_t reg)
2070 for (ntries = 0; ntries < 100; ntries++) {
2071 if (!(RAL_READ(sc, RT2560_BBPCSR) & RT2560_BBP_BUSY))
2075 if (ntries == 100) {
2076 device_printf(sc->sc_dev, "could not read from BBP\n");
2080 val = RT2560_BBP_BUSY | reg << 8;
2081 RAL_WRITE(sc, RT2560_BBPCSR, val);
2083 for (ntries = 0; ntries < 100; ntries++) {
2084 val = RAL_READ(sc, RT2560_BBPCSR);
2085 if (!(val & RT2560_BBP_BUSY))
2090 device_printf(sc->sc_dev, "could not read from BBP\n");
2095 rt2560_rf_write(struct rt2560_softc *sc, uint8_t reg, uint32_t val)
2100 for (ntries = 0; ntries < 100; ntries++) {
2101 if (!(RAL_READ(sc, RT2560_RFCSR) & RT2560_RF_BUSY))
2105 if (ntries == 100) {
2106 device_printf(sc->sc_dev, "could not write to RF\n");
2110 tmp = RT2560_RF_BUSY | RT2560_RF_20BIT | (val & 0xfffff) << 2 |
2112 RAL_WRITE(sc, RT2560_RFCSR, tmp);
2114 /* remember last written value in sc */
2115 sc->rf_regs[reg] = val;
2117 DPRINTFN(sc, 15, "RF R[%u] <- 0x%05x\n", reg & 0x3, val & 0xfffff);
2121 rt2560_set_chan(struct rt2560_softc *sc, struct ieee80211_channel *c)
2123 struct ifnet *ifp = sc->sc_ifp;
2124 struct ieee80211com *ic = ifp->if_l2com;
2128 chan = ieee80211_chan2ieee(ic, c);
2129 KASSERT(chan != 0 && chan != IEEE80211_CHAN_ANY, ("chan 0x%x", chan));
2131 if (IEEE80211_IS_CHAN_2GHZ(c))
2132 power = min(sc->txpow[chan - 1], 31);
2136 /* adjust txpower using ifconfig settings */
2137 power -= (100 - ic->ic_txpowlimit) / 8;
2139 DPRINTFN(sc, 2, "setting channel to %u, txpower to %u\n", chan, power);
2141 switch (sc->rf_rev) {
2142 case RT2560_RF_2522:
2143 rt2560_rf_write(sc, RAL_RF1, 0x00814);
2144 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2522_r2[chan - 1]);
2145 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
2148 case RT2560_RF_2523:
2149 rt2560_rf_write(sc, RAL_RF1, 0x08804);
2150 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2523_r2[chan - 1]);
2151 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x38044);
2152 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2155 case RT2560_RF_2524:
2156 rt2560_rf_write(sc, RAL_RF1, 0x0c808);
2157 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2524_r2[chan - 1]);
2158 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
2159 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2162 case RT2560_RF_2525:
2163 rt2560_rf_write(sc, RAL_RF1, 0x08808);
2164 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525_hi_r2[chan - 1]);
2165 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2166 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2168 rt2560_rf_write(sc, RAL_RF1, 0x08808);
2169 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525_r2[chan - 1]);
2170 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2171 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2174 case RT2560_RF_2525E:
2175 rt2560_rf_write(sc, RAL_RF1, 0x08808);
2176 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525e_r2[chan - 1]);
2177 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2178 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00286 : 0x00282);
2181 case RT2560_RF_2526:
2182 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2526_hi_r2[chan - 1]);
2183 rt2560_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
2184 rt2560_rf_write(sc, RAL_RF1, 0x08804);
2186 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2526_r2[chan - 1]);
2187 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2188 rt2560_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
2192 case RT2560_RF_5222:
2193 for (i = 0; rt2560_rf5222[i].chan != chan; i++);
2195 rt2560_rf_write(sc, RAL_RF1, rt2560_rf5222[i].r1);
2196 rt2560_rf_write(sc, RAL_RF2, rt2560_rf5222[i].r2);
2197 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
2198 rt2560_rf_write(sc, RAL_RF4, rt2560_rf5222[i].r4);
2201 printf("unknown ral rev=%d\n", sc->rf_rev);
2205 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
2206 /* set Japan filter bit for channel 14 */
2207 tmp = rt2560_bbp_read(sc, 70);
2209 tmp &= ~RT2560_JAPAN_FILTER;
2211 tmp |= RT2560_JAPAN_FILTER;
2213 rt2560_bbp_write(sc, 70, tmp);
2215 /* clear CRC errors */
2216 RAL_READ(sc, RT2560_CNT0);
2221 rt2560_set_channel(struct ieee80211com *ic)
2223 struct ifnet *ifp = ic->ic_ifp;
2224 struct rt2560_softc *sc = ifp->if_softc;
2227 rt2560_set_chan(sc, ic->ic_curchan);
2234 * Disable RF auto-tuning.
2237 rt2560_disable_rf_tune(struct rt2560_softc *sc)
2241 if (sc->rf_rev != RT2560_RF_2523) {
2242 tmp = sc->rf_regs[RAL_RF1] & ~RAL_RF1_AUTOTUNE;
2243 rt2560_rf_write(sc, RAL_RF1, tmp);
2246 tmp = sc->rf_regs[RAL_RF3] & ~RAL_RF3_AUTOTUNE;
2247 rt2560_rf_write(sc, RAL_RF3, tmp);
2249 DPRINTFN(sc, 2, "%s", "disabling RF autotune\n");
2254 * Refer to IEEE Std 802.11-1999 pp. 123 for more information on TSF
2258 rt2560_enable_tsf_sync(struct rt2560_softc *sc)
2260 struct ifnet *ifp = sc->sc_ifp;
2261 struct ieee80211com *ic = ifp->if_l2com;
2262 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2263 uint16_t logcwmin, preload;
2266 /* first, disable TSF synchronization */
2267 RAL_WRITE(sc, RT2560_CSR14, 0);
2269 tmp = 16 * vap->iv_bss->ni_intval;
2270 RAL_WRITE(sc, RT2560_CSR12, tmp);
2272 RAL_WRITE(sc, RT2560_CSR13, 0);
2275 preload = (vap->iv_opmode == IEEE80211_M_STA) ? 384 : 1024;
2276 tmp = logcwmin << 16 | preload;
2277 RAL_WRITE(sc, RT2560_BCNOCSR, tmp);
2279 /* finally, enable TSF synchronization */
2280 tmp = RT2560_ENABLE_TSF | RT2560_ENABLE_TBCN;
2281 if (ic->ic_opmode == IEEE80211_M_STA)
2282 tmp |= RT2560_ENABLE_TSF_SYNC(1);
2284 tmp |= RT2560_ENABLE_TSF_SYNC(2) |
2285 RT2560_ENABLE_BEACON_GENERATOR;
2286 RAL_WRITE(sc, RT2560_CSR14, tmp);
2288 DPRINTF(sc, "%s", "enabling TSF synchronization\n");
2292 rt2560_enable_tsf(struct rt2560_softc *sc)
2294 RAL_WRITE(sc, RT2560_CSR14, 0);
2295 RAL_WRITE(sc, RT2560_CSR14,
2296 RT2560_ENABLE_TSF_SYNC(2) | RT2560_ENABLE_TSF);
2300 rt2560_update_plcp(struct rt2560_softc *sc)
2302 struct ifnet *ifp = sc->sc_ifp;
2303 struct ieee80211com *ic = ifp->if_l2com;
2305 /* no short preamble for 1Mbps */
2306 RAL_WRITE(sc, RT2560_PLCP1MCSR, 0x00700400);
2308 if (!(ic->ic_flags & IEEE80211_F_SHPREAMBLE)) {
2309 /* values taken from the reference driver */
2310 RAL_WRITE(sc, RT2560_PLCP2MCSR, 0x00380401);
2311 RAL_WRITE(sc, RT2560_PLCP5p5MCSR, 0x00150402);
2312 RAL_WRITE(sc, RT2560_PLCP11MCSR, 0x000b8403);
2314 /* same values as above or'ed 0x8 */
2315 RAL_WRITE(sc, RT2560_PLCP2MCSR, 0x00380409);
2316 RAL_WRITE(sc, RT2560_PLCP5p5MCSR, 0x0015040a);
2317 RAL_WRITE(sc, RT2560_PLCP11MCSR, 0x000b840b);
2320 DPRINTF(sc, "updating PLCP for %s preamble\n",
2321 (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ? "short" : "long");
2325 * This function can be called by ieee80211_set_shortslottime(). Refer to
2326 * IEEE Std 802.11-1999 pp. 85 to know how these values are computed.
2329 rt2560_update_slot(struct ifnet *ifp)
2331 struct rt2560_softc *sc = ifp->if_softc;
2332 struct ieee80211com *ic = ifp->if_l2com;
2334 uint16_t tx_sifs, tx_pifs, tx_difs, eifs;
2337 #ifndef FORCE_SLOTTIME
2338 slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
2341 * Setting slot time according to "short slot time" capability
2342 * in beacon/probe_resp seems to cause problem to acknowledge
2343 * certain AP's data frames transimitted at CCK/DS rates: the
2344 * problematic AP keeps retransmitting data frames, probably
2345 * because MAC level acks are not received by hardware.
2346 * So we cheat a little bit here by claiming we are capable of
2347 * "short slot time" but setting hardware slot time to the normal
2348 * slot time. ral(4) does not seem to have trouble to receive
2349 * frames transmitted using short slot time even if hardware
2350 * slot time is set to normal slot time. If we didn't use this
2351 * trick, we would have to claim that short slot time is not
2352 * supported; this would give relative poor RX performance
2353 * (-1Mb~-2Mb lower) and the _whole_ BSS would stop using short
2359 /* update the MAC slot boundaries */
2360 tx_sifs = RAL_SIFS - RT2560_TXRX_TURNAROUND;
2361 tx_pifs = tx_sifs + slottime;
2362 tx_difs = tx_sifs + 2 * slottime;
2363 eifs = (ic->ic_curmode == IEEE80211_MODE_11B) ? 364 : 60;
2365 tmp = RAL_READ(sc, RT2560_CSR11);
2366 tmp = (tmp & ~0x1f00) | slottime << 8;
2367 RAL_WRITE(sc, RT2560_CSR11, tmp);
2369 tmp = tx_pifs << 16 | tx_sifs;
2370 RAL_WRITE(sc, RT2560_CSR18, tmp);
2372 tmp = eifs << 16 | tx_difs;
2373 RAL_WRITE(sc, RT2560_CSR19, tmp);
2375 DPRINTF(sc, "setting slottime to %uus\n", slottime);
2379 rt2560_set_basicrates(struct rt2560_softc *sc)
2381 struct ifnet *ifp = sc->sc_ifp;
2382 struct ieee80211com *ic = ifp->if_l2com;
2384 /* update basic rate set */
2385 if (ic->ic_curmode == IEEE80211_MODE_11B) {
2386 /* 11b basic rates: 1, 2Mbps */
2387 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x3);
2388 } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan)) {
2389 /* 11a basic rates: 6, 12, 24Mbps */
2390 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x150);
2392 /* 11g basic rates: 1, 2, 5.5, 11, 6, 12, 24Mbps */
2393 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x15f);
2398 rt2560_update_led(struct rt2560_softc *sc, int led1, int led2)
2402 /* set ON period to 70ms and OFF period to 30ms */
2403 tmp = led1 << 16 | led2 << 17 | 70 << 8 | 30;
2404 RAL_WRITE(sc, RT2560_LEDCSR, tmp);
2408 rt2560_set_bssid(struct rt2560_softc *sc, const uint8_t *bssid)
2412 tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24;
2413 RAL_WRITE(sc, RT2560_CSR5, tmp);
2415 tmp = bssid[4] | bssid[5] << 8;
2416 RAL_WRITE(sc, RT2560_CSR6, tmp);
2418 DPRINTF(sc, "setting BSSID to %6D\n", bssid, ":");
2422 rt2560_set_macaddr(struct rt2560_softc *sc, uint8_t *addr)
2426 tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24;
2427 RAL_WRITE(sc, RT2560_CSR3, tmp);
2429 tmp = addr[4] | addr[5] << 8;
2430 RAL_WRITE(sc, RT2560_CSR4, tmp);
2432 DPRINTF(sc, "setting MAC address to %6D\n", addr, ":");
2436 rt2560_get_macaddr(struct rt2560_softc *sc, uint8_t *addr)
2440 tmp = RAL_READ(sc, RT2560_CSR3);
2441 addr[0] = tmp & 0xff;
2442 addr[1] = (tmp >> 8) & 0xff;
2443 addr[2] = (tmp >> 16) & 0xff;
2444 addr[3] = (tmp >> 24);
2446 tmp = RAL_READ(sc, RT2560_CSR4);
2447 addr[4] = tmp & 0xff;
2448 addr[5] = (tmp >> 8) & 0xff;
2452 rt2560_update_promisc(struct ifnet *ifp)
2454 struct rt2560_softc *sc = ifp->if_softc;
2457 tmp = RAL_READ(sc, RT2560_RXCSR0);
2459 tmp &= ~RT2560_DROP_NOT_TO_ME;
2460 if (!(ifp->if_flags & IFF_PROMISC))
2461 tmp |= RT2560_DROP_NOT_TO_ME;
2463 RAL_WRITE(sc, RT2560_RXCSR0, tmp);
2465 DPRINTF(sc, "%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
2466 "entering" : "leaving");
2470 rt2560_get_rf(int rev)
2473 case RT2560_RF_2522: return "RT2522";
2474 case RT2560_RF_2523: return "RT2523";
2475 case RT2560_RF_2524: return "RT2524";
2476 case RT2560_RF_2525: return "RT2525";
2477 case RT2560_RF_2525E: return "RT2525e";
2478 case RT2560_RF_2526: return "RT2526";
2479 case RT2560_RF_5222: return "RT5222";
2480 default: return "unknown";
2485 rt2560_read_config(struct rt2560_softc *sc)
2490 val = rt2560_eeprom_read(sc, RT2560_EEPROM_CONFIG0);
2491 sc->rf_rev = (val >> 11) & 0x7;
2492 sc->hw_radio = (val >> 10) & 0x1;
2493 sc->led_mode = (val >> 6) & 0x7;
2494 sc->rx_ant = (val >> 4) & 0x3;
2495 sc->tx_ant = (val >> 2) & 0x3;
2496 sc->nb_ant = val & 0x3;
2498 /* read default values for BBP registers */
2499 for (i = 0; i < 16; i++) {
2500 val = rt2560_eeprom_read(sc, RT2560_EEPROM_BBP_BASE + i);
2501 if (val == 0 || val == 0xffff)
2504 sc->bbp_prom[i].reg = val >> 8;
2505 sc->bbp_prom[i].val = val & 0xff;
2508 /* read Tx power for all b/g channels */
2509 for (i = 0; i < 14 / 2; i++) {
2510 val = rt2560_eeprom_read(sc, RT2560_EEPROM_TXPOWER + i);
2511 sc->txpow[i * 2] = val & 0xff;
2512 sc->txpow[i * 2 + 1] = val >> 8;
2514 for (i = 0; i < 14; ++i) {
2515 if (sc->txpow[i] > 31)
2519 val = rt2560_eeprom_read(sc, RT2560_EEPROM_CALIBRATE);
2520 if ((val & 0xff) == 0xff)
2521 sc->rssi_corr = RT2560_DEFAULT_RSSI_CORR;
2523 sc->rssi_corr = val & 0xff;
2524 DPRINTF(sc, "rssi correction %d, calibrate 0x%02x\n",
2525 sc->rssi_corr, val);
2530 rt2560_scan_start(struct ieee80211com *ic)
2532 struct ifnet *ifp = ic->ic_ifp;
2533 struct rt2560_softc *sc = ifp->if_softc;
2535 /* abort TSF synchronization */
2536 RAL_WRITE(sc, RT2560_CSR14, 0);
2537 rt2560_set_bssid(sc, ifp->if_broadcastaddr);
2541 rt2560_scan_end(struct ieee80211com *ic)
2543 struct ifnet *ifp = ic->ic_ifp;
2544 struct rt2560_softc *sc = ifp->if_softc;
2545 struct ieee80211vap *vap = ic->ic_scan->ss_vap;
2547 rt2560_enable_tsf_sync(sc);
2548 /* XXX keep local copy */
2549 rt2560_set_bssid(sc, vap->iv_bss->ni_bssid);
2553 rt2560_bbp_init(struct rt2560_softc *sc)
2555 #define N(a) (sizeof (a) / sizeof ((a)[0]))
2558 /* wait for BBP to be ready */
2559 for (ntries = 0; ntries < 100; ntries++) {
2560 if (rt2560_bbp_read(sc, RT2560_BBP_VERSION) != 0)
2564 if (ntries == 100) {
2565 device_printf(sc->sc_dev, "timeout waiting for BBP\n");
2569 /* initialize BBP registers to default values */
2570 for (i = 0; i < N(rt2560_def_bbp); i++) {
2571 rt2560_bbp_write(sc, rt2560_def_bbp[i].reg,
2572 rt2560_def_bbp[i].val);
2575 /* initialize BBP registers to values stored in EEPROM */
2576 for (i = 0; i < 16; i++) {
2577 if (sc->bbp_prom[i].reg == 0 && sc->bbp_prom[i].val == 0)
2579 rt2560_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
2581 rt2560_bbp_write(sc, 17, 0x48); /* XXX restore bbp17 */
2588 rt2560_set_txantenna(struct rt2560_softc *sc, int antenna)
2593 tx = rt2560_bbp_read(sc, RT2560_BBP_TX) & ~RT2560_BBP_ANTMASK;
2595 tx |= RT2560_BBP_ANTA;
2596 else if (antenna == 2)
2597 tx |= RT2560_BBP_ANTB;
2599 tx |= RT2560_BBP_DIVERSITY;
2601 /* need to force I/Q flip for RF 2525e, 2526 and 5222 */
2602 if (sc->rf_rev == RT2560_RF_2525E || sc->rf_rev == RT2560_RF_2526 ||
2603 sc->rf_rev == RT2560_RF_5222)
2604 tx |= RT2560_BBP_FLIPIQ;
2606 rt2560_bbp_write(sc, RT2560_BBP_TX, tx);
2608 /* update values for CCK and OFDM in BBPCSR1 */
2609 tmp = RAL_READ(sc, RT2560_BBPCSR1) & ~0x00070007;
2610 tmp |= (tx & 0x7) << 16 | (tx & 0x7);
2611 RAL_WRITE(sc, RT2560_BBPCSR1, tmp);
2615 rt2560_set_rxantenna(struct rt2560_softc *sc, int antenna)
2619 rx = rt2560_bbp_read(sc, RT2560_BBP_RX) & ~RT2560_BBP_ANTMASK;
2621 rx |= RT2560_BBP_ANTA;
2622 else if (antenna == 2)
2623 rx |= RT2560_BBP_ANTB;
2625 rx |= RT2560_BBP_DIVERSITY;
2627 /* need to force no I/Q flip for RF 2525e and 2526 */
2628 if (sc->rf_rev == RT2560_RF_2525E || sc->rf_rev == RT2560_RF_2526)
2629 rx &= ~RT2560_BBP_FLIPIQ;
2631 rt2560_bbp_write(sc, RT2560_BBP_RX, rx);
2635 rt2560_init_locked(struct rt2560_softc *sc)
2637 #define N(a) (sizeof (a) / sizeof ((a)[0]))
2638 struct ifnet *ifp = sc->sc_ifp;
2639 struct ieee80211com *ic = ifp->if_l2com;
2643 RAL_LOCK_ASSERT(sc);
2645 rt2560_stop_locked(sc);
2647 /* setup tx rings */
2648 tmp = RT2560_PRIO_RING_COUNT << 24 |
2649 RT2560_ATIM_RING_COUNT << 16 |
2650 RT2560_TX_RING_COUNT << 8 |
2651 RT2560_TX_DESC_SIZE;
2653 /* rings must be initialized in this exact order */
2654 RAL_WRITE(sc, RT2560_TXCSR2, tmp);
2655 RAL_WRITE(sc, RT2560_TXCSR3, sc->txq.physaddr);
2656 RAL_WRITE(sc, RT2560_TXCSR5, sc->prioq.physaddr);
2657 RAL_WRITE(sc, RT2560_TXCSR4, sc->atimq.physaddr);
2658 RAL_WRITE(sc, RT2560_TXCSR6, sc->bcnq.physaddr);
2661 tmp = RT2560_RX_RING_COUNT << 8 | RT2560_RX_DESC_SIZE;
2663 RAL_WRITE(sc, RT2560_RXCSR1, tmp);
2664 RAL_WRITE(sc, RT2560_RXCSR2, sc->rxq.physaddr);
2666 /* initialize MAC registers to default values */
2667 for (i = 0; i < N(rt2560_def_mac); i++)
2668 RAL_WRITE(sc, rt2560_def_mac[i].reg, rt2560_def_mac[i].val);
2670 rt2560_set_macaddr(sc, IF_LLADDR(ifp));
2672 /* set basic rate set (will be updated later) */
2673 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x153);
2675 rt2560_update_slot(ifp);
2676 rt2560_update_plcp(sc);
2677 rt2560_update_led(sc, 0, 0);
2679 RAL_WRITE(sc, RT2560_CSR1, RT2560_RESET_ASIC);
2680 RAL_WRITE(sc, RT2560_CSR1, RT2560_HOST_READY);
2682 if (rt2560_bbp_init(sc) != 0) {
2688 rt2560_set_txantenna(sc, sc->tx_ant);
2689 rt2560_set_rxantenna(sc, sc->rx_ant);
2691 /* set default BSS channel */
2692 rt2560_set_chan(sc, ic->ic_curchan);
2695 tmp = RT2560_DROP_PHY_ERROR | RT2560_DROP_CRC_ERROR;
2696 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2697 tmp |= RT2560_DROP_CTL | RT2560_DROP_VERSION_ERROR;
2698 if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
2699 ic->ic_opmode != IEEE80211_M_MBSS)
2700 tmp |= RT2560_DROP_TODS;
2701 if (!(ifp->if_flags & IFF_PROMISC))
2702 tmp |= RT2560_DROP_NOT_TO_ME;
2704 RAL_WRITE(sc, RT2560_RXCSR0, tmp);
2706 /* clear old FCS and Rx FIFO errors */
2707 RAL_READ(sc, RT2560_CNT0);
2708 RAL_READ(sc, RT2560_CNT4);
2710 /* clear any pending interrupts */
2711 RAL_WRITE(sc, RT2560_CSR7, 0xffffffff);
2713 /* enable interrupts */
2714 RAL_WRITE(sc, RT2560_CSR8, RT2560_INTR_MASK);
2716 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2717 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2719 callout_reset(&sc->watchdog_ch, hz, rt2560_watchdog, sc);
2724 rt2560_init(void *priv)
2726 struct rt2560_softc *sc = priv;
2727 struct ifnet *ifp = sc->sc_ifp;
2728 struct ieee80211com *ic = ifp->if_l2com;
2731 rt2560_init_locked(sc);
2734 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2735 ieee80211_start_all(ic); /* start all vap's */
2739 rt2560_stop_locked(struct rt2560_softc *sc)
2741 struct ifnet *ifp = sc->sc_ifp;
2742 volatile int *flags = &sc->sc_flags;
2744 RAL_LOCK_ASSERT(sc);
2746 while (*flags & RT2560_F_INPUT_RUNNING)
2747 msleep(sc, &sc->sc_mtx, 0, "ralrunning", hz/10);
2749 callout_stop(&sc->watchdog_ch);
2750 sc->sc_tx_timer = 0;
2752 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
2753 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2756 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_ABORT_TX);
2759 RAL_WRITE(sc, RT2560_RXCSR0, RT2560_DISABLE_RX);
2761 /* reset ASIC (imply reset BBP) */
2762 RAL_WRITE(sc, RT2560_CSR1, RT2560_RESET_ASIC);
2763 RAL_WRITE(sc, RT2560_CSR1, 0);
2765 /* disable interrupts */
2766 RAL_WRITE(sc, RT2560_CSR8, 0xffffffff);
2768 /* reset Tx and Rx rings */
2769 rt2560_reset_tx_ring(sc, &sc->txq);
2770 rt2560_reset_tx_ring(sc, &sc->atimq);
2771 rt2560_reset_tx_ring(sc, &sc->prioq);
2772 rt2560_reset_tx_ring(sc, &sc->bcnq);
2773 rt2560_reset_rx_ring(sc, &sc->rxq);
2775 sc->sc_flags &= ~(RT2560_F_PRIO_OACTIVE | RT2560_F_DATA_OACTIVE);
2779 rt2560_stop(void *arg)
2781 struct rt2560_softc *sc = arg;
2784 rt2560_stop_locked(sc);
2789 rt2560_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2790 const struct ieee80211_bpf_params *params)
2792 struct ieee80211com *ic = ni->ni_ic;
2793 struct ifnet *ifp = ic->ic_ifp;
2794 struct rt2560_softc *sc = ifp->if_softc;
2798 /* prevent management frames from being sent if we're not ready */
2799 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2802 ieee80211_free_node(ni);
2805 if (sc->prioq.queued >= RT2560_PRIO_RING_COUNT) {
2806 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2807 sc->sc_flags |= RT2560_F_PRIO_OACTIVE;
2810 ieee80211_free_node(ni);
2811 return ENOBUFS; /* XXX */
2816 if (params == NULL) {
2818 * Legacy path; interpret frame contents to decide
2819 * precisely how to send the frame.
2821 if (rt2560_tx_mgt(sc, m, ni) != 0)
2825 * Caller supplied explicit parameters to use in
2826 * sending the frame.
2828 if (rt2560_tx_raw(sc, m, ni, params))
2831 sc->sc_tx_timer = 5;
2838 ieee80211_free_node(ni);
2840 return EIO; /* XXX */