Probe responses must be acknowledged
[ralink_drivers/rt2870_fbsd72.git] / rt2870.c
blobec75c0db17d3aca82a8d9193ae481b5b41c7e2ef
2 /*-
3 * Copyright (c) 2009-2010 Alexander Egorenkov <egorenar@gmail.com>
4 * Copyright (c) 2009 Damien Bergamini <damien.bergamini@free.fr>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include "rt2870_softc.h"
20 #include "rt2870_reg.h"
21 #include "rt2870_eeprom.h"
22 #include "rt2870_ucode.h"
23 #include "rt2870_rxwi.h"
24 #include "rt2870_rxinfo.h"
25 #include "rt2870_txwi.h"
26 #include "rt2870_txinfo.h"
27 #include "rt2870_read_eeprom.h"
28 #include "rt2870_io.h"
29 #include "rt2870_rf.h"
30 #include "rt2870_led.h"
31 #include "rt2870_debug.h"
34 * Defines and macros
37 #define USB_PRODUCT_LINKSYS4_WUSB600N 0x0071
38 #define USB_PRODUCT_DLINK2_DWA140 0x3c09
39 #define USB_PRODUCT_DLINK2_DWA160AREVB 0x3c11
40 #define USB_PRODUCT_ASUS_RT2770F 0x1742
41 #define USB_PRODUCT_RALINK_RT2770 0x2770
43 #define RT2870_USB_CONFIG_NO 1
44 #define RT2870_USB_IFACE_INDEX 0
46 #define RT2870_USB_REQ_MAC_READ_MULTI 0x07
48 /* packet length + Rx wireless info + Rx info */
49 #define RT2870_RX_DESC_SIZE \
50 (sizeof(uint32_t) + sizeof(struct rt2870_rxwi) + sizeof(struct rt2870_rxinfo))
52 /* Tx info + Tx wireless info + max padding */
53 #define RT2870_TX_DESC_SIZE \
54 (sizeof(struct rt2870_txinfo) + sizeof(struct rt2870_txwi) + 11)
56 #define RT2870_MAX_AGG_SIZE 3840
58 #define RT2870_USB_RX_BULK_BUFLEN (2048 * 12)
60 #define RT2870_NOISE_FLOOR -95
62 #define RT2870_AID2WCID(aid) ((aid) & 0xff)
64 #define RT2870_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
66 #define RT2870_ACK_SIZE 14
68 #define IEEE80211_HAS_ADDR4(wh) \
69 (((wh)->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS)
71 #define RT2870_MS(_v, _f) (((_v) & _f) >> _f##_S)
72 #define RT2870_SM(_v, _f) (((_v) << _f##_S) & _f)
74 #define RT2870_USB_XFER_TIMEOUT 5000
76 #define RT2870_TX_WATCHDOG_TIMEOUT 5
79 * Data structures and types
82 struct rt2870_cmd_argv_newstate
84 enum ieee80211_state nstate;
85 int arg;
88 struct rt2870_cmd_argv_newassoc
90 uint16_t associd;
91 int isnew;
92 uint8_t macaddr[IEEE80211_ADDR_LEN];
95 struct rt2870_cmd_argv_updatebeacon
97 int what;
100 struct rt2870_cmd_argv_keyset
102 struct ieee80211_key key;
103 uint16_t associd;
106 struct rt2870_cmd_argv_keydelete
108 struct ieee80211_key key;
109 uint16_t associd;
113 * Static function prototypes
116 static int rt2870_find_usb_endpoints(struct rt2870_softc *sc);
118 static void rt2870_init_channels(struct rt2870_softc *sc);
120 static void rt2870_init_channels_ht40(struct rt2870_softc *sc);
122 static void rt2870_init_locked(void *priv);
124 static void rt2870_init(void *priv);
126 static int rt2870_init_bbp(struct rt2870_softc *sc);
128 static void rt2870_stop_locked(void *priv);
130 static void rt2870_stop(void *priv);
132 static void rt2870_start(struct ifnet *ifp);
134 static int rt2870_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
136 static int rt2870_reset(struct ifnet *ifp);
138 static int rt2870_newstate(struct ieee80211com *ic,
139 enum ieee80211_state nstate, int arg);
141 static void rt2870_scan_start(struct ieee80211com *ic);
143 static void rt2870_scan_end(struct ieee80211com *ic);
145 static void rt2870_set_channel(struct ieee80211com *ic);
147 static void rt2870_newassoc(struct ieee80211_node *ni, int isnew);
149 static void rt2870_updateslot(struct ifnet *ifp);
151 static int rt2870_wme_update(struct ieee80211com *ic);
153 static void rt2870_update_beacon(struct ieee80211com *ic, int what);
155 static void rt2870_key_update_begin(struct ieee80211com *ic);
157 static void rt2870_key_update_end(struct ieee80211com *ic);
159 static int rt2870_key_set(struct ieee80211com *ic,
160 const struct ieee80211_key *k, const uint8_t mac[IEEE80211_ADDR_LEN]);
162 static int rt2870_key_delete(struct ieee80211com *ic,
163 const struct ieee80211_key *k);
165 static int rt2870_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
166 const struct ieee80211_bpf_params *params);
168 static int rt2870_media_change(struct ifnet *ifp);
170 static struct ieee80211_node *rt2870_node_alloc(struct ieee80211_node_table *nt);
172 static void rt2870_recv_action(struct ieee80211_node *ni,
173 const uint8_t *frm, const uint8_t *efrm);
175 static int rt2870_send_action(struct ieee80211_node *ni,
176 int category, int action, uint16_t args[4]);
178 static void rt2870_amrr_update_iter_func(void *arg, struct ieee80211_node *ni);
180 static void rt2870_periodic(void *arg);
182 static void rt2870_tx_watchdog(void *arg);
184 static int rt2870_do_async(struct rt2870_softc *sc,
185 void (*cb)(struct rt2870_softc *sc, void *priv),
186 void *arg, int len);
188 static void rt2870_newstate_cb(struct rt2870_softc *sc, void *arg);
190 static void rt2870_scan_start_cb(struct rt2870_softc *sc, void *arg);
192 static void rt2870_scan_end_cb(struct rt2870_softc *sc, void *arg);
194 static void rt2870_set_channel_cb(struct rt2870_softc *sc, void *arg);
196 static void rt2870_newassoc_cb(struct rt2870_softc *sc, void *arg);
198 static void rt2870_updateslot_cb(struct rt2870_softc *sc, void *arg);
200 static void rt2870_update_beacon_cb(struct rt2870_softc *sc, void *arg);
202 static void rt2870_wme_update_cb(struct rt2870_softc *sc, void *arg);
204 static void rt2870_key_update_begin_cb(struct rt2870_softc *sc, void *arg);
206 static void rt2870_key_update_end_cb(struct rt2870_softc *sc, void *arg);
208 static void rt2870_key_set_cb(struct rt2870_softc *sc, void *arg);
210 static void rt2870_key_delete_cb(struct rt2870_softc *sc, void *arg);
212 static void rt2870_asic_set_bssid(struct rt2870_softc *sc,
213 const uint8_t *bssid);
215 static void rt2870_asic_set_macaddr(struct rt2870_softc *sc,
216 const uint8_t *addr);
218 static void rt2870_asic_enable_tsf_sync(struct rt2870_softc *sc);
220 static void rt2870_asic_disable_tsf_sync(struct rt2870_softc *sc);
222 static void rt2870_asic_enable_mrr(struct rt2870_softc *sc);
224 static void rt2870_asic_set_txpreamble(struct rt2870_softc *sc);
226 static void rt2870_asic_set_basicrates(struct rt2870_softc *sc);
228 static void rt2870_asic_update_rtsthreshold(struct rt2870_softc *sc);
230 static void rt2870_asic_update_txpower(struct rt2870_softc *sc);
232 static void rt2870_asic_update_promisc(struct rt2870_softc *sc);
234 static void rt2870_asic_updateprot(struct rt2870_softc *sc);
236 static void rt2870_asic_updateslot(struct rt2870_softc *sc);
238 static void rt2870_asic_wme_update(struct rt2870_softc *sc);
240 static int rt2870_asic_update_beacon(struct rt2870_softc *sc);
242 static void rt2870_asic_clear_keytables(struct rt2870_softc *sc);
244 static uint8_t rt2870_rxrate(struct rt2870_rxwi *rxwi);
246 static uint8_t rt2870_maxrssi_rxpath(struct rt2870_softc *sc,
247 const struct rt2870_rxwi *rxwi);
249 static int8_t rt2870_rssi2dbm(struct rt2870_softc *sc,
250 uint8_t rssi, uint8_t rxpath);
252 static uint8_t rt2870_rate2mcs(uint8_t rate);
254 static int rt2870_ackrate(struct ieee80211com *ic, int rate);
256 static uint16_t rt2870_txtime(int len, int rate, uint32_t flags);
258 static void rt2870_rx_frame(struct rt2870_softc *sc,
259 uint8_t *buf, uint32_t dmalen);
261 static int rt2870_tx_frame(struct rt2870_softc *sc,
262 struct mbuf *m, struct ieee80211_node *ni, int qid);
264 static int rt2870_tx_raw(struct rt2870_softc *sc,
265 struct mbuf *m, struct ieee80211_node *ni,
266 const struct ieee80211_bpf_params *params);
268 static void rt2870_rx_intr(usbd_xfer_handle xfer,
269 usbd_private_handle priv, usbd_status status);
271 static void rt2870_tx_intr(usbd_xfer_handle xfer,
272 usbd_private_handle priv, usbd_status status);
274 static void rt2870_rx_done_task(void *context, int pending);
276 static void rt2870_tx_done_task(void *context, int pending);
278 static void rt2870_periodic_task(void *context, int pending);
280 static void rt2870_cmd_task(void *context, int pending);
282 static int rt2870_rx_eof(struct rt2870_softc *sc, int limit);
284 static void rt2870_tx_eof(struct rt2870_softc *sc,
285 struct rt2870_softc_tx_ring *ring);
287 static void rt2870_update_stats(struct rt2870_softc *sc);
289 static void rt2870_bbp_tuning(struct rt2870_softc *sc);
291 static void rt2870_watchdog(struct rt2870_softc *sc);
293 static void rt2870_drain_fifo_stats(struct rt2870_softc *sc);
295 static void rt2870_update_raw_counters(struct rt2870_softc *sc);
297 static int rt2870_alloc_rx_ring(struct rt2870_softc *sc,
298 struct rt2870_softc_rx_ring *ring);
300 static void rt2870_reset_rx_ring(struct rt2870_softc *sc,
301 struct rt2870_softc_rx_ring *ring);
303 static void rt2870_free_rx_ring(struct rt2870_softc *sc,
304 struct rt2870_softc_rx_ring *ring);
306 static int rt2870_alloc_tx_ring(struct rt2870_softc *sc,
307 struct rt2870_softc_tx_ring *ring, int qid);
309 static void rt2870_reset_tx_ring(struct rt2870_softc *sc,
310 struct rt2870_softc_tx_ring *ring);
312 static void rt2870_free_tx_ring(struct rt2870_softc *sc,
313 struct rt2870_softc_tx_ring *ring);
315 static void rt2870_reset_cmd_ring(struct rt2870_softc *sc,
316 struct rt2870_softc_cmd_ring *ring);
318 static void rt2870_sysctl_attach(struct rt2870_softc *sc);
321 * Static variables
324 static const struct usb_devno rt2870_usb_devno[] =
326 { USB_VENDOR_LINKSYS4, USB_PRODUCT_LINKSYS4_WUSB600N }, /* Linksys WUSB600N */
327 { USB_VENDOR_DLINK2, USB_PRODUCT_DLINK2_DWA140 }, /* D-Link DWA-140 */
328 { USB_VENDOR_DLINK2, USB_PRODUCT_DLINK2_DWA160AREVB }, /* D-Link DWA-160A Rev. B */
329 { USB_VENDOR_ASUS, USB_PRODUCT_ASUS_RT2770F }, /* Asus RT2770F */
330 { USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2770 }, /* Ralink RT2770 */
333 static const struct
335 uint32_t reg;
336 uint32_t val;
337 } rt2870_def_mac[] =
339 { RT2870_REG_PBF_BCN_OFFSET0, 0xf8f0e8e0 },
340 { RT2870_REG_PBF_BCN_OFFSET1, 0x6f77d0c8 },
341 { RT2870_REG_LEGACY_BASIC_RATE, 0x0000013f },
342 { RT2870_REG_HT_BASIC_RATE, 0x00008003 },
343 { RT2870_REG_SYS_CTRL, 0x00000000 },
344 { RT2870_REG_RX_FILTER_CFG, 0x00017f97 },
345 { RT2870_REG_BKOFF_SLOT_CFG, 0x00000209 },
346 { RT2870_REG_TX_SW_CFG0, 0x00000000 },
347 { RT2870_REG_TX_SW_CFG1, 0x00080606 },
348 { RT2870_REG_TX_LINK_CFG, 0x00001020 },
349 { RT2870_REG_TX_TIMEOUT_CFG, 0x000a2090 },
350 { RT2870_REG_MAX_LEN_CFG, (1 << 12) | RT2870_MAX_AGG_SIZE },
351 { RT2870_REG_LED_CFG, 0x7f031e46 },
352 { RT2870_REG_PBF_MAX_PCNT, 0x1f3fbf9f },
353 { RT2870_REG_TX_RTY_CFG, 0x47d01f0f },
354 { RT2870_REG_AUTO_RSP_CFG, 0x00000013 },
355 { RT2870_REG_TX_CCK_PROT_CFG, 0x05740003 },
356 { RT2870_REG_TX_OFDM_PROT_CFG, 0x05740003 },
357 { RT2870_REG_PBF_CFG, 0x00f40006 },
358 { RT2870_REG_TX_MM40_PROT_CFG, 0x03f44084 },
359 { RT2870_REG_SCHDMA_WPDMA_GLO_CFG, 0x00000030 },
360 { RT2870_REG_TX_GF20_PROT_CFG, 0x01744004 },
361 { RT2870_REG_TX_GF40_PROT_CFG, 0x03f44084 },
362 { RT2870_REG_TX_MM20_PROT_CFG, 0x01744004 },
363 { RT2870_REG_TX_TXOP_CTRL_CFG, 0x0000583f },
364 { RT2870_REG_TX_RTS_CFG, 0x00092b20 },
365 { RT2870_REG_TX_EXP_ACK_TIME, 0x002400ca },
366 { RT2870_REG_HCCAPSMP_TXOP_HLDR_ET, 0x00000002 },
367 { RT2870_REG_XIFS_TIME_CFG, 0x33a41010 },
368 { RT2870_REG_PWR_PIN_CFG, 0x00000003 },
369 { RT2870_REG_SCHDMA_WMM_AIFSN_CFG, 0x00002273 },
370 { RT2870_REG_SCHDMA_WMM_CWMIN_CFG, 0x00002344 },
371 { RT2870_REG_SCHDMA_WMM_CWMAX_CFG, 0x000034aa },
374 #define RT2870_DEF_MAC_SIZE (sizeof(rt2870_def_mac) / sizeof(rt2870_def_mac[0]))
376 static const struct
378 uint8_t reg;
379 uint8_t val;
380 } rt2870_def_bbp[] =
382 { 65, 0x2c },
383 { 66, 0x38 },
384 { 69, 0x12 },
385 { 70, 0x0a },
386 { 73, 0x10 },
387 { 81, 0x37 },
388 { 82, 0x62 },
389 { 83, 0x6a },
390 { 84, 0x99 },
391 { 86, 0x00 },
392 { 91, 0x04 },
393 { 92, 0x00 },
394 { 103, 0x00 },
395 { 105, 0x05 },
398 #define RT2870_DEF_BBP_SIZE (sizeof(rt2870_def_bbp) / sizeof(rt2870_def_bbp[0]))
400 SYSCTL_NODE(_hw, OID_AUTO, rt2870, CTLFLAG_RD, 0, "RT2870 driver parameters");
402 #ifdef RT2870_DEBUG
403 static int rt2870_debug = 0;
404 SYSCTL_INT(_hw_rt2870, OID_AUTO, debug, CTLFLAG_RW, &rt2870_debug, 0, "rt2870 debug level");
405 TUNABLE_INT("hw.rt2870.debug", &rt2870_debug);
406 #endif
409 * rt2870_probe
411 static int rt2870_probe(device_t dev)
413 struct usb_attach_arg *uaa;
415 uaa = device_get_ivars(dev);
417 if (uaa->iface != NULL)
418 return UMATCH_NONE;
420 return (usb_lookup(rt2870_usb_devno, uaa->vendor, uaa->product) != NULL) ?
421 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
425 * rt2870_attach
427 static int rt2870_attach(device_t dev)
429 struct rt2870_softc *sc;
430 struct usb_attach_arg *uaa;
431 usbd_status usb_error;
432 struct ieee80211com *ic;
433 struct ifnet *ifp;
434 int error, ntries, i;
436 sc = device_get_softc(dev);
437 uaa = device_get_ivars(dev);
439 sc->dev = dev;
440 sc->usb_dev = uaa->device;
442 #ifdef RT2870_DEBUG
443 sc->debug = rt2870_debug;
445 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
446 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
447 "debug", CTLFLAG_RW, &sc->debug, 0, "rt2870 debug level");
448 #endif
450 RT2870_DPRINTF(sc, RT2870_DEBUG_ANY,
451 "%s: attaching\n",
452 device_get_nameunit(sc->dev));
454 if (usbd_set_config_no(sc->usb_dev, RT2870_USB_CONFIG_NO, 0) != 0)
456 printf("%s: could not set USB configuration no\n",
457 device_get_nameunit(sc->dev));
458 return ENXIO;
461 usb_error = usbd_device2interface_handle(sc->usb_dev,
462 RT2870_USB_IFACE_INDEX, &sc->usb_iface);
463 if (usb_error != 0)
465 printf("%s: could not get USB interface handle\n",
466 device_get_nameunit(sc->dev));
467 return ENXIO;
470 error = rt2870_find_usb_endpoints(sc);
471 if (error != 0)
472 return error;
474 for (ntries = 0; ntries < 100; ntries++)
476 sc->mac_rev = rt2870_io_mac_read(sc, RT2870_REG_MAC_CSR0);
477 if (sc->mac_rev != 0x00000000 && sc->mac_rev != 0xffffffff)
478 break;
480 DELAY(10);
483 if (ntries == 100)
485 printf("%s: timeout waiting for NIC to initialize\n",
486 device_get_nameunit(sc->dev));
487 return ENXIO;
490 rt2870_read_eeprom(sc);
492 printf("%s: MAC/BBP RT2870 (rev 0x%08x), RF %s\n",
493 device_get_nameunit(sc->dev), sc->mac_rev,
494 rt2870_rf_name(sc->rf_rev));
496 /* open Tx and Rx USB bulk pipes */
498 for (i = 0; i < sc->usb_endpoints - 1; i++)
500 error = usbd_open_pipe(sc->usb_iface, sc->tx_ring[i].usb_ep,
501 USBD_EXCLUSIVE_USE, &sc->tx_ring[i].usb_pipe);
502 if (error != 0)
504 printf("%s: could not open Tx pipe #%d: %s\n",
505 device_get_nameunit(sc->dev), i, usbd_errstr(error));
506 goto fail;
510 error = usbd_open_pipe(sc->usb_iface, sc->rx_ring.usb_ep,
511 USBD_EXCLUSIVE_USE, &sc->rx_ring.usb_pipe);
512 if (error != 0)
514 printf("%s: could not open Rx pipe: %s\n",
515 device_get_nameunit(sc->dev), usbd_errstr(error));
516 goto fail;
519 /* allocate Tx and Rx rings */
521 for (i = 0; i < sc->usb_endpoints - 1; i++)
523 error = rt2870_alloc_tx_ring(sc, &sc->tx_ring[i], i);
524 if (error != 0)
526 printf("%s: could not allocate Tx ring #%d\n",
527 device_get_nameunit(sc->dev), i);
528 goto fail;
532 if (sc->usb_endpoints == (RT2870_SOFTC_TX_RING_COUNT + 1))
533 sc->tx_ring_mgtqid = 5;
534 else
535 sc->tx_ring_mgtqid = 0;
537 error = rt2870_alloc_rx_ring(sc, &sc->rx_ring);
538 if (error != 0)
540 printf("%s: could not allocate Rx ring\n",
541 device_get_nameunit(sc->dev));
542 goto fail;
545 callout_init(&sc->periodic_ch, 0);
546 callout_init(&sc->tx_watchdog_ch, 0);
548 ifp = sc->ifp = if_alloc(IFT_ETHER);
549 if (ifp == NULL)
551 printf("%s: could not if_alloc()\n",
552 device_get_nameunit(sc->dev));
553 error = ENOMEM;
554 goto fail;
557 ifp->if_softc = sc;
559 if_initname(ifp, "rt2870", device_get_unit(sc->dev));
561 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST |
562 IFF_NEEDSGIANT;
564 ifp->if_init = rt2870_init;
565 ifp->if_ioctl = rt2870_ioctl;
566 ifp->if_start = rt2870_start;
568 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
569 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
570 IFQ_SET_READY(&ifp->if_snd);
572 ic = &sc->ic;
574 ic->ic_ifp = ifp;
576 ic->ic_phytype = IEEE80211_T_HT;
577 ic->ic_opmode = IEEE80211_M_STA;
578 ic->ic_state = IEEE80211_S_INIT;
580 ic->ic_caps = IEEE80211_C_MONITOR |
581 IEEE80211_C_IBSS |
582 IEEE80211_C_AHDEMO |
583 IEEE80211_C_HOSTAP |
584 IEEE80211_C_WDS |
585 IEEE80211_C_BGSCAN |
586 IEEE80211_C_TXPMGT |
587 IEEE80211_C_SHPREAMBLE |
588 IEEE80211_C_SHSLOT |
589 IEEE80211_C_TXFRAG |
590 IEEE80211_C_BURST |
591 IEEE80211_C_WME |
592 IEEE80211_C_WEP |
593 IEEE80211_C_TKIP |
594 IEEE80211_C_AES_CCM |
595 IEEE80211_C_WPA;
597 ic->ic_htcaps = IEEE80211_HTC_HT |
598 IEEE80211_HTC_AMSDU | /* A-MSDU Tx */
599 IEEE80211_HTC_AMPDU | /* A-MPDU Tx */
600 IEEE80211_HTCAP_MAXAMSDU_3839 | /* max. A-MSDU Rx length */
601 IEEE80211_HTCAP_CHWIDTH40 | /* HT 40MHz channel width */
602 IEEE80211_HTCAP_GREENFIELD | /* HT greenfield */
603 IEEE80211_HTCAP_SHORTGI20 | /* HT 20MHz short GI */
604 IEEE80211_HTCAP_SHORTGI40 | /* HT 40MHz short GI */
605 IEEE80211_HTCAP_SMPS_OFF | /* SM power save mode */
606 IEEE80211_HTCAP_MCSFBACK_UNSOL | /* unsolicited MCS feedback */
607 IEEE80211_HTCAP_HTC; /* HTC+ */
609 /* spatial streams */
611 if (sc->nrxpath == 2)
612 ic->ic_htcaps |= IEEE80211_HTCAP_RXSTBC_2STREAM;
613 else if (sc->nrxpath == 3)
614 ic->ic_htcaps |= IEEE80211_HTCAP_RXSTBC_3STREAM;
615 else
616 ic->ic_htcaps |= IEEE80211_HTCAP_RXSTBC_1STREAM;
618 if (sc->ntxpath > 1)
619 ic->ic_htcaps |= IEEE80211_HTCAP_TXSTBC;
621 /* delayed BA */
623 if (sc->mac_rev != 0x28600100)
624 ic->ic_htcaps |= IEEE80211_HTCAP_DELBA;
626 /* init channels */
628 ic->ic_nchans = 0;
629 ic->ic_regdomain = 0;
630 ic->ic_countrycode = CTRY_DEFAULT;
631 ic->ic_location = 0;
633 rt2870_init_channels(sc);
635 rt2870_init_channels_ht40(sc);
637 IEEE80211_ADDR_COPY(ic->ic_myaddr, sc->mac_addr);
639 ieee80211_ifattach(ic);
641 sc->newstate = ic->ic_newstate;
642 ic->ic_newstate = rt2870_newstate;
644 ic->ic_reset = rt2870_reset;
645 ic->ic_node_alloc = rt2870_node_alloc;
646 ic->ic_scan_start = rt2870_scan_start;
647 ic->ic_scan_end = rt2870_scan_end;
648 ic->ic_set_channel = rt2870_set_channel;
649 ic->ic_newassoc = rt2870_newassoc;
650 ic->ic_updateslot = rt2870_updateslot;
651 ic->ic_wme.wme_update = rt2870_wme_update;
652 ic->ic_update_beacon = rt2870_update_beacon;
653 ic->ic_crypto.cs_key_update_begin = rt2870_key_update_begin;
654 ic->ic_crypto.cs_key_update_end = rt2870_key_update_end;
655 ic->ic_crypto.cs_key_set = rt2870_key_set;
656 ic->ic_crypto.cs_key_delete = rt2870_key_delete;
657 ic->ic_raw_xmit = rt2870_raw_xmit;
659 sc->recv_action = ic->ic_recv_action;
660 ic->ic_recv_action = rt2870_recv_action;
662 sc->send_action = ic->ic_send_action;
663 ic->ic_send_action = rt2870_send_action;
665 /* override Rx A-MPDU factor */
667 ic->ic_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_32K;
668 ic->ic_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_NA;
669 ic->ic_ampdu_limit = ic->ic_ampdu_rxmax;
671 /* hardware requires padding between 802.11 frame header and body */
673 ic->ic_flags |= IEEE80211_F_WME | IEEE80211_F_DATAPAD | IEEE80211_F_DOTH;
675 ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS;
677 ieee80211_media_init(ic, rt2870_media_change, ieee80211_media_status);
679 rt2870_amrr_init(&sc->amrr, ic,
680 sc->ntxpath,
681 RT2870_AMRR_MIN_SUCCESS_THRESHOLD,
682 RT2870_AMRR_MAX_SUCCESS_THRESHOLD,
683 500);
685 bpfattach2(ifp, DLT_IEEE802_11_RADIO,
686 sizeof(struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN,
687 &sc->drvbpf);
689 sc->rxtap_len = sizeof(sc->rxtapu);
690 sc->rxtap.ihdr.it_len = htole16(sc->rxtap_len);
691 sc->rxtap.ihdr.it_present = htole32(RT2870_SOFTC_RX_RADIOTAP_PRESENT);
693 sc->txtap_len = sizeof(sc->txtapu);
694 sc->txtap.ihdr.it_len = htole16(sc->txtap_len);
695 sc->txtap.ihdr.it_present = htole32(RT2870_SOFTC_TX_RADIOTAP_PRESENT);
697 /* init task queue */
699 TASK_INIT(&sc->rx_done_task, 0, rt2870_rx_done_task, sc);
700 TASK_INIT(&sc->tx_done_task, 0, rt2870_tx_done_task, sc);
701 TASK_INIT(&sc->periodic_task, 0, rt2870_periodic_task, sc);
702 TASK_INIT(&sc->cmd_task, 0, rt2870_cmd_task, sc);
704 sc->rx_process_limit = 100;
706 sc->taskqueue = taskqueue_create("rt2870_taskq", M_NOWAIT,
707 taskqueue_thread_enqueue, &sc->taskqueue);
709 taskqueue_start_threads(&sc->taskqueue, 1, PI_NET, "%s taskq",
710 device_get_nameunit(sc->dev));
712 rt2870_sysctl_attach(sc);
714 if (bootverbose)
715 ieee80211_announce(ic);
717 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->usb_dev, sc->dev);
719 return 0;
721 fail:
723 /* close Tx and Rx USB bulk pipes */
725 if (sc->rx_ring.usb_pipe != NULL)
727 usbd_abort_pipe(sc->rx_ring.usb_pipe);
728 usbd_close_pipe(sc->rx_ring.usb_pipe);
729 sc->rx_ring.usb_pipe = NULL;
732 for (i = 0; i < sc->usb_endpoints - 1; i++)
734 if (sc->tx_ring[i].usb_pipe != NULL)
736 usbd_abort_pipe(sc->tx_ring[i].usb_pipe);
737 usbd_close_pipe(sc->tx_ring[i].usb_pipe);
738 sc->tx_ring[i].usb_pipe = NULL;
742 /* free Tx and Rx rings */
744 for (i = 0; i < sc->usb_endpoints - 1; i++)
745 rt2870_free_tx_ring(sc, &sc->tx_ring[i]);
747 rt2870_free_rx_ring(sc, &sc->rx_ring);
749 return error;
753 * rt2870_detach
755 static int rt2870_detach(device_t dev)
757 struct rt2870_softc *sc;
758 struct ieee80211com *ic;
759 struct ifnet *ifp;
760 int i;
762 if (!device_is_attached(dev))
763 return 0;
765 sc = device_get_softc(dev);
766 ic = &sc->ic;
767 ifp = ic->ic_ifp;
769 RT2870_DPRINTF(sc, RT2870_DEBUG_ANY,
770 "%s: detaching\n",
771 device_get_nameunit(sc->dev));
773 sc->tx_timer = 0;
775 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
777 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
779 callout_stop(&sc->periodic_ch);
780 callout_stop(&sc->tx_watchdog_ch);
782 taskqueue_drain(sc->taskqueue, &sc->rx_done_task);
783 taskqueue_drain(sc->taskqueue, &sc->tx_done_task);
784 taskqueue_drain(sc->taskqueue, &sc->periodic_task);
785 taskqueue_drain(sc->taskqueue, &sc->cmd_task);
787 /* close Tx and Rx USB bulk pipes */
789 if (sc->rx_ring.usb_pipe != NULL)
791 usbd_abort_pipe(sc->rx_ring.usb_pipe);
792 usbd_close_pipe(sc->rx_ring.usb_pipe);
793 sc->rx_ring.usb_pipe = NULL;
796 for (i = 0; i < sc->usb_endpoints - 1; i++)
798 if (sc->tx_ring[i].usb_pipe != NULL)
800 usbd_abort_pipe(sc->tx_ring[i].usb_pipe);
801 usbd_close_pipe(sc->tx_ring[i].usb_pipe);
802 sc->tx_ring[i].usb_pipe = NULL;
806 /* free Tx and Rx rings */
808 for (i = 0; i < sc->usb_endpoints - 1; i++)
809 rt2870_free_tx_ring(sc, &sc->tx_ring[i]);
811 rt2870_free_rx_ring(sc, &sc->rx_ring);
813 bpfdetach(ifp);
815 ieee80211_ifdetach(ic);
817 if_free(ifp);
819 taskqueue_free(sc->taskqueue);
821 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->usb_dev, sc->dev);
823 return 0;
827 * rt2870_find_usb_endpoints
829 static int rt2870_find_usb_endpoints(struct rt2870_softc *sc)
831 usb_interface_descriptor_t *id;
832 usb_endpoint_descriptor_t *ed;
833 int i, j;
835 id = usbd_get_interface_descriptor(sc->usb_iface);
837 sc->usb_endpoints = id->bNumEndpoints;
838 if ((sc->usb_endpoints != (RT2870_SOFTC_TX_RING_COUNT + 1)) &&
839 (sc->usb_endpoints != (RT2870_SOFTC_TX_RING_COUNT - 2 + 1)))
841 printf("%s: wrong number of USB endpoints=%d\n",
842 device_get_nameunit(sc->dev), sc->usb_endpoints);
843 return ENXIO;
846 sc->rx_ring.usb_ep = -1;
848 for (i = 0; i < RT2870_SOFTC_TX_RING_COUNT; i++)
849 sc->tx_ring[i].usb_ep = -1;
851 for (i = 0, j = 0; i < id->bNumEndpoints; i++)
853 ed = usbd_interface2endpoint_descriptor(sc->usb_iface, i);
854 if (ed == NULL)
856 printf("%s: no endpoint descriptor #%d for USB interface\n",
857 device_get_nameunit(sc->dev), i);
858 return ENXIO;
861 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
862 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
864 sc->rx_ring.usb_ep = ed->bEndpointAddress;
866 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
867 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
869 if (j < RT2870_SOFTC_TX_RING_COUNT)
870 sc->tx_ring[j++].usb_ep = ed->bEndpointAddress;
871 else
872 sc->tx_ring[RT2870_SOFTC_TX_RING_COUNT - 1].usb_ep =
873 ed->bEndpointAddress;
877 if ((sc->rx_ring.usb_ep == -1) ||
878 ((j != RT2870_SOFTC_TX_RING_COUNT) && (j != RT2870_SOFTC_TX_RING_COUNT - 2)))
880 printf("%s: missing USB endpoints\n",
881 device_get_nameunit(sc->dev));
882 return ENXIO;
885 return 0;
889 * rt2870_init_channels
891 static void rt2870_init_channels(struct rt2870_softc *sc)
893 struct ieee80211com *ic;
894 struct ieee80211_channel *c;
895 int i, flags;
897 ic = &sc->ic;
899 /* set supported channels for 2GHz band */
901 for (i = 1; i <= 14; i++)
903 c = &ic->ic_channels[ic->ic_nchans++];
904 flags = IEEE80211_CHAN_B;
906 c->ic_freq = ieee80211_ieee2mhz(i, flags);
907 c->ic_ieee = i;
908 c->ic_flags = flags;
910 c = &ic->ic_channels[ic->ic_nchans++];
911 flags = IEEE80211_CHAN_B | IEEE80211_CHAN_HT20;
913 c->ic_freq = ieee80211_ieee2mhz(i, flags);
914 c->ic_ieee = i;
915 c->ic_flags = flags;
917 c = &ic->ic_channels[ic->ic_nchans++];
918 flags = IEEE80211_CHAN_G;
920 c->ic_freq = ieee80211_ieee2mhz(i, flags);
921 c->ic_ieee = i;
922 c->ic_flags = flags;
924 c = &ic->ic_channels[ic->ic_nchans++];
925 flags = IEEE80211_CHAN_G | IEEE80211_CHAN_HT20;
927 c->ic_freq = ieee80211_ieee2mhz(i, flags);
928 c->ic_ieee = i;
929 c->ic_flags = flags;
932 /* set supported channels for 5GHz band */
934 if (sc->rf_rev == RT2870_EEPROM_RF_2850 ||
935 sc->rf_rev == RT2870_EEPROM_RF_2750)
937 for (i = 36; i <= 64; i += 4)
939 c = &ic->ic_channels[ic->ic_nchans++];
940 flags = IEEE80211_CHAN_A;
942 c->ic_freq = ieee80211_ieee2mhz(i, flags);
943 c->ic_ieee = i;
944 c->ic_flags = flags;
946 c = &ic->ic_channels[ic->ic_nchans++];
947 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20;
949 c->ic_freq = ieee80211_ieee2mhz(i, flags);
950 c->ic_ieee = i;
951 c->ic_flags = flags;
954 for (i = 100; i <= 140; i += 4)
956 c = &ic->ic_channels[ic->ic_nchans++];
957 flags = IEEE80211_CHAN_A;
959 c->ic_freq = ieee80211_ieee2mhz(i, flags);
960 c->ic_ieee = i;
961 c->ic_flags = flags;
963 c = &ic->ic_channels[ic->ic_nchans++];
964 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20;
966 c->ic_freq = ieee80211_ieee2mhz(i, flags);
967 c->ic_ieee = i;
968 c->ic_flags = flags;
971 for (i = 149; i <= 165; i += 4)
973 c = &ic->ic_channels[ic->ic_nchans++];
974 flags = IEEE80211_CHAN_A;
976 c->ic_freq = ieee80211_ieee2mhz(i, flags);
977 c->ic_ieee = i;
978 c->ic_flags = flags;
980 c = &ic->ic_channels[ic->ic_nchans++];
981 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20;
983 c->ic_freq = ieee80211_ieee2mhz(i, flags);
984 c->ic_ieee = i;
985 c->ic_flags = flags;
991 * rt2870_init_channels_ht40
993 static void rt2870_init_channels_ht40(struct rt2870_softc *sc)
995 struct ieee80211com *ic;
996 struct ieee80211_channel *c, *cent, *ext;
997 int i, flags;
999 ic = &sc->ic;
1001 /* set supported channels for 2GHz band */
1003 for (i = 1; i <= 14; i++)
1005 flags = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40;
1007 /* find the center channel */
1009 cent = ieee80211_find_channel_byieee(ic, i,
1010 flags & ~IEEE80211_CHAN_HT);
1011 if (cent == NULL)
1013 printf("%s: skip channel %d, could not find center channel\n",
1014 device_get_nameunit(sc->dev), i);
1015 continue;
1018 /* find the extension channel */
1020 ext = ieee80211_find_channel(ic, cent->ic_freq + 20,
1021 flags & ~IEEE80211_CHAN_HT);
1022 if (ext == NULL)
1024 printf("%s: skip channel %d, could not find extension channel\n",
1025 device_get_nameunit(sc->dev), i);
1026 continue;
1029 c = &ic->ic_channels[ic->ic_nchans++];
1031 *c = *cent;
1032 c->ic_extieee = ext->ic_ieee;
1033 c->ic_flags &= ~IEEE80211_CHAN_HT;
1034 c->ic_flags |= IEEE80211_CHAN_HT40U;
1036 c = &ic->ic_channels[ic->ic_nchans++];
1038 *c = *ext;
1039 c->ic_extieee = cent->ic_ieee;
1040 c->ic_flags &= ~IEEE80211_CHAN_HT;
1041 c->ic_flags |= IEEE80211_CHAN_HT40D;
1044 /* set supported channels for 5GHz band */
1046 if (sc->rf_rev == RT2870_EEPROM_RF_2850 ||
1047 sc->rf_rev == RT2870_EEPROM_RF_2750)
1049 for (i = 36; i <= 64; i += 4)
1051 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40;
1053 /* find the center channel */
1055 cent = ieee80211_find_channel_byieee(ic, i,
1056 flags & ~IEEE80211_CHAN_HT);
1057 if (cent == NULL)
1059 printf("%s: skip channel %d, could not find center channel\n",
1060 device_get_nameunit(sc->dev), i);
1061 continue;
1064 /* find the extension channel */
1066 ext = ieee80211_find_channel(ic, cent->ic_freq + 20,
1067 flags & ~IEEE80211_CHAN_HT);
1068 if (ext == NULL)
1070 printf("%s: skip channel %d, could not find extension channel\n",
1071 device_get_nameunit(sc->dev), i);
1072 continue;
1075 c = &ic->ic_channels[ic->ic_nchans++];
1077 *c = *cent;
1078 c->ic_extieee = ext->ic_ieee;
1079 c->ic_flags &= ~IEEE80211_CHAN_HT;
1080 c->ic_flags |= IEEE80211_CHAN_HT40U;
1082 c = &ic->ic_channels[ic->ic_nchans++];
1084 *c = *ext;
1085 c->ic_extieee = cent->ic_ieee;
1086 c->ic_flags &= ~IEEE80211_CHAN_HT;
1087 c->ic_flags |= IEEE80211_CHAN_HT40D;
1090 for (i = 100; i <= 140; i += 4)
1092 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40;
1094 /* find the center channel */
1096 cent = ieee80211_find_channel_byieee(ic, i,
1097 flags & ~IEEE80211_CHAN_HT);
1098 if (cent == NULL)
1100 printf("%s: skip channel %d, could not find center channel\n",
1101 device_get_nameunit(sc->dev), i);
1102 continue;
1105 /* find the extension channel */
1107 ext = ieee80211_find_channel(ic, cent->ic_freq + 20,
1108 flags & ~IEEE80211_CHAN_HT);
1109 if (ext == NULL)
1111 printf("%s: skip channel %d, could not find extension channel\n",
1112 device_get_nameunit(sc->dev), i);
1113 continue;
1116 c = &ic->ic_channels[ic->ic_nchans++];
1118 *c = *cent;
1119 c->ic_extieee = ext->ic_ieee;
1120 c->ic_flags &= ~IEEE80211_CHAN_HT;
1121 c->ic_flags |= IEEE80211_CHAN_HT40U;
1123 c = &ic->ic_channels[ic->ic_nchans++];
1125 *c = *ext;
1126 c->ic_extieee = cent->ic_ieee;
1127 c->ic_flags &= ~IEEE80211_CHAN_HT;
1128 c->ic_flags |= IEEE80211_CHAN_HT40D;
1131 for (i = 149; i <= 165; i += 4)
1133 flags = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40;
1135 /* find the center channel */
1137 cent = ieee80211_find_channel_byieee(ic, i,
1138 flags & ~IEEE80211_CHAN_HT);
1139 if (cent == NULL)
1141 printf("%s: skip channel %d, could not find center channel\n",
1142 device_get_nameunit(sc->dev), i);
1143 continue;
1146 /* find the extension channel */
1148 ext = ieee80211_find_channel(ic, cent->ic_freq + 20,
1149 flags & ~IEEE80211_CHAN_HT);
1150 if (ext == NULL)
1152 printf("%s: skip channel %d, could not find extension channel\n",
1153 device_get_nameunit(sc->dev), i);
1154 continue;
1157 c = &ic->ic_channels[ic->ic_nchans++];
1159 *c = *cent;
1160 c->ic_extieee = ext->ic_ieee;
1161 c->ic_flags &= ~IEEE80211_CHAN_HT;
1162 c->ic_flags |= IEEE80211_CHAN_HT40U;
1164 c = &ic->ic_channels[ic->ic_nchans++];
1166 *c = *ext;
1167 c->ic_extieee = cent->ic_ieee;
1168 c->ic_flags &= ~IEEE80211_CHAN_HT;
1169 c->ic_flags |= IEEE80211_CHAN_HT40D;
1175 * rt2870_init_locked
1177 static void rt2870_init_locked(void *priv)
1179 struct rt2870_softc *sc;
1180 struct ieee80211com *ic;
1181 struct ifnet *ifp;
1182 struct rt2870_softc_rx_data *data;
1183 int ntries, error, i;
1184 uint32_t tmp, stacnt[6];
1186 sc = priv;
1187 ic = &sc->ic;
1188 ifp = ic->ic_ifp;
1190 RT2870_DPRINTF(sc, RT2870_DEBUG_ANY,
1191 "%s: initializing\n",
1192 device_get_nameunit(sc->dev));
1194 if (!(sc->flags & RT2870_SOFTC_FLAGS_UCODE_LOADED))
1196 RT2870_DPRINTF(sc, RT2870_DEBUG_ANY,
1197 "%s: loading 8051 microcode\n",
1198 device_get_nameunit(sc->dev));
1200 error = rt2870_io_mcu_load_ucode(sc, rt2870_ucode, sizeof(rt2870_ucode));
1201 if (error != 0)
1203 printf("%s: could not load 8051 microcode\n",
1204 device_get_nameunit(sc->dev));
1205 goto fail;
1208 RT2870_DPRINTF(sc, RT2870_DEBUG_ANY,
1209 "%s: 8051 microcode was successfully loaded\n",
1210 device_get_nameunit(sc->dev));
1212 sc->flags |= RT2870_SOFTC_FLAGS_UCODE_LOADED;
1215 /* wait while DMA engine is busy */
1217 for (ntries = 0; ntries < 100; ntries++)
1219 tmp = rt2870_io_mac_read(sc, RT2870_REG_SCHDMA_WPDMA_GLO_CFG);
1220 if (!(tmp & (RT2870_REG_TX_DMA_BUSY | RT2870_REG_RX_DMA_BUSY)))
1221 break;
1223 DELAY(1000);
1226 if (ntries == 100)
1228 printf("%s: timeout waiting for DMA engine\n",
1229 device_get_nameunit(sc->dev));
1230 goto fail;
1233 tmp &= 0xff0;
1234 tmp |= RT2870_REG_TX_WB_DDONE;
1236 rt2870_io_mac_write(sc, RT2870_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
1238 /* PBF hardware reset */
1240 tmp = rt2870_io_mac_read(sc, RT2870_REG_PBF_SYS_CTRL);
1242 tmp &= ~(1 << 13);
1244 rt2870_io_mac_write(sc, RT2870_REG_PBF_SYS_CTRL, tmp);
1246 rt2870_io_mac_write(sc, RT2870_REG_SYS_CTRL,
1247 RT2870_REG_MAC_SRST | RT2870_REG_BBP_HRST);
1249 rt2870_io_mac_write(sc, RT2870_REG_SCHDMA_USB_DMA_CFG, 0);
1251 rt2870_io_mcu_reset(sc);
1253 rt2870_io_mac_write(sc, RT2870_REG_SYS_CTRL, 0);
1255 /* init Tx power per rate */
1257 for (i = 0; i < RT2870_SOFTC_TXPOW_RATE_COUNT; i++)
1259 if (sc->txpow_rate_20mhz[i] == 0xffffffff)
1260 continue;
1262 rt2870_io_mac_write(sc, RT2870_REG_TX_PWR_CFG(i),
1263 sc->txpow_rate_20mhz[i]);
1266 for (i = 0; i < RT2870_DEF_MAC_SIZE; i++)
1267 rt2870_io_mac_write(sc, rt2870_def_mac[i].reg,
1268 rt2870_def_mac[i].val);
1270 /* wait while MAC is busy */
1272 for (ntries = 0; ntries < 100; ntries++)
1274 if (!(rt2870_io_mac_read(sc, RT2870_REG_STATUS_CFG) &
1275 (RT2870_REG_STATUS_TX_BUSY | RT2870_REG_STATUS_RX_BUSY)))
1276 break;
1278 DELAY(1000);
1281 if (ntries == 100)
1283 printf("%s: timeout waiting for MAC\n",
1284 device_get_nameunit(sc->dev));
1285 goto fail;
1288 /* clear Host to MCU mailbox */
1290 rt2870_io_mac_write(sc, RT2870_REG_H2M_MAILBOX_BBP_AGENT, 0);
1291 rt2870_io_mac_write(sc, RT2870_REG_H2M_MAILBOX, 0);
1293 DELAY(1000);
1295 error = rt2870_init_bbp(sc);
1296 if (error != 0)
1297 goto fail;
1299 /* set up maximum buffer sizes */
1301 tmp = (1 << 12) | RT2870_MAX_AGG_SIZE;
1303 if (sc->mac_rev >= 0x28720200 && sc->mac_rev < 0x30700200)
1305 tmp &= 0xfff;
1306 tmp |= 0x2000;
1309 rt2870_io_mac_write(sc, RT2870_REG_MAX_LEN_CFG, tmp);
1311 /* set mac address */
1313 IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
1315 rt2870_asic_set_macaddr(sc, ic->ic_myaddr);
1317 /* clear statistic registers */
1319 rt2870_io_mac_read_multi(sc, RT2870_REG_RX_STA_CNT0,
1320 stacnt, sizeof(stacnt));
1322 /* send LEDs operating mode to microcontroller */
1324 rt2870_io_mcu_cmd(sc, RT2870_IO_MCU_CMD_LED1,
1325 RT2870_REG_H2M_TOKEN_NO_INTR, sc->led_off[0]);
1326 rt2870_io_mcu_cmd(sc, RT2870_IO_MCU_CMD_LED2,
1327 RT2870_REG_H2M_TOKEN_NO_INTR, sc->led_off[1]);
1328 rt2870_io_mcu_cmd(sc, RT2870_IO_MCU_CMD_LED3,
1329 RT2870_REG_H2M_TOKEN_NO_INTR, sc->led_off[2]);
1331 /* write vendor-specific BBP values (from EEPROM) */
1333 for (i = 0; i < RT2870_SOFTC_BBP_EEPROM_COUNT; i++)
1335 if (sc->bbp_eeprom[i].reg == 0x00 ||
1336 sc->bbp_eeprom[i].reg == 0xff)
1337 continue;
1339 rt2870_io_bbp_write(sc, sc->bbp_eeprom[i].reg,
1340 sc->bbp_eeprom[i].val);
1343 /* disable non-existing Rx chains */
1345 tmp = rt2870_io_bbp_read(sc, 3);
1347 tmp &= ~((1 << 4) | (1 << 3));
1349 if (sc->nrxpath == 3)
1350 tmp |= (1 << 4);
1351 else if (sc->nrxpath == 2)
1352 tmp |= (1 << 3);
1354 rt2870_io_bbp_write(sc, 3, tmp);
1356 /* disable non-existing Tx chains */
1358 tmp = rt2870_io_bbp_read(sc, 1);
1360 tmp &= ~((1 << 4) | (1 << 3));
1362 if (sc->ntxpath == 2)
1363 tmp |= (1 << 4);
1365 rt2870_io_bbp_write(sc, 1, tmp);
1367 /* set current channel */
1369 rt2870_rf_set_chan(sc, ic->ic_curchan);
1371 /* turn radio LED on */
1373 rt2870_led_cmd(sc, RT2870_LED_CMD_RADIO_ON);
1375 rt2870_io_mcu_cmd(sc, RT2870_IO_MCU_CMD_BOOT,
1376 RT2870_REG_H2M_TOKEN_NO_INTR, 0);
1378 /* set RTS threshold */
1380 rt2870_asic_update_rtsthreshold(sc);
1382 /* set Tx power */
1384 rt2870_asic_update_txpower(sc);
1386 /* set up protection mode */
1388 rt2870_asic_updateprot(sc);
1390 /* clear key tables */
1392 rt2870_asic_clear_keytables(sc);
1394 /* clear beacon frame space (entries = 8, entry size = 512) */
1396 rt2870_io_mac_set_region_4(sc, RT2870_REG_BEACON_BASE(0), 0, 1024);
1398 /* enable Tx/Rx DMA engine */
1400 tmp = rt2870_io_mac_read(sc, RT2870_REG_SCHDMA_USB_CYC_CFG);
1402 tmp &= 0xffffff00;
1403 tmp |= 0x1e;
1405 rt2870_io_mac_write(sc, RT2870_REG_SCHDMA_USB_CYC_CFG, tmp);
1407 if ((sc->mac_rev & 0xffff) != 0x0101)
1408 rt2870_io_mac_write(sc, RT2870_REG_TX_TXOP_CTRL_CFG, 0x583f);
1410 rt2870_io_mac_write(sc, RT2870_REG_SYS_CTRL, RT2870_REG_TX_ENABLE);
1412 for (ntries = 0; ntries < 200; ntries++)
1414 tmp = rt2870_io_mac_read(sc, RT2870_REG_SCHDMA_WPDMA_GLO_CFG);
1415 if (!(tmp & (RT2870_REG_TX_DMA_BUSY | RT2870_REG_RX_DMA_BUSY)))
1416 break;
1418 DELAY(1000);
1421 if (ntries == 200)
1423 printf("%s: timeout waiting for DMA engine\n",
1424 device_get_nameunit(sc->dev));
1425 goto fail;
1428 DELAY(50);
1430 rt2870_io_mac_write(sc, RT2870_REG_SCHDMA_WMM_TXOP0_CFG, 0);
1431 rt2870_io_mac_write(sc, RT2870_REG_SCHDMA_WMM_TXOP1_CFG,
1432 (48 << 16) | 96);
1434 tmp |= RT2870_REG_TX_WB_DDONE |
1435 RT2870_REG_RX_DMA_ENABLE |
1436 RT2870_REG_TX_DMA_ENABLE;
1438 rt2870_io_mac_write(sc, RT2870_REG_SCHDMA_WPDMA_GLO_CFG, tmp);
1440 tmp = RT2870_REG_USB_DMA_TX_ENABLE |
1441 RT2870_REG_USB_DMA_RX_ENABLE |
1442 RT2870_REG_USB_DMA_RX_AGG_ENABLE |
1443 /* Rx agg limit in unit of 1024 byte */
1444 ((RT2870_USB_RX_BULK_BUFLEN / 1024 - 3) << RT2870_REG_USB_DMA_RX_AGG_LIMIT_SHIFT) |
1445 /* Rx agg timeout in unit of 33ns */
1446 0x80;
1448 rt2870_io_mac_write(sc, RT2870_REG_SCHDMA_USB_DMA_CFG, tmp);
1450 /* set Rx filter */
1452 tmp = RT2870_REG_RX_FILTER_DROP_CRC_ERR |
1453 RT2870_REG_RX_FILTER_DROP_PHY_ERR;
1455 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1457 tmp |= RT2870_REG_RX_FILTER_DROP_DUPL |
1458 RT2870_REG_RX_FILTER_DROP_CTS |
1459 RT2870_REG_RX_FILTER_DROP_BA |
1460 RT2870_REG_RX_FILTER_DROP_ACK |
1461 RT2870_REG_RX_FILTER_DROP_VER_ERR |
1462 RT2870_REG_RX_FILTER_DROP_CTRL_RSV |
1463 RT2870_REG_RX_FILTER_DROP_CFACK |
1464 RT2870_REG_RX_FILTER_DROP_CFEND;
1466 if (ic->ic_opmode == IEEE80211_M_STA)
1467 tmp |= RT2870_REG_RX_FILTER_DROP_RTS |
1468 RT2870_REG_RX_FILTER_DROP_PSPOLL;
1470 if (!(ifp->if_flags & IFF_PROMISC))
1471 tmp |= RT2870_REG_RX_FILTER_DROP_UC_NOME;
1474 rt2870_io_mac_write(sc, RT2870_REG_RX_FILTER_CFG, tmp);
1476 rt2870_io_mac_write(sc, RT2870_REG_SYS_CTRL,
1477 RT2870_REG_RX_ENABLE | RT2870_REG_TX_ENABLE);
1479 /* clear garbage interrupts */
1481 tmp = rt2870_io_mac_read(sc, 0x1300);
1483 taskqueue_unblock(sc->taskqueue);
1485 /* init Tx and Rx rings */
1487 for(i = 0; i < sc->usb_endpoints - 1; i++)
1488 rt2870_reset_tx_ring(sc, &sc->tx_ring[i]);
1490 rt2870_reset_rx_ring(sc, &sc->rx_ring);
1492 rt2870_reset_cmd_ring(sc, &sc->cmd_ring);
1494 /* start up the receive pipe */
1496 for (i = 0; i < RT2870_SOFTC_RX_RING_DATA_COUNT; i++)
1498 data = &sc->rx_ring.data[i];
1500 STAILQ_INSERT_TAIL(&sc->rx_ring.active, data, next);
1502 usbd_setup_xfer(data->xfer, sc->rx_ring.usb_pipe, sc, data->buf,
1503 RT2870_USB_RX_BULK_BUFLEN, USBD_SHORT_XFER_OK | USBD_NO_COPY,
1504 USBD_NO_TIMEOUT, rt2870_rx_intr);
1506 usbd_transfer(data->xfer);
1509 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1510 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1512 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1514 if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
1515 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
1517 else
1519 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1522 sc->periodic_round = 0;
1524 callout_reset(&sc->periodic_ch, hz / 10, rt2870_periodic, sc);
1526 return;
1528 fail:
1530 rt2870_stop_locked(sc);
1534 * rt2870_init
1536 static void rt2870_init(void *priv)
1538 struct rt2870_softc *sc;
1540 sc = priv;
1542 rt2870_init_locked(sc);
1546 * rt2870_init_bbp
1548 static int rt2870_init_bbp(struct rt2870_softc *sc)
1550 int ntries, i;
1551 uint8_t tmp;
1553 for (ntries = 0; ntries < 20; ntries++)
1555 tmp = rt2870_io_bbp_read(sc, 0);
1556 if (tmp != 0x00 && tmp != 0xff)
1557 break;
1560 if (tmp == 0x00 || tmp == 0xff)
1562 printf("%s: timeout waiting for BBP to wakeup\n",
1563 device_get_nameunit(sc->dev));
1564 return ETIMEDOUT;
1567 for (i = 0; i < RT2870_DEF_BBP_SIZE; i++)
1568 rt2870_io_bbp_write(sc, rt2870_def_bbp[i].reg,
1569 rt2870_def_bbp[i].val);
1571 if ((sc->mac_rev & 0xffff) != 0x0101)
1572 rt2870_io_bbp_write(sc, 84, 0x19);
1574 return 0;
1578 * rt2870_stop
1580 static void rt2870_stop_locked(void *priv)
1582 struct rt2870_softc *sc;
1583 struct ieee80211com *ic;
1584 struct ifnet *ifp;
1585 uint32_t tmp;
1586 int i;
1588 sc = priv;
1589 ic = &sc->ic;
1590 ifp = ic->ic_ifp;
1592 RT2870_DPRINTF(sc, RT2870_DEBUG_ANY,
1593 "%s: stopping\n",
1594 device_get_nameunit(sc->dev));
1596 sc->tx_timer = 0;
1598 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1599 rt2870_led_cmd(sc, RT2870_LED_CMD_RADIO_OFF);
1601 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1603 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
1605 callout_stop(&sc->periodic_ch);
1606 callout_stop(&sc->tx_watchdog_ch);
1608 taskqueue_block(sc->taskqueue);
1610 taskqueue_drain(sc->taskqueue, &sc->rx_done_task);
1611 taskqueue_drain(sc->taskqueue, &sc->tx_done_task);
1612 taskqueue_drain(sc->taskqueue, &sc->periodic_task);
1613 taskqueue_drain(sc->taskqueue, &sc->cmd_task);
1615 /* clear key tables */
1617 rt2870_asic_clear_keytables(sc);
1619 /* disable Tx/Rx */
1621 tmp = rt2870_io_mac_read(sc, RT2870_REG_SYS_CTRL);
1623 tmp &= ~(RT2870_REG_RX_ENABLE | RT2870_REG_TX_ENABLE);
1625 rt2870_io_mac_write(sc, RT2870_REG_SYS_CTRL, tmp);
1627 rt2870_io_mac_write(sc, RT2870_REG_SCHDMA_USB_DMA_CFG, 0);
1629 /* reset adapter */
1631 rt2870_io_mac_write(sc, RT2870_REG_SYS_CTRL,
1632 RT2870_REG_MAC_SRST | RT2870_REG_BBP_HRST);
1633 rt2870_io_mac_write(sc, RT2870_REG_SYS_CTRL, 0);
1635 /* abort any pending transfers */
1637 if (sc->rx_ring.usb_pipe != NULL)
1638 usbd_abort_pipe(sc->rx_ring.usb_pipe);
1640 for (i = 0; i < sc->usb_endpoints - 1; i++)
1641 if (sc->tx_ring[i].usb_pipe != NULL)
1642 usbd_abort_pipe(sc->tx_ring[i].usb_pipe);
1646 * rt2870_stop
1648 static void rt2870_stop(void *priv)
1650 struct rt2870_softc *sc;
1652 sc = priv;
1654 rt2870_stop_locked(sc);
1658 * rt2870_start
1660 static void rt2870_start(struct ifnet *ifp)
1662 struct rt2870_softc *sc;
1663 struct ieee80211com *ic;
1664 struct ieee80211_node *ni;
1665 struct ether_header *eh;
1666 struct mbuf *m;
1667 int qid;
1669 sc = ifp->if_softc;
1670 ic = &sc->ic;
1672 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1673 return;
1675 for (;;)
1677 IF_POLL(&ic->ic_mgtq, m);
1678 if (m != NULL)
1680 if (sc->tx_ring[sc->tx_ring_mgtqid].queued >= RT2870_SOFTC_TX_RING_DATA_COUNT)
1682 RT2870_DPRINTF(sc, RT2870_DEBUG_TX,
1683 "%s: if_start: Tx ring with qid=%d is full\n",
1684 device_get_nameunit(sc->dev), sc->tx_ring_mgtqid);
1686 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1688 sc->tx_data_queue_full[sc->tx_ring_mgtqid]++;
1690 break;
1693 IF_DEQUEUE(&ic->ic_mgtq, m);
1695 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1696 m->m_pkthdr.rcvif = NULL;
1698 if (bpf_peers_present(ic->ic_rawbpf))
1699 bpf_mtap(ic->ic_rawbpf, m);
1701 if (rt2870_tx_frame(sc, m, ni, sc->tx_ring_mgtqid) != 0)
1702 break;
1704 else
1706 if (ic->ic_state != IEEE80211_S_RUN)
1707 break;
1709 IF_POLL(&ifp->if_snd, m);
1710 if (m == NULL)
1711 break;
1713 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1715 if (ic->ic_flags & IEEE80211_F_SCAN)
1716 ieee80211_cancel_scan(ic);
1718 if (m->m_len < sizeof(struct ether_header) &&
1719 !(m = m_pullup(m, sizeof (struct ether_header))))
1720 continue;
1722 eh = mtod(m, struct ether_header *);
1724 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1725 if (ni == NULL)
1727 RT2870_DPRINTF(sc, RT2870_DEBUG_TX,
1728 "%s: if_start: could not find Tx node\n",
1729 device_get_nameunit(sc->dev));
1731 m_freem(m);
1733 continue;
1736 ieee80211_classify(ic, m, ni);
1738 qid = M_WME_GETAC(m);
1740 if (sc->tx_ring[qid].queued >= RT2870_SOFTC_TX_RING_DATA_COUNT)
1742 RT2870_DPRINTF(sc, RT2870_DEBUG_TX,
1743 "%s: if_start: Tx ring with qid=%d is full\n",
1744 device_get_nameunit(sc->dev), qid);
1746 m_freem(m);
1747 ieee80211_free_node(ni);
1749 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1750 ifp->if_oerrors++;
1752 sc->tx_data_queue_full[qid]++;
1754 break;
1757 BPF_MTAP(ifp, m);
1759 m = ieee80211_encap(ic, m, ni);
1760 if (m == NULL)
1762 ieee80211_free_node(ni);
1764 ifp->if_oerrors++;
1766 continue;
1769 if (bpf_peers_present(ic->ic_rawbpf))
1770 bpf_mtap(ic->ic_rawbpf, m);
1772 if (rt2870_tx_frame(sc, m, ni, qid) != 0)
1774 ieee80211_free_node(ni);
1776 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1777 ifp->if_oerrors++;
1779 break;
1783 sc->tx_timer = RT2870_TX_WATCHDOG_TIMEOUT;
1785 ic->ic_lastdata = ticks;
1787 callout_reset(&sc->tx_watchdog_ch, hz, rt2870_tx_watchdog, sc);
1792 * rt2870_ioctl
1794 static int rt2870_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1796 struct rt2870_softc *sc;
1797 struct ieee80211com *ic;
1798 int error;
1800 sc = ifp->if_softc;
1801 ic = &sc->ic;
1803 error = 0;
1805 switch (cmd)
1807 case SIOCSIFFLAGS:
1808 if (ifp->if_flags & IFF_UP)
1810 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1812 if ((ifp->if_flags ^ sc->if_flags) & IFF_PROMISC)
1813 rt2870_asic_update_promisc(sc);
1815 else
1817 rt2870_init_locked(sc);
1820 else
1822 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1823 rt2870_stop_locked(sc);
1826 sc->if_flags = ifp->if_flags;
1827 break;
1829 default:
1830 error = ieee80211_ioctl(ic, cmd, data);
1833 if (error == ENETRESET)
1835 if ((ifp->if_flags & IFF_UP) &&
1836 (ifp->if_drv_flags & IFF_DRV_RUNNING) &&
1837 (ic->ic_roaming != IEEE80211_ROAMING_MANUAL))
1839 rt2870_stop_locked(sc);
1840 rt2870_init_locked(sc);
1843 error = 0;
1846 return error;
1850 * rt2870_reset
1852 static int rt2870_reset(struct ifnet *ifp)
1854 struct rt2870_softc *sc;
1855 struct ieee80211com *ic;
1857 sc = ifp->if_softc;
1858 ic = &sc->ic;
1860 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1861 return ENETRESET;
1863 rt2870_rf_set_chan(sc, ic->ic_curchan);
1865 return 0;
1869 * rt2870_newstate
1871 static int rt2870_newstate(struct ieee80211com *ic,
1872 enum ieee80211_state nstate, int arg)
1874 struct rt2870_softc *sc;
1875 struct ifnet *ifp;
1876 struct rt2870_cmd_argv_newstate cmd_argv;
1878 ifp = ic->ic_ifp;
1879 sc = ifp->if_softc;
1881 cmd_argv.nstate = nstate;
1882 cmd_argv.arg = arg;
1884 rt2870_do_async(sc, rt2870_newstate_cb, &cmd_argv,
1885 sizeof(struct rt2870_cmd_argv_newstate));
1887 return 0;
1891 * rt2870_scan_start
1893 static void rt2870_scan_start(struct ieee80211com *ic)
1895 struct rt2870_softc *sc;
1896 struct ifnet *ifp;
1898 ifp = ic->ic_ifp;
1899 sc = ifp->if_softc;
1901 rt2870_do_async(sc, rt2870_scan_start_cb, NULL, 0);
1905 * rt2870_scan_end
1907 static void rt2870_scan_end(struct ieee80211com *ic)
1909 struct rt2870_softc *sc;
1910 struct ifnet *ifp;
1912 ifp = ic->ic_ifp;
1913 sc = ifp->if_softc;
1915 rt2870_do_async(sc, rt2870_scan_end_cb, NULL, 0);
1919 * rt2870_set_channel
1921 static void rt2870_set_channel(struct ieee80211com *ic)
1923 struct rt2870_softc *sc;
1924 struct ifnet *ifp;
1926 ifp = ic->ic_ifp;
1927 sc = ifp->if_softc;
1929 rt2870_do_async(sc, rt2870_set_channel_cb, NULL, 0);
1933 * rt2870_newassoc
1935 static void rt2870_newassoc(struct ieee80211_node *ni, int isnew)
1937 struct rt2870_softc *sc;
1938 struct ieee80211com *ic;
1939 struct ifnet *ifp;
1940 struct rt2870_cmd_argv_newassoc cmd_argv;
1942 ic = ni->ni_ic;
1943 ifp = ic->ic_ifp;
1944 sc = ifp->if_softc;
1946 cmd_argv.associd = (ni != NULL) ? ni->ni_associd : 0;
1947 cmd_argv.isnew = isnew;
1948 memcpy(cmd_argv.macaddr, ni->ni_macaddr, IEEE80211_ADDR_LEN);
1950 rt2870_do_async(sc, rt2870_newassoc_cb, &cmd_argv,
1951 sizeof(struct rt2870_cmd_argv_newassoc));
1955 * rt2870_updateslot
1957 static void rt2870_updateslot(struct ifnet *ifp)
1959 struct rt2870_softc *sc;
1961 sc = ifp->if_softc;
1963 rt2870_do_async(sc, rt2870_updateslot_cb, NULL, 0);
1967 * rt2870_wme_update
1969 static int rt2870_wme_update(struct ieee80211com *ic)
1971 struct rt2870_softc *sc;
1972 struct ifnet *ifp;
1974 ifp = ic->ic_ifp;
1975 sc = ifp->if_softc;
1977 rt2870_do_async(sc, rt2870_wme_update_cb, NULL, 0);
1979 return 0;
1983 * rt2870_update_beacon
1985 static void rt2870_update_beacon(struct ieee80211com *ic, int what)
1987 struct rt2870_softc *sc;
1988 struct ifnet *ifp;
1989 struct rt2870_cmd_argv_updatebeacon cmd_argv;
1991 ifp = ic->ic_ifp;
1992 sc = ifp->if_softc;
1994 cmd_argv.what = what;
1996 rt2870_do_async(sc, rt2870_update_beacon_cb, &cmd_argv,
1997 sizeof(struct rt2870_cmd_argv_updatebeacon));
2001 * rt2870_key_update_begin
2003 static void rt2870_key_update_begin(struct ieee80211com *ic)
2005 struct rt2870_softc *sc;
2006 struct ifnet *ifp;
2008 ifp = ic->ic_ifp;
2009 sc = ifp->if_softc;
2011 rt2870_do_async(sc, rt2870_key_update_begin_cb, NULL, 0);
2015 * rt2870_key_update_end
2017 static void rt2870_key_update_end(struct ieee80211com *ic)
2019 struct rt2870_softc *sc;
2020 struct ifnet *ifp;
2022 ifp = ic->ic_ifp;
2023 sc = ifp->if_softc;
2025 rt2870_do_async(sc, rt2870_key_update_end_cb, NULL, 0);
2029 * rt2870_key_set
2031 static int rt2870_key_set(struct ieee80211com *ic,
2032 const struct ieee80211_key *k, const uint8_t mac[IEEE80211_ADDR_LEN])
2034 struct rt2870_softc *sc;
2035 struct ifnet *ifp;
2036 struct ieee80211_node *ni;
2037 struct rt2870_cmd_argv_keyset cmd_argv;
2039 if (k->wk_cipher->ic_cipher != IEEE80211_CIPHER_WEP &&
2040 k->wk_cipher->ic_cipher != IEEE80211_CIPHER_TKIP &&
2041 k->wk_cipher->ic_cipher != IEEE80211_CIPHER_AES_CCM)
2042 return EINVAL;
2044 ifp = ic->ic_ifp;
2045 sc = ifp->if_softc;
2047 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2048 ni = ic->ic_bss;
2049 else
2050 ni = ieee80211_find_node(&ic->ic_sta, mac);
2052 memcpy(&cmd_argv.key, k, sizeof(struct ieee80211_key));
2053 cmd_argv.associd = (ni != NULL) ? ni->ni_associd : 0;
2055 if ((ic->ic_opmode == IEEE80211_M_HOSTAP) && (ni != NULL))
2056 ieee80211_free_node(ni);
2058 rt2870_do_async(sc, rt2870_key_set_cb, &cmd_argv,
2059 sizeof(struct rt2870_cmd_argv_keyset));
2061 return 1;
2065 * rt2870_key_delete
2067 static int rt2870_key_delete(struct ieee80211com *ic,
2068 const struct ieee80211_key *k)
2070 struct rt2870_softc *sc;
2071 struct ifnet *ifp;
2072 struct ieee80211_node *ni;
2073 struct rt2870_cmd_argv_keydelete cmd_argv;
2075 ifp = ic->ic_ifp;
2076 sc = ifp->if_softc;
2077 ni = ic->ic_bss;
2079 memcpy(&cmd_argv.key, k, sizeof(struct ieee80211_key));
2080 cmd_argv.associd = (ni != NULL) ? ni->ni_associd : 0;
2082 rt2870_do_async(sc, rt2870_key_delete_cb, &cmd_argv,
2083 sizeof(struct rt2870_cmd_argv_keydelete));
2085 return 1;
2089 * rt2870_raw_xmit
2091 static int rt2870_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2092 const struct ieee80211_bpf_params *params)
2094 struct ieee80211com *ic;
2095 struct ifnet *ifp;
2096 struct rt2870_softc *sc;
2098 ic = ni->ni_ic;
2099 ifp = ic->ic_ifp;
2100 sc = ifp->if_softc;
2102 return 0;
2106 * rt2870_media_change
2108 static int rt2870_media_change(struct ifnet *ifp)
2110 struct rt2870_softc *sc;
2111 int error;
2113 sc = ifp->if_softc;
2115 error = ieee80211_media_change(ifp);
2116 if (error != ENETRESET)
2117 return error;
2119 if ((ifp->if_flags & IFF_UP) && (ifp->if_drv_flags & IFF_DRV_RUNNING))
2121 rt2870_stop(sc);
2122 rt2870_init(sc);
2125 return 0;
2129 * rt2870_node_alloc
2131 static struct ieee80211_node *rt2870_node_alloc(struct ieee80211_node_table *nt)
2133 return malloc(sizeof(struct rt2870_softc_node),
2134 M_80211_NODE, M_NOWAIT | M_ZERO);
2138 * rt2870_recv_action
2140 static void rt2870_recv_action(struct ieee80211_node *ni,
2141 const uint8_t *frm, const uint8_t *efrm)
2143 struct rt2870_softc *sc;
2144 struct ieee80211com *ic;
2145 struct ifnet *ifp;
2146 const struct ieee80211_action *ia;
2147 struct ieee80211_tx_ampdu *tx_ampdu;
2148 uint16_t associd, status, baparamset;
2149 uint8_t wcid;
2150 int tid, bufsize, ac;
2151 uint32_t tmp;
2153 ic = ni->ni_ic;
2154 ifp = ic->ic_ifp;
2155 sc = ifp->if_softc;
2157 ia = (const struct ieee80211_action *) frm;
2159 sc->recv_action(ni, frm, efrm);
2161 if (ia->ia_category != IEEE80211_ACTION_CAT_BA)
2162 return;
2164 associd = (ni != NULL) ? ni->ni_associd : 0;
2165 wcid = RT2870_AID2WCID(associd);
2167 switch (ia->ia_action)
2169 /* IEEE80211_ACTION_BA_ADDBA_REQUEST */
2170 case IEEE80211_ACTION_BA_ADDBA_REQUEST:
2171 baparamset = LE_READ_2(frm + 3);
2172 tid = RT2870_MS(baparamset, IEEE80211_BAPS_TID);
2173 bufsize = RT2870_MS(baparamset, IEEE80211_BAPS_BUFSIZ);
2175 RT2870_DPRINTF(sc, RT2870_DEBUG_BA,
2176 "%s: received ADDBA request: associd=0x%04x, tid=%d, bufsize=%d\n",
2177 device_get_nameunit(sc->dev), associd, tid, bufsize);
2179 tmp = rt2870_io_mac_read(sc, RT2870_REG_WCID(wcid) + 4);
2181 tmp |= (0x10000 << tid);
2183 rt2870_io_mac_write(sc, RT2870_REG_WCID(wcid) + 4, tmp);
2184 break;
2186 /* IEEE80211_ACTION_BA_ADDBA_RESPONSE */
2187 case IEEE80211_ACTION_BA_ADDBA_RESPONSE:
2188 status = LE_READ_2(frm + 3);
2189 baparamset = LE_READ_2(frm + 4);
2190 tid = RT2870_MS(baparamset, IEEE80211_BAPS_TID);
2191 bufsize = RT2870_MS(baparamset, IEEE80211_BAPS_BUFSIZ);
2192 ac = TID_TO_WME_AC(tid);
2193 tx_ampdu = &ni->ni_tx_ampdu[ac];
2195 RT2870_DPRINTF(sc, RT2870_DEBUG_BA,
2196 "%s: received ADDBA response: associd=0x%04x, status=%d, tid=%d, bufsize=%d\n",
2197 device_get_nameunit(sc->dev), associd, status, tid, bufsize);
2199 if (sc->mac_rev >= 0x28720200)
2201 if (tx_ampdu->txa_wnd > 13)
2202 tx_ampdu->txa_wnd = 13;
2204 else
2206 if (tx_ampdu->txa_wnd > 7)
2207 tx_ampdu->txa_wnd = 7;
2210 if (status == IEEE80211_STATUS_SUCCESS)
2212 sc->tx_ampdu_sessions++;
2214 rt2870_asic_updateprot(sc);
2216 break;
2218 /* IEEE80211_ACTION_BA_DELBA */
2219 case IEEE80211_ACTION_BA_DELBA:
2220 baparamset = LE_READ_2(frm + 2);
2221 tid = RT2870_MS(baparamset, IEEE80211_BAPS_TID);
2223 RT2870_DPRINTF(sc, RT2870_DEBUG_BA,
2224 "%s: received DELBA request: associd=0x%04x, tid=%d\n",
2225 device_get_nameunit(sc->dev), associd, tid);
2227 tmp = rt2870_io_mac_read(sc, RT2870_REG_WCID(wcid) + 4);
2229 tmp &= ~(0x10000 << tid);
2231 rt2870_io_mac_write(sc, RT2870_REG_WCID(wcid) + 4, tmp);
2232 break;
2237 * rt2870_send_action
2239 static int rt2870_send_action(struct ieee80211_node *ni,
2240 int category, int action, uint16_t args[4])
2242 struct rt2870_softc *sc;
2243 struct ieee80211com *ic;
2244 struct ifnet *ifp;
2245 uint16_t associd, baparamset;
2246 uint8_t wcid;
2247 int ret, tid;
2248 uint32_t tmp;
2250 ic = ni->ni_ic;
2251 ifp = ic->ic_ifp;
2252 sc = ifp->if_softc;
2254 ret = sc->send_action(ni, category, action, args);
2256 if (category != IEEE80211_ACTION_CAT_BA)
2257 return ret;
2259 associd = (ni != NULL) ? ni->ni_associd : 0;
2260 wcid = RT2870_AID2WCID(associd);
2262 switch (action)
2264 /* IEEE80211_ACTION_BA_DELBA */
2265 case IEEE80211_ACTION_BA_DELBA:
2266 baparamset = RT2870_SM(args[0], IEEE80211_DELBAPS_TID) | args[1];
2268 tid = RT2870_MS(baparamset, IEEE80211_DELBAPS_TID);
2270 RT2870_DPRINTF(sc, RT2870_DEBUG_BA,
2271 "%s: sending DELBA request: associd=0x%04x, tid=%d\n",
2272 device_get_nameunit(sc->dev), associd, tid);
2274 if (RT2870_MS(baparamset, IEEE80211_DELBAPS_INIT) != IEEE80211_DELBAPS_INIT)
2276 tmp = rt2870_io_mac_read(sc, RT2870_REG_WCID(wcid) + 4);
2278 tmp &= ~(0x10000 << tid);
2280 rt2870_io_mac_write(sc, RT2870_REG_WCID(wcid) + 4, tmp);
2282 else
2284 sc->tx_ampdu_sessions--;
2286 rt2870_asic_updateprot(sc);
2288 break;
2291 return ret;
2295 * rt2870_amrr_update_iter_func
2297 static void rt2870_amrr_update_iter_func(void *arg, struct ieee80211_node *ni)
2299 struct rt2870_softc *sc;
2300 struct ieee80211com *ic;
2301 uint8_t wcid;
2303 sc = arg;
2304 ic = &sc->ic;
2306 /* only associated stations */
2308 if (ni->ni_associd != 0)
2310 wcid = RT2870_AID2WCID(ni->ni_associd);
2312 RT2870_DPRINTF(sc, RT2870_DEBUG_RATE,
2313 "%s: AMRR node: wcid=0x%02x, txcnt=%d, success=%d, retrycnt=%d\n",
2314 device_get_nameunit(sc->dev),
2315 wcid, sc->amrr_node[wcid].txcnt, sc->amrr_node[wcid].success, sc->amrr_node[wcid].retrycnt);
2317 rt2870_amrr_choose(ni, &sc->amrr_node[wcid]);
2319 RT2870_DPRINTF(sc, RT2870_DEBUG_RATE,
2320 "%s:%s node Tx rate: associd=0x%04x, rate=0x%02x, max rate=0x%02x\n",
2321 device_get_nameunit(sc->dev),
2322 (ni->ni_flags & IEEE80211_NODE_HT) ? " HT" : "",
2323 ni->ni_associd,
2324 (ni->ni_flags & IEEE80211_NODE_HT) ?
2325 (ni->ni_htrates.rs_rates[ni->ni_txrate] | IEEE80211_RATE_MCS) :
2326 (ni->ni_rates.rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL),
2327 (ni->ni_flags & IEEE80211_NODE_HT) ?
2328 (ni->ni_htrates.rs_rates[ni->ni_htrates.rs_nrates - 1] | IEEE80211_RATE_MCS) :
2329 (ni->ni_rates.rs_rates[ni->ni_rates.rs_nrates - 1] & IEEE80211_RATE_VAL));
2334 * rt2870_periodic
2336 static void rt2870_periodic(void *arg)
2338 struct rt2870_softc *sc;
2339 struct ifnet *ifp;
2341 sc = arg;
2342 ifp = sc->ifp;
2344 RT2870_DPRINTF(sc, RT2870_DEBUG_PERIODIC,
2345 "%s: periodic\n",
2346 device_get_nameunit(sc->dev));
2348 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
2349 return;
2351 taskqueue_enqueue(sc->taskqueue, &sc->periodic_task);
2355 * rt2870_tx_watchdog
2357 static void rt2870_tx_watchdog(void *arg)
2359 struct rt2870_softc *sc;
2360 struct ifnet *ifp;
2362 sc = arg;
2363 ifp = sc->ifp;
2365 if (sc->tx_timer == 0)
2366 return;
2368 if (--sc->tx_timer == 0)
2370 printf("%s: Tx watchdog timeout: resetting\n",
2371 device_get_nameunit(sc->dev));
2373 rt2870_stop_locked(sc);
2374 rt2870_init_locked(sc);
2376 ifp->if_oerrors++;
2378 sc->tx_watchdog_timeouts++;
2381 callout_reset(&sc->tx_watchdog_ch, hz, rt2870_tx_watchdog, sc);
2385 * rt2870_do_async
2387 static int rt2870_do_async(struct rt2870_softc *sc,
2388 void (*cb)(struct rt2870_softc *sc, void *arg),
2389 void *arg, int len)
2391 struct ifnet *ifp;
2392 struct rt2870_softc_cmd_ring *ring;
2393 struct rt2870_softc_cmd *cmd;
2394 int run_cmd_task;
2396 ifp = sc->ifp;
2397 ring = &sc->cmd_ring;
2399 RT2870_SOFTC_LOCK(sc);
2401 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
2403 RT2870_SOFTC_UNLOCK(sc);
2404 return -1;
2407 if (ring->queued >= RT2870_SOFTC_CMD_RING_CMD_COUNT)
2409 RT2870_SOFTC_UNLOCK(sc);
2410 return -1;
2413 cmd = STAILQ_FIRST(&ring->inactive);
2414 STAILQ_REMOVE_HEAD(&ring->inactive, next);
2416 cmd->cb = cb;
2418 if(arg != NULL)
2419 memcpy(cmd->data, arg, len);
2421 STAILQ_INSERT_TAIL(&ring->active, cmd, next);
2422 ring->queued++;
2424 run_cmd_task = (ring->queued == 1);
2426 RT2870_SOFTC_UNLOCK(sc);
2428 if (run_cmd_task)
2429 taskqueue_enqueue(sc->taskqueue, &sc->cmd_task);
2431 return 0;
2435 * rt2870_newstate_cb
2437 static void rt2870_newstate_cb(struct rt2870_softc *sc, void *arg)
2439 struct ieee80211com *ic;
2440 enum ieee80211_state nstate;
2441 struct ieee80211_node *ni;
2442 struct rt2870_cmd_argv_newstate *cmd_argv;
2443 int error;
2445 ic = &sc->ic;
2446 cmd_argv = arg;
2447 nstate = cmd_argv->nstate;
2449 RT2870_DPRINTF(sc, RT2870_DEBUG_STATE,
2450 "%s: newstate: %s -> %s\n",
2451 device_get_nameunit(sc->dev),
2452 ieee80211_state_name[ic->ic_state], ieee80211_state_name[nstate]);
2454 error = sc->newstate(ic, nstate, cmd_argv->arg);
2455 if (error != 0)
2456 return;
2458 /* turn link LED off */
2460 if (nstate != IEEE80211_S_RUN)
2461 rt2870_led_cmd(sc, RT2870_LED_CMD_RADIO_OFF);
2463 switch (nstate)
2465 case IEEE80211_S_INIT:
2466 rt2870_asic_disable_tsf_sync(sc);
2467 break;
2469 case IEEE80211_S_RUN:
2470 ni = ic->ic_bss;
2472 if (ic->ic_opmode != IEEE80211_M_MONITOR)
2473 rt2870_rf_set_chan(sc, ni->ni_chan);
2475 if (ic->ic_opmode != IEEE80211_M_MONITOR)
2477 rt2870_asic_enable_mrr(sc);
2478 rt2870_asic_set_txpreamble(sc);
2479 rt2870_asic_set_basicrates(sc);
2480 rt2870_asic_set_bssid(sc, ni->ni_bssid);
2483 if (ic->ic_opmode == IEEE80211_M_STA)
2484 rt2870_newassoc(ni, 1);
2486 if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
2487 ic->ic_opmode == IEEE80211_M_IBSS)
2489 error = rt2870_asic_update_beacon(sc);
2490 if (error != 0)
2491 break;
2494 if (ic->ic_opmode != IEEE80211_M_MONITOR)
2495 rt2870_asic_enable_tsf_sync(sc);
2497 /* turn link LED on */
2499 if (ic->ic_opmode != IEEE80211_M_MONITOR)
2501 rt2870_led_cmd(sc, RT2870_LED_CMD_RADIO_ON |
2502 (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan) ?
2503 RT2870_LED_CMD_LINK_2GHZ : RT2870_LED_CMD_LINK_5GHZ));
2505 break;
2507 default:
2508 break;
2513 * rt2870_scan_start_cb
2515 static void rt2870_scan_start_cb(struct rt2870_softc *sc, void *arg)
2517 struct ieee80211com *ic;
2518 struct ifnet *ifp;
2520 ic = &sc->ic;
2521 ifp = ic->ic_ifp;
2523 rt2870_asic_disable_tsf_sync(sc);
2527 * rt2870_scan_end_cb
2529 static void rt2870_scan_end_cb(struct rt2870_softc *sc, void *arg)
2531 struct ieee80211com *ic;
2532 struct ieee80211_node *ni;
2534 ic = &sc->ic;
2535 ni = ic->ic_bss;
2537 rt2870_asic_enable_tsf_sync(sc);
2541 * rt2870_set_channel_cb
2543 static void rt2870_set_channel_cb(struct rt2870_softc *sc, void *arg)
2545 struct ieee80211com *ic;
2547 ic = &sc->ic;
2549 RT2870_DPRINTF(sc, RT2870_DEBUG_CHAN,
2550 "%s: set channel: channel=%u, HT%s%s\n",
2551 device_get_nameunit(sc->dev),
2552 ieee80211_chan2ieee(ic, ic->ic_curchan),
2553 !IEEE80211_IS_CHAN_HT(ic->ic_curchan) ? " disabled" :
2554 IEEE80211_IS_CHAN_HT20(ic->ic_curchan) ? "20":
2555 IEEE80211_IS_CHAN_HT40U(ic->ic_curchan) ? "40U" : "40D",
2556 (ic->ic_flags & IEEE80211_F_SCAN) ? ", scanning" : "");
2558 rt2870_rf_set_chan(sc, ic->ic_curchan);
2562 * rt2870_newassoc_cb
2564 static void rt2870_newassoc_cb(struct rt2870_softc *sc, void *arg)
2566 struct ieee80211com *ic;
2567 struct ieee80211_node *ni;
2568 struct rt2870_cmd_argv_newassoc *cmd_argv;
2569 uint16_t associd;
2570 int isnew;
2571 uint8_t *macaddr, wcid;
2573 ic = &sc->ic;
2574 ni = ic->ic_bss;
2575 cmd_argv = arg;
2576 associd = cmd_argv->associd;
2577 isnew = cmd_argv->isnew;
2578 macaddr = cmd_argv->macaddr;
2580 wcid = RT2870_AID2WCID(associd);
2582 RT2870_DPRINTF(sc, RT2870_DEBUG_NODE,
2583 "%s: new association: wcid=0x%02x, "
2584 "mac addr=%s, QoS %s, ERP %s, HT %s\n",
2585 device_get_nameunit(sc->dev), wcid,
2586 ether_sprintf(macaddr),
2587 (ni->ni_flags & IEEE80211_NODE_QOS) ? "enabled" : "disabled",
2588 (ni->ni_flags & IEEE80211_NODE_ERP) ? "enabled" : "disabled",
2589 (ni->ni_flags & IEEE80211_NODE_HT) ? "enabled" : "disabled");
2591 rt2870_io_mac_write_multi(sc, RT2870_REG_WCID(wcid),
2592 macaddr, IEEE80211_ADDR_LEN);
2594 rt2870_amrr_node_init(&sc->amrr, &sc->amrr_node[wcid], ni);
2596 RT2870_DPRINTF(sc, RT2870_DEBUG_RATE,
2597 "%s: initial%s node Tx rate: associd=0x%04x, rate=0x%02x, max rate=0x%02x\n",
2598 device_get_nameunit(sc->dev),
2599 (ni->ni_flags & IEEE80211_NODE_HT) ? " HT" : "",
2600 ni->ni_associd,
2601 (ni->ni_flags & IEEE80211_NODE_HT) ?
2602 (ni->ni_htrates.rs_rates[ni->ni_txrate] | IEEE80211_RATE_MCS) :
2603 (ni->ni_rates.rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL),
2604 (ni->ni_flags & IEEE80211_NODE_HT) ?
2605 (ni->ni_htrates.rs_rates[ni->ni_htrates.rs_nrates - 1] | IEEE80211_RATE_MCS) :
2606 (ni->ni_rates.rs_rates[ni->ni_rates.rs_nrates - 1] & IEEE80211_RATE_VAL));
2608 rt2870_asic_updateprot(sc);
2609 rt2870_asic_updateslot(sc);
2610 rt2870_asic_set_txpreamble(sc);
2614 * rt2870_updateslot_cb
2616 static void rt2870_updateslot_cb(struct rt2870_softc *sc, void *arg)
2618 rt2870_asic_updateslot(sc);
2622 * rt2870_wme_update_cb
2624 static void rt2870_wme_update_cb(struct rt2870_softc *sc, void *arg)
2626 rt2870_asic_wme_update(sc);
2630 * rt2870_update_beacon_cb
2632 static void rt2870_update_beacon_cb(struct rt2870_softc *sc, void *arg)
2634 struct rt2870_cmd_argv_updatebeacon *cmd_argv;
2636 cmd_argv = arg;
2638 RT2870_DPRINTF(sc, RT2870_DEBUG_BEACON,
2639 "%s: update beacon\n",
2640 device_get_nameunit(sc->dev));
2642 rt2870_asic_update_beacon(sc);
2646 * rt2870_key_update_begin_cb
2648 static void rt2870_key_update_begin_cb(struct rt2870_softc *sc, void *arg)
2650 struct ifnet *ifp;
2652 ifp = sc->ifp;
2654 RT2870_DPRINTF(sc, RT2870_DEBUG_KEY,
2655 "%s: key update begin\n",
2656 device_get_nameunit(sc->dev));
2658 taskqueue_block(sc->taskqueue);
2660 IF_LOCK(&ifp->if_snd);
2664 * rt2870_key_update_end_cb
2666 static void rt2870_key_update_end_cb(struct rt2870_softc *sc, void *arg)
2668 struct ifnet *ifp;
2670 ifp = sc->ifp;
2672 RT2870_DPRINTF(sc, RT2870_DEBUG_KEY,
2673 "%s: key update end\n",
2674 device_get_nameunit(sc->dev));
2676 IF_UNLOCK(&ifp->if_snd);
2678 taskqueue_unblock(sc->taskqueue);
2682 * rt2870_key_set_cb
2684 static void rt2870_key_set_cb(struct rt2870_softc *sc, void *arg)
2686 struct ieee80211com *ic;
2687 struct ieee80211_key *k;
2688 struct rt2870_cmd_argv_keyset *cmd_argv;
2689 uint16_t associd, key_base, keymode_base;
2690 uint8_t mode, vapid, wcid, iv[8];
2691 uint32_t tmp;
2693 ic = &sc->ic;
2694 cmd_argv = arg;
2695 k = &cmd_argv->key;
2696 associd = cmd_argv->associd;
2698 switch (k->wk_cipher->ic_cipher)
2700 case IEEE80211_CIPHER_WEP:
2701 if(k->wk_keylen < 8)
2702 mode = RT2870_REG_CIPHER_MODE_WEP40;
2703 else
2704 mode = RT2870_REG_CIPHER_MODE_WEP104;
2705 break;
2707 case IEEE80211_CIPHER_TKIP:
2708 mode = RT2870_REG_CIPHER_MODE_TKIP;
2709 break;
2711 case IEEE80211_CIPHER_AES_CCM:
2712 mode = RT2870_REG_CIPHER_MODE_AES_CCMP;
2713 break;
2715 default:
2716 return;
2719 RT2870_DPRINTF(sc, RT2870_DEBUG_KEY,
2720 "%s: set key: keyix=%d, keylen=%d, associd=0x%04x, mode=%d, group=%d\n",
2721 device_get_nameunit(sc->dev), k->wk_keyix, k->wk_keylen, associd, mode,
2722 (k->wk_flags & IEEE80211_KEY_GROUP) ? 1 : 0);
2724 if (!(k->wk_flags & IEEE80211_KEY_GROUP))
2726 /* install pairwise key */
2728 vapid = 0;
2729 wcid = RT2870_AID2WCID(associd);
2730 key_base = RT2870_REG_PKEY(wcid);
2732 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_WEP)
2734 memset(iv, 0, 8);
2736 iv[3] = (k->wk_keyix << 6);
2738 else
2740 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP)
2742 iv[0] = (k->wk_keytsc >> 8);
2743 iv[1] = ((iv[0] | 0x20) & 0x7f);
2744 iv[2] = k->wk_keytsc;
2746 else
2748 /* AES CCMP */
2750 iv[0] = k->wk_keytsc;
2751 iv[1] = k->wk_keytsc >> 8;
2752 iv[2] = 0;
2755 iv[3] = ((k->wk_keyix << 6) | IEEE80211_WEP_EXTIV);
2756 iv[4] = (k->wk_keytsc >> 16);
2757 iv[5] = (k->wk_keytsc >> 24);
2758 iv[6] = (k->wk_keytsc >> 32);
2759 iv[7] = (k->wk_keytsc >> 40);
2761 RT2870_DPRINTF(sc, RT2870_DEBUG_KEY,
2762 "%s: set key: iv=%02x %02x %02x %02x %02x %02x %02x %02x\n",
2763 device_get_nameunit(sc->dev),
2764 iv[0], iv[1], iv[2], iv[3], iv[4], iv[5], iv[6], iv[7]);
2767 rt2870_io_mac_write_multi(sc, RT2870_REG_IVEIV(wcid), iv, 8);
2769 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP)
2771 rt2870_io_mac_write_multi(sc, key_base, k->wk_key, 16);
2773 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2775 rt2870_io_mac_write_multi(sc, key_base + 16, &k->wk_key[16], 8);
2776 rt2870_io_mac_write_multi(sc, key_base + 24, &k->wk_key[24], 8);
2778 else
2780 rt2870_io_mac_write_multi(sc, key_base + 16, &k->wk_key[24], 8);
2781 rt2870_io_mac_write_multi(sc, key_base + 24, &k->wk_key[16], 8);
2784 else
2786 rt2870_io_mac_write_multi(sc, key_base, k->wk_key, k->wk_keylen);
2789 tmp = ((vapid & RT2870_REG_VAP_MASK) << RT2870_REG_VAP_SHIFT) |
2790 (mode << RT2870_REG_CIPHER_MODE_SHIFT) | RT2870_REG_PKEY_ENABLE;
2792 rt2870_io_mac_write(sc, RT2870_REG_WCID_ATTR(wcid), tmp);
2795 if ((k->wk_flags & IEEE80211_KEY_GROUP) ||
2796 (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_WEP))
2798 /* install group key */
2800 vapid = 0;
2801 key_base = RT2870_REG_SKEY(vapid, k->wk_keyix);
2802 keymode_base = RT2870_REG_SKEY_MODE(vapid);
2804 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP)
2806 rt2870_io_mac_write_multi(sc, key_base, k->wk_key, 16);
2808 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2810 rt2870_io_mac_write_multi(sc, key_base + 16, &k->wk_key[16], 8);
2811 rt2870_io_mac_write_multi(sc, key_base + 24, &k->wk_key[24], 8);
2813 else
2815 rt2870_io_mac_write_multi(sc, key_base + 16, &k->wk_key[24], 8);
2816 rt2870_io_mac_write_multi(sc, key_base + 24, &k->wk_key[16], 8);
2819 else
2821 rt2870_io_mac_write_multi(sc, key_base, k->wk_key, k->wk_keylen);
2824 tmp = rt2870_io_mac_read(sc, keymode_base);
2826 tmp &= ~(0xf << (k->wk_keyix * 4 + 16 * (vapid % 2)));
2827 tmp |= (mode << (k->wk_keyix * 4 + 16 * (vapid % 2)));
2829 rt2870_io_mac_write(sc, keymode_base, tmp);
2834 * rt2870_key_delete_cb
2836 static void rt2870_key_delete_cb(struct rt2870_softc *sc, void *arg)
2838 struct ieee80211com *ic;
2839 struct ieee80211_key *k;
2840 struct rt2870_cmd_argv_keydelete *cmd_argv;
2841 uint16_t associd;
2842 uint8_t vapid, wcid;
2843 uint32_t tmp;
2845 ic = &sc->ic;
2846 cmd_argv = arg;
2847 k = &cmd_argv->key;
2848 associd = cmd_argv->associd;
2850 RT2870_DPRINTF(sc, RT2870_DEBUG_KEY,
2851 "%s: delete key: keyix=%d, keylen=%d, associd=0x%04x, group=%d\n",
2852 device_get_nameunit(sc->dev), k->wk_keyix, k->wk_keylen, associd,
2853 (k->wk_flags & IEEE80211_KEY_GROUP) ? 1 : 0);
2855 if (!(k->wk_flags & IEEE80211_KEY_GROUP))
2857 /* remove pairwise key */
2859 vapid = 0;
2860 wcid = RT2870_AID2WCID(associd);
2862 tmp = ((vapid & RT2870_REG_VAP_MASK) << RT2870_REG_VAP_SHIFT) |
2863 (RT2870_REG_CIPHER_MODE_NONE << RT2870_REG_CIPHER_MODE_SHIFT) | RT2870_REG_PKEY_ENABLE;
2865 rt2870_io_mac_write(sc, RT2870_REG_WCID_ATTR(wcid), tmp);
2867 else
2869 /* remove group key */
2871 vapid = 0;
2873 tmp = rt2870_io_mac_read(sc, RT2870_REG_SKEY_MODE(vapid));
2875 tmp &= ~(0xf << (k->wk_keyix * 4 + 16 * (vapid % 2)));
2876 tmp |= (RT2870_REG_CIPHER_MODE_NONE << (k->wk_keyix * 4 + 16 * (vapid % 2)));
2878 rt2870_io_mac_write(sc, RT2870_REG_SKEY_MODE(vapid), tmp);
2883 * rt2870_asic_set_bssid
2885 static void rt2870_asic_set_bssid(struct rt2870_softc *sc,
2886 const uint8_t *bssid)
2888 uint32_t tmp;
2890 RT2870_DPRINTF(sc, RT2870_DEBUG_STATE,
2891 "%s: set bssid: bssid=%s\n",
2892 device_get_nameunit(sc->dev), ether_sprintf(bssid));
2894 tmp = bssid[0] | (bssid[1]) << 8 | (bssid[2] << 16) | (bssid[3] << 24);
2896 rt2870_io_mac_write(sc, RT2870_REG_BSSID_DW0, tmp);
2898 tmp = bssid[4] | (bssid[5] << 8);
2900 rt2870_io_mac_write(sc, RT2870_REG_BSSID_DW1, tmp);
2904 * rt2870_asic_set_macaddr
2906 static void rt2870_asic_set_macaddr(struct rt2870_softc *sc,
2907 const uint8_t *addr)
2909 uint32_t tmp;
2911 tmp = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
2913 rt2870_io_mac_write(sc, RT2870_REG_ADDR_DW0, tmp);
2915 tmp = addr[4] | (addr[5] << 8);
2917 rt2870_io_mac_write(sc, RT2870_REG_ADDR_DW1, tmp);
2921 * rt2870_asic_enable_tsf_sync
2923 static void rt2870_asic_enable_tsf_sync(struct rt2870_softc *sc)
2925 struct ieee80211com *ic;
2926 uint32_t tmp;
2928 ic = &sc->ic;
2930 RT2870_DPRINTF(sc, RT2870_DEBUG_BEACON,
2931 "%s: enabling TSF\n",
2932 device_get_nameunit(sc->dev));
2934 tmp = rt2870_io_mac_read(sc, RT2870_REG_BCN_TIME_CFG);
2936 tmp &= ~0x1fffff;
2937 tmp |= ic->ic_bss->ni_intval * 16;
2938 tmp |= (RT2870_REG_TSF_TIMER_ENABLE | RT2870_REG_TBTT_TIMER_ENABLE);
2940 if (ic->ic_opmode == IEEE80211_M_STA)
2942 tmp |= (RT2870_REG_TSF_SYNC_MODE_STA << RT2870_REG_TSF_SYNC_MODE_SHIFT);
2944 else if (ic->ic_opmode == IEEE80211_M_IBSS)
2946 tmp |= RT2870_REG_BCN_TX_ENABLE;
2947 tmp |= (RT2870_REG_TSF_SYNC_MODE_IBSS << RT2870_REG_TSF_SYNC_MODE_SHIFT);
2949 else if (ic->ic_opmode == IEEE80211_M_HOSTAP)
2951 tmp |= RT2870_REG_BCN_TX_ENABLE;
2952 tmp |= (RT2870_REG_TSF_SYNC_MODE_HOSTAP << RT2870_REG_TSF_SYNC_MODE_SHIFT);
2955 rt2870_io_mac_write(sc, RT2870_REG_BCN_TIME_CFG, tmp);
2959 * rt2870_asic_disable_tsf_sync
2961 static void rt2870_asic_disable_tsf_sync(struct rt2870_softc *sc)
2963 uint32_t tmp;
2965 RT2870_DPRINTF(sc, RT2870_DEBUG_BEACON,
2966 "%s: disabling TSF\n",
2967 device_get_nameunit(sc->dev));
2969 tmp = rt2870_io_mac_read(sc, RT2870_REG_BCN_TIME_CFG);
2971 tmp &= ~(RT2870_REG_BCN_TX_ENABLE |
2972 RT2870_REG_TSF_TIMER_ENABLE |
2973 RT2870_REG_TBTT_TIMER_ENABLE);
2975 tmp &= ~(RT2870_REG_TSF_SYNC_MODE_MASK << RT2870_REG_TSF_SYNC_MODE_SHIFT);
2976 tmp |= (RT2870_REG_TSF_SYNC_MODE_DISABLE << RT2870_REG_TSF_SYNC_MODE_SHIFT);
2978 rt2870_io_mac_write(sc, RT2870_REG_BCN_TIME_CFG, tmp);
2982 * rt2870_asic_enable_mrr
2984 static void rt2870_asic_enable_mrr(struct rt2870_softc *sc)
2986 #define CCK(mcs) (mcs)
2987 #define OFDM(mcs) ((1 << 3) | (mcs))
2988 #define HT(mcs) (mcs)
2990 rt2870_io_mac_write(sc, RT2870_REG_TX_LG_FBK_CFG0,
2991 (OFDM(6) << 28) | /* 54 -> 48 */
2992 (OFDM(5) << 24) | /* 48 -> 36 */
2993 (OFDM(4) << 20) | /* 36 -> 24 */
2994 (OFDM(3) << 16) | /* 24 -> 18 */
2995 (OFDM(2) << 12) | /* 18 -> 12 */
2996 (OFDM(1) << 8) | /* 12 -> 9 */
2997 (OFDM(0) << 4) | /* 9 -> 6 */
2998 OFDM(0)); /* 6 -> 6 */
3000 rt2870_io_mac_write(sc, RT2870_REG_TX_LG_FBK_CFG1,
3001 (CCK(2) << 12) | /* 11 -> 5.5 */
3002 (CCK(1) << 8) | /* 5.5 -> 2 */
3003 (CCK(0) << 4) | /* 2 -> 1 */
3004 CCK(0)); /* 1 -> 1 */
3006 rt2870_io_mac_write(sc, RT2870_REG_TX_HT_FBK_CFG0,
3007 (HT(6) << 28) |
3008 (HT(5) << 24) |
3009 (HT(4) << 20) |
3010 (HT(3) << 16) |
3011 (HT(2) << 12) |
3012 (HT(1) << 8) |
3013 (HT(0) << 4) |
3014 HT(0));
3016 rt2870_io_mac_write(sc, RT2870_REG_TX_HT_FBK_CFG1,
3017 (HT(14) << 28) |
3018 (HT(13) << 24) |
3019 (HT(12) << 20) |
3020 (HT(11) << 16) |
3021 (HT(10) << 12) |
3022 (HT(9) << 8) |
3023 (HT(8) << 4) |
3024 HT(7));
3026 #undef HT
3027 #undef OFDM
3028 #undef CCK
3032 * rt2870_asic_set_txpreamble
3034 static void rt2870_asic_set_txpreamble(struct rt2870_softc *sc)
3036 struct ieee80211com *ic;
3037 uint32_t tmp;
3039 ic = &sc->ic;
3041 RT2870_DPRINTF(sc, RT2870_DEBUG_STATE,
3042 "%s: %s short Tx preamble\n",
3043 device_get_nameunit(sc->dev),
3044 (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ? "enabling" : "disabling");
3046 tmp = rt2870_io_mac_read(sc, RT2870_REG_AUTO_RSP_CFG);
3048 tmp &= ~RT2870_REG_CCK_SHORT_ENABLE;
3050 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
3051 tmp |= RT2870_REG_CCK_SHORT_ENABLE;
3053 rt2870_io_mac_write(sc, RT2870_REG_AUTO_RSP_CFG, tmp);
3057 * rt2870_asic_set_basicrates
3059 static void rt2870_asic_set_basicrates(struct rt2870_softc *sc)
3061 struct ieee80211com *ic;
3063 ic = &sc->ic;
3065 if (ic->ic_curmode == IEEE80211_MODE_11B)
3066 rt2870_io_mac_write(sc, RT2870_REG_LEGACY_BASIC_RATE, 0x3);
3067 else if (ic->ic_curmode == IEEE80211_MODE_11A)
3068 rt2870_io_mac_write(sc, RT2870_REG_LEGACY_BASIC_RATE, 0x150);
3069 else
3070 rt2870_io_mac_write(sc, RT2870_REG_LEGACY_BASIC_RATE, 0x15f);
3074 * rt2870_asic_update_rtsthreshold
3076 static void rt2870_asic_update_rtsthreshold(struct rt2870_softc *sc)
3078 struct ieee80211com *ic;
3079 uint32_t tmp;
3080 uint16_t threshold;
3082 ic = &sc->ic;
3084 RT2870_DPRINTF(sc, RT2870_DEBUG_PROT,
3085 "%s: updating RTS threshold: %d\n",
3086 device_get_nameunit(sc->dev), ic->ic_rtsthreshold);
3088 tmp = rt2870_io_mac_read(sc, RT2870_REG_TX_RTS_CFG);
3090 tmp &= ~(RT2870_REG_TX_RTS_THRESHOLD_MASK << RT2870_REG_TX_RTS_THRESHOLD_SHIFT);
3092 if (ic->ic_flags_ext & IEEE80211_FEXT_AMSDU_TX)
3093 threshold = 0x1000;
3094 else
3095 threshold = ic->ic_rtsthreshold;
3097 tmp |= ((threshold & RT2870_REG_TX_RTS_THRESHOLD_MASK) <<
3098 RT2870_REG_TX_RTS_THRESHOLD_SHIFT);
3100 rt2870_io_mac_write(sc, RT2870_REG_TX_RTS_CFG, tmp);
3104 * rt2870_asic_update_txpower
3106 static void rt2870_asic_update_txpower(struct rt2870_softc *sc)
3108 struct ieee80211com *ic;
3109 uint32_t *txpow_rate;
3110 int8_t delta;
3111 uint8_t val;
3112 uint32_t tmp;
3113 int i;
3115 ic = &sc->ic;
3117 RT2870_DPRINTF(sc, RT2870_DEBUG_STATE,
3118 "%s: updating Tx power: %d\n",
3119 device_get_nameunit(sc->dev), ic->ic_txpowlimit);
3121 if (!IEEE80211_IS_CHAN_HT40(ic->ic_curchan))
3123 txpow_rate = sc->txpow_rate_20mhz;
3125 else
3127 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
3128 txpow_rate = sc->txpow_rate_40mhz_2ghz;
3129 else
3130 txpow_rate = sc->txpow_rate_40mhz_5ghz;
3133 delta = 0;
3135 val = rt2870_io_bbp_read(sc, 1);
3136 val &= 0xfc;
3138 if (ic->ic_txpowlimit > 90)
3140 /* do nothing */
3142 else if (ic->ic_txpowlimit > 60)
3144 delta -= 1;
3146 else if (ic->ic_txpowlimit > 30)
3148 delta -= 3;
3150 else if (ic->ic_txpowlimit > 15)
3152 val |= 0x1;
3154 else if (ic->ic_txpowlimit > 9)
3156 val |= 0x1;
3157 delta -= 3;
3159 else
3161 val |= 0x2;
3164 rt2870_io_bbp_write(sc, 1, val);
3166 for (i = 0; i < RT2870_SOFTC_TXPOW_RATE_COUNT; i++)
3168 if (txpow_rate[i] == 0xffffffff)
3169 continue;
3171 tmp = rt2870_read_eeprom_txpow_rate_add_delta(txpow_rate[i], delta);
3173 rt2870_io_mac_write(sc, RT2870_REG_TX_PWR_CFG(i), tmp);
3178 * rt2870_asic_update_promisc
3180 static void rt2870_asic_update_promisc(struct rt2870_softc *sc)
3182 struct ifnet *ifp;
3183 uint32_t tmp;
3185 ifp = sc->ic.ic_ifp;
3187 printf("%s: %s promiscuous mode\n",
3188 device_get_nameunit(sc->dev),
3189 (ifp->if_flags & IFF_PROMISC) ? "entering" : "leaving");
3191 tmp = rt2870_io_mac_read(sc, RT2870_REG_RX_FILTER_CFG);
3193 tmp &= ~RT2870_REG_RX_FILTER_DROP_UC_NOME;
3195 if (!(ifp->if_flags & IFF_PROMISC))
3196 tmp |= RT2870_REG_RX_FILTER_DROP_UC_NOME;
3198 rt2870_io_mac_write(sc, RT2870_REG_RX_FILTER_CFG, tmp);
3202 * rt2870_asic_updateprot
3204 static void rt2870_asic_updateprot(struct rt2870_softc *sc)
3206 struct ieee80211com *ic;
3207 uint32_t cck_prot, ofdm_prot, mm20_prot, mm40_prot, gf20_prot, gf40_prot;
3208 uint8_t htopmode;
3209 enum ieee80211_protmode htprotmode;
3211 ic = &sc->ic;
3213 /* CCK frame protection */
3215 cck_prot = RT2870_REG_RTSTH_ENABLE | RT2870_REG_PROT_NAV_SHORT |
3216 RT2870_REG_TXOP_ALLOW_ALL | RT2870_REG_PROT_CTRL_NONE;
3218 /* set up protection frame phy mode and rate (MCS code) */
3220 if (ic->ic_curmode == IEEE80211_MODE_11A)
3221 cck_prot |= (RT2870_REG_PROT_PHYMODE_OFDM << RT2870_REG_PROT_PHYMODE_SHIFT) |
3222 (0 << RT2870_REG_PROT_MCS_SHIFT);
3223 else
3224 cck_prot |= ((RT2870_REG_PROT_PHYMODE_CCK << RT2870_REG_PROT_PHYMODE_SHIFT) |
3225 (3 << RT2870_REG_PROT_MCS_SHIFT));
3227 rt2870_io_mac_write(sc, RT2870_REG_TX_CCK_PROT_CFG, cck_prot);
3229 /* OFDM frame protection */
3231 ofdm_prot = RT2870_REG_RTSTH_ENABLE | RT2870_REG_PROT_NAV_SHORT |
3232 RT2870_REG_TXOP_ALLOW_ALL;
3234 if (ic->ic_flags & IEEE80211_F_USEPROT)
3236 RT2870_DPRINTF(sc, RT2870_DEBUG_PROT,
3237 "%s: updating protection mode: b/g protection mode=%s\n",
3238 device_get_nameunit(sc->dev),
3239 (ic->ic_protmode == IEEE80211_PROT_RTSCTS) ? "RTS/CTS" :
3240 ((ic->ic_protmode == IEEE80211_PROT_CTSONLY) ? "CTS-to-self" : "none"));
3242 if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
3243 ofdm_prot |= RT2870_REG_PROT_CTRL_RTS_CTS;
3244 else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
3245 ofdm_prot |= RT2870_REG_PROT_CTRL_CTS;
3246 else
3247 ofdm_prot |= RT2870_REG_PROT_CTRL_NONE;
3249 else
3251 RT2870_DPRINTF(sc, RT2870_DEBUG_PROT,
3252 "%s: updating protection mode: b/g protection mode=%s\n",
3253 device_get_nameunit(sc->dev), "none");
3255 ofdm_prot |= RT2870_REG_PROT_CTRL_NONE;
3258 rt2870_io_mac_write(sc, RT2870_REG_TX_OFDM_PROT_CFG, ofdm_prot);
3260 /* HT frame protection */
3262 if ((ic->ic_opmode == IEEE80211_M_STA) && (ic->ic_state == IEEE80211_S_RUN))
3263 htopmode = ic->ic_bss->ni_htopmode;
3264 else
3265 htopmode = ic->ic_curhtprotmode;
3267 htprotmode = ic->ic_htprotmode;
3269 /* force HT mixed mode and RTS/CTS protection if A-MPDU Tx aggregation is enabled */
3271 if (sc->tx_ampdu_sessions > 0)
3273 RT2870_DPRINTF(sc, RT2870_DEBUG_PROT,
3274 "%s: updating protection mode: forcing HT mixed mode and RTS/CTS protection\n",
3275 device_get_nameunit(sc->dev));
3277 htopmode = IEEE80211_HTINFO_OPMODE_MIXED;
3278 htprotmode = IEEE80211_PROT_RTSCTS;
3281 RT2870_DPRINTF(sc, RT2870_DEBUG_PROT,
3282 "%s: updating protection mode: HT operation mode=0x%02x, protection mode=%s\n",
3283 device_get_nameunit(sc->dev),
3284 htopmode & IEEE80211_HTINFO_OPMODE,
3285 (htprotmode == IEEE80211_PROT_RTSCTS) ? "RTS/CTS" :
3286 ((htprotmode == IEEE80211_PROT_CTSONLY) ? "CTS-to-self" : "none"));
3288 switch (htopmode & IEEE80211_HTINFO_OPMODE)
3290 /* IEEE80211_HTINFO_OPMODE_HT20PR */
3291 case IEEE80211_HTINFO_OPMODE_HT20PR:
3292 mm20_prot = RT2870_REG_PROT_NAV_SHORT | RT2870_REG_PROT_CTRL_NONE |
3293 RT2870_REG_TXOP_ALLOW_CCK | RT2870_REG_TXOP_ALLOW_OFDM |
3294 RT2870_REG_TXOP_ALLOW_MM20 | RT2870_REG_TXOP_ALLOW_GF20 |
3295 (RT2870_REG_PROT_PHYMODE_OFDM << RT2870_REG_PROT_PHYMODE_SHIFT) |
3296 (4 << RT2870_REG_PROT_MCS_SHIFT);
3298 gf20_prot = mm20_prot;
3300 mm40_prot = RT2870_REG_PROT_NAV_SHORT | RT2870_REG_PROT_CTRL_NONE |
3301 RT2870_REG_TXOP_ALLOW_ALL |
3302 (RT2870_REG_PROT_PHYMODE_OFDM << RT2870_REG_PROT_PHYMODE_SHIFT) |
3303 (0x84 << RT2870_REG_PROT_MCS_SHIFT);
3305 if (htprotmode == IEEE80211_PROT_RTSCTS)
3306 mm40_prot |= RT2870_REG_PROT_CTRL_RTS_CTS;
3307 else if (htprotmode == IEEE80211_PROT_CTSONLY)
3308 mm40_prot |= RT2870_REG_PROT_CTRL_CTS;
3309 else
3310 mm40_prot |= RT2870_REG_PROT_CTRL_NONE;
3312 gf40_prot = mm40_prot;
3313 break;
3315 /* IEEE80211_HTINFO_OPMODE_MIXED */
3316 case IEEE80211_HTINFO_OPMODE_MIXED:
3317 mm20_prot = RT2870_REG_PROT_NAV_SHORT |
3318 RT2870_REG_TXOP_ALLOW_CCK | RT2870_REG_TXOP_ALLOW_OFDM |
3319 RT2870_REG_TXOP_ALLOW_MM20 | RT2870_REG_TXOP_ALLOW_GF20;
3321 if (ic->ic_flags & IEEE80211_F_USEPROT)
3322 mm20_prot |= (RT2870_REG_PROT_PHYMODE_CCK << RT2870_REG_PROT_PHYMODE_SHIFT) |
3323 (3 << RT2870_REG_PROT_MCS_SHIFT);
3324 else
3325 mm20_prot |= (RT2870_REG_PROT_PHYMODE_OFDM << RT2870_REG_PROT_PHYMODE_SHIFT) |
3326 (4 << RT2870_REG_PROT_MCS_SHIFT);
3328 if (htprotmode == IEEE80211_PROT_RTSCTS)
3329 mm20_prot |= RT2870_REG_PROT_CTRL_RTS_CTS;
3330 else if (htprotmode == IEEE80211_PROT_CTSONLY)
3331 mm20_prot |= RT2870_REG_PROT_CTRL_CTS;
3332 else
3333 mm20_prot |= RT2870_REG_PROT_CTRL_NONE;
3335 gf20_prot = mm20_prot;
3337 mm40_prot = RT2870_REG_PROT_NAV_SHORT | RT2870_REG_TXOP_ALLOW_ALL;
3339 if (ic->ic_flags & IEEE80211_F_USEPROT)
3340 mm40_prot |= (RT2870_REG_PROT_PHYMODE_CCK << RT2870_REG_PROT_PHYMODE_SHIFT) |
3341 (3 << RT2870_REG_PROT_MCS_SHIFT);
3342 else
3343 mm40_prot |= (RT2870_REG_PROT_PHYMODE_OFDM << RT2870_REG_PROT_PHYMODE_SHIFT) |
3344 (0x84 << RT2870_REG_PROT_MCS_SHIFT);
3346 if (htprotmode == IEEE80211_PROT_RTSCTS)
3347 mm40_prot |= RT2870_REG_PROT_CTRL_RTS_CTS;
3348 else if (htprotmode == IEEE80211_PROT_CTSONLY)
3349 mm40_prot |= RT2870_REG_PROT_CTRL_CTS;
3350 else
3351 mm40_prot |= RT2870_REG_PROT_CTRL_NONE;
3353 gf40_prot = mm40_prot;
3354 break;
3357 * IEEE80211_HTINFO_OPMODE_PURE
3358 * IEEE80211_HTINFO_OPMODE_PROTOPT
3360 case IEEE80211_HTINFO_OPMODE_PURE:
3361 case IEEE80211_HTINFO_OPMODE_PROTOPT:
3362 default:
3363 mm20_prot = RT2870_REG_PROT_NAV_SHORT | RT2870_REG_PROT_CTRL_NONE |
3364 RT2870_REG_TXOP_ALLOW_CCK | RT2870_REG_TXOP_ALLOW_OFDM |
3365 RT2870_REG_TXOP_ALLOW_MM20 | RT2870_REG_TXOP_ALLOW_GF20 |
3366 (RT2870_REG_PROT_PHYMODE_OFDM << RT2870_REG_PROT_PHYMODE_SHIFT) |
3367 (4 << RT2870_REG_PROT_MCS_SHIFT);
3369 gf20_prot = mm20_prot;
3371 mm40_prot = RT2870_REG_PROT_NAV_SHORT | RT2870_REG_PROT_CTRL_NONE |
3372 RT2870_REG_TXOP_ALLOW_ALL |
3373 (RT2870_REG_PROT_PHYMODE_OFDM << RT2870_REG_PROT_PHYMODE_SHIFT) |
3374 (0x84 << RT2870_REG_PROT_MCS_SHIFT);
3376 gf40_prot = mm40_prot;
3377 break;
3380 rt2870_io_mac_write(sc, RT2870_REG_TX_MM20_PROT_CFG, mm20_prot);
3381 rt2870_io_mac_write(sc, RT2870_REG_TX_MM40_PROT_CFG, mm40_prot);
3382 rt2870_io_mac_write(sc, RT2870_REG_TX_GF20_PROT_CFG, gf20_prot);
3383 rt2870_io_mac_write(sc, RT2870_REG_TX_GF40_PROT_CFG, gf40_prot);
3387 * rt2870_asic_updateslot
3389 static void rt2870_asic_updateslot(struct rt2870_softc *sc)
3391 struct ieee80211com *ic;
3392 uint32_t tmp;
3394 ic = &sc->ic;
3396 RT2870_DPRINTF(sc, RT2870_DEBUG_STATE,
3397 "%s: %s short slot time\n",
3398 device_get_nameunit(sc->dev),
3399 (ic->ic_flags & (IEEE80211_F_SHSLOT | IEEE80211_F_BURST)) ? "enabling" : "disabling");
3401 tmp = rt2870_io_mac_read(sc, RT2870_REG_BKOFF_SLOT_CFG);
3403 tmp &= ~0xff;
3404 tmp |= (ic->ic_flags & (IEEE80211_F_SHSLOT | IEEE80211_F_BURST)) ? 9 : 20;
3406 rt2870_io_mac_write(sc, RT2870_REG_BKOFF_SLOT_CFG, tmp);
3410 * rt2870_asic_wme_update
3412 static void rt2870_asic_wme_update(struct rt2870_softc *sc)
3414 struct ieee80211com *ic;
3415 struct ieee80211_wme_state *wme;
3416 const struct wmeParams *wmep;
3417 int i;
3419 ic = &sc->ic;
3420 wme = &ic->ic_wme;
3421 wmep = wme->wme_chanParams.cap_wmeParams;
3423 RT2870_DPRINTF(sc, RT2870_DEBUG_WME,
3424 "%s: wme update: WME_AC_VO=%d/%d/%d/%d, WME_AC_VI=%d/%d/%d/%d, "
3425 "WME_AC_BK=%d/%d/%d/%d, WME_AC_BE=%d/%d/%d/%d\n",
3426 device_get_nameunit(sc->dev),
3427 wmep[WME_AC_VO].wmep_aifsn,
3428 wmep[WME_AC_VO].wmep_logcwmin, wmep[WME_AC_VO].wmep_logcwmax,
3429 wmep[WME_AC_VO].wmep_txopLimit,
3430 wmep[WME_AC_VI].wmep_aifsn,
3431 wmep[WME_AC_VI].wmep_logcwmin, wmep[WME_AC_VI].wmep_logcwmax,
3432 wmep[WME_AC_VI].wmep_txopLimit,
3433 wmep[WME_AC_BK].wmep_aifsn,
3434 wmep[WME_AC_BK].wmep_logcwmin, wmep[WME_AC_BK].wmep_logcwmax,
3435 wmep[WME_AC_BK].wmep_txopLimit,
3436 wmep[WME_AC_BE].wmep_aifsn,
3437 wmep[WME_AC_BE].wmep_logcwmin, wmep[WME_AC_BE].wmep_logcwmax,
3438 wmep[WME_AC_BE].wmep_txopLimit);
3440 for (i = 0; i < WME_NUM_AC; i++)
3441 rt2870_io_mac_write(sc, RT2870_REG_TX_EDCA_AC_CFG(i),
3442 (wmep[i].wmep_logcwmax << 16) | (wmep[i].wmep_logcwmin << 12) |
3443 (wmep[i].wmep_aifsn << 8) | wmep[i].wmep_txopLimit);
3445 rt2870_io_mac_write(sc, RT2870_REG_SCHDMA_WMM_AIFSN_CFG,
3446 (wmep[WME_AC_VO].wmep_aifsn << 12) | (wmep[WME_AC_VI].wmep_aifsn << 8) |
3447 (wmep[WME_AC_BK].wmep_aifsn << 4) | wmep[WME_AC_BE].wmep_aifsn);
3449 rt2870_io_mac_write(sc, RT2870_REG_SCHDMA_WMM_CWMIN_CFG,
3450 (wmep[WME_AC_VO].wmep_logcwmin << 12) | (wmep[WME_AC_VI].wmep_logcwmin << 8) |
3451 (wmep[WME_AC_BK].wmep_logcwmin << 4) | wmep[WME_AC_BE].wmep_logcwmin);
3453 rt2870_io_mac_write(sc, RT2870_REG_SCHDMA_WMM_CWMAX_CFG,
3454 (wmep[WME_AC_VO].wmep_logcwmax << 12) | (wmep[WME_AC_VI].wmep_logcwmax << 8) |
3455 (wmep[WME_AC_BK].wmep_logcwmax << 4) | wmep[WME_AC_BE].wmep_logcwmax);
3457 rt2870_io_mac_write(sc, RT2870_REG_SCHDMA_WMM_TXOP0_CFG,
3458 (wmep[WME_AC_BK].wmep_txopLimit << 16) | wmep[WME_AC_BE].wmep_txopLimit);
3460 rt2870_io_mac_write(sc, RT2870_REG_SCHDMA_WMM_TXOP1_CFG,
3461 (wmep[WME_AC_VO].wmep_txopLimit << 16) | wmep[WME_AC_VI].wmep_txopLimit);
3465 * rt2870_asic_update_beacon
3467 static int rt2870_asic_update_beacon(struct rt2870_softc *sc)
3469 struct ieee80211com *ic;
3470 struct mbuf *m;
3471 struct rt2870_txwi txwi;
3472 uint8_t rate, mcs;
3473 uint32_t tmp;
3475 ic = &sc->ic;
3477 m = ieee80211_beacon_alloc(ic->ic_bss, &sc->beacon_offsets);
3478 if (m == NULL)
3479 return ENOMEM;
3481 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
3482 mcs = rt2870_rate2mcs(rate);
3484 memset(&txwi, 0, sizeof(struct rt2870_txwi));
3486 txwi.wcid = 0xff;
3487 txwi.pid_mpdu_len = ((htole16(m->m_pkthdr.len) & RT2870_TXWI_MPDU_LEN_MASK) <<
3488 RT2870_TXWI_MPDU_LEN_SHIFT);
3489 txwi.txop = (RT2870_TXWI_TXOP_HT << RT2870_TXWI_TXOP_SHIFT);
3490 txwi.mpdu_density_flags |=
3491 (RT2870_TXWI_FLAGS_TS << RT2870_TXWI_FLAGS_SHIFT);
3492 txwi.bawin_size_xflags |=
3493 (RT2870_TXWI_XFLAGS_NSEQ << RT2870_TXWI_XFLAGS_SHIFT);
3495 if (rate == 2)
3497 txwi.phymode_ifs_stbc_shortgi =
3498 (RT2870_TXWI_PHYMODE_CCK << RT2870_TXWI_PHYMODE_SHIFT);
3500 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
3501 mcs |= RT2870_TXWI_MCS_SHOTPRE;
3503 else
3505 txwi.phymode_ifs_stbc_shortgi =
3506 (RT2870_TXWI_PHYMODE_OFDM << RT2870_TXWI_PHYMODE_SHIFT);
3509 txwi.bw_mcs = (RT2870_TXWI_BW_20 << RT2870_TXWI_BW_SHIFT) |
3510 ((mcs & RT2870_TXWI_MCS_MASK) << RT2870_TXWI_MCS_SHIFT);
3512 /* disable temporarily TSF sync */
3514 tmp = rt2870_io_mac_read(sc, RT2870_REG_BCN_TIME_CFG);
3516 tmp &= ~(RT2870_REG_BCN_TX_ENABLE |
3517 RT2870_REG_TSF_TIMER_ENABLE |
3518 RT2870_REG_TBTT_TIMER_ENABLE);
3520 rt2870_io_mac_write(sc, RT2870_REG_BCN_TIME_CFG, tmp);
3522 /* write Tx wireless info and beacon frame to on-chip memory */
3524 rt2870_io_mac_write_multi(sc, RT2870_REG_BEACON_BASE(0),
3525 &txwi, sizeof(struct rt2870_txwi));
3527 rt2870_io_mac_write_multi(sc, RT2870_REG_BEACON_BASE(0) + sizeof(struct rt2870_txwi),
3528 mtod(m, uint8_t *), m->m_pkthdr.len);
3530 /* enable again TSF sync */
3532 tmp = rt2870_io_mac_read(sc, RT2870_REG_BCN_TIME_CFG);
3534 tmp |= (RT2870_REG_BCN_TX_ENABLE |
3535 RT2870_REG_TSF_TIMER_ENABLE |
3536 RT2870_REG_TBTT_TIMER_ENABLE);
3538 rt2870_io_mac_write(sc, RT2870_REG_BCN_TIME_CFG, tmp);
3540 m_freem(m);
3542 return 0;
3546 * rt2870_asic_clear_keytables
3548 static void rt2870_asic_clear_keytables(struct rt2870_softc *sc)
3550 int i;
3552 /* clear Rx WCID search table (entries = 256, entry size = 8) */
3554 for (i = 0; i < 256; i++)
3556 rt2870_io_mac_write(sc, RT2870_REG_WCID(i), 0xffffffff);
3557 rt2870_io_mac_write(sc, RT2870_REG_WCID(i) + 4, 0x0000ffff);
3560 /* clear WCID attribute table (entries = 256, entry size = 4) */
3562 rt2870_io_mac_set_region_4(sc, RT2870_REG_WCID_ATTR(0), 0, 256);
3564 /* clear IV/EIV table (entries = 256, entry size = 8) */
3566 rt2870_io_mac_set_region_4(sc, RT2870_REG_IVEIV(0), 0, 2 * 256);
3568 /* clear pairwise key table (entries = 256, entry size = 32) */
3570 rt2870_io_mac_set_region_4(sc, RT2870_REG_PKEY(0), 0, 8 * 256);
3572 /* clear shared key table (entries = 32, entry size = 32) */
3574 rt2870_io_mac_set_region_4(sc, RT2870_REG_SKEY(0, 0), 0, 8 * 32);
3576 /* clear shared key mode (entries = 32, entry size = 2) */
3578 rt2870_io_mac_set_region_4(sc, RT2870_REG_SKEY_MODE(0), 0, 16);
3582 * rt2870_rxrate
3584 static uint8_t rt2870_rxrate(struct rt2870_rxwi *rxwi)
3586 uint8_t mcs, phymode;
3587 uint8_t rate;
3589 mcs = (rxwi->bw_mcs >> RT2870_RXWI_MCS_SHIFT) & RT2870_RXWI_MCS_MASK;
3590 phymode = (rxwi->phymode_stbc_shortgi >> RT2870_RXWI_PHYMODE_SHIFT) &
3591 RT2870_RXWI_PHYMODE_MASK;
3593 rate = 2;
3595 switch (phymode)
3597 case RT2870_RXWI_PHYMODE_CCK:
3598 switch (mcs & ~RT2870_RXWI_MCS_SHOTPRE)
3600 case 0: rate = 2; break; /* 1 Mbps */
3601 case 1: rate = 4; break; /* 2 MBps */
3602 case 2: rate = 11; break; /* 5.5 Mbps */
3603 case 3: rate = 22; break; /* 11 Mbps */
3605 break;
3607 case RT2870_RXWI_PHYMODE_OFDM:
3608 switch (mcs)
3610 case 0: rate = 12; break; /* 6 Mbps */
3611 case 1: rate = 18; break; /* 9 Mbps */
3612 case 2: rate = 24; break; /* 12 Mbps */
3613 case 3: rate = 36; break; /* 18 Mbps */
3614 case 4: rate = 48; break; /* 24 Mbps */
3615 case 5: rate = 72; break; /* 36 Mbps */
3616 case 6: rate = 96; break; /* 48 Mbps */
3617 case 7: rate = 108; break; /* 54 Mbps */
3619 break;
3621 case RT2870_RXWI_PHYMODE_HT_MIXED:
3622 case RT2870_RXWI_PHYMODE_HT_GF:
3623 break;
3626 return rate;
3630 * rt2870_maxrssi_rxpath
3632 static uint8_t rt2870_maxrssi_rxpath(struct rt2870_softc *sc,
3633 const struct rt2870_rxwi *rxwi)
3635 uint8_t rxpath;
3637 rxpath = 0;
3639 if (sc->nrxpath > 1)
3640 if (rxwi->rssi[1] > rxwi->rssi[rxpath])
3641 rxpath = 1;
3643 if (sc->nrxpath > 2)
3644 if (rxwi->rssi[2] > rxwi->rssi[rxpath])
3645 rxpath = 2;
3647 return rxpath;
3651 * rt2870_rssi2dbm
3653 static int8_t rt2870_rssi2dbm(struct rt2870_softc *sc,
3654 uint8_t rssi, uint8_t rxpath)
3656 struct ieee80211com *ic;
3657 struct ieee80211_channel *c;
3658 int chan;
3659 int8_t rssi_off, lna_gain;
3661 if (rssi == 0)
3662 return -99;
3664 ic = &sc->ic;
3665 c = ic->ic_curchan;
3666 chan = ieee80211_chan2ieee(ic, c);
3668 if (IEEE80211_IS_CHAN_5GHZ(c))
3670 rssi_off = sc->rssi_off_5ghz[rxpath];
3672 if (chan <= 64)
3673 lna_gain = sc->lna_gain[1];
3674 else if (chan <= 128)
3675 lna_gain = sc->lna_gain[2];
3676 else
3677 lna_gain = sc->lna_gain[3];
3679 else
3681 rssi_off = sc->rssi_off_2ghz[rxpath];
3682 lna_gain = sc->lna_gain[0];
3685 return (-12 - rssi_off - lna_gain - rssi);
3689 * rt2870_rate2mcs
3691 static uint8_t rt2870_rate2mcs(uint8_t rate)
3693 switch (rate)
3695 /* CCK rates */
3696 case 2: return 0;
3697 case 4: return 1;
3698 case 11: return 2;
3699 case 22: return 3;
3701 /* OFDM rates */
3702 case 12: return 0;
3703 case 18: return 1;
3704 case 24: return 2;
3705 case 36: return 3;
3706 case 48: return 4;
3707 case 72: return 5;
3708 case 96: return 6;
3709 case 108: return 7;
3712 return 0;
3716 * rt2870_ackrate
3718 static int rt2870_ackrate(struct ieee80211com *ic, int rate)
3720 switch (rate)
3722 /* CCK rates */
3724 case 2:
3725 return 2;
3727 case 4:
3728 case 11:
3729 case 22:
3730 return (ic->ic_curmode == IEEE80211_MODE_11B) ? 4 : rate;
3732 /* OFDM rates */
3734 case 12:
3735 case 18:
3736 return 12;
3738 case 24:
3739 case 36:
3740 return 24;
3742 case 48:
3743 case 72:
3744 case 96:
3745 case 108:
3746 return 48;
3749 /* default to 1Mbps */
3750 return 2;
3754 * rt2870_txtime
3756 static uint16_t rt2870_txtime(int len, int rate, uint32_t flags)
3758 uint16_t txtime;
3760 if (RT2870_RATE_IS_OFDM(rate))
3762 txtime = (8 + 4 * len + 3 + rate - 1) / rate;
3763 txtime = 16 + 4 + 4 * txtime + 6;
3765 else
3767 txtime = (16 * len + rate - 1) / rate;
3769 if (rate != 2 && (flags & IEEE80211_F_SHPREAMBLE))
3770 txtime += 72 + 24;
3771 else
3772 txtime += 144 + 48;
3775 return txtime;
3779 * rt2870_rx_frame
3781 static void rt2870_rx_frame(struct rt2870_softc *sc,
3782 uint8_t *buf, uint32_t dmalen)
3784 struct ieee80211com *ic;
3785 struct ifnet *ifp;
3786 struct ieee80211_frame *wh;
3787 struct ieee80211_node *ni;
3788 struct rt2870_softc_node *rni;
3789 struct rt2870_softc_rx_radiotap_header *tap;
3790 struct rt2870_rxwi *rxwi;
3791 struct rt2870_rxinfo *rxinfo;
3792 struct mbuf *m;
3793 uint32_t rxinfo_flags;
3794 uint8_t cipher_err, rssi, ant, phymode, bw, shortgi, mcs;
3795 int8_t rssi_dbm;
3796 int len, ampdu, amsdu, i;
3798 ic = &sc->ic;
3799 ifp = ic->ic_ifp;
3801 /* get Rx wireless info */
3803 rxwi = (struct rt2870_rxwi *) buf;
3804 len = (le16toh(rxwi->tid_size) >> RT2870_RXWI_SIZE_SHIFT) &
3805 RT2870_RXWI_SIZE_MASK;
3807 if (len > dmalen)
3809 RT2870_DPRINTF(sc, RT2870_DEBUG_RX,
3810 "%s: bad rxwi len: DMA len=%d, rxwi len=%d\n",
3811 device_get_nameunit(sc->dev), dmalen, len);
3812 return;
3815 /* get Rx info */
3817 rxinfo = (struct rt2870_rxinfo *) ((caddr_t) rxwi + dmalen);
3818 rxinfo_flags = le32toh(rxinfo->flags);
3820 RT2870_DPRINTF(sc, RT2870_DEBUG_RX,
3821 "%s: Rx frame: DMA len=%d, len=%d, rxinfo flags=0x%08x\n",
3822 device_get_nameunit(sc->dev), dmalen, len, rxinfo_flags);
3824 /* check for crc errors */
3826 if (rxinfo_flags & RT2870_RXINFO_FLAGS_CRC_ERR)
3828 RT2870_DPRINTF(sc, RT2870_DEBUG_RX,
3829 "%s: rxinfo: crc error\n",
3830 device_get_nameunit(sc->dev));
3832 ifp->if_ierrors++;
3834 if (!(ifp->if_flags & IFF_PROMISC))
3835 return;
3838 wh = (struct ieee80211_frame *) (rxwi + 1);
3840 /* check for L2 padding between IEEE 802.11 frame header and body */
3842 if (rxinfo_flags & RT2870_RXINFO_FLAGS_L2PAD)
3844 RT2870_DPRINTF(sc, RT2870_DEBUG_RX,
3845 "%s: L2 padding: DMA len=%d, len=%d\n",
3846 device_get_nameunit(sc->dev), dmalen, len);
3848 len += 2;
3851 m = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
3852 if (m == NULL)
3854 sc->rx_mbuf_alloc_errors++;
3855 ifp->if_ierrors++;
3856 return;
3859 m->m_pkthdr.rcvif = ifp;
3860 m->m_pkthdr.len = m->m_len = len;
3862 m_copyback(m, 0, len, (caddr_t) wh);
3864 wh = mtod(m, struct ieee80211_frame *);
3866 /* check for cipher errors */
3868 if (rxinfo_flags & RT2870_RXINFO_FLAGS_DECRYPTED)
3870 cipher_err = ((rxinfo_flags >> RT2870_RXINFO_FLAGS_CIPHER_ERR_SHIFT) &
3871 RT2870_RXINFO_FLAGS_CIPHER_ERR_MASK);
3872 if (cipher_err == RT2870_RXINFO_FLAGS_CIPHER_ERR_NONE)
3874 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
3875 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
3877 m->m_flags |= M_WEP;
3879 else
3881 RT2870_DPRINTF(sc, RT2870_DEBUG_RX,
3882 "%s: rxinfo: cipher error=0x%02x\n",
3883 device_get_nameunit(sc->dev), cipher_err);
3885 ifp->if_ierrors++;
3887 if (!(ifp->if_flags & IFF_PROMISC))
3889 m_free(m);
3890 return;
3894 else
3896 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
3898 ifp->if_ierrors++;
3900 if (!(ifp->if_flags & IFF_PROMISC))
3902 m_free(m);
3903 return;
3908 /* check for A-MPDU */
3910 if (rxinfo_flags & RT2870_RXINFO_FLAGS_AMPDU)
3912 m->m_flags |= M_AMPDU;
3914 sc->rx_ampdu++;
3916 ampdu = 1;
3918 else
3920 ampdu = 0;
3923 /* check for A-MSDU */
3925 if (rxinfo_flags & RT2870_RXINFO_FLAGS_AMSDU)
3927 sc->rx_amsdu++;
3929 amsdu = 1;
3931 else
3933 amsdu = 0;
3936 ant = rt2870_maxrssi_rxpath(sc, rxwi);
3937 rssi = rxwi->rssi[ant];
3938 rssi_dbm = rt2870_rssi2dbm(sc, rssi, ant);
3939 phymode = ((rxwi->phymode_stbc_shortgi >> RT2870_RXWI_PHYMODE_SHIFT) &
3940 RT2870_RXWI_PHYMODE_MASK);
3941 bw = ((rxwi->bw_mcs >> RT2870_RXWI_BW_SHIFT) & RT2870_RXWI_BW_MASK);
3942 shortgi = ((rxwi->phymode_stbc_shortgi >> RT2870_RXWI_SHORTGI_SHIFT) &
3943 RT2870_RXWI_SHORTGI_MASK);
3944 mcs = ((rxwi->bw_mcs >> RT2870_RXWI_MCS_SHIFT) & RT2870_RXWI_MCS_MASK);
3946 if (bpf_peers_present(sc->drvbpf))
3948 tap = &sc->rxtap;
3950 tap->flags = IEEE80211_RADIOTAP_F_DATAPAD;
3951 tap->dbm_antsignal = rssi_dbm;
3952 tap->dbm_antnoise = RT2870_NOISE_FLOOR;
3953 tap->antenna = ant;
3954 tap->antsignal = rssi;
3955 tap->chan_flags = htole32(ic->ic_curchan->ic_flags);
3956 tap->chan_freq = htole16(ic->ic_curchan->ic_freq);
3957 tap->chan_ieee = ic->ic_curchan->ic_ieee;
3958 tap->chan_maxpow = 0;
3960 if (phymode == RT2870_RXWI_PHYMODE_CCK || phymode == RT2870_RXWI_PHYMODE_OFDM)
3961 tap->rate = rt2870_rxrate(rxwi);
3962 else
3963 tap->rate = mcs | IEEE80211_RATE_MCS;
3965 if (rxwi->bw_mcs & RT2870_RXWI_MCS_SHOTPRE)
3966 tap->flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
3968 if (shortgi)
3969 tap->flags |= IEEE80211_RADIOTAP_F_SHORTGI;
3971 bpf_mtap2(sc->drvbpf, tap, sc->rxtap_len, m);
3974 RT2870_DPRINTF(sc, RT2870_DEBUG_RX,
3975 "%s: received frame: len=%d, phymode=%d, bw=%d, shortgi=%d, mcs=%d, "
3976 "ant=%d, rssi=%d/%d/%d, snr=%d/%d, wcid=0x%02x, ampdu=%d, amsdu=%d\n",
3977 device_get_nameunit(sc->dev),
3978 len, phymode, bw, shortgi, mcs,
3979 ant, rxwi->rssi[0], rxwi->rssi[1], rxwi->rssi[2],
3980 rxwi->snr[0], rxwi->snr[1],
3981 rxwi->wcid, ampdu, amsdu);
3983 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *) wh);
3985 if (ni != NULL)
3987 rni = (struct rt2870_softc_node *) ni;
3989 for (i = 0; i < RT2870_SOFTC_RSSI_DBM_COUNT; i++)
3990 rni->last_rssi_dbm[i] = rt2870_rssi2dbm(sc, rxwi->rssi[i], i);
3993 ieee80211_input(ic, m, ni, rssi_dbm, RT2870_NOISE_FLOOR, 0);
3995 ieee80211_free_node(ni);
3999 * rt2870_tx_frame
4001 static int rt2870_tx_frame(struct rt2870_softc *sc,
4002 struct mbuf *m, struct ieee80211_node *ni, int qid)
4004 struct ieee80211com *ic;
4005 struct rt2870_softc_tx_ring *ring;
4006 struct rt2870_softc_tx_data *data;
4007 struct rt2870_txinfo *txinfo;
4008 struct rt2870_txwi *txwi;
4009 struct ieee80211_frame *wh;
4010 struct ieee80211_tx_ampdu *tx_ampdu;
4011 struct rt2870_softc_tx_radiotap_header *tap;
4012 u_int hdrsize, hdrspace;
4013 uint8_t type, rate, bw, stbc, shortgi, mcs, pid, wcid, mpdu_density, bawin_size;
4014 uint16_t qos, len, dmalen, mpdu_len, dur;
4015 int hasqos, ac, ampdu, ackrate;
4017 ic = &sc->ic;
4019 ring = &sc->tx_ring[qid];
4020 data = STAILQ_FIRST(&ring->inactive);
4021 STAILQ_REMOVE_HEAD(&ring->inactive, next);
4022 txinfo = (struct rt2870_txinfo *) data->buf;
4023 txwi = (struct rt2870_txwi *) (txinfo + 1);
4025 wh = mtod(m, struct ieee80211_frame *);
4027 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
4029 hasqos = IEEE80211_QOS_HAS_SEQ(wh);
4030 if (hasqos)
4032 if (IEEE80211_HAS_ADDR4(wh))
4033 qos = le16toh(*(const uint16_t *)
4034 (((struct ieee80211_qosframe_addr4 *) wh)->i_qos));
4035 else
4036 qos = le16toh(*(const uint16_t *)
4037 (((struct ieee80211_qosframe *) wh)->i_qos));
4039 else
4041 qos = 0;
4044 if (ni->ni_flags & IEEE80211_NODE_HT)
4046 if (IEEE80211_IS_MULTICAST(wh->i_addr1) || type != IEEE80211_FC0_TYPE_DATA)
4047 rate = 0;
4048 else if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE)
4049 rate = ic->ic_fixed_rate;
4050 else
4051 rate = ni->ni_htrates.rs_rates[ni->ni_txrate];
4053 else
4055 if (IEEE80211_IS_MULTICAST(wh->i_addr1) || type != IEEE80211_FC0_TYPE_DATA)
4056 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
4057 else if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE)
4058 rate = ic->ic_fixed_rate;
4059 else
4060 rate = ni->ni_rates.rs_rates[ni->ni_txrate];
4063 rate &= IEEE80211_RATE_VAL;
4065 len = sizeof(struct rt2870_txinfo) + sizeof(struct rt2870_txwi) + m->m_pkthdr.len;
4067 /* align end on a 4-bytes boundary */
4069 dmalen = (len + 3) & ~3;
4071 /* fill Tx info */
4073 memset(txinfo, 0, sizeof(struct rt2870_txinfo));
4075 txinfo->len = htole16(dmalen);
4077 txinfo->qsel_flags = (RT2870_TXINFO_QSEL_EDCA << RT2870_TXINFO_QSEL_SHIFT);
4079 /* fill Tx wireless info */
4081 if (ni->ni_flags & IEEE80211_NODE_HT)
4082 mcs = rate;
4083 else
4084 mcs = rt2870_rate2mcs(rate);
4086 pid = mcs;
4088 wcid = (type == IEEE80211_FC0_TYPE_DATA) ?
4089 RT2870_AID2WCID(ni->ni_associd) : 0xff;
4091 /* calculate MPDU length without padding */
4093 hdrsize = ieee80211_hdrsize(wh);
4094 hdrspace = ieee80211_hdrspace(ic, wh);
4095 mpdu_len = m->m_pkthdr.len - hdrspace + hdrsize;
4097 memset(txwi, 0, sizeof(struct rt2870_txwi));
4099 txwi->wcid = wcid;
4101 txwi->pid_mpdu_len = ((htole16(pid) & RT2870_TXWI_PID_MASK) <<
4102 RT2870_TXWI_PID_SHIFT) | ((htole16(mpdu_len) & RT2870_TXWI_MPDU_LEN_MASK) <<
4103 RT2870_TXWI_MPDU_LEN_SHIFT);
4105 stbc = (ni->ni_htcap & IEEE80211_HTCAP_RXSTBC) ? 1 : 0;
4107 shortgi = (ic->ic_flags_ext & (IEEE80211_FEXT_SHORTGI20 | IEEE80211_FEXT_SHORTGI40)) &&
4108 (ni->ni_flags & IEEE80211_NODE_HT) && (ni->ni_htcap & (IEEE80211_HTCAP_SHORTGI20 | IEEE80211_HTCAP_SHORTGI40));
4110 txwi->phymode_ifs_stbc_shortgi |=
4111 ((stbc & RT2870_TXWI_STBC_MASK) << RT2870_TXWI_STBC_SHIFT) |
4112 ((shortgi & RT2870_TXWI_SHORTGI_MASK) << RT2870_TXWI_SHORTGI_SHIFT);
4114 if (ni->ni_flags & IEEE80211_NODE_HT)
4116 txwi->phymode_ifs_stbc_shortgi |=
4117 (RT2870_TXWI_PHYMODE_HT_MIXED << RT2870_TXWI_PHYMODE_SHIFT);
4119 else
4121 if (!RT2870_RATE_IS_OFDM(rate))
4123 txwi->phymode_ifs_stbc_shortgi |=
4124 (RT2870_TXWI_PHYMODE_CCK << RT2870_TXWI_PHYMODE_SHIFT);
4126 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
4127 mcs |= RT2870_TXWI_MCS_SHOTPRE;
4129 else
4131 txwi->phymode_ifs_stbc_shortgi |=
4132 (RT2870_TXWI_PHYMODE_OFDM << RT2870_TXWI_PHYMODE_SHIFT);
4136 if ((ni->ni_flags & IEEE80211_NODE_HT) && (ni->ni_chw == 40))
4137 bw = RT2870_TXWI_BW_40;
4138 else
4139 bw = RT2870_TXWI_BW_20;
4141 txwi->bw_mcs = ((bw & RT2870_TXWI_BW_MASK) << RT2870_TXWI_BW_SHIFT) |
4142 ((mcs & RT2870_TXWI_MCS_MASK) << RT2870_TXWI_MCS_SHIFT);
4144 if (type != IEEE80211_FC0_TYPE_DATA)
4145 txwi->txop = (RT2870_TXWI_TXOP_BACKOFF << RT2870_TXWI_TXOP_SHIFT);
4146 else
4147 txwi->txop = (RT2870_TXWI_TXOP_HT << RT2870_TXWI_TXOP_SHIFT);
4149 /* skip ACKs for multicast frames */
4151 if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
4152 (!hasqos || (qos & IEEE80211_QOS_ACKPOLICY) != IEEE80211_QOS_ACKPOLICY_NOACK))
4154 txwi->bawin_size_xflags |=
4155 (RT2870_TXWI_XFLAGS_ACK << RT2870_TXWI_XFLAGS_SHIFT);
4157 if (ni->ni_flags & IEEE80211_NODE_HT)
4159 /* preamble + plcp + signal extension + SIFS */
4161 dur = 16 + 4 + 6 + 10;
4163 else
4165 ackrate = rt2870_ackrate(ic, rate);
4167 dur = rt2870_txtime(RT2870_ACK_SIZE, ackrate, ic->ic_flags) +
4168 sc->sifs;
4171 *(uint16_t *) wh->i_dur = htole16(dur);
4174 /* check fo A-MPDU */
4176 if ((qos & IEEE80211_QOS_ACKPOLICY) == IEEE80211_QOS_ACKPOLICY_BA)
4178 ac = M_WME_GETAC(m);
4179 tx_ampdu = &ni->ni_tx_ampdu[ac];
4181 mpdu_density = RT2870_MS(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY);
4182 bawin_size = tx_ampdu->txa_wnd;
4184 txwi->mpdu_density_flags |=
4185 ((mpdu_density & RT2870_TXWI_MPDU_DENSITY_MASK) << RT2870_TXWI_MPDU_DENSITY_SHIFT) |
4186 (RT2870_TXWI_FLAGS_AMPDU << RT2870_TXWI_FLAGS_SHIFT);
4188 txwi->bawin_size_xflags |=
4189 ((bawin_size & RT2870_TXWI_BAWIN_SIZE_MASK) << RT2870_TXWI_BAWIN_SIZE_SHIFT);
4191 if (IEEE80211_HAS_ADDR4(wh))
4192 ((struct ieee80211_qosframe_addr4 *) wh)->i_qos[0] &= ~IEEE80211_QOS_ACKPOLICY;
4193 else
4194 ((struct ieee80211_qosframe *) wh)->i_qos[0] &= ~IEEE80211_QOS_ACKPOLICY;
4196 ampdu = 1;
4198 else
4200 ampdu = 0;
4203 /* ask MAC to insert timestamp into probe responses */
4205 if ((wh->i_fc[0] & (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
4206 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
4207 txwi->mpdu_density_flags |=
4208 (RT2870_TXWI_FLAGS_TS << RT2870_TXWI_FLAGS_SHIFT);
4210 if (bpf_peers_present(sc->drvbpf))
4212 tap = &sc->txtap;
4214 tap->flags = IEEE80211_RADIOTAP_F_DATAPAD;
4215 tap->chan_flags = htole32(ic->ic_curchan->ic_flags);
4216 tap->chan_freq = htole16(ic->ic_curchan->ic_freq);
4217 tap->chan_ieee = ic->ic_curchan->ic_ieee;
4218 tap->chan_maxpow = 0;
4220 if (ni->ni_flags & IEEE80211_NODE_HT)
4221 tap->rate = mcs | IEEE80211_RATE_MCS;
4222 else
4223 tap->rate = rate;
4225 if (mcs & RT2870_TXWI_MCS_SHOTPRE)
4226 tap->flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
4228 if (shortgi)
4229 tap->flags |= IEEE80211_RADIOTAP_F_SHORTGI;
4231 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
4232 tap->flags |= IEEE80211_RADIOTAP_F_WEP;
4234 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
4236 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
4238 bpf_mtap2(sc->drvbpf, tap, sc->txtap_len, m);
4240 wh->i_fc[1] |= IEEE80211_FC1_WEP;
4242 else
4244 bpf_mtap2(sc->drvbpf, tap, sc->txtap_len, m);
4248 m_copydata(m, 0, m->m_pkthdr.len, (caddr_t) (txwi + 1));
4250 RT2870_DPRINTF(sc, RT2870_DEBUG_TX,
4251 "%s: sending frame: qid=%d, hdrsize=%d, hdrspace=%d, len=%d, "
4252 "bw=%d, stbc=%d, shortgi=%d, mcs=%d, wcid=0x%02x, ampdu=%d, DMA len=%d\n",
4253 device_get_nameunit(sc->dev),
4254 qid, hdrsize, hdrspace, m->m_pkthdr.len, bw, stbc, shortgi, mcs, wcid, ampdu, dmalen);
4256 data->m = m;
4257 data->ni = ni;
4259 STAILQ_INSERT_TAIL(&ring->active, data, next);
4260 ring->queued++;
4262 usbd_setup_xfer(data->xfer, ring->usb_pipe, ring, data->buf, len,
4263 USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RT2870_USB_XFER_TIMEOUT, rt2870_tx_intr);
4265 usbd_transfer(data->xfer);
4267 return 0;
4271 * rt2870_tx_raw
4273 static int rt2870_tx_raw(struct rt2870_softc *sc,
4274 struct mbuf *m, struct ieee80211_node *ni,
4275 const struct ieee80211_bpf_params *params)
4277 RT2870_DPRINTF(sc, RT2870_DEBUG_TX,
4278 "%s: Tx raw\n",
4279 device_get_nameunit(sc->dev));
4281 return 0;
4285 * rt2870_rx_intr
4287 static void rt2870_rx_intr(usbd_xfer_handle xfer,
4288 usbd_private_handle priv, usbd_status status)
4290 struct rt2870_softc *sc;
4291 struct ifnet *ifp;
4292 struct rt2870_softc_rx_ring *ring;
4293 struct rt2870_softc_rx_data *data;
4294 int len;
4296 sc = priv;
4297 ifp = sc->ifp;
4298 ring = &sc->rx_ring;
4300 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
4301 return;
4303 RT2870_DPRINTF(sc, RT2870_DEBUG_INTR,
4304 "%s: Rx interrupt: %s\n",
4305 device_get_nameunit(sc->dev), usbd_errstr(status));
4307 sc->interrupts++;
4308 sc->rx_interrupts++;
4310 switch (status)
4312 case USBD_NORMAL_COMPLETION:
4313 data = STAILQ_FIRST(&ring->active);
4314 if (data == NULL)
4315 break;
4317 STAILQ_REMOVE_HEAD(&ring->active, next);
4319 KASSERT(data->xfer == xfer,
4320 ("%s: Rx interrupt: invalid USB xfer\n",
4321 device_get_nameunit(sc->dev)));
4323 usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
4325 data->len = len;
4327 STAILQ_INSERT_TAIL(&ring->done, data, next);
4329 taskqueue_enqueue(sc->taskqueue, &sc->rx_done_task);
4330 break;
4332 case USBD_CANCELLED:
4333 case USBD_NOT_STARTED:
4334 break;
4336 default:
4337 if (status == USBD_STALLED)
4338 usbd_clear_endpoint_stall_async(ring->usb_pipe);
4340 data = STAILQ_FIRST(&ring->active);
4341 if (data != NULL)
4343 STAILQ_REMOVE_HEAD(&ring->active, next);
4345 STAILQ_INSERT_TAIL(&ring->active, data, next);
4347 usbd_setup_xfer(xfer, ring->usb_pipe, sc, data->buf,
4348 RT2870_USB_RX_BULK_BUFLEN, USBD_SHORT_XFER_OK | USBD_NO_COPY,
4349 USBD_NO_TIMEOUT, rt2870_rx_intr);
4351 usbd_transfer(xfer);
4353 break;
4358 * rt2870_tx_intr
4360 static void rt2870_tx_intr(usbd_xfer_handle xfer,
4361 usbd_private_handle priv, usbd_status status)
4363 struct rt2870_softc *sc;
4364 struct ifnet *ifp;
4365 struct rt2870_softc_tx_ring *ring;
4366 struct rt2870_softc_tx_data *data;
4368 ring = priv;
4369 sc = ring->sc;
4370 ifp = sc->ifp;
4372 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
4373 return;
4375 RT2870_DPRINTF(sc, RT2870_DEBUG_INTR,
4376 "%s: Tx interrupt: %s\n",
4377 device_get_nameunit(sc->dev), usbd_errstr(status));
4379 sc->interrupts++;
4380 sc->tx_interrupts[ring->qid]++;
4382 switch (status)
4384 case USBD_NORMAL_COMPLETION:
4385 data = STAILQ_FIRST(&ring->active);
4386 if (data == NULL)
4387 break;
4389 STAILQ_REMOVE_HEAD(&ring->active, next);
4391 STAILQ_INSERT_TAIL(&ring->done, data, next);
4393 sc->tx_qid_pending_mask |= (1 << ring->qid);
4395 taskqueue_enqueue(sc->taskqueue, &sc->tx_done_task);
4396 break;
4398 case USBD_CANCELLED:
4399 case USBD_NOT_STARTED:
4400 break;
4402 default:
4403 data = STAILQ_FIRST(&ring->active);
4404 if (data != NULL)
4406 STAILQ_REMOVE_HEAD(&ring->active, next);
4408 if (data->m != NULL)
4410 m_freem(data->m);
4411 data->m = NULL;
4414 if (data->ni != NULL)
4416 ieee80211_free_node(data->ni);
4417 data->ni = NULL;
4420 STAILQ_INSERT_TAIL(&ring->inactive, data, next);
4422 ring->queued--;
4425 printf("%s: could not transmit buffer: qid=%d, status=%s\n",
4426 device_get_nameunit(sc->dev), ring->qid, usbd_errstr(status));
4428 ifp->if_oerrors++;
4429 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
4431 if (status == USBD_STALLED)
4432 usbd_clear_endpoint_stall_async(ring->usb_pipe);
4433 break;
4438 * rt2870_rx_done_task
4440 static void rt2870_rx_done_task(void *context, int pending)
4442 struct rt2870_softc *sc;
4443 struct ifnet *ifp;
4445 sc = context;
4446 ifp = sc->ifp;
4448 RT2870_DPRINTF(sc, RT2870_DEBUG_RX,
4449 "%s: Rx done task\n",
4450 device_get_nameunit(sc->dev));
4452 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
4453 return;
4455 if (rt2870_rx_eof(sc, sc->rx_process_limit) != 0)
4457 RT2870_DPRINTF(sc, RT2870_DEBUG_RX,
4458 "%s: Rx done task: scheduling again\n",
4459 device_get_nameunit(sc->dev));
4461 taskqueue_enqueue(sc->taskqueue, &sc->rx_done_task);
4466 * rt2870_tx_done_task
4468 static void rt2870_tx_done_task(void *context, int pending)
4470 struct rt2870_softc *sc;
4471 struct ifnet *ifp;
4472 int i;
4474 sc = context;
4475 ifp = sc->ifp;
4477 RT2870_DPRINTF(sc, RT2870_DEBUG_TX,
4478 "%s: Tx done task\n",
4479 device_get_nameunit(sc->dev));
4481 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
4482 return;
4484 for (i = sc->usb_endpoints - 2; i >= 0; i--)
4486 if (sc->tx_qid_pending_mask & (1 << i))
4488 sc->tx_qid_pending_mask &= ~(1 << i);
4490 rt2870_tx_eof(sc, &sc->tx_ring[i]);
4494 if (sc->tx_qid_pending_mask != 0)
4496 RT2870_DPRINTF(sc, RT2870_DEBUG_TX,
4497 "%s: Tx done task: scheduling again\n",
4498 device_get_nameunit(sc->dev));
4500 taskqueue_enqueue(sc->taskqueue, &sc->tx_done_task);
4503 sc->tx_timer = 0;
4505 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
4509 * rt2870_periodic_task
4511 static void rt2870_periodic_task(void *context, int pending)
4513 struct rt2870_softc *sc;
4514 struct ifnet *ifp;
4515 struct ieee80211com *ic;
4517 sc = context;
4518 ifp = sc->ifp;
4519 ic = &sc->ic;
4521 RT2870_DPRINTF(sc, RT2870_DEBUG_PERIODIC,
4522 "%s: periodic task: round=%lu\n",
4523 device_get_nameunit(sc->dev), sc->periodic_round);
4525 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
4526 return;
4528 sc->periodic_round++;
4530 rt2870_update_stats(sc);
4532 if ((sc->periodic_round % 10) == 0)
4534 rt2870_bbp_tuning(sc);
4536 rt2870_update_raw_counters(sc);
4538 rt2870_watchdog(sc);
4540 if (ic->ic_opmode == IEEE80211_M_STA)
4541 rt2870_amrr_update_iter_func(sc, ic->ic_bss);
4542 else
4543 ieee80211_iterate_nodes(&ic->ic_sta, rt2870_amrr_update_iter_func, sc);
4546 callout_reset(&sc->periodic_ch, hz / 10, rt2870_periodic, sc);
4550 * rt2870_cmd_task
4552 static void rt2870_cmd_task(void *context, int pending)
4554 struct rt2870_softc *sc;
4555 struct rt2870_softc_cmd_ring *ring;
4556 struct rt2870_softc_cmd *cmd;
4558 sc = context;
4559 ring = &sc->cmd_ring;
4561 while (1)
4563 RT2870_SOFTC_LOCK(sc);
4565 cmd = STAILQ_FIRST(&ring->active);
4566 if (cmd == NULL)
4568 RT2870_SOFTC_UNLOCK(sc);
4569 break;
4572 STAILQ_REMOVE_HEAD(&ring->active, next);
4574 RT2870_SOFTC_UNLOCK(sc);
4576 cmd->cb(sc, cmd->data);
4578 RT2870_SOFTC_LOCK(sc);
4580 STAILQ_INSERT_TAIL(&ring->inactive, cmd, next);
4581 ring->queued--;
4583 RT2870_SOFTC_UNLOCK(sc);
4588 * rt2870_rx_eof
4590 static int rt2870_rx_eof(struct rt2870_softc *sc, int limit)
4592 struct ifnet *ifp;
4593 struct rt2870_softc_rx_ring *ring;
4594 struct rt2870_softc_rx_data *data;
4595 uint32_t dmalen;
4596 uint8_t *buf;
4597 int nframes, len;
4599 ifp = sc->ifp;
4600 ring = &sc->rx_ring;
4602 nframes = 0;
4604 while (limit != 0)
4606 RT2870_SOFTC_LOCK(sc);
4608 data = STAILQ_FIRST(&ring->done);
4609 if (data == NULL)
4611 RT2870_SOFTC_UNLOCK(sc);
4612 break;
4615 STAILQ_REMOVE_HEAD(&ring->done, next);
4617 RT2870_SOFTC_UNLOCK(sc);
4619 buf = data->buf;
4620 len = data->len;
4622 if (len < RT2870_RX_DESC_SIZE)
4624 ifp->if_ierrors++;
4625 goto skip;
4628 while (len > (sizeof(uint32_t) + sizeof(struct rt2870_rxinfo)))
4630 dmalen = (le32toh(*(uint32_t *) buf) & 0xffff);
4632 if (dmalen == 0 || (dmalen & 3) != 0)
4634 RT2870_DPRINTF(sc, RT2870_DEBUG_RX,
4635 "%s: bad DMA len=%u\n",
4636 device_get_nameunit(sc->dev), dmalen);
4637 goto skip;
4640 if ((dmalen + sizeof(uint32_t) + sizeof(struct rt2870_rxinfo)) > len)
4642 RT2870_DPRINTF(sc, RT2870_DEBUG_RX,
4643 "%s: bad DMA len: DMA len=%u, USB len=%d\n",
4644 device_get_nameunit(sc->dev),
4645 (unsigned int) (dmalen + sizeof(uint32_t) + sizeof(struct rt2870_rxinfo)), len);
4646 goto skip;
4649 nframes++;
4651 rt2870_rx_frame(sc, buf + sizeof(uint32_t), dmalen);
4653 buf += (dmalen + sizeof(uint32_t) + sizeof(struct rt2870_rxinfo));
4654 len -= (dmalen + sizeof(uint32_t) + sizeof(struct rt2870_rxinfo));
4657 skip:
4659 RT2870_SOFTC_LOCK(sc);
4661 STAILQ_INSERT_TAIL(&ring->active, data, next);
4663 RT2870_SOFTC_UNLOCK(sc);
4665 usbd_setup_xfer(data->xfer, ring->usb_pipe, sc, data->buf,
4666 RT2870_USB_RX_BULK_BUFLEN, USBD_SHORT_XFER_OK | USBD_NO_COPY,
4667 USBD_NO_TIMEOUT, rt2870_rx_intr);
4669 usbd_transfer(data->xfer);
4671 limit--;
4674 RT2870_DPRINTF(sc, RT2870_DEBUG_RX,
4675 "%s: Rx eof: nframes=%d\n",
4676 device_get_nameunit(sc->dev), nframes);
4678 sc->rx_packets += nframes;
4680 return (limit == 0);
4684 * rt2870_tx_eof
4686 static void rt2870_tx_eof(struct rt2870_softc *sc,
4687 struct rt2870_softc_tx_ring *ring)
4689 struct ifnet *ifp;
4690 struct rt2870_softc_tx_data *data;
4691 int nframes;
4693 ifp = sc->ifp;
4695 nframes = 0;
4697 while (1)
4699 RT2870_SOFTC_LOCK(sc);
4701 data = STAILQ_FIRST(&ring->done);
4702 if (data == NULL)
4704 RT2870_SOFTC_UNLOCK(sc);
4705 break;
4708 STAILQ_REMOVE_HEAD(&ring->done, next);
4710 RT2870_SOFTC_UNLOCK(sc);
4712 rt2870_drain_fifo_stats(sc);
4714 nframes++;
4716 if (data->m->m_flags & M_TXCB)
4717 ieee80211_process_callback(data->ni, data->m, 0);
4719 m_freem(data->m);
4721 ieee80211_free_node(data->ni);
4723 data->m = NULL;
4724 data->ni = NULL;
4726 RT2870_SOFTC_LOCK(sc);
4728 STAILQ_INSERT_TAIL(&ring->inactive, data, next);
4730 ring->queued--;
4732 RT2870_SOFTC_UNLOCK(sc);
4734 ifp->if_opackets++;
4737 RT2870_DPRINTF(sc, RT2870_DEBUG_TX,
4738 "%s: Tx eof: qid=%d, nframes=%d\n",
4739 device_get_nameunit(sc->dev), ring->qid, nframes);
4743 * rt2870_update_stats
4745 static void rt2870_update_stats(struct rt2870_softc *sc)
4747 struct ifnet *ifp;
4748 struct ieee80211com *ic;
4749 struct ieee80211_node *ni;
4750 uint32_t stacnt[3];
4751 int beacons, noretryok, retryok, failed, underflows, zerolen;
4752 uint16_t associd;
4753 uint8_t wcid;
4755 ifp = sc->ifp;
4756 ic = &sc->ic;
4758 RT2870_DPRINTF(sc, RT2870_DEBUG_STATS,
4759 "%s: update statistic\n",
4760 device_get_nameunit(sc->dev));
4762 rt2870_drain_fifo_stats(sc);
4764 /* read and clear Tx statistic registers */
4766 rt2870_io_mac_read_multi(sc, RT2870_REG_TX_STA_CNT0,
4767 stacnt, sizeof(stacnt));
4769 stacnt[0] = le32toh(stacnt[0]);
4770 stacnt[1] = le32toh(stacnt[1]);
4771 stacnt[2] = le32toh(stacnt[2]);
4773 beacons = stacnt[0] >> 16;
4774 noretryok = stacnt[1] & 0xffff;
4775 retryok = stacnt[1] >> 16;
4776 failed = stacnt[0] & 0xffff;
4777 underflows = stacnt[2] >> 16;
4778 zerolen = stacnt[2] & 0xffff;
4780 RT2870_DPRINTF(sc, RT2870_DEBUG_STATS,
4781 "%s: update statistic: beacons=%d, noretryok=%d, retryok=%d, failed=%d, underflows=%d, zerolen=%d\n",
4782 device_get_nameunit(sc->dev),
4783 beacons, noretryok, retryok, failed, underflows, zerolen);
4785 ifp->if_oerrors += failed;
4787 sc->tx_beacons += beacons;
4788 sc->tx_noretryok += noretryok;
4789 sc->tx_retryok += retryok;
4790 sc->tx_failed += failed;
4791 sc->tx_underflows += underflows;
4792 sc->tx_zerolen += zerolen;
4794 if (ic->ic_opmode == IEEE80211_M_STA && ic->ic_state == IEEE80211_S_RUN)
4796 ni = ic->ic_bss;
4798 associd = (ni != NULL) ? ni->ni_associd : 0;
4799 wcid = RT2870_AID2WCID(associd);
4801 rt2870_amrr_tx_update(&sc->amrr_node[wcid],
4802 noretryok + retryok + failed, noretryok + retryok, retryok + failed);
4807 * rt2870_bbp_tuning
4809 static void rt2870_bbp_tuning(struct rt2870_softc *sc)
4811 struct ieee80211com *ic;
4812 struct ieee80211_node *ni;
4813 int chan, group;
4814 int8_t rssi, old, new;
4816 /* RT2860C does not support BBP tuning */
4818 if (sc->mac_rev == 0x28600100)
4819 return;
4821 ic = &sc->ic;
4823 if ((ic->ic_flags & IEEE80211_F_SCAN) ||
4824 ic->ic_opmode != IEEE80211_M_STA || ic->ic_state != IEEE80211_S_RUN)
4825 return;
4827 ni = ic->ic_bss;
4829 chan = ieee80211_chan2ieee(ic, ni->ni_chan);
4831 if (chan <= 14)
4832 group = 0;
4833 else if (chan <= 64)
4834 group = 1;
4835 else if (chan <= 128)
4836 group = 2;
4837 else
4838 group = 3;
4840 rssi = ieee80211_getrssi(ic);
4842 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
4844 new = 0x2e + sc->lna_gain[group];
4846 else
4848 if (!IEEE80211_IS_CHAN_HT40(ni->ni_chan))
4849 new = 0x32 + sc->lna_gain[group] * 5 / 3;
4850 else
4851 new = 0x3a + sc->lna_gain[group] * 5 / 3;
4854 /* Tune if absolute average RSSI is greater than -80 */
4856 if (rssi > -80)
4857 new += 0x10;
4859 old = rt2870_io_bbp_read(sc, 66);
4861 if (old != new)
4862 rt2870_io_bbp_write(sc, 66, new);
4866 * rt2870_watchdog
4868 static void rt2870_watchdog(struct rt2870_softc *sc)
4870 uint32_t tmp;
4871 int ntries;
4873 tmp = rt2870_io_mac_read(sc, RT2870_REG_PBF_TXRXQ_PCNT);
4875 RT2870_DPRINTF(sc, RT2870_DEBUG_WATCHDOG,
4876 "%s: watchdog: TXRXQ_PCNT=0x%08x\n",
4877 device_get_nameunit(sc->dev), tmp);
4879 if (((tmp >> RT2870_REG_TX0Q_PCNT_SHIFT) & RT2870_REG_TX0Q_PCNT_MASK) != 0)
4881 sc->tx_queue_not_empty[0]++;
4883 rt2870_io_mac_write(sc, RT2870_REG_PBF_CFG, 0xf40012);
4885 for (ntries = 0; ntries < 10; ntries++)
4887 tmp = rt2870_io_mac_read(sc, RT2870_REG_PBF_TXRXQ_PCNT);
4888 if (((tmp >> RT2870_REG_TX0Q_PCNT_SHIFT) & RT2870_REG_TX0Q_PCNT_MASK) == 0)
4889 break;
4891 DELAY(1);
4894 rt2870_io_mac_write(sc, RT2870_REG_PBF_CFG, 0xf40006);
4897 if (((tmp >> RT2870_REG_TX1Q_PCNT_SHIFT) & RT2870_REG_TX1Q_PCNT_MASK) != 0)
4899 sc->tx_queue_not_empty[1]++;
4901 rt2870_io_mac_write(sc, RT2870_REG_PBF_CFG, 0xf4000a);
4903 for (ntries = 0; ntries < 10; ntries++)
4905 tmp = rt2870_io_mac_read(sc, RT2870_REG_PBF_TXRXQ_PCNT);
4906 if (((tmp >> RT2870_REG_TX1Q_PCNT_SHIFT) & RT2870_REG_TX1Q_PCNT_MASK) == 0)
4907 break;
4909 DELAY(1);
4912 rt2870_io_mac_write(sc, RT2870_REG_PBF_CFG, 0xf40006);
4917 * rt2870_drain_fifo_stats
4919 static void rt2870_drain_fifo_stats(struct rt2870_softc *sc)
4921 struct ifnet *ifp;
4922 uint32_t stats;
4923 uint8_t wcid, mcs, pid;
4924 int ok, agg, retrycnt;
4926 ifp = sc->ic.ic_ifp;
4928 /* drain Tx status FIFO (maxsize = 16) */
4930 while ((stats = rt2870_io_mac_read(sc, RT2870_REG_TX_STA_FIFO)) &
4931 RT2870_REG_TX_STA_FIFO_VALID)
4933 wcid = (stats >> RT2870_REG_TX_STA_FIFO_WCID_SHIFT) &
4934 RT2870_REG_TX_STA_FIFO_WCID_MASK;
4936 /* if no ACK was requested, no feedback is available */
4938 if (!(stats & RT2870_REG_TX_STA_FIFO_ACK_REQ) || wcid == 0xff)
4939 continue;
4941 /* update AMRR statistic */
4943 ok = (stats & RT2870_REG_TX_STA_FIFO_TX_OK) ? 1 : 0;
4944 agg = (stats & RT2870_REG_TX_STA_FIFO_AGG) ? 1 : 0;
4945 mcs = (stats >> RT2870_REG_TX_STA_FIFO_MCS_SHIFT) &
4946 RT2870_REG_TX_STA_FIFO_MCS_MASK;
4947 pid = (stats >> RT2870_REG_TX_STA_FIFO_PID_SHIFT) &
4948 RT2870_REG_TX_STA_FIFO_PID_MASK;
4949 retrycnt = pid - mcs;
4951 RT2870_DPRINTF(sc, RT2870_DEBUG_STATS,
4952 "%s: FIFO statistic: wcid=0x%02x, ok=%d, agg=%d, mcs=0x%02x, pid=0x%02x, retrycnt=%d\n",
4953 device_get_nameunit(sc->dev),
4954 wcid, ok, agg, mcs, pid, retrycnt);
4956 rt2870_amrr_tx_complete(&sc->amrr_node[wcid], ok, retrycnt);
4958 if (!ok)
4959 ifp->if_oerrors++;
4964 * rt2870_update_raw_counters
4966 static void rt2870_update_raw_counters(struct rt2870_softc *sc)
4968 uint32_t tmp;
4970 tmp = rt2870_io_mac_read(sc, RT2870_REG_TX_AGG_CNT);
4972 sc->tx_nonagg += tmp & 0xffff;
4973 sc->tx_agg += tmp >> 16;
4975 tmp = rt2870_io_mac_read(sc, RT2870_REG_TX_AGG_CNT0);
4977 sc->tx_ampdu += (tmp & 0xffff) / 1 + (tmp >> 16) / 2;
4979 tmp = rt2870_io_mac_read(sc, RT2870_REG_TX_AGG_CNT1);
4981 sc->tx_ampdu += (tmp & 0xffff) / 3 + (tmp >> 16) / 4;
4983 tmp = rt2870_io_mac_read(sc, RT2870_REG_TX_AGG_CNT2);
4985 sc->tx_ampdu += (tmp & 0xffff) / 5 + (tmp >> 16) / 6;
4987 tmp = rt2870_io_mac_read(sc, RT2870_REG_TX_AGG_CNT3);
4989 sc->tx_ampdu += (tmp & 0xffff) / 7 + (tmp >> 16) / 8;
4991 tmp = rt2870_io_mac_read(sc, RT2870_REG_TX_AGG_CNT4);
4993 sc->tx_ampdu += (tmp & 0xffff) / 9 + (tmp >> 16) / 10;
4995 tmp = rt2870_io_mac_read(sc, RT2870_REG_TX_AGG_CNT5);
4997 sc->tx_ampdu += (tmp & 0xffff) / 11 + (tmp >> 16) / 12;
4999 tmp = rt2870_io_mac_read(sc, RT2870_REG_TX_AGG_CNT6);
5001 sc->tx_ampdu += (tmp & 0xffff) / 13 + (tmp >> 16) / 14;
5003 tmp = rt2870_io_mac_read(sc, RT2870_REG_TX_AGG_CNT7);
5005 sc->tx_ampdu += (tmp & 0xffff) / 15 + (tmp >> 16) / 16;
5007 tmp = rt2870_io_mac_read(sc, RT2870_REG_RX_STA_CNT0);
5009 sc->rx_crc_errors += tmp & 0xffff;
5010 sc->rx_phy_errors += tmp >> 16;
5012 tmp = rt2870_io_mac_read(sc, RT2870_REG_RX_STA_CNT1);
5014 sc->rx_false_ccas += tmp & 0xffff;
5015 sc->rx_plcp_errors += tmp >> 16;
5017 tmp = rt2870_io_mac_read(sc, RT2870_REG_RX_STA_CNT2);
5019 sc->rx_dup_packets += tmp & 0xffff;
5020 sc->rx_fifo_overflows += tmp >> 16;
5022 tmp = rt2870_io_mac_read(sc, RT2870_REG_TXRX_MPDU_DEN_CNT);
5024 sc->tx_mpdu_zero_density += tmp & 0xffff;
5025 sc->rx_mpdu_zero_density += tmp >> 16;
5029 * rt2870_alloc_rx_ring
5031 static int rt2870_alloc_rx_ring(struct rt2870_softc *sc,
5032 struct rt2870_softc_rx_ring *ring)
5034 struct rt2870_softc_rx_data *data;
5035 int i, error;
5037 STAILQ_INIT(&ring->active);
5038 STAILQ_INIT(&ring->done);
5040 for (i = 0; i < RT2870_SOFTC_RX_RING_DATA_COUNT; i++)
5042 data = &ring->data[i];
5044 data->xfer = usbd_alloc_xfer(sc->usb_dev);
5045 if (data->xfer == NULL)
5047 printf("%s: could not allocate Rx xfer\n",
5048 device_get_nameunit(sc->dev));
5049 error = ENOMEM;
5050 goto fail;
5053 data->buf = usbd_alloc_buffer(data->xfer, RT2870_USB_RX_BULK_BUFLEN);
5054 if (data->buf == NULL)
5056 printf("%s: could not allocate Rx buffer\n",
5057 device_get_nameunit(sc->dev));
5058 error = ENOMEM;
5059 goto fail;
5063 return 0;
5065 fail:
5067 rt2870_free_rx_ring(sc, ring);
5069 return error;
5073 * rt2870_reset_rx_ring
5075 static void rt2870_reset_rx_ring(struct rt2870_softc *sc,
5076 struct rt2870_softc_rx_ring *ring)
5078 STAILQ_INIT(&ring->active);
5079 STAILQ_INIT(&ring->done);
5083 * rt2870_free_rx_ring
5085 static void rt2870_free_rx_ring(struct rt2870_softc *sc,
5086 struct rt2870_softc_rx_ring *ring)
5088 struct rt2870_softc_rx_data *data;
5089 int i;
5091 for (i = 0; i < RT2870_SOFTC_RX_RING_DATA_COUNT; i++)
5093 data = &ring->data[i];
5095 if (data->xfer != NULL)
5097 usbd_free_xfer(data->xfer);
5098 data->xfer = NULL;
5104 * rt2870_alloc_tx_ring
5106 static int rt2870_alloc_tx_ring(struct rt2870_softc *sc,
5107 struct rt2870_softc_tx_ring *ring, int qid)
5109 struct rt2870_softc_tx_data *data;
5110 int i, error;
5112 STAILQ_INIT(&ring->inactive);
5113 STAILQ_INIT(&ring->active);
5114 STAILQ_INIT(&ring->done);
5116 ring->sc = sc;
5117 ring->queued = 0;
5118 ring->qid = qid;
5120 for (i = 0; i < RT2870_SOFTC_TX_RING_DATA_COUNT; i++)
5122 data = &ring->data[i];
5124 data->xfer = usbd_alloc_xfer(sc->usb_dev);
5125 if (data->xfer == NULL)
5127 printf("%s: could not allocate Tx xfer\n",
5128 device_get_nameunit(sc->dev));
5129 error = ENOMEM;
5130 goto fail;
5133 data->buf = usbd_alloc_buffer(data->xfer,
5134 RT2870_TX_DESC_SIZE + MJUMPAGESIZE);
5135 if (data->buf == NULL)
5137 printf("%s: could not allocate Tx buffer\n",
5138 device_get_nameunit(sc->dev));
5139 error = ENOMEM;
5140 goto fail;
5143 memset(data->buf, 0, RT2870_TX_DESC_SIZE);
5145 STAILQ_INSERT_TAIL(&ring->inactive, data, next);
5148 return 0;
5150 fail:
5152 rt2870_free_tx_ring(sc, ring);
5154 return error;
5158 * rt2870_reset_tx_ring
5160 static void rt2870_reset_tx_ring(struct rt2870_softc *sc,
5161 struct rt2870_softc_tx_ring *ring)
5163 struct rt2870_softc_tx_data *data;
5164 int i;
5166 STAILQ_INIT(&ring->inactive);
5167 STAILQ_INIT(&ring->active);
5168 STAILQ_INIT(&ring->done);
5170 ring->queued = 0;
5172 for (i = 0; i < RT2870_SOFTC_TX_RING_DATA_COUNT; i++)
5174 data = &ring->data[i];
5176 if (data->m != NULL)
5178 m_free(data->m);
5179 data->m = NULL;
5182 if (data->ni != NULL)
5184 ieee80211_free_node(data->ni);
5185 data->ni = NULL;
5188 STAILQ_INSERT_TAIL(&ring->inactive, data, next);
5193 * rt2870_free_tx_ring
5195 static void rt2870_free_tx_ring(struct rt2870_softc *sc,
5196 struct rt2870_softc_tx_ring *ring)
5198 struct rt2870_softc_tx_data *data;
5199 int i;
5201 for (i = 0; i < RT2870_SOFTC_TX_RING_DATA_COUNT; i++)
5203 data = &ring->data[i];
5205 if (data->xfer != NULL)
5207 usbd_free_xfer(data->xfer);
5208 data->xfer = NULL;
5211 if (data->m != NULL)
5213 m_free(data->m);
5214 data->m = NULL;
5217 if (data->ni != NULL)
5219 ieee80211_free_node(data->ni);
5220 data->ni = NULL;
5226 * rt2870_reset_cmd_ring
5228 static void rt2870_reset_cmd_ring(struct rt2870_softc *sc,
5229 struct rt2870_softc_cmd_ring *ring)
5231 struct rt2870_softc_cmd *cmd;
5232 int i;
5234 STAILQ_INIT(&ring->inactive);
5235 STAILQ_INIT(&ring->active);
5237 ring->queued = 0;
5239 for (i = 0; i < RT2870_SOFTC_CMD_RING_CMD_COUNT; i++)
5241 cmd = &ring->cmd[i];
5243 STAILQ_INSERT_TAIL(&ring->inactive, cmd, next);
5248 * rt2870_sysctl_attach
5250 static void rt2870_sysctl_attach(struct rt2870_softc *sc)
5252 struct sysctl_ctx_list *ctx;
5253 struct sysctl_oid *tree;
5254 struct sysctl_oid *stats;
5256 ctx = device_get_sysctl_ctx(sc->dev);
5257 tree = device_get_sysctl_tree(sc->dev);
5259 stats = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
5260 "stats", CTLFLAG_RD, 0, "statistic");
5262 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5263 "interrupts", CTLFLAG_RD, &sc->interrupts, 0,
5264 "all interrupts");
5266 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5267 "rx_interrupts", CTLFLAG_RD, &sc->rx_interrupts, 0,
5268 "Rx interrupts");
5270 if (sc->usb_endpoints == (RT2870_SOFTC_TX_RING_COUNT + 1))
5272 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5273 "tx_mgmt_interrupts", CTLFLAG_RD, &sc->tx_interrupts[5], 0,
5274 "Tx MGMT interrupts");
5276 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5277 "tx_hcca_interrupts", CTLFLAG_RD, &sc->tx_interrupts[4], 0,
5278 "Tx HCCA interrupts");
5281 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5282 "tx_ac3_interrupts", CTLFLAG_RD, &sc->tx_interrupts[3], 0,
5283 "Tx AC3 interrupts");
5285 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5286 "tx_ac2_interrupts", CTLFLAG_RD, &sc->tx_interrupts[2], 0,
5287 "Tx AC2 interrupts");
5289 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5290 "tx_ac1_interrupts", CTLFLAG_RD, &sc->tx_interrupts[1], 0,
5291 "Tx AC1 interrupts");
5293 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5294 "tx_ac0_interrupts", CTLFLAG_RD, &sc->tx_interrupts[0], 0,
5295 "Tx AC0 interrupts");
5297 if (sc->usb_endpoints == (RT2870_SOFTC_TX_RING_COUNT + 1))
5299 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5300 "tx_mgmt_data_queued", CTLFLAG_RD, &sc->tx_ring[5].queued, 0,
5301 "Tx MGMT data queued");
5303 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5304 "tx_hcca_data_queued", CTLFLAG_RD, &sc->tx_ring[4].queued, 0,
5305 "Tx HCCA data queued");
5308 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5309 "tx_ac3_data_queued", CTLFLAG_RD, &sc->tx_ring[3].queued, 0,
5310 "Tx AC3 data queued");
5312 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5313 "tx_ac2_data_queued", CTLFLAG_RD, &sc->tx_ring[2].queued, 0,
5314 "Tx AC2 data queued");
5316 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5317 "tx_ac1_data_queued", CTLFLAG_RD, &sc->tx_ring[1].queued, 0,
5318 "Tx AC1 data queued");
5320 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5321 "tx_ac0_data_queued", CTLFLAG_RD, &sc->tx_ring[0].queued, 0,
5322 "Tx AC0 data queued");
5324 if (sc->usb_endpoints == (RT2870_SOFTC_TX_RING_COUNT + 1))
5326 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5327 "tx_mgmt_data_queue_full", CTLFLAG_RD, &sc->tx_data_queue_full[5], 0,
5328 "Tx MGMT data queue full");
5330 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5331 "tx_hcca_data_queue_full", CTLFLAG_RD, &sc->tx_data_queue_full[4], 0,
5332 "Tx HCCA data queue full");
5335 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5336 "tx_ac3_data_queue_full", CTLFLAG_RD, &sc->tx_data_queue_full[3], 0,
5337 "Tx AC3 data queue full");
5339 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5340 "tx_ac2_data_queue_full", CTLFLAG_RD, &sc->tx_data_queue_full[2], 0,
5341 "Tx AC2 data queue full");
5343 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5344 "tx_ac1_data_queue_full", CTLFLAG_RD, &sc->tx_data_queue_full[1], 0,
5345 "Tx AC1 data queue full");
5347 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5348 "tx_ac0_data_queue_full", CTLFLAG_RD, &sc->tx_data_queue_full[0], 0,
5349 "Tx AC0 data queue full");
5351 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5352 "tx_watchdog_timeouts", CTLFLAG_RD, &sc->tx_watchdog_timeouts, 0,
5353 "Tx watchdog timeouts");
5355 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5356 "rx_mbuf_alloc_errors", CTLFLAG_RD, &sc->rx_mbuf_alloc_errors, 0,
5357 "Rx mbuf allocation errors");
5359 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5360 "tx_queue_0_not_empty", CTLFLAG_RD, &sc->tx_queue_not_empty[0], 0,
5361 "Tx queue 0 not empty");
5363 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5364 "tx_queue_1_not_empty", CTLFLAG_RD, &sc->tx_queue_not_empty[1], 0,
5365 "Tx queue 1 not empty");
5367 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5368 "tx_beacons", CTLFLAG_RD, &sc->tx_beacons, 0,
5369 "Tx beacons");
5371 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5372 "tx_noretryok", CTLFLAG_RD, &sc->tx_noretryok, 0,
5373 "Tx successfull without retries");
5375 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5376 "tx_retryok", CTLFLAG_RD, &sc->tx_retryok, 0,
5377 "Tx successfull with retries");
5379 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5380 "tx_failed", CTLFLAG_RD, &sc->tx_failed, 0,
5381 "Tx failed");
5383 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5384 "tx_underflows", CTLFLAG_RD, &sc->tx_underflows, 0,
5385 "Tx underflows");
5387 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5388 "tx_zerolen", CTLFLAG_RD, &sc->tx_zerolen, 0,
5389 "Tx zero length");
5391 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5392 "tx_nonagg", CTLFLAG_RD, &sc->tx_nonagg, 0,
5393 "Tx non-aggregated");
5395 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5396 "tx_agg", CTLFLAG_RD, &sc->tx_agg, 0,
5397 "Tx aggregated");
5399 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5400 "tx_ampdu", CTLFLAG_RD, &sc->tx_ampdu, 0,
5401 "Tx A-MPDU");
5403 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5404 "tx_mpdu_zero_density", CTLFLAG_RD, &sc->tx_mpdu_zero_density, 0,
5405 "Tx MPDU with zero density");
5407 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5408 "rx_packets", CTLFLAG_RD, &sc->rx_packets, 0,
5409 "Rx packets");
5411 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5412 "rx_ampdu", CTLFLAG_RD, &sc->rx_ampdu, 0,
5413 "Rx A-MPDU");
5415 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5416 "rx_mpdu_zero_density", CTLFLAG_RD, &sc->rx_mpdu_zero_density, 0,
5417 "Rx MPDU with zero density");
5419 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5420 "rx_amsdu", CTLFLAG_RD, &sc->rx_amsdu, 0,
5421 "Rx A-MSDU");
5423 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5424 "rx_crc_errors", CTLFLAG_RD, &sc->rx_crc_errors, 0,
5425 "Rx CRC errors");
5427 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5428 "rx_phy_errors", CTLFLAG_RD, &sc->rx_phy_errors, 0,
5429 "Rx PHY errors");
5431 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5432 "rx_false_ccas", CTLFLAG_RD, &sc->rx_false_ccas, 0,
5433 "Rx false CCAs");
5435 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5436 "rx_plcp_errors", CTLFLAG_RD, &sc->rx_plcp_errors, 0,
5437 "Rx PLCP errors");
5439 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5440 "rx_dup_packets", CTLFLAG_RD, &sc->rx_dup_packets, 0,
5441 "Rx duplicate packets");
5443 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(stats), OID_AUTO,
5444 "rx_fifo_overflows", CTLFLAG_RD, &sc->rx_fifo_overflows, 0,
5445 "Rx FIFO overflows");
5448 static device_method_t rt2870_dev_methods[] =
5450 DEVMETHOD(device_probe, rt2870_probe),
5451 DEVMETHOD(device_attach, rt2870_attach),
5452 DEVMETHOD(device_detach, rt2870_detach),
5453 { 0, 0 }
5456 static driver_t rt2870_driver =
5458 "rt2870",
5459 rt2870_dev_methods,
5460 sizeof(struct rt2870_softc),
5463 static devclass_t rt2870_dev_class;
5465 DRIVER_MODULE(rt2870, uhub, rt2870_driver, rt2870_dev_class,
5466 usbd_driver_load, 0);
5468 MODULE_DEPEND(rt2870, usb, 1, 1, 1);
5469 MODULE_DEPEND(rt2870, wlan, 1, 1, 1);