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"
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
;
88 struct rt2870_cmd_argv_newassoc
92 uint8_t macaddr
[IEEE80211_ADDR_LEN
];
95 struct rt2870_cmd_argv_updatebeacon
100 struct rt2870_cmd_argv_keyset
102 struct ieee80211_key key
;
106 struct rt2870_cmd_argv_keydelete
108 struct ieee80211_key key
;
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 void rt2870_recv_action(struct ieee80211_node
*ni
,
171 const uint8_t *frm
, const uint8_t *efrm
);
173 static int rt2870_send_action(struct ieee80211_node
*ni
,
174 int category
, int action
, uint16_t args
[4]);
176 static void rt2870_amrr_update_iter_func(void *arg
, struct ieee80211_node
*ni
);
178 static void rt2870_periodic(void *arg
);
180 static void rt2870_tx_watchdog(void *arg
);
182 static int rt2870_do_async(struct rt2870_softc
*sc
,
183 void (*cb
)(struct rt2870_softc
*sc
, void *priv
),
186 static void rt2870_newstate_cb(struct rt2870_softc
*sc
, void *arg
);
188 static void rt2870_scan_start_cb(struct rt2870_softc
*sc
, void *arg
);
190 static void rt2870_scan_end_cb(struct rt2870_softc
*sc
, void *arg
);
192 static void rt2870_set_channel_cb(struct rt2870_softc
*sc
, void *arg
);
194 static void rt2870_newassoc_cb(struct rt2870_softc
*sc
, void *arg
);
196 static void rt2870_updateslot_cb(struct rt2870_softc
*sc
, void *arg
);
198 static void rt2870_update_beacon_cb(struct rt2870_softc
*sc
, void *arg
);
200 static void rt2870_wme_update_cb(struct rt2870_softc
*sc
, void *arg
);
202 static void rt2870_key_update_begin_cb(struct rt2870_softc
*sc
, void *arg
);
204 static void rt2870_key_update_end_cb(struct rt2870_softc
*sc
, void *arg
);
206 static void rt2870_key_set_cb(struct rt2870_softc
*sc
, void *arg
);
208 static void rt2870_key_delete_cb(struct rt2870_softc
*sc
, void *arg
);
210 static void rt2870_asic_set_bssid(struct rt2870_softc
*sc
,
211 const uint8_t *bssid
);
213 static void rt2870_asic_set_macaddr(struct rt2870_softc
*sc
,
214 const uint8_t *addr
);
216 static void rt2870_asic_enable_tsf_sync(struct rt2870_softc
*sc
);
218 static void rt2870_asic_disable_tsf_sync(struct rt2870_softc
*sc
);
220 static void rt2870_asic_enable_mrr(struct rt2870_softc
*sc
);
222 static void rt2870_asic_set_txpreamble(struct rt2870_softc
*sc
);
224 static void rt2870_asic_set_basicrates(struct rt2870_softc
*sc
);
226 static void rt2870_asic_update_rtsthreshold(struct rt2870_softc
*sc
);
228 static void rt2870_asic_update_txpower(struct rt2870_softc
*sc
);
230 static void rt2870_asic_update_promisc(struct rt2870_softc
*sc
);
232 static void rt2870_asic_updateprot(struct rt2870_softc
*sc
);
234 static void rt2870_asic_updateslot(struct rt2870_softc
*sc
);
236 static void rt2870_asic_wme_update(struct rt2870_softc
*sc
);
238 static int rt2870_asic_update_beacon(struct rt2870_softc
*sc
);
240 static void rt2870_asic_clear_keytables(struct rt2870_softc
*sc
);
242 static uint8_t rt2870_rxrate(struct rt2870_rxwi
*rxwi
);
244 static uint8_t rt2870_maxrssi_rxpath(struct rt2870_softc
*sc
,
245 const struct rt2870_rxwi
*rxwi
);
247 static int8_t rt2870_rssi2dbm(struct rt2870_softc
*sc
,
248 uint8_t rssi
, uint8_t rxpath
);
250 static uint8_t rt2870_rate2mcs(uint8_t rate
);
252 static int rt2870_ackrate(struct ieee80211com
*ic
, int rate
);
254 static uint16_t rt2870_txtime(int len
, int rate
, uint32_t flags
);
256 static void rt2870_rx_frame(struct rt2870_softc
*sc
,
257 uint8_t *buf
, uint32_t dmalen
);
259 static int rt2870_tx_frame(struct rt2870_softc
*sc
,
260 struct mbuf
*m
, struct ieee80211_node
*ni
, int qid
);
262 static int rt2870_tx_raw(struct rt2870_softc
*sc
,
263 struct mbuf
*m
, struct ieee80211_node
*ni
,
264 const struct ieee80211_bpf_params
*params
);
266 static void rt2870_rx_intr(usbd_xfer_handle xfer
,
267 usbd_private_handle priv
, usbd_status status
);
269 static void rt2870_tx_intr(usbd_xfer_handle xfer
,
270 usbd_private_handle priv
, usbd_status status
);
272 static void rt2870_rx_done_task(void *context
, int pending
);
274 static void rt2870_tx_done_task(void *context
, int pending
);
276 static void rt2870_periodic_task(void *context
, int pending
);
278 static void rt2870_cmd_task(void *context
, int pending
);
280 static int rt2870_rx_eof(struct rt2870_softc
*sc
, int limit
);
282 static void rt2870_tx_eof(struct rt2870_softc
*sc
,
283 struct rt2870_softc_tx_ring
*ring
);
285 static void rt2870_update_stats(struct rt2870_softc
*sc
);
287 static void rt2870_watchdog(struct rt2870_softc
*sc
);
289 static void rt2870_drain_fifo_stats(struct rt2870_softc
*sc
);
291 static void rt2870_update_raw_counters(struct rt2870_softc
*sc
);
293 static int rt2870_alloc_rx_ring(struct rt2870_softc
*sc
,
294 struct rt2870_softc_rx_ring
*ring
);
296 static void rt2870_reset_rx_ring(struct rt2870_softc
*sc
,
297 struct rt2870_softc_rx_ring
*ring
);
299 static void rt2870_free_rx_ring(struct rt2870_softc
*sc
,
300 struct rt2870_softc_rx_ring
*ring
);
302 static int rt2870_alloc_tx_ring(struct rt2870_softc
*sc
,
303 struct rt2870_softc_tx_ring
*ring
, int qid
);
305 static void rt2870_reset_tx_ring(struct rt2870_softc
*sc
,
306 struct rt2870_softc_tx_ring
*ring
);
308 static void rt2870_free_tx_ring(struct rt2870_softc
*sc
,
309 struct rt2870_softc_tx_ring
*ring
);
311 static void rt2870_reset_cmd_ring(struct rt2870_softc
*sc
,
312 struct rt2870_softc_cmd_ring
*ring
);
314 static void rt2870_sysctl_attach(struct rt2870_softc
*sc
);
320 static const struct usb_devno rt2870_usb_devno
[] =
322 { USB_VENDOR_LINKSYS4
, USB_PRODUCT_LINKSYS4_WUSB600N
}, /* Linksys WUSB600N */
323 { USB_VENDOR_DLINK2
, USB_PRODUCT_DLINK2_DWA140
}, /* D-Link DWA-140 */
324 { USB_VENDOR_DLINK2
, USB_PRODUCT_DLINK2_DWA160AREVB
}, /* D-Link DWA-160A Rev. B */
325 { USB_VENDOR_ASUS
, USB_PRODUCT_ASUS_RT2770F
}, /* Asus RT2770F */
326 { USB_VENDOR_RALINK
, USB_PRODUCT_RALINK_RT2770
}, /* Ralink RT2770 */
335 { RT2870_REG_PBF_BCN_OFFSET0
, 0xf8f0e8e0 },
336 { RT2870_REG_PBF_BCN_OFFSET1
, 0x6f77d0c8 },
337 { RT2870_REG_LEGACY_BASIC_RATE
, 0x0000013f },
338 { RT2870_REG_HT_BASIC_RATE
, 0x00008003 },
339 { RT2870_REG_SYS_CTRL
, 0x00000000 },
340 { RT2870_REG_RX_FILTER_CFG
, 0x00017f97 },
341 { RT2870_REG_BKOFF_SLOT_CFG
, 0x00000209 },
342 { RT2870_REG_TX_SW_CFG0
, 0x00000000 },
343 { RT2870_REG_TX_SW_CFG1
, 0x00080606 },
344 { RT2870_REG_TX_LINK_CFG
, 0x00001020 },
345 { RT2870_REG_TX_TIMEOUT_CFG
, 0x000a2090 },
346 { RT2870_REG_MAX_LEN_CFG
, (1 << 12) | RT2870_MAX_AGG_SIZE
},
347 { RT2870_REG_LED_CFG
, 0x7f031e46 },
348 { RT2870_REG_PBF_MAX_PCNT
, 0x1f3fbf9f },
349 { RT2870_REG_TX_RTY_CFG
, 0x47d01f0f },
350 { RT2870_REG_AUTO_RSP_CFG
, 0x00000013 },
351 { RT2870_REG_TX_CCK_PROT_CFG
, 0x05740003 },
352 { RT2870_REG_TX_OFDM_PROT_CFG
, 0x05740003 },
353 { RT2870_REG_PBF_CFG
, 0x00f40006 },
354 { RT2870_REG_TX_MM40_PROT_CFG
, 0x03f44084 },
355 { RT2870_REG_SCHDMA_WPDMA_GLO_CFG
, 0x00000030 },
356 { RT2870_REG_TX_GF20_PROT_CFG
, 0x01744004 },
357 { RT2870_REG_TX_GF40_PROT_CFG
, 0x03f44084 },
358 { RT2870_REG_TX_MM20_PROT_CFG
, 0x01744004 },
359 { RT2870_REG_TX_TXOP_CTRL_CFG
, 0x0000583f },
360 { RT2870_REG_TX_RTS_CFG
, 0x00092b20 },
361 { RT2870_REG_TX_EXP_ACK_TIME
, 0x002400ca },
362 { RT2870_REG_HCCAPSMP_TXOP_HLDR_ET
, 0x00000002 },
363 { RT2870_REG_XIFS_TIME_CFG
, 0x33a41010 },
364 { RT2870_REG_PWR_PIN_CFG
, 0x00000003 },
365 { RT2870_REG_SCHDMA_WMM_AIFSN_CFG
, 0x00002273 },
366 { RT2870_REG_SCHDMA_WMM_CWMIN_CFG
, 0x00002344 },
367 { RT2870_REG_SCHDMA_WMM_CWMAX_CFG
, 0x000034aa },
370 #define RT2870_DEF_MAC_SIZE (sizeof(rt2870_def_mac) / sizeof(rt2870_def_mac[0]))
394 #define RT2870_DEF_BBP_SIZE (sizeof(rt2870_def_bbp) / sizeof(rt2870_def_bbp[0]))
396 SYSCTL_NODE(_hw
, OID_AUTO
, rt2870
, CTLFLAG_RD
, 0, "RT2870 driver parameters");
399 static int rt2870_debug
= 0;
400 SYSCTL_INT(_hw_rt2870
, OID_AUTO
, debug
, CTLFLAG_RW
, &rt2870_debug
, 0, "rt2870 debug level");
401 TUNABLE_INT("hw.rt2870.debug", &rt2870_debug
);
407 static int rt2870_probe(device_t dev
)
409 struct usb_attach_arg
*uaa
;
411 uaa
= device_get_ivars(dev
);
413 if (uaa
->iface
!= NULL
)
416 return (usb_lookup(rt2870_usb_devno
, uaa
->vendor
, uaa
->product
) != NULL
) ?
417 UMATCH_VENDOR_PRODUCT
: UMATCH_NONE
;
423 static int rt2870_attach(device_t dev
)
425 struct rt2870_softc
*sc
;
426 struct usb_attach_arg
*uaa
;
427 usbd_status usb_error
;
428 struct ieee80211com
*ic
;
430 int error
, ntries
, i
;
432 sc
= device_get_softc(dev
);
433 uaa
= device_get_ivars(dev
);
436 sc
->usb_dev
= uaa
->device
;
439 sc
->debug
= rt2870_debug
;
441 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev
),
442 SYSCTL_CHILDREN(device_get_sysctl_tree(dev
)), OID_AUTO
,
443 "debug", CTLFLAG_RW
, &sc
->debug
, 0, "rt2870 debug level");
446 RT2870_DPRINTF(sc
, RT2870_DEBUG_ANY
,
448 device_get_nameunit(sc
->dev
));
450 if (usbd_set_config_no(sc
->usb_dev
, RT2870_USB_CONFIG_NO
, 0) != 0)
452 printf("%s: could not set USB configuration no\n",
453 device_get_nameunit(sc
->dev
));
457 usb_error
= usbd_device2interface_handle(sc
->usb_dev
,
458 RT2870_USB_IFACE_INDEX
, &sc
->usb_iface
);
461 printf("%s: could not get USB interface handle\n",
462 device_get_nameunit(sc
->dev
));
466 error
= rt2870_find_usb_endpoints(sc
);
470 for (ntries
= 0; ntries
< 100; ntries
++)
472 sc
->mac_rev
= rt2870_io_mac_read(sc
, RT2870_REG_MAC_CSR0
);
473 if (sc
->mac_rev
!= 0x00000000 && sc
->mac_rev
!= 0xffffffff)
481 printf("%s: timeout waiting for NIC to initialize\n",
482 device_get_nameunit(sc
->dev
));
486 rt2870_read_eeprom(sc
);
488 printf("%s: MAC/BBP RT2870 (rev 0x%08x), RF %s\n",
489 device_get_nameunit(sc
->dev
), sc
->mac_rev
,
490 rt2870_rf_name(sc
->rf_rev
));
492 /* open Tx and Rx USB bulk pipes */
494 for (i
= 0; i
< sc
->usb_endpoints
- 1; i
++)
496 error
= usbd_open_pipe(sc
->usb_iface
, sc
->tx_ring
[i
].usb_ep
,
497 USBD_EXCLUSIVE_USE
, &sc
->tx_ring
[i
].usb_pipe
);
500 printf("%s: could not open Tx pipe #%d: %s\n",
501 device_get_nameunit(sc
->dev
), i
, usbd_errstr(error
));
506 error
= usbd_open_pipe(sc
->usb_iface
, sc
->rx_ring
.usb_ep
,
507 USBD_EXCLUSIVE_USE
, &sc
->rx_ring
.usb_pipe
);
510 printf("%s: could not open Rx pipe: %s\n",
511 device_get_nameunit(sc
->dev
), usbd_errstr(error
));
515 /* allocate Tx and Rx rings */
517 for (i
= 0; i
< sc
->usb_endpoints
- 1; i
++)
519 error
= rt2870_alloc_tx_ring(sc
, &sc
->tx_ring
[i
], i
);
522 printf("%s: could not allocate Tx ring #%d\n",
523 device_get_nameunit(sc
->dev
), i
);
528 if (sc
->usb_endpoints
== (RT2870_SOFTC_TX_RING_COUNT
+ 1))
529 sc
->tx_ring_mgtqid
= 5;
531 sc
->tx_ring_mgtqid
= 0;
533 error
= rt2870_alloc_rx_ring(sc
, &sc
->rx_ring
);
536 printf("%s: could not allocate Rx ring\n",
537 device_get_nameunit(sc
->dev
));
541 callout_init(&sc
->periodic_ch
, 0);
542 callout_init(&sc
->tx_watchdog_ch
, 0);
544 ifp
= sc
->ifp
= if_alloc(IFT_ETHER
);
547 printf("%s: could not if_alloc()\n",
548 device_get_nameunit(sc
->dev
));
555 if_initname(ifp
, "rt2870", device_get_unit(sc
->dev
));
557 ifp
->if_flags
= IFF_BROADCAST
| IFF_SIMPLEX
| IFF_MULTICAST
|
560 ifp
->if_init
= rt2870_init
;
561 ifp
->if_ioctl
= rt2870_ioctl
;
562 ifp
->if_start
= rt2870_start
;
564 IFQ_SET_MAXLEN(&ifp
->if_snd
, IFQ_MAXLEN
);
565 ifp
->if_snd
.ifq_drv_maxlen
= IFQ_MAXLEN
;
566 IFQ_SET_READY(&ifp
->if_snd
);
572 ic
->ic_phytype
= IEEE80211_T_HT
;
573 ic
->ic_opmode
= IEEE80211_M_STA
;
574 ic
->ic_state
= IEEE80211_S_INIT
;
576 ic
->ic_caps
= IEEE80211_C_MONITOR
|
581 /* IEEE80211_C_BGSCAN | */
583 IEEE80211_C_SHPREAMBLE
|
590 IEEE80211_C_AES_CCM
|
593 ic
->ic_htcaps
= IEEE80211_HTC_HT
|
594 IEEE80211_HTC_AMSDU
| /* A-MSDU Tx */
595 IEEE80211_HTCAP_MAXAMSDU_3839
| /* max. A-MSDU Rx length */
596 IEEE80211_HTCAP_CHWIDTH40
| /* HT 40MHz channel width */
597 IEEE80211_HTCAP_GREENFIELD
| /* HT greenfield */
598 IEEE80211_HTCAP_SHORTGI20
| /* HT 20MHz short GI */
599 IEEE80211_HTCAP_SHORTGI40
| /* HT 40MHz short GI */
600 IEEE80211_HTCAP_DSSSCCK40
; /* HT 40MHz DSSS/CCK modulation */
602 /* spatial streams */
604 if (sc
->nrxpath
== 2)
605 ic
->ic_htcaps
|= IEEE80211_HTCAP_RXSTBC_2STREAM
;
606 else if (sc
->nrxpath
== 3)
607 ic
->ic_htcaps
|= IEEE80211_HTCAP_RXSTBC_3STREAM
;
609 ic
->ic_htcaps
|= IEEE80211_HTCAP_RXSTBC_1STREAM
;
612 ic
->ic_htcaps
|= IEEE80211_HTCAP_TXSTBC
;
616 if (sc
->mac_rev
!= 0x28600100)
617 ic
->ic_htcaps
|= IEEE80211_HTCAP_DELBA
;
622 ic
->ic_regdomain
= 0;
623 ic
->ic_countrycode
= CTRY_DEFAULT
;
626 rt2870_init_channels(sc
);
628 rt2870_init_channels_ht40(sc
);
630 IEEE80211_ADDR_COPY(ic
->ic_myaddr
, sc
->mac_addr
);
632 ieee80211_ifattach(ic
);
634 sc
->newstate
= ic
->ic_newstate
;
635 ic
->ic_newstate
= rt2870_newstate
;
637 ic
->ic_reset
= rt2870_reset
;
638 ic
->ic_scan_start
= rt2870_scan_start
;
639 ic
->ic_scan_end
= rt2870_scan_end
;
640 ic
->ic_set_channel
= rt2870_set_channel
;
641 ic
->ic_newassoc
= rt2870_newassoc
;
642 ic
->ic_updateslot
= rt2870_updateslot
;
643 ic
->ic_wme
.wme_update
= rt2870_wme_update
;
644 ic
->ic_update_beacon
= rt2870_update_beacon
;
645 ic
->ic_crypto
.cs_key_update_begin
= rt2870_key_update_begin
;
646 ic
->ic_crypto
.cs_key_update_end
= rt2870_key_update_end
;
647 ic
->ic_crypto
.cs_key_set
= rt2870_key_set
;
648 ic
->ic_crypto
.cs_key_delete
= rt2870_key_delete
;
649 ic
->ic_raw_xmit
= rt2870_raw_xmit
;
651 sc
->recv_action
= ic
->ic_recv_action
;
652 ic
->ic_recv_action
= rt2870_recv_action
;
654 sc
->send_action
= ic
->ic_send_action
;
655 ic
->ic_send_action
= rt2870_send_action
;
657 /* hardware requires padding between 802.11 frame header and body */
659 ic
->ic_flags
|= IEEE80211_F_WME
| IEEE80211_F_DATAPAD
| IEEE80211_F_DOTH
;
661 ic
->ic_flags_ext
|= IEEE80211_FEXT_SWBMISS
;
663 ieee80211_media_init(ic
, rt2870_media_change
, ieee80211_media_status
);
665 rt2870_amrr_init(&sc
->amrr
, ic
,
666 RT2870_AMRR_MIN_SUCCESS_THRESHOLD
,
667 RT2870_AMRR_MAX_SUCCESS_THRESHOLD
,
670 bpfattach2(ifp
, DLT_IEEE802_11_RADIO
,
671 sizeof(struct ieee80211_frame
) + IEEE80211_RADIOTAP_HDRLEN
,
674 sc
->rxtap_len
= sizeof(sc
->rxtapu
);
675 sc
->rxtap
.ihdr
.it_len
= htole16(sc
->rxtap_len
);
676 sc
->rxtap
.ihdr
.it_present
= htole32(RT2870_SOFTC_RX_RADIOTAP_PRESENT
);
678 sc
->txtap_len
= sizeof(sc
->txtapu
);
679 sc
->txtap
.ihdr
.it_len
= htole16(sc
->txtap_len
);
680 sc
->txtap
.ihdr
.it_present
= htole32(RT2870_SOFTC_TX_RADIOTAP_PRESENT
);
682 /* init task queue */
684 TASK_INIT(&sc
->rx_done_task
, 0, rt2870_rx_done_task
, sc
);
685 TASK_INIT(&sc
->tx_done_task
, 0, rt2870_tx_done_task
, sc
);
686 TASK_INIT(&sc
->periodic_task
, 0, rt2870_periodic_task
, sc
);
687 TASK_INIT(&sc
->cmd_task
, 0, rt2870_cmd_task
, sc
);
689 sc
->rx_process_limit
= 100;
691 sc
->taskqueue
= taskqueue_create("rt2870_taskq", M_NOWAIT
,
692 taskqueue_thread_enqueue
, &sc
->taskqueue
);
694 taskqueue_start_threads(&sc
->taskqueue
, 1, PI_NET
, "%s taskq",
695 device_get_nameunit(sc
->dev
));
697 rt2870_sysctl_attach(sc
);
700 ieee80211_announce(ic
);
702 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH
, sc
->usb_dev
, sc
->dev
);
708 /* close Tx and Rx USB bulk pipes */
710 if (sc
->rx_ring
.usb_pipe
!= NULL
)
712 usbd_abort_pipe(sc
->rx_ring
.usb_pipe
);
713 usbd_close_pipe(sc
->rx_ring
.usb_pipe
);
714 sc
->rx_ring
.usb_pipe
= NULL
;
717 for (i
= 0; i
< sc
->usb_endpoints
- 1; i
++)
719 if (sc
->tx_ring
[i
].usb_pipe
!= NULL
)
721 usbd_abort_pipe(sc
->tx_ring
[i
].usb_pipe
);
722 usbd_close_pipe(sc
->tx_ring
[i
].usb_pipe
);
723 sc
->tx_ring
[i
].usb_pipe
= NULL
;
727 /* free Tx and Rx rings */
729 for (i
= 0; i
< sc
->usb_endpoints
- 1; i
++)
730 rt2870_free_tx_ring(sc
, &sc
->tx_ring
[i
]);
732 rt2870_free_rx_ring(sc
, &sc
->rx_ring
);
740 static int rt2870_detach(device_t dev
)
742 struct rt2870_softc
*sc
;
743 struct ieee80211com
*ic
;
747 if (!device_is_attached(dev
))
750 sc
= device_get_softc(dev
);
754 RT2870_DPRINTF(sc
, RT2870_DEBUG_ANY
,
756 device_get_nameunit(sc
->dev
));
760 ifp
->if_drv_flags
&= ~(IFF_DRV_RUNNING
| IFF_DRV_OACTIVE
);
762 ieee80211_new_state(ic
, IEEE80211_S_INIT
, -1);
764 callout_stop(&sc
->periodic_ch
);
765 callout_stop(&sc
->tx_watchdog_ch
);
767 taskqueue_drain(sc
->taskqueue
, &sc
->rx_done_task
);
768 taskqueue_drain(sc
->taskqueue
, &sc
->tx_done_task
);
769 taskqueue_drain(sc
->taskqueue
, &sc
->periodic_task
);
770 taskqueue_drain(sc
->taskqueue
, &sc
->cmd_task
);
772 /* close Tx and Rx USB bulk pipes */
774 if (sc
->rx_ring
.usb_pipe
!= NULL
)
776 usbd_abort_pipe(sc
->rx_ring
.usb_pipe
);
777 usbd_close_pipe(sc
->rx_ring
.usb_pipe
);
778 sc
->rx_ring
.usb_pipe
= NULL
;
781 for (i
= 0; i
< sc
->usb_endpoints
- 1; i
++)
783 if (sc
->tx_ring
[i
].usb_pipe
!= NULL
)
785 usbd_abort_pipe(sc
->tx_ring
[i
].usb_pipe
);
786 usbd_close_pipe(sc
->tx_ring
[i
].usb_pipe
);
787 sc
->tx_ring
[i
].usb_pipe
= NULL
;
791 /* free Tx and Rx rings */
793 for (i
= 0; i
< sc
->usb_endpoints
- 1; i
++)
794 rt2870_free_tx_ring(sc
, &sc
->tx_ring
[i
]);
796 rt2870_free_rx_ring(sc
, &sc
->rx_ring
);
800 ieee80211_ifdetach(ic
);
804 taskqueue_free(sc
->taskqueue
);
806 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH
, sc
->usb_dev
, sc
->dev
);
812 * rt2870_find_usb_endpoints
814 static int rt2870_find_usb_endpoints(struct rt2870_softc
*sc
)
816 usb_interface_descriptor_t
*id
;
817 usb_endpoint_descriptor_t
*ed
;
820 id
= usbd_get_interface_descriptor(sc
->usb_iface
);
822 sc
->usb_endpoints
= id
->bNumEndpoints
;
823 if ((sc
->usb_endpoints
!= (RT2870_SOFTC_TX_RING_COUNT
+ 1)) &&
824 (sc
->usb_endpoints
!= (RT2870_SOFTC_TX_RING_COUNT
- 2 + 1)))
826 printf("%s: wrong number of USB endpoints=%d\n",
827 device_get_nameunit(sc
->dev
), sc
->usb_endpoints
);
831 sc
->rx_ring
.usb_ep
= -1;
833 for (i
= 0; i
< RT2870_SOFTC_TX_RING_COUNT
; i
++)
834 sc
->tx_ring
[i
].usb_ep
= -1;
836 for (i
= 0, j
= 0; i
< id
->bNumEndpoints
; i
++)
838 ed
= usbd_interface2endpoint_descriptor(sc
->usb_iface
, i
);
841 printf("%s: no endpoint descriptor #%d for USB interface\n",
842 device_get_nameunit(sc
->dev
), i
);
846 if (UE_GET_DIR(ed
->bEndpointAddress
) == UE_DIR_IN
&&
847 UE_GET_XFERTYPE(ed
->bmAttributes
) == UE_BULK
)
849 sc
->rx_ring
.usb_ep
= ed
->bEndpointAddress
;
851 else if (UE_GET_DIR(ed
->bEndpointAddress
) == UE_DIR_OUT
&&
852 UE_GET_XFERTYPE(ed
->bmAttributes
) == UE_BULK
)
854 if (j
< RT2870_SOFTC_TX_RING_COUNT
)
855 sc
->tx_ring
[j
++].usb_ep
= ed
->bEndpointAddress
;
857 sc
->tx_ring
[RT2870_SOFTC_TX_RING_COUNT
- 1].usb_ep
=
858 ed
->bEndpointAddress
;
862 if ((sc
->rx_ring
.usb_ep
== -1) ||
863 ((j
!= RT2870_SOFTC_TX_RING_COUNT
) && (j
!= RT2870_SOFTC_TX_RING_COUNT
- 2)))
865 printf("%s: missing USB endpoints\n",
866 device_get_nameunit(sc
->dev
));
874 * rt2870_init_channels
876 static void rt2870_init_channels(struct rt2870_softc
*sc
)
878 struct ieee80211com
*ic
;
879 struct ieee80211_channel
*c
;
884 /* set supported channels for 2GHz band */
886 for (i
= 1; i
<= 14; i
++)
888 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
889 flags
= IEEE80211_CHAN_B
;
891 c
->ic_freq
= ieee80211_ieee2mhz(i
, flags
);
895 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
896 flags
= IEEE80211_CHAN_B
| IEEE80211_CHAN_HT20
;
898 c
->ic_freq
= ieee80211_ieee2mhz(i
, flags
);
902 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
903 flags
= IEEE80211_CHAN_G
;
905 c
->ic_freq
= ieee80211_ieee2mhz(i
, flags
);
909 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
910 flags
= IEEE80211_CHAN_G
| IEEE80211_CHAN_HT20
;
912 c
->ic_freq
= ieee80211_ieee2mhz(i
, flags
);
917 /* set supported channels for 5GHz band */
919 if (sc
->rf_rev
== RT2870_EEPROM_RF_2850
||
920 sc
->rf_rev
== RT2870_EEPROM_RF_2750
)
922 for (i
= 36; i
<= 64; i
+= 4)
924 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
925 flags
= IEEE80211_CHAN_A
;
927 c
->ic_freq
= ieee80211_ieee2mhz(i
, flags
);
931 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
932 flags
= IEEE80211_CHAN_A
| IEEE80211_CHAN_HT20
;
934 c
->ic_freq
= ieee80211_ieee2mhz(i
, flags
);
939 for (i
= 100; i
<= 140; i
+= 4)
941 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
942 flags
= IEEE80211_CHAN_A
;
944 c
->ic_freq
= ieee80211_ieee2mhz(i
, flags
);
948 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
949 flags
= IEEE80211_CHAN_A
| IEEE80211_CHAN_HT20
;
951 c
->ic_freq
= ieee80211_ieee2mhz(i
, flags
);
956 for (i
= 149; i
<= 165; i
+= 4)
958 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
959 flags
= IEEE80211_CHAN_A
;
961 c
->ic_freq
= ieee80211_ieee2mhz(i
, flags
);
965 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
966 flags
= IEEE80211_CHAN_A
| IEEE80211_CHAN_HT20
;
968 c
->ic_freq
= ieee80211_ieee2mhz(i
, flags
);
976 * rt2870_init_channels_ht40
978 static void rt2870_init_channels_ht40(struct rt2870_softc
*sc
)
980 struct ieee80211com
*ic
;
981 struct ieee80211_channel
*c
, *cent
, *ext
;
986 /* set supported channels for 2GHz band */
988 for (i
= 1; i
<= 14; i
++)
990 flags
= IEEE80211_CHAN_G
| IEEE80211_CHAN_HT40
;
992 /* find the center channel */
994 cent
= ieee80211_find_channel_byieee(ic
, i
,
995 flags
& ~IEEE80211_CHAN_HT
);
998 printf("%s: skip channel %d, could not find center channel\n",
999 device_get_nameunit(sc
->dev
), i
);
1003 /* find the extension channel */
1005 ext
= ieee80211_find_channel(ic
, cent
->ic_freq
+ 20,
1006 flags
& ~IEEE80211_CHAN_HT
);
1009 printf("%s: skip channel %d, could not find extension channel\n",
1010 device_get_nameunit(sc
->dev
), i
);
1014 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
1017 c
->ic_extieee
= ext
->ic_ieee
;
1018 c
->ic_flags
&= ~IEEE80211_CHAN_HT
;
1019 c
->ic_flags
|= IEEE80211_CHAN_HT40U
;
1021 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
1024 c
->ic_extieee
= cent
->ic_ieee
;
1025 c
->ic_flags
&= ~IEEE80211_CHAN_HT
;
1026 c
->ic_flags
|= IEEE80211_CHAN_HT40D
;
1029 /* set supported channels for 5GHz band */
1031 if (sc
->rf_rev
== RT2870_EEPROM_RF_2850
||
1032 sc
->rf_rev
== RT2870_EEPROM_RF_2750
)
1034 for (i
= 36; i
<= 64; i
+= 4)
1036 flags
= IEEE80211_CHAN_A
| IEEE80211_CHAN_HT40
;
1038 /* find the center channel */
1040 cent
= ieee80211_find_channel_byieee(ic
, i
,
1041 flags
& ~IEEE80211_CHAN_HT
);
1044 printf("%s: skip channel %d, could not find center channel\n",
1045 device_get_nameunit(sc
->dev
), i
);
1049 /* find the extension channel */
1051 ext
= ieee80211_find_channel(ic
, cent
->ic_freq
+ 20,
1052 flags
& ~IEEE80211_CHAN_HT
);
1055 printf("%s: skip channel %d, could not find extension channel\n",
1056 device_get_nameunit(sc
->dev
), i
);
1060 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
1063 c
->ic_extieee
= ext
->ic_ieee
;
1064 c
->ic_flags
&= ~IEEE80211_CHAN_HT
;
1065 c
->ic_flags
|= IEEE80211_CHAN_HT40U
;
1067 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
1070 c
->ic_extieee
= cent
->ic_ieee
;
1071 c
->ic_flags
&= ~IEEE80211_CHAN_HT
;
1072 c
->ic_flags
|= IEEE80211_CHAN_HT40D
;
1075 for (i
= 100; i
<= 140; i
+= 4)
1077 flags
= IEEE80211_CHAN_A
| IEEE80211_CHAN_HT40
;
1079 /* find the center channel */
1081 cent
= ieee80211_find_channel_byieee(ic
, i
,
1082 flags
& ~IEEE80211_CHAN_HT
);
1085 printf("%s: skip channel %d, could not find center channel\n",
1086 device_get_nameunit(sc
->dev
), i
);
1090 /* find the extension channel */
1092 ext
= ieee80211_find_channel(ic
, cent
->ic_freq
+ 20,
1093 flags
& ~IEEE80211_CHAN_HT
);
1096 printf("%s: skip channel %d, could not find extension channel\n",
1097 device_get_nameunit(sc
->dev
), i
);
1101 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
1104 c
->ic_extieee
= ext
->ic_ieee
;
1105 c
->ic_flags
&= ~IEEE80211_CHAN_HT
;
1106 c
->ic_flags
|= IEEE80211_CHAN_HT40U
;
1108 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
1111 c
->ic_extieee
= cent
->ic_ieee
;
1112 c
->ic_flags
&= ~IEEE80211_CHAN_HT
;
1113 c
->ic_flags
|= IEEE80211_CHAN_HT40D
;
1116 for (i
= 149; i
<= 165; i
+= 4)
1118 flags
= IEEE80211_CHAN_A
| IEEE80211_CHAN_HT40
;
1120 /* find the center channel */
1122 cent
= ieee80211_find_channel_byieee(ic
, i
,
1123 flags
& ~IEEE80211_CHAN_HT
);
1126 printf("%s: skip channel %d, could not find center channel\n",
1127 device_get_nameunit(sc
->dev
), i
);
1131 /* find the extension channel */
1133 ext
= ieee80211_find_channel(ic
, cent
->ic_freq
+ 20,
1134 flags
& ~IEEE80211_CHAN_HT
);
1137 printf("%s: skip channel %d, could not find extension channel\n",
1138 device_get_nameunit(sc
->dev
), i
);
1142 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
1145 c
->ic_extieee
= ext
->ic_ieee
;
1146 c
->ic_flags
&= ~IEEE80211_CHAN_HT
;
1147 c
->ic_flags
|= IEEE80211_CHAN_HT40U
;
1149 c
= &ic
->ic_channels
[ic
->ic_nchans
++];
1152 c
->ic_extieee
= cent
->ic_ieee
;
1153 c
->ic_flags
&= ~IEEE80211_CHAN_HT
;
1154 c
->ic_flags
|= IEEE80211_CHAN_HT40D
;
1160 * rt2870_init_locked
1162 static void rt2870_init_locked(void *priv
)
1164 struct rt2870_softc
*sc
;
1165 struct ieee80211com
*ic
;
1167 struct rt2870_softc_rx_data
*data
;
1168 int ntries
, error
, i
;
1169 uint32_t tmp
, stacnt
[6];
1175 RT2870_DPRINTF(sc
, RT2870_DEBUG_ANY
,
1176 "%s: initializing\n",
1177 device_get_nameunit(sc
->dev
));
1179 if (!(sc
->flags
& RT2870_SOFTC_FLAGS_UCODE_LOADED
))
1181 RT2870_DPRINTF(sc
, RT2870_DEBUG_ANY
,
1182 "%s: loading 8051 microcode\n",
1183 device_get_nameunit(sc
->dev
));
1185 error
= rt2870_io_mcu_load_ucode(sc
, rt2870_ucode
, sizeof(rt2870_ucode
));
1188 printf("%s: could not load 8051 microcode\n",
1189 device_get_nameunit(sc
->dev
));
1193 RT2870_DPRINTF(sc
, RT2870_DEBUG_ANY
,
1194 "%s: 8051 microcode was successfully loaded\n",
1195 device_get_nameunit(sc
->dev
));
1197 sc
->flags
|= RT2870_SOFTC_FLAGS_UCODE_LOADED
;
1200 /* wait while DMA engine is busy */
1202 for (ntries
= 0; ntries
< 100; ntries
++)
1204 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_SCHDMA_WPDMA_GLO_CFG
);
1205 if (!(tmp
& (RT2870_REG_TX_DMA_BUSY
| RT2870_REG_RX_DMA_BUSY
)))
1213 printf("%s: timeout waiting for DMA engine\n",
1214 device_get_nameunit(sc
->dev
));
1219 tmp
|= RT2870_REG_TX_WB_DDONE
;
1221 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_WPDMA_GLO_CFG
, tmp
);
1223 /* PBF hardware reset */
1225 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_PBF_SYS_CTRL
);
1229 rt2870_io_mac_write(sc
, RT2870_REG_PBF_SYS_CTRL
, tmp
);
1231 rt2870_io_mac_write(sc
, RT2870_REG_SYS_CTRL
,
1232 RT2870_REG_MAC_SRST
| RT2870_REG_BBP_HRST
);
1234 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_USB_DMA_CFG
, 0);
1236 rt2870_io_mcu_reset(sc
);
1238 rt2870_io_mac_write(sc
, RT2870_REG_SYS_CTRL
, 0);
1240 /* init Tx power per rate */
1242 for (i
= 0; i
< RT2870_SOFTC_TXPOW_RATE_COUNT
; i
++)
1244 if (sc
->txpow_rate_20mhz
[i
] == 0xffffffff)
1247 rt2870_io_mac_write(sc
, RT2870_REG_TX_PWR_CFG(i
),
1248 sc
->txpow_rate_20mhz
[i
]);
1251 for (i
= 0; i
< RT2870_DEF_MAC_SIZE
; i
++)
1252 rt2870_io_mac_write(sc
, rt2870_def_mac
[i
].reg
,
1253 rt2870_def_mac
[i
].val
);
1255 /* wait while MAC is busy */
1257 for (ntries
= 0; ntries
< 100; ntries
++)
1259 if (!(rt2870_io_mac_read(sc
, RT2870_REG_STATUS_CFG
) &
1260 (RT2870_REG_STATUS_TX_BUSY
| RT2870_REG_STATUS_RX_BUSY
)))
1268 printf("%s: timeout waiting for MAC\n",
1269 device_get_nameunit(sc
->dev
));
1273 /* clear Host to MCU mailbox */
1275 rt2870_io_mac_write(sc
, RT2870_REG_H2M_MAILBOX_BBP_AGENT
, 0);
1276 rt2870_io_mac_write(sc
, RT2870_REG_H2M_MAILBOX
, 0);
1280 error
= rt2870_init_bbp(sc
);
1284 /* set up maximum buffer sizes */
1286 tmp
= (1 << 12) | RT2870_MAX_AGG_SIZE
;
1288 if (sc
->mac_rev
>= 0x28720200 && sc
->mac_rev
< 0x30700200)
1294 rt2870_io_mac_write(sc
, RT2870_REG_MAX_LEN_CFG
, tmp
);
1296 /* set mac address */
1298 IEEE80211_ADDR_COPY(ic
->ic_myaddr
, IF_LLADDR(ifp
));
1300 rt2870_asic_set_macaddr(sc
, ic
->ic_myaddr
);
1302 /* clear statistic registers */
1304 rt2870_io_mac_read_multi(sc
, RT2870_REG_RX_STA_CNT0
,
1305 stacnt
, sizeof(stacnt
));
1307 /* send LEDs operating mode to microcontroller */
1309 rt2870_io_mcu_cmd(sc
, RT2870_IO_MCU_CMD_LED1
,
1310 RT2870_REG_H2M_TOKEN_NO_INTR
, sc
->led_off
[0]);
1311 rt2870_io_mcu_cmd(sc
, RT2870_IO_MCU_CMD_LED2
,
1312 RT2870_REG_H2M_TOKEN_NO_INTR
, sc
->led_off
[1]);
1313 rt2870_io_mcu_cmd(sc
, RT2870_IO_MCU_CMD_LED3
,
1314 RT2870_REG_H2M_TOKEN_NO_INTR
, sc
->led_off
[2]);
1316 /* write vendor-specific BBP values (from EEPROM) */
1318 for (i
= 0; i
< RT2870_SOFTC_BBP_EEPROM_COUNT
; i
++)
1320 if (sc
->bbp_eeprom
[i
].reg
== 0x00 ||
1321 sc
->bbp_eeprom
[i
].reg
== 0xff)
1324 rt2870_io_bbp_write(sc
, sc
->bbp_eeprom
[i
].reg
,
1325 sc
->bbp_eeprom
[i
].val
);
1328 /* disable non-existing Rx chains */
1330 tmp
= rt2870_io_bbp_read(sc
, 3);
1332 tmp
&= ~((1 << 4) | (1 << 3));
1334 if (sc
->nrxpath
== 3)
1336 else if (sc
->nrxpath
== 2)
1339 rt2870_io_bbp_write(sc
, 3, tmp
);
1341 /* disable non-existing Tx chains */
1343 tmp
= rt2870_io_bbp_read(sc
, 1);
1345 tmp
&= ~((1 << 4) | (1 << 3));
1347 if (sc
->ntxpath
== 2)
1350 rt2870_io_bbp_write(sc
, 1, tmp
);
1352 /* set current channel */
1354 rt2870_rf_set_chan(sc
, ic
->ic_curchan
);
1356 /* turn radio LED on */
1358 rt2870_led_cmd(sc
, RT2870_LED_CMD_RADIO_ON
);
1360 rt2870_io_mcu_cmd(sc
, RT2870_IO_MCU_CMD_BOOT
,
1361 RT2870_REG_H2M_TOKEN_NO_INTR
, 0);
1363 /* set RTS threshold */
1365 rt2870_asic_update_rtsthreshold(sc
);
1369 rt2870_asic_update_txpower(sc
);
1371 /* set up protection mode */
1373 rt2870_asic_updateprot(sc
);
1375 /* clear key tables */
1377 rt2870_asic_clear_keytables(sc
);
1379 /* clear beacon frame space (entries = 8, entry size = 512) */
1381 rt2870_io_mac_set_region_4(sc
, RT2870_REG_BEACON_BASE(0), 0, 1024);
1383 /* enable Tx/Rx DMA engine */
1385 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_SCHDMA_USB_CYC_CFG
);
1390 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_USB_CYC_CFG
, tmp
);
1392 if ((sc
->mac_rev
& 0xffff) != 0x0101)
1393 rt2870_io_mac_write(sc
, RT2870_REG_TX_TXOP_CTRL_CFG
, 0x583f);
1395 rt2870_io_mac_write(sc
, RT2870_REG_SYS_CTRL
, RT2870_REG_TX_ENABLE
);
1397 for (ntries
= 0; ntries
< 200; ntries
++)
1399 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_SCHDMA_WPDMA_GLO_CFG
);
1400 if (!(tmp
& (RT2870_REG_TX_DMA_BUSY
| RT2870_REG_RX_DMA_BUSY
)))
1408 printf("%s: timeout waiting for DMA engine\n",
1409 device_get_nameunit(sc
->dev
));
1415 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_WMM_TXOP0_CFG
, 0);
1416 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_WMM_TXOP1_CFG
,
1419 tmp
|= RT2870_REG_TX_WB_DDONE
|
1420 RT2870_REG_RX_DMA_ENABLE
|
1421 RT2870_REG_TX_DMA_ENABLE
;
1423 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_WPDMA_GLO_CFG
, tmp
);
1425 tmp
= RT2870_REG_USB_DMA_TX_ENABLE
|
1426 RT2870_REG_USB_DMA_RX_ENABLE
|
1427 RT2870_REG_USB_DMA_RX_AGG_ENABLE
|
1428 /* Rx agg limit in unit of 1024 byte */
1429 ((RT2870_USB_RX_BULK_BUFLEN
/ 1024 - 3) << RT2870_REG_USB_DMA_RX_AGG_LIMIT_SHIFT
) |
1430 /* Rx agg timeout in unit of 33ns */
1433 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_USB_DMA_CFG
, tmp
);
1437 tmp
= RT2870_REG_RX_FILTER_DROP_CRC_ERR
|
1438 RT2870_REG_RX_FILTER_DROP_PHY_ERR
;
1440 if (ic
->ic_opmode
!= IEEE80211_M_MONITOR
)
1442 tmp
|= RT2870_REG_RX_FILTER_DROP_DUPL
|
1443 RT2870_REG_RX_FILTER_DROP_CTS
|
1444 RT2870_REG_RX_FILTER_DROP_BA
|
1445 RT2870_REG_RX_FILTER_DROP_ACK
|
1446 RT2870_REG_RX_FILTER_DROP_VER_ERR
|
1447 RT2870_REG_RX_FILTER_DROP_CTRL_RSV
|
1448 RT2870_REG_RX_FILTER_DROP_CFACK
|
1449 RT2870_REG_RX_FILTER_DROP_CFEND
;
1451 if (ic
->ic_opmode
== IEEE80211_M_STA
)
1452 tmp
|= RT2870_REG_RX_FILTER_DROP_RTS
|
1453 RT2870_REG_RX_FILTER_DROP_PSPOLL
;
1455 if (!(ifp
->if_flags
& IFF_PROMISC
))
1456 tmp
|= RT2870_REG_RX_FILTER_DROP_UC_NOME
;
1459 rt2870_io_mac_write(sc
, RT2870_REG_RX_FILTER_CFG
, tmp
);
1461 rt2870_io_mac_write(sc
, RT2870_REG_SYS_CTRL
,
1462 RT2870_REG_RX_ENABLE
| RT2870_REG_TX_ENABLE
);
1464 /* clear garbage interrupts */
1466 tmp
= rt2870_io_mac_read(sc
, 0x1300);
1468 taskqueue_unblock(sc
->taskqueue
);
1470 /* init Tx and Rx rings */
1472 for(i
= 0; i
< sc
->usb_endpoints
- 1; i
++)
1473 rt2870_reset_tx_ring(sc
, &sc
->tx_ring
[i
]);
1475 rt2870_reset_rx_ring(sc
, &sc
->rx_ring
);
1477 rt2870_reset_cmd_ring(sc
, &sc
->cmd_ring
);
1479 /* start up the receive pipe */
1481 for (i
= 0; i
< RT2870_SOFTC_RX_RING_DATA_COUNT
; i
++)
1483 data
= &sc
->rx_ring
.data
[i
];
1485 STAILQ_INSERT_TAIL(&sc
->rx_ring
.active
, data
, next
);
1487 usbd_setup_xfer(data
->xfer
, sc
->rx_ring
.usb_pipe
, sc
, data
->buf
,
1488 RT2870_USB_RX_BULK_BUFLEN
, USBD_SHORT_XFER_OK
| USBD_NO_COPY
,
1489 USBD_NO_TIMEOUT
, rt2870_rx_intr
);
1491 usbd_transfer(data
->xfer
);
1494 ifp
->if_drv_flags
&= ~IFF_DRV_OACTIVE
;
1495 ifp
->if_drv_flags
|= IFF_DRV_RUNNING
;
1497 if (ic
->ic_opmode
!= IEEE80211_M_MONITOR
)
1499 if (ic
->ic_roaming
!= IEEE80211_ROAMING_MANUAL
)
1500 ieee80211_new_state(ic
, IEEE80211_S_SCAN
, -1);
1504 ieee80211_new_state(ic
, IEEE80211_S_RUN
, -1);
1507 sc
->periodic_round
= 0;
1509 callout_reset(&sc
->periodic_ch
, hz
/ 10, rt2870_periodic
, sc
);
1515 rt2870_stop_locked(sc
);
1521 static void rt2870_init(void *priv
)
1523 struct rt2870_softc
*sc
;
1527 rt2870_init_locked(sc
);
1533 static int rt2870_init_bbp(struct rt2870_softc
*sc
)
1538 for (ntries
= 0; ntries
< 20; ntries
++)
1540 tmp
= rt2870_io_bbp_read(sc
, 0);
1541 if (tmp
!= 0x00 && tmp
!= 0xff)
1545 if (tmp
== 0x00 || tmp
== 0xff)
1547 printf("%s: timeout waiting for BBP to wakeup\n",
1548 device_get_nameunit(sc
->dev
));
1552 for (i
= 0; i
< RT2870_DEF_BBP_SIZE
; i
++)
1553 rt2870_io_bbp_write(sc
, rt2870_def_bbp
[i
].reg
,
1554 rt2870_def_bbp
[i
].val
);
1556 if ((sc
->mac_rev
& 0xffff) != 0x0101)
1557 rt2870_io_bbp_write(sc
, 84, 0x19);
1565 static void rt2870_stop_locked(void *priv
)
1567 struct rt2870_softc
*sc
;
1568 struct ieee80211com
*ic
;
1577 RT2870_DPRINTF(sc
, RT2870_DEBUG_ANY
,
1579 device_get_nameunit(sc
->dev
));
1583 if (ifp
->if_drv_flags
& IFF_DRV_RUNNING
)
1584 rt2870_led_cmd(sc
, RT2870_LED_CMD_RADIO_OFF
);
1586 ifp
->if_drv_flags
&= ~(IFF_DRV_RUNNING
| IFF_DRV_OACTIVE
);
1588 ieee80211_new_state(ic
, IEEE80211_S_INIT
, -1);
1590 callout_stop(&sc
->periodic_ch
);
1591 callout_stop(&sc
->tx_watchdog_ch
);
1593 taskqueue_block(sc
->taskqueue
);
1595 taskqueue_drain(sc
->taskqueue
, &sc
->rx_done_task
);
1596 taskqueue_drain(sc
->taskqueue
, &sc
->tx_done_task
);
1597 taskqueue_drain(sc
->taskqueue
, &sc
->periodic_task
);
1598 taskqueue_drain(sc
->taskqueue
, &sc
->cmd_task
);
1600 /* clear key tables */
1602 rt2870_asic_clear_keytables(sc
);
1606 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_SYS_CTRL
);
1608 tmp
&= ~(RT2870_REG_RX_ENABLE
| RT2870_REG_TX_ENABLE
);
1610 rt2870_io_mac_write(sc
, RT2870_REG_SYS_CTRL
, tmp
);
1612 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_USB_DMA_CFG
, 0);
1616 rt2870_io_mac_write(sc
, RT2870_REG_SYS_CTRL
,
1617 RT2870_REG_MAC_SRST
| RT2870_REG_BBP_HRST
);
1618 rt2870_io_mac_write(sc
, RT2870_REG_SYS_CTRL
, 0);
1620 /* abort any pending transfers */
1622 if (sc
->rx_ring
.usb_pipe
!= NULL
)
1623 usbd_abort_pipe(sc
->rx_ring
.usb_pipe
);
1625 for (i
= 0; i
< sc
->usb_endpoints
- 1; i
++)
1626 if (sc
->tx_ring
[i
].usb_pipe
!= NULL
)
1627 usbd_abort_pipe(sc
->tx_ring
[i
].usb_pipe
);
1633 static void rt2870_stop(void *priv
)
1635 struct rt2870_softc
*sc
;
1639 rt2870_stop_locked(sc
);
1645 static void rt2870_start(struct ifnet
*ifp
)
1647 struct rt2870_softc
*sc
;
1648 struct ieee80211com
*ic
;
1649 struct ieee80211_node
*ni
;
1650 struct ether_header
*eh
;
1657 if (!(ifp
->if_drv_flags
& IFF_DRV_RUNNING
))
1662 IF_POLL(&ic
->ic_mgtq
, m
);
1665 if (sc
->tx_ring
[sc
->tx_ring_mgtqid
].queued
>= RT2870_SOFTC_TX_RING_DATA_COUNT
)
1667 RT2870_DPRINTF(sc
, RT2870_DEBUG_TX
,
1668 "%s: if_start: Tx ring with qid=%d is full\n",
1669 device_get_nameunit(sc
->dev
), sc
->tx_ring_mgtqid
);
1671 ifp
->if_drv_flags
|= IFF_DRV_OACTIVE
;
1673 sc
->tx_data_queue_full
[sc
->tx_ring_mgtqid
]++;
1678 IF_DEQUEUE(&ic
->ic_mgtq
, m
);
1680 ni
= (struct ieee80211_node
*) m
->m_pkthdr
.rcvif
;
1681 m
->m_pkthdr
.rcvif
= NULL
;
1683 if (bpf_peers_present(ic
->ic_rawbpf
))
1684 bpf_mtap(ic
->ic_rawbpf
, m
);
1686 if (rt2870_tx_frame(sc
, m
, ni
, sc
->tx_ring_mgtqid
) != 0)
1691 if (ic
->ic_state
!= IEEE80211_S_RUN
)
1694 IF_POLL(&ifp
->if_snd
, m
);
1698 IFQ_DRV_DEQUEUE(&ifp
->if_snd
, m
);
1700 if (ic
->ic_flags
& IEEE80211_F_SCAN
)
1701 ieee80211_cancel_scan(ic
);
1703 if (m
->m_len
< sizeof(struct ether_header
) &&
1704 !(m
= m_pullup(m
, sizeof (struct ether_header
))))
1707 eh
= mtod(m
, struct ether_header
*);
1709 ni
= ieee80211_find_txnode(ic
, eh
->ether_dhost
);
1712 RT2870_DPRINTF(sc
, RT2870_DEBUG_TX
,
1713 "%s: if_start: could not find Tx node\n",
1714 device_get_nameunit(sc
->dev
));
1721 ieee80211_classify(ic
, m
, ni
);
1723 qid
= M_WME_GETAC(m
);
1725 if (sc
->tx_ring
[qid
].queued
>= RT2870_SOFTC_TX_RING_DATA_COUNT
)
1727 RT2870_DPRINTF(sc
, RT2870_DEBUG_TX
,
1728 "%s: if_start: Tx ring with qid=%d is full\n",
1729 device_get_nameunit(sc
->dev
), qid
);
1732 ieee80211_free_node(ni
);
1734 ifp
->if_drv_flags
|= IFF_DRV_OACTIVE
;
1737 sc
->tx_data_queue_full
[qid
]++;
1744 m
= ieee80211_encap(ic
, m
, ni
);
1747 ieee80211_free_node(ni
);
1754 if (bpf_peers_present(ic
->ic_rawbpf
))
1755 bpf_mtap(ic
->ic_rawbpf
, m
);
1757 if (rt2870_tx_frame(sc
, m
, ni
, qid
) != 0)
1759 ieee80211_free_node(ni
);
1761 ifp
->if_drv_flags
|= IFF_DRV_OACTIVE
;
1768 sc
->tx_timer
= RT2870_TX_WATCHDOG_TIMEOUT
;
1770 ic
->ic_lastdata
= ticks
;
1772 callout_reset(&sc
->tx_watchdog_ch
, hz
, rt2870_tx_watchdog
, sc
);
1779 static int rt2870_ioctl(struct ifnet
*ifp
, u_long cmd
, caddr_t data
)
1781 struct rt2870_softc
*sc
;
1782 struct ieee80211com
*ic
;
1793 if (ifp
->if_flags
& IFF_UP
)
1795 if (ifp
->if_drv_flags
& IFF_DRV_RUNNING
)
1797 if ((ifp
->if_flags
^ sc
->if_flags
) & IFF_PROMISC
)
1798 rt2870_asic_update_promisc(sc
);
1802 rt2870_init_locked(sc
);
1807 if (ifp
->if_drv_flags
& IFF_DRV_RUNNING
)
1808 rt2870_stop_locked(sc
);
1811 sc
->if_flags
= ifp
->if_flags
;
1815 error
= ieee80211_ioctl(ic
, cmd
, data
);
1818 if (error
== ENETRESET
)
1820 if ((ifp
->if_flags
& IFF_UP
) &&
1821 (ifp
->if_drv_flags
& IFF_DRV_RUNNING
) &&
1822 (ic
->ic_roaming
!= IEEE80211_ROAMING_MANUAL
))
1824 rt2870_stop_locked(sc
);
1825 rt2870_init_locked(sc
);
1837 static int rt2870_reset(struct ifnet
*ifp
)
1839 struct rt2870_softc
*sc
;
1840 struct ieee80211com
*ic
;
1845 if (ic
->ic_opmode
!= IEEE80211_M_MONITOR
)
1848 rt2870_rf_set_chan(sc
, ic
->ic_curchan
);
1856 static int rt2870_newstate(struct ieee80211com
*ic
,
1857 enum ieee80211_state nstate
, int arg
)
1859 struct rt2870_softc
*sc
;
1861 struct rt2870_cmd_argv_newstate cmd_argv
;
1866 cmd_argv
.nstate
= nstate
;
1869 rt2870_do_async(sc
, rt2870_newstate_cb
, &cmd_argv
,
1870 sizeof(struct rt2870_cmd_argv_newstate
));
1878 static void rt2870_scan_start(struct ieee80211com
*ic
)
1880 struct rt2870_softc
*sc
;
1886 rt2870_do_async(sc
, rt2870_scan_start_cb
, NULL
, 0);
1892 static void rt2870_scan_end(struct ieee80211com
*ic
)
1894 struct rt2870_softc
*sc
;
1900 rt2870_do_async(sc
, rt2870_scan_end_cb
, NULL
, 0);
1904 * rt2870_set_channel
1906 static void rt2870_set_channel(struct ieee80211com
*ic
)
1908 struct rt2870_softc
*sc
;
1914 rt2870_do_async(sc
, rt2870_set_channel_cb
, NULL
, 0);
1920 static void rt2870_newassoc(struct ieee80211_node
*ni
, int isnew
)
1922 struct rt2870_softc
*sc
;
1923 struct ieee80211com
*ic
;
1925 struct rt2870_cmd_argv_newassoc cmd_argv
;
1931 cmd_argv
.associd
= (ni
!= NULL
) ? ni
->ni_associd
: 0;
1932 cmd_argv
.isnew
= isnew
;
1933 memcpy(cmd_argv
.macaddr
, ni
->ni_macaddr
, IEEE80211_ADDR_LEN
);
1935 rt2870_do_async(sc
, rt2870_newassoc_cb
, &cmd_argv
,
1936 sizeof(struct rt2870_cmd_argv_newassoc
));
1942 static void rt2870_updateslot(struct ifnet
*ifp
)
1944 struct rt2870_softc
*sc
;
1948 rt2870_do_async(sc
, rt2870_updateslot_cb
, NULL
, 0);
1954 static int rt2870_wme_update(struct ieee80211com
*ic
)
1956 struct rt2870_softc
*sc
;
1962 rt2870_do_async(sc
, rt2870_wme_update_cb
, NULL
, 0);
1968 * rt2870_update_beacon
1970 static void rt2870_update_beacon(struct ieee80211com
*ic
, int what
)
1972 struct rt2870_softc
*sc
;
1974 struct rt2870_cmd_argv_updatebeacon cmd_argv
;
1979 cmd_argv
.what
= what
;
1981 rt2870_do_async(sc
, rt2870_update_beacon_cb
, &cmd_argv
,
1982 sizeof(struct rt2870_cmd_argv_updatebeacon
));
1986 * rt2870_key_update_begin
1988 static void rt2870_key_update_begin(struct ieee80211com
*ic
)
1990 struct rt2870_softc
*sc
;
1996 rt2870_do_async(sc
, rt2870_key_update_begin_cb
, NULL
, 0);
2000 * rt2870_key_update_end
2002 static void rt2870_key_update_end(struct ieee80211com
*ic
)
2004 struct rt2870_softc
*sc
;
2010 rt2870_do_async(sc
, rt2870_key_update_end_cb
, NULL
, 0);
2016 static int rt2870_key_set(struct ieee80211com
*ic
,
2017 const struct ieee80211_key
*k
, const uint8_t mac
[IEEE80211_ADDR_LEN
])
2019 struct rt2870_softc
*sc
;
2021 struct ieee80211_node
*ni
;
2022 struct rt2870_cmd_argv_keyset cmd_argv
;
2024 if (k
->wk_cipher
->ic_cipher
!= IEEE80211_CIPHER_WEP
&&
2025 k
->wk_cipher
->ic_cipher
!= IEEE80211_CIPHER_TKIP
&&
2026 k
->wk_cipher
->ic_cipher
!= IEEE80211_CIPHER_AES_CCM
)
2032 if (ic
->ic_opmode
!= IEEE80211_M_HOSTAP
)
2035 ni
= ieee80211_find_node(&ic
->ic_sta
, mac
);
2037 memcpy(&cmd_argv
.key
, k
, sizeof(struct ieee80211_key
));
2038 cmd_argv
.associd
= (ni
!= NULL
) ? ni
->ni_associd
: 0;
2040 if ((ic
->ic_opmode
== IEEE80211_M_HOSTAP
) && (ni
!= NULL
))
2041 ieee80211_free_node(ni
);
2043 rt2870_do_async(sc
, rt2870_key_set_cb
, &cmd_argv
,
2044 sizeof(struct rt2870_cmd_argv_keyset
));
2052 static int rt2870_key_delete(struct ieee80211com
*ic
,
2053 const struct ieee80211_key
*k
)
2055 struct rt2870_softc
*sc
;
2057 struct ieee80211_node
*ni
;
2058 struct rt2870_cmd_argv_keydelete cmd_argv
;
2064 memcpy(&cmd_argv
.key
, k
, sizeof(struct ieee80211_key
));
2065 cmd_argv
.associd
= (ni
!= NULL
) ? ni
->ni_associd
: 0;
2067 rt2870_do_async(sc
, rt2870_key_delete_cb
, &cmd_argv
,
2068 sizeof(struct rt2870_cmd_argv_keydelete
));
2076 static int rt2870_raw_xmit(struct ieee80211_node
*ni
, struct mbuf
*m
,
2077 const struct ieee80211_bpf_params
*params
)
2079 struct ieee80211com
*ic
;
2081 struct rt2870_softc
*sc
;
2091 * rt2870_media_change
2093 static int rt2870_media_change(struct ifnet
*ifp
)
2095 struct rt2870_softc
*sc
;
2100 error
= ieee80211_media_change(ifp
);
2101 if (error
!= ENETRESET
)
2104 if ((ifp
->if_flags
& IFF_UP
) && (ifp
->if_drv_flags
& IFF_DRV_RUNNING
))
2114 * rt2870_recv_action
2116 static void rt2870_recv_action(struct ieee80211_node
*ni
,
2117 const uint8_t *frm
, const uint8_t *efrm
)
2119 struct rt2870_softc
*sc
;
2120 struct ieee80211com
*ic
;
2122 const struct ieee80211_action
*ia
;
2123 uint16_t associd
, baparamset
;
2132 ia
= (const struct ieee80211_action
*) frm
;
2134 sc
->recv_action(ni
, frm
, efrm
);
2136 if (ia
->ia_category
!= IEEE80211_ACTION_CAT_BA
)
2139 associd
= (ni
!= NULL
) ? ni
->ni_associd
: 0;
2140 wcid
= RT2870_AID2WCID(associd
);
2142 switch (ia
->ia_action
)
2144 /* IEEE80211_ACTION_BA_ADDBA_REQUEST */
2145 case IEEE80211_ACTION_BA_ADDBA_REQUEST
:
2146 baparamset
= LE_READ_2(frm
+ 3);
2147 tid
= RT2870_MS(baparamset
, IEEE80211_BAPS_TID
);
2148 bufsize
= RT2870_MS(baparamset
, IEEE80211_BAPS_BUFSIZ
);
2150 RT2870_DPRINTF(sc
, RT2870_DEBUG_BA
,
2151 "%s: received ADDBA: associd=0x%04x, tid=%d, bufsize=%d\n",
2152 device_get_nameunit(sc
->dev
), associd
, tid
, bufsize
);
2154 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_WCID(wcid
) + 4);
2156 tmp
|= (0x10000 << tid
);
2158 rt2870_io_mac_write(sc
, RT2870_REG_WCID(wcid
) + 4, tmp
);
2161 /* IEEE80211_ACTION_BA_DELBA */
2162 case IEEE80211_ACTION_BA_DELBA
:
2163 baparamset
= LE_READ_2(frm
+ 2);
2164 tid
= RT2870_MS(baparamset
, IEEE80211_BAPS_TID
);
2166 RT2870_DPRINTF(sc
, RT2870_DEBUG_BA
,
2167 "%s: received DELBA: associd=0x%04x, tid=%d\n",
2168 device_get_nameunit(sc
->dev
), associd
, tid
);
2170 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_WCID(wcid
) + 4);
2172 tmp
&= ~(0x10000 << tid
);
2174 rt2870_io_mac_write(sc
, RT2870_REG_WCID(wcid
) + 4, tmp
);
2180 * rt2870_send_action
2182 static int rt2870_send_action(struct ieee80211_node
*ni
,
2183 int category
, int action
, uint16_t args
[4])
2185 struct rt2870_softc
*sc
;
2186 struct ieee80211com
*ic
;
2188 uint16_t associd
, baparamset
;
2197 ret
= sc
->send_action(ni
, category
, action
, args
);
2199 if (category
!= IEEE80211_ACTION_CAT_BA
)
2202 associd
= (ni
!= NULL
) ? ni
->ni_associd
: 0;
2203 wcid
= RT2870_AID2WCID(associd
);
2207 /* IEEE80211_ACTION_BA_DELBA */
2208 case IEEE80211_ACTION_BA_DELBA
:
2209 baparamset
= RT2870_SM(args
[0], IEEE80211_DELBAPS_TID
) |
2210 RT2870_SM(args
[1], IEEE80211_DELBAPS_INIT
);
2212 if (RT2870_MS(baparamset
, IEEE80211_DELBAPS_INIT
) == IEEE80211_DELBAPS_INIT
)
2215 tid
= RT2870_MS(baparamset
, IEEE80211_DELBAPS_TID
);
2217 RT2870_DPRINTF(sc
, RT2870_DEBUG_BA
,
2218 "%s: sending DELBA: associd=0x%04x, tid=%d\n",
2219 device_get_nameunit(sc
->dev
), associd
, tid
);
2221 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_WCID(wcid
) + 4);
2223 tmp
&= ~(0x10000 << tid
);
2225 rt2870_io_mac_write(sc
, RT2870_REG_WCID(wcid
) + 4, tmp
);
2233 * rt2870_amrr_update_iter_func
2235 static void rt2870_amrr_update_iter_func(void *arg
, struct ieee80211_node
*ni
)
2237 struct rt2870_softc
*sc
;
2238 struct ieee80211com
*ic
;
2244 /* only associated stations */
2246 if (ni
->ni_associd
!= 0)
2248 wcid
= RT2870_AID2WCID(ni
->ni_associd
);
2250 RT2870_DPRINTF(sc
, RT2870_DEBUG_RATE
,
2251 "%s: AMRR node: wcid=0x%02x, txcnt=%d, success=%d, retrycnt=%d\n",
2252 device_get_nameunit(sc
->dev
),
2253 wcid
, sc
->amrr_node
[wcid
].txcnt
, sc
->amrr_node
[wcid
].success
, sc
->amrr_node
[wcid
].retrycnt
);
2255 rt2870_amrr_choose(ni
, &sc
->amrr_node
[wcid
]);
2257 RT2870_DPRINTF(sc
, RT2870_DEBUG_RATE
,
2258 "%s:%s node Tx rate: associd=0x%04x, rate=0x%02x, max rate=0x%02x\n",
2259 device_get_nameunit(sc
->dev
),
2260 (ni
->ni_flags
& IEEE80211_NODE_HT
) ? " HT" : "",
2262 (ni
->ni_flags
& IEEE80211_NODE_HT
) ?
2263 (ni
->ni_htrates
.rs_rates
[ni
->ni_txrate
] | IEEE80211_RATE_MCS
) :
2264 (ni
->ni_rates
.rs_rates
[ni
->ni_txrate
] & IEEE80211_RATE_VAL
),
2265 (ni
->ni_flags
& IEEE80211_NODE_HT
) ?
2266 (ni
->ni_htrates
.rs_rates
[ni
->ni_htrates
.rs_nrates
- 1] | IEEE80211_RATE_MCS
) :
2267 (ni
->ni_rates
.rs_rates
[ni
->ni_rates
.rs_nrates
- 1] & IEEE80211_RATE_VAL
));
2274 static void rt2870_periodic(void *arg
)
2276 struct rt2870_softc
*sc
;
2282 RT2870_DPRINTF(sc
, RT2870_DEBUG_PERIODIC
,
2284 device_get_nameunit(sc
->dev
));
2286 if (!(ifp
->if_drv_flags
& IFF_DRV_RUNNING
))
2289 taskqueue_enqueue(sc
->taskqueue
, &sc
->periodic_task
);
2293 * rt2870_tx_watchdog
2295 static void rt2870_tx_watchdog(void *arg
)
2297 struct rt2870_softc
*sc
;
2303 if (sc
->tx_timer
== 0)
2306 if (--sc
->tx_timer
== 0)
2308 printf("%s: Tx watchdog timeout: resetting\n",
2309 device_get_nameunit(sc
->dev
));
2311 rt2870_stop_locked(sc
);
2312 rt2870_init_locked(sc
);
2316 sc
->tx_watchdog_timeouts
++;
2319 callout_reset(&sc
->tx_watchdog_ch
, hz
, rt2870_tx_watchdog
, sc
);
2325 static int rt2870_do_async(struct rt2870_softc
*sc
,
2326 void (*cb
)(struct rt2870_softc
*sc
, void *arg
),
2330 struct rt2870_softc_cmd_ring
*ring
;
2331 struct rt2870_softc_cmd
*cmd
;
2335 ring
= &sc
->cmd_ring
;
2337 RT2870_SOFTC_LOCK(sc
);
2339 if (!(ifp
->if_drv_flags
& IFF_DRV_RUNNING
))
2341 RT2870_SOFTC_UNLOCK(sc
);
2345 if (ring
->queued
>= RT2870_SOFTC_CMD_RING_CMD_COUNT
)
2347 RT2870_SOFTC_UNLOCK(sc
);
2351 cmd
= STAILQ_FIRST(&ring
->inactive
);
2352 STAILQ_REMOVE_HEAD(&ring
->inactive
, next
);
2357 memcpy(cmd
->data
, arg
, len
);
2359 STAILQ_INSERT_TAIL(&ring
->active
, cmd
, next
);
2362 run_cmd_task
= (ring
->queued
== 1);
2364 RT2870_SOFTC_UNLOCK(sc
);
2367 taskqueue_enqueue(sc
->taskqueue
, &sc
->cmd_task
);
2373 * rt2870_newstate_cb
2375 static void rt2870_newstate_cb(struct rt2870_softc
*sc
, void *arg
)
2377 struct ieee80211com
*ic
;
2378 enum ieee80211_state nstate
;
2379 struct ieee80211_node
*ni
;
2380 struct rt2870_cmd_argv_newstate
*cmd_argv
;
2385 nstate
= cmd_argv
->nstate
;
2387 RT2870_DPRINTF(sc
, RT2870_DEBUG_STATE
,
2388 "%s: newstate: %s -> %s\n",
2389 device_get_nameunit(sc
->dev
),
2390 ieee80211_state_name
[ic
->ic_state
], ieee80211_state_name
[nstate
]);
2392 error
= sc
->newstate(ic
, nstate
, cmd_argv
->arg
);
2396 /* turn link LED off */
2398 if (nstate
!= IEEE80211_S_RUN
)
2399 rt2870_led_cmd(sc
, RT2870_LED_CMD_RADIO_OFF
);
2403 case IEEE80211_S_INIT
:
2404 rt2870_asic_disable_tsf_sync(sc
);
2407 case IEEE80211_S_RUN
:
2410 rt2870_rf_set_chan(sc
, ni
->ni_chan
);
2412 if (ic
->ic_opmode
!= IEEE80211_M_MONITOR
)
2414 rt2870_asic_enable_mrr(sc
);
2415 rt2870_asic_set_txpreamble(sc
);
2416 rt2870_asic_set_basicrates(sc
);
2417 rt2870_asic_set_bssid(sc
, ni
->ni_bssid
);
2420 if (ic
->ic_opmode
== IEEE80211_M_STA
)
2421 rt2870_newassoc(ni
, 1);
2423 if (ic
->ic_opmode
== IEEE80211_M_HOSTAP
||
2424 ic
->ic_opmode
== IEEE80211_M_IBSS
)
2426 error
= rt2870_asic_update_beacon(sc
);
2431 if (ic
->ic_opmode
!= IEEE80211_M_MONITOR
)
2432 rt2870_asic_enable_tsf_sync(sc
);
2434 /* turn link LED on */
2436 if (ic
->ic_opmode
!= IEEE80211_M_MONITOR
)
2438 rt2870_led_cmd(sc
, RT2870_LED_CMD_RADIO_ON
|
2439 (IEEE80211_IS_CHAN_2GHZ(ni
->ni_chan
) ?
2440 RT2870_LED_CMD_LINK_2GHZ
: RT2870_LED_CMD_LINK_5GHZ
));
2450 * rt2870_scan_start_cb
2452 static void rt2870_scan_start_cb(struct rt2870_softc
*sc
, void *arg
)
2454 struct ieee80211com
*ic
;
2460 rt2870_asic_disable_tsf_sync(sc
);
2464 * rt2870_scan_end_cb
2466 static void rt2870_scan_end_cb(struct rt2870_softc
*sc
, void *arg
)
2468 struct ieee80211com
*ic
;
2469 struct ieee80211_node
*ni
;
2474 rt2870_asic_enable_tsf_sync(sc
);
2478 * rt2870_set_channel_cb
2480 static void rt2870_set_channel_cb(struct rt2870_softc
*sc
, void *arg
)
2482 struct ieee80211com
*ic
;
2486 RT2870_DPRINTF(sc
, RT2870_DEBUG_CHAN
,
2487 "%s: set channel: channel=%u, HT%s%s\n",
2488 device_get_nameunit(sc
->dev
),
2489 ieee80211_chan2ieee(ic
, ic
->ic_curchan
),
2490 !IEEE80211_IS_CHAN_HT(ic
->ic_curchan
) ? " disabled" :
2491 IEEE80211_IS_CHAN_HT20(ic
->ic_curchan
) ? "20":
2492 IEEE80211_IS_CHAN_HT40U(ic
->ic_curchan
) ? "40U" : "40D",
2493 (ic
->ic_flags
& IEEE80211_F_SCAN
) ? ", scanning" : "");
2495 rt2870_rf_set_chan(sc
, ic
->ic_curchan
);
2499 * rt2870_newassoc_cb
2501 static void rt2870_newassoc_cb(struct rt2870_softc
*sc
, void *arg
)
2503 struct ieee80211com
*ic
;
2504 struct ieee80211_node
*ni
;
2505 struct rt2870_cmd_argv_newassoc
*cmd_argv
;
2508 uint8_t *macaddr
, wcid
;
2513 associd
= cmd_argv
->associd
;
2514 isnew
= cmd_argv
->isnew
;
2515 macaddr
= cmd_argv
->macaddr
;
2517 wcid
= RT2870_AID2WCID(associd
);
2519 RT2870_DPRINTF(sc
, RT2870_DEBUG_NODE
,
2520 "%s: new association: wcid=0x%02x, "
2521 "mac addr=%s, QoS %s, ERP %s, HT %s\n",
2522 device_get_nameunit(sc
->dev
), wcid
,
2523 ether_sprintf(macaddr
),
2524 (ni
->ni_flags
& IEEE80211_NODE_QOS
) ? "enabled" : "disabled",
2525 (ni
->ni_flags
& IEEE80211_NODE_ERP
) ? "enabled" : "disabled",
2526 (ni
->ni_flags
& IEEE80211_NODE_HT
) ? "enabled" : "disabled");
2528 rt2870_io_mac_write_multi(sc
, RT2870_REG_WCID(wcid
),
2529 macaddr
, IEEE80211_ADDR_LEN
);
2531 rt2870_amrr_node_init(&sc
->amrr
, &sc
->amrr_node
[wcid
], ni
);
2533 RT2870_DPRINTF(sc
, RT2870_DEBUG_RATE
,
2534 "%s: initial%s node Tx rate: associd=0x%04x, rate=0x%02x, max rate=0x%02x\n",
2535 device_get_nameunit(sc
->dev
),
2536 (ni
->ni_flags
& IEEE80211_NODE_HT
) ? " HT" : "",
2538 (ni
->ni_flags
& IEEE80211_NODE_HT
) ?
2539 (ni
->ni_htrates
.rs_rates
[ni
->ni_txrate
] | IEEE80211_RATE_MCS
) :
2540 (ni
->ni_rates
.rs_rates
[ni
->ni_txrate
] & IEEE80211_RATE_VAL
),
2541 (ni
->ni_flags
& IEEE80211_NODE_HT
) ?
2542 (ni
->ni_htrates
.rs_rates
[ni
->ni_htrates
.rs_nrates
- 1] | IEEE80211_RATE_MCS
) :
2543 (ni
->ni_rates
.rs_rates
[ni
->ni_rates
.rs_nrates
- 1] & IEEE80211_RATE_VAL
));
2545 rt2870_asic_updateprot(sc
);
2546 rt2870_asic_updateslot(sc
);
2547 rt2870_asic_set_txpreamble(sc
);
2551 * rt2870_updateslot_cb
2553 static void rt2870_updateslot_cb(struct rt2870_softc
*sc
, void *arg
)
2555 rt2870_asic_updateslot(sc
);
2559 * rt2870_wme_update_cb
2561 static void rt2870_wme_update_cb(struct rt2870_softc
*sc
, void *arg
)
2563 rt2870_asic_wme_update(sc
);
2567 * rt2870_update_beacon_cb
2569 static void rt2870_update_beacon_cb(struct rt2870_softc
*sc
, void *arg
)
2571 struct rt2870_cmd_argv_updatebeacon
*cmd_argv
;
2575 RT2870_DPRINTF(sc
, RT2870_DEBUG_BEACON
,
2576 "%s: update beacon\n",
2577 device_get_nameunit(sc
->dev
));
2579 rt2870_asic_update_beacon(sc
);
2583 * rt2870_key_update_begin_cb
2585 static void rt2870_key_update_begin_cb(struct rt2870_softc
*sc
, void *arg
)
2591 RT2870_DPRINTF(sc
, RT2870_DEBUG_KEY
,
2592 "%s: key update begin\n",
2593 device_get_nameunit(sc
->dev
));
2595 taskqueue_block(sc
->taskqueue
);
2597 IF_LOCK(&ifp
->if_snd
);
2601 * rt2870_key_update_end_cb
2603 static void rt2870_key_update_end_cb(struct rt2870_softc
*sc
, void *arg
)
2609 RT2870_DPRINTF(sc
, RT2870_DEBUG_KEY
,
2610 "%s: key update end\n",
2611 device_get_nameunit(sc
->dev
));
2613 IF_UNLOCK(&ifp
->if_snd
);
2615 taskqueue_unblock(sc
->taskqueue
);
2621 static void rt2870_key_set_cb(struct rt2870_softc
*sc
, void *arg
)
2623 struct ieee80211com
*ic
;
2624 struct ieee80211_key
*k
;
2625 struct rt2870_cmd_argv_keyset
*cmd_argv
;
2626 uint16_t associd
, key_base
, keymode_base
;
2627 uint8_t mode
, vapid
, wcid
, iv
[8];
2633 associd
= cmd_argv
->associd
;
2635 switch (k
->wk_cipher
->ic_cipher
)
2637 case IEEE80211_CIPHER_WEP
:
2638 if(k
->wk_keylen
< 8)
2639 mode
= RT2870_REG_CIPHER_MODE_WEP40
;
2641 mode
= RT2870_REG_CIPHER_MODE_WEP104
;
2644 case IEEE80211_CIPHER_TKIP
:
2645 mode
= RT2870_REG_CIPHER_MODE_TKIP
;
2648 case IEEE80211_CIPHER_AES_CCM
:
2649 mode
= RT2870_REG_CIPHER_MODE_AES_CCMP
;
2656 RT2870_DPRINTF(sc
, RT2870_DEBUG_KEY
,
2657 "%s: set key: keyix=%d, keylen=%d, associd=0x%04x, mode=%d, group=%d\n",
2658 device_get_nameunit(sc
->dev
), k
->wk_keyix
, k
->wk_keylen
, associd
, mode
,
2659 (k
->wk_flags
& IEEE80211_KEY_GROUP
) ? 1 : 0);
2661 if (!(k
->wk_flags
& IEEE80211_KEY_GROUP
))
2663 /* install pairwise key */
2666 wcid
= RT2870_AID2WCID(associd
);
2667 key_base
= RT2870_REG_PKEY(wcid
);
2669 if (k
->wk_cipher
->ic_cipher
== IEEE80211_CIPHER_WEP
)
2673 iv
[3] = (k
->wk_keyix
<< 6);
2677 if (k
->wk_cipher
->ic_cipher
== IEEE80211_CIPHER_TKIP
)
2679 iv
[0] = (k
->wk_keytsc
>> 8);
2680 iv
[1] = ((iv
[0] | 0x20) & 0x7f);
2681 iv
[2] = k
->wk_keytsc
;
2687 iv
[0] = k
->wk_keytsc
;
2688 iv
[1] = k
->wk_keytsc
>> 8;
2692 iv
[3] = ((k
->wk_keyix
<< 6) | IEEE80211_WEP_EXTIV
);
2693 iv
[4] = (k
->wk_keytsc
>> 16);
2694 iv
[5] = (k
->wk_keytsc
>> 24);
2695 iv
[6] = (k
->wk_keytsc
>> 32);
2696 iv
[7] = (k
->wk_keytsc
>> 40);
2698 RT2870_DPRINTF(sc
, RT2870_DEBUG_KEY
,
2699 "%s: set key: iv=%02x %02x %02x %02x %02x %02x %02x %02x\n",
2700 device_get_nameunit(sc
->dev
),
2701 iv
[0], iv
[1], iv
[2], iv
[3], iv
[4], iv
[5], iv
[6], iv
[7]);
2704 rt2870_io_mac_write_multi(sc
, RT2870_REG_IVEIV(wcid
), iv
, 8);
2706 if (k
->wk_cipher
->ic_cipher
== IEEE80211_CIPHER_TKIP
)
2708 rt2870_io_mac_write_multi(sc
, key_base
, k
->wk_key
, 16);
2710 if (ic
->ic_opmode
!= IEEE80211_M_HOSTAP
)
2712 rt2870_io_mac_write_multi(sc
, key_base
+ 16, &k
->wk_key
[16], 8);
2713 rt2870_io_mac_write_multi(sc
, key_base
+ 24, &k
->wk_key
[24], 8);
2717 rt2870_io_mac_write_multi(sc
, key_base
+ 16, &k
->wk_key
[24], 8);
2718 rt2870_io_mac_write_multi(sc
, key_base
+ 24, &k
->wk_key
[16], 8);
2723 rt2870_io_mac_write_multi(sc
, key_base
, k
->wk_key
, k
->wk_keylen
);
2726 tmp
= ((vapid
& RT2870_REG_VAP_MASK
) << RT2870_REG_VAP_SHIFT
) |
2727 (mode
<< RT2870_REG_CIPHER_MODE_SHIFT
) | RT2870_REG_PKEY_ENABLE
;
2729 rt2870_io_mac_write(sc
, RT2870_REG_WCID_ATTR(wcid
), tmp
);
2732 if ((k
->wk_flags
& IEEE80211_KEY_GROUP
) ||
2733 (k
->wk_cipher
->ic_cipher
== IEEE80211_CIPHER_WEP
))
2735 /* install group key */
2738 key_base
= RT2870_REG_SKEY(vapid
, k
->wk_keyix
);
2739 keymode_base
= RT2870_REG_SKEY_MODE(vapid
);
2741 if (k
->wk_cipher
->ic_cipher
== IEEE80211_CIPHER_TKIP
)
2743 rt2870_io_mac_write_multi(sc
, key_base
, k
->wk_key
, 16);
2745 if (ic
->ic_opmode
!= IEEE80211_M_HOSTAP
)
2747 rt2870_io_mac_write_multi(sc
, key_base
+ 16, &k
->wk_key
[16], 8);
2748 rt2870_io_mac_write_multi(sc
, key_base
+ 24, &k
->wk_key
[24], 8);
2752 rt2870_io_mac_write_multi(sc
, key_base
+ 16, &k
->wk_key
[24], 8);
2753 rt2870_io_mac_write_multi(sc
, key_base
+ 24, &k
->wk_key
[16], 8);
2758 rt2870_io_mac_write_multi(sc
, key_base
, k
->wk_key
, k
->wk_keylen
);
2761 tmp
= rt2870_io_mac_read(sc
, keymode_base
);
2763 tmp
&= ~(0xf << (k
->wk_keyix
* 4 + 16 * (vapid
% 2)));
2764 tmp
|= (mode
<< (k
->wk_keyix
* 4 + 16 * (vapid
% 2)));
2766 rt2870_io_mac_write(sc
, keymode_base
, tmp
);
2771 * rt2870_key_delete_cb
2773 static void rt2870_key_delete_cb(struct rt2870_softc
*sc
, void *arg
)
2775 struct ieee80211com
*ic
;
2776 struct ieee80211_key
*k
;
2777 struct rt2870_cmd_argv_keydelete
*cmd_argv
;
2779 uint8_t vapid
, wcid
;
2785 associd
= cmd_argv
->associd
;
2787 RT2870_DPRINTF(sc
, RT2870_DEBUG_KEY
,
2788 "%s: delete key: keyix=%d, keylen=%d, associd=0x%04x, group=%d\n",
2789 device_get_nameunit(sc
->dev
), k
->wk_keyix
, k
->wk_keylen
, associd
,
2790 (k
->wk_flags
& IEEE80211_KEY_GROUP
) ? 1 : 0);
2792 if (!(k
->wk_flags
& IEEE80211_KEY_GROUP
))
2794 /* remove pairwise key */
2797 wcid
= RT2870_AID2WCID(associd
);
2799 tmp
= ((vapid
& RT2870_REG_VAP_MASK
) << RT2870_REG_VAP_SHIFT
) |
2800 (RT2870_REG_CIPHER_MODE_NONE
<< RT2870_REG_CIPHER_MODE_SHIFT
) | RT2870_REG_PKEY_ENABLE
;
2802 rt2870_io_mac_write(sc
, RT2870_REG_WCID_ATTR(wcid
), tmp
);
2806 /* remove group key */
2810 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_SKEY_MODE(vapid
));
2812 tmp
&= ~(0xf << (k
->wk_keyix
* 4 + 16 * (vapid
% 2)));
2813 tmp
|= (RT2870_REG_CIPHER_MODE_NONE
<< (k
->wk_keyix
* 4 + 16 * (vapid
% 2)));
2815 rt2870_io_mac_write(sc
, RT2870_REG_SKEY_MODE(vapid
), tmp
);
2820 * rt2870_asic_set_bssid
2822 static void rt2870_asic_set_bssid(struct rt2870_softc
*sc
,
2823 const uint8_t *bssid
)
2827 RT2870_DPRINTF(sc
, RT2870_DEBUG_STATE
,
2828 "%s: set bssid: bssid=%s\n",
2829 device_get_nameunit(sc
->dev
), ether_sprintf(bssid
));
2831 tmp
= bssid
[0] | (bssid
[1]) << 8 | (bssid
[2] << 16) | (bssid
[3] << 24);
2833 rt2870_io_mac_write(sc
, RT2870_REG_BSSID_DW0
, tmp
);
2835 tmp
= bssid
[4] | (bssid
[5] << 8);
2837 rt2870_io_mac_write(sc
, RT2870_REG_BSSID_DW1
, tmp
);
2841 * rt2870_asic_set_macaddr
2843 static void rt2870_asic_set_macaddr(struct rt2870_softc
*sc
,
2844 const uint8_t *addr
)
2848 tmp
= addr
[0] | (addr
[1] << 8) | (addr
[2] << 16) | (addr
[3] << 24);
2850 rt2870_io_mac_write(sc
, RT2870_REG_ADDR_DW0
, tmp
);
2852 tmp
= addr
[4] | (addr
[5] << 8);
2854 rt2870_io_mac_write(sc
, RT2870_REG_ADDR_DW1
, tmp
);
2858 * rt2870_asic_enable_tsf_sync
2860 static void rt2870_asic_enable_tsf_sync(struct rt2870_softc
*sc
)
2862 struct ieee80211com
*ic
;
2867 RT2870_DPRINTF(sc
, RT2870_DEBUG_BEACON
,
2868 "%s: enabling TSF\n",
2869 device_get_nameunit(sc
->dev
));
2871 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_BCN_TIME_CFG
);
2874 tmp
|= ic
->ic_bss
->ni_intval
* 16;
2875 tmp
|= (RT2870_REG_TSF_TIMER_ENABLE
| RT2870_REG_TBTT_TIMER_ENABLE
);
2877 if (ic
->ic_opmode
== IEEE80211_M_STA
)
2879 tmp
|= (RT2870_REG_TSF_SYNC_MODE_STA
<< RT2870_REG_TSF_SYNC_MODE_SHIFT
);
2881 else if (ic
->ic_opmode
== IEEE80211_M_IBSS
)
2883 tmp
|= RT2870_REG_BCN_TX_ENABLE
;
2884 tmp
|= (RT2870_REG_TSF_SYNC_MODE_IBSS
<< RT2870_REG_TSF_SYNC_MODE_SHIFT
);
2886 else if (ic
->ic_opmode
== IEEE80211_M_HOSTAP
)
2888 tmp
|= RT2870_REG_BCN_TX_ENABLE
;
2889 tmp
|= (RT2870_REG_TSF_SYNC_MODE_HOSTAP
<< RT2870_REG_TSF_SYNC_MODE_SHIFT
);
2892 rt2870_io_mac_write(sc
, RT2870_REG_BCN_TIME_CFG
, tmp
);
2896 * rt2870_asic_disable_tsf_sync
2898 static void rt2870_asic_disable_tsf_sync(struct rt2870_softc
*sc
)
2902 RT2870_DPRINTF(sc
, RT2870_DEBUG_BEACON
,
2903 "%s: disabling TSF\n",
2904 device_get_nameunit(sc
->dev
));
2906 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_BCN_TIME_CFG
);
2908 tmp
&= ~(RT2870_REG_BCN_TX_ENABLE
|
2909 RT2870_REG_TSF_TIMER_ENABLE
|
2910 RT2870_REG_TBTT_TIMER_ENABLE
);
2912 tmp
&= ~(RT2870_REG_TSF_SYNC_MODE_MASK
<< RT2870_REG_TSF_SYNC_MODE_SHIFT
);
2913 tmp
|= (RT2870_REG_TSF_SYNC_MODE_DISABLE
<< RT2870_REG_TSF_SYNC_MODE_SHIFT
);
2915 rt2870_io_mac_write(sc
, RT2870_REG_BCN_TIME_CFG
, tmp
);
2919 * rt2870_asic_enable_mrr
2921 static void rt2870_asic_enable_mrr(struct rt2870_softc
*sc
)
2923 #define CCK(mcs) (mcs)
2924 #define OFDM(mcs) ((1 << 3) | (mcs))
2925 #define HT(mcs) (mcs)
2927 rt2870_io_mac_write(sc
, RT2870_REG_TX_LG_FBK_CFG0
,
2928 (OFDM(6) << 28) | /* 54 -> 48 */
2929 (OFDM(5) << 24) | /* 48 -> 36 */
2930 (OFDM(4) << 20) | /* 36 -> 24 */
2931 (OFDM(3) << 16) | /* 24 -> 18 */
2932 (OFDM(2) << 12) | /* 18 -> 12 */
2933 (OFDM(1) << 8) | /* 12 -> 9 */
2934 (OFDM(0) << 4) | /* 9 -> 6 */
2935 OFDM(0)); /* 6 -> 6 */
2937 rt2870_io_mac_write(sc
, RT2870_REG_TX_LG_FBK_CFG1
,
2938 (CCK(2) << 12) | /* 11 -> 5.5 */
2939 (CCK(1) << 8) | /* 5.5 -> 2 */
2940 (CCK(0) << 4) | /* 2 -> 1 */
2941 CCK(0)); /* 1 -> 1 */
2943 rt2870_io_mac_write(sc
, RT2870_REG_TX_HT_FBK_CFG0
,
2953 rt2870_io_mac_write(sc
, RT2870_REG_TX_HT_FBK_CFG1
,
2969 * rt2870_asic_set_txpreamble
2971 static void rt2870_asic_set_txpreamble(struct rt2870_softc
*sc
)
2973 struct ieee80211com
*ic
;
2978 RT2870_DPRINTF(sc
, RT2870_DEBUG_STATE
,
2979 "%s: %s short Tx preamble\n",
2980 device_get_nameunit(sc
->dev
),
2981 (ic
->ic_flags
& IEEE80211_F_SHPREAMBLE
) ? "enabling" : "disabling");
2983 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_AUTO_RSP_CFG
);
2985 tmp
&= ~RT2870_REG_CCK_SHORT_ENABLE
;
2987 if (ic
->ic_flags
& IEEE80211_F_SHPREAMBLE
)
2988 tmp
|= RT2870_REG_CCK_SHORT_ENABLE
;
2990 rt2870_io_mac_write(sc
, RT2870_REG_AUTO_RSP_CFG
, tmp
);
2994 * rt2870_asic_set_basicrates
2996 static void rt2870_asic_set_basicrates(struct rt2870_softc
*sc
)
2998 struct ieee80211com
*ic
;
3002 if (ic
->ic_curmode
== IEEE80211_MODE_11B
)
3003 rt2870_io_mac_write(sc
, RT2870_REG_LEGACY_BASIC_RATE
, 0x3);
3004 else if (ic
->ic_curmode
== IEEE80211_MODE_11A
)
3005 rt2870_io_mac_write(sc
, RT2870_REG_LEGACY_BASIC_RATE
, 0x150);
3007 rt2870_io_mac_write(sc
, RT2870_REG_LEGACY_BASIC_RATE
, 0x15f);
3011 * rt2870_asic_update_rtsthreshold
3013 static void rt2870_asic_update_rtsthreshold(struct rt2870_softc
*sc
)
3015 struct ieee80211com
*ic
;
3021 RT2870_DPRINTF(sc
, RT2870_DEBUG_PROT
,
3022 "%s: updating RTS threshold: %d\n",
3023 device_get_nameunit(sc
->dev
), ic
->ic_rtsthreshold
);
3025 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_RTS_CFG
);
3027 tmp
&= ~(RT2870_REG_TX_RTS_THRESHOLD_MASK
<< RT2870_REG_TX_RTS_THRESHOLD_SHIFT
);
3029 threshold
= (ic
->ic_rtsthreshold
< IEEE80211_RTS_MAX
) ?
3030 ic
->ic_rtsthreshold
: 0x1000;
3032 tmp
|= ((threshold
& RT2870_REG_TX_RTS_THRESHOLD_MASK
) <<
3033 RT2870_REG_TX_RTS_THRESHOLD_SHIFT
);
3035 rt2870_io_mac_write(sc
, RT2870_REG_TX_RTS_CFG
, tmp
);
3039 * rt2870_asic_update_txpower
3041 static void rt2870_asic_update_txpower(struct rt2870_softc
*sc
)
3043 struct ieee80211com
*ic
;
3044 uint32_t *txpow_rate
;
3052 RT2870_DPRINTF(sc
, RT2870_DEBUG_STATE
,
3053 "%s: updating Tx power: %d\n",
3054 device_get_nameunit(sc
->dev
), ic
->ic_txpowlimit
);
3056 if (!IEEE80211_IS_CHAN_HT40(ic
->ic_curchan
))
3058 txpow_rate
= sc
->txpow_rate_20mhz
;
3062 if (IEEE80211_IS_CHAN_2GHZ(ic
->ic_curchan
))
3063 txpow_rate
= sc
->txpow_rate_40mhz_2ghz
;
3065 txpow_rate
= sc
->txpow_rate_40mhz_5ghz
;
3070 val
= rt2870_io_bbp_read(sc
, 1);
3073 if (ic
->ic_txpowlimit
> 90)
3077 else if (ic
->ic_txpowlimit
> 60)
3081 else if (ic
->ic_txpowlimit
> 30)
3085 else if (ic
->ic_txpowlimit
> 15)
3089 else if (ic
->ic_txpowlimit
> 9)
3099 rt2870_io_bbp_write(sc
, 1, val
);
3101 for (i
= 0; i
< RT2870_SOFTC_TXPOW_RATE_COUNT
; i
++)
3103 if (txpow_rate
[i
] == 0xffffffff)
3106 tmp
= rt2870_read_eeprom_txpow_rate_add_delta(txpow_rate
[i
], delta
);
3108 rt2870_io_mac_write(sc
, RT2870_REG_TX_PWR_CFG(i
), tmp
);
3113 * rt2870_asic_update_promisc
3115 static void rt2870_asic_update_promisc(struct rt2870_softc
*sc
)
3120 ifp
= sc
->ic
.ic_ifp
;
3122 printf("%s: %s promiscuous mode\n",
3123 device_get_nameunit(sc
->dev
),
3124 (ifp
->if_flags
& IFF_PROMISC
) ? "entering" : "leaving");
3126 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_RX_FILTER_CFG
);
3128 tmp
&= ~RT2870_REG_RX_FILTER_DROP_UC_NOME
;
3130 if (!(ifp
->if_flags
& IFF_PROMISC
))
3131 tmp
|= RT2870_REG_RX_FILTER_DROP_UC_NOME
;
3133 rt2870_io_mac_write(sc
, RT2870_REG_RX_FILTER_CFG
, tmp
);
3137 * rt2870_asic_updateprot
3139 static void rt2870_asic_updateprot(struct rt2870_softc
*sc
)
3141 struct ieee80211com
*ic
;
3142 uint32_t cck_prot
, ofdm_prot
, mm20_prot
, mm40_prot
, gf20_prot
, gf40_prot
;
3147 /* CCK frame protection */
3149 cck_prot
= RT2870_REG_RTSTH_ENABLE
| RT2870_REG_PROT_NAV_SHORT
|
3150 RT2870_REG_TXOP_ALLOW_ALL
| RT2870_REG_PROT_CTRL_NONE
;
3152 /* set up protection frame phy mode and rate (MCS code) */
3154 if (ic
->ic_curmode
== IEEE80211_MODE_11A
)
3155 cck_prot
|= (RT2870_REG_PROT_PHYMODE_OFDM
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3156 (0 << RT2870_REG_PROT_MCS_SHIFT
);
3158 cck_prot
|= ((RT2870_REG_PROT_PHYMODE_CCK
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3159 (3 << RT2870_REG_PROT_MCS_SHIFT
));
3161 rt2870_io_mac_write(sc
, RT2870_REG_TX_CCK_PROT_CFG
, cck_prot
);
3163 /* OFDM frame protection */
3165 ofdm_prot
= RT2870_REG_RTSTH_ENABLE
| RT2870_REG_PROT_NAV_SHORT
|
3166 RT2870_REG_TXOP_ALLOW_ALL
;
3168 if (ic
->ic_flags
& IEEE80211_F_USEPROT
)
3170 RT2870_DPRINTF(sc
, RT2870_DEBUG_PROT
,
3171 "%s: updating protection mode: b/g protection mode=%s\n",
3172 device_get_nameunit(sc
->dev
),
3173 (ic
->ic_protmode
== IEEE80211_PROT_RTSCTS
) ? "RTS/CTS" :
3174 ((ic
->ic_protmode
== IEEE80211_PROT_CTSONLY
) ? "CTS-to-self" : "none"));
3176 if (ic
->ic_protmode
== IEEE80211_PROT_RTSCTS
)
3177 ofdm_prot
|= RT2870_REG_PROT_CTRL_RTS_CTS
;
3178 else if (ic
->ic_protmode
== IEEE80211_PROT_CTSONLY
)
3179 ofdm_prot
|= RT2870_REG_PROT_CTRL_CTS
;
3181 ofdm_prot
|= RT2870_REG_PROT_CTRL_NONE
;
3185 RT2870_DPRINTF(sc
, RT2870_DEBUG_PROT
,
3186 "%s: updating protection mode: b/g protection mode=%s\n",
3187 device_get_nameunit(sc
->dev
), "none");
3189 ofdm_prot
|= RT2870_REG_PROT_CTRL_NONE
;
3192 rt2870_io_mac_write(sc
, RT2870_REG_TX_OFDM_PROT_CFG
, ofdm_prot
);
3194 /* HT frame protection */
3196 if ((ic
->ic_opmode
== IEEE80211_M_STA
) && (ic
->ic_state
== IEEE80211_S_RUN
))
3197 htopmode
= ic
->ic_bss
->ni_htopmode
;
3199 htopmode
= ic
->ic_curhtprotmode
;
3201 RT2870_DPRINTF(sc
, RT2870_DEBUG_PROT
,
3202 "%s: updating protection mode: HT operation mode=0x%02x, protection mode=%s\n",
3203 device_get_nameunit(sc
->dev
),
3204 htopmode
& IEEE80211_HTINFO_OPMODE
,
3205 (ic
->ic_htprotmode
== IEEE80211_PROT_RTSCTS
) ? "RTS/CTS" :
3206 ((ic
->ic_htprotmode
== IEEE80211_PROT_CTSONLY
) ? "CTS-to-self" : "none"));
3208 switch (htopmode
& IEEE80211_HTINFO_OPMODE
)
3210 /* IEEE80211_HTINFO_OPMODE_HT20PR */
3211 case IEEE80211_HTINFO_OPMODE_HT20PR
:
3212 mm20_prot
= RT2870_REG_PROT_NAV_SHORT
| RT2870_REG_PROT_CTRL_NONE
|
3213 RT2870_REG_TXOP_ALLOW_CCK
| RT2870_REG_TXOP_ALLOW_OFDM
|
3214 RT2870_REG_TXOP_ALLOW_MM20
| RT2870_REG_TXOP_ALLOW_GF20
|
3215 (RT2870_REG_PROT_PHYMODE_OFDM
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3216 (4 << RT2870_REG_PROT_MCS_SHIFT
);
3218 gf20_prot
= mm20_prot
;
3220 mm40_prot
= RT2870_REG_PROT_NAV_SHORT
| RT2870_REG_PROT_CTRL_NONE
|
3221 RT2870_REG_TXOP_ALLOW_ALL
|
3222 (RT2870_REG_PROT_PHYMODE_OFDM
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3223 (0x84 << RT2870_REG_PROT_MCS_SHIFT
);
3225 if (ic
->ic_htprotmode
== IEEE80211_PROT_RTSCTS
)
3226 mm40_prot
|= RT2870_REG_PROT_CTRL_RTS_CTS
;
3227 else if (ic
->ic_htprotmode
== IEEE80211_PROT_CTSONLY
)
3228 mm40_prot
|= RT2870_REG_PROT_CTRL_CTS
;
3230 mm40_prot
|= RT2870_REG_PROT_CTRL_NONE
;
3232 gf40_prot
= mm40_prot
;
3235 /* IEEE80211_HTINFO_OPMODE_MIXED */
3236 case IEEE80211_HTINFO_OPMODE_MIXED
:
3237 mm20_prot
= RT2870_REG_PROT_NAV_SHORT
|
3238 RT2870_REG_TXOP_ALLOW_CCK
| RT2870_REG_TXOP_ALLOW_OFDM
|
3239 RT2870_REG_TXOP_ALLOW_MM20
| RT2870_REG_TXOP_ALLOW_GF20
;
3241 if (ic
->ic_flags
& IEEE80211_F_USEPROT
)
3242 mm20_prot
|= (RT2870_REG_PROT_PHYMODE_CCK
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3243 (3 << RT2870_REG_PROT_MCS_SHIFT
);
3245 mm20_prot
|= (RT2870_REG_PROT_PHYMODE_OFDM
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3246 (4 << RT2870_REG_PROT_MCS_SHIFT
);
3248 if (ic
->ic_htprotmode
== IEEE80211_PROT_RTSCTS
)
3249 mm20_prot
|= RT2870_REG_PROT_CTRL_RTS_CTS
;
3250 else if (ic
->ic_htprotmode
== IEEE80211_PROT_CTSONLY
)
3251 mm20_prot
|= RT2870_REG_PROT_CTRL_CTS
;
3253 mm20_prot
|= RT2870_REG_PROT_CTRL_NONE
;
3255 gf20_prot
= mm20_prot
;
3257 mm40_prot
= RT2870_REG_PROT_NAV_SHORT
| RT2870_REG_TXOP_ALLOW_ALL
;
3259 if (ic
->ic_flags
& IEEE80211_F_USEPROT
)
3260 mm40_prot
|= (RT2870_REG_PROT_PHYMODE_CCK
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3261 (3 << RT2870_REG_PROT_MCS_SHIFT
);
3263 mm40_prot
|= (RT2870_REG_PROT_PHYMODE_OFDM
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3264 (0x84 << RT2870_REG_PROT_MCS_SHIFT
);
3266 if (ic
->ic_htprotmode
== IEEE80211_PROT_RTSCTS
)
3267 mm40_prot
|= RT2870_REG_PROT_CTRL_RTS_CTS
;
3268 else if (ic
->ic_htprotmode
== IEEE80211_PROT_CTSONLY
)
3269 mm40_prot
|= RT2870_REG_PROT_CTRL_CTS
;
3271 mm40_prot
|= RT2870_REG_PROT_CTRL_NONE
;
3273 gf40_prot
= mm40_prot
;
3277 * IEEE80211_HTINFO_OPMODE_PURE
3278 * IEEE80211_HTINFO_OPMODE_PROTOPT
3280 case IEEE80211_HTINFO_OPMODE_PURE
:
3281 case IEEE80211_HTINFO_OPMODE_PROTOPT
:
3283 mm20_prot
= RT2870_REG_PROT_NAV_SHORT
| RT2870_REG_PROT_CTRL_NONE
|
3284 RT2870_REG_TXOP_ALLOW_CCK
| RT2870_REG_TXOP_ALLOW_OFDM
|
3285 RT2870_REG_TXOP_ALLOW_MM20
| RT2870_REG_TXOP_ALLOW_GF20
|
3286 (RT2870_REG_PROT_PHYMODE_OFDM
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3287 (4 << RT2870_REG_PROT_MCS_SHIFT
);
3289 gf20_prot
= mm20_prot
;
3291 mm40_prot
= RT2870_REG_PROT_NAV_SHORT
| RT2870_REG_PROT_CTRL_NONE
|
3292 RT2870_REG_TXOP_ALLOW_ALL
|
3293 (RT2870_REG_PROT_PHYMODE_OFDM
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3294 (0x84 << RT2870_REG_PROT_MCS_SHIFT
);
3296 gf40_prot
= mm40_prot
;
3300 rt2870_io_mac_write(sc
, RT2870_REG_TX_MM20_PROT_CFG
, mm20_prot
);
3301 rt2870_io_mac_write(sc
, RT2870_REG_TX_MM40_PROT_CFG
, mm40_prot
);
3302 rt2870_io_mac_write(sc
, RT2870_REG_TX_GF20_PROT_CFG
, gf20_prot
);
3303 rt2870_io_mac_write(sc
, RT2870_REG_TX_GF40_PROT_CFG
, gf40_prot
);
3307 * rt2870_asic_updateslot
3309 static void rt2870_asic_updateslot(struct rt2870_softc
*sc
)
3311 struct ieee80211com
*ic
;
3316 RT2870_DPRINTF(sc
, RT2870_DEBUG_STATE
,
3317 "%s: %s short slot time\n",
3318 device_get_nameunit(sc
->dev
),
3319 (ic
->ic_flags
& IEEE80211_F_SHSLOT
) ? "enabling" : "disabling");
3321 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_BKOFF_SLOT_CFG
);
3324 tmp
|= (ic
->ic_flags
& IEEE80211_F_SHSLOT
) ? 9 : 20;
3326 rt2870_io_mac_write(sc
, RT2870_REG_BKOFF_SLOT_CFG
, tmp
);
3330 * rt2870_asic_wme_update
3332 static void rt2870_asic_wme_update(struct rt2870_softc
*sc
)
3334 struct ieee80211com
*ic
;
3335 struct ieee80211_wme_state
*wme
;
3336 const struct wmeParams
*wmep
;
3341 wmep
= wme
->wme_chanParams
.cap_wmeParams
;
3343 RT2870_DPRINTF(sc
, RT2870_DEBUG_WME
,
3344 "%s: wme update: WME_AC_VO=%d/%d/%d/%d, WME_AC_VI=%d/%d/%d/%d, "
3345 "WME_AC_BK=%d/%d/%d/%d, WME_AC_BE=%d/%d/%d/%d\n",
3346 device_get_nameunit(sc
->dev
),
3347 wmep
[WME_AC_VO
].wmep_aifsn
,
3348 wmep
[WME_AC_VO
].wmep_logcwmin
, wmep
[WME_AC_VO
].wmep_logcwmax
,
3349 wmep
[WME_AC_VO
].wmep_txopLimit
,
3350 wmep
[WME_AC_VI
].wmep_aifsn
,
3351 wmep
[WME_AC_VI
].wmep_logcwmin
, wmep
[WME_AC_VI
].wmep_logcwmax
,
3352 wmep
[WME_AC_VI
].wmep_txopLimit
,
3353 wmep
[WME_AC_BK
].wmep_aifsn
,
3354 wmep
[WME_AC_BK
].wmep_logcwmin
, wmep
[WME_AC_BK
].wmep_logcwmax
,
3355 wmep
[WME_AC_BK
].wmep_txopLimit
,
3356 wmep
[WME_AC_BE
].wmep_aifsn
,
3357 wmep
[WME_AC_BE
].wmep_logcwmin
, wmep
[WME_AC_BE
].wmep_logcwmax
,
3358 wmep
[WME_AC_BE
].wmep_txopLimit
);
3360 for (i
= 0; i
< WME_NUM_AC
; i
++)
3361 rt2870_io_mac_write(sc
, RT2870_REG_TX_EDCA_AC_CFG(i
),
3362 (wmep
[i
].wmep_logcwmax
<< 16) | (wmep
[i
].wmep_logcwmin
<< 12) |
3363 (wmep
[i
].wmep_aifsn
<< 8) | wmep
[i
].wmep_txopLimit
);
3365 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_WMM_AIFSN_CFG
,
3366 (wmep
[WME_AC_VO
].wmep_aifsn
<< 12) | (wmep
[WME_AC_VI
].wmep_aifsn
<< 8) |
3367 (wmep
[WME_AC_BK
].wmep_aifsn
<< 4) | wmep
[WME_AC_BE
].wmep_aifsn
);
3369 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_WMM_CWMIN_CFG
,
3370 (wmep
[WME_AC_VO
].wmep_logcwmin
<< 12) | (wmep
[WME_AC_VI
].wmep_logcwmin
<< 8) |
3371 (wmep
[WME_AC_BK
].wmep_logcwmin
<< 4) | wmep
[WME_AC_BE
].wmep_logcwmin
);
3373 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_WMM_CWMAX_CFG
,
3374 (wmep
[WME_AC_VO
].wmep_logcwmax
<< 12) | (wmep
[WME_AC_VI
].wmep_logcwmax
<< 8) |
3375 (wmep
[WME_AC_BK
].wmep_logcwmax
<< 4) | wmep
[WME_AC_BE
].wmep_logcwmax
);
3377 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_WMM_TXOP0_CFG
,
3378 (wmep
[WME_AC_BK
].wmep_txopLimit
<< 16) | wmep
[WME_AC_BE
].wmep_txopLimit
);
3380 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_WMM_TXOP1_CFG
,
3381 (wmep
[WME_AC_VO
].wmep_txopLimit
<< 16) | wmep
[WME_AC_VI
].wmep_txopLimit
);
3385 * rt2870_asic_update_beacon
3387 static int rt2870_asic_update_beacon(struct rt2870_softc
*sc
)
3389 struct ieee80211com
*ic
;
3391 struct rt2870_txwi txwi
;
3397 m
= ieee80211_beacon_alloc(ic
->ic_bss
, &sc
->beacon_offsets
);
3401 rate
= IEEE80211_IS_CHAN_5GHZ(ic
->ic_curchan
) ? 12 : 2;
3402 mcs
= rt2870_rate2mcs(rate
);
3404 memset(&txwi
, 0, sizeof(struct rt2870_txwi
));
3407 txwi
.pid_mpdu_len
= ((htole16(m
->m_pkthdr
.len
) & RT2870_TXWI_MPDU_LEN_MASK
) <<
3408 RT2870_TXWI_MPDU_LEN_SHIFT
);
3409 txwi
.txop
= (RT2870_TXWI_TXOP_HT
<< RT2870_TXWI_TXOP_SHIFT
);
3410 txwi
.mpdu_density_flags
|=
3411 (RT2870_TXWI_FLAGS_TS
<< RT2870_TXWI_FLAGS_SHIFT
);
3412 txwi
.bawin_size_xflags
|=
3413 (RT2870_TXWI_XFLAGS_NSEQ
<< RT2870_TXWI_XFLAGS_SHIFT
);
3417 txwi
.phymode_ifs_stbc_shortgi
=
3418 (RT2870_TXWI_PHYMODE_CCK
<< RT2870_TXWI_PHYMODE_SHIFT
);
3420 if (rate
!= 2 && (ic
->ic_flags
& IEEE80211_F_SHPREAMBLE
))
3421 mcs
|= RT2870_TXWI_MCS_SHOTPRE
;
3425 txwi
.phymode_ifs_stbc_shortgi
=
3426 (RT2870_TXWI_PHYMODE_OFDM
<< RT2870_TXWI_PHYMODE_SHIFT
);
3429 txwi
.bw_mcs
= (RT2870_TXWI_BW_20
<< RT2870_TXWI_BW_SHIFT
) |
3430 ((mcs
& RT2870_TXWI_MCS_MASK
) << RT2870_TXWI_MCS_SHIFT
);
3432 /* disable temporarily TSF sync */
3434 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_BCN_TIME_CFG
);
3436 tmp
&= ~(RT2870_REG_BCN_TX_ENABLE
|
3437 RT2870_REG_TSF_TIMER_ENABLE
|
3438 RT2870_REG_TBTT_TIMER_ENABLE
);
3440 rt2870_io_mac_write(sc
, RT2870_REG_BCN_TIME_CFG
, tmp
);
3442 /* write Tx wireless info and beacon frame to on-chip memory */
3444 rt2870_io_mac_write_multi(sc
, RT2870_REG_BEACON_BASE(0),
3445 &txwi
, sizeof(struct rt2870_txwi
));
3447 rt2870_io_mac_write_multi(sc
, RT2870_REG_BEACON_BASE(0) + sizeof(struct rt2870_txwi
),
3448 mtod(m
, uint8_t *), m
->m_pkthdr
.len
);
3450 /* enable again TSF sync */
3452 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_BCN_TIME_CFG
);
3454 tmp
|= (RT2870_REG_BCN_TX_ENABLE
|
3455 RT2870_REG_TSF_TIMER_ENABLE
|
3456 RT2870_REG_TBTT_TIMER_ENABLE
);
3458 rt2870_io_mac_write(sc
, RT2870_REG_BCN_TIME_CFG
, tmp
);
3466 * rt2870_asic_clear_keytables
3468 static void rt2870_asic_clear_keytables(struct rt2870_softc
*sc
)
3472 /* clear Rx WCID search table (entries = 256, entry size = 8) */
3474 for (i
= 0; i
< 256; i
++)
3476 rt2870_io_mac_write(sc
, RT2870_REG_WCID(i
), 0xffffffff);
3477 rt2870_io_mac_write(sc
, RT2870_REG_WCID(i
) + 4, 0x0000ffff);
3480 /* clear WCID attribute table (entries = 256, entry size = 4) */
3482 rt2870_io_mac_set_region_4(sc
, RT2870_REG_WCID_ATTR(0), 0, 256);
3484 /* clear IV/EIV table (entries = 256, entry size = 8) */
3486 rt2870_io_mac_set_region_4(sc
, RT2870_REG_IVEIV(0), 0, 2 * 256);
3488 /* clear pairwise key table (entries = 256, entry size = 32) */
3490 rt2870_io_mac_set_region_4(sc
, RT2870_REG_PKEY(0), 0, 8 * 256);
3492 /* clear shared key table (entries = 32, entry size = 32) */
3494 rt2870_io_mac_set_region_4(sc
, RT2870_REG_SKEY(0, 0), 0, 8 * 32);
3496 /* clear shared key mode (entries = 32, entry size = 2) */
3498 rt2870_io_mac_set_region_4(sc
, RT2870_REG_SKEY_MODE(0), 0, 16);
3504 static uint8_t rt2870_rxrate(struct rt2870_rxwi
*rxwi
)
3506 uint8_t mcs
, phymode
;
3509 mcs
= (rxwi
->bw_mcs
>> RT2870_RXWI_MCS_SHIFT
) & RT2870_RXWI_MCS_MASK
;
3510 phymode
= (rxwi
->phymode_stbc_shortgi
>> RT2870_RXWI_PHYMODE_SHIFT
) &
3511 RT2870_RXWI_PHYMODE_MASK
;
3517 case RT2870_RXWI_PHYMODE_CCK
:
3518 switch (mcs
& ~RT2870_RXWI_MCS_SHOTPRE
)
3520 case 0: rate
= 2; break; /* 1 Mbps */
3521 case 1: rate
= 4; break; /* 2 MBps */
3522 case 2: rate
= 11; break; /* 5.5 Mbps */
3523 case 3: rate
= 22; break; /* 11 Mbps */
3527 case RT2870_RXWI_PHYMODE_OFDM
:
3530 case 0: rate
= 12; break; /* 6 Mbps */
3531 case 1: rate
= 18; break; /* 9 Mbps */
3532 case 2: rate
= 24; break; /* 12 Mbps */
3533 case 3: rate
= 36; break; /* 18 Mbps */
3534 case 4: rate
= 48; break; /* 24 Mbps */
3535 case 5: rate
= 72; break; /* 36 Mbps */
3536 case 6: rate
= 96; break; /* 48 Mbps */
3537 case 7: rate
= 108; break; /* 54 Mbps */
3541 case RT2870_RXWI_PHYMODE_HT_MIXED
:
3542 case RT2870_RXWI_PHYMODE_HT_GF
:
3550 * rt2870_maxrssi_rxpath
3552 static uint8_t rt2870_maxrssi_rxpath(struct rt2870_softc
*sc
,
3553 const struct rt2870_rxwi
*rxwi
)
3559 if (sc
->nrxpath
> 1)
3560 if (rxwi
->rssi
[1] > rxwi
->rssi
[rxpath
])
3563 if (sc
->nrxpath
> 2)
3564 if (rxwi
->rssi
[2] > rxwi
->rssi
[rxpath
])
3573 static int8_t rt2870_rssi2dbm(struct rt2870_softc
*sc
,
3574 uint8_t rssi
, uint8_t rxpath
)
3576 struct ieee80211com
*ic
;
3577 struct ieee80211_channel
*c
;
3579 int8_t rssi_off
, lna_gain
;
3586 chan
= ieee80211_chan2ieee(ic
, c
);
3588 if (IEEE80211_IS_CHAN_5GHZ(c
))
3590 rssi_off
= sc
->rssi_off_5ghz
[rxpath
];
3593 lna_gain
= sc
->lna_gain
[1];
3594 else if (chan
<= 128)
3595 lna_gain
= sc
->lna_gain
[2];
3597 lna_gain
= sc
->lna_gain
[3];
3601 rssi_off
= sc
->rssi_off_2ghz
[rxpath
];
3602 lna_gain
= sc
->lna_gain
[0];
3605 return (-12 - rssi_off
- lna_gain
- rssi
);
3611 static uint8_t rt2870_rate2mcs(uint8_t rate
)
3638 static int rt2870_ackrate(struct ieee80211com
*ic
, int rate
)
3650 return (ic
->ic_curmode
== IEEE80211_MODE_11B
) ? 4 : rate
;
3669 /* default to 1Mbps */
3676 static uint16_t rt2870_txtime(int len
, int rate
, uint32_t flags
)
3680 if (RT2870_RATE_IS_OFDM(rate
))
3682 txtime
= (8 + 4 * len
+ 3 + rate
- 1) / rate
;
3683 txtime
= 16 + 4 + 4 * txtime
+ 6;
3687 txtime
= (16 * len
+ rate
- 1) / rate
;
3689 if (rate
!= 2 && (flags
& IEEE80211_F_SHPREAMBLE
))
3701 static void rt2870_rx_frame(struct rt2870_softc
*sc
,
3702 uint8_t *buf
, uint32_t dmalen
)
3704 struct ieee80211com
*ic
;
3706 struct ieee80211_frame
*wh
;
3707 struct ieee80211_node
*ni
;
3708 struct rt2870_softc_rx_radiotap_header
*tap
;
3709 struct rt2870_rxwi
*rxwi
;
3710 struct rt2870_rxinfo
*rxinfo
;
3712 uint32_t rxinfo_flags
;
3713 uint8_t cipher_err
, rssi
, ant
, phymode
, bw
, shortgi
, mcs
;
3715 int len
, ampdu
, amsdu
;
3720 /* get Rx wireless info */
3722 rxwi
= (struct rt2870_rxwi
*) buf
;
3723 len
= (le16toh(rxwi
->tid_size
) >> RT2870_RXWI_SIZE_SHIFT
) &
3724 RT2870_RXWI_SIZE_MASK
;
3728 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
3729 "%s: bad rxwi len: DMA len=%d, rxwi len=%d\n",
3730 device_get_nameunit(sc
->dev
), dmalen
, len
);
3736 rxinfo
= (struct rt2870_rxinfo
*) ((caddr_t
) rxwi
+ dmalen
);
3737 rxinfo_flags
= le32toh(rxinfo
->flags
);
3739 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
3740 "%s: Rx frame: DMA len=%d, len=%d, rxinfo flags=0x%08x\n",
3741 device_get_nameunit(sc
->dev
), dmalen
, len
, rxinfo_flags
);
3743 /* check for crc errors */
3745 if (rxinfo_flags
& RT2870_RXINFO_FLAGS_CRC_ERR
)
3747 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
3748 "%s: rxinfo: crc error\n",
3749 device_get_nameunit(sc
->dev
));
3753 if (!(ifp
->if_flags
& IFF_PROMISC
))
3757 wh
= (struct ieee80211_frame
*) (rxwi
+ 1);
3759 /* check for L2 padding between IEEE 802.11 frame header and body */
3761 if (rxinfo_flags
& RT2870_RXINFO_FLAGS_L2PAD
)
3763 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
3764 "%s: L2 padding: DMA len=%d, len=%d\n",
3765 device_get_nameunit(sc
->dev
), dmalen
, len
);
3770 m
= m_getjcl(M_DONTWAIT
, MT_DATA
, M_PKTHDR
, MJUMPAGESIZE
);
3773 sc
->rx_mbuf_alloc_errors
++;
3778 m
->m_pkthdr
.rcvif
= ifp
;
3779 m
->m_pkthdr
.len
= m
->m_len
= len
;
3781 m_copyback(m
, 0, len
, (caddr_t
) wh
);
3783 wh
= mtod(m
, struct ieee80211_frame
*);
3785 /* check for cipher errors */
3787 if (rxinfo_flags
& RT2870_RXINFO_FLAGS_DECRYPTED
)
3789 cipher_err
= ((rxinfo_flags
>> RT2870_RXINFO_FLAGS_CIPHER_ERR_SHIFT
) &
3790 RT2870_RXINFO_FLAGS_CIPHER_ERR_MASK
);
3791 if (cipher_err
== RT2870_RXINFO_FLAGS_CIPHER_ERR_NONE
)
3793 if (wh
->i_fc
[1] & IEEE80211_FC1_WEP
)
3794 wh
->i_fc
[1] &= ~IEEE80211_FC1_WEP
;
3796 m
->m_flags
|= M_WEP
;
3800 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
3801 "%s: rxinfo: cipher error=0x%02x\n",
3802 device_get_nameunit(sc
->dev
), cipher_err
);
3806 if (!(ifp
->if_flags
& IFF_PROMISC
))
3815 if (wh
->i_fc
[1] & IEEE80211_FC1_WEP
)
3819 if (!(ifp
->if_flags
& IFF_PROMISC
))
3827 /* check for A-MPDU */
3829 if (rxinfo_flags
& RT2870_RXINFO_FLAGS_AMPDU
)
3831 m
->m_flags
|= M_AMPDU
;
3842 /* check for A-MSDU */
3844 if (rxinfo_flags
& RT2870_RXINFO_FLAGS_AMSDU
)
3855 ant
= rt2870_maxrssi_rxpath(sc
, rxwi
);
3856 rssi
= rxwi
->rssi
[ant
];
3857 rssi_dbm
= rt2870_rssi2dbm(sc
, rssi
, ant
);
3858 phymode
= ((rxwi
->phymode_stbc_shortgi
>> RT2870_RXWI_PHYMODE_SHIFT
) &
3859 RT2870_RXWI_PHYMODE_MASK
);
3860 bw
= ((rxwi
->bw_mcs
>> RT2870_RXWI_BW_SHIFT
) & RT2870_RXWI_BW_MASK
);
3861 shortgi
= ((rxwi
->phymode_stbc_shortgi
>> RT2870_RXWI_SHORTGI_SHIFT
) &
3862 RT2870_RXWI_SHORTGI_MASK
);
3863 mcs
= ((rxwi
->bw_mcs
>> RT2870_RXWI_MCS_SHIFT
) & RT2870_RXWI_MCS_MASK
);
3865 if (bpf_peers_present(sc
->drvbpf
))
3869 tap
->flags
= IEEE80211_RADIOTAP_F_DATAPAD
;
3870 tap
->dbm_antsignal
= rssi_dbm
;
3871 tap
->dbm_antnoise
= RT2870_NOISE_FLOOR
;
3873 tap
->antsignal
= rssi
;
3874 tap
->chan_flags
= htole32(ic
->ic_curchan
->ic_flags
);
3875 tap
->chan_freq
= htole16(ic
->ic_curchan
->ic_freq
);
3876 tap
->chan_ieee
= ic
->ic_curchan
->ic_ieee
;
3877 tap
->chan_maxpow
= 0;
3879 if (phymode
== RT2870_RXWI_PHYMODE_CCK
|| phymode
== RT2870_RXWI_PHYMODE_OFDM
)
3880 tap
->rate
= rt2870_rxrate(rxwi
);
3882 tap
->rate
= mcs
| IEEE80211_RATE_MCS
;
3884 if (rxwi
->bw_mcs
& RT2870_RXWI_MCS_SHOTPRE
)
3885 tap
->flags
|= IEEE80211_RADIOTAP_F_SHORTPRE
;
3888 tap
->flags
|= IEEE80211_RADIOTAP_F_SHORTGI
;
3890 bpf_mtap2(sc
->drvbpf
, tap
, sc
->rxtap_len
, m
);
3893 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
3894 "%s: received frame: len=%d, phymode=%d, bw=%d, shortgi=%d, mcs=%d, "
3895 "ant=%d, rssi=%d/%d/%d, snr=%d/%d, wcid=0x%02x, ampdu=%d, amsdu=%d\n",
3896 device_get_nameunit(sc
->dev
),
3897 len
, phymode
, bw
, shortgi
, mcs
,
3898 ant
, rxwi
->rssi
[0], rxwi
->rssi
[1], rxwi
->rssi
[2],
3899 rxwi
->snr
[0], rxwi
->snr
[1],
3900 rxwi
->wcid
, ampdu
, amsdu
);
3902 ni
= ieee80211_find_rxnode(ic
, (struct ieee80211_frame_min
*) wh
);
3904 ieee80211_input(ic
, m
, ni
, rssi_dbm
- RT2870_NOISE_FLOOR
, RT2870_NOISE_FLOOR
, 0);
3906 ieee80211_free_node(ni
);
3912 static int rt2870_tx_frame(struct rt2870_softc
*sc
,
3913 struct mbuf
*m
, struct ieee80211_node
*ni
, int qid
)
3915 struct ieee80211com
*ic
;
3916 struct rt2870_softc_tx_ring
*ring
;
3917 struct rt2870_softc_tx_data
*data
;
3918 struct rt2870_txinfo
*txinfo
;
3919 struct rt2870_txwi
*txwi
;
3920 struct ieee80211_frame
*wh
;
3921 struct rt2870_softc_tx_radiotap_header
*tap
;
3922 u_int hdrsize
, hdrspace
;
3923 uint8_t type
, rate
, bw
, stbc
, shortgi
, mcs
, pid
, wcid
;
3924 uint16_t qos
, len
, dmalen
, mpdu_len
, dur
;
3925 int hasqos
, ackrate
;
3929 ring
= &sc
->tx_ring
[qid
];
3930 data
= STAILQ_FIRST(&ring
->inactive
);
3931 STAILQ_REMOVE_HEAD(&ring
->inactive
, next
);
3932 txinfo
= (struct rt2870_txinfo
*) data
->buf
;
3933 txwi
= (struct rt2870_txwi
*) (txinfo
+ 1);
3935 wh
= mtod(m
, struct ieee80211_frame
*);
3937 type
= wh
->i_fc
[0] & IEEE80211_FC0_TYPE_MASK
;
3939 hasqos
= IEEE80211_QOS_HAS_SEQ(wh
);
3942 if (IEEE80211_HAS_ADDR4(wh
))
3943 qos
= le16toh(*(const uint16_t *)
3944 (((struct ieee80211_qosframe_addr4
*) wh
)->i_qos
));
3946 qos
= le16toh(*(const uint16_t *)
3947 (((struct ieee80211_qosframe
*) wh
)->i_qos
));
3954 if (ni
->ni_flags
& IEEE80211_NODE_HT
)
3956 if (IEEE80211_IS_MULTICAST(wh
->i_addr1
) || type
!= IEEE80211_FC0_TYPE_DATA
)
3958 else if (ic
->ic_fixed_rate
!= IEEE80211_FIXED_RATE_NONE
)
3959 rate
= ic
->ic_fixed_rate
;
3961 rate
= ni
->ni_htrates
.rs_rates
[ni
->ni_txrate
];
3965 if (IEEE80211_IS_MULTICAST(wh
->i_addr1
) || type
!= IEEE80211_FC0_TYPE_DATA
)
3966 rate
= IEEE80211_IS_CHAN_5GHZ(ic
->ic_curchan
) ? 12 : 2;
3967 else if (ic
->ic_fixed_rate
!= IEEE80211_FIXED_RATE_NONE
)
3968 rate
= ic
->ic_fixed_rate
;
3970 rate
= ni
->ni_rates
.rs_rates
[ni
->ni_txrate
];
3973 rate
&= IEEE80211_RATE_VAL
;
3975 len
= sizeof(struct rt2870_txinfo
) + sizeof(struct rt2870_txwi
) + m
->m_pkthdr
.len
;
3977 /* align end on a 4-bytes boundary */
3979 dmalen
= (len
+ 3) & ~3;
3983 memset(txinfo
, 0, sizeof(struct rt2870_txinfo
));
3985 txinfo
->len
= htole16(dmalen
);
3987 txinfo
->qsel_flags
= (RT2870_TXINFO_QSEL_EDCA
<< RT2870_TXINFO_QSEL_SHIFT
);
3989 /* fill Tx wireless info */
3991 if (ni
->ni_flags
& IEEE80211_NODE_HT
)
3994 mcs
= rt2870_rate2mcs(rate
);
3998 wcid
= (type
== IEEE80211_FC0_TYPE_DATA
) ?
3999 RT2870_AID2WCID(ni
->ni_associd
) : 0xff;
4001 /* calculate MPDU length without padding */
4003 hdrsize
= ieee80211_hdrsize(wh
);
4004 hdrspace
= ieee80211_hdrspace(ic
, wh
);
4005 mpdu_len
= m
->m_pkthdr
.len
- hdrspace
+ hdrsize
;
4007 memset(txwi
, 0, sizeof(struct rt2870_txwi
));
4011 txwi
->pid_mpdu_len
= ((htole16(pid
) & RT2870_TXWI_PID_MASK
) <<
4012 RT2870_TXWI_PID_SHIFT
) | ((htole16(mpdu_len
) & RT2870_TXWI_MPDU_LEN_MASK
) <<
4013 RT2870_TXWI_MPDU_LEN_SHIFT
);
4015 stbc
= (ni
->ni_htcap
& IEEE80211_HTCAP_RXSTBC
) ? 1 : 0;
4017 shortgi
= (ic
->ic_flags_ext
& (IEEE80211_FEXT_SHORTGI20
| IEEE80211_FEXT_SHORTGI40
)) &&
4018 (ni
->ni_flags
& IEEE80211_NODE_HT
);
4020 txwi
->phymode_ifs_stbc_shortgi
|=
4021 ((stbc
& RT2870_TXWI_STBC_MASK
) << RT2870_TXWI_STBC_SHIFT
) |
4022 ((shortgi
& RT2870_TXWI_SHORTGI_MASK
) << RT2870_TXWI_SHORTGI_SHIFT
);
4024 if (ni
->ni_flags
& IEEE80211_NODE_HT
)
4026 txwi
->phymode_ifs_stbc_shortgi
|=
4027 (RT2870_TXWI_PHYMODE_HT_MIXED
<< RT2870_TXWI_PHYMODE_SHIFT
);
4031 if (!RT2870_RATE_IS_OFDM(rate
))
4033 txwi
->phymode_ifs_stbc_shortgi
|=
4034 (RT2870_TXWI_PHYMODE_CCK
<< RT2870_TXWI_PHYMODE_SHIFT
);
4036 if (rate
!= 2 && (ic
->ic_flags
& IEEE80211_F_SHPREAMBLE
))
4037 mcs
|= RT2870_TXWI_MCS_SHOTPRE
;
4041 txwi
->phymode_ifs_stbc_shortgi
|=
4042 (RT2870_TXWI_PHYMODE_OFDM
<< RT2870_TXWI_PHYMODE_SHIFT
);
4046 if ((ni
->ni_flags
& IEEE80211_NODE_HT
) &&
4047 (ni
->ni_htcap
& IEEE80211_HTCAP_CHWIDTH40
))
4048 bw
= RT2870_TXWI_BW_40
;
4050 bw
= RT2870_TXWI_BW_20
;
4052 txwi
->bw_mcs
= ((bw
& RT2870_TXWI_BW_MASK
) << RT2870_TXWI_BW_SHIFT
) |
4053 ((mcs
& RT2870_TXWI_MCS_MASK
) << RT2870_TXWI_MCS_SHIFT
);
4055 if (type
!= IEEE80211_FC0_TYPE_DATA
)
4056 txwi
->txop
= (RT2870_TXWI_TXOP_BACKOFF
<< RT2870_TXWI_TXOP_SHIFT
);
4058 txwi
->txop
= (RT2870_TXWI_TXOP_HT
<< RT2870_TXWI_TXOP_SHIFT
);
4060 /* skip ACKs for multicast frames and probe responses */
4062 if (!IEEE80211_IS_MULTICAST(wh
->i_addr1
) &&
4063 ((wh
->i_fc
[0] & (IEEE80211_FC0_TYPE_MASK
| IEEE80211_FC0_SUBTYPE_MASK
)) !=
4064 (IEEE80211_FC0_TYPE_MGT
| IEEE80211_FC0_SUBTYPE_PROBE_RESP
)) &&
4065 (!hasqos
|| (qos
& IEEE80211_QOS_ACKPOLICY
) != IEEE80211_QOS_ACKPOLICY_NOACK
))
4067 txwi
->bawin_size_xflags
|=
4068 (RT2870_TXWI_XFLAGS_ACK
<< RT2870_TXWI_XFLAGS_SHIFT
);
4070 if (ni
->ni_flags
& IEEE80211_NODE_HT
)
4072 /* preamble + plcp + signal extension */
4078 ackrate
= rt2870_ackrate(ic
, rate
);
4080 dur
= rt2870_txtime(RT2870_ACK_SIZE
, ackrate
, ic
->ic_flags
) +
4084 *(uint16_t *) wh
->i_dur
= htole16(dur
);
4087 /* ask MAC to insert timestamp into probe responses */
4089 if ((wh
->i_fc
[0] & (IEEE80211_FC0_TYPE_MASK
| IEEE80211_FC0_SUBTYPE_MASK
)) ==
4090 (IEEE80211_FC0_TYPE_MGT
| IEEE80211_FC0_SUBTYPE_PROBE_RESP
))
4091 txwi
->mpdu_density_flags
|=
4092 (RT2870_TXWI_FLAGS_TS
<< RT2870_TXWI_FLAGS_SHIFT
);
4094 if (bpf_peers_present(sc
->drvbpf
))
4098 tap
->flags
= IEEE80211_RADIOTAP_F_DATAPAD
;
4099 tap
->chan_flags
= htole32(ic
->ic_curchan
->ic_flags
);
4100 tap
->chan_freq
= htole16(ic
->ic_curchan
->ic_freq
);
4101 tap
->chan_ieee
= ic
->ic_curchan
->ic_ieee
;
4102 tap
->chan_maxpow
= 0;
4104 if (ni
->ni_flags
& IEEE80211_NODE_HT
)
4105 tap
->rate
= mcs
| IEEE80211_RATE_MCS
;
4109 if (mcs
& RT2870_TXWI_MCS_SHOTPRE
)
4110 tap
->flags
|= IEEE80211_RADIOTAP_F_SHORTPRE
;
4113 tap
->flags
|= IEEE80211_RADIOTAP_F_SHORTGI
;
4115 if (wh
->i_fc
[1] & IEEE80211_FC1_WEP
)
4116 tap
->flags
|= IEEE80211_RADIOTAP_F_WEP
;
4118 if (wh
->i_fc
[1] & IEEE80211_FC1_WEP
)
4120 wh
->i_fc
[1] &= ~IEEE80211_FC1_WEP
;
4122 bpf_mtap2(sc
->drvbpf
, tap
, sc
->txtap_len
, m
);
4124 wh
->i_fc
[1] |= IEEE80211_FC1_WEP
;
4128 bpf_mtap2(sc
->drvbpf
, tap
, sc
->txtap_len
, m
);
4132 m_copydata(m
, 0, m
->m_pkthdr
.len
, (caddr_t
) (txwi
+ 1));
4134 RT2870_DPRINTF(sc
, RT2870_DEBUG_TX
,
4135 "%s: sending frame: qid=%d, hdrsize=%d, hdrspace=%d, len=%d, "
4136 "bw=%d, stbc=%d, shortgi=%d, mcs=%d, wcid=0x%02x, DMA len=%d\n",
4137 device_get_nameunit(sc
->dev
),
4138 qid
, hdrsize
, hdrspace
, m
->m_pkthdr
.len
, bw
, stbc
, shortgi
, mcs
, wcid
, dmalen
);
4143 STAILQ_INSERT_TAIL(&ring
->active
, data
, next
);
4146 usbd_setup_xfer(data
->xfer
, ring
->usb_pipe
, ring
, data
->buf
, len
,
4147 USBD_FORCE_SHORT_XFER
| USBD_NO_COPY
, RT2870_USB_XFER_TIMEOUT
, rt2870_tx_intr
);
4149 usbd_transfer(data
->xfer
);
4157 static int rt2870_tx_raw(struct rt2870_softc
*sc
,
4158 struct mbuf
*m
, struct ieee80211_node
*ni
,
4159 const struct ieee80211_bpf_params
*params
)
4161 RT2870_DPRINTF(sc
, RT2870_DEBUG_TX
,
4163 device_get_nameunit(sc
->dev
));
4171 static void rt2870_rx_intr(usbd_xfer_handle xfer
,
4172 usbd_private_handle priv
, usbd_status status
)
4174 struct rt2870_softc
*sc
;
4176 struct rt2870_softc_rx_ring
*ring
;
4177 struct rt2870_softc_rx_data
*data
;
4182 ring
= &sc
->rx_ring
;
4184 if (!(ifp
->if_drv_flags
& IFF_DRV_RUNNING
))
4187 RT2870_DPRINTF(sc
, RT2870_DEBUG_INTR
,
4188 "%s: Rx interrupt: %s\n",
4189 device_get_nameunit(sc
->dev
), usbd_errstr(status
));
4192 sc
->rx_interrupts
++;
4196 case USBD_NORMAL_COMPLETION
:
4197 data
= STAILQ_FIRST(&ring
->active
);
4201 STAILQ_REMOVE_HEAD(&ring
->active
, next
);
4203 KASSERT(data
->xfer
== xfer
,
4204 ("%s: Rx interrupt: invalid USB xfer\n",
4205 device_get_nameunit(sc
->dev
)));
4207 usbd_get_xfer_status(xfer
, NULL
, NULL
, &len
, NULL
);
4211 STAILQ_INSERT_TAIL(&ring
->done
, data
, next
);
4213 taskqueue_enqueue(sc
->taskqueue
, &sc
->rx_done_task
);
4216 case USBD_CANCELLED
:
4217 case USBD_NOT_STARTED
:
4221 if (status
== USBD_STALLED
)
4222 usbd_clear_endpoint_stall_async(ring
->usb_pipe
);
4224 data
= STAILQ_FIRST(&ring
->active
);
4227 STAILQ_REMOVE_HEAD(&ring
->active
, next
);
4229 STAILQ_INSERT_TAIL(&ring
->active
, data
, next
);
4231 usbd_setup_xfer(xfer
, ring
->usb_pipe
, sc
, data
->buf
,
4232 RT2870_USB_RX_BULK_BUFLEN
, USBD_SHORT_XFER_OK
| USBD_NO_COPY
,
4233 USBD_NO_TIMEOUT
, rt2870_rx_intr
);
4235 usbd_transfer(xfer
);
4244 static void rt2870_tx_intr(usbd_xfer_handle xfer
,
4245 usbd_private_handle priv
, usbd_status status
)
4247 struct rt2870_softc
*sc
;
4249 struct rt2870_softc_tx_ring
*ring
;
4250 struct rt2870_softc_tx_data
*data
;
4256 if (!(ifp
->if_drv_flags
& IFF_DRV_RUNNING
))
4259 RT2870_DPRINTF(sc
, RT2870_DEBUG_INTR
,
4260 "%s: Tx interrupt: %s\n",
4261 device_get_nameunit(sc
->dev
), usbd_errstr(status
));
4264 sc
->tx_interrupts
[ring
->qid
]++;
4268 case USBD_NORMAL_COMPLETION
:
4269 data
= STAILQ_FIRST(&ring
->active
);
4273 STAILQ_REMOVE_HEAD(&ring
->active
, next
);
4275 STAILQ_INSERT_TAIL(&ring
->done
, data
, next
);
4277 sc
->tx_qid_pending_mask
|= (1 << ring
->qid
);
4279 taskqueue_enqueue(sc
->taskqueue
, &sc
->tx_done_task
);
4282 case USBD_CANCELLED
:
4283 case USBD_NOT_STARTED
:
4287 data
= STAILQ_FIRST(&ring
->active
);
4290 STAILQ_REMOVE_HEAD(&ring
->active
, next
);
4292 if (data
->m
!= NULL
)
4298 if (data
->ni
!= NULL
)
4300 ieee80211_free_node(data
->ni
);
4304 STAILQ_INSERT_TAIL(&ring
->inactive
, data
, next
);
4309 printf("%s: could not transmit buffer: qid=%d, status=%s\n",
4310 device_get_nameunit(sc
->dev
), ring
->qid
, usbd_errstr(status
));
4313 ifp
->if_drv_flags
&= ~IFF_DRV_OACTIVE
;
4315 if (status
== USBD_STALLED
)
4316 usbd_clear_endpoint_stall_async(ring
->usb_pipe
);
4322 * rt2870_rx_done_task
4324 static void rt2870_rx_done_task(void *context
, int pending
)
4326 struct rt2870_softc
*sc
;
4332 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
4333 "%s: Rx done task\n",
4334 device_get_nameunit(sc
->dev
));
4336 if (!(ifp
->if_drv_flags
& IFF_DRV_RUNNING
))
4339 if (rt2870_rx_eof(sc
, sc
->rx_process_limit
) != 0)
4341 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
4342 "%s: Rx done task: scheduling again\n",
4343 device_get_nameunit(sc
->dev
));
4345 taskqueue_enqueue(sc
->taskqueue
, &sc
->rx_done_task
);
4350 * rt2870_tx_done_task
4352 static void rt2870_tx_done_task(void *context
, int pending
)
4354 struct rt2870_softc
*sc
;
4361 RT2870_DPRINTF(sc
, RT2870_DEBUG_TX
,
4362 "%s: Tx done task\n",
4363 device_get_nameunit(sc
->dev
));
4365 if (!(ifp
->if_drv_flags
& IFF_DRV_RUNNING
))
4368 for (i
= sc
->usb_endpoints
- 2; i
>= 0; i
--)
4370 if (sc
->tx_qid_pending_mask
& (1 << i
))
4372 sc
->tx_qid_pending_mask
&= ~(1 << i
);
4374 rt2870_tx_eof(sc
, &sc
->tx_ring
[i
]);
4378 if (sc
->tx_qid_pending_mask
!= 0)
4380 RT2870_DPRINTF(sc
, RT2870_DEBUG_TX
,
4381 "%s: Tx done task: scheduling again\n",
4382 device_get_nameunit(sc
->dev
));
4384 taskqueue_enqueue(sc
->taskqueue
, &sc
->tx_done_task
);
4389 ifp
->if_drv_flags
&= ~IFF_DRV_OACTIVE
;
4393 * rt2870_periodic_task
4395 static void rt2870_periodic_task(void *context
, int pending
)
4397 struct rt2870_softc
*sc
;
4399 struct ieee80211com
*ic
;
4405 RT2870_DPRINTF(sc
, RT2870_DEBUG_PERIODIC
,
4406 "%s: periodic task: round=%lu\n",
4407 device_get_nameunit(sc
->dev
), sc
->periodic_round
);
4409 if (!(ifp
->if_drv_flags
& IFF_DRV_RUNNING
))
4412 sc
->periodic_round
++;
4414 rt2870_update_stats(sc
);
4416 if ((sc
->periodic_round
% 10) == 0)
4418 rt2870_update_raw_counters(sc
);
4420 rt2870_watchdog(sc
);
4422 if (ic
->ic_opmode
== IEEE80211_M_STA
)
4423 rt2870_amrr_update_iter_func(sc
, ic
->ic_bss
);
4425 ieee80211_iterate_nodes(&ic
->ic_sta
, rt2870_amrr_update_iter_func
, sc
);
4428 callout_reset(&sc
->periodic_ch
, hz
/ 10, rt2870_periodic
, sc
);
4434 static void rt2870_cmd_task(void *context
, int pending
)
4436 struct rt2870_softc
*sc
;
4437 struct rt2870_softc_cmd_ring
*ring
;
4438 struct rt2870_softc_cmd
*cmd
;
4441 ring
= &sc
->cmd_ring
;
4445 RT2870_SOFTC_LOCK(sc
);
4447 cmd
= STAILQ_FIRST(&ring
->active
);
4450 RT2870_SOFTC_UNLOCK(sc
);
4454 STAILQ_REMOVE_HEAD(&ring
->active
, next
);
4456 RT2870_SOFTC_UNLOCK(sc
);
4458 cmd
->cb(sc
, cmd
->data
);
4460 RT2870_SOFTC_LOCK(sc
);
4462 STAILQ_INSERT_TAIL(&ring
->inactive
, cmd
, next
);
4465 RT2870_SOFTC_UNLOCK(sc
);
4472 static int rt2870_rx_eof(struct rt2870_softc
*sc
, int limit
)
4475 struct rt2870_softc_rx_ring
*ring
;
4476 struct rt2870_softc_rx_data
*data
;
4482 ring
= &sc
->rx_ring
;
4488 RT2870_SOFTC_LOCK(sc
);
4490 data
= STAILQ_FIRST(&ring
->done
);
4493 RT2870_SOFTC_UNLOCK(sc
);
4497 STAILQ_REMOVE_HEAD(&ring
->done
, next
);
4499 RT2870_SOFTC_UNLOCK(sc
);
4504 if (len
< RT2870_RX_DESC_SIZE
)
4510 while (len
> (sizeof(uint32_t) + sizeof(struct rt2870_rxinfo
)))
4512 dmalen
= (le32toh(*(uint32_t *) buf
) & 0xffff);
4514 if (dmalen
== 0 || (dmalen
& 3) != 0)
4516 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
4517 "%s: bad DMA len=%u\n",
4518 device_get_nameunit(sc
->dev
), dmalen
);
4522 if ((dmalen
+ sizeof(uint32_t) + sizeof(struct rt2870_rxinfo
)) > len
)
4524 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
4525 "%s: bad DMA len: DMA len=%u, USB len=%d\n",
4526 device_get_nameunit(sc
->dev
),
4527 (unsigned int) (dmalen
+ sizeof(uint32_t) + sizeof(struct rt2870_rxinfo
)), len
);
4533 rt2870_rx_frame(sc
, buf
+ sizeof(uint32_t), dmalen
);
4535 buf
+= (dmalen
+ sizeof(uint32_t) + sizeof(struct rt2870_rxinfo
));
4536 len
-= (dmalen
+ sizeof(uint32_t) + sizeof(struct rt2870_rxinfo
));
4541 RT2870_SOFTC_LOCK(sc
);
4543 STAILQ_INSERT_TAIL(&ring
->active
, data
, next
);
4545 RT2870_SOFTC_UNLOCK(sc
);
4547 usbd_setup_xfer(data
->xfer
, ring
->usb_pipe
, sc
, data
->buf
,
4548 RT2870_USB_RX_BULK_BUFLEN
, USBD_SHORT_XFER_OK
| USBD_NO_COPY
,
4549 USBD_NO_TIMEOUT
, rt2870_rx_intr
);
4551 usbd_transfer(data
->xfer
);
4556 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
4557 "%s: Rx eof: nframes=%d\n",
4558 device_get_nameunit(sc
->dev
), nframes
);
4560 sc
->rx_packets
+= nframes
;
4562 return (limit
== 0);
4568 static void rt2870_tx_eof(struct rt2870_softc
*sc
,
4569 struct rt2870_softc_tx_ring
*ring
)
4572 struct rt2870_softc_tx_data
*data
;
4581 RT2870_SOFTC_LOCK(sc
);
4583 data
= STAILQ_FIRST(&ring
->done
);
4586 RT2870_SOFTC_UNLOCK(sc
);
4590 STAILQ_REMOVE_HEAD(&ring
->done
, next
);
4592 RT2870_SOFTC_UNLOCK(sc
);
4596 if (data
->m
->m_flags
& M_TXCB
)
4597 ieee80211_process_callback(data
->ni
, data
->m
, 0);
4601 ieee80211_free_node(data
->ni
);
4606 RT2870_SOFTC_LOCK(sc
);
4608 STAILQ_INSERT_TAIL(&ring
->inactive
, data
, next
);
4612 RT2870_SOFTC_UNLOCK(sc
);
4617 RT2870_DPRINTF(sc
, RT2870_DEBUG_TX
,
4618 "%s: Tx eof: qid=%d, nframes=%d\n",
4619 device_get_nameunit(sc
->dev
), ring
->qid
, nframes
);
4623 * rt2870_update_stats
4625 static void rt2870_update_stats(struct rt2870_softc
*sc
)
4628 struct ieee80211com
*ic
;
4629 struct ieee80211_node
*ni
;
4631 int beacons
, noretryok
, retryok
, failed
;
4638 RT2870_DPRINTF(sc
, RT2870_DEBUG_STATS
,
4639 "%s: update statistic\n",
4640 device_get_nameunit(sc
->dev
));
4642 rt2870_drain_fifo_stats(sc
);
4644 /* read and clear Tx statistic registers */
4646 rt2870_io_mac_read_multi(sc
, RT2870_REG_TX_STA_CNT0
,
4647 stacnt
, sizeof(stacnt
));
4649 stacnt
[0] = le32toh(stacnt
[0]);
4650 stacnt
[1] = le32toh(stacnt
[1]);
4651 stacnt
[2] = le32toh(stacnt
[2]);
4653 beacons
= stacnt
[0] >> 16;
4654 noretryok
= stacnt
[1] & 0xffff;
4655 retryok
= stacnt
[1] >> 16;
4656 failed
= stacnt
[0] & 0xffff;
4658 RT2870_DPRINTF(sc
, RT2870_DEBUG_STATS
,
4659 "%s: update statistic: beacons=%d, noretryok=%d, retryok=%d, failed=%d\n",
4660 device_get_nameunit(sc
->dev
),
4661 beacons
, noretryok
, retryok
, failed
);
4663 ifp
->if_oerrors
+= failed
;
4665 sc
->tx_beacons
+= beacons
;
4666 sc
->tx_noretryok
+= noretryok
;
4667 sc
->tx_retryok
+= retryok
;
4668 sc
->tx_failed
+= failed
;
4670 if (ic
->ic_opmode
== IEEE80211_M_STA
&& ic
->ic_state
== IEEE80211_S_RUN
)
4674 associd
= (ni
!= NULL
) ? ni
->ni_associd
: 0;
4675 wcid
= RT2870_AID2WCID(associd
);
4677 rt2870_amrr_tx_update(&sc
->amrr_node
[wcid
],
4678 noretryok
+ retryok
+ failed
, noretryok
+ retryok
, retryok
+ failed
);
4685 static void rt2870_watchdog(struct rt2870_softc
*sc
)
4690 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_PBF_TXRXQ_PCNT
);
4692 RT2870_DPRINTF(sc
, RT2870_DEBUG_WATCHDOG
,
4693 "%s: watchdog: TXRXQ_PCNT=0x%08x\n",
4694 device_get_nameunit(sc
->dev
), tmp
);
4696 if (((tmp
>> RT2870_REG_TX0Q_PCNT_SHIFT
) & RT2870_REG_TX0Q_PCNT_MASK
) != 0)
4698 sc
->tx_queue_not_empty
[0]++;
4700 rt2870_io_mac_write(sc
, RT2870_REG_PBF_CFG
, 0xf40012);
4702 for (ntries
= 0; ntries
< 10; ntries
++)
4704 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_PBF_TXRXQ_PCNT
);
4705 if (((tmp
>> RT2870_REG_TX0Q_PCNT_SHIFT
) & RT2870_REG_TX0Q_PCNT_MASK
) == 0)
4711 rt2870_io_mac_write(sc
, RT2870_REG_PBF_CFG
, 0xf40006);
4714 if (((tmp
>> RT2870_REG_TX1Q_PCNT_SHIFT
) & RT2870_REG_TX1Q_PCNT_MASK
) != 0)
4716 sc
->tx_queue_not_empty
[1]++;
4718 rt2870_io_mac_write(sc
, RT2870_REG_PBF_CFG
, 0xf4000a);
4720 for (ntries
= 0; ntries
< 10; ntries
++)
4722 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_PBF_TXRXQ_PCNT
);
4723 if (((tmp
>> RT2870_REG_TX1Q_PCNT_SHIFT
) & RT2870_REG_TX1Q_PCNT_MASK
) == 0)
4729 rt2870_io_mac_write(sc
, RT2870_REG_PBF_CFG
, 0xf40006);
4734 * rt2870_drain_fifo_stats
4736 static void rt2870_drain_fifo_stats(struct rt2870_softc
*sc
)
4740 uint8_t wcid
, mcs
, pid
;
4741 int ok
, agg
, retrycnt
;
4743 ifp
= sc
->ic
.ic_ifp
;
4745 /* drain Tx status FIFO (maxsize = 16) */
4747 while ((stats
= rt2870_io_mac_read(sc
, RT2870_REG_TX_STA_FIFO
)) &
4748 RT2870_REG_TX_STA_FIFO_VALID
)
4750 wcid
= (stats
>> RT2870_REG_TX_STA_FIFO_WCID_SHIFT
) &
4751 RT2870_REG_TX_STA_FIFO_WCID_MASK
;
4753 /* if no ACK was requested, no feedback is available */
4755 if (!(stats
& RT2870_REG_TX_STA_FIFO_ACK_REQ
) || wcid
== 0xff)
4758 /* update AMRR statistic */
4760 ok
= (stats
& RT2870_REG_TX_STA_FIFO_TX_OK
) ? 1 : 0;
4761 agg
= (stats
& RT2870_REG_TX_STA_FIFO_AGG
) ? 1 : 0;
4762 mcs
= (stats
>> RT2870_REG_TX_STA_FIFO_MCS_SHIFT
) &
4763 RT2870_REG_TX_STA_FIFO_MCS_MASK
;
4764 pid
= (stats
>> RT2870_REG_TX_STA_FIFO_PID_SHIFT
) &
4765 RT2870_REG_TX_STA_FIFO_PID_MASK
;
4766 retrycnt
= pid
- mcs
;
4768 RT2870_DPRINTF(sc
, RT2870_DEBUG_STATS
,
4769 "%s: FIFO statistic: wcid=0x%02x, ok=%d, agg=%d, mcs=0x%02x, pid=0x%02x, retrycnt=%d\n",
4770 device_get_nameunit(sc
->dev
),
4771 wcid
, ok
, agg
, mcs
, pid
, retrycnt
);
4773 rt2870_amrr_tx_complete(&sc
->amrr_node
[wcid
], ok
, retrycnt
);
4781 * rt2870_update_raw_counters
4783 static void rt2870_update_raw_counters(struct rt2870_softc
*sc
)
4787 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_AGG_CNT
);
4789 sc
->tx_nonagg
+= tmp
& 0xffff;
4790 sc
->tx_agg
+= tmp
>> 16;
4792 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_AGG_CNT0
);
4794 sc
->tx_ampdu
+= (tmp
& 0xffff) / 1 + (tmp
>> 16) / 2;
4796 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_AGG_CNT1
);
4798 sc
->tx_ampdu
+= (tmp
& 0xffff) / 3 + (tmp
>> 16) / 4;
4800 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_AGG_CNT2
);
4802 sc
->tx_ampdu
+= (tmp
& 0xffff) / 5 + (tmp
>> 16) / 6;
4804 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_AGG_CNT3
);
4806 sc
->tx_ampdu
+= (tmp
& 0xffff) / 7 + (tmp
>> 16) / 8;
4808 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_AGG_CNT4
);
4810 sc
->tx_ampdu
+= (tmp
& 0xffff) / 9 + (tmp
>> 16) / 10;
4812 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_AGG_CNT5
);
4814 sc
->tx_ampdu
+= (tmp
& 0xffff) / 11 + (tmp
>> 16) / 12;
4816 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_AGG_CNT6
);
4818 sc
->tx_ampdu
+= (tmp
& 0xffff) / 13 + (tmp
>> 16) / 14;
4820 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_AGG_CNT7
);
4822 sc
->tx_ampdu
+= (tmp
& 0xffff) / 15 + (tmp
>> 16) / 16;
4824 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_RX_STA_CNT0
);
4826 sc
->rx_crc_errors
+= tmp
& 0xffff;
4827 sc
->rx_phy_errors
+= tmp
>> 16;
4829 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_RX_STA_CNT1
);
4831 sc
->rx_false_ccas
+= tmp
& 0xffff;
4832 sc
->rx_plcp_errors
+= tmp
>> 16;
4834 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_RX_STA_CNT2
);
4836 sc
->rx_dup_packets
+= tmp
& 0xffff;
4837 sc
->rx_fifo_overflows
+= tmp
>> 16;
4841 * rt2870_alloc_rx_ring
4843 static int rt2870_alloc_rx_ring(struct rt2870_softc
*sc
,
4844 struct rt2870_softc_rx_ring
*ring
)
4846 struct rt2870_softc_rx_data
*data
;
4849 STAILQ_INIT(&ring
->active
);
4850 STAILQ_INIT(&ring
->done
);
4852 for (i
= 0; i
< RT2870_SOFTC_RX_RING_DATA_COUNT
; i
++)
4854 data
= &ring
->data
[i
];
4856 data
->xfer
= usbd_alloc_xfer(sc
->usb_dev
);
4857 if (data
->xfer
== NULL
)
4859 printf("%s: could not allocate Rx xfer\n",
4860 device_get_nameunit(sc
->dev
));
4865 data
->buf
= usbd_alloc_buffer(data
->xfer
, RT2870_USB_RX_BULK_BUFLEN
);
4866 if (data
->buf
== NULL
)
4868 printf("%s: could not allocate Rx buffer\n",
4869 device_get_nameunit(sc
->dev
));
4879 rt2870_free_rx_ring(sc
, ring
);
4885 * rt2870_reset_rx_ring
4887 static void rt2870_reset_rx_ring(struct rt2870_softc
*sc
,
4888 struct rt2870_softc_rx_ring
*ring
)
4890 STAILQ_INIT(&ring
->active
);
4891 STAILQ_INIT(&ring
->done
);
4895 * rt2870_free_rx_ring
4897 static void rt2870_free_rx_ring(struct rt2870_softc
*sc
,
4898 struct rt2870_softc_rx_ring
*ring
)
4900 struct rt2870_softc_rx_data
*data
;
4903 for (i
= 0; i
< RT2870_SOFTC_RX_RING_DATA_COUNT
; i
++)
4905 data
= &ring
->data
[i
];
4907 if (data
->xfer
!= NULL
)
4909 usbd_free_xfer(data
->xfer
);
4916 * rt2870_alloc_tx_ring
4918 static int rt2870_alloc_tx_ring(struct rt2870_softc
*sc
,
4919 struct rt2870_softc_tx_ring
*ring
, int qid
)
4921 struct rt2870_softc_tx_data
*data
;
4924 STAILQ_INIT(&ring
->inactive
);
4925 STAILQ_INIT(&ring
->active
);
4926 STAILQ_INIT(&ring
->done
);
4932 for (i
= 0; i
< RT2870_SOFTC_TX_RING_DATA_COUNT
; i
++)
4934 data
= &ring
->data
[i
];
4936 data
->xfer
= usbd_alloc_xfer(sc
->usb_dev
);
4937 if (data
->xfer
== NULL
)
4939 printf("%s: could not allocate Tx xfer\n",
4940 device_get_nameunit(sc
->dev
));
4945 data
->buf
= usbd_alloc_buffer(data
->xfer
,
4946 RT2870_TX_DESC_SIZE
+ MJUMPAGESIZE
);
4947 if (data
->buf
== NULL
)
4949 printf("%s: could not allocate Tx buffer\n",
4950 device_get_nameunit(sc
->dev
));
4955 memset(data
->buf
, 0, RT2870_TX_DESC_SIZE
);
4957 STAILQ_INSERT_TAIL(&ring
->inactive
, data
, next
);
4964 rt2870_free_tx_ring(sc
, ring
);
4970 * rt2870_reset_tx_ring
4972 static void rt2870_reset_tx_ring(struct rt2870_softc
*sc
,
4973 struct rt2870_softc_tx_ring
*ring
)
4975 struct rt2870_softc_tx_data
*data
;
4978 STAILQ_INIT(&ring
->inactive
);
4979 STAILQ_INIT(&ring
->active
);
4980 STAILQ_INIT(&ring
->done
);
4984 for (i
= 0; i
< RT2870_SOFTC_TX_RING_DATA_COUNT
; i
++)
4986 data
= &ring
->data
[i
];
4988 if (data
->m
!= NULL
)
4994 if (data
->ni
!= NULL
)
4996 ieee80211_free_node(data
->ni
);
5000 STAILQ_INSERT_TAIL(&ring
->inactive
, data
, next
);
5005 * rt2870_free_tx_ring
5007 static void rt2870_free_tx_ring(struct rt2870_softc
*sc
,
5008 struct rt2870_softc_tx_ring
*ring
)
5010 struct rt2870_softc_tx_data
*data
;
5013 for (i
= 0; i
< RT2870_SOFTC_TX_RING_DATA_COUNT
; i
++)
5015 data
= &ring
->data
[i
];
5017 if (data
->xfer
!= NULL
)
5019 usbd_free_xfer(data
->xfer
);
5023 if (data
->m
!= NULL
)
5029 if (data
->ni
!= NULL
)
5031 ieee80211_free_node(data
->ni
);
5038 * rt2870_reset_cmd_ring
5040 static void rt2870_reset_cmd_ring(struct rt2870_softc
*sc
,
5041 struct rt2870_softc_cmd_ring
*ring
)
5043 struct rt2870_softc_cmd
*cmd
;
5046 STAILQ_INIT(&ring
->inactive
);
5047 STAILQ_INIT(&ring
->active
);
5051 for (i
= 0; i
< RT2870_SOFTC_CMD_RING_CMD_COUNT
; i
++)
5053 cmd
= &ring
->cmd
[i
];
5055 STAILQ_INSERT_TAIL(&ring
->inactive
, cmd
, next
);
5060 * rt2870_sysctl_attach
5062 static void rt2870_sysctl_attach(struct rt2870_softc
*sc
)
5064 struct sysctl_ctx_list
*ctx
;
5065 struct sysctl_oid
*tree
;
5066 struct sysctl_oid
*stats
;
5068 ctx
= device_get_sysctl_ctx(sc
->dev
);
5069 tree
= device_get_sysctl_tree(sc
->dev
);
5071 stats
= SYSCTL_ADD_NODE(ctx
, SYSCTL_CHILDREN(tree
), OID_AUTO
,
5072 "stats", CTLFLAG_RD
, 0, "statistic");
5074 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5075 "interrupts", CTLFLAG_RD
, &sc
->interrupts
, 0,
5078 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5079 "rx_interrupts", CTLFLAG_RD
, &sc
->rx_interrupts
, 0,
5082 if (sc
->usb_endpoints
== (RT2870_SOFTC_TX_RING_COUNT
+ 1))
5084 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5085 "tx_mgmt_interrupts", CTLFLAG_RD
, &sc
->tx_interrupts
[5], 0,
5086 "Tx MGMT interrupts");
5088 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5089 "tx_hcca_interrupts", CTLFLAG_RD
, &sc
->tx_interrupts
[4], 0,
5090 "Tx HCCA interrupts");
5093 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5094 "tx_ac3_interrupts", CTLFLAG_RD
, &sc
->tx_interrupts
[3], 0,
5095 "Tx AC3 interrupts");
5097 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5098 "tx_ac2_interrupts", CTLFLAG_RD
, &sc
->tx_interrupts
[2], 0,
5099 "Tx AC2 interrupts");
5101 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5102 "tx_ac1_interrupts", CTLFLAG_RD
, &sc
->tx_interrupts
[1], 0,
5103 "Tx AC1 interrupts");
5105 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5106 "tx_ac0_interrupts", CTLFLAG_RD
, &sc
->tx_interrupts
[0], 0,
5107 "Tx AC0 interrupts");
5109 if (sc
->usb_endpoints
== (RT2870_SOFTC_TX_RING_COUNT
+ 1))
5111 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5112 "tx_mgmt_data_queued", CTLFLAG_RD
, &sc
->tx_ring
[5].queued
, 0,
5113 "Tx MGMT data queued");
5115 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5116 "tx_hcca_data_queued", CTLFLAG_RD
, &sc
->tx_ring
[4].queued
, 0,
5117 "Tx HCCA data queued");
5120 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5121 "tx_ac3_data_queued", CTLFLAG_RD
, &sc
->tx_ring
[3].queued
, 0,
5122 "Tx AC3 data queued");
5124 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5125 "tx_ac2_data_queued", CTLFLAG_RD
, &sc
->tx_ring
[2].queued
, 0,
5126 "Tx AC2 data queued");
5128 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5129 "tx_ac1_data_queued", CTLFLAG_RD
, &sc
->tx_ring
[1].queued
, 0,
5130 "Tx AC1 data queued");
5132 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5133 "tx_ac0_data_queued", CTLFLAG_RD
, &sc
->tx_ring
[0].queued
, 0,
5134 "Tx AC0 data queued");
5136 if (sc
->usb_endpoints
== (RT2870_SOFTC_TX_RING_COUNT
+ 1))
5138 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5139 "tx_mgmt_data_queue_full", CTLFLAG_RD
, &sc
->tx_data_queue_full
[5], 0,
5140 "Tx MGMT data queue full");
5142 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5143 "tx_hcca_data_queue_full", CTLFLAG_RD
, &sc
->tx_data_queue_full
[4], 0,
5144 "Tx HCCA data queue full");
5147 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5148 "tx_ac3_data_queue_full", CTLFLAG_RD
, &sc
->tx_data_queue_full
[3], 0,
5149 "Tx AC3 data queue full");
5151 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5152 "tx_ac2_data_queue_full", CTLFLAG_RD
, &sc
->tx_data_queue_full
[2], 0,
5153 "Tx AC2 data queue full");
5155 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5156 "tx_ac1_data_queue_full", CTLFLAG_RD
, &sc
->tx_data_queue_full
[1], 0,
5157 "Tx AC1 data queue full");
5159 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5160 "tx_ac0_data_queue_full", CTLFLAG_RD
, &sc
->tx_data_queue_full
[0], 0,
5161 "Tx AC0 data queue full");
5163 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5164 "tx_watchdog_timeouts", CTLFLAG_RD
, &sc
->tx_watchdog_timeouts
, 0,
5165 "Tx watchdog timeouts");
5167 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5168 "rx_mbuf_alloc_errors", CTLFLAG_RD
, &sc
->rx_mbuf_alloc_errors
, 0,
5169 "Rx mbuf allocation errors");
5171 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5172 "tx_queue_0_not_empty", CTLFLAG_RD
, &sc
->tx_queue_not_empty
[0], 0,
5173 "Tx queue 0 not empty");
5175 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5176 "tx_queue_1_not_empty", CTLFLAG_RD
, &sc
->tx_queue_not_empty
[1], 0,
5177 "Tx queue 1 not empty");
5179 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5180 "tx_beacons", CTLFLAG_RD
, &sc
->tx_beacons
, 0,
5183 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5184 "tx_noretryok", CTLFLAG_RD
, &sc
->tx_noretryok
, 0,
5185 "Tx successfull without retries");
5187 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5188 "tx_retryok", CTLFLAG_RD
, &sc
->tx_retryok
, 0,
5189 "Tx successfull with retries");
5191 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5192 "tx_failed", CTLFLAG_RD
, &sc
->tx_failed
, 0,
5195 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5196 "tx_nonagg", CTLFLAG_RD
, &sc
->tx_nonagg
, 0,
5197 "Tx non-aggregated");
5199 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5200 "tx_agg", CTLFLAG_RD
, &sc
->tx_agg
, 0,
5203 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5204 "tx_ampdu", CTLFLAG_RD
, &sc
->tx_ampdu
, 0,
5207 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5208 "rx_packets", CTLFLAG_RD
, &sc
->rx_packets
, 0,
5211 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5212 "rx_ampdu", CTLFLAG_RD
, &sc
->rx_ampdu
, 0,
5215 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5216 "rx_amsdu", CTLFLAG_RD
, &sc
->rx_amsdu
, 0,
5219 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5220 "rx_crc_errors", CTLFLAG_RD
, &sc
->rx_crc_errors
, 0,
5223 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5224 "rx_phy_errors", CTLFLAG_RD
, &sc
->rx_phy_errors
, 0,
5227 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5228 "rx_false_ccas", CTLFLAG_RD
, &sc
->rx_false_ccas
, 0,
5231 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5232 "rx_plcp_errors", CTLFLAG_RD
, &sc
->rx_plcp_errors
, 0,
5235 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5236 "rx_dup_packets", CTLFLAG_RD
, &sc
->rx_dup_packets
, 0,
5237 "Rx duplicate packets");
5239 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5240 "rx_fifo_overflows", CTLFLAG_RD
, &sc
->rx_fifo_overflows
, 0,
5241 "Rx FIFO overflows");
5244 static device_method_t rt2870_dev_methods
[] =
5246 DEVMETHOD(device_probe
, rt2870_probe
),
5247 DEVMETHOD(device_attach
, rt2870_attach
),
5248 DEVMETHOD(device_detach
, rt2870_detach
),
5252 static driver_t rt2870_driver
=
5256 sizeof(struct rt2870_softc
),
5259 static devclass_t rt2870_dev_class
;
5261 DRIVER_MODULE(rt2870
, uhub
, rt2870_driver
, rt2870_dev_class
,
5262 usbd_driver_load
, 0);
5264 MODULE_DEPEND(rt2870
, usb
, 1, 1, 1);
5265 MODULE_DEPEND(rt2870
, wlan
, 1, 1, 1);