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 /* enable Tx/Rx DMA engine */
1381 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_SCHDMA_USB_CYC_CFG
);
1386 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_USB_CYC_CFG
, tmp
);
1388 if ((sc
->mac_rev
& 0xffff) != 0x0101)
1389 rt2870_io_mac_write(sc
, RT2870_REG_TX_TXOP_CTRL_CFG
, 0x583f);
1391 rt2870_io_mac_write(sc
, RT2870_REG_SYS_CTRL
, RT2870_REG_TX_ENABLE
);
1393 for (ntries
= 0; ntries
< 200; ntries
++)
1395 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_SCHDMA_WPDMA_GLO_CFG
);
1396 if (!(tmp
& (RT2870_REG_TX_DMA_BUSY
| RT2870_REG_RX_DMA_BUSY
)))
1404 printf("%s: timeout waiting for DMA engine\n",
1405 device_get_nameunit(sc
->dev
));
1411 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_WMM_TXOP0_CFG
, 0);
1412 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_WMM_TXOP1_CFG
,
1415 tmp
|= RT2870_REG_TX_WB_DDONE
|
1416 RT2870_REG_RX_DMA_ENABLE
|
1417 RT2870_REG_TX_DMA_ENABLE
;
1419 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_WPDMA_GLO_CFG
, tmp
);
1421 tmp
= RT2870_REG_USB_DMA_TX_ENABLE
|
1422 RT2870_REG_USB_DMA_RX_ENABLE
|
1423 RT2870_REG_USB_DMA_RX_AGG_ENABLE
|
1424 /* Rx agg limit in unit of 1024 byte */
1425 ((RT2870_USB_RX_BULK_BUFLEN
/ 1024 - 3) << RT2870_REG_USB_DMA_RX_AGG_LIMIT_SHIFT
) |
1426 /* Rx agg timeout in unit of 33ns */
1429 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_USB_DMA_CFG
, tmp
);
1433 tmp
= RT2870_REG_RX_FILTER_DROP_CRC_ERR
|
1434 RT2870_REG_RX_FILTER_DROP_PHY_ERR
;
1436 if (ic
->ic_opmode
!= IEEE80211_M_MONITOR
)
1438 tmp
|= RT2870_REG_RX_FILTER_DROP_DUPL
|
1439 RT2870_REG_RX_FILTER_DROP_CTS
|
1440 RT2870_REG_RX_FILTER_DROP_BA
|
1441 RT2870_REG_RX_FILTER_DROP_ACK
|
1442 RT2870_REG_RX_FILTER_DROP_VER_ERR
|
1443 RT2870_REG_RX_FILTER_DROP_CTRL_RSV
|
1444 RT2870_REG_RX_FILTER_DROP_CFACK
|
1445 RT2870_REG_RX_FILTER_DROP_CFEND
;
1447 if (ic
->ic_opmode
== IEEE80211_M_STA
)
1448 tmp
|= RT2870_REG_RX_FILTER_DROP_RTS
|
1449 RT2870_REG_RX_FILTER_DROP_PSPOLL
;
1451 if (!(ifp
->if_flags
& IFF_PROMISC
))
1452 tmp
|= RT2870_REG_RX_FILTER_DROP_UC_NOME
;
1455 rt2870_io_mac_write(sc
, RT2870_REG_RX_FILTER_CFG
, tmp
);
1457 rt2870_io_mac_write(sc
, RT2870_REG_SYS_CTRL
,
1458 RT2870_REG_RX_ENABLE
| RT2870_REG_TX_ENABLE
);
1460 /* clear garbage interrupts */
1462 tmp
= rt2870_io_mac_read(sc
, 0x1300);
1464 taskqueue_unblock(sc
->taskqueue
);
1466 /* init Tx and Rx rings */
1468 for(i
= 0; i
< sc
->usb_endpoints
- 1; i
++)
1469 rt2870_reset_tx_ring(sc
, &sc
->tx_ring
[i
]);
1471 rt2870_reset_rx_ring(sc
, &sc
->rx_ring
);
1473 rt2870_reset_cmd_ring(sc
, &sc
->cmd_ring
);
1475 /* start up the receive pipe */
1477 for (i
= 0; i
< RT2870_SOFTC_RX_RING_DATA_COUNT
; i
++)
1479 data
= &sc
->rx_ring
.data
[i
];
1481 STAILQ_INSERT_TAIL(&sc
->rx_ring
.active
, data
, next
);
1483 usbd_setup_xfer(data
->xfer
, sc
->rx_ring
.usb_pipe
, sc
, data
->buf
,
1484 RT2870_USB_RX_BULK_BUFLEN
, USBD_SHORT_XFER_OK
| USBD_NO_COPY
,
1485 USBD_NO_TIMEOUT
, rt2870_rx_intr
);
1487 usbd_transfer(data
->xfer
);
1490 ifp
->if_drv_flags
&= ~IFF_DRV_OACTIVE
;
1491 ifp
->if_drv_flags
|= IFF_DRV_RUNNING
;
1493 if (ic
->ic_opmode
!= IEEE80211_M_MONITOR
)
1495 if (ic
->ic_roaming
!= IEEE80211_ROAMING_MANUAL
)
1496 ieee80211_new_state(ic
, IEEE80211_S_SCAN
, -1);
1500 ieee80211_new_state(ic
, IEEE80211_S_RUN
, -1);
1503 sc
->periodic_round
= 0;
1505 callout_reset(&sc
->periodic_ch
, hz
/ 10, rt2870_periodic
, sc
);
1511 rt2870_stop_locked(sc
);
1517 static void rt2870_init(void *priv
)
1519 struct rt2870_softc
*sc
;
1523 rt2870_init_locked(sc
);
1529 static int rt2870_init_bbp(struct rt2870_softc
*sc
)
1534 for (ntries
= 0; ntries
< 20; ntries
++)
1536 tmp
= rt2870_io_bbp_read(sc
, 0);
1537 if (tmp
!= 0x00 && tmp
!= 0xff)
1541 if (tmp
== 0x00 || tmp
== 0xff)
1543 printf("%s: timeout waiting for BBP to wakeup\n",
1544 device_get_nameunit(sc
->dev
));
1548 for (i
= 0; i
< RT2870_DEF_BBP_SIZE
; i
++)
1549 rt2870_io_bbp_write(sc
, rt2870_def_bbp
[i
].reg
,
1550 rt2870_def_bbp
[i
].val
);
1552 if ((sc
->mac_rev
& 0xffff) != 0x0101)
1553 rt2870_io_bbp_write(sc
, 84, 0x19);
1561 static void rt2870_stop_locked(void *priv
)
1563 struct rt2870_softc
*sc
;
1564 struct ieee80211com
*ic
;
1573 RT2870_DPRINTF(sc
, RT2870_DEBUG_ANY
,
1575 device_get_nameunit(sc
->dev
));
1579 if (ifp
->if_drv_flags
& IFF_DRV_RUNNING
)
1580 rt2870_led_cmd(sc
, RT2870_LED_CMD_RADIO_OFF
);
1582 ifp
->if_drv_flags
&= ~(IFF_DRV_RUNNING
| IFF_DRV_OACTIVE
);
1584 ieee80211_new_state(ic
, IEEE80211_S_INIT
, -1);
1586 callout_stop(&sc
->periodic_ch
);
1587 callout_stop(&sc
->tx_watchdog_ch
);
1589 taskqueue_block(sc
->taskqueue
);
1591 taskqueue_drain(sc
->taskqueue
, &sc
->rx_done_task
);
1592 taskqueue_drain(sc
->taskqueue
, &sc
->tx_done_task
);
1593 taskqueue_drain(sc
->taskqueue
, &sc
->periodic_task
);
1594 taskqueue_drain(sc
->taskqueue
, &sc
->cmd_task
);
1596 /* clear key tables */
1598 rt2870_asic_clear_keytables(sc
);
1602 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_SYS_CTRL
);
1604 tmp
&= ~(RT2870_REG_RX_ENABLE
| RT2870_REG_TX_ENABLE
);
1606 rt2870_io_mac_write(sc
, RT2870_REG_SYS_CTRL
, tmp
);
1608 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_USB_DMA_CFG
, 0);
1612 rt2870_io_mac_write(sc
, RT2870_REG_SYS_CTRL
,
1613 RT2870_REG_MAC_SRST
| RT2870_REG_BBP_HRST
);
1614 rt2870_io_mac_write(sc
, RT2870_REG_SYS_CTRL
, 0);
1616 /* abort any pending transfers */
1618 if (sc
->rx_ring
.usb_pipe
!= NULL
)
1619 usbd_abort_pipe(sc
->rx_ring
.usb_pipe
);
1621 for (i
= 0; i
< sc
->usb_endpoints
- 1; i
++)
1622 if (sc
->tx_ring
[i
].usb_pipe
!= NULL
)
1623 usbd_abort_pipe(sc
->tx_ring
[i
].usb_pipe
);
1629 static void rt2870_stop(void *priv
)
1631 struct rt2870_softc
*sc
;
1635 rt2870_stop_locked(sc
);
1641 static void rt2870_start(struct ifnet
*ifp
)
1643 struct rt2870_softc
*sc
;
1644 struct ieee80211com
*ic
;
1645 struct ieee80211_node
*ni
;
1646 struct ether_header
*eh
;
1653 if (!(ifp
->if_drv_flags
& IFF_DRV_RUNNING
))
1658 IF_POLL(&ic
->ic_mgtq
, m
);
1661 if (sc
->tx_ring
[sc
->tx_ring_mgtqid
].queued
>= RT2870_SOFTC_TX_RING_DATA_COUNT
)
1663 RT2870_DPRINTF(sc
, RT2870_DEBUG_TX
,
1664 "%s: if_start: Tx ring with qid=%d is full\n",
1665 device_get_nameunit(sc
->dev
), sc
->tx_ring_mgtqid
);
1667 ifp
->if_drv_flags
|= IFF_DRV_OACTIVE
;
1669 sc
->tx_data_queue_full
[sc
->tx_ring_mgtqid
]++;
1674 IF_DEQUEUE(&ic
->ic_mgtq
, m
);
1676 ni
= (struct ieee80211_node
*) m
->m_pkthdr
.rcvif
;
1677 m
->m_pkthdr
.rcvif
= NULL
;
1679 if (bpf_peers_present(ic
->ic_rawbpf
))
1680 bpf_mtap(ic
->ic_rawbpf
, m
);
1682 if (rt2870_tx_frame(sc
, m
, ni
, sc
->tx_ring_mgtqid
) != 0)
1687 if (ic
->ic_state
!= IEEE80211_S_RUN
)
1690 IF_POLL(&ifp
->if_snd
, m
);
1694 IFQ_DRV_DEQUEUE(&ifp
->if_snd
, m
);
1696 if (ic
->ic_flags
& IEEE80211_F_SCAN
)
1697 ieee80211_cancel_scan(ic
);
1699 if (m
->m_len
< sizeof(struct ether_header
) &&
1700 !(m
= m_pullup(m
, sizeof (struct ether_header
))))
1703 eh
= mtod(m
, struct ether_header
*);
1705 ni
= ieee80211_find_txnode(ic
, eh
->ether_dhost
);
1708 RT2870_DPRINTF(sc
, RT2870_DEBUG_TX
,
1709 "%s: if_start: could not find Tx node\n",
1710 device_get_nameunit(sc
->dev
));
1717 ieee80211_classify(ic
, m
, ni
);
1719 qid
= M_WME_GETAC(m
);
1721 if (sc
->tx_ring
[qid
].queued
>= RT2870_SOFTC_TX_RING_DATA_COUNT
)
1723 RT2870_DPRINTF(sc
, RT2870_DEBUG_TX
,
1724 "%s: if_start: Tx ring with qid=%d is full\n",
1725 device_get_nameunit(sc
->dev
), qid
);
1728 ieee80211_free_node(ni
);
1730 ifp
->if_drv_flags
|= IFF_DRV_OACTIVE
;
1733 sc
->tx_data_queue_full
[qid
]++;
1740 m
= ieee80211_encap(ic
, m
, ni
);
1743 ieee80211_free_node(ni
);
1750 if (bpf_peers_present(ic
->ic_rawbpf
))
1751 bpf_mtap(ic
->ic_rawbpf
, m
);
1753 if (rt2870_tx_frame(sc
, m
, ni
, qid
) != 0)
1755 ieee80211_free_node(ni
);
1757 ifp
->if_drv_flags
|= IFF_DRV_OACTIVE
;
1764 sc
->tx_timer
= RT2870_TX_WATCHDOG_TIMEOUT
;
1766 ic
->ic_lastdata
= ticks
;
1768 callout_reset(&sc
->tx_watchdog_ch
, hz
, rt2870_tx_watchdog
, sc
);
1775 static int rt2870_ioctl(struct ifnet
*ifp
, u_long cmd
, caddr_t data
)
1777 struct rt2870_softc
*sc
;
1778 struct ieee80211com
*ic
;
1789 if (ifp
->if_flags
& IFF_UP
)
1791 if (ifp
->if_drv_flags
& IFF_DRV_RUNNING
)
1793 if ((ifp
->if_flags
^ sc
->if_flags
) & IFF_PROMISC
)
1794 rt2870_asic_update_promisc(sc
);
1798 rt2870_init_locked(sc
);
1803 if (ifp
->if_drv_flags
& IFF_DRV_RUNNING
)
1804 rt2870_stop_locked(sc
);
1807 sc
->if_flags
= ifp
->if_flags
;
1811 error
= ieee80211_ioctl(ic
, cmd
, data
);
1814 if (error
== ENETRESET
)
1816 if ((ifp
->if_flags
& IFF_UP
) &&
1817 (ifp
->if_drv_flags
& IFF_DRV_RUNNING
) &&
1818 (ic
->ic_roaming
!= IEEE80211_ROAMING_MANUAL
))
1820 rt2870_stop_locked(sc
);
1821 rt2870_init_locked(sc
);
1833 static int rt2870_reset(struct ifnet
*ifp
)
1835 struct rt2870_softc
*sc
;
1836 struct ieee80211com
*ic
;
1841 if (ic
->ic_opmode
!= IEEE80211_M_MONITOR
)
1844 rt2870_rf_set_chan(sc
, ic
->ic_curchan
);
1852 static int rt2870_newstate(struct ieee80211com
*ic
,
1853 enum ieee80211_state nstate
, int arg
)
1855 struct rt2870_softc
*sc
;
1857 struct rt2870_cmd_argv_newstate cmd_argv
;
1862 cmd_argv
.nstate
= nstate
;
1865 rt2870_do_async(sc
, rt2870_newstate_cb
, &cmd_argv
,
1866 sizeof(struct rt2870_cmd_argv_newstate
));
1874 static void rt2870_scan_start(struct ieee80211com
*ic
)
1876 struct rt2870_softc
*sc
;
1882 rt2870_do_async(sc
, rt2870_scan_start_cb
, NULL
, 0);
1888 static void rt2870_scan_end(struct ieee80211com
*ic
)
1890 struct rt2870_softc
*sc
;
1896 rt2870_do_async(sc
, rt2870_scan_end_cb
, NULL
, 0);
1900 * rt2870_set_channel
1902 static void rt2870_set_channel(struct ieee80211com
*ic
)
1904 struct rt2870_softc
*sc
;
1910 rt2870_do_async(sc
, rt2870_set_channel_cb
, NULL
, 0);
1916 static void rt2870_newassoc(struct ieee80211_node
*ni
, int isnew
)
1918 struct rt2870_softc
*sc
;
1919 struct ieee80211com
*ic
;
1921 struct rt2870_cmd_argv_newassoc cmd_argv
;
1927 cmd_argv
.associd
= (ni
!= NULL
) ? ni
->ni_associd
: 0;
1928 cmd_argv
.isnew
= isnew
;
1929 memcpy(cmd_argv
.macaddr
, ni
->ni_macaddr
, IEEE80211_ADDR_LEN
);
1931 rt2870_do_async(sc
, rt2870_newassoc_cb
, &cmd_argv
,
1932 sizeof(struct rt2870_cmd_argv_newassoc
));
1938 static void rt2870_updateslot(struct ifnet
*ifp
)
1940 struct rt2870_softc
*sc
;
1944 rt2870_do_async(sc
, rt2870_updateslot_cb
, NULL
, 0);
1950 static int rt2870_wme_update(struct ieee80211com
*ic
)
1952 struct rt2870_softc
*sc
;
1958 rt2870_do_async(sc
, rt2870_wme_update_cb
, NULL
, 0);
1964 * rt2870_update_beacon
1966 static void rt2870_update_beacon(struct ieee80211com
*ic
, int what
)
1968 struct rt2870_softc
*sc
;
1970 struct rt2870_cmd_argv_updatebeacon cmd_argv
;
1975 cmd_argv
.what
= what
;
1977 rt2870_do_async(sc
, rt2870_update_beacon_cb
, &cmd_argv
,
1978 sizeof(struct rt2870_cmd_argv_updatebeacon
));
1982 * rt2870_key_update_begin
1984 static void rt2870_key_update_begin(struct ieee80211com
*ic
)
1986 struct rt2870_softc
*sc
;
1992 rt2870_do_async(sc
, rt2870_key_update_begin_cb
, NULL
, 0);
1996 * rt2870_key_update_end
1998 static void rt2870_key_update_end(struct ieee80211com
*ic
)
2000 struct rt2870_softc
*sc
;
2006 rt2870_do_async(sc
, rt2870_key_update_end_cb
, NULL
, 0);
2012 static int rt2870_key_set(struct ieee80211com
*ic
,
2013 const struct ieee80211_key
*k
, const uint8_t mac
[IEEE80211_ADDR_LEN
])
2015 struct rt2870_softc
*sc
;
2017 struct ieee80211_node
*ni
;
2018 struct rt2870_cmd_argv_keyset cmd_argv
;
2020 if (k
->wk_cipher
->ic_cipher
!= IEEE80211_CIPHER_WEP
&&
2021 k
->wk_cipher
->ic_cipher
!= IEEE80211_CIPHER_TKIP
&&
2022 k
->wk_cipher
->ic_cipher
!= IEEE80211_CIPHER_AES_CCM
)
2028 if (ic
->ic_opmode
!= IEEE80211_M_HOSTAP
)
2031 ni
= ieee80211_find_node(&ic
->ic_sta
, mac
);
2033 memcpy(&cmd_argv
.key
, k
, sizeof(struct ieee80211_key
));
2034 cmd_argv
.associd
= (ni
!= NULL
) ? ni
->ni_associd
: 0;
2036 if ((ic
->ic_opmode
== IEEE80211_M_HOSTAP
) && (ni
!= NULL
))
2037 ieee80211_free_node(ni
);
2039 rt2870_do_async(sc
, rt2870_key_set_cb
, &cmd_argv
,
2040 sizeof(struct rt2870_cmd_argv_keyset
));
2048 static int rt2870_key_delete(struct ieee80211com
*ic
,
2049 const struct ieee80211_key
*k
)
2051 struct rt2870_softc
*sc
;
2053 struct ieee80211_node
*ni
;
2054 struct rt2870_cmd_argv_keydelete cmd_argv
;
2060 memcpy(&cmd_argv
.key
, k
, sizeof(struct ieee80211_key
));
2061 cmd_argv
.associd
= (ni
!= NULL
) ? ni
->ni_associd
: 0;
2063 rt2870_do_async(sc
, rt2870_key_delete_cb
, &cmd_argv
,
2064 sizeof(struct rt2870_cmd_argv_keydelete
));
2072 static int rt2870_raw_xmit(struct ieee80211_node
*ni
, struct mbuf
*m
,
2073 const struct ieee80211_bpf_params
*params
)
2075 struct ieee80211com
*ic
;
2077 struct rt2870_softc
*sc
;
2087 * rt2870_media_change
2089 static int rt2870_media_change(struct ifnet
*ifp
)
2091 struct rt2870_softc
*sc
;
2096 error
= ieee80211_media_change(ifp
);
2097 if (error
!= ENETRESET
)
2100 if ((ifp
->if_flags
& IFF_UP
) && (ifp
->if_drv_flags
& IFF_DRV_RUNNING
))
2110 * rt2870_recv_action
2112 static void rt2870_recv_action(struct ieee80211_node
*ni
,
2113 const uint8_t *frm
, const uint8_t *efrm
)
2115 struct rt2870_softc
*sc
;
2116 struct ieee80211com
*ic
;
2118 const struct ieee80211_action
*ia
;
2119 uint16_t associd
, baparamset
;
2128 ia
= (const struct ieee80211_action
*) frm
;
2130 sc
->recv_action(ni
, frm
, efrm
);
2132 if (ia
->ia_category
!= IEEE80211_ACTION_CAT_BA
)
2135 associd
= (ni
!= NULL
) ? ni
->ni_associd
: 0;
2136 wcid
= RT2870_AID2WCID(associd
);
2138 switch (ia
->ia_action
)
2140 /* IEEE80211_ACTION_BA_ADDBA_REQUEST */
2141 case IEEE80211_ACTION_BA_ADDBA_REQUEST
:
2142 baparamset
= LE_READ_2(frm
+ 3);
2143 tid
= RT2870_MS(baparamset
, IEEE80211_BAPS_TID
);
2144 bufsize
= RT2870_MS(baparamset
, IEEE80211_BAPS_BUFSIZ
);
2146 RT2870_DPRINTF(sc
, RT2870_DEBUG_BA
,
2147 "%s: received ADDBA: associd=0x%04x, tid=%d, bufsize=%d\n",
2148 device_get_nameunit(sc
->dev
), associd
, tid
, bufsize
);
2150 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_WCID(wcid
) + 4);
2152 tmp
|= (0x10000 << tid
);
2154 rt2870_io_mac_write(sc
, RT2870_REG_WCID(wcid
) + 4, tmp
);
2157 /* IEEE80211_ACTION_BA_DELBA */
2158 case IEEE80211_ACTION_BA_DELBA
:
2159 baparamset
= LE_READ_2(frm
+ 2);
2160 tid
= RT2870_MS(baparamset
, IEEE80211_BAPS_TID
);
2162 RT2870_DPRINTF(sc
, RT2870_DEBUG_BA
,
2163 "%s: received DELBA: associd=0x%04x, tid=%d\n",
2164 device_get_nameunit(sc
->dev
), associd
, tid
);
2166 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_WCID(wcid
) + 4);
2168 tmp
&= ~(0x10000 << tid
);
2170 rt2870_io_mac_write(sc
, RT2870_REG_WCID(wcid
) + 4, tmp
);
2176 * rt2870_send_action
2178 static int rt2870_send_action(struct ieee80211_node
*ni
,
2179 int category
, int action
, uint16_t args
[4])
2181 struct rt2870_softc
*sc
;
2182 struct ieee80211com
*ic
;
2184 uint16_t associd
, baparamset
;
2193 ret
= sc
->send_action(ni
, category
, action
, args
);
2195 if (category
!= IEEE80211_ACTION_CAT_BA
)
2198 associd
= (ni
!= NULL
) ? ni
->ni_associd
: 0;
2199 wcid
= RT2870_AID2WCID(associd
);
2203 /* IEEE80211_ACTION_BA_DELBA */
2204 case IEEE80211_ACTION_BA_DELBA
:
2205 baparamset
= RT2870_SM(args
[0], IEEE80211_DELBAPS_TID
) |
2206 RT2870_SM(args
[1], IEEE80211_DELBAPS_INIT
);
2208 if (RT2870_MS(baparamset
, IEEE80211_DELBAPS_INIT
) == IEEE80211_DELBAPS_INIT
)
2211 tid
= RT2870_MS(baparamset
, IEEE80211_DELBAPS_TID
);
2213 RT2870_DPRINTF(sc
, RT2870_DEBUG_BA
,
2214 "%s: sending DELBA: associd=0x%04x, tid=%d\n",
2215 device_get_nameunit(sc
->dev
), associd
, tid
);
2217 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_WCID(wcid
) + 4);
2219 tmp
&= ~(0x10000 << tid
);
2221 rt2870_io_mac_write(sc
, RT2870_REG_WCID(wcid
) + 4, tmp
);
2229 * rt2870_amrr_update_iter_func
2231 static void rt2870_amrr_update_iter_func(void *arg
, struct ieee80211_node
*ni
)
2233 struct rt2870_softc
*sc
;
2234 struct ieee80211com
*ic
;
2240 /* only associated stations */
2242 if (ni
->ni_associd
!= 0)
2244 wcid
= RT2870_AID2WCID(ni
->ni_associd
);
2246 rt2870_amrr_choose(ni
, &sc
->amrr_node
[wcid
]);
2248 RT2870_DPRINTF(sc
, RT2870_DEBUG_RATE
,
2249 "%s:%s node Tx rate: associd=0x%04x, rate=0x%02x, max rate=0x%02x\n",
2250 device_get_nameunit(sc
->dev
),
2251 (ni
->ni_flags
& IEEE80211_NODE_HT
) ? " HT" : "",
2253 (ni
->ni_flags
& IEEE80211_NODE_HT
) ?
2254 (ni
->ni_htrates
.rs_rates
[ni
->ni_txrate
] | IEEE80211_RATE_MCS
) :
2255 (ni
->ni_rates
.rs_rates
[ni
->ni_txrate
] & IEEE80211_RATE_VAL
),
2256 (ni
->ni_flags
& IEEE80211_NODE_HT
) ?
2257 (ni
->ni_htrates
.rs_rates
[ni
->ni_htrates
.rs_nrates
- 1] | IEEE80211_RATE_MCS
) :
2258 (ni
->ni_rates
.rs_rates
[ni
->ni_rates
.rs_nrates
- 1] & IEEE80211_RATE_VAL
));
2265 static void rt2870_periodic(void *arg
)
2267 struct rt2870_softc
*sc
;
2273 RT2870_DPRINTF(sc
, RT2870_DEBUG_PERIODIC
,
2275 device_get_nameunit(sc
->dev
));
2277 if (!(ifp
->if_drv_flags
& IFF_DRV_RUNNING
))
2280 taskqueue_enqueue(sc
->taskqueue
, &sc
->periodic_task
);
2284 * rt2870_tx_watchdog
2286 static void rt2870_tx_watchdog(void *arg
)
2288 struct rt2870_softc
*sc
;
2294 if (sc
->tx_timer
== 0)
2297 if (--sc
->tx_timer
== 0)
2299 printf("%s: Tx watchdog timeout: resetting\n",
2300 device_get_nameunit(sc
->dev
));
2302 rt2870_stop_locked(sc
);
2303 rt2870_init_locked(sc
);
2307 sc
->tx_watchdog_timeouts
++;
2310 callout_reset(&sc
->tx_watchdog_ch
, hz
, rt2870_tx_watchdog
, sc
);
2316 static int rt2870_do_async(struct rt2870_softc
*sc
,
2317 void (*cb
)(struct rt2870_softc
*sc
, void *arg
),
2321 struct rt2870_softc_cmd_ring
*ring
;
2322 struct rt2870_softc_cmd
*cmd
;
2326 ring
= &sc
->cmd_ring
;
2328 RT2870_SOFTC_LOCK(sc
);
2330 if (!(ifp
->if_drv_flags
& IFF_DRV_RUNNING
))
2332 RT2870_SOFTC_UNLOCK(sc
);
2336 if (ring
->queued
>= RT2870_SOFTC_CMD_RING_CMD_COUNT
)
2338 RT2870_SOFTC_UNLOCK(sc
);
2342 cmd
= STAILQ_FIRST(&ring
->inactive
);
2343 STAILQ_REMOVE_HEAD(&ring
->inactive
, next
);
2348 memcpy(cmd
->data
, arg
, len
);
2350 STAILQ_INSERT_TAIL(&ring
->active
, cmd
, next
);
2353 run_cmd_task
= (ring
->queued
== 1);
2355 RT2870_SOFTC_UNLOCK(sc
);
2358 taskqueue_enqueue(sc
->taskqueue
, &sc
->cmd_task
);
2364 * rt2870_newstate_cb
2366 static void rt2870_newstate_cb(struct rt2870_softc
*sc
, void *arg
)
2368 struct ieee80211com
*ic
;
2369 enum ieee80211_state nstate
;
2370 struct ieee80211_node
*ni
;
2371 struct rt2870_cmd_argv_newstate
*cmd_argv
;
2376 nstate
= cmd_argv
->nstate
;
2378 RT2870_DPRINTF(sc
, RT2870_DEBUG_STATE
,
2379 "%s: newstate: %s -> %s\n",
2380 device_get_nameunit(sc
->dev
),
2381 ieee80211_state_name
[ic
->ic_state
], ieee80211_state_name
[nstate
]);
2383 error
= sc
->newstate(ic
, nstate
, cmd_argv
->arg
);
2387 /* turn link LED off */
2389 if (nstate
!= IEEE80211_S_RUN
)
2390 rt2870_led_cmd(sc
, RT2870_LED_CMD_RADIO_OFF
);
2394 case IEEE80211_S_INIT
:
2395 rt2870_asic_disable_tsf_sync(sc
);
2398 case IEEE80211_S_RUN
:
2401 rt2870_rf_set_chan(sc
, ni
->ni_chan
);
2403 if (ic
->ic_opmode
!= IEEE80211_M_MONITOR
)
2405 rt2870_asic_enable_mrr(sc
);
2406 rt2870_asic_set_txpreamble(sc
);
2407 rt2870_asic_set_basicrates(sc
);
2408 rt2870_asic_set_bssid(sc
, ni
->ni_bssid
);
2411 if (ic
->ic_opmode
== IEEE80211_M_STA
)
2412 rt2870_newassoc(ni
, 1);
2414 if (ic
->ic_opmode
== IEEE80211_M_HOSTAP
||
2415 ic
->ic_opmode
== IEEE80211_M_IBSS
)
2417 error
= rt2870_asic_update_beacon(sc
);
2422 if (ic
->ic_opmode
!= IEEE80211_M_MONITOR
)
2423 rt2870_asic_enable_tsf_sync(sc
);
2425 /* turn link LED on */
2427 if (ic
->ic_opmode
!= IEEE80211_M_MONITOR
)
2429 rt2870_led_cmd(sc
, RT2870_LED_CMD_RADIO_ON
|
2430 (IEEE80211_IS_CHAN_2GHZ(ni
->ni_chan
) ?
2431 RT2870_LED_CMD_LINK_2GHZ
: RT2870_LED_CMD_LINK_5GHZ
));
2441 * rt2870_scan_start_cb
2443 static void rt2870_scan_start_cb(struct rt2870_softc
*sc
, void *arg
)
2445 struct ieee80211com
*ic
;
2451 rt2870_asic_disable_tsf_sync(sc
);
2455 * rt2870_scan_end_cb
2457 static void rt2870_scan_end_cb(struct rt2870_softc
*sc
, void *arg
)
2459 struct ieee80211com
*ic
;
2460 struct ieee80211_node
*ni
;
2465 rt2870_asic_enable_tsf_sync(sc
);
2469 * rt2870_set_channel_cb
2471 static void rt2870_set_channel_cb(struct rt2870_softc
*sc
, void *arg
)
2473 struct ieee80211com
*ic
;
2477 RT2870_DPRINTF(sc
, RT2870_DEBUG_CHAN
,
2478 "%s: set channel: channel=%u, HT%s%s\n",
2479 device_get_nameunit(sc
->dev
),
2480 ieee80211_chan2ieee(ic
, ic
->ic_curchan
),
2481 !IEEE80211_IS_CHAN_HT(ic
->ic_curchan
) ? " disabled" :
2482 IEEE80211_IS_CHAN_HT20(ic
->ic_curchan
) ? "20":
2483 IEEE80211_IS_CHAN_HT40U(ic
->ic_curchan
) ? "40U" : "40D",
2484 (ic
->ic_flags
& IEEE80211_F_SCAN
) ? ", scanning" : "");
2486 rt2870_rf_set_chan(sc
, ic
->ic_curchan
);
2490 * rt2870_newassoc_cb
2492 static void rt2870_newassoc_cb(struct rt2870_softc
*sc
, void *arg
)
2494 struct ieee80211com
*ic
;
2495 struct ieee80211_node
*ni
;
2496 struct rt2870_cmd_argv_newassoc
*cmd_argv
;
2499 uint8_t *macaddr
, wcid
;
2504 associd
= cmd_argv
->associd
;
2505 isnew
= cmd_argv
->isnew
;
2506 macaddr
= cmd_argv
->macaddr
;
2508 wcid
= RT2870_AID2WCID(associd
);
2510 RT2870_DPRINTF(sc
, RT2870_DEBUG_NODE
,
2511 "%s: new association: wcid=0x%02x, "
2512 "mac addr=%s, QoS %s, ERP %s, HT %s\n",
2513 device_get_nameunit(sc
->dev
), wcid
,
2514 ether_sprintf(macaddr
),
2515 (ni
->ni_flags
& IEEE80211_NODE_QOS
) ? "enabled" : "disabled",
2516 (ni
->ni_flags
& IEEE80211_NODE_ERP
) ? "enabled" : "disabled",
2517 (ni
->ni_flags
& IEEE80211_NODE_HT
) ? "enabled" : "disabled");
2519 rt2870_io_mac_write_multi(sc
, RT2870_REG_WCID(wcid
),
2520 macaddr
, IEEE80211_ADDR_LEN
);
2522 rt2870_amrr_node_init(&sc
->amrr
, &sc
->amrr_node
[wcid
], ni
);
2524 RT2870_DPRINTF(sc
, RT2870_DEBUG_RATE
,
2525 "%s: initial%s node Tx rate: associd=0x%04x, rate=0x%02x, max rate=0x%02x\n",
2526 device_get_nameunit(sc
->dev
),
2527 (ni
->ni_flags
& IEEE80211_NODE_HT
) ? " HT" : "",
2529 (ni
->ni_flags
& IEEE80211_NODE_HT
) ?
2530 (ni
->ni_htrates
.rs_rates
[ni
->ni_txrate
] | IEEE80211_RATE_MCS
) :
2531 (ni
->ni_rates
.rs_rates
[ni
->ni_txrate
] & IEEE80211_RATE_VAL
),
2532 (ni
->ni_flags
& IEEE80211_NODE_HT
) ?
2533 (ni
->ni_htrates
.rs_rates
[ni
->ni_htrates
.rs_nrates
- 1] | IEEE80211_RATE_MCS
) :
2534 (ni
->ni_rates
.rs_rates
[ni
->ni_rates
.rs_nrates
- 1] & IEEE80211_RATE_VAL
));
2536 rt2870_asic_updateprot(sc
);
2537 rt2870_asic_updateslot(sc
);
2538 rt2870_asic_set_txpreamble(sc
);
2542 * rt2870_updateslot_cb
2544 static void rt2870_updateslot_cb(struct rt2870_softc
*sc
, void *arg
)
2546 rt2870_asic_updateslot(sc
);
2550 * rt2870_wme_update_cb
2552 static void rt2870_wme_update_cb(struct rt2870_softc
*sc
, void *arg
)
2554 rt2870_asic_wme_update(sc
);
2558 * rt2870_update_beacon_cb
2560 static void rt2870_update_beacon_cb(struct rt2870_softc
*sc
, void *arg
)
2562 struct rt2870_cmd_argv_updatebeacon
*cmd_argv
;
2566 RT2870_DPRINTF(sc
, RT2870_DEBUG_BEACON
,
2567 "%s: update beacon\n",
2568 device_get_nameunit(sc
->dev
));
2570 rt2870_asic_update_beacon(sc
);
2574 * rt2870_key_update_begin_cb
2576 static void rt2870_key_update_begin_cb(struct rt2870_softc
*sc
, void *arg
)
2582 RT2870_DPRINTF(sc
, RT2870_DEBUG_KEY
,
2583 "%s: key update begin\n",
2584 device_get_nameunit(sc
->dev
));
2586 taskqueue_block(sc
->taskqueue
);
2588 IF_LOCK(&ifp
->if_snd
);
2592 * rt2870_key_update_end_cb
2594 static void rt2870_key_update_end_cb(struct rt2870_softc
*sc
, void *arg
)
2600 RT2870_DPRINTF(sc
, RT2870_DEBUG_KEY
,
2601 "%s: key update end\n",
2602 device_get_nameunit(sc
->dev
));
2604 IF_UNLOCK(&ifp
->if_snd
);
2606 taskqueue_unblock(sc
->taskqueue
);
2612 static void rt2870_key_set_cb(struct rt2870_softc
*sc
, void *arg
)
2614 struct ieee80211com
*ic
;
2615 struct ieee80211_key
*k
;
2616 struct rt2870_cmd_argv_keyset
*cmd_argv
;
2617 uint16_t associd
, key_base
, keymode_base
;
2618 uint8_t mode
, vapid
, wcid
, iv
[8];
2624 associd
= cmd_argv
->associd
;
2626 switch (k
->wk_cipher
->ic_cipher
)
2628 case IEEE80211_CIPHER_WEP
:
2629 if(k
->wk_keylen
< 8)
2630 mode
= RT2870_REG_CIPHER_MODE_WEP40
;
2632 mode
= RT2870_REG_CIPHER_MODE_WEP104
;
2635 case IEEE80211_CIPHER_TKIP
:
2636 mode
= RT2870_REG_CIPHER_MODE_TKIP
;
2639 case IEEE80211_CIPHER_AES_CCM
:
2640 mode
= RT2870_REG_CIPHER_MODE_AES_CCMP
;
2647 RT2870_DPRINTF(sc
, RT2870_DEBUG_KEY
,
2648 "%s: set key: keyix=%d, keylen=%d, associd=0x%04x, mode=%d, group=%d\n",
2649 device_get_nameunit(sc
->dev
), k
->wk_keyix
, k
->wk_keylen
, associd
, mode
,
2650 (k
->wk_flags
& IEEE80211_KEY_GROUP
) ? 1 : 0);
2652 if (!(k
->wk_flags
& IEEE80211_KEY_GROUP
))
2654 /* install pairwise key */
2657 wcid
= RT2870_AID2WCID(associd
);
2658 key_base
= RT2870_REG_PKEY(wcid
);
2660 if (k
->wk_cipher
->ic_cipher
== IEEE80211_CIPHER_WEP
)
2664 iv
[3] = (k
->wk_keyix
<< 6);
2668 if (k
->wk_cipher
->ic_cipher
== IEEE80211_CIPHER_TKIP
)
2670 iv
[0] = (k
->wk_keytsc
>> 8);
2671 iv
[1] = ((iv
[0] | 0x20) & 0x7f);
2672 iv
[2] = k
->wk_keytsc
;
2678 iv
[0] = k
->wk_keytsc
;
2679 iv
[1] = k
->wk_keytsc
>> 8;
2683 iv
[3] = ((k
->wk_keyix
<< 6) | IEEE80211_WEP_EXTIV
);
2684 iv
[4] = (k
->wk_keytsc
>> 16);
2685 iv
[5] = (k
->wk_keytsc
>> 24);
2686 iv
[6] = (k
->wk_keytsc
>> 32);
2687 iv
[7] = (k
->wk_keytsc
>> 40);
2689 RT2870_DPRINTF(sc
, RT2870_DEBUG_KEY
,
2690 "%s: set key: iv=%02x %02x %02x %02x %02x %02x %02x %02x\n",
2691 device_get_nameunit(sc
->dev
),
2692 iv
[0], iv
[1], iv
[2], iv
[3], iv
[4], iv
[5], iv
[6], iv
[7]);
2695 rt2870_io_mac_write_multi(sc
, RT2870_REG_IVEIV(wcid
), iv
, 8);
2697 if (k
->wk_cipher
->ic_cipher
== IEEE80211_CIPHER_TKIP
)
2699 rt2870_io_mac_write_multi(sc
, key_base
, k
->wk_key
, 16);
2701 if (ic
->ic_opmode
!= IEEE80211_M_HOSTAP
)
2703 rt2870_io_mac_write_multi(sc
, key_base
+ 16, &k
->wk_key
[16], 8);
2704 rt2870_io_mac_write_multi(sc
, key_base
+ 24, &k
->wk_key
[24], 8);
2708 rt2870_io_mac_write_multi(sc
, key_base
+ 16, &k
->wk_key
[24], 8);
2709 rt2870_io_mac_write_multi(sc
, key_base
+ 24, &k
->wk_key
[16], 8);
2714 rt2870_io_mac_write_multi(sc
, key_base
, k
->wk_key
, k
->wk_keylen
);
2717 tmp
= ((vapid
& RT2870_REG_VAP_MASK
) << RT2870_REG_VAP_SHIFT
) |
2718 (mode
<< RT2870_REG_CIPHER_MODE_SHIFT
) | RT2870_REG_PKEY_ENABLE
;
2720 rt2870_io_mac_write(sc
, RT2870_REG_WCID_ATTR(wcid
), tmp
);
2723 if ((k
->wk_flags
& IEEE80211_KEY_GROUP
) ||
2724 (k
->wk_cipher
->ic_cipher
== IEEE80211_CIPHER_WEP
))
2726 /* install group key */
2729 key_base
= RT2870_REG_SKEY(vapid
, k
->wk_keyix
);
2730 keymode_base
= RT2870_REG_SKEY_MODE(vapid
);
2732 if (k
->wk_cipher
->ic_cipher
== IEEE80211_CIPHER_TKIP
)
2734 rt2870_io_mac_write_multi(sc
, key_base
, k
->wk_key
, 16);
2736 if (ic
->ic_opmode
!= IEEE80211_M_HOSTAP
)
2738 rt2870_io_mac_write_multi(sc
, key_base
+ 16, &k
->wk_key
[16], 8);
2739 rt2870_io_mac_write_multi(sc
, key_base
+ 24, &k
->wk_key
[24], 8);
2743 rt2870_io_mac_write_multi(sc
, key_base
+ 16, &k
->wk_key
[24], 8);
2744 rt2870_io_mac_write_multi(sc
, key_base
+ 24, &k
->wk_key
[16], 8);
2749 rt2870_io_mac_write_multi(sc
, key_base
, k
->wk_key
, k
->wk_keylen
);
2752 tmp
= rt2870_io_mac_read(sc
, keymode_base
);
2754 tmp
&= ~(0xf << (k
->wk_keyix
* 4 + 16 * (vapid
% 2)));
2755 tmp
|= (mode
<< (k
->wk_keyix
* 4 + 16 * (vapid
% 2)));
2757 rt2870_io_mac_write(sc
, keymode_base
, tmp
);
2762 * rt2870_key_delete_cb
2764 static void rt2870_key_delete_cb(struct rt2870_softc
*sc
, void *arg
)
2766 struct ieee80211com
*ic
;
2767 struct ieee80211_key
*k
;
2768 struct rt2870_cmd_argv_keydelete
*cmd_argv
;
2770 uint8_t vapid
, wcid
;
2776 associd
= cmd_argv
->associd
;
2778 RT2870_DPRINTF(sc
, RT2870_DEBUG_KEY
,
2779 "%s: delete key: keyix=%d, keylen=%d, associd=0x%04x, group=%d\n",
2780 device_get_nameunit(sc
->dev
), k
->wk_keyix
, k
->wk_keylen
, associd
,
2781 (k
->wk_flags
& IEEE80211_KEY_GROUP
) ? 1 : 0);
2783 if (!(k
->wk_flags
& IEEE80211_KEY_GROUP
))
2785 /* remove pairwise key */
2788 wcid
= RT2870_AID2WCID(associd
);
2790 tmp
= ((vapid
& RT2870_REG_VAP_MASK
) << RT2870_REG_VAP_SHIFT
) |
2791 (RT2870_REG_CIPHER_MODE_NONE
<< RT2870_REG_CIPHER_MODE_SHIFT
) | RT2870_REG_PKEY_ENABLE
;
2793 rt2870_io_mac_write(sc
, RT2870_REG_WCID_ATTR(wcid
), tmp
);
2797 /* remove group key */
2801 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_SKEY_MODE(vapid
));
2803 tmp
&= ~(0xf << (k
->wk_keyix
* 4 + 16 * (vapid
% 2)));
2804 tmp
|= (RT2870_REG_CIPHER_MODE_NONE
<< (k
->wk_keyix
* 4 + 16 * (vapid
% 2)));
2806 rt2870_io_mac_write(sc
, RT2870_REG_SKEY_MODE(vapid
), tmp
);
2811 * rt2870_asic_set_bssid
2813 static void rt2870_asic_set_bssid(struct rt2870_softc
*sc
,
2814 const uint8_t *bssid
)
2818 RT2870_DPRINTF(sc
, RT2870_DEBUG_STATE
,
2819 "%s: set bssid: bssid=%s\n",
2820 device_get_nameunit(sc
->dev
), ether_sprintf(bssid
));
2822 tmp
= bssid
[0] | (bssid
[1]) << 8 | (bssid
[2] << 16) | (bssid
[3] << 24);
2824 rt2870_io_mac_write(sc
, RT2870_REG_BSSID_DW0
, tmp
);
2826 tmp
= bssid
[4] | (bssid
[5] << 8);
2828 rt2870_io_mac_write(sc
, RT2870_REG_BSSID_DW1
, tmp
);
2832 * rt2870_asic_set_macaddr
2834 static void rt2870_asic_set_macaddr(struct rt2870_softc
*sc
,
2835 const uint8_t *addr
)
2839 tmp
= addr
[0] | (addr
[1] << 8) | (addr
[2] << 16) | (addr
[3] << 24);
2841 rt2870_io_mac_write(sc
, RT2870_REG_ADDR_DW0
, tmp
);
2843 tmp
= addr
[4] | (addr
[5] << 8);
2845 rt2870_io_mac_write(sc
, RT2870_REG_ADDR_DW1
, tmp
);
2849 * rt2870_asic_enable_tsf_sync
2851 static void rt2870_asic_enable_tsf_sync(struct rt2870_softc
*sc
)
2853 struct ieee80211com
*ic
;
2858 RT2870_DPRINTF(sc
, RT2870_DEBUG_BEACON
,
2859 "%s: enabling TSF\n",
2860 device_get_nameunit(sc
->dev
));
2862 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_BCN_TIME_CFG
);
2865 tmp
|= ic
->ic_bss
->ni_intval
* 16;
2866 tmp
|= (RT2870_REG_TSF_TIMER_ENABLE
| RT2870_REG_TBTT_TIMER_ENABLE
);
2868 if (ic
->ic_opmode
== IEEE80211_M_STA
)
2870 tmp
|= (RT2870_REG_TSF_SYNC_MODE_STA
<< RT2870_REG_TSF_SYNC_MODE_SHIFT
);
2872 else if (ic
->ic_opmode
== IEEE80211_M_IBSS
)
2874 tmp
|= RT2870_REG_BCN_TX_ENABLE
;
2875 tmp
|= (RT2870_REG_TSF_SYNC_MODE_IBSS
<< RT2870_REG_TSF_SYNC_MODE_SHIFT
);
2877 else if (ic
->ic_opmode
== IEEE80211_M_HOSTAP
)
2879 tmp
|= RT2870_REG_BCN_TX_ENABLE
;
2880 tmp
|= (RT2870_REG_TSF_SYNC_MODE_HOSTAP
<< RT2870_REG_TSF_SYNC_MODE_SHIFT
);
2883 rt2870_io_mac_write(sc
, RT2870_REG_BCN_TIME_CFG
, tmp
);
2887 * rt2870_asic_disable_tsf_sync
2889 static void rt2870_asic_disable_tsf_sync(struct rt2870_softc
*sc
)
2893 RT2870_DPRINTF(sc
, RT2870_DEBUG_BEACON
,
2894 "%s: disabling TSF\n",
2895 device_get_nameunit(sc
->dev
));
2897 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_BCN_TIME_CFG
);
2899 tmp
&= ~(RT2870_REG_BCN_TX_ENABLE
|
2900 RT2870_REG_TSF_TIMER_ENABLE
|
2901 RT2870_REG_TBTT_TIMER_ENABLE
);
2903 tmp
&= ~(RT2870_REG_TSF_SYNC_MODE_MASK
<< RT2870_REG_TSF_SYNC_MODE_SHIFT
);
2904 tmp
|= (RT2870_REG_TSF_SYNC_MODE_DISABLE
<< RT2870_REG_TSF_SYNC_MODE_SHIFT
);
2906 rt2870_io_mac_write(sc
, RT2870_REG_BCN_TIME_CFG
, tmp
);
2910 * rt2870_asic_enable_mrr
2912 static void rt2870_asic_enable_mrr(struct rt2870_softc
*sc
)
2914 #define CCK(mcs) (mcs)
2915 #define OFDM(mcs) ((1 << 3) | (mcs))
2916 #define HT(mcs) (mcs)
2918 rt2870_io_mac_write(sc
, RT2870_REG_TX_LG_FBK_CFG0
,
2919 (OFDM(6) << 28) | /* 54 -> 48 */
2920 (OFDM(5) << 24) | /* 48 -> 36 */
2921 (OFDM(4) << 20) | /* 36 -> 24 */
2922 (OFDM(3) << 16) | /* 24 -> 18 */
2923 (OFDM(2) << 12) | /* 18 -> 12 */
2924 (OFDM(1) << 8) | /* 12 -> 9 */
2925 (OFDM(0) << 4) | /* 9 -> 6 */
2926 OFDM(0)); /* 6 -> 6 */
2928 rt2870_io_mac_write(sc
, RT2870_REG_TX_LG_FBK_CFG1
,
2929 (CCK(2) << 12) | /* 11 -> 5.5 */
2930 (CCK(1) << 8) | /* 5.5 -> 2 */
2931 (CCK(0) << 4) | /* 2 -> 1 */
2932 CCK(0)); /* 1 -> 1 */
2934 rt2870_io_mac_write(sc
, RT2870_REG_TX_HT_FBK_CFG0
,
2944 rt2870_io_mac_write(sc
, RT2870_REG_TX_HT_FBK_CFG1
,
2960 * rt2870_asic_set_txpreamble
2962 static void rt2870_asic_set_txpreamble(struct rt2870_softc
*sc
)
2964 struct ieee80211com
*ic
;
2969 RT2870_DPRINTF(sc
, RT2870_DEBUG_STATE
,
2970 "%s: %s short Tx preamble\n",
2971 device_get_nameunit(sc
->dev
),
2972 (ic
->ic_flags
& IEEE80211_F_SHPREAMBLE
) ? "enabling" : "disabling");
2974 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_AUTO_RSP_CFG
);
2976 tmp
&= ~RT2870_REG_CCK_SHORT_ENABLE
;
2978 if (ic
->ic_flags
& IEEE80211_F_SHPREAMBLE
)
2979 tmp
|= RT2870_REG_CCK_SHORT_ENABLE
;
2981 rt2870_io_mac_write(sc
, RT2870_REG_AUTO_RSP_CFG
, tmp
);
2985 * rt2870_asic_set_basicrates
2987 static void rt2870_asic_set_basicrates(struct rt2870_softc
*sc
)
2989 struct ieee80211com
*ic
;
2993 if (ic
->ic_curmode
== IEEE80211_MODE_11B
)
2994 rt2870_io_mac_write(sc
, RT2870_REG_LEGACY_BASIC_RATE
, 0x3);
2995 else if (ic
->ic_curmode
== IEEE80211_MODE_11A
)
2996 rt2870_io_mac_write(sc
, RT2870_REG_LEGACY_BASIC_RATE
, 0x150);
2998 rt2870_io_mac_write(sc
, RT2870_REG_LEGACY_BASIC_RATE
, 0x15f);
3002 * rt2870_asic_update_rtsthreshold
3004 static void rt2870_asic_update_rtsthreshold(struct rt2870_softc
*sc
)
3006 struct ieee80211com
*ic
;
3012 RT2870_DPRINTF(sc
, RT2870_DEBUG_PROT
,
3013 "%s: updating RTS threshold: %d\n",
3014 device_get_nameunit(sc
->dev
), ic
->ic_rtsthreshold
);
3016 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_RTS_CFG
);
3018 tmp
&= ~(RT2870_REG_TX_RTS_THRESHOLD_MASK
<< RT2870_REG_TX_RTS_THRESHOLD_SHIFT
);
3020 threshold
= (ic
->ic_rtsthreshold
< IEEE80211_RTS_MAX
) ?
3021 ic
->ic_rtsthreshold
: 0x1000;
3023 tmp
|= ((threshold
& RT2870_REG_TX_RTS_THRESHOLD_MASK
) <<
3024 RT2870_REG_TX_RTS_THRESHOLD_SHIFT
);
3026 rt2870_io_mac_write(sc
, RT2870_REG_TX_RTS_CFG
, tmp
);
3030 * rt2870_asic_update_txpower
3032 static void rt2870_asic_update_txpower(struct rt2870_softc
*sc
)
3034 struct ieee80211com
*ic
;
3035 uint32_t *txpow_rate
;
3043 RT2870_DPRINTF(sc
, RT2870_DEBUG_STATE
,
3044 "%s: updating Tx power: %d\n",
3045 device_get_nameunit(sc
->dev
), ic
->ic_txpowlimit
);
3047 if (!IEEE80211_IS_CHAN_HT40(ic
->ic_curchan
))
3049 txpow_rate
= sc
->txpow_rate_20mhz
;
3053 if (IEEE80211_IS_CHAN_2GHZ(ic
->ic_curchan
))
3054 txpow_rate
= sc
->txpow_rate_40mhz_2ghz
;
3056 txpow_rate
= sc
->txpow_rate_40mhz_5ghz
;
3061 val
= rt2870_io_bbp_read(sc
, 1);
3064 if (ic
->ic_txpowlimit
> 90)
3068 else if (ic
->ic_txpowlimit
> 60)
3072 else if (ic
->ic_txpowlimit
> 30)
3076 else if (ic
->ic_txpowlimit
> 15)
3080 else if (ic
->ic_txpowlimit
> 9)
3090 rt2870_io_bbp_write(sc
, 1, val
);
3092 for (i
= 0; i
< RT2870_SOFTC_TXPOW_RATE_COUNT
; i
++)
3094 if (txpow_rate
[i
] == 0xffffffff)
3097 tmp
= rt2870_read_eeprom_txpow_rate_add_delta(txpow_rate
[i
], delta
);
3099 rt2870_io_mac_write(sc
, RT2870_REG_TX_PWR_CFG(i
), tmp
);
3104 * rt2870_asic_update_promisc
3106 static void rt2870_asic_update_promisc(struct rt2870_softc
*sc
)
3111 ifp
= sc
->ic
.ic_ifp
;
3113 printf("%s: %s promiscuous mode\n",
3114 device_get_nameunit(sc
->dev
),
3115 (ifp
->if_flags
& IFF_PROMISC
) ? "entering" : "leaving");
3117 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_RX_FILTER_CFG
);
3119 tmp
&= ~RT2870_REG_RX_FILTER_DROP_UC_NOME
;
3121 if (!(ifp
->if_flags
& IFF_PROMISC
))
3122 tmp
|= RT2870_REG_RX_FILTER_DROP_UC_NOME
;
3124 rt2870_io_mac_write(sc
, RT2870_REG_RX_FILTER_CFG
, tmp
);
3128 * rt2870_asic_updateprot
3130 static void rt2870_asic_updateprot(struct rt2870_softc
*sc
)
3132 struct ieee80211com
*ic
;
3133 uint32_t cck_prot
, ofdm_prot
, mm20_prot
, mm40_prot
, gf20_prot
, gf40_prot
;
3138 /* CCK frame protection */
3140 cck_prot
= RT2870_REG_RTSTH_ENABLE
| RT2870_REG_PROT_NAV_SHORT
|
3141 RT2870_REG_TXOP_ALLOW_ALL
| RT2870_REG_PROT_CTRL_NONE
;
3143 /* set up protection frame phy mode and rate (MCS code) */
3145 if (ic
->ic_curmode
== IEEE80211_MODE_11A
)
3146 cck_prot
|= (RT2870_REG_PROT_PHYMODE_OFDM
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3147 (0 << RT2870_REG_PROT_MCS_SHIFT
);
3149 cck_prot
|= ((RT2870_REG_PROT_PHYMODE_CCK
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3150 (3 << RT2870_REG_PROT_MCS_SHIFT
));
3152 rt2870_io_mac_write(sc
, RT2870_REG_TX_CCK_PROT_CFG
, cck_prot
);
3154 /* OFDM frame protection */
3156 ofdm_prot
= RT2870_REG_RTSTH_ENABLE
| RT2870_REG_PROT_NAV_SHORT
|
3157 RT2870_REG_TXOP_ALLOW_ALL
;
3159 if (ic
->ic_flags
& IEEE80211_F_USEPROT
)
3161 RT2870_DPRINTF(sc
, RT2870_DEBUG_PROT
,
3162 "%s: updating protection mode: b/g protection mode=%s\n",
3163 device_get_nameunit(sc
->dev
),
3164 (ic
->ic_protmode
== IEEE80211_PROT_RTSCTS
) ? "RTS/CTS" :
3165 ((ic
->ic_protmode
== IEEE80211_PROT_CTSONLY
) ? "CTS-to-self" : "none"));
3167 if (ic
->ic_protmode
== IEEE80211_PROT_RTSCTS
)
3168 ofdm_prot
|= RT2870_REG_PROT_CTRL_RTS_CTS
;
3169 else if (ic
->ic_protmode
== IEEE80211_PROT_CTSONLY
)
3170 ofdm_prot
|= RT2870_REG_PROT_CTRL_CTS
;
3172 ofdm_prot
|= RT2870_REG_PROT_CTRL_NONE
;
3176 RT2870_DPRINTF(sc
, RT2870_DEBUG_PROT
,
3177 "%s: updating protection mode: b/g protection mode=%s\n",
3178 device_get_nameunit(sc
->dev
), "none");
3180 ofdm_prot
|= RT2870_REG_PROT_CTRL_NONE
;
3183 rt2870_io_mac_write(sc
, RT2870_REG_TX_OFDM_PROT_CFG
, ofdm_prot
);
3185 /* HT frame protection */
3187 if ((ic
->ic_opmode
== IEEE80211_M_STA
) && (ic
->ic_state
== IEEE80211_S_RUN
))
3188 htopmode
= ic
->ic_bss
->ni_htopmode
;
3190 htopmode
= ic
->ic_curhtprotmode
;
3192 RT2870_DPRINTF(sc
, RT2870_DEBUG_PROT
,
3193 "%s: updating protection mode: HT operation mode=0x%02x, protection mode=%s\n",
3194 device_get_nameunit(sc
->dev
),
3195 htopmode
& IEEE80211_HTINFO_OPMODE
,
3196 (ic
->ic_htprotmode
== IEEE80211_PROT_RTSCTS
) ? "RTS/CTS" :
3197 ((ic
->ic_htprotmode
== IEEE80211_PROT_CTSONLY
) ? "CTS-to-self" : "none"));
3199 switch (htopmode
& IEEE80211_HTINFO_OPMODE
)
3201 /* IEEE80211_HTINFO_OPMODE_HT20PR */
3202 case IEEE80211_HTINFO_OPMODE_HT20PR
:
3203 mm20_prot
= RT2870_REG_PROT_NAV_SHORT
| RT2870_REG_PROT_CTRL_NONE
|
3204 RT2870_REG_TXOP_ALLOW_CCK
| RT2870_REG_TXOP_ALLOW_OFDM
|
3205 RT2870_REG_TXOP_ALLOW_MM20
| RT2870_REG_TXOP_ALLOW_GF20
|
3206 (RT2870_REG_PROT_PHYMODE_OFDM
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3207 (4 << RT2870_REG_PROT_MCS_SHIFT
);
3209 gf20_prot
= mm20_prot
;
3211 mm40_prot
= RT2870_REG_PROT_NAV_SHORT
| RT2870_REG_PROT_CTRL_NONE
|
3212 RT2870_REG_TXOP_ALLOW_ALL
|
3213 (RT2870_REG_PROT_PHYMODE_OFDM
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3214 (0x84 << RT2870_REG_PROT_MCS_SHIFT
);
3216 if (ic
->ic_htprotmode
== IEEE80211_PROT_RTSCTS
)
3217 mm40_prot
|= RT2870_REG_PROT_CTRL_RTS_CTS
;
3218 else if (ic
->ic_htprotmode
== IEEE80211_PROT_CTSONLY
)
3219 mm40_prot
|= RT2870_REG_PROT_CTRL_CTS
;
3221 mm40_prot
|= RT2870_REG_PROT_CTRL_NONE
;
3223 gf40_prot
= mm40_prot
;
3226 /* IEEE80211_HTINFO_OPMODE_MIXED */
3227 case IEEE80211_HTINFO_OPMODE_MIXED
:
3228 mm20_prot
= RT2870_REG_PROT_NAV_SHORT
|
3229 RT2870_REG_TXOP_ALLOW_CCK
| RT2870_REG_TXOP_ALLOW_OFDM
|
3230 RT2870_REG_TXOP_ALLOW_MM20
| RT2870_REG_TXOP_ALLOW_GF20
;
3232 if (ic
->ic_flags
& IEEE80211_F_USEPROT
)
3233 mm20_prot
|= (RT2870_REG_PROT_PHYMODE_CCK
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3234 (3 << RT2870_REG_PROT_MCS_SHIFT
);
3236 mm20_prot
|= (RT2870_REG_PROT_PHYMODE_OFDM
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3237 (4 << RT2870_REG_PROT_MCS_SHIFT
);
3239 if (ic
->ic_htprotmode
== IEEE80211_PROT_RTSCTS
)
3240 mm20_prot
|= RT2870_REG_PROT_CTRL_RTS_CTS
;
3241 else if (ic
->ic_htprotmode
== IEEE80211_PROT_CTSONLY
)
3242 mm20_prot
|= RT2870_REG_PROT_CTRL_CTS
;
3244 mm20_prot
|= RT2870_REG_PROT_CTRL_NONE
;
3246 gf20_prot
= mm20_prot
;
3248 mm40_prot
= RT2870_REG_PROT_NAV_SHORT
| RT2870_REG_TXOP_ALLOW_ALL
;
3250 if (ic
->ic_flags
& IEEE80211_F_USEPROT
)
3251 mm40_prot
|= (RT2870_REG_PROT_PHYMODE_CCK
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3252 (3 << RT2870_REG_PROT_MCS_SHIFT
);
3254 mm40_prot
|= (RT2870_REG_PROT_PHYMODE_OFDM
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3255 (0x84 << RT2870_REG_PROT_MCS_SHIFT
);
3257 if (ic
->ic_htprotmode
== IEEE80211_PROT_RTSCTS
)
3258 mm40_prot
|= RT2870_REG_PROT_CTRL_RTS_CTS
;
3259 else if (ic
->ic_htprotmode
== IEEE80211_PROT_CTSONLY
)
3260 mm40_prot
|= RT2870_REG_PROT_CTRL_CTS
;
3262 mm40_prot
|= RT2870_REG_PROT_CTRL_NONE
;
3264 gf40_prot
= mm40_prot
;
3268 * IEEE80211_HTINFO_OPMODE_PURE
3269 * IEEE80211_HTINFO_OPMODE_PROTOPT
3271 case IEEE80211_HTINFO_OPMODE_PURE
:
3272 case IEEE80211_HTINFO_OPMODE_PROTOPT
:
3274 mm20_prot
= RT2870_REG_PROT_NAV_SHORT
| RT2870_REG_PROT_CTRL_NONE
|
3275 RT2870_REG_TXOP_ALLOW_CCK
| RT2870_REG_TXOP_ALLOW_OFDM
|
3276 RT2870_REG_TXOP_ALLOW_MM20
| RT2870_REG_TXOP_ALLOW_GF20
|
3277 (RT2870_REG_PROT_PHYMODE_OFDM
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3278 (4 << RT2870_REG_PROT_MCS_SHIFT
);
3280 gf20_prot
= mm20_prot
;
3282 mm40_prot
= RT2870_REG_PROT_NAV_SHORT
| RT2870_REG_PROT_CTRL_NONE
|
3283 RT2870_REG_TXOP_ALLOW_ALL
|
3284 (RT2870_REG_PROT_PHYMODE_OFDM
<< RT2870_REG_PROT_PHYMODE_SHIFT
) |
3285 (0x84 << RT2870_REG_PROT_MCS_SHIFT
);
3287 gf40_prot
= mm40_prot
;
3291 rt2870_io_mac_write(sc
, RT2870_REG_TX_MM20_PROT_CFG
, mm20_prot
);
3292 rt2870_io_mac_write(sc
, RT2870_REG_TX_MM40_PROT_CFG
, mm40_prot
);
3293 rt2870_io_mac_write(sc
, RT2870_REG_TX_GF20_PROT_CFG
, gf20_prot
);
3294 rt2870_io_mac_write(sc
, RT2870_REG_TX_GF40_PROT_CFG
, gf40_prot
);
3298 * rt2870_asic_updateslot
3300 static void rt2870_asic_updateslot(struct rt2870_softc
*sc
)
3302 struct ieee80211com
*ic
;
3307 RT2870_DPRINTF(sc
, RT2870_DEBUG_STATE
,
3308 "%s: %s short slot time\n",
3309 device_get_nameunit(sc
->dev
),
3310 (ic
->ic_flags
& IEEE80211_F_SHSLOT
) ? "enabling" : "disabling");
3312 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_BKOFF_SLOT_CFG
);
3315 tmp
|= (ic
->ic_flags
& IEEE80211_F_SHSLOT
) ? 9 : 20;
3317 rt2870_io_mac_write(sc
, RT2870_REG_BKOFF_SLOT_CFG
, tmp
);
3321 * rt2870_asic_wme_update
3323 static void rt2870_asic_wme_update(struct rt2870_softc
*sc
)
3325 struct ieee80211com
*ic
;
3326 struct ieee80211_wme_state
*wme
;
3327 const struct wmeParams
*wmep
;
3332 wmep
= wme
->wme_chanParams
.cap_wmeParams
;
3334 RT2870_DPRINTF(sc
, RT2870_DEBUG_WME
,
3335 "%s: wme update: WME_AC_VO=%d/%d/%d/%d, WME_AC_VI=%d/%d/%d/%d, "
3336 "WME_AC_BK=%d/%d/%d/%d, WME_AC_BE=%d/%d/%d/%d\n",
3337 device_get_nameunit(sc
->dev
),
3338 wmep
[WME_AC_VO
].wmep_aifsn
,
3339 wmep
[WME_AC_VO
].wmep_logcwmin
, wmep
[WME_AC_VO
].wmep_logcwmax
,
3340 wmep
[WME_AC_VO
].wmep_txopLimit
,
3341 wmep
[WME_AC_VI
].wmep_aifsn
,
3342 wmep
[WME_AC_VI
].wmep_logcwmin
, wmep
[WME_AC_VI
].wmep_logcwmax
,
3343 wmep
[WME_AC_VI
].wmep_txopLimit
,
3344 wmep
[WME_AC_BK
].wmep_aifsn
,
3345 wmep
[WME_AC_BK
].wmep_logcwmin
, wmep
[WME_AC_BK
].wmep_logcwmax
,
3346 wmep
[WME_AC_BK
].wmep_txopLimit
,
3347 wmep
[WME_AC_BE
].wmep_aifsn
,
3348 wmep
[WME_AC_BE
].wmep_logcwmin
, wmep
[WME_AC_BE
].wmep_logcwmax
,
3349 wmep
[WME_AC_BE
].wmep_txopLimit
);
3351 for (i
= 0; i
< WME_NUM_AC
; i
++)
3352 rt2870_io_mac_write(sc
, RT2870_REG_TX_EDCA_AC_CFG(i
),
3353 (wmep
[i
].wmep_logcwmax
<< 16) | (wmep
[i
].wmep_logcwmin
<< 12) |
3354 (wmep
[i
].wmep_aifsn
<< 8) | wmep
[i
].wmep_txopLimit
);
3356 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_WMM_AIFSN_CFG
,
3357 (wmep
[WME_AC_VO
].wmep_aifsn
<< 12) | (wmep
[WME_AC_VI
].wmep_aifsn
<< 8) |
3358 (wmep
[WME_AC_BK
].wmep_aifsn
<< 4) | wmep
[WME_AC_BE
].wmep_aifsn
);
3360 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_WMM_CWMIN_CFG
,
3361 (wmep
[WME_AC_VO
].wmep_logcwmin
<< 12) | (wmep
[WME_AC_VI
].wmep_logcwmin
<< 8) |
3362 (wmep
[WME_AC_BK
].wmep_logcwmin
<< 4) | wmep
[WME_AC_BE
].wmep_logcwmin
);
3364 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_WMM_CWMAX_CFG
,
3365 (wmep
[WME_AC_VO
].wmep_logcwmax
<< 12) | (wmep
[WME_AC_VI
].wmep_logcwmax
<< 8) |
3366 (wmep
[WME_AC_BK
].wmep_logcwmax
<< 4) | wmep
[WME_AC_BE
].wmep_logcwmax
);
3368 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_WMM_TXOP0_CFG
,
3369 (wmep
[WME_AC_BK
].wmep_txopLimit
<< 16) | wmep
[WME_AC_BE
].wmep_txopLimit
);
3371 rt2870_io_mac_write(sc
, RT2870_REG_SCHDMA_WMM_TXOP1_CFG
,
3372 (wmep
[WME_AC_VO
].wmep_txopLimit
<< 16) | wmep
[WME_AC_VI
].wmep_txopLimit
);
3376 * rt2870_asic_update_beacon
3378 static int rt2870_asic_update_beacon(struct rt2870_softc
*sc
)
3380 struct ieee80211com
*ic
;
3382 struct rt2870_txwi txwi
;
3388 m
= ieee80211_beacon_alloc(ic
->ic_bss
, &sc
->beacon_offsets
);
3392 rate
= IEEE80211_IS_CHAN_5GHZ(ic
->ic_curchan
) ? 12 : 2;
3393 mcs
= rt2870_rate2mcs(rate
);
3395 memset(&txwi
, 0, sizeof(struct rt2870_txwi
));
3398 txwi
.pid_mpdu_len
= ((htole16(m
->m_pkthdr
.len
) & RT2870_TXWI_MPDU_LEN_MASK
) <<
3399 RT2870_TXWI_MPDU_LEN_SHIFT
);
3400 txwi
.txop
= (RT2870_TXWI_TXOP_HT
<< RT2870_TXWI_TXOP_SHIFT
);
3401 txwi
.mpdu_density_flags
|=
3402 (RT2870_TXWI_FLAGS_TS
<< RT2870_TXWI_FLAGS_SHIFT
);
3403 txwi
.bawin_size_xflags
|=
3404 (RT2870_TXWI_XFLAGS_NSEQ
<< RT2870_TXWI_XFLAGS_SHIFT
);
3408 txwi
.phymode_ifs_stbc_shortgi
=
3409 (RT2870_TXWI_PHYMODE_CCK
<< RT2870_TXWI_PHYMODE_SHIFT
);
3411 if (rate
!= 2 && (ic
->ic_flags
& IEEE80211_F_SHPREAMBLE
))
3412 mcs
|= RT2870_TXWI_MCS_SHOTPRE
;
3416 txwi
.phymode_ifs_stbc_shortgi
=
3417 (RT2870_TXWI_PHYMODE_OFDM
<< RT2870_TXWI_PHYMODE_SHIFT
);
3420 txwi
.bw_mcs
= (RT2870_TXWI_BW_20
<< RT2870_TXWI_BW_SHIFT
) |
3421 ((mcs
& RT2870_TXWI_MCS_MASK
) << RT2870_TXWI_MCS_SHIFT
);
3423 /* disable temporarily TSF sync */
3425 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_BCN_TIME_CFG
);
3427 tmp
&= ~(RT2870_REG_BCN_TX_ENABLE
|
3428 RT2870_REG_TSF_TIMER_ENABLE
|
3429 RT2870_REG_TBTT_TIMER_ENABLE
);
3431 rt2870_io_mac_write(sc
, RT2870_REG_BCN_TIME_CFG
, tmp
);
3433 /* write Tx wireless info and beacon frame to on-chip memory */
3435 rt2870_io_mac_write_multi(sc
, RT2870_REG_BEACON_BASE(0),
3436 &txwi
, sizeof(struct rt2870_txwi
));
3438 rt2870_io_mac_write_multi(sc
, RT2870_REG_BEACON_BASE(0) + sizeof(struct rt2870_txwi
),
3439 mtod(m
, uint8_t *), m
->m_pkthdr
.len
);
3441 /* enable again TSF sync */
3443 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_BCN_TIME_CFG
);
3445 tmp
|= (RT2870_REG_BCN_TX_ENABLE
|
3446 RT2870_REG_TSF_TIMER_ENABLE
|
3447 RT2870_REG_TBTT_TIMER_ENABLE
);
3449 rt2870_io_mac_write(sc
, RT2870_REG_BCN_TIME_CFG
, tmp
);
3457 * rt2870_asic_clear_keytables
3459 static void rt2870_asic_clear_keytables(struct rt2870_softc
*sc
)
3463 /* clear Rx WCID search table (entries = 256, entry size = 8) */
3465 for (i
= 0; i
< 256; i
++)
3467 rt2870_io_mac_write(sc
, RT2870_REG_WCID(i
), 0xffffffff);
3468 rt2870_io_mac_write(sc
, RT2870_REG_WCID(i
) + 4, 0x0000ffff);
3471 /* clear WCID attribute table (entries = 256, entry size = 4) */
3473 rt2870_io_mac_set_region_4(sc
, RT2870_REG_WCID_ATTR(0), 0, 256);
3475 /* clear IV/EIV table (entries = 256, entry size = 8) */
3477 rt2870_io_mac_set_region_4(sc
, RT2870_REG_IVEIV(0), 0, 2 * 256);
3479 /* clear pairwise key table (entries = 256, entry size = 32) */
3481 rt2870_io_mac_set_region_4(sc
, RT2870_REG_PKEY(0), 0, 8 * 256);
3483 /* clear shared key table (entries = 32, entry size = 32) */
3485 rt2870_io_mac_set_region_4(sc
, RT2870_REG_SKEY(0, 0), 0, 8 * 32);
3487 /* clear shared key mode (entries = 32, entry size = 2) */
3489 rt2870_io_mac_set_region_4(sc
, RT2870_REG_SKEY_MODE(0), 0, 16);
3495 static uint8_t rt2870_rxrate(struct rt2870_rxwi
*rxwi
)
3497 uint8_t mcs
, phymode
;
3500 mcs
= (rxwi
->bw_mcs
>> RT2870_RXWI_MCS_SHIFT
) & RT2870_RXWI_MCS_MASK
;
3501 phymode
= (rxwi
->phymode_stbc_shortgi
>> RT2870_RXWI_PHYMODE_SHIFT
) &
3502 RT2870_RXWI_PHYMODE_MASK
;
3508 case RT2870_RXWI_PHYMODE_CCK
:
3509 switch (mcs
& ~RT2870_RXWI_MCS_SHOTPRE
)
3511 case 0: rate
= 2; break; /* 1 Mbps */
3512 case 1: rate
= 4; break; /* 2 MBps */
3513 case 2: rate
= 11; break; /* 5.5 Mbps */
3514 case 3: rate
= 22; break; /* 11 Mbps */
3518 case RT2870_RXWI_PHYMODE_OFDM
:
3521 case 0: rate
= 12; break; /* 6 Mbps */
3522 case 1: rate
= 18; break; /* 9 Mbps */
3523 case 2: rate
= 24; break; /* 12 Mbps */
3524 case 3: rate
= 36; break; /* 18 Mbps */
3525 case 4: rate
= 48; break; /* 24 Mbps */
3526 case 5: rate
= 72; break; /* 36 Mbps */
3527 case 6: rate
= 96; break; /* 48 Mbps */
3528 case 7: rate
= 108; break; /* 54 Mbps */
3532 case RT2870_RXWI_PHYMODE_HT_MIXED
:
3533 case RT2870_RXWI_PHYMODE_HT_GF
:
3541 * rt2870_maxrssi_rxpath
3543 static uint8_t rt2870_maxrssi_rxpath(struct rt2870_softc
*sc
,
3544 const struct rt2870_rxwi
*rxwi
)
3550 if (sc
->nrxpath
> 1)
3551 if (rxwi
->rssi
[1] > rxwi
->rssi
[rxpath
])
3554 if (sc
->nrxpath
> 2)
3555 if (rxwi
->rssi
[2] > rxwi
->rssi
[rxpath
])
3564 static int8_t rt2870_rssi2dbm(struct rt2870_softc
*sc
,
3565 uint8_t rssi
, uint8_t rxpath
)
3567 struct ieee80211com
*ic
;
3568 struct ieee80211_channel
*c
;
3570 int8_t rssi_off
, lna_gain
;
3577 chan
= ieee80211_chan2ieee(ic
, c
);
3579 if (IEEE80211_IS_CHAN_5GHZ(c
))
3581 rssi_off
= sc
->rssi_off_5ghz
[rxpath
];
3584 lna_gain
= sc
->lna_gain
[1];
3585 else if (chan
<= 128)
3586 lna_gain
= sc
->lna_gain
[2];
3588 lna_gain
= sc
->lna_gain
[3];
3592 rssi_off
= sc
->rssi_off_2ghz
[rxpath
];
3593 lna_gain
= sc
->lna_gain
[0];
3596 return (-12 - rssi_off
- lna_gain
- rssi
);
3602 static uint8_t rt2870_rate2mcs(uint8_t rate
)
3629 static int rt2870_ackrate(struct ieee80211com
*ic
, int rate
)
3641 return (ic
->ic_curmode
== IEEE80211_MODE_11B
) ? 4 : rate
;
3660 /* default to 1Mbps */
3667 static uint16_t rt2870_txtime(int len
, int rate
, uint32_t flags
)
3671 if (RT2870_RATE_IS_OFDM(rate
))
3673 txtime
= (8 + 4 * len
+ 3 + rate
- 1) / rate
;
3674 txtime
= 16 + 4 + 4 * txtime
+ 6;
3678 txtime
= (16 * len
+ rate
- 1) / rate
;
3680 if (rate
!= 2 && (flags
& IEEE80211_F_SHPREAMBLE
))
3692 static void rt2870_rx_frame(struct rt2870_softc
*sc
,
3693 uint8_t *buf
, uint32_t dmalen
)
3695 struct ieee80211com
*ic
;
3697 struct ieee80211_frame
*wh
;
3698 struct ieee80211_node
*ni
;
3699 struct rt2870_softc_rx_radiotap_header
*tap
;
3700 struct rt2870_rxwi
*rxwi
;
3701 struct rt2870_rxinfo
*rxinfo
;
3703 uint32_t rxinfo_flags
;
3704 uint8_t cipher_err
, rssi
, ant
, phymode
, bw
, shortgi
, mcs
;
3706 int len
, ampdu
, amsdu
;
3711 /* get Rx wireless info */
3713 rxwi
= (struct rt2870_rxwi
*) buf
;
3714 len
= (le16toh(rxwi
->tid_size
) >> RT2870_RXWI_SIZE_SHIFT
) &
3715 RT2870_RXWI_SIZE_MASK
;
3719 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
3720 "%s: bad rxwi len: DMA len=%d, rxwi len=%d\n",
3721 device_get_nameunit(sc
->dev
), dmalen
, len
);
3727 rxinfo
= (struct rt2870_rxinfo
*) ((caddr_t
) rxwi
+ dmalen
);
3728 rxinfo_flags
= le32toh(rxinfo
->flags
);
3730 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
3731 "%s: Rx frame: DMA len=%d, len=%d, rxinfo flags=0x%08x\n",
3732 device_get_nameunit(sc
->dev
), dmalen
, len
, rxinfo_flags
);
3734 /* check for crc errors */
3736 if (rxinfo_flags
& RT2870_RXINFO_FLAGS_CRC_ERR
)
3738 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
3739 "%s: rxinfo: crc error\n",
3740 device_get_nameunit(sc
->dev
));
3744 if (!(ifp
->if_flags
& IFF_PROMISC
))
3748 wh
= (struct ieee80211_frame
*) (rxwi
+ 1);
3750 /* check for L2 padding between IEEE 802.11 frame header and body */
3752 if (rxinfo_flags
& RT2870_RXINFO_FLAGS_L2PAD
)
3754 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
3755 "%s: L2 padding: DMA len=%d, len=%d\n",
3756 device_get_nameunit(sc
->dev
), dmalen
, len
);
3761 m
= m_getjcl(M_DONTWAIT
, MT_DATA
, M_PKTHDR
, MJUMPAGESIZE
);
3764 sc
->rx_mbuf_alloc_errors
++;
3769 m
->m_pkthdr
.rcvif
= ifp
;
3770 m
->m_pkthdr
.len
= m
->m_len
= len
;
3772 m_copyback(m
, 0, len
, (caddr_t
) wh
);
3774 wh
= mtod(m
, struct ieee80211_frame
*);
3776 /* check for cipher errors */
3778 if (rxinfo_flags
& RT2870_RXINFO_FLAGS_DECRYPTED
)
3780 cipher_err
= ((rxinfo_flags
>> RT2870_RXINFO_FLAGS_CIPHER_ERR_SHIFT
) &
3781 RT2870_RXINFO_FLAGS_CIPHER_ERR_MASK
);
3782 if (cipher_err
== RT2870_RXINFO_FLAGS_CIPHER_ERR_NONE
)
3784 if (wh
->i_fc
[1] & IEEE80211_FC1_WEP
)
3785 wh
->i_fc
[1] &= ~IEEE80211_FC1_WEP
;
3787 m
->m_flags
|= M_WEP
;
3791 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
3792 "%s: rxinfo: cipher error=0x%02x\n",
3793 device_get_nameunit(sc
->dev
), cipher_err
);
3797 if (!(ifp
->if_flags
& IFF_PROMISC
))
3806 if (wh
->i_fc
[1] & IEEE80211_FC1_WEP
)
3810 if (!(ifp
->if_flags
& IFF_PROMISC
))
3818 /* check for A-MPDU */
3820 if (rxinfo_flags
& RT2870_RXINFO_FLAGS_AMPDU
)
3822 m
->m_flags
|= M_AMPDU
;
3833 /* check for A-MSDU */
3835 if (rxinfo_flags
& RT2870_RXINFO_FLAGS_AMSDU
)
3846 ant
= rt2870_maxrssi_rxpath(sc
, rxwi
);
3847 rssi
= rxwi
->rssi
[ant
];
3848 rssi_dbm
= rt2870_rssi2dbm(sc
, rssi
, ant
);
3849 phymode
= ((rxwi
->phymode_stbc_shortgi
>> RT2870_RXWI_PHYMODE_SHIFT
) &
3850 RT2870_RXWI_PHYMODE_MASK
);
3851 bw
= ((rxwi
->bw_mcs
>> RT2870_RXWI_BW_SHIFT
) & RT2870_RXWI_BW_MASK
);
3852 shortgi
= ((rxwi
->phymode_stbc_shortgi
>> RT2870_RXWI_SHORTGI_SHIFT
) &
3853 RT2870_RXWI_SHORTGI_MASK
);
3854 mcs
= ((rxwi
->bw_mcs
>> RT2870_RXWI_MCS_SHIFT
) & RT2870_RXWI_MCS_MASK
);
3856 if (bpf_peers_present(sc
->drvbpf
))
3860 tap
->flags
= IEEE80211_RADIOTAP_F_DATAPAD
;
3861 tap
->dbm_antsignal
= rssi_dbm
;
3862 tap
->dbm_antnoise
= RT2870_NOISE_FLOOR
;
3864 tap
->antsignal
= rssi
;
3865 tap
->chan_flags
= htole32(ic
->ic_curchan
->ic_flags
);
3866 tap
->chan_freq
= htole16(ic
->ic_curchan
->ic_freq
);
3867 tap
->chan_ieee
= ic
->ic_curchan
->ic_ieee
;
3868 tap
->chan_maxpow
= 0;
3870 if (phymode
== RT2870_RXWI_PHYMODE_CCK
|| phymode
== RT2870_RXWI_PHYMODE_OFDM
)
3871 tap
->rate
= rt2870_rxrate(rxwi
);
3873 tap
->rate
= mcs
| IEEE80211_RATE_MCS
;
3875 if (rxwi
->bw_mcs
& RT2870_RXWI_MCS_SHOTPRE
)
3876 tap
->flags
|= IEEE80211_RADIOTAP_F_SHORTPRE
;
3879 tap
->flags
|= IEEE80211_RADIOTAP_F_SHORTGI
;
3881 bpf_mtap2(sc
->drvbpf
, tap
, sc
->rxtap_len
, m
);
3884 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
3885 "%s: received frame: len=%d, phymode=%d, bw=%d, shortgi=%d, mcs=%d, "
3886 "ant=%d, rssi=%d/%d/%d, snr=%d/%d, wcid=0x%02x, ampdu=%d, amsdu=%d\n",
3887 device_get_nameunit(sc
->dev
),
3888 len
, phymode
, bw
, shortgi
, mcs
,
3889 ant
, rxwi
->rssi
[0], rxwi
->rssi
[1], rxwi
->rssi
[2],
3890 rxwi
->snr
[0], rxwi
->snr
[1],
3891 rxwi
->wcid
, ampdu
, amsdu
);
3893 ni
= ieee80211_find_rxnode(ic
, (struct ieee80211_frame_min
*) wh
);
3895 ieee80211_input(ic
, m
, ni
, rssi_dbm
- RT2870_NOISE_FLOOR
, RT2870_NOISE_FLOOR
, 0);
3897 ieee80211_free_node(ni
);
3903 static int rt2870_tx_frame(struct rt2870_softc
*sc
,
3904 struct mbuf
*m
, struct ieee80211_node
*ni
, int qid
)
3906 struct ieee80211com
*ic
;
3907 struct rt2870_softc_tx_ring
*ring
;
3908 struct rt2870_softc_tx_data
*data
;
3909 struct rt2870_txinfo
*txinfo
;
3910 struct rt2870_txwi
*txwi
;
3911 struct ieee80211_frame
*wh
;
3912 struct rt2870_softc_tx_radiotap_header
*tap
;
3913 u_int hdrsize
, hdrspace
;
3914 uint8_t type
, rate
, bw
, stbc
, shortgi
, mcs
, pid
, wcid
;
3915 uint16_t qos
, len
, dmalen
, mpdu_len
, dur
;
3916 int hasqos
, ackrate
;
3920 ring
= &sc
->tx_ring
[qid
];
3921 data
= STAILQ_FIRST(&ring
->inactive
);
3922 STAILQ_REMOVE_HEAD(&ring
->inactive
, next
);
3923 txinfo
= (struct rt2870_txinfo
*) data
->buf
;
3924 txwi
= (struct rt2870_txwi
*) (txinfo
+ 1);
3926 wh
= mtod(m
, struct ieee80211_frame
*);
3928 type
= wh
->i_fc
[0] & IEEE80211_FC0_TYPE_MASK
;
3930 hasqos
= IEEE80211_QOS_HAS_SEQ(wh
);
3933 if (IEEE80211_HAS_ADDR4(wh
))
3934 qos
= le16toh(*(const uint16_t *)
3935 (((struct ieee80211_qosframe_addr4
*) wh
)->i_qos
));
3937 qos
= le16toh(*(const uint16_t *)
3938 (((struct ieee80211_qosframe
*) wh
)->i_qos
));
3945 if (ni
->ni_flags
& IEEE80211_NODE_HT
)
3947 if (IEEE80211_IS_MULTICAST(wh
->i_addr1
) || type
!= IEEE80211_FC0_TYPE_DATA
)
3949 else if (ic
->ic_fixed_rate
!= IEEE80211_FIXED_RATE_NONE
)
3950 rate
= ic
->ic_fixed_rate
;
3952 rate
= ni
->ni_htrates
.rs_rates
[ni
->ni_txrate
];
3956 if (IEEE80211_IS_MULTICAST(wh
->i_addr1
) || type
!= IEEE80211_FC0_TYPE_DATA
)
3957 rate
= IEEE80211_IS_CHAN_5GHZ(ic
->ic_curchan
) ? 12 : 2;
3958 else if (ic
->ic_fixed_rate
!= IEEE80211_FIXED_RATE_NONE
)
3959 rate
= ic
->ic_fixed_rate
;
3961 rate
= ni
->ni_rates
.rs_rates
[ni
->ni_txrate
];
3964 rate
&= IEEE80211_RATE_VAL
;
3966 len
= sizeof(struct rt2870_txinfo
) + sizeof(struct rt2870_txwi
) + m
->m_pkthdr
.len
;
3968 /* align end on a 4-bytes boundary */
3970 dmalen
= (len
+ 3) & ~3;
3974 memset(txinfo
, 0, sizeof(struct rt2870_txinfo
));
3976 txinfo
->len
= htole16(dmalen
);
3978 txinfo
->qsel_flags
= (RT2870_TXINFO_QSEL_EDCA
<< RT2870_TXINFO_QSEL_SHIFT
);
3980 /* fill Tx wireless info */
3982 if (ni
->ni_flags
& IEEE80211_NODE_HT
)
3985 mcs
= rt2870_rate2mcs(rate
);
3989 wcid
= (type
== IEEE80211_FC0_TYPE_DATA
) ?
3990 RT2870_AID2WCID(ni
->ni_associd
) : 0xff;
3992 /* calculate MPDU length without padding */
3994 hdrsize
= ieee80211_hdrsize(wh
);
3995 hdrspace
= ieee80211_hdrspace(ic
, wh
);
3996 mpdu_len
= m
->m_pkthdr
.len
- hdrspace
+ hdrsize
;
3998 memset(txwi
, 0, sizeof(struct rt2870_txwi
));
4002 txwi
->pid_mpdu_len
= ((htole16(pid
) & RT2870_TXWI_PID_MASK
) <<
4003 RT2870_TXWI_PID_SHIFT
) | ((htole16(mpdu_len
) & RT2870_TXWI_MPDU_LEN_MASK
) <<
4004 RT2870_TXWI_MPDU_LEN_SHIFT
);
4006 stbc
= (ni
->ni_htcap
& IEEE80211_HTCAP_RXSTBC
) ? 1 : 0;
4008 shortgi
= (ic
->ic_flags_ext
& (IEEE80211_FEXT_SHORTGI20
| IEEE80211_FEXT_SHORTGI40
)) &&
4009 (ni
->ni_flags
& IEEE80211_NODE_HT
);
4011 txwi
->phymode_ifs_stbc_shortgi
|=
4012 ((stbc
& RT2870_TXWI_STBC_MASK
) << RT2870_TXWI_STBC_SHIFT
) |
4013 ((shortgi
& RT2870_TXWI_SHORTGI_MASK
) << RT2870_TXWI_SHORTGI_SHIFT
);
4015 if (ni
->ni_flags
& IEEE80211_NODE_HT
)
4017 txwi
->phymode_ifs_stbc_shortgi
|=
4018 (RT2870_TXWI_PHYMODE_HT_MIXED
<< RT2870_TXWI_PHYMODE_SHIFT
);
4022 if (!RT2870_RATE_IS_OFDM(rate
))
4024 txwi
->phymode_ifs_stbc_shortgi
|=
4025 (RT2870_TXWI_PHYMODE_CCK
<< RT2870_TXWI_PHYMODE_SHIFT
);
4027 if (rate
!= 2 && (ic
->ic_flags
& IEEE80211_F_SHPREAMBLE
))
4028 mcs
|= RT2870_TXWI_MCS_SHOTPRE
;
4032 txwi
->phymode_ifs_stbc_shortgi
|=
4033 (RT2870_TXWI_PHYMODE_OFDM
<< RT2870_TXWI_PHYMODE_SHIFT
);
4037 if ((ni
->ni_flags
& IEEE80211_NODE_HT
) &&
4038 (ni
->ni_htcap
& IEEE80211_HTCAP_CHWIDTH40
))
4039 bw
= RT2870_TXWI_BW_40
;
4041 bw
= RT2870_TXWI_BW_20
;
4043 txwi
->bw_mcs
= ((bw
& RT2870_TXWI_BW_MASK
) << RT2870_TXWI_BW_SHIFT
) |
4044 ((mcs
& RT2870_TXWI_MCS_MASK
) << RT2870_TXWI_MCS_SHIFT
);
4046 if (type
!= IEEE80211_FC0_TYPE_DATA
)
4047 txwi
->txop
= (RT2870_TXWI_TXOP_BACKOFF
<< RT2870_TXWI_TXOP_SHIFT
);
4049 txwi
->txop
= (RT2870_TXWI_TXOP_HT
<< RT2870_TXWI_TXOP_SHIFT
);
4051 /* skip ACKs for multicast frames and probe responses */
4053 if (!IEEE80211_IS_MULTICAST(wh
->i_addr1
) &&
4054 ((wh
->i_fc
[0] & (IEEE80211_FC0_TYPE_MASK
| IEEE80211_FC0_SUBTYPE_MASK
)) !=
4055 (IEEE80211_FC0_TYPE_MGT
| IEEE80211_FC0_SUBTYPE_PROBE_RESP
)) &&
4056 (!hasqos
|| (qos
& IEEE80211_QOS_ACKPOLICY
) != IEEE80211_QOS_ACKPOLICY_NOACK
))
4058 txwi
->bawin_size_xflags
|=
4059 (RT2870_TXWI_XFLAGS_ACK
<< RT2870_TXWI_XFLAGS_SHIFT
);
4061 if (ni
->ni_flags
& IEEE80211_NODE_HT
)
4063 /* preamble + plcp + signal extension */
4069 ackrate
= rt2870_ackrate(ic
, rate
);
4071 dur
= rt2870_txtime(RT2870_ACK_SIZE
, ackrate
, ic
->ic_flags
) +
4075 *(uint16_t *) wh
->i_dur
= htole16(dur
);
4078 /* ask MAC to insert timestamp into probe responses */
4080 if ((wh
->i_fc
[0] & (IEEE80211_FC0_TYPE_MASK
| IEEE80211_FC0_SUBTYPE_MASK
)) ==
4081 (IEEE80211_FC0_TYPE_MGT
| IEEE80211_FC0_SUBTYPE_PROBE_RESP
))
4082 txwi
->mpdu_density_flags
|=
4083 (RT2870_TXWI_FLAGS_TS
<< RT2870_TXWI_FLAGS_SHIFT
);
4085 if (bpf_peers_present(sc
->drvbpf
))
4089 tap
->flags
= IEEE80211_RADIOTAP_F_DATAPAD
;
4090 tap
->chan_flags
= htole32(ic
->ic_curchan
->ic_flags
);
4091 tap
->chan_freq
= htole16(ic
->ic_curchan
->ic_freq
);
4092 tap
->chan_ieee
= ic
->ic_curchan
->ic_ieee
;
4093 tap
->chan_maxpow
= 0;
4095 if (ni
->ni_flags
& IEEE80211_NODE_HT
)
4096 tap
->rate
= mcs
| IEEE80211_RATE_MCS
;
4100 if (mcs
& RT2870_TXWI_MCS_SHOTPRE
)
4101 tap
->flags
|= IEEE80211_RADIOTAP_F_SHORTPRE
;
4104 tap
->flags
|= IEEE80211_RADIOTAP_F_SHORTGI
;
4106 if (wh
->i_fc
[1] & IEEE80211_FC1_WEP
)
4107 tap
->flags
|= IEEE80211_RADIOTAP_F_WEP
;
4109 if (wh
->i_fc
[1] & IEEE80211_FC1_WEP
)
4111 wh
->i_fc
[1] &= ~IEEE80211_FC1_WEP
;
4113 bpf_mtap2(sc
->drvbpf
, tap
, sc
->txtap_len
, m
);
4115 wh
->i_fc
[1] |= IEEE80211_FC1_WEP
;
4119 bpf_mtap2(sc
->drvbpf
, tap
, sc
->txtap_len
, m
);
4123 m_copydata(m
, 0, m
->m_pkthdr
.len
, (caddr_t
) (txwi
+ 1));
4125 RT2870_DPRINTF(sc
, RT2870_DEBUG_TX
,
4126 "%s: sending frame: qid=%d, hdrsize=%d, hdrspace=%d, len=%d, "
4127 "bw=%d, stbc=%d, shortgi=%d, mcs=%d, wcid=0x%02x, DMA len=%d\n",
4128 device_get_nameunit(sc
->dev
),
4129 qid
, hdrsize
, hdrspace
, m
->m_pkthdr
.len
, bw
, stbc
, shortgi
, mcs
, wcid
, dmalen
);
4134 STAILQ_INSERT_TAIL(&ring
->active
, data
, next
);
4137 usbd_setup_xfer(data
->xfer
, ring
->usb_pipe
, ring
, data
->buf
, len
,
4138 USBD_FORCE_SHORT_XFER
| USBD_NO_COPY
, RT2870_USB_XFER_TIMEOUT
, rt2870_tx_intr
);
4140 usbd_transfer(data
->xfer
);
4148 static int rt2870_tx_raw(struct rt2870_softc
*sc
,
4149 struct mbuf
*m
, struct ieee80211_node
*ni
,
4150 const struct ieee80211_bpf_params
*params
)
4152 RT2870_DPRINTF(sc
, RT2870_DEBUG_TX
,
4154 device_get_nameunit(sc
->dev
));
4162 static void rt2870_rx_intr(usbd_xfer_handle xfer
,
4163 usbd_private_handle priv
, usbd_status status
)
4165 struct rt2870_softc
*sc
;
4167 struct rt2870_softc_rx_ring
*ring
;
4168 struct rt2870_softc_rx_data
*data
;
4173 ring
= &sc
->rx_ring
;
4175 if (!(ifp
->if_drv_flags
& IFF_DRV_RUNNING
))
4178 RT2870_DPRINTF(sc
, RT2870_DEBUG_INTR
,
4179 "%s: Rx interrupt: %s\n",
4180 device_get_nameunit(sc
->dev
), usbd_errstr(status
));
4183 sc
->rx_interrupts
++;
4187 case USBD_NORMAL_COMPLETION
:
4188 data
= STAILQ_FIRST(&ring
->active
);
4192 STAILQ_REMOVE_HEAD(&ring
->active
, next
);
4194 KASSERT(data
->xfer
== xfer
,
4195 ("%s: Rx interrupt: invalid USB xfer\n",
4196 device_get_nameunit(sc
->dev
)));
4198 usbd_get_xfer_status(xfer
, NULL
, NULL
, &len
, NULL
);
4202 STAILQ_INSERT_TAIL(&ring
->done
, data
, next
);
4204 taskqueue_enqueue(sc
->taskqueue
, &sc
->rx_done_task
);
4207 case USBD_CANCELLED
:
4208 case USBD_NOT_STARTED
:
4212 if (status
== USBD_STALLED
)
4213 usbd_clear_endpoint_stall_async(ring
->usb_pipe
);
4215 data
= STAILQ_FIRST(&ring
->active
);
4218 STAILQ_REMOVE_HEAD(&ring
->active
, next
);
4220 STAILQ_INSERT_TAIL(&ring
->active
, data
, next
);
4222 usbd_setup_xfer(xfer
, ring
->usb_pipe
, sc
, data
->buf
,
4223 RT2870_USB_RX_BULK_BUFLEN
, USBD_SHORT_XFER_OK
| USBD_NO_COPY
,
4224 USBD_NO_TIMEOUT
, rt2870_rx_intr
);
4226 usbd_transfer(xfer
);
4235 static void rt2870_tx_intr(usbd_xfer_handle xfer
,
4236 usbd_private_handle priv
, usbd_status status
)
4238 struct rt2870_softc
*sc
;
4240 struct rt2870_softc_tx_ring
*ring
;
4241 struct rt2870_softc_tx_data
*data
;
4247 if (!(ifp
->if_drv_flags
& IFF_DRV_RUNNING
))
4250 RT2870_DPRINTF(sc
, RT2870_DEBUG_INTR
,
4251 "%s: Tx interrupt: %s\n",
4252 device_get_nameunit(sc
->dev
), usbd_errstr(status
));
4255 sc
->tx_interrupts
[ring
->qid
]++;
4259 case USBD_NORMAL_COMPLETION
:
4260 data
= STAILQ_FIRST(&ring
->active
);
4264 STAILQ_REMOVE_HEAD(&ring
->active
, next
);
4266 STAILQ_INSERT_TAIL(&ring
->done
, data
, next
);
4268 sc
->tx_qid_pending_mask
|= (1 << ring
->qid
);
4270 taskqueue_enqueue(sc
->taskqueue
, &sc
->tx_done_task
);
4273 case USBD_CANCELLED
:
4274 case USBD_NOT_STARTED
:
4278 data
= STAILQ_FIRST(&ring
->active
);
4281 STAILQ_REMOVE_HEAD(&ring
->active
, next
);
4283 if (data
->m
!= NULL
)
4289 if (data
->ni
!= NULL
)
4291 ieee80211_free_node(data
->ni
);
4295 STAILQ_INSERT_TAIL(&ring
->inactive
, data
, next
);
4300 printf("%s: could not transmit buffer: qid=%d, status=%s\n",
4301 device_get_nameunit(sc
->dev
), ring
->qid
, usbd_errstr(status
));
4304 ifp
->if_drv_flags
&= ~IFF_DRV_OACTIVE
;
4306 if (status
== USBD_STALLED
)
4307 usbd_clear_endpoint_stall_async(ring
->usb_pipe
);
4313 * rt2870_rx_done_task
4315 static void rt2870_rx_done_task(void *context
, int pending
)
4317 struct rt2870_softc
*sc
;
4323 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
4324 "%s: Rx done task\n",
4325 device_get_nameunit(sc
->dev
));
4327 if (!(ifp
->if_drv_flags
& IFF_DRV_RUNNING
))
4330 if (rt2870_rx_eof(sc
, sc
->rx_process_limit
) != 0)
4332 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
4333 "%s: Rx done task: scheduling again\n",
4334 device_get_nameunit(sc
->dev
));
4336 taskqueue_enqueue(sc
->taskqueue
, &sc
->rx_done_task
);
4341 * rt2870_tx_done_task
4343 static void rt2870_tx_done_task(void *context
, int pending
)
4345 struct rt2870_softc
*sc
;
4352 RT2870_DPRINTF(sc
, RT2870_DEBUG_TX
,
4353 "%s: Tx done task\n",
4354 device_get_nameunit(sc
->dev
));
4356 if (!(ifp
->if_drv_flags
& IFF_DRV_RUNNING
))
4359 for (i
= sc
->usb_endpoints
- 2; i
>= 0; i
--)
4361 if (sc
->tx_qid_pending_mask
& (1 << i
))
4363 sc
->tx_qid_pending_mask
&= ~(1 << i
);
4365 rt2870_tx_eof(sc
, &sc
->tx_ring
[i
]);
4369 if (sc
->tx_qid_pending_mask
!= 0)
4371 RT2870_DPRINTF(sc
, RT2870_DEBUG_TX
,
4372 "%s: Tx done task: scheduling again\n",
4373 device_get_nameunit(sc
->dev
));
4375 taskqueue_enqueue(sc
->taskqueue
, &sc
->tx_done_task
);
4380 ifp
->if_drv_flags
&= ~IFF_DRV_OACTIVE
;
4384 * rt2870_periodic_task
4386 static void rt2870_periodic_task(void *context
, int pending
)
4388 struct rt2870_softc
*sc
;
4390 struct ieee80211com
*ic
;
4396 RT2870_DPRINTF(sc
, RT2870_DEBUG_PERIODIC
,
4397 "%s: periodic task: round=%lu\n",
4398 device_get_nameunit(sc
->dev
), sc
->periodic_round
);
4400 if (!(ifp
->if_drv_flags
& IFF_DRV_RUNNING
))
4403 sc
->periodic_round
++;
4405 rt2870_update_stats(sc
);
4407 if ((sc
->periodic_round
% 10) == 0)
4409 rt2870_update_raw_counters(sc
);
4411 rt2870_watchdog(sc
);
4413 if (ic
->ic_opmode
== IEEE80211_M_STA
)
4414 rt2870_amrr_update_iter_func(sc
, ic
->ic_bss
);
4416 ieee80211_iterate_nodes(&ic
->ic_sta
, rt2870_amrr_update_iter_func
, sc
);
4419 callout_reset(&sc
->periodic_ch
, hz
/ 10, rt2870_periodic
, sc
);
4425 static void rt2870_cmd_task(void *context
, int pending
)
4427 struct rt2870_softc
*sc
;
4428 struct rt2870_softc_cmd_ring
*ring
;
4429 struct rt2870_softc_cmd
*cmd
;
4432 ring
= &sc
->cmd_ring
;
4436 RT2870_SOFTC_LOCK(sc
);
4438 cmd
= STAILQ_FIRST(&ring
->active
);
4441 RT2870_SOFTC_UNLOCK(sc
);
4445 STAILQ_REMOVE_HEAD(&ring
->active
, next
);
4447 RT2870_SOFTC_UNLOCK(sc
);
4449 cmd
->cb(sc
, cmd
->data
);
4451 RT2870_SOFTC_LOCK(sc
);
4453 STAILQ_INSERT_TAIL(&ring
->inactive
, cmd
, next
);
4456 RT2870_SOFTC_UNLOCK(sc
);
4463 static int rt2870_rx_eof(struct rt2870_softc
*sc
, int limit
)
4466 struct rt2870_softc_rx_ring
*ring
;
4467 struct rt2870_softc_rx_data
*data
;
4473 ring
= &sc
->rx_ring
;
4479 RT2870_SOFTC_LOCK(sc
);
4481 data
= STAILQ_FIRST(&ring
->done
);
4484 RT2870_SOFTC_UNLOCK(sc
);
4488 STAILQ_REMOVE_HEAD(&ring
->done
, next
);
4490 RT2870_SOFTC_UNLOCK(sc
);
4495 if (len
< RT2870_RX_DESC_SIZE
)
4501 while (len
> (sizeof(uint32_t) + sizeof(struct rt2870_rxinfo
)))
4503 dmalen
= (le32toh(*(uint32_t *) buf
) & 0xffff);
4505 if (dmalen
== 0 || (dmalen
& 3) != 0)
4507 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
4508 "%s: bad DMA len=%u\n",
4509 device_get_nameunit(sc
->dev
), dmalen
);
4513 if ((dmalen
+ sizeof(uint32_t) + sizeof(struct rt2870_rxinfo
)) > len
)
4515 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
4516 "%s: bad DMA len: DMA len=%u, USB len=%d\n",
4517 device_get_nameunit(sc
->dev
),
4518 (dmalen
+ sizeof(uint32_t) + sizeof(struct rt2870_rxinfo
)), len
);
4524 rt2870_rx_frame(sc
, buf
+ sizeof(uint32_t), dmalen
);
4526 buf
+= (dmalen
+ sizeof(uint32_t) + sizeof(struct rt2870_rxinfo
));
4527 len
-= (dmalen
+ sizeof(uint32_t) + sizeof(struct rt2870_rxinfo
));
4532 RT2870_SOFTC_LOCK(sc
);
4534 STAILQ_INSERT_TAIL(&ring
->active
, data
, next
);
4536 RT2870_SOFTC_UNLOCK(sc
);
4538 usbd_setup_xfer(data
->xfer
, ring
->usb_pipe
, sc
, data
->buf
,
4539 RT2870_USB_RX_BULK_BUFLEN
, USBD_SHORT_XFER_OK
| USBD_NO_COPY
,
4540 USBD_NO_TIMEOUT
, rt2870_rx_intr
);
4542 usbd_transfer(data
->xfer
);
4547 RT2870_DPRINTF(sc
, RT2870_DEBUG_RX
,
4548 "%s: Rx eof: nframes=%d\n",
4549 device_get_nameunit(sc
->dev
), nframes
);
4551 sc
->rx_packets
+= nframes
;
4553 return (limit
== 0);
4559 static void rt2870_tx_eof(struct rt2870_softc
*sc
,
4560 struct rt2870_softc_tx_ring
*ring
)
4563 struct rt2870_softc_tx_data
*data
;
4572 RT2870_SOFTC_LOCK(sc
);
4574 data
= STAILQ_FIRST(&ring
->done
);
4577 RT2870_SOFTC_UNLOCK(sc
);
4581 STAILQ_REMOVE_HEAD(&ring
->done
, next
);
4583 RT2870_SOFTC_UNLOCK(sc
);
4587 if (data
->m
->m_flags
& M_TXCB
)
4588 ieee80211_process_callback(data
->ni
, data
->m
, 0);
4592 ieee80211_free_node(data
->ni
);
4597 RT2870_SOFTC_LOCK(sc
);
4599 STAILQ_INSERT_TAIL(&ring
->inactive
, data
, next
);
4603 RT2870_SOFTC_UNLOCK(sc
);
4608 RT2870_DPRINTF(sc
, RT2870_DEBUG_TX
,
4609 "%s: Tx eof: qid=%d, nframes=%d\n",
4610 device_get_nameunit(sc
->dev
), ring
->qid
, nframes
);
4614 * rt2870_update_stats
4616 static void rt2870_update_stats(struct rt2870_softc
*sc
)
4619 struct ieee80211com
*ic
;
4620 struct ieee80211_node
*ni
;
4622 int beacons
, noretryok
, retryok
, failed
;
4629 RT2870_DPRINTF(sc
, RT2870_DEBUG_STATS
,
4630 "%s: update statistic\n",
4631 device_get_nameunit(sc
->dev
));
4633 rt2870_drain_fifo_stats(sc
);
4635 /* read and clear Tx statistic registers */
4637 rt2870_io_mac_read_multi(sc
, RT2870_REG_TX_STA_CNT0
,
4638 stacnt
, sizeof(stacnt
));
4640 stacnt
[0] = le32toh(stacnt
[0]);
4641 stacnt
[1] = le32toh(stacnt
[1]);
4642 stacnt
[2] = le32toh(stacnt
[2]);
4644 beacons
= stacnt
[0] >> 16;
4645 noretryok
= stacnt
[1] & 0xffff;
4646 retryok
= stacnt
[1] >> 16;
4647 failed
= stacnt
[0] & 0xffff;
4649 RT2870_DPRINTF(sc
, RT2870_DEBUG_STATS
,
4650 "%s: update statistic: beacons=%d, noretryok=%d, retryok=%d, failed=%d\n",
4651 device_get_nameunit(sc
->dev
),
4652 beacons
, noretryok
, retryok
, failed
);
4654 ifp
->if_oerrors
+= failed
;
4656 sc
->tx_beacons
+= beacons
;
4657 sc
->tx_noretryok
+= noretryok
;
4658 sc
->tx_retryok
+= retryok
;
4659 sc
->tx_failed
+= failed
;
4661 if (ic
->ic_opmode
== IEEE80211_M_STA
&& ic
->ic_state
== IEEE80211_S_RUN
)
4665 associd
= (ni
!= NULL
) ? ni
->ni_associd
: 0;
4666 wcid
= RT2870_AID2WCID(associd
);
4668 rt2870_amrr_tx_update(&sc
->amrr_node
[wcid
],
4669 noretryok
+ retryok
+ failed
, noretryok
+ retryok
, retryok
+ failed
);
4676 static void rt2870_watchdog(struct rt2870_softc
*sc
)
4681 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_PBF_TXRXQ_PCNT
);
4683 RT2870_DPRINTF(sc
, RT2870_DEBUG_WATCHDOG
,
4684 "%s: watchdog: TXRXQ_PCNT=0x%08x\n",
4685 device_get_nameunit(sc
->dev
), tmp
);
4687 if (((tmp
>> RT2870_REG_TX0Q_PCNT_SHIFT
) & RT2870_REG_TX0Q_PCNT_MASK
) != 0)
4689 sc
->tx_queue_not_empty
[0]++;
4691 rt2870_io_mac_write(sc
, RT2870_REG_PBF_CFG
, 0xf40012);
4693 for (ntries
= 0; ntries
< 10; ntries
++)
4695 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_PBF_TXRXQ_PCNT
);
4696 if (((tmp
>> RT2870_REG_TX0Q_PCNT_SHIFT
) & RT2870_REG_TX0Q_PCNT_MASK
) == 0)
4702 rt2870_io_mac_write(sc
, RT2870_REG_PBF_CFG
, 0xf40006);
4705 if (((tmp
>> RT2870_REG_TX1Q_PCNT_SHIFT
) & RT2870_REG_TX1Q_PCNT_MASK
) != 0)
4707 sc
->tx_queue_not_empty
[1]++;
4709 rt2870_io_mac_write(sc
, RT2870_REG_PBF_CFG
, 0xf4000a);
4711 for (ntries
= 0; ntries
< 10; ntries
++)
4713 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_PBF_TXRXQ_PCNT
);
4714 if (((tmp
>> RT2870_REG_TX1Q_PCNT_SHIFT
) & RT2870_REG_TX1Q_PCNT_MASK
) == 0)
4720 rt2870_io_mac_write(sc
, RT2870_REG_PBF_CFG
, 0xf40006);
4725 * rt2870_drain_fifo_stats
4727 static void rt2870_drain_fifo_stats(struct rt2870_softc
*sc
)
4731 uint8_t wcid
, mcs
, pid
;
4732 int ok
, agg
, retrycnt
;
4734 ifp
= sc
->ic
.ic_ifp
;
4736 /* drain Tx status FIFO (maxsize = 16) */
4738 while ((stats
= rt2870_io_mac_read(sc
, RT2870_REG_TX_STA_FIFO
)) &
4739 RT2870_REG_TX_STA_FIFO_VALID
)
4741 wcid
= (stats
>> RT2870_REG_TX_STA_FIFO_WCID_SHIFT
) &
4742 RT2870_REG_TX_STA_FIFO_WCID_MASK
;
4744 /* if no ACK was requested, no feedback is available */
4746 if (!(stats
& RT2870_REG_TX_STA_FIFO_ACK_REQ
) || wcid
== 0xff)
4749 /* update AMRR statistic */
4751 ok
= (stats
& RT2870_REG_TX_STA_FIFO_TX_OK
) ? 1 : 0;
4752 agg
= (stats
& RT2870_REG_TX_STA_FIFO_AGG
) ? 1 : 0;
4753 mcs
= (stats
>> RT2870_REG_TX_STA_FIFO_MCS_SHIFT
) &
4754 RT2870_REG_TX_STA_FIFO_MCS_MASK
;
4755 pid
= (stats
>> RT2870_REG_TX_STA_FIFO_PID_SHIFT
) &
4756 RT2870_REG_TX_STA_FIFO_PID_MASK
;
4757 retrycnt
= pid
- mcs
;
4759 RT2870_DPRINTF(sc
, RT2870_DEBUG_STATS
,
4760 "%s: FIFO statistic: wcid=0x%02x, ok=%d, agg=%d, mcs=0x%02x, pid=0x%02x, retrycnt=%d\n",
4761 device_get_nameunit(sc
->dev
),
4762 wcid
, ok
, agg
, mcs
, pid
, retrycnt
);
4764 rt2870_amrr_tx_complete(&sc
->amrr_node
[wcid
], ok
, retrycnt
);
4772 * rt2870_update_raw_counters
4774 static void rt2870_update_raw_counters(struct rt2870_softc
*sc
)
4778 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_AGG_CNT
);
4780 sc
->tx_nonagg
+= tmp
& 0xffff;
4781 sc
->tx_agg
+= tmp
>> 16;
4783 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_AGG_CNT0
);
4785 sc
->tx_ampdu
+= (tmp
& 0xffff) / 1 + (tmp
>> 16) / 2;
4787 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_AGG_CNT1
);
4789 sc
->tx_ampdu
+= (tmp
& 0xffff) / 3 + (tmp
>> 16) / 4;
4791 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_AGG_CNT2
);
4793 sc
->tx_ampdu
+= (tmp
& 0xffff) / 5 + (tmp
>> 16) / 6;
4795 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_AGG_CNT3
);
4797 sc
->tx_ampdu
+= (tmp
& 0xffff) / 7 + (tmp
>> 16) / 8;
4799 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_AGG_CNT4
);
4801 sc
->tx_ampdu
+= (tmp
& 0xffff) / 9 + (tmp
>> 16) / 10;
4803 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_AGG_CNT5
);
4805 sc
->tx_ampdu
+= (tmp
& 0xffff) / 11 + (tmp
>> 16) / 12;
4807 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_AGG_CNT6
);
4809 sc
->tx_ampdu
+= (tmp
& 0xffff) / 13 + (tmp
>> 16) / 14;
4811 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_TX_AGG_CNT7
);
4813 sc
->tx_ampdu
+= (tmp
& 0xffff) / 15 + (tmp
>> 16) / 16;
4815 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_RX_STA_CNT0
);
4817 sc
->rx_crc_errors
+= tmp
& 0xffff;
4818 sc
->rx_phy_errors
+= tmp
>> 16;
4820 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_RX_STA_CNT1
);
4822 sc
->rx_false_ccas
+= tmp
& 0xffff;
4823 sc
->rx_plcp_errors
+= tmp
>> 16;
4825 tmp
= rt2870_io_mac_read(sc
, RT2870_REG_RX_STA_CNT2
);
4827 sc
->rx_dup_packets
+= tmp
& 0xffff;
4828 sc
->rx_fifo_overflows
+= tmp
>> 16;
4832 * rt2870_alloc_rx_ring
4834 static int rt2870_alloc_rx_ring(struct rt2870_softc
*sc
,
4835 struct rt2870_softc_rx_ring
*ring
)
4837 struct rt2870_softc_rx_data
*data
;
4840 STAILQ_INIT(&ring
->active
);
4841 STAILQ_INIT(&ring
->done
);
4843 for (i
= 0; i
< RT2870_SOFTC_RX_RING_DATA_COUNT
; i
++)
4845 data
= &ring
->data
[i
];
4847 data
->xfer
= usbd_alloc_xfer(sc
->usb_dev
);
4848 if (data
->xfer
== NULL
)
4850 printf("%s: could not allocate Rx xfer\n",
4851 device_get_nameunit(sc
->dev
));
4856 data
->buf
= usbd_alloc_buffer(data
->xfer
, RT2870_USB_RX_BULK_BUFLEN
);
4857 if (data
->buf
== NULL
)
4859 printf("%s: could not allocate Rx buffer\n",
4860 device_get_nameunit(sc
->dev
));
4870 rt2870_free_rx_ring(sc
, ring
);
4876 * rt2870_reset_rx_ring
4878 static void rt2870_reset_rx_ring(struct rt2870_softc
*sc
,
4879 struct rt2870_softc_rx_ring
*ring
)
4881 STAILQ_INIT(&ring
->active
);
4882 STAILQ_INIT(&ring
->done
);
4886 * rt2870_free_rx_ring
4888 static void rt2870_free_rx_ring(struct rt2870_softc
*sc
,
4889 struct rt2870_softc_rx_ring
*ring
)
4891 struct rt2870_softc_rx_data
*data
;
4894 for (i
= 0; i
< RT2870_SOFTC_RX_RING_DATA_COUNT
; i
++)
4896 data
= &ring
->data
[i
];
4898 if (data
->xfer
!= NULL
)
4900 usbd_free_xfer(data
->xfer
);
4907 * rt2870_alloc_tx_ring
4909 static int rt2870_alloc_tx_ring(struct rt2870_softc
*sc
,
4910 struct rt2870_softc_tx_ring
*ring
, int qid
)
4912 struct rt2870_softc_tx_data
*data
;
4915 STAILQ_INIT(&ring
->inactive
);
4916 STAILQ_INIT(&ring
->active
);
4917 STAILQ_INIT(&ring
->done
);
4923 for (i
= 0; i
< RT2870_SOFTC_TX_RING_DATA_COUNT
; i
++)
4925 data
= &ring
->data
[i
];
4927 data
->xfer
= usbd_alloc_xfer(sc
->usb_dev
);
4928 if (data
->xfer
== NULL
)
4930 printf("%s: could not allocate Tx xfer\n",
4931 device_get_nameunit(sc
->dev
));
4936 data
->buf
= usbd_alloc_buffer(data
->xfer
,
4937 RT2870_TX_DESC_SIZE
+ MJUMPAGESIZE
);
4938 if (data
->buf
== NULL
)
4940 printf("%s: could not allocate Tx buffer\n",
4941 device_get_nameunit(sc
->dev
));
4946 memset(data
->buf
, 0, RT2870_TX_DESC_SIZE
);
4948 STAILQ_INSERT_TAIL(&ring
->inactive
, data
, next
);
4955 rt2870_free_tx_ring(sc
, ring
);
4961 * rt2870_reset_tx_ring
4963 static void rt2870_reset_tx_ring(struct rt2870_softc
*sc
,
4964 struct rt2870_softc_tx_ring
*ring
)
4966 struct rt2870_softc_tx_data
*data
;
4969 STAILQ_INIT(&ring
->inactive
);
4970 STAILQ_INIT(&ring
->active
);
4971 STAILQ_INIT(&ring
->done
);
4975 for (i
= 0; i
< RT2870_SOFTC_TX_RING_DATA_COUNT
; i
++)
4977 data
= &ring
->data
[i
];
4979 if (data
->m
!= NULL
)
4985 if (data
->ni
!= NULL
)
4987 ieee80211_free_node(data
->ni
);
4991 STAILQ_INSERT_TAIL(&ring
->inactive
, data
, next
);
4996 * rt2870_free_tx_ring
4998 static void rt2870_free_tx_ring(struct rt2870_softc
*sc
,
4999 struct rt2870_softc_tx_ring
*ring
)
5001 struct rt2870_softc_tx_data
*data
;
5004 for (i
= 0; i
< RT2870_SOFTC_TX_RING_DATA_COUNT
; i
++)
5006 data
= &ring
->data
[i
];
5008 if (data
->xfer
!= NULL
)
5010 usbd_free_xfer(data
->xfer
);
5014 if (data
->m
!= NULL
)
5020 if (data
->ni
!= NULL
)
5022 ieee80211_free_node(data
->ni
);
5029 * rt2870_reset_cmd_ring
5031 static void rt2870_reset_cmd_ring(struct rt2870_softc
*sc
,
5032 struct rt2870_softc_cmd_ring
*ring
)
5034 struct rt2870_softc_cmd
*cmd
;
5037 STAILQ_INIT(&ring
->inactive
);
5038 STAILQ_INIT(&ring
->active
);
5042 for (i
= 0; i
< RT2870_SOFTC_CMD_RING_CMD_COUNT
; i
++)
5044 cmd
= &ring
->cmd
[i
];
5046 STAILQ_INSERT_TAIL(&ring
->inactive
, cmd
, next
);
5051 * rt2870_sysctl_attach
5053 static void rt2870_sysctl_attach(struct rt2870_softc
*sc
)
5055 struct sysctl_ctx_list
*ctx
;
5056 struct sysctl_oid
*tree
;
5057 struct sysctl_oid
*stats
;
5059 ctx
= device_get_sysctl_ctx(sc
->dev
);
5060 tree
= device_get_sysctl_tree(sc
->dev
);
5062 stats
= SYSCTL_ADD_NODE(ctx
, SYSCTL_CHILDREN(tree
), OID_AUTO
,
5063 "stats", CTLFLAG_RD
, 0, "statistic");
5065 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5066 "interrupts", CTLFLAG_RD
, &sc
->interrupts
, 0,
5069 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5070 "rx_interrupts", CTLFLAG_RD
, &sc
->rx_interrupts
, 0,
5073 if (sc
->usb_endpoints
== (RT2870_SOFTC_TX_RING_COUNT
+ 1))
5075 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5076 "tx_mgmt_interrupts", CTLFLAG_RD
, &sc
->tx_interrupts
[5], 0,
5077 "Tx MGMT interrupts");
5079 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5080 "tx_hcca_interrupts", CTLFLAG_RD
, &sc
->tx_interrupts
[4], 0,
5081 "Tx HCCA interrupts");
5084 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5085 "tx_ac3_interrupts", CTLFLAG_RD
, &sc
->tx_interrupts
[3], 0,
5086 "Tx AC3 interrupts");
5088 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5089 "tx_ac2_interrupts", CTLFLAG_RD
, &sc
->tx_interrupts
[2], 0,
5090 "Tx AC2 interrupts");
5092 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5093 "tx_ac1_interrupts", CTLFLAG_RD
, &sc
->tx_interrupts
[1], 0,
5094 "Tx AC1 interrupts");
5096 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5097 "tx_ac0_interrupts", CTLFLAG_RD
, &sc
->tx_interrupts
[0], 0,
5098 "Tx AC0 interrupts");
5100 if (sc
->usb_endpoints
== (RT2870_SOFTC_TX_RING_COUNT
+ 1))
5102 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5103 "tx_mgmt_data_queued", CTLFLAG_RD
, &sc
->tx_ring
[5].queued
, 0,
5104 "Tx MGMT data queued");
5106 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5107 "tx_hcca_data_queued", CTLFLAG_RD
, &sc
->tx_ring
[4].queued
, 0,
5108 "Tx HCCA data queued");
5111 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5112 "tx_ac3_data_queued", CTLFLAG_RD
, &sc
->tx_ring
[3].queued
, 0,
5113 "Tx AC3 data queued");
5115 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5116 "tx_ac2_data_queued", CTLFLAG_RD
, &sc
->tx_ring
[2].queued
, 0,
5117 "Tx AC2 data queued");
5119 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5120 "tx_ac1_data_queued", CTLFLAG_RD
, &sc
->tx_ring
[1].queued
, 0,
5121 "Tx AC1 data queued");
5123 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5124 "tx_ac0_data_queued", CTLFLAG_RD
, &sc
->tx_ring
[0].queued
, 0,
5125 "Tx AC0 data queued");
5127 if (sc
->usb_endpoints
== (RT2870_SOFTC_TX_RING_COUNT
+ 1))
5129 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5130 "tx_mgmt_data_queue_full", CTLFLAG_RD
, &sc
->tx_data_queue_full
[5], 0,
5131 "Tx MGMT data queue full");
5133 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5134 "tx_hcca_data_queue_full", CTLFLAG_RD
, &sc
->tx_data_queue_full
[4], 0,
5135 "Tx HCCA data queue full");
5138 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5139 "tx_ac3_data_queue_full", CTLFLAG_RD
, &sc
->tx_data_queue_full
[3], 0,
5140 "Tx AC3 data queue full");
5142 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5143 "tx_ac2_data_queue_full", CTLFLAG_RD
, &sc
->tx_data_queue_full
[2], 0,
5144 "Tx AC2 data queue full");
5146 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5147 "tx_ac1_data_queue_full", CTLFLAG_RD
, &sc
->tx_data_queue_full
[1], 0,
5148 "Tx AC1 data queue full");
5150 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5151 "tx_ac0_data_queue_full", CTLFLAG_RD
, &sc
->tx_data_queue_full
[0], 0,
5152 "Tx AC0 data queue full");
5154 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5155 "tx_watchdog_timeouts", CTLFLAG_RD
, &sc
->tx_watchdog_timeouts
, 0,
5156 "Tx watchdog timeouts");
5158 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5159 "rx_mbuf_alloc_errors", CTLFLAG_RD
, &sc
->rx_mbuf_alloc_errors
, 0,
5160 "Rx mbuf allocation errors");
5162 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5163 "tx_queue_0_not_empty", CTLFLAG_RD
, &sc
->tx_queue_not_empty
[0], 0,
5164 "Tx queue 0 not empty");
5166 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5167 "tx_queue_1_not_empty", CTLFLAG_RD
, &sc
->tx_queue_not_empty
[1], 0,
5168 "Tx queue 1 not empty");
5170 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5171 "tx_beacons", CTLFLAG_RD
, &sc
->tx_beacons
, 0,
5174 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5175 "tx_noretryok", CTLFLAG_RD
, &sc
->tx_noretryok
, 0,
5176 "Tx successfull without retries");
5178 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5179 "tx_retryok", CTLFLAG_RD
, &sc
->tx_retryok
, 0,
5180 "Tx successfull with retries");
5182 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5183 "tx_failed", CTLFLAG_RD
, &sc
->tx_failed
, 0,
5186 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5187 "tx_nonagg", CTLFLAG_RD
, &sc
->tx_nonagg
, 0,
5188 "Tx non-aggregated");
5190 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5191 "tx_agg", CTLFLAG_RD
, &sc
->tx_agg
, 0,
5194 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5195 "tx_ampdu", CTLFLAG_RD
, &sc
->tx_ampdu
, 0,
5198 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5199 "rx_packets", CTLFLAG_RD
, &sc
->rx_packets
, 0,
5202 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5203 "rx_ampdu", CTLFLAG_RD
, &sc
->rx_ampdu
, 0,
5206 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5207 "rx_amsdu", CTLFLAG_RD
, &sc
->rx_amsdu
, 0,
5210 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5211 "rx_crc_errors", CTLFLAG_RD
, &sc
->rx_crc_errors
, 0,
5214 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5215 "rx_phy_errors", CTLFLAG_RD
, &sc
->rx_phy_errors
, 0,
5218 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5219 "rx_false_ccas", CTLFLAG_RD
, &sc
->rx_false_ccas
, 0,
5222 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5223 "rx_plcp_errors", CTLFLAG_RD
, &sc
->rx_plcp_errors
, 0,
5226 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5227 "rx_dup_packets", CTLFLAG_RD
, &sc
->rx_dup_packets
, 0,
5228 "Rx duplicate packets");
5230 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(stats
), OID_AUTO
,
5231 "rx_fifo_overflows", CTLFLAG_RD
, &sc
->rx_fifo_overflows
, 0,
5232 "Rx FIFO overflows");
5235 static device_method_t rt2870_dev_methods
[] =
5237 DEVMETHOD(device_probe
, rt2870_probe
),
5238 DEVMETHOD(device_attach
, rt2870_attach
),
5239 DEVMETHOD(device_detach
, rt2870_detach
),
5243 static driver_t rt2870_driver
=
5247 sizeof(struct rt2870_softc
),
5250 static devclass_t rt2870_dev_class
;
5252 DRIVER_MODULE(rt2870
, uhub
, rt2870_driver
, rt2870_dev_class
,
5253 usbd_driver_load
, 0);
5255 MODULE_DEPEND(rt2870
, usb
, 1, 1, 1);
5256 MODULE_DEPEND(rt2870
, wlan
, 1, 1, 1);