1 /* $OpenBSD: if_rsu.c,v 1.17 2013/04/15 09:23:01 mglocker Exp $ */
4 * Copyright (c) 2010 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.
18 #include <sys/cdefs.h>
19 __FBSDID("$FreeBSD$");
22 * Driver for Realtek RTL8188SU/RTL8191SU/RTL8192SU.
26 * o hostap / ibss / mesh
27 * o power-save operation
32 #include <sys/param.h>
33 #include <sys/endian.h>
34 #include <sys/sockio.h>
35 #include <sys/malloc.h>
37 #include <sys/kernel.h>
38 #include <sys/socket.h>
39 #include <sys/systm.h>
42 #include <sys/firmware.h>
43 #include <sys/module.h>
47 #include <net/if_var.h>
48 #include <net/if_arp.h>
49 #include <net/if_dl.h>
50 #include <net/if_media.h>
51 #include <net/if_types.h>
53 #include <netinet/in.h>
54 #include <netinet/in_systm.h>
55 #include <netinet/in_var.h>
56 #include <netinet/if_ether.h>
57 #include <netinet/ip.h>
59 #include <net80211/ieee80211_var.h>
60 #include <net80211/ieee80211_regdomain.h>
61 #include <net80211/ieee80211_radiotap.h>
63 #include <dev/usb/usb.h>
64 #include <dev/usb/usbdi.h>
67 #include <dev/rtwn/if_rtwn_ridx.h> /* XXX */
68 #include <dev/usb/wlan/if_rsureg.h>
70 #define RSU_RATE_IS_CCK RTWN_RATE_IS_CCK
73 static int rsu_debug = 0;
74 SYSCTL_NODE(_hw_usb, OID_AUTO, rsu, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
76 SYSCTL_INT(_hw_usb_rsu, OID_AUTO, debug, CTLFLAG_RWTUN, &rsu_debug, 0,
78 #define RSU_DPRINTF(_sc, _flg, ...) \
80 if (((_flg) == (RSU_DEBUG_ANY)) || (rsu_debug & (_flg))) \
81 device_printf((_sc)->sc_dev, __VA_ARGS__); \
84 #define RSU_DPRINTF(_sc, _flg, ...)
87 static int rsu_enable_11n = 1;
88 TUNABLE_INT("hw.usb.rsu.enable_11n", &rsu_enable_11n);
90 #define RSU_DEBUG_ANY 0xffffffff
91 #define RSU_DEBUG_TX 0x00000001
92 #define RSU_DEBUG_RX 0x00000002
93 #define RSU_DEBUG_RESET 0x00000004
94 #define RSU_DEBUG_CALIB 0x00000008
95 #define RSU_DEBUG_STATE 0x00000010
96 #define RSU_DEBUG_SCAN 0x00000020
97 #define RSU_DEBUG_FWCMD 0x00000040
98 #define RSU_DEBUG_TXDONE 0x00000080
99 #define RSU_DEBUG_FW 0x00000100
100 #define RSU_DEBUG_FWDBG 0x00000200
101 #define RSU_DEBUG_AMPDU 0x00000400
102 #define RSU_DEBUG_KEY 0x00000800
103 #define RSU_DEBUG_USB 0x00001000
105 static const STRUCT_USB_HOST_ID rsu_devs[] = {
106 #define RSU_HT_NOT_SUPPORTED 0
107 #define RSU_HT_SUPPORTED 1
108 #define RSU_DEV_HT(v,p) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, \
110 #define RSU_DEV(v,p) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, \
111 RSU_HT_NOT_SUPPORTED) }
112 RSU_DEV(ASUS, RTL8192SU),
113 RSU_DEV(AZUREWAVE, RTL8192SU_4),
114 RSU_DEV(SITECOMEU, WLA1000),
115 RSU_DEV_HT(ACCTON, RTL8192SU),
116 RSU_DEV_HT(ASUS, USBN10),
117 RSU_DEV_HT(AZUREWAVE, RTL8192SU_1),
118 RSU_DEV_HT(AZUREWAVE, RTL8192SU_2),
119 RSU_DEV_HT(AZUREWAVE, RTL8192SU_3),
120 RSU_DEV_HT(AZUREWAVE, RTL8192SU_5),
121 RSU_DEV_HT(BELKIN, RTL8192SU_1),
122 RSU_DEV_HT(BELKIN, RTL8192SU_2),
123 RSU_DEV_HT(BELKIN, RTL8192SU_3),
124 RSU_DEV_HT(CONCEPTRONIC2, RTL8192SU_1),
125 RSU_DEV_HT(CONCEPTRONIC2, RTL8192SU_2),
126 RSU_DEV_HT(CONCEPTRONIC2, RTL8192SU_3),
127 RSU_DEV_HT(COREGA, RTL8192SU),
128 RSU_DEV_HT(DLINK2, DWA131A1),
129 RSU_DEV_HT(DLINK2, RTL8192SU_1),
130 RSU_DEV_HT(DLINK2, RTL8192SU_2),
131 RSU_DEV_HT(EDIMAX, RTL8192SU_1),
132 RSU_DEV_HT(EDIMAX, RTL8192SU_2),
133 RSU_DEV_HT(EDIMAX, EW7622UMN),
134 RSU_DEV_HT(GUILLEMOT, HWGUN54),
135 RSU_DEV_HT(GUILLEMOT, HWNUM300),
136 RSU_DEV_HT(HAWKING, RTL8192SU_1),
137 RSU_DEV_HT(HAWKING, RTL8192SU_2),
138 RSU_DEV_HT(PLANEX2, GWUSNANO),
139 RSU_DEV_HT(REALTEK, RTL8171),
140 RSU_DEV_HT(REALTEK, RTL8172),
141 RSU_DEV_HT(REALTEK, RTL8173),
142 RSU_DEV_HT(REALTEK, RTL8174),
143 RSU_DEV_HT(REALTEK, RTL8192SU),
144 RSU_DEV_HT(REALTEK, RTL8712),
145 RSU_DEV_HT(REALTEK, RTL8713),
146 RSU_DEV_HT(SENAO, RTL8192SU_1),
147 RSU_DEV_HT(SENAO, RTL8192SU_2),
148 RSU_DEV_HT(SITECOMEU, WL349V1),
149 RSU_DEV_HT(SITECOMEU, WL353),
150 RSU_DEV_HT(SWEEX2, LW154),
151 RSU_DEV_HT(TRENDNET, TEW646UBH),
156 static device_probe_t rsu_match;
157 static device_attach_t rsu_attach;
158 static device_detach_t rsu_detach;
159 static usb_callback_t rsu_bulk_tx_callback_be_bk;
160 static usb_callback_t rsu_bulk_tx_callback_vi_vo;
161 static usb_callback_t rsu_bulk_tx_callback_h2c;
162 static usb_callback_t rsu_bulk_rx_callback;
163 static usb_error_t rsu_do_request(struct rsu_softc *,
164 struct usb_device_request *, void *);
165 static struct ieee80211vap *
166 rsu_vap_create(struct ieee80211com *, const char name[],
167 int, enum ieee80211_opmode, int, const uint8_t bssid[],
168 const uint8_t mac[]);
169 static void rsu_vap_delete(struct ieee80211vap *);
170 static void rsu_scan_start(struct ieee80211com *);
171 static void rsu_scan_end(struct ieee80211com *);
172 static void rsu_getradiocaps(struct ieee80211com *, int, int *,
173 struct ieee80211_channel[]);
174 static void rsu_set_channel(struct ieee80211com *);
175 static void rsu_scan_curchan(struct ieee80211_scan_state *, unsigned long);
176 static void rsu_scan_mindwell(struct ieee80211_scan_state *);
177 static void rsu_update_promisc(struct ieee80211com *);
178 static uint8_t rsu_get_multi_pos(const uint8_t[]);
179 static void rsu_set_multi(struct rsu_softc *);
180 static void rsu_update_mcast(struct ieee80211com *);
181 static int rsu_alloc_rx_list(struct rsu_softc *);
182 static void rsu_free_rx_list(struct rsu_softc *);
183 static int rsu_alloc_tx_list(struct rsu_softc *);
184 static void rsu_free_tx_list(struct rsu_softc *);
185 static void rsu_free_list(struct rsu_softc *, struct rsu_data [], int);
186 static struct rsu_data *_rsu_getbuf(struct rsu_softc *);
187 static struct rsu_data *rsu_getbuf(struct rsu_softc *);
188 static void rsu_freebuf(struct rsu_softc *, struct rsu_data *);
189 static int rsu_write_region_1(struct rsu_softc *, uint16_t, uint8_t *,
191 static void rsu_write_1(struct rsu_softc *, uint16_t, uint8_t);
192 static void rsu_write_2(struct rsu_softc *, uint16_t, uint16_t);
193 static void rsu_write_4(struct rsu_softc *, uint16_t, uint32_t);
194 static int rsu_read_region_1(struct rsu_softc *, uint16_t, uint8_t *,
196 static uint8_t rsu_read_1(struct rsu_softc *, uint16_t);
197 static uint16_t rsu_read_2(struct rsu_softc *, uint16_t);
198 static uint32_t rsu_read_4(struct rsu_softc *, uint16_t);
199 static int rsu_fw_iocmd(struct rsu_softc *, uint32_t);
200 static uint8_t rsu_efuse_read_1(struct rsu_softc *, uint16_t);
201 static int rsu_read_rom(struct rsu_softc *);
202 static int rsu_fw_cmd(struct rsu_softc *, uint8_t, void *, int);
203 static void rsu_calib_task(void *, int);
204 static void rsu_tx_task(void *, int);
205 static void rsu_set_led(struct rsu_softc *, int);
206 static int rsu_monitor_newstate(struct ieee80211vap *,
207 enum ieee80211_state, int);
208 static int rsu_newstate(struct ieee80211vap *, enum ieee80211_state, int);
209 static int rsu_key_alloc(struct ieee80211vap *, struct ieee80211_key *,
210 ieee80211_keyix *, ieee80211_keyix *);
211 static int rsu_process_key(struct ieee80211vap *,
212 const struct ieee80211_key *, int);
213 static int rsu_key_set(struct ieee80211vap *,
214 const struct ieee80211_key *);
215 static int rsu_key_delete(struct ieee80211vap *,
216 const struct ieee80211_key *);
217 static int rsu_cam_read(struct rsu_softc *, uint8_t, uint32_t *);
218 static void rsu_cam_write(struct rsu_softc *, uint8_t, uint32_t);
219 static int rsu_key_check(struct rsu_softc *, ieee80211_keyix, int);
220 static uint8_t rsu_crypto_mode(struct rsu_softc *, u_int, int);
221 static int rsu_set_key_group(struct rsu_softc *,
222 const struct ieee80211_key *);
223 static int rsu_set_key_pair(struct rsu_softc *,
224 const struct ieee80211_key *);
225 static int rsu_reinit_static_keys(struct rsu_softc *);
226 static int rsu_delete_key(struct rsu_softc *sc, ieee80211_keyix);
227 static void rsu_delete_key_pair_cb(void *, int);
228 static int rsu_site_survey(struct rsu_softc *,
229 struct ieee80211_scan_ssid *);
230 static int rsu_join_bss(struct rsu_softc *, struct ieee80211_node *);
231 static int rsu_disconnect(struct rsu_softc *);
232 static int rsu_hwrssi_to_rssi(struct rsu_softc *, int hw_rssi);
233 static void rsu_event_survey(struct rsu_softc *, uint8_t *, int);
234 static void rsu_event_join_bss(struct rsu_softc *, uint8_t *, int);
235 static void rsu_rx_event(struct rsu_softc *, uint8_t, uint8_t *, int);
236 static void rsu_rx_multi_event(struct rsu_softc *, uint8_t *, int);
237 static int8_t rsu_get_rssi(struct rsu_softc *, int, void *);
238 static struct mbuf * rsu_rx_copy_to_mbuf(struct rsu_softc *,
239 struct r92s_rx_stat *, int);
240 static uint32_t rsu_get_tsf_low(struct rsu_softc *);
241 static uint32_t rsu_get_tsf_high(struct rsu_softc *);
242 static struct ieee80211_node * rsu_rx_frame(struct rsu_softc *, struct mbuf *);
243 static struct mbuf * rsu_rx_multi_frame(struct rsu_softc *, uint8_t *, int);
245 rsu_rxeof(struct usb_xfer *, struct rsu_data *);
246 static void rsu_txeof(struct usb_xfer *, struct rsu_data *);
247 static int rsu_raw_xmit(struct ieee80211_node *, struct mbuf *,
248 const struct ieee80211_bpf_params *);
249 static void rsu_rxfilter_init(struct rsu_softc *);
250 static void rsu_rxfilter_set(struct rsu_softc *, uint32_t, uint32_t);
251 static void rsu_rxfilter_refresh(struct rsu_softc *);
252 static int rsu_init(struct rsu_softc *);
253 static int rsu_tx_start(struct rsu_softc *, struct ieee80211_node *,
254 struct mbuf *, struct rsu_data *);
255 static int rsu_transmit(struct ieee80211com *, struct mbuf *);
256 static void rsu_start(struct rsu_softc *);
257 static void _rsu_start(struct rsu_softc *);
258 static int rsu_ioctl_net(struct ieee80211com *, u_long, void *);
259 static void rsu_parent(struct ieee80211com *);
260 static void rsu_stop(struct rsu_softc *);
261 static void rsu_ms_delay(struct rsu_softc *, int);
263 static device_method_t rsu_methods[] = {
264 DEVMETHOD(device_probe, rsu_match),
265 DEVMETHOD(device_attach, rsu_attach),
266 DEVMETHOD(device_detach, rsu_detach),
271 static driver_t rsu_driver = {
273 .methods = rsu_methods,
274 .size = sizeof(struct rsu_softc)
277 static devclass_t rsu_devclass;
279 DRIVER_MODULE(rsu, uhub, rsu_driver, rsu_devclass, NULL, 0);
280 MODULE_DEPEND(rsu, wlan, 1, 1, 1);
281 MODULE_DEPEND(rsu, usb, 1, 1, 1);
282 MODULE_DEPEND(rsu, firmware, 1, 1, 1);
283 MODULE_VERSION(rsu, 1);
284 USB_PNP_HOST_INFO(rsu_devs);
286 static uint8_t rsu_wme_ac_xfer_map[4] = {
287 [WME_AC_BE] = RSU_BULK_TX_BE_BK,
288 [WME_AC_BK] = RSU_BULK_TX_BE_BK,
289 [WME_AC_VI] = RSU_BULK_TX_VI_VO,
290 [WME_AC_VO] = RSU_BULK_TX_VI_VO,
294 #define RSU_H2C_ENDPOINT 3
296 static const struct usb_config rsu_config[RSU_N_TRANSFER] = {
299 .endpoint = UE_ADDR_ANY,
300 .direction = UE_DIR_IN,
301 .bufsize = RSU_RXBUFSZ,
306 .callback = rsu_bulk_rx_callback
308 [RSU_BULK_TX_BE_BK] = {
311 .direction = UE_DIR_OUT,
312 .bufsize = RSU_TXBUFSZ,
316 .force_short_xfer = 1
318 .callback = rsu_bulk_tx_callback_be_bk,
319 .timeout = RSU_TX_TIMEOUT
321 [RSU_BULK_TX_VI_VO] = {
324 .direction = UE_DIR_OUT,
325 .bufsize = RSU_TXBUFSZ,
329 .force_short_xfer = 1
331 .callback = rsu_bulk_tx_callback_vi_vo,
332 .timeout = RSU_TX_TIMEOUT
334 [RSU_BULK_TX_H2C] = {
337 .direction = UE_DIR_OUT,
338 .bufsize = RSU_TXBUFSZ,
344 .callback = rsu_bulk_tx_callback_h2c,
345 .timeout = RSU_TX_TIMEOUT
350 rsu_match(device_t self)
352 struct usb_attach_arg *uaa = device_get_ivars(self);
354 if (uaa->usb_mode != USB_MODE_HOST ||
355 uaa->info.bIfaceIndex != 0 ||
356 uaa->info.bConfigIndex != 0)
359 return (usbd_lookup_id_by_uaa(rsu_devs, sizeof(rsu_devs), uaa));
363 rsu_send_mgmt(struct ieee80211_node *ni, int type, int arg)
370 rsu_update_chw(struct ieee80211com *ic)
376 * notification from net80211 that it'd like to do A-MPDU on the given TID.
378 * Note: this actually hangs traffic at the present moment, so don't use it.
379 * The firmware debug does indiciate it's sending and establishing a TX AMPDU
380 * session, but then no traffic flows.
383 rsu_ampdu_enable(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
386 struct rsu_softc *sc = ni->ni_ic->ic_softc;
387 struct r92s_add_ba_req req;
389 /* Don't enable if it's requested or running */
390 if (IEEE80211_AMPDU_REQUESTED(tap))
392 if (IEEE80211_AMPDU_RUNNING(tap))
395 /* We've decided to send addba; so send it */
396 req.tid = htole32(tap->txa_tid);
398 /* Attempt net80211 state */
399 if (ieee80211_ampdu_tx_request_ext(ni, tap->txa_tid) != 1)
402 /* Send the firmware command */
403 RSU_DPRINTF(sc, RSU_DEBUG_AMPDU, "%s: establishing AMPDU TX for TID %d\n",
408 if (rsu_fw_cmd(sc, R92S_CMD_ADDBA_REQ, &req, sizeof(req)) != 1) {
411 (void) ieee80211_ampdu_tx_request_active_ext(ni, tap->txa_tid, 0);
416 /* Mark success; we don't get any further notifications */
417 (void) ieee80211_ampdu_tx_request_active_ext(ni, tap->txa_tid, 1);
419 /* Return 0, we're driving this ourselves */
424 rsu_wme_update(struct ieee80211com *ic)
427 /* Firmware handles this; not our problem */
432 rsu_attach(device_t self)
434 struct usb_attach_arg *uaa = device_get_ivars(self);
435 struct rsu_softc *sc = device_get_softc(self);
436 struct ieee80211com *ic = &sc->sc_ic;
439 struct usb_interface *iface;
442 device_set_usb_desc(self);
443 sc->sc_udev = uaa->device;
445 sc->sc_rx_checksum_enable = 1;
447 sc->sc_ht = !! (USB_GET_DRIVER_INFO(uaa) & RSU_HT_SUPPORTED);
449 /* Get number of endpoints */
450 iface = usbd_get_iface(sc->sc_udev, 0);
451 sc->sc_nendpoints = iface->idesc->bNumEndpoints;
453 /* Endpoints are hard-coded for now, so enforce 4-endpoint only */
454 if (sc->sc_nendpoints != 4) {
455 device_printf(sc->sc_dev,
456 "the driver currently only supports 4-endpoint devices\n");
460 mtx_init(&sc->sc_mtx, device_get_nameunit(self), MTX_NETWORK_LOCK,
462 RSU_DELKEY_BMAP_LOCK_INIT(sc);
463 TIMEOUT_TASK_INIT(taskqueue_thread, &sc->calib_task, 0,
465 TASK_INIT(&sc->del_key_task, 0, rsu_delete_key_pair_cb, sc);
466 TASK_INIT(&sc->tx_task, 0, rsu_tx_task, sc);
467 mbufq_init(&sc->sc_snd, ifqmaxlen);
469 /* Allocate Tx/Rx buffers. */
470 error = rsu_alloc_rx_list(sc);
472 device_printf(sc->sc_dev, "could not allocate Rx buffers\n");
476 error = rsu_alloc_tx_list(sc);
478 device_printf(sc->sc_dev, "could not allocate Tx buffers\n");
479 rsu_free_rx_list(sc);
484 error = usbd_transfer_setup(uaa->device, &iface_index, sc->sc_xfer,
485 rsu_config, RSU_N_TRANSFER, sc, &sc->sc_mtx);
487 device_printf(sc->sc_dev,
488 "could not allocate USB transfers, err=%s\n",
493 /* Read chip revision. */
494 sc->cut = MS(rsu_read_4(sc, R92S_PMC_FSM), R92S_PMC_FSM_CUT);
496 sc->cut = (sc->cut >> 1) + 1;
497 error = rsu_read_rom(sc);
500 device_printf(self, "could not read ROM\n");
504 /* Figure out TX/RX streams */
505 switch (sc->rom[84]) {
507 sc->sc_rftype = RTL8712_RFCONFIG_1T1R;
508 sc->sc_nrxstream = 1;
509 sc->sc_ntxstream = 1;
513 sc->sc_rftype = RTL8712_RFCONFIG_1T2R;
514 sc->sc_nrxstream = 2;
515 sc->sc_ntxstream = 1;
519 sc->sc_rftype = RTL8712_RFCONFIG_2T2R;
520 sc->sc_nrxstream = 2;
521 sc->sc_ntxstream = 2;
524 case 0x3: /* "green" NIC */
525 sc->sc_rftype = RTL8712_RFCONFIG_1T2R;
526 sc->sc_nrxstream = 2;
527 sc->sc_ntxstream = 1;
528 rft = "1T2R ('green')";
531 device_printf(sc->sc_dev,
532 "%s: unknown board type (rfconfig=0x%02x)\n",
538 IEEE80211_ADDR_COPY(ic->ic_macaddr, &sc->rom[0x12]);
539 device_printf(self, "MAC/BB RTL8712 cut %d %s\n", sc->cut, rft);
542 ic->ic_name = device_get_nameunit(self);
543 ic->ic_phytype = IEEE80211_T_OFDM; /* Not only, but not used. */
544 ic->ic_opmode = IEEE80211_M_STA; /* Default to BSS mode. */
546 /* Set device capabilities. */
548 IEEE80211_C_STA | /* station mode */
549 IEEE80211_C_MONITOR | /* monitor mode supported */
551 IEEE80211_C_BGSCAN | /* Background scan. */
553 IEEE80211_C_SHPREAMBLE | /* Short preamble supported. */
554 IEEE80211_C_WME | /* WME/QoS */
555 IEEE80211_C_SHSLOT | /* Short slot time supported. */
556 IEEE80211_C_WPA; /* WPA/RSN. */
559 IEEE80211_CRYPTO_WEP |
560 IEEE80211_CRYPTO_TKIP |
561 IEEE80211_CRYPTO_AES_CCM;
563 /* Check if HT support is present. */
565 device_printf(sc->sc_dev, "%s: enabling 11n\n", __func__);
567 /* Enable basic HT */
568 ic->ic_htcaps = IEEE80211_HTC_HT |
570 IEEE80211_HTC_AMPDU |
572 IEEE80211_HTC_AMSDU |
573 IEEE80211_HTCAP_MAXAMSDU_3839 |
574 IEEE80211_HTCAP_SMPS_OFF;
575 ic->ic_htcaps |= IEEE80211_HTCAP_CHWIDTH40;
577 /* set number of spatial streams */
578 ic->ic_txstream = sc->sc_ntxstream;
579 ic->ic_rxstream = sc->sc_nrxstream;
581 ic->ic_flags_ext |= IEEE80211_FEXT_SCAN_OFFLOAD;
583 rsu_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
586 ieee80211_ifattach(ic);
587 ic->ic_raw_xmit = rsu_raw_xmit;
588 ic->ic_scan_start = rsu_scan_start;
589 ic->ic_scan_end = rsu_scan_end;
590 ic->ic_getradiocaps = rsu_getradiocaps;
591 ic->ic_set_channel = rsu_set_channel;
592 ic->ic_scan_curchan = rsu_scan_curchan;
593 ic->ic_scan_mindwell = rsu_scan_mindwell;
594 ic->ic_vap_create = rsu_vap_create;
595 ic->ic_vap_delete = rsu_vap_delete;
596 ic->ic_update_promisc = rsu_update_promisc;
597 ic->ic_update_mcast = rsu_update_mcast;
598 ic->ic_ioctl = rsu_ioctl_net;
599 ic->ic_parent = rsu_parent;
600 ic->ic_transmit = rsu_transmit;
601 ic->ic_send_mgmt = rsu_send_mgmt;
602 ic->ic_update_chw = rsu_update_chw;
603 ic->ic_ampdu_enable = rsu_ampdu_enable;
604 ic->ic_wme.wme_update = rsu_wme_update;
606 ieee80211_radiotap_attach(ic, &sc->sc_txtap.wt_ihdr,
607 sizeof(sc->sc_txtap), RSU_TX_RADIOTAP_PRESENT,
608 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
609 RSU_RX_RADIOTAP_PRESENT);
612 ieee80211_announce(ic);
617 usbd_transfer_unsetup(sc->sc_xfer, RSU_N_TRANSFER);
619 mtx_destroy(&sc->sc_mtx);
624 rsu_detach(device_t self)
626 struct rsu_softc *sc = device_get_softc(self);
627 struct ieee80211com *ic = &sc->sc_ic;
631 usbd_transfer_unsetup(sc->sc_xfer, RSU_N_TRANSFER);
634 * Free buffers /before/ we detach from net80211, else node
635 * references to destroyed vaps will lead to a panic.
637 /* Free Tx/Rx buffers. */
639 rsu_free_tx_list(sc);
640 rsu_free_rx_list(sc);
643 /* Frames are freed; detach from net80211 */
644 ieee80211_ifdetach(ic);
646 taskqueue_drain_timeout(taskqueue_thread, &sc->calib_task);
647 taskqueue_drain(taskqueue_thread, &sc->del_key_task);
648 taskqueue_drain(taskqueue_thread, &sc->tx_task);
650 RSU_DELKEY_BMAP_LOCK_DESTROY(sc);
651 mtx_destroy(&sc->sc_mtx);
657 rsu_do_request(struct rsu_softc *sc, struct usb_device_request *req,
663 RSU_ASSERT_LOCKED(sc);
666 err = usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
667 req, data, 0, NULL, 250 /* ms */);
668 if (err == 0 || err == USB_ERR_NOT_CONFIGURED)
670 RSU_DPRINTF(sc, RSU_DEBUG_USB,
671 "Control request failed, %s (retries left: %d)\n",
672 usbd_errstr(err), ntries);
673 rsu_ms_delay(sc, 10);
679 static struct ieee80211vap *
680 rsu_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
681 enum ieee80211_opmode opmode, int flags,
682 const uint8_t bssid[IEEE80211_ADDR_LEN],
683 const uint8_t mac[IEEE80211_ADDR_LEN])
685 struct rsu_softc *sc = ic->ic_softc;
687 struct ieee80211vap *vap;
690 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */
693 uvp = malloc(sizeof(struct rsu_vap), M_80211_VAP, M_WAITOK | M_ZERO);
696 if (ieee80211_vap_setup(ic, vap, name, unit, opmode,
697 flags, bssid) != 0) {
699 free(uvp, M_80211_VAP);
704 ifp->if_capabilities = IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6;
706 if (sc->sc_rx_checksum_enable)
707 ifp->if_capenable |= IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6;
710 /* override state transition machine */
711 uvp->newstate = vap->iv_newstate;
712 if (opmode == IEEE80211_M_MONITOR)
713 vap->iv_newstate = rsu_monitor_newstate;
715 vap->iv_newstate = rsu_newstate;
716 vap->iv_key_alloc = rsu_key_alloc;
717 vap->iv_key_set = rsu_key_set;
718 vap->iv_key_delete = rsu_key_delete;
720 /* Limits from the r92su driver */
721 vap->iv_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_16;
722 vap->iv_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_32K;
725 ieee80211_vap_attach(vap, ieee80211_media_change,
726 ieee80211_media_status, mac);
727 ic->ic_opmode = opmode;
733 rsu_vap_delete(struct ieee80211vap *vap)
735 struct rsu_vap *uvp = RSU_VAP(vap);
737 ieee80211_vap_detach(vap);
738 free(uvp, M_80211_VAP);
742 rsu_scan_start(struct ieee80211com *ic)
744 struct rsu_softc *sc = ic->ic_softc;
745 struct ieee80211_scan_state *ss = ic->ic_scan;
746 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
749 /* Scanning is done by the firmware. */
751 sc->sc_active_scan = !!(ss->ss_flags & IEEE80211_SCAN_ACTIVE);
752 /* XXX TODO: force awake if in network-sleep? */
753 error = rsu_site_survey(sc, ss->ss_nssid > 0 ? &ss->ss_ssid[0] : NULL);
756 device_printf(sc->sc_dev,
757 "could not send site survey command\n");
758 ieee80211_cancel_scan(vap);
763 rsu_scan_end(struct ieee80211com *ic)
765 /* Nothing to do here. */
769 rsu_getradiocaps(struct ieee80211com *ic,
770 int maxchans, int *nchans, struct ieee80211_channel chans[])
772 struct rsu_softc *sc = ic->ic_softc;
773 uint8_t bands[IEEE80211_MODE_BYTES];
775 /* Set supported .11b and .11g rates. */
776 memset(bands, 0, sizeof(bands));
777 setbit(bands, IEEE80211_MODE_11B);
778 setbit(bands, IEEE80211_MODE_11G);
780 setbit(bands, IEEE80211_MODE_11NG);
781 ieee80211_add_channels_default_2ghz(chans, maxchans, nchans,
782 bands, (ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) ?
783 NET80211_CBW_FLAG_HT40 : 0);
787 rsu_set_channel(struct ieee80211com *ic)
789 struct rsu_softc *sc = ic->ic_softc;
792 * Only need to set the channel in Monitor mode. AP scanning and auth
793 * are already taken care of by their respective firmware commands.
795 if (ic->ic_opmode == IEEE80211_M_MONITOR) {
796 struct r92s_set_channel cmd;
799 cmd.channel = IEEE80211_CHAN2IEEE(ic->ic_curchan);
802 error = rsu_fw_cmd(sc, R92S_CMD_SET_CHANNEL, &cmd,
805 device_printf(sc->sc_dev,
806 "%s: error %d setting channel\n", __func__,
814 rsu_scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell)
816 /* Scan is done in rsu_scan_start(). */
820 * Called by the net80211 framework to indicate
821 * the minimum dwell time has been met, terminate the scan.
822 * We don't actually terminate the scan as the firmware will notify
823 * us when it's finished and we have no way to interrupt it.
826 rsu_scan_mindwell(struct ieee80211_scan_state *ss)
828 /* NB: don't try to abort scan; wait for firmware to finish */
832 rsu_update_promisc(struct ieee80211com *ic)
834 struct rsu_softc *sc = ic->ic_softc;
838 rsu_rxfilter_refresh(sc);
843 * The same as rtwn_get_multi_pos() / rtwn_set_multi().
846 rsu_get_multi_pos(const uint8_t maddr[])
848 uint64_t mask = 0x00004d101df481b4;
849 uint8_t pos = 0x27; /* initial value */
852 for (i = 0; i < IEEE80211_ADDR_LEN; i++)
853 for (j = (i == 0) ? 1 : 0; j < 8; j++)
854 if ((maddr[i] >> j) & 1)
855 pos ^= (mask >> (i * 8 + j - 1));
863 rsu_hash_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
865 uint32_t *mfilt = arg;
868 pos = rsu_get_multi_pos(LLADDR(sdl));
869 mfilt[pos / 32] |= (1 << (pos % 32));
875 rsu_set_multi(struct rsu_softc *sc)
877 struct ieee80211com *ic = &sc->sc_ic;
880 RSU_ASSERT_LOCKED(sc);
882 /* general structure was copied from ath(4). */
883 if (ic->ic_allmulti == 0) {
884 struct ieee80211vap *vap;
887 * Merge multicast addresses to form the hardware filter.
889 mfilt[0] = mfilt[1] = 0;
890 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
891 if_foreach_llmaddr(vap->iv_ifp, rsu_hash_maddr, &mfilt);
893 mfilt[0] = mfilt[1] = ~0;
895 rsu_write_4(sc, R92S_MAR + 0, mfilt[0]);
896 rsu_write_4(sc, R92S_MAR + 4, mfilt[1]);
898 RSU_DPRINTF(sc, RSU_DEBUG_STATE, "%s: MC filter %08x:%08x\n",
899 __func__, mfilt[0], mfilt[1]);
903 rsu_update_mcast(struct ieee80211com *ic)
905 struct rsu_softc *sc = ic->ic_softc;
914 rsu_alloc_list(struct rsu_softc *sc, struct rsu_data data[],
915 int ndata, int maxsz)
919 for (i = 0; i < ndata; i++) {
920 struct rsu_data *dp = &data[i];
923 dp->buf = malloc(maxsz, M_USBDEV, M_NOWAIT);
924 if (dp->buf == NULL) {
925 device_printf(sc->sc_dev,
926 "could not allocate buffer\n");
935 rsu_free_list(sc, data, ndata);
940 rsu_alloc_rx_list(struct rsu_softc *sc)
944 error = rsu_alloc_list(sc, sc->sc_rx, RSU_RX_LIST_COUNT,
949 STAILQ_INIT(&sc->sc_rx_active);
950 STAILQ_INIT(&sc->sc_rx_inactive);
952 for (i = 0; i < RSU_RX_LIST_COUNT; i++)
953 STAILQ_INSERT_HEAD(&sc->sc_rx_inactive, &sc->sc_rx[i], next);
959 rsu_alloc_tx_list(struct rsu_softc *sc)
963 error = rsu_alloc_list(sc, sc->sc_tx, RSU_TX_LIST_COUNT,
968 STAILQ_INIT(&sc->sc_tx_inactive);
970 for (i = 0; i != RSU_N_TRANSFER; i++) {
971 STAILQ_INIT(&sc->sc_tx_active[i]);
972 STAILQ_INIT(&sc->sc_tx_pending[i]);
975 for (i = 0; i < RSU_TX_LIST_COUNT; i++) {
976 STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, &sc->sc_tx[i], next);
983 rsu_free_tx_list(struct rsu_softc *sc)
987 /* prevent further allocations from TX list(s) */
988 STAILQ_INIT(&sc->sc_tx_inactive);
990 for (i = 0; i != RSU_N_TRANSFER; i++) {
991 STAILQ_INIT(&sc->sc_tx_active[i]);
992 STAILQ_INIT(&sc->sc_tx_pending[i]);
995 rsu_free_list(sc, sc->sc_tx, RSU_TX_LIST_COUNT);
999 rsu_free_rx_list(struct rsu_softc *sc)
1001 /* prevent further allocations from RX list(s) */
1002 STAILQ_INIT(&sc->sc_rx_inactive);
1003 STAILQ_INIT(&sc->sc_rx_active);
1005 rsu_free_list(sc, sc->sc_rx, RSU_RX_LIST_COUNT);
1009 rsu_free_list(struct rsu_softc *sc, struct rsu_data data[], int ndata)
1013 for (i = 0; i < ndata; i++) {
1014 struct rsu_data *dp = &data[i];
1016 if (dp->buf != NULL) {
1017 free(dp->buf, M_USBDEV);
1020 if (dp->ni != NULL) {
1021 ieee80211_free_node(dp->ni);
1027 static struct rsu_data *
1028 _rsu_getbuf(struct rsu_softc *sc)
1030 struct rsu_data *bf;
1032 bf = STAILQ_FIRST(&sc->sc_tx_inactive);
1034 STAILQ_REMOVE_HEAD(&sc->sc_tx_inactive, next);
1040 static struct rsu_data *
1041 rsu_getbuf(struct rsu_softc *sc)
1043 struct rsu_data *bf;
1045 RSU_ASSERT_LOCKED(sc);
1047 bf = _rsu_getbuf(sc);
1049 RSU_DPRINTF(sc, RSU_DEBUG_TX, "%s: no buffers\n", __func__);
1055 rsu_freebuf(struct rsu_softc *sc, struct rsu_data *bf)
1058 RSU_ASSERT_LOCKED(sc);
1059 STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, bf, next);
1063 rsu_write_region_1(struct rsu_softc *sc, uint16_t addr, uint8_t *buf,
1066 usb_device_request_t req;
1068 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1069 req.bRequest = R92S_REQ_REGS;
1070 USETW(req.wValue, addr);
1071 USETW(req.wIndex, 0);
1072 USETW(req.wLength, len);
1074 return (rsu_do_request(sc, &req, buf));
1078 rsu_write_1(struct rsu_softc *sc, uint16_t addr, uint8_t val)
1080 rsu_write_region_1(sc, addr, &val, 1);
1084 rsu_write_2(struct rsu_softc *sc, uint16_t addr, uint16_t val)
1087 rsu_write_region_1(sc, addr, (uint8_t *)&val, 2);
1091 rsu_write_4(struct rsu_softc *sc, uint16_t addr, uint32_t val)
1094 rsu_write_region_1(sc, addr, (uint8_t *)&val, 4);
1098 rsu_read_region_1(struct rsu_softc *sc, uint16_t addr, uint8_t *buf,
1101 usb_device_request_t req;
1103 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1104 req.bRequest = R92S_REQ_REGS;
1105 USETW(req.wValue, addr);
1106 USETW(req.wIndex, 0);
1107 USETW(req.wLength, len);
1109 return (rsu_do_request(sc, &req, buf));
1113 rsu_read_1(struct rsu_softc *sc, uint16_t addr)
1117 if (rsu_read_region_1(sc, addr, &val, 1) != 0)
1123 rsu_read_2(struct rsu_softc *sc, uint16_t addr)
1127 if (rsu_read_region_1(sc, addr, (uint8_t *)&val, 2) != 0)
1129 return (le16toh(val));
1133 rsu_read_4(struct rsu_softc *sc, uint16_t addr)
1137 if (rsu_read_region_1(sc, addr, (uint8_t *)&val, 4) != 0)
1138 return (0xffffffff);
1139 return (le32toh(val));
1143 rsu_fw_iocmd(struct rsu_softc *sc, uint32_t iocmd)
1147 rsu_write_4(sc, R92S_IOCMD_CTRL, iocmd);
1148 rsu_ms_delay(sc, 1);
1149 for (ntries = 0; ntries < 50; ntries++) {
1150 if (rsu_read_4(sc, R92S_IOCMD_CTRL) == 0)
1152 rsu_ms_delay(sc, 1);
1158 rsu_efuse_read_1(struct rsu_softc *sc, uint16_t addr)
1163 reg = rsu_read_4(sc, R92S_EFUSE_CTRL);
1164 reg = RW(reg, R92S_EFUSE_CTRL_ADDR, addr);
1165 reg &= ~R92S_EFUSE_CTRL_VALID;
1166 rsu_write_4(sc, R92S_EFUSE_CTRL, reg);
1167 /* Wait for read operation to complete. */
1168 for (ntries = 0; ntries < 100; ntries++) {
1169 reg = rsu_read_4(sc, R92S_EFUSE_CTRL);
1170 if (reg & R92S_EFUSE_CTRL_VALID)
1171 return (MS(reg, R92S_EFUSE_CTRL_DATA));
1172 rsu_ms_delay(sc, 1);
1174 device_printf(sc->sc_dev,
1175 "could not read efuse byte at address 0x%x\n", addr);
1180 rsu_read_rom(struct rsu_softc *sc)
1182 uint8_t *rom = sc->rom;
1188 /* Make sure that ROM type is eFuse and that autoload succeeded. */
1189 reg = rsu_read_1(sc, R92S_EE_9346CR);
1190 if ((reg & (R92S_9356SEL | R92S_EEPROM_EN)) != R92S_EEPROM_EN)
1193 /* Turn on 2.5V to prevent eFuse leakage. */
1194 reg = rsu_read_1(sc, R92S_EFUSE_TEST + 3);
1195 rsu_write_1(sc, R92S_EFUSE_TEST + 3, reg | 0x80);
1196 rsu_ms_delay(sc, 1);
1197 rsu_write_1(sc, R92S_EFUSE_TEST + 3, reg & ~0x80);
1199 /* Read full ROM image. */
1200 memset(&sc->rom, 0xff, sizeof(sc->rom));
1201 while (addr < 512) {
1202 reg = rsu_efuse_read_1(sc, addr);
1208 for (i = 0; i < 4; i++) {
1211 rom[off * 8 + i * 2 + 0] =
1212 rsu_efuse_read_1(sc, addr);
1214 rom[off * 8 + i * 2 + 1] =
1215 rsu_efuse_read_1(sc, addr);
1220 if (rsu_debug & RSU_DEBUG_RESET) {
1221 /* Dump ROM content. */
1223 for (i = 0; i < sizeof(sc->rom); i++)
1224 printf("%02x:", rom[i]);
1232 rsu_fw_cmd(struct rsu_softc *sc, uint8_t code, void *buf, int len)
1234 const uint8_t which = RSU_H2C_ENDPOINT;
1235 struct rsu_data *data;
1236 struct r92s_tx_desc *txd;
1237 struct r92s_fw_cmd_hdr *cmd;
1241 RSU_ASSERT_LOCKED(sc);
1243 data = rsu_getbuf(sc);
1247 /* Blank the entire payload, just to be safe */
1248 memset(data->buf, '\0', RSU_TXBUFSZ);
1250 /* Round-up command length to a multiple of 8 bytes. */
1251 /* XXX TODO: is this required? */
1252 cmdsz = (len + 7) & ~7;
1254 xferlen = sizeof(*txd) + sizeof(*cmd) + cmdsz;
1255 KASSERT(xferlen <= RSU_TXBUFSZ, ("%s: invalid length", __func__));
1256 memset(data->buf, 0, xferlen);
1258 /* Setup Tx descriptor. */
1259 txd = (struct r92s_tx_desc *)data->buf;
1260 txd->txdw0 = htole32(
1261 SM(R92S_TXDW0_OFFSET, sizeof(*txd)) |
1262 SM(R92S_TXDW0_PKTLEN, sizeof(*cmd) + cmdsz) |
1263 R92S_TXDW0_OWN | R92S_TXDW0_FSG | R92S_TXDW0_LSG);
1264 txd->txdw1 = htole32(SM(R92S_TXDW1_QSEL, R92S_TXDW1_QSEL_H2C));
1266 /* Setup command header. */
1267 cmd = (struct r92s_fw_cmd_hdr *)&txd[1];
1268 cmd->len = htole16(cmdsz);
1270 cmd->seq = sc->cmd_seq;
1271 sc->cmd_seq = (sc->cmd_seq + 1) & 0x7f;
1273 /* Copy command payload. */
1274 memcpy(&cmd[1], buf, len);
1276 RSU_DPRINTF(sc, RSU_DEBUG_TX | RSU_DEBUG_FWCMD,
1277 "%s: Tx cmd code=0x%x len=0x%x\n",
1278 __func__, code, cmdsz);
1279 data->buflen = xferlen;
1280 STAILQ_INSERT_TAIL(&sc->sc_tx_pending[which], data, next);
1281 usbd_transfer_start(sc->sc_xfer[which]);
1288 rsu_calib_task(void *arg, int pending __unused)
1290 struct rsu_softc *sc = arg;
1295 RSU_DPRINTF(sc, RSU_DEBUG_CALIB, "%s: running calibration task\n",
1300 /* Read WPS PBC status. */
1301 rsu_write_1(sc, R92S_MAC_PINMUX_CTRL,
1302 R92S_GPIOMUX_EN | SM(R92S_GPIOSEL_GPIO, R92S_GPIOSEL_GPIO_JTAG));
1303 rsu_write_1(sc, R92S_GPIO_IO_SEL,
1304 rsu_read_1(sc, R92S_GPIO_IO_SEL) & ~R92S_GPIO_WPS);
1305 reg = rsu_read_1(sc, R92S_GPIO_CTRL);
1306 if (reg != 0xff && (reg & R92S_GPIO_WPS))
1307 RSU_DPRINTF(sc, RSU_DEBUG_CALIB, "WPS PBC is pushed\n");
1309 /* Read current signal level. */
1310 if (rsu_fw_iocmd(sc, 0xf4000001) == 0) {
1311 sc->sc_currssi = rsu_read_4(sc, R92S_IOCMD_DATA);
1312 RSU_DPRINTF(sc, RSU_DEBUG_CALIB, "%s: RSSI=%d (%d)\n",
1313 __func__, sc->sc_currssi,
1314 rsu_hwrssi_to_rssi(sc, sc->sc_currssi));
1316 if (sc->sc_calibrating)
1317 taskqueue_enqueue_timeout(taskqueue_thread, &sc->calib_task, hz);
1322 rsu_tx_task(void *arg, int pending __unused)
1324 struct rsu_softc *sc = arg;
1331 #define RSU_PWR_UNKNOWN 0x0
1332 #define RSU_PWR_ACTIVE 0x1
1333 #define RSU_PWR_OFF 0x2
1334 #define RSU_PWR_SLEEP 0x3
1337 * Set the current power state.
1339 * The rtlwifi code doesn't do this so aggressively; it
1340 * waits for an idle period after association with
1341 * no traffic before doing this.
1343 * For now - it's on in all states except RUN, and
1344 * in RUN it'll transition to allow sleep.
1347 struct r92s_pwr_cmd {
1350 uint8_t bcn_pass_time;
1354 rsu_set_fw_power_state(struct rsu_softc *sc, int state)
1356 struct r92s_set_pwr_mode cmd;
1357 //struct r92s_pwr_cmd cmd;
1360 RSU_ASSERT_LOCKED(sc);
1362 /* only change state if required */
1363 if (sc->sc_curpwrstate == state)
1366 memset(&cmd, 0, sizeof(cmd));
1369 case RSU_PWR_ACTIVE:
1370 /* Force the hardware awake */
1371 rsu_write_1(sc, R92S_USB_HRPWM,
1372 R92S_USB_HRPWM_PS_ST_ACTIVE | R92S_USB_HRPWM_PS_ALL_ON);
1373 cmd.mode = R92S_PS_MODE_ACTIVE;
1376 cmd.mode = R92S_PS_MODE_DTIM; /* XXX configurable? */
1377 cmd.smart_ps = 1; /* XXX 2 if doing p2p */
1378 cmd.bcn_pass_time = 5; /* in 100mS usb.c, linux/rtlwifi */
1381 cmd.mode = R92S_PS_MODE_RADIOOFF;
1384 device_printf(sc->sc_dev, "%s: unknown ps mode (%d)\n",
1390 RSU_DPRINTF(sc, RSU_DEBUG_RESET,
1391 "%s: setting ps mode to %d (mode %d)\n",
1392 __func__, state, cmd.mode);
1393 error = rsu_fw_cmd(sc, R92S_CMD_SET_PWR_MODE, &cmd, sizeof(cmd));
1395 sc->sc_curpwrstate = state;
1401 rsu_set_led(struct rsu_softc *sc, int on)
1403 rsu_write_1(sc, R92S_LEDCFG,
1404 (rsu_read_1(sc, R92S_LEDCFG) & 0xf0) | (!on << 3));
1408 rsu_monitor_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate,
1411 struct ieee80211com *ic = vap->iv_ic;
1412 struct rsu_softc *sc = ic->ic_softc;
1413 struct rsu_vap *uvp = RSU_VAP(vap);
1415 if (vap->iv_state != nstate) {
1416 IEEE80211_UNLOCK(ic);
1420 case IEEE80211_S_INIT:
1421 sc->sc_vap_is_running = 0;
1424 case IEEE80211_S_RUN:
1425 sc->sc_vap_is_running = 1;
1432 rsu_rxfilter_refresh(sc);
1438 return (uvp->newstate(vap, nstate, arg));
1442 rsu_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1444 struct rsu_vap *uvp = RSU_VAP(vap);
1445 struct ieee80211com *ic = vap->iv_ic;
1446 struct rsu_softc *sc = ic->ic_softc;
1447 struct ieee80211_node *ni;
1448 struct ieee80211_rateset *rs;
1449 enum ieee80211_state ostate;
1450 int error, startcal = 0;
1452 ostate = vap->iv_state;
1453 RSU_DPRINTF(sc, RSU_DEBUG_STATE, "%s: %s -> %s\n",
1455 ieee80211_state_name[ostate],
1456 ieee80211_state_name[nstate]);
1458 IEEE80211_UNLOCK(ic);
1459 if (ostate == IEEE80211_S_RUN) {
1461 /* Stop calibration. */
1462 sc->sc_calibrating = 0;
1464 /* Pause Tx for AC queues. */
1465 rsu_write_1(sc, R92S_TXPAUSE, R92S_TXPAUSE_AC);
1466 usb_pause_mtx(&sc->sc_mtx, USB_MS_TO_TICKS(10));
1469 taskqueue_drain_timeout(taskqueue_thread, &sc->calib_task);
1470 taskqueue_drain(taskqueue_thread, &sc->tx_task);
1472 /* Disassociate from our current BSS. */
1474 usb_pause_mtx(&sc->sc_mtx, USB_MS_TO_TICKS(10));
1476 /* Refresh Rx filter (may be modified by firmware). */
1477 sc->sc_vap_is_running = 0;
1478 rsu_rxfilter_refresh(sc);
1480 /* Reinstall static keys. */
1482 rsu_reinit_static_keys(sc);
1486 case IEEE80211_S_INIT:
1487 (void) rsu_set_fw_power_state(sc, RSU_PWR_ACTIVE);
1489 case IEEE80211_S_AUTH:
1490 ni = ieee80211_ref_node(vap->iv_bss);
1491 (void) rsu_set_fw_power_state(sc, RSU_PWR_ACTIVE);
1492 error = rsu_join_bss(sc, ni);
1493 ieee80211_free_node(ni);
1495 device_printf(sc->sc_dev,
1496 "could not send join command\n");
1499 case IEEE80211_S_RUN:
1500 /* Flush all AC queues. */
1501 rsu_write_1(sc, R92S_TXPAUSE, 0);
1503 ni = ieee80211_ref_node(vap->iv_bss);
1505 /* Indicate highest supported rate. */
1506 ni->ni_txrate = rs->rs_rates[rs->rs_nrates - 1];
1507 (void) rsu_set_fw_power_state(sc, RSU_PWR_SLEEP);
1508 ieee80211_free_node(ni);
1514 if (startcal != 0) {
1515 sc->sc_calibrating = 1;
1516 /* Start periodic calibration. */
1517 taskqueue_enqueue_timeout(taskqueue_thread, &sc->calib_task,
1522 return (uvp->newstate(vap, nstate, arg));
1526 rsu_key_alloc(struct ieee80211vap *vap, struct ieee80211_key *k,
1527 ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
1529 struct rsu_softc *sc = vap->iv_ic->ic_softc;
1532 if (&vap->iv_nw_keys[0] <= k &&
1533 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID]) {
1534 *keyix = ieee80211_crypto_get_key_wepidx(vap, k);
1536 if (vap->iv_opmode != IEEE80211_M_STA) {
1538 /* TODO: obtain keyix from node id */
1540 k->wk_flags |= IEEE80211_KEY_SWCRYPT;
1542 *keyix = R92S_MACID_BSS;
1547 if (isset(sc->keys_bmap, *keyix)) {
1548 device_printf(sc->sc_dev,
1549 "%s: key slot %d is already used!\n",
1554 setbit(sc->keys_bmap, *keyix);
1564 rsu_process_key(struct ieee80211vap *vap, const struct ieee80211_key *k,
1567 struct rsu_softc *sc = vap->iv_ic->ic_softc;
1570 if (k->wk_flags & IEEE80211_KEY_SWCRYPT) {
1575 /* Handle group keys. */
1576 if (&vap->iv_nw_keys[0] <= k &&
1577 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID]) {
1578 KASSERT(k->wk_keyix < nitems(sc->group_keys),
1579 ("keyix %u > %zu\n", k->wk_keyix, nitems(sc->group_keys)));
1582 sc->group_keys[k->wk_keyix] = (set ? k : NULL);
1583 if (!sc->sc_running) {
1584 /* Static keys will be set during device startup. */
1590 ret = rsu_set_key_group(sc, k);
1592 ret = rsu_delete_key(sc, k->wk_keyix);
1599 /* wait for pending key removal */
1600 taskqueue_drain(taskqueue_thread, &sc->del_key_task);
1603 ret = rsu_set_key_pair(sc, k);
1606 RSU_DELKEY_BMAP_LOCK(sc);
1607 setbit(sc->free_keys_bmap, k->wk_keyix);
1608 RSU_DELKEY_BMAP_UNLOCK(sc);
1610 /* workaround ieee80211_node_delucastkey() locking */
1611 taskqueue_enqueue(taskqueue_thread, &sc->del_key_task);
1612 ret = 0; /* fake success */
1619 rsu_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
1621 return (rsu_process_key(vap, k, 1));
1625 rsu_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
1627 return (rsu_process_key(vap, k, 0));
1631 rsu_cam_read(struct rsu_softc *sc, uint8_t addr, uint32_t *val)
1635 rsu_write_4(sc, R92S_CAMCMD,
1636 R92S_CAMCMD_POLLING | SM(R92S_CAMCMD_ADDR, addr));
1637 for (ntries = 0; ntries < 10; ntries++) {
1638 if (!(rsu_read_4(sc, R92S_CAMCMD) & R92S_CAMCMD_POLLING))
1641 usb_pause_mtx(&sc->sc_mtx, USB_MS_TO_TICKS(1));
1644 device_printf(sc->sc_dev,
1645 "%s: cannot read CAM entry at address %02X\n",
1650 *val = rsu_read_4(sc, R92S_CAMREAD);
1656 rsu_cam_write(struct rsu_softc *sc, uint8_t addr, uint32_t data)
1659 rsu_write_4(sc, R92S_CAMWRITE, data);
1660 rsu_write_4(sc, R92S_CAMCMD,
1661 R92S_CAMCMD_POLLING | R92S_CAMCMD_WRITE |
1662 SM(R92S_CAMCMD_ADDR, addr));
1666 rsu_key_check(struct rsu_softc *sc, ieee80211_keyix keyix, int is_valid)
1671 for (ntries = 0; ntries < 20; ntries++) {
1672 usb_pause_mtx(&sc->sc_mtx, USB_MS_TO_TICKS(1));
1674 error = rsu_cam_read(sc, R92S_CAM_CTL0(keyix), &val);
1676 device_printf(sc->sc_dev,
1677 "%s: cannot check key status!\n", __func__);
1680 if (((val & R92S_CAM_VALID) == 0) ^ is_valid)
1684 device_printf(sc->sc_dev,
1685 "%s: key %d is %s marked as valid, rejecting request\n",
1686 __func__, keyix, is_valid ? "not" : "still");
1694 * Map net80211 cipher to RTL8712 security mode.
1697 rsu_crypto_mode(struct rsu_softc *sc, u_int cipher, int keylen)
1700 case IEEE80211_CIPHER_WEP:
1701 return keylen < 8 ? R92S_KEY_ALGO_WEP40 : R92S_KEY_ALGO_WEP104;
1702 case IEEE80211_CIPHER_TKIP:
1703 return R92S_KEY_ALGO_TKIP;
1704 case IEEE80211_CIPHER_AES_CCM:
1705 return R92S_KEY_ALGO_AES;
1707 device_printf(sc->sc_dev, "unknown cipher %d\n", cipher);
1708 return R92S_KEY_ALGO_INVALID;
1713 rsu_set_key_group(struct rsu_softc *sc, const struct ieee80211_key *k)
1715 struct r92s_fw_cmd_set_key key;
1719 RSU_ASSERT_LOCKED(sc);
1721 /* Map net80211 cipher to HW crypto algorithm. */
1722 algo = rsu_crypto_mode(sc, k->wk_cipher->ic_cipher, k->wk_keylen);
1723 if (algo == R92S_KEY_ALGO_INVALID)
1726 memset(&key, 0, sizeof(key));
1728 key.cam_id = k->wk_keyix;
1729 key.grpkey = (k->wk_flags & IEEE80211_KEY_GROUP) != 0;
1730 memcpy(key.key, k->wk_key, MIN(k->wk_keylen, sizeof(key.key)));
1732 RSU_DPRINTF(sc, RSU_DEBUG_KEY | RSU_DEBUG_FWCMD,
1733 "%s: keyix %u, group %u, algo %u/%u, flags %04X, len %u, "
1734 "macaddr %s\n", __func__, key.cam_id, key.grpkey,
1735 k->wk_cipher->ic_cipher, key.algo, k->wk_flags, k->wk_keylen,
1736 ether_sprintf(k->wk_macaddr));
1738 error = rsu_fw_cmd(sc, R92S_CMD_SET_KEY, &key, sizeof(key));
1740 device_printf(sc->sc_dev,
1741 "%s: cannot send firmware command, error %d\n",
1746 return (rsu_key_check(sc, k->wk_keyix, 1));
1750 rsu_set_key_pair(struct rsu_softc *sc, const struct ieee80211_key *k)
1752 struct r92s_fw_cmd_set_key_mac key;
1756 RSU_ASSERT_LOCKED(sc);
1758 if (!sc->sc_running)
1761 /* Map net80211 cipher to HW crypto algorithm. */
1762 algo = rsu_crypto_mode(sc, k->wk_cipher->ic_cipher, k->wk_keylen);
1763 if (algo == R92S_KEY_ALGO_INVALID)
1766 memset(&key, 0, sizeof(key));
1768 memcpy(key.macaddr, k->wk_macaddr, sizeof(key.macaddr));
1769 memcpy(key.key, k->wk_key, MIN(k->wk_keylen, sizeof(key.key)));
1771 RSU_DPRINTF(sc, RSU_DEBUG_KEY | RSU_DEBUG_FWCMD,
1772 "%s: keyix %u, algo %u/%u, flags %04X, len %u, macaddr %s\n",
1773 __func__, k->wk_keyix, k->wk_cipher->ic_cipher, key.algo,
1774 k->wk_flags, k->wk_keylen, ether_sprintf(key.macaddr));
1776 error = rsu_fw_cmd(sc, R92S_CMD_SET_STA_KEY, &key, sizeof(key));
1778 device_printf(sc->sc_dev,
1779 "%s: cannot send firmware command, error %d\n",
1784 return (rsu_key_check(sc, k->wk_keyix, 1));
1788 rsu_reinit_static_keys(struct rsu_softc *sc)
1792 for (i = 0; i < nitems(sc->group_keys); i++) {
1793 if (sc->group_keys[i] != NULL) {
1794 error = rsu_set_key_group(sc, sc->group_keys[i]);
1796 device_printf(sc->sc_dev,
1797 "%s: failed to set static key %d, "
1798 "error %d\n", __func__, i, error);
1808 rsu_delete_key(struct rsu_softc *sc, ieee80211_keyix keyix)
1810 struct r92s_fw_cmd_set_key key;
1814 RSU_ASSERT_LOCKED(sc);
1816 if (!sc->sc_running)
1819 /* check if it was automatically removed by firmware */
1820 error = rsu_cam_read(sc, R92S_CAM_CTL0(keyix), &val);
1821 if (error == 0 && (val & R92S_CAM_VALID) == 0) {
1822 RSU_DPRINTF(sc, RSU_DEBUG_KEY,
1823 "%s: key %u does not exist\n", __func__, keyix);
1824 clrbit(sc->keys_bmap, keyix);
1828 memset(&key, 0, sizeof(key));
1831 RSU_DPRINTF(sc, RSU_DEBUG_KEY | RSU_DEBUG_FWCMD,
1832 "%s: removing key %u\n", __func__, key.cam_id);
1834 error = rsu_fw_cmd(sc, R92S_CMD_SET_KEY, &key, sizeof(key));
1836 device_printf(sc->sc_dev,
1837 "%s: cannot send firmware command, error %d\n",
1842 usb_pause_mtx(&sc->sc_mtx, USB_MS_TO_TICKS(5));
1845 * Clear 'valid' bit manually (cannot be done via firmware command).
1846 * Used for key check + when firmware command cannot be sent.
1849 rsu_cam_write(sc, R92S_CAM_CTL0(keyix), 0);
1851 clrbit(sc->keys_bmap, keyix);
1853 return (rsu_key_check(sc, keyix, 0));
1857 rsu_delete_key_pair_cb(void *arg, int pending __unused)
1859 struct rsu_softc *sc = arg;
1862 RSU_DELKEY_BMAP_LOCK(sc);
1863 for (i = IEEE80211_WEP_NKID; i < R92S_CAM_ENTRY_LIMIT; i++) {
1864 if (isset(sc->free_keys_bmap, i)) {
1865 RSU_DELKEY_BMAP_UNLOCK(sc);
1868 RSU_DPRINTF(sc, RSU_DEBUG_KEY,
1869 "%s: calling rsu_delete_key() with keyix = %d\n",
1871 (void) rsu_delete_key(sc, i);
1874 RSU_DELKEY_BMAP_LOCK(sc);
1875 clrbit(sc->free_keys_bmap, i);
1877 /* bmap can be changed */
1878 i = IEEE80211_WEP_NKID - 1;
1882 RSU_DELKEY_BMAP_UNLOCK(sc);
1886 rsu_site_survey(struct rsu_softc *sc, struct ieee80211_scan_ssid *ssid)
1888 struct r92s_fw_cmd_sitesurvey cmd;
1890 RSU_ASSERT_LOCKED(sc);
1892 memset(&cmd, 0, sizeof(cmd));
1893 /* TODO: passive channels? */
1894 if (sc->sc_active_scan)
1895 cmd.active = htole32(1);
1896 cmd.limit = htole32(48);
1899 sc->sc_extra_scan = 1;
1900 cmd.ssidlen = htole32(ssid->len);
1901 memcpy(cmd.ssid, ssid->ssid, ssid->len);
1904 if (rsu_debug & (RSU_DEBUG_SCAN | RSU_DEBUG_FWCMD)) {
1905 device_printf(sc->sc_dev,
1906 "sending site survey command, active %d",
1907 le32toh(cmd.active));
1910 ieee80211_print_essid(cmd.ssid, le32toh(cmd.ssidlen));
1915 return (rsu_fw_cmd(sc, R92S_CMD_SITE_SURVEY, &cmd, sizeof(cmd)));
1919 rsu_join_bss(struct rsu_softc *sc, struct ieee80211_node *ni)
1921 struct ieee80211com *ic = &sc->sc_ic;
1922 struct ieee80211vap *vap = ni->ni_vap;
1923 struct ndis_wlan_bssid_ex *bss;
1924 struct ndis_802_11_fixed_ies *fixed;
1925 struct r92s_fw_cmd_auth auth;
1926 uint8_t buf[sizeof(*bss) + 128] __aligned(4);
1931 RSU_ASSERT_LOCKED(sc);
1933 /* Let the FW decide the opmode based on the capinfo field. */
1934 opmode = NDIS802_11AUTOUNKNOWN;
1935 RSU_DPRINTF(sc, RSU_DEBUG_RESET,
1936 "%s: setting operating mode to %d\n",
1938 error = rsu_fw_cmd(sc, R92S_CMD_SET_OPMODE, &opmode, sizeof(opmode));
1942 memset(&auth, 0, sizeof(auth));
1943 if (vap->iv_flags & IEEE80211_F_WPA) {
1944 auth.mode = R92S_AUTHMODE_WPA;
1945 auth.dot1x = (ni->ni_authmode == IEEE80211_AUTH_8021X);
1947 auth.mode = R92S_AUTHMODE_OPEN;
1948 RSU_DPRINTF(sc, RSU_DEBUG_RESET,
1949 "%s: setting auth mode to %d\n",
1950 __func__, auth.mode);
1951 error = rsu_fw_cmd(sc, R92S_CMD_SET_AUTH, &auth, sizeof(auth));
1955 memset(buf, 0, sizeof(buf));
1956 bss = (struct ndis_wlan_bssid_ex *)buf;
1957 IEEE80211_ADDR_COPY(bss->macaddr, ni->ni_bssid);
1958 bss->ssid.ssidlen = htole32(ni->ni_esslen);
1959 memcpy(bss->ssid.ssid, ni->ni_essid, ni->ni_esslen);
1960 if (vap->iv_flags & (IEEE80211_F_PRIVACY | IEEE80211_F_WPA))
1961 bss->privacy = htole32(1);
1962 bss->rssi = htole32(ni->ni_avgrssi);
1963 if (ic->ic_curmode == IEEE80211_MODE_11B)
1964 bss->networktype = htole32(NDIS802_11DS);
1966 bss->networktype = htole32(NDIS802_11OFDM24);
1967 bss->config.len = htole32(sizeof(bss->config));
1968 bss->config.bintval = htole32(ni->ni_intval);
1969 bss->config.dsconfig = htole32(ieee80211_chan2ieee(ic, ni->ni_chan));
1970 bss->inframode = htole32(NDIS802_11INFRASTRUCTURE);
1971 /* XXX verify how this is supposed to look! */
1972 memcpy(bss->supprates, ni->ni_rates.rs_rates,
1973 ni->ni_rates.rs_nrates);
1974 /* Write the fixed fields of the beacon frame. */
1975 fixed = (struct ndis_802_11_fixed_ies *)&bss[1];
1976 memcpy(&fixed->tstamp, ni->ni_tstamp.data, 8);
1977 fixed->bintval = htole16(ni->ni_intval);
1978 fixed->capabilities = htole16(ni->ni_capinfo);
1979 /* Write IEs to be included in the association request. */
1980 frm = (uint8_t *)&fixed[1];
1981 frm = ieee80211_add_rsn(frm, vap);
1982 frm = ieee80211_add_wpa(frm, vap);
1983 frm = ieee80211_add_qos(frm, ni);
1984 if ((ic->ic_flags & IEEE80211_F_WME) &&
1985 (ni->ni_ies.wme_ie != NULL))
1986 frm = ieee80211_add_wme_info(frm, &ic->ic_wme, ni);
1987 if (ni->ni_flags & IEEE80211_NODE_HT) {
1988 frm = ieee80211_add_htcap(frm, ni);
1989 frm = ieee80211_add_htinfo(frm, ni);
1991 bss->ieslen = htole32(frm - (uint8_t *)fixed);
1992 bss->len = htole32(((frm - buf) + 3) & ~3);
1993 RSU_DPRINTF(sc, RSU_DEBUG_RESET | RSU_DEBUG_FWCMD,
1994 "%s: sending join bss command to %s chan %d\n",
1996 ether_sprintf(bss->macaddr), le32toh(bss->config.dsconfig));
1997 return (rsu_fw_cmd(sc, R92S_CMD_JOIN_BSS, buf, sizeof(buf)));
2001 rsu_disconnect(struct rsu_softc *sc)
2003 uint32_t zero = 0; /* :-) */
2005 /* Disassociate from our current BSS. */
2006 RSU_DPRINTF(sc, RSU_DEBUG_STATE | RSU_DEBUG_FWCMD,
2007 "%s: sending disconnect command\n", __func__);
2008 return (rsu_fw_cmd(sc, R92S_CMD_DISCONNECT, &zero, sizeof(zero)));
2012 * Map the hardware provided RSSI value to a signal level.
2013 * For the most part it's just something we divide by and cap
2014 * so it doesn't overflow the representation by net80211.
2017 rsu_hwrssi_to_rssi(struct rsu_softc *sc, int hw_rssi)
2029 CTASSERT(MCLBYTES > sizeof(struct ieee80211_frame));
2032 rsu_event_survey(struct rsu_softc *sc, uint8_t *buf, int len)
2034 struct ieee80211com *ic = &sc->sc_ic;
2035 struct ieee80211_frame *wh;
2036 struct ndis_wlan_bssid_ex *bss;
2037 struct ieee80211_rx_stats rxs;
2042 if (__predict_false(len < sizeof(*bss)))
2044 bss = (struct ndis_wlan_bssid_ex *)buf;
2045 ieslen = le32toh(bss->ieslen);
2046 /* range check length of information element */
2047 if (__predict_false(ieslen > (uint32_t)(len - sizeof(*bss))))
2050 RSU_DPRINTF(sc, RSU_DEBUG_SCAN,
2051 "%s: found BSS %s: len=%d chan=%d inframode=%d "
2052 "networktype=%d privacy=%d, RSSI=%d\n",
2054 ether_sprintf(bss->macaddr), ieslen,
2055 le32toh(bss->config.dsconfig), le32toh(bss->inframode),
2056 le32toh(bss->networktype), le32toh(bss->privacy),
2057 le32toh(bss->rssi));
2059 /* Build a fake beacon frame to let net80211 do all the parsing. */
2060 /* XXX TODO: just call the new scan API methods! */
2061 if (__predict_false(ieslen > (size_t)(MCLBYTES - sizeof(*wh))))
2063 pktlen = sizeof(*wh) + ieslen;
2064 m = m_get2(pktlen, M_NOWAIT, MT_DATA, M_PKTHDR);
2065 if (__predict_false(m == NULL))
2067 wh = mtod(m, struct ieee80211_frame *);
2068 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
2069 IEEE80211_FC0_SUBTYPE_BEACON;
2070 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2071 USETW(wh->i_dur, 0);
2072 IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr);
2073 IEEE80211_ADDR_COPY(wh->i_addr2, bss->macaddr);
2074 IEEE80211_ADDR_COPY(wh->i_addr3, bss->macaddr);
2075 *(uint16_t *)wh->i_seq = 0;
2076 memcpy(&wh[1], (uint8_t *)&bss[1], ieslen);
2078 /* Finalize mbuf. */
2079 m->m_pkthdr.len = m->m_len = pktlen;
2081 /* Set channel flags for input path */
2082 bzero(&rxs, sizeof(rxs));
2083 rxs.r_flags |= IEEE80211_R_IEEE | IEEE80211_R_FREQ;
2084 rxs.r_flags |= IEEE80211_R_NF | IEEE80211_R_RSSI;
2085 rxs.c_ieee = le32toh(bss->config.dsconfig);
2086 rxs.c_freq = ieee80211_ieee2mhz(rxs.c_ieee, IEEE80211_CHAN_2GHZ);
2087 /* This is a number from 0..100; so let's just divide it down a bit */
2088 rxs.c_rssi = le32toh(bss->rssi) / 2;
2090 if (ieee80211_add_rx_params(m, &rxs) == 0)
2093 /* XXX avoid a LOR */
2095 ieee80211_input_mimo_all(ic, m);
2100 rsu_event_join_bss(struct rsu_softc *sc, uint8_t *buf, int len)
2102 struct ieee80211com *ic = &sc->sc_ic;
2103 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2104 struct ieee80211_node *ni = vap->iv_bss;
2105 struct r92s_event_join_bss *rsp;
2109 if (__predict_false(len < sizeof(*rsp)))
2111 rsp = (struct r92s_event_join_bss *)buf;
2112 res = (int)le32toh(rsp->join_res);
2114 RSU_DPRINTF(sc, RSU_DEBUG_STATE | RSU_DEBUG_FWCMD,
2115 "%s: Rx join BSS event len=%d res=%d\n",
2116 __func__, len, res);
2119 * XXX Don't do this; there's likely a better way to tell
2120 * the caller we failed.
2124 ieee80211_new_state(vap, IEEE80211_S_SCAN, -1);
2129 tmp = le32toh(rsp->associd);
2130 if (tmp >= vap->iv_max_aid) {
2131 RSU_DPRINTF(sc, RSU_DEBUG_ANY, "Assoc ID overflow\n");
2134 RSU_DPRINTF(sc, RSU_DEBUG_STATE | RSU_DEBUG_FWCMD,
2135 "%s: associated with %s associd=%d\n",
2136 __func__, ether_sprintf(rsp->bss.macaddr), tmp);
2137 /* XXX is this required? What's the top two bits for again? */
2138 ni->ni_associd = tmp | 0xc000;
2140 /* Refresh Rx filter (was changed by firmware). */
2141 sc->sc_vap_is_running = 1;
2142 rsu_rxfilter_refresh(sc);
2145 ieee80211_new_state(vap, IEEE80211_S_RUN,
2146 IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
2151 rsu_event_addba_req_report(struct rsu_softc *sc, uint8_t *buf, int len)
2153 struct ieee80211com *ic = &sc->sc_ic;
2154 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2155 struct r92s_add_ba_event *ba = (void *) buf;
2156 struct ieee80211_node *ni;
2158 if (len < sizeof(*ba)) {
2159 device_printf(sc->sc_dev, "%s: short read (%d)\n", __func__, len);
2166 RSU_DPRINTF(sc, RSU_DEBUG_AMPDU, "%s: mac=%s, tid=%d, ssn=%d\n",
2168 ether_sprintf(ba->mac_addr),
2170 (int) le16toh(ba->ssn));
2172 /* XXX do node lookup; this is STA specific */
2174 ni = ieee80211_ref_node(vap->iv_bss);
2175 ieee80211_ampdu_rx_start_ext(ni, ba->tid, le16toh(ba->ssn) >> 4, 32);
2176 ieee80211_free_node(ni);
2180 rsu_rx_event(struct rsu_softc *sc, uint8_t code, uint8_t *buf, int len)
2182 struct ieee80211com *ic = &sc->sc_ic;
2183 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2185 RSU_DPRINTF(sc, RSU_DEBUG_RX | RSU_DEBUG_FWCMD,
2186 "%s: Rx event code=%d len=%d\n", __func__, code, len);
2188 case R92S_EVT_SURVEY:
2189 rsu_event_survey(sc, buf, len);
2191 case R92S_EVT_SURVEY_DONE:
2192 RSU_DPRINTF(sc, RSU_DEBUG_SCAN,
2193 "%s: %s scan done, found %d BSS\n",
2194 __func__, sc->sc_extra_scan ? "direct" : "broadcast",
2195 le32toh(*(uint32_t *)buf));
2196 if (sc->sc_extra_scan == 1) {
2197 /* Send broadcast probe request. */
2198 sc->sc_extra_scan = 0;
2199 if (vap != NULL && rsu_site_survey(sc, NULL) != 0) {
2201 ieee80211_cancel_scan(vap);
2208 ieee80211_scan_done(vap);
2212 case R92S_EVT_JOIN_BSS:
2213 if (vap->iv_state == IEEE80211_S_AUTH)
2214 rsu_event_join_bss(sc, buf, len);
2216 case R92S_EVT_DEL_STA:
2217 RSU_DPRINTF(sc, RSU_DEBUG_FWCMD | RSU_DEBUG_STATE,
2218 "%s: disassociated from %s\n", __func__,
2219 ether_sprintf(buf));
2220 if (vap->iv_state == IEEE80211_S_RUN &&
2221 IEEE80211_ADDR_EQ(vap->iv_bss->ni_bssid, buf)) {
2223 ieee80211_new_state(vap, IEEE80211_S_SCAN, -1);
2227 case R92S_EVT_WPS_PBC:
2228 RSU_DPRINTF(sc, RSU_DEBUG_RX | RSU_DEBUG_FWCMD,
2229 "%s: WPS PBC pushed.\n", __func__);
2231 case R92S_EVT_FWDBG:
2233 RSU_DPRINTF(sc, RSU_DEBUG_FWDBG, "FWDBG: %s\n", (char *)buf);
2235 case R92S_EVT_ADDBA_REQ_REPORT:
2236 rsu_event_addba_req_report(sc, buf, len);
2239 device_printf(sc->sc_dev, "%s: unhandled code (%d)\n", __func__, code);
2245 rsu_rx_multi_event(struct rsu_softc *sc, uint8_t *buf, int len)
2247 struct r92s_fw_cmd_hdr *cmd;
2250 RSU_DPRINTF(sc, RSU_DEBUG_RX, "%s: Rx events len=%d\n", __func__, len);
2252 /* Skip Rx status. */
2253 buf += sizeof(struct r92s_rx_stat);
2254 len -= sizeof(struct r92s_rx_stat);
2256 /* Process all events. */
2258 /* Check that command header fits. */
2259 if (__predict_false(len < sizeof(*cmd)))
2261 cmd = (struct r92s_fw_cmd_hdr *)buf;
2262 /* Check that command payload fits. */
2263 cmdsz = le16toh(cmd->len);
2264 if (__predict_false(len < sizeof(*cmd) + cmdsz))
2267 /* Process firmware event. */
2268 rsu_rx_event(sc, cmd->code, (uint8_t *)&cmd[1], cmdsz);
2270 if (!(cmd->seq & R92S_FW_CMD_MORE))
2272 buf += sizeof(*cmd) + cmdsz;
2273 len -= sizeof(*cmd) + cmdsz;
2278 rsu_get_rssi(struct rsu_softc *sc, int rate, void *physt)
2280 static const int8_t cckoff[] = { 14, -2, -20, -40 };
2281 struct r92s_rx_phystat *phy;
2282 struct r92s_rx_cck *cck;
2287 cck = (struct r92s_rx_cck *)physt;
2288 rpt = (cck->agc_rpt >> 6) & 0x3;
2289 rssi = cck->agc_rpt & 0x3e;
2290 rssi = cckoff[rpt] - rssi;
2291 } else { /* OFDM/HT. */
2292 phy = (struct r92s_rx_phystat *)physt;
2293 rssi = ((le32toh(phy->phydw1) >> 1) & 0x7f) - 106;
2298 static struct mbuf *
2299 rsu_rx_copy_to_mbuf(struct rsu_softc *sc, struct r92s_rx_stat *stat,
2302 struct ieee80211com *ic = &sc->sc_ic;
2307 rxdw0 = le32toh(stat->rxdw0);
2308 if (__predict_false(rxdw0 & (R92S_RXDW0_CRCERR | R92S_RXDW0_ICVERR))) {
2309 RSU_DPRINTF(sc, RSU_DEBUG_RX,
2310 "%s: RX flags error (%s)\n", __func__,
2311 rxdw0 & R92S_RXDW0_CRCERR ? "CRC" : "ICV");
2315 pktlen = MS(rxdw0, R92S_RXDW0_PKTLEN);
2316 if (__predict_false(pktlen < sizeof (struct ieee80211_frame_ack))) {
2317 RSU_DPRINTF(sc, RSU_DEBUG_RX,
2318 "%s: frame is too short: %d\n", __func__, pktlen);
2322 m = m_get2(totlen, M_NOWAIT, MT_DATA, M_PKTHDR);
2323 if (__predict_false(m == NULL)) {
2324 device_printf(sc->sc_dev,
2325 "%s: could not allocate RX mbuf, totlen %d\n",
2330 /* Finalize mbuf. */
2331 memcpy(mtod(m, uint8_t *), (uint8_t *)stat, totlen);
2332 m->m_pkthdr.len = m->m_len = totlen;
2336 counter_u64_add(ic->ic_ierrors, 1);
2341 rsu_get_tsf_low(struct rsu_softc *sc)
2343 return (rsu_read_4(sc, R92S_TSFTR));
2347 rsu_get_tsf_high(struct rsu_softc *sc)
2349 return (rsu_read_4(sc, R92S_TSFTR + 4));
2352 static struct ieee80211_node *
2353 rsu_rx_frame(struct rsu_softc *sc, struct mbuf *m)
2355 struct ieee80211com *ic = &sc->sc_ic;
2356 struct ieee80211_frame_min *wh;
2357 struct ieee80211_rx_stats rxs;
2358 struct r92s_rx_stat *stat;
2359 uint32_t rxdw0, rxdw3;
2360 uint8_t cipher, rate;
2364 stat = mtod(m, struct r92s_rx_stat *);
2365 rxdw0 = le32toh(stat->rxdw0);
2366 rxdw3 = le32toh(stat->rxdw3);
2368 rate = MS(rxdw3, R92S_RXDW3_RATE);
2369 cipher = MS(rxdw0, R92S_RXDW0_CIPHER);
2370 infosz = MS(rxdw0, R92S_RXDW0_INFOSZ) * 8;
2372 /* Get RSSI from PHY status descriptor if present. */
2373 if (infosz != 0 && (rxdw0 & R92S_RXDW0_PHYST))
2374 rssi = rsu_get_rssi(sc, rate, &stat[1]);
2376 /* Cheat and get the last calibrated RSSI */
2377 rssi = rsu_hwrssi_to_rssi(sc, sc->sc_currssi);
2380 /* Hardware does Rx TCP checksum offload. */
2382 * This flag can be set for some other
2383 * (e.g., EAPOL) frame types, so don't rely on it.
2385 if (rxdw3 & R92S_RXDW3_TCPCHKVALID) {
2386 RSU_DPRINTF(sc, RSU_DEBUG_RX,
2387 "%s: TCP/IP checksums: %schecked / %schecked\n",
2389 (rxdw3 & R92S_RXDW3_TCPCHKRPT) ? "" : "not ",
2390 (rxdw3 & R92S_RXDW3_IPCHKRPT) ? "" : "not ");
2393 * 'IP header checksum valid' bit will not be set if
2394 * the frame was not checked / has incorrect checksum /
2395 * does not have checksum (IPv6).
2397 * NB: if DF bit is not set then frame will not be checked.
2399 if (rxdw3 & R92S_RXDW3_IPCHKRPT) {
2400 m->m_pkthdr.csum_flags = CSUM_IP_CHECKED;
2401 m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
2405 * This is independent of the above check.
2407 if (rxdw3 & R92S_RXDW3_TCPCHKRPT) {
2408 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID;
2409 m->m_pkthdr.csum_flags |= CSUM_PSEUDO_HDR;
2410 m->m_pkthdr.csum_data = 0xffff;
2416 /* Set channel flags for input path */
2417 bzero(&rxs, sizeof(rxs));
2420 rxs.r_flags |= IEEE80211_R_NF | IEEE80211_R_RSSI;
2426 rxs.c_rate = ridx2rate[rate];
2427 if (RSU_RATE_IS_CCK(rate))
2428 rxs.c_pktflags |= IEEE80211_RX_F_CCK;
2430 rxs.c_pktflags |= IEEE80211_RX_F_OFDM;
2432 rxs.c_rate = IEEE80211_RATE_MCS | (rate - 12);
2433 rxs.c_pktflags |= IEEE80211_RX_F_HT;
2436 if (ieee80211_radiotap_active(ic)) {
2437 struct rsu_rx_radiotap_header *tap = &sc->sc_rxtap;
2439 /* Map HW rate index to 802.11 rate. */
2440 tap->wr_flags = 0; /* TODO */
2441 tap->wr_tsft = rsu_get_tsf_high(sc);
2442 if (le32toh(stat->tsf_low) > rsu_get_tsf_low(sc))
2444 tap->wr_tsft = (uint64_t)htole32(tap->wr_tsft) << 32;
2445 tap->wr_tsft += stat->tsf_low;
2447 tap->wr_rate = rxs.c_rate;
2448 tap->wr_dbm_antsignal = rssi;
2451 (void) ieee80211_add_rx_params(m, &rxs);
2453 /* Drop descriptor. */
2454 m_adj(m, sizeof(*stat) + infosz);
2455 wh = mtod(m, struct ieee80211_frame_min *);
2456 if ((wh->i_fc[1] & IEEE80211_FC1_PROTECTED) &&
2457 cipher != R92S_KEY_ALGO_NONE) {
2458 m->m_flags |= M_WEP;
2461 RSU_DPRINTF(sc, RSU_DEBUG_RX,
2462 "%s: Rx frame len %d, rate %d, infosz %d\n",
2463 __func__, m->m_len, rate, infosz);
2465 if (m->m_len >= sizeof(*wh))
2466 return (ieee80211_find_rxnode(ic, wh));
2471 static struct mbuf *
2472 rsu_rx_multi_frame(struct rsu_softc *sc, uint8_t *buf, int len)
2474 struct r92s_rx_stat *stat;
2476 int totlen, pktlen, infosz, npkts;
2477 struct mbuf *m, *m0 = NULL, *prevm = NULL;
2480 * don't pass packets to the ieee80211 framework if the driver isn't
2483 if (!sc->sc_running)
2486 /* Get the number of encapsulated frames. */
2487 stat = (struct r92s_rx_stat *)buf;
2488 npkts = MS(le32toh(stat->rxdw2), R92S_RXDW2_PKTCNT);
2489 RSU_DPRINTF(sc, RSU_DEBUG_RX,
2490 "%s: Rx %d frames in one chunk\n", __func__, npkts);
2492 /* Process all of them. */
2493 while (npkts-- > 0) {
2494 if (__predict_false(len < sizeof(*stat)))
2496 stat = (struct r92s_rx_stat *)buf;
2497 rxdw0 = le32toh(stat->rxdw0);
2499 pktlen = MS(rxdw0, R92S_RXDW0_PKTLEN);
2500 if (__predict_false(pktlen == 0))
2503 infosz = MS(rxdw0, R92S_RXDW0_INFOSZ) * 8;
2505 /* Make sure everything fits in xfer. */
2506 totlen = sizeof(*stat) + infosz + pktlen;
2507 if (__predict_false(totlen > len))
2510 /* Process 802.11 frame. */
2511 m = rsu_rx_copy_to_mbuf(sc, stat, totlen);
2520 /* Next chunk is 128-byte aligned. */
2521 totlen = (totlen + 127) & ~127;
2529 static struct mbuf *
2530 rsu_rxeof(struct usb_xfer *xfer, struct rsu_data *data)
2532 struct rsu_softc *sc = data->sc;
2533 struct ieee80211com *ic = &sc->sc_ic;
2534 struct r92s_rx_stat *stat;
2537 usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
2539 if (__predict_false(len < sizeof(*stat))) {
2540 RSU_DPRINTF(sc, RSU_DEBUG_RX, "xfer too short %d\n", len);
2541 counter_u64_add(ic->ic_ierrors, 1);
2544 /* Determine if it is a firmware C2H event or an 802.11 frame. */
2545 stat = (struct r92s_rx_stat *)data->buf;
2546 if ((le32toh(stat->rxdw1) & 0x1ff) == 0x1ff) {
2547 rsu_rx_multi_event(sc, data->buf, len);
2548 /* No packets to process. */
2551 return (rsu_rx_multi_frame(sc, data->buf, len));
2555 rsu_bulk_rx_callback(struct usb_xfer *xfer, usb_error_t error)
2557 struct epoch_tracker et;
2558 struct rsu_softc *sc = usbd_xfer_softc(xfer);
2559 struct ieee80211com *ic = &sc->sc_ic;
2560 struct ieee80211_node *ni;
2561 struct mbuf *m = NULL, *next;
2562 struct rsu_data *data;
2564 RSU_ASSERT_LOCKED(sc);
2566 switch (USB_GET_STATE(xfer)) {
2567 case USB_ST_TRANSFERRED:
2568 data = STAILQ_FIRST(&sc->sc_rx_active);
2571 STAILQ_REMOVE_HEAD(&sc->sc_rx_active, next);
2572 m = rsu_rxeof(xfer, data);
2573 STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next);
2577 data = STAILQ_FIRST(&sc->sc_rx_inactive);
2579 KASSERT(m == NULL, ("mbuf isn't NULL"));
2582 STAILQ_REMOVE_HEAD(&sc->sc_rx_inactive, next);
2583 STAILQ_INSERT_TAIL(&sc->sc_rx_active, data, next);
2584 usbd_xfer_set_frame_data(xfer, 0, data->buf,
2585 usbd_xfer_max_len(xfer));
2586 usbd_transfer_submit(xfer);
2588 * To avoid LOR we should unlock our private mutex here to call
2589 * ieee80211_input() because here is at the end of a USB
2590 * callback and safe to unlock.
2592 NET_EPOCH_ENTER(et);
2597 ni = rsu_rx_frame(sc, m);
2601 if (ni->ni_flags & IEEE80211_NODE_HT)
2602 m->m_flags |= M_AMPDU;
2603 (void)ieee80211_input_mimo(ni, m);
2604 ieee80211_free_node(ni);
2606 (void)ieee80211_input_mimo_all(ic, m);
2614 /* needs it to the inactive queue due to a error. */
2615 data = STAILQ_FIRST(&sc->sc_rx_active);
2617 STAILQ_REMOVE_HEAD(&sc->sc_rx_active, next);
2618 STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next);
2620 if (error != USB_ERR_CANCELLED) {
2621 usbd_xfer_set_stall(xfer);
2622 counter_u64_add(ic->ic_ierrors, 1);
2631 rsu_txeof(struct usb_xfer *xfer, struct rsu_data *data)
2634 struct rsu_softc *sc = usbd_xfer_softc(xfer);
2637 RSU_DPRINTF(sc, RSU_DEBUG_TXDONE, "%s: called; data=%p\n",
2643 ieee80211_tx_complete(data->ni, data->m, 0);
2650 rsu_bulk_tx_callback_sub(struct usb_xfer *xfer, usb_error_t error,
2653 struct rsu_softc *sc = usbd_xfer_softc(xfer);
2654 struct ieee80211com *ic = &sc->sc_ic;
2655 struct rsu_data *data;
2657 RSU_ASSERT_LOCKED(sc);
2659 switch (USB_GET_STATE(xfer)) {
2660 case USB_ST_TRANSFERRED:
2661 data = STAILQ_FIRST(&sc->sc_tx_active[which]);
2664 RSU_DPRINTF(sc, RSU_DEBUG_TXDONE, "%s: transfer done %p\n",
2666 STAILQ_REMOVE_HEAD(&sc->sc_tx_active[which], next);
2667 rsu_txeof(xfer, data);
2668 rsu_freebuf(sc, data);
2672 data = STAILQ_FIRST(&sc->sc_tx_pending[which]);
2674 RSU_DPRINTF(sc, RSU_DEBUG_TXDONE,
2675 "%s: empty pending queue sc %p\n", __func__, sc);
2678 STAILQ_REMOVE_HEAD(&sc->sc_tx_pending[which], next);
2679 STAILQ_INSERT_TAIL(&sc->sc_tx_active[which], data, next);
2680 usbd_xfer_set_frame_data(xfer, 0, data->buf, data->buflen);
2681 RSU_DPRINTF(sc, RSU_DEBUG_TXDONE,
2682 "%s: submitting transfer %p\n",
2685 usbd_transfer_submit(xfer);
2688 data = STAILQ_FIRST(&sc->sc_tx_active[which]);
2690 STAILQ_REMOVE_HEAD(&sc->sc_tx_active[which], next);
2691 rsu_txeof(xfer, data);
2692 rsu_freebuf(sc, data);
2694 counter_u64_add(ic->ic_oerrors, 1);
2696 if (error != USB_ERR_CANCELLED) {
2697 usbd_xfer_set_stall(xfer);
2704 * XXX TODO: if the queue is low, flush out FF TX frames.
2705 * Remember to unlock the driver for now; net80211 doesn't
2711 rsu_bulk_tx_callback_be_bk(struct usb_xfer *xfer, usb_error_t error)
2713 struct rsu_softc *sc = usbd_xfer_softc(xfer);
2715 rsu_bulk_tx_callback_sub(xfer, error, RSU_BULK_TX_BE_BK);
2717 /* This kicks the TX taskqueue */
2722 rsu_bulk_tx_callback_vi_vo(struct usb_xfer *xfer, usb_error_t error)
2724 struct rsu_softc *sc = usbd_xfer_softc(xfer);
2726 rsu_bulk_tx_callback_sub(xfer, error, RSU_BULK_TX_VI_VO);
2728 /* This kicks the TX taskqueue */
2733 rsu_bulk_tx_callback_h2c(struct usb_xfer *xfer, usb_error_t error)
2735 struct rsu_softc *sc = usbd_xfer_softc(xfer);
2737 rsu_bulk_tx_callback_sub(xfer, error, RSU_BULK_TX_H2C);
2739 /* This kicks the TX taskqueue */
2744 * Transmit the given frame.
2746 * This doesn't free the node or mbuf upon failure.
2749 rsu_tx_start(struct rsu_softc *sc, struct ieee80211_node *ni,
2750 struct mbuf *m0, struct rsu_data *data)
2752 const struct ieee80211_txparam *tp = ni->ni_txparms;
2753 struct ieee80211vap *vap = ni->ni_vap;
2754 struct ieee80211_frame *wh;
2755 struct ieee80211_key *k = NULL;
2756 struct r92s_tx_desc *txd;
2757 uint8_t rate, ridx, type, cipher, qos;
2765 RSU_ASSERT_LOCKED(sc);
2767 wh = mtod(m0, struct ieee80211_frame *);
2768 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
2769 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
2771 RSU_DPRINTF(sc, RSU_DEBUG_TX, "%s: data=%p, m=%p\n",
2772 __func__, data, m0);
2774 /* Choose a TX rate index. */
2775 if (type == IEEE80211_FC0_TYPE_MGT ||
2776 type == IEEE80211_FC0_TYPE_CTL ||
2777 (m0->m_flags & M_EAPOL) != 0)
2778 rate = tp->mgmtrate;
2780 rate = tp->mcastrate;
2781 else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
2782 rate = tp->ucastrate;
2787 ridx = rate2ridx(rate);
2789 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
2790 k = ieee80211_crypto_encap(ni, m0);
2792 device_printf(sc->sc_dev,
2793 "ieee80211_crypto_encap returns NULL.\n");
2794 /* XXX we don't expect the fragmented frames */
2797 wh = mtod(m0, struct ieee80211_frame *);
2799 /* If we have QoS then use it */
2800 /* XXX TODO: mbuf WME/PRI versus TID? */
2801 if (IEEE80211_QOS_HAS_SEQ(wh)) {
2803 prio = M_WME_GETAC(m0);
2804 which = rsu_wme_ac_xfer_map[prio];
2806 qos = ((const struct ieee80211_qosframe *)wh)->i_qos[0];
2809 /* XXX TODO: tid=0 for non-qos TID? */
2810 which = rsu_wme_ac_xfer_map[WME_AC_BE];
2816 qid = rsu_ac2qid[prio];
2819 case IEEE80211_FC0_TYPE_CTL:
2820 case IEEE80211_FC0_TYPE_MGT:
2821 which = rsu_wme_ac_xfer_map[WME_AC_VO];
2824 which = rsu_wme_ac_xfer_map[M_WME_GETAC(m0)];
2830 RSU_DPRINTF(sc, RSU_DEBUG_TX, "%s: pri=%d, which=%d, hasqos=%d\n",
2836 /* Fill Tx descriptor. */
2837 txd = (struct r92s_tx_desc *)data->buf;
2838 memset(txd, 0, sizeof(*txd));
2840 txd->txdw0 |= htole32(
2841 SM(R92S_TXDW0_PKTLEN, m0->m_pkthdr.len) |
2842 SM(R92S_TXDW0_OFFSET, sizeof(*txd)) |
2843 R92S_TXDW0_OWN | R92S_TXDW0_FSG | R92S_TXDW0_LSG);
2845 txd->txdw1 |= htole32(
2846 SM(R92S_TXDW1_MACID, R92S_MACID_BSS) | SM(R92S_TXDW1_QSEL, qid));
2848 txd->txdw1 |= htole32(R92S_TXDW1_NONQOS);
2849 if (k != NULL && !(k->wk_flags & IEEE80211_KEY_SWENCRYPT)) {
2850 switch (k->wk_cipher->ic_cipher) {
2851 case IEEE80211_CIPHER_WEP:
2852 cipher = R92S_TXDW1_CIPHER_WEP;
2854 case IEEE80211_CIPHER_TKIP:
2855 cipher = R92S_TXDW1_CIPHER_TKIP;
2857 case IEEE80211_CIPHER_AES_CCM:
2858 cipher = R92S_TXDW1_CIPHER_AES;
2861 cipher = R92S_TXDW1_CIPHER_NONE;
2863 txd->txdw1 |= htole32(
2864 SM(R92S_TXDW1_CIPHER, cipher) |
2865 SM(R92S_TXDW1_KEYIDX, k->wk_keyix));
2867 /* XXX todo: set AGGEN bit if appropriate? */
2868 txd->txdw2 |= htole32(R92S_TXDW2_BK);
2870 txd->txdw2 |= htole32(R92S_TXDW2_BMCAST);
2872 if (!ismcast && (!qos || (qos & IEEE80211_QOS_ACKPOLICY) !=
2873 IEEE80211_QOS_ACKPOLICY_NOACK)) {
2874 txd->txdw2 |= htole32(R92S_TXDW2_RTY_LMT_ENA);
2875 txd->txdw2 |= htole32(SM(R92S_TXDW2_RTY_LMT, tp->maxretry));
2878 /* Force mgmt / mcast / ucast rate if needed. */
2880 /* Data rate fallback limit (max). */
2881 txd->txdw5 |= htole32(SM(R92S_TXDW5_DATARATE_FB_LMT, 0x1f));
2882 txd->txdw5 |= htole32(SM(R92S_TXDW5_DATARATE, ridx));
2883 txd->txdw4 |= htole32(R92S_TXDW4_DRVRATE);
2887 * Firmware will use and increment the sequence number for the
2888 * specified priority.
2890 txd->txdw3 |= htole32(SM(R92S_TXDW3_SEQ, prio));
2892 if (ieee80211_radiotap_active_vap(vap)) {
2893 struct rsu_tx_radiotap_header *tap = &sc->sc_txtap;
2896 ieee80211_radiotap_tx(vap, m0);
2899 xferlen = sizeof(*txd) + m0->m_pkthdr.len;
2900 m_copydata(m0, 0, m0->m_pkthdr.len, (caddr_t)&txd[1]);
2902 data->buflen = xferlen;
2905 STAILQ_INSERT_TAIL(&sc->sc_tx_pending[which], data, next);
2907 /* start transfer, if any */
2908 usbd_transfer_start(sc->sc_xfer[which]);
2913 rsu_transmit(struct ieee80211com *ic, struct mbuf *m)
2915 struct rsu_softc *sc = ic->ic_softc;
2919 if (!sc->sc_running) {
2925 * XXX TODO: ensure that we treat 'm' as a list of frames
2928 error = mbufq_enqueue(&sc->sc_snd, m);
2930 RSU_DPRINTF(sc, RSU_DEBUG_TX,
2931 "%s: mbufq_enable: failed (%d)\n",
2939 /* This kicks the TX taskqueue */
2946 rsu_drain_mbufq(struct rsu_softc *sc)
2949 struct ieee80211_node *ni;
2951 RSU_ASSERT_LOCKED(sc);
2952 while ((m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
2953 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
2954 m->m_pkthdr.rcvif = NULL;
2955 ieee80211_free_node(ni);
2961 _rsu_start(struct rsu_softc *sc)
2963 struct ieee80211_node *ni;
2964 struct rsu_data *bf;
2967 RSU_ASSERT_LOCKED(sc);
2969 while ((m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
2970 bf = rsu_getbuf(sc);
2972 RSU_DPRINTF(sc, RSU_DEBUG_TX,
2973 "%s: failed to get buffer\n", __func__);
2974 mbufq_prepend(&sc->sc_snd, m);
2978 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
2979 m->m_pkthdr.rcvif = NULL;
2981 if (rsu_tx_start(sc, ni, m, bf) != 0) {
2982 RSU_DPRINTF(sc, RSU_DEBUG_TX,
2983 "%s: failed to transmit\n", __func__);
2984 if_inc_counter(ni->ni_vap->iv_ifp,
2985 IFCOUNTER_OERRORS, 1);
2986 rsu_freebuf(sc, bf);
2987 ieee80211_free_node(ni);
2995 rsu_start(struct rsu_softc *sc)
2998 taskqueue_enqueue(taskqueue_thread, &sc->tx_task);
3002 rsu_ioctl_net(struct ieee80211com *ic, u_long cmd, void *data)
3004 struct rsu_softc *sc = ic->ic_softc;
3005 struct ifreq *ifr = (struct ifreq *)data;
3012 struct ieee80211vap *vap;
3015 rxmask = ifr->ifr_reqcap & (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6);
3018 /* Both RXCSUM bits must be set (or unset). */
3019 if (sc->sc_rx_checksum_enable &&
3020 rxmask != (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6)) {
3022 sc->sc_rx_checksum_enable = 0;
3023 rsu_rxfilter_set(sc, R92S_RCR_TCP_OFFLD_EN, 0);
3024 } else if (!sc->sc_rx_checksum_enable && rxmask != 0) {
3025 rxmask = IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6;
3026 sc->sc_rx_checksum_enable = 1;
3027 rsu_rxfilter_set(sc, 0, R92S_RCR_TCP_OFFLD_EN);
3029 /* Nothing to do. */
3035 IEEE80211_LOCK(ic); /* XXX */
3036 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
3037 struct ifnet *ifp = vap->iv_ifp;
3039 ifp->if_capenable &=
3040 ~(IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6);
3041 ifp->if_capenable |= rxmask;
3043 IEEE80211_UNLOCK(ic);
3047 error = ENOTTY; /* for net80211 */
3055 rsu_parent(struct ieee80211com *ic)
3057 struct rsu_softc *sc = ic->ic_softc;
3059 if (ic->ic_nrunning > 0) {
3060 if (rsu_init(sc) == 0)
3061 ieee80211_start_all(ic);
3063 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
3065 ieee80211_stop(vap);
3072 * Power on sequence for A-cut adapters.
3075 rsu_power_on_acut(struct rsu_softc *sc)
3079 rsu_write_1(sc, R92S_SPS0_CTRL + 1, 0x53);
3080 rsu_write_1(sc, R92S_SPS0_CTRL + 0, 0x57);
3082 /* Enable AFE macro block's bandgap and Mbias. */
3083 rsu_write_1(sc, R92S_AFE_MISC,
3084 rsu_read_1(sc, R92S_AFE_MISC) |
3085 R92S_AFE_MISC_BGEN | R92S_AFE_MISC_MBEN);
3086 /* Enable LDOA15 block. */
3087 rsu_write_1(sc, R92S_LDOA15_CTRL,
3088 rsu_read_1(sc, R92S_LDOA15_CTRL) | R92S_LDA15_EN);
3090 rsu_write_1(sc, R92S_SPS1_CTRL,
3091 rsu_read_1(sc, R92S_SPS1_CTRL) | R92S_SPS1_LDEN);
3092 rsu_ms_delay(sc, 2000);
3093 /* Enable switch regulator block. */
3094 rsu_write_1(sc, R92S_SPS1_CTRL,
3095 rsu_read_1(sc, R92S_SPS1_CTRL) | R92S_SPS1_SWEN);
3097 rsu_write_4(sc, R92S_SPS1_CTRL, 0x00a7b267);
3099 rsu_write_1(sc, R92S_SYS_ISO_CTRL + 1,
3100 rsu_read_1(sc, R92S_SYS_ISO_CTRL + 1) | 0x08);
3102 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1,
3103 rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) | 0x20);
3105 rsu_write_1(sc, R92S_SYS_ISO_CTRL + 1,
3106 rsu_read_1(sc, R92S_SYS_ISO_CTRL + 1) & ~0x90);
3108 /* Enable AFE clock. */
3109 rsu_write_1(sc, R92S_AFE_XTAL_CTRL + 1,
3110 rsu_read_1(sc, R92S_AFE_XTAL_CTRL + 1) & ~0x04);
3111 /* Enable AFE PLL macro block. */
3112 rsu_write_1(sc, R92S_AFE_PLL_CTRL,
3113 rsu_read_1(sc, R92S_AFE_PLL_CTRL) | 0x11);
3114 /* Attach AFE PLL to MACTOP/BB. */
3115 rsu_write_1(sc, R92S_SYS_ISO_CTRL,
3116 rsu_read_1(sc, R92S_SYS_ISO_CTRL) & ~0x11);
3118 /* Switch to 40MHz clock instead of 80MHz. */
3119 rsu_write_2(sc, R92S_SYS_CLKR,
3120 rsu_read_2(sc, R92S_SYS_CLKR) & ~R92S_SYS_CLKSEL);
3122 /* Enable MAC clock. */
3123 rsu_write_2(sc, R92S_SYS_CLKR,
3124 rsu_read_2(sc, R92S_SYS_CLKR) |
3125 R92S_MAC_CLK_EN | R92S_SYS_CLK_EN);
3127 rsu_write_1(sc, R92S_PMC_FSM, 0x02);
3129 /* Enable digital core and IOREG R/W. */
3130 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1,
3131 rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) | 0x08);
3133 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1,
3134 rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) | 0x80);
3136 /* Switch the control path to firmware. */
3137 reg = rsu_read_2(sc, R92S_SYS_CLKR);
3138 reg = (reg & ~R92S_SWHW_SEL) | R92S_FWHW_SEL;
3139 rsu_write_2(sc, R92S_SYS_CLKR, reg);
3141 rsu_write_2(sc, R92S_CR, 0x37fc);
3143 /* Fix USB RX FIFO issue. */
3144 rsu_write_1(sc, 0xfe5c,
3145 rsu_read_1(sc, 0xfe5c) | 0x80);
3146 rsu_write_1(sc, 0x00ab,
3147 rsu_read_1(sc, 0x00ab) | 0xc0);
3149 rsu_write_1(sc, R92S_SYS_CLKR,
3150 rsu_read_1(sc, R92S_SYS_CLKR) & ~R92S_SYS_CPU_CLKSEL);
3154 * Power on sequence for B-cut and C-cut adapters.
3157 rsu_power_on_bcut(struct rsu_softc *sc)
3162 /* Prevent eFuse leakage. */
3163 rsu_write_1(sc, 0x37, 0xb0);
3164 rsu_ms_delay(sc, 10);
3165 rsu_write_1(sc, 0x37, 0x30);
3167 /* Switch the control path to hardware. */
3168 reg = rsu_read_2(sc, R92S_SYS_CLKR);
3169 if (reg & R92S_FWHW_SEL) {
3170 rsu_write_2(sc, R92S_SYS_CLKR,
3171 reg & ~(R92S_SWHW_SEL | R92S_FWHW_SEL));
3173 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1,
3174 rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) & ~0x8c);
3175 rsu_ms_delay(sc, 1);
3177 rsu_write_1(sc, R92S_SPS0_CTRL + 1, 0x53);
3178 rsu_write_1(sc, R92S_SPS0_CTRL + 0, 0x57);
3180 reg = rsu_read_1(sc, R92S_AFE_MISC);
3181 rsu_write_1(sc, R92S_AFE_MISC, reg | R92S_AFE_MISC_BGEN);
3182 rsu_write_1(sc, R92S_AFE_MISC, reg | R92S_AFE_MISC_BGEN |
3183 R92S_AFE_MISC_MBEN | R92S_AFE_MISC_I32_EN);
3186 rsu_write_1(sc, R92S_LDOA15_CTRL,
3187 rsu_read_1(sc, R92S_LDOA15_CTRL) | R92S_LDA15_EN);
3189 rsu_write_1(sc, R92S_LDOV12D_CTRL,
3190 rsu_read_1(sc, R92S_LDOV12D_CTRL) | R92S_LDV12_EN);
3192 rsu_write_1(sc, R92S_SYS_ISO_CTRL + 1,
3193 rsu_read_1(sc, R92S_SYS_ISO_CTRL + 1) | 0x08);
3195 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1,
3196 rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) | 0x20);
3198 /* Support 64KB IMEM. */
3199 rsu_write_1(sc, R92S_SYS_ISO_CTRL + 1,
3200 rsu_read_1(sc, R92S_SYS_ISO_CTRL + 1) & ~0x97);
3202 /* Enable AFE clock. */
3203 rsu_write_1(sc, R92S_AFE_XTAL_CTRL + 1,
3204 rsu_read_1(sc, R92S_AFE_XTAL_CTRL + 1) & ~0x04);
3205 /* Enable AFE PLL macro block. */
3206 reg = rsu_read_1(sc, R92S_AFE_PLL_CTRL);
3207 rsu_write_1(sc, R92S_AFE_PLL_CTRL, reg | 0x11);
3208 rsu_ms_delay(sc, 1);
3209 rsu_write_1(sc, R92S_AFE_PLL_CTRL, reg | 0x51);
3210 rsu_ms_delay(sc, 1);
3211 rsu_write_1(sc, R92S_AFE_PLL_CTRL, reg | 0x11);
3212 rsu_ms_delay(sc, 1);
3214 /* Attach AFE PLL to MACTOP/BB. */
3215 rsu_write_1(sc, R92S_SYS_ISO_CTRL,
3216 rsu_read_1(sc, R92S_SYS_ISO_CTRL) & ~0x11);
3218 /* Switch to 40MHz clock. */
3219 rsu_write_1(sc, R92S_SYS_CLKR, 0x00);
3220 /* Disable CPU clock and 80MHz SSC. */
3221 rsu_write_1(sc, R92S_SYS_CLKR,
3222 rsu_read_1(sc, R92S_SYS_CLKR) | 0xa0);
3223 /* Enable MAC clock. */
3224 rsu_write_2(sc, R92S_SYS_CLKR,
3225 rsu_read_2(sc, R92S_SYS_CLKR) |
3226 R92S_MAC_CLK_EN | R92S_SYS_CLK_EN);
3228 rsu_write_1(sc, R92S_PMC_FSM, 0x02);
3230 /* Enable digital core and IOREG R/W. */
3231 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1,
3232 rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) | 0x08);
3234 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1,
3235 rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) | 0x80);
3237 /* Switch the control path to firmware. */
3238 reg = rsu_read_2(sc, R92S_SYS_CLKR);
3239 reg = (reg & ~R92S_SWHW_SEL) | R92S_FWHW_SEL;
3240 rsu_write_2(sc, R92S_SYS_CLKR, reg);
3242 rsu_write_2(sc, R92S_CR, 0x37fc);
3244 /* Fix USB RX FIFO issue. */
3245 rsu_write_1(sc, 0xfe5c,
3246 rsu_read_1(sc, 0xfe5c) | 0x80);
3248 rsu_write_1(sc, R92S_SYS_CLKR,
3249 rsu_read_1(sc, R92S_SYS_CLKR) & ~R92S_SYS_CPU_CLKSEL);
3251 rsu_write_1(sc, 0xfe1c, 0x80);
3253 /* Make sure TxDMA is ready to download firmware. */
3254 for (ntries = 0; ntries < 20; ntries++) {
3255 reg = rsu_read_1(sc, R92S_TCR);
3256 if ((reg & (R92S_TCR_IMEM_CHK_RPT | R92S_TCR_EMEM_CHK_RPT)) ==
3257 (R92S_TCR_IMEM_CHK_RPT | R92S_TCR_EMEM_CHK_RPT))
3259 rsu_ms_delay(sc, 1);
3262 RSU_DPRINTF(sc, RSU_DEBUG_RESET | RSU_DEBUG_TX,
3263 "%s: TxDMA is not ready\n",
3266 reg = rsu_read_1(sc, R92S_CR);
3267 rsu_write_1(sc, R92S_CR, reg & ~R92S_CR_TXDMA_EN);
3268 rsu_ms_delay(sc, 1);
3269 rsu_write_1(sc, R92S_CR, reg | R92S_CR_TXDMA_EN);
3274 rsu_power_off(struct rsu_softc *sc)
3277 rsu_write_1(sc, R92S_RF_CTRL, 0x00);
3278 rsu_ms_delay(sc, 5);
3281 /* Switch control path. */
3282 rsu_write_1(sc, R92S_SYS_CLKR + 1, 0x38);
3284 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1, 0x70);
3285 rsu_write_1(sc, R92S_PMC_FSM, 0x06);
3286 rsu_write_1(sc, R92S_SYS_ISO_CTRL + 0, 0xf9);
3287 rsu_write_1(sc, R92S_SYS_ISO_CTRL + 1, 0xe8);
3289 /* Disable AFE PLL. */
3290 rsu_write_1(sc, R92S_AFE_PLL_CTRL, 0x00);
3292 rsu_write_1(sc, R92S_LDOA15_CTRL, 0x54);
3293 /* Disable eFuse 1.2V. */
3294 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1, 0x50);
3295 rsu_write_1(sc, R92S_LDOV12D_CTRL, 0x24);
3296 /* Enable AFE macro block's bandgap and Mbias. */
3297 rsu_write_1(sc, R92S_AFE_MISC, 0x30);
3298 /* Disable 1.6V LDO. */
3299 rsu_write_1(sc, R92S_SPS0_CTRL + 0, 0x56);
3300 rsu_write_1(sc, R92S_SPS0_CTRL + 1, 0x43);
3302 /* Firmware - tell it to switch things off */
3303 (void) rsu_set_fw_power_state(sc, RSU_PWR_OFF);
3307 rsu_fw_loadsection(struct rsu_softc *sc, const uint8_t *buf, int len)
3309 const uint8_t which = rsu_wme_ac_xfer_map[WME_AC_VO];
3310 struct rsu_data *data;
3311 struct r92s_tx_desc *txd;
3315 data = rsu_getbuf(sc);
3318 txd = (struct r92s_tx_desc *)data->buf;
3319 memset(txd, 0, sizeof(*txd));
3320 if (len <= RSU_TXBUFSZ - sizeof(*txd)) {
3322 txd->txdw0 |= htole32(R92S_TXDW0_LINIP);
3325 mlen = RSU_TXBUFSZ - sizeof(*txd);
3326 txd->txdw0 |= htole32(SM(R92S_TXDW0_PKTLEN, mlen));
3327 memcpy(&txd[1], buf, mlen);
3328 data->buflen = sizeof(*txd) + mlen;
3329 RSU_DPRINTF(sc, RSU_DEBUG_TX | RSU_DEBUG_FW | RSU_DEBUG_RESET,
3330 "%s: starting transfer %p\n",
3332 STAILQ_INSERT_TAIL(&sc->sc_tx_pending[which], data, next);
3336 usbd_transfer_start(sc->sc_xfer[which]);
3340 CTASSERT(sizeof(size_t) >= sizeof(uint32_t));
3343 rsu_load_firmware(struct rsu_softc *sc)
3345 const struct r92s_fw_hdr *hdr;
3346 struct r92s_fw_priv *dmem;
3347 struct ieee80211com *ic = &sc->sc_ic;
3348 const uint8_t *imem, *emem;
3349 uint32_t imemsz, ememsz;
3350 const struct firmware *fw;
3355 if (rsu_read_1(sc, R92S_TCR) & R92S_TCR_FWRDY) {
3356 RSU_DPRINTF(sc, RSU_DEBUG_ANY,
3357 "%s: Firmware already loaded\n",
3363 /* Read firmware image from the filesystem. */
3364 if ((fw = firmware_get("rsu-rtl8712fw")) == NULL) {
3365 device_printf(sc->sc_dev,
3366 "%s: failed load firmware of file rsu-rtl8712fw\n",
3372 size = fw->datasize;
3373 if (size < sizeof(*hdr)) {
3374 device_printf(sc->sc_dev, "firmware too short\n");
3378 hdr = (const struct r92s_fw_hdr *)fw->data;
3379 if (hdr->signature != htole16(0x8712) &&
3380 hdr->signature != htole16(0x8192)) {
3381 device_printf(sc->sc_dev,
3382 "invalid firmware signature 0x%x\n",
3383 le16toh(hdr->signature));
3387 RSU_DPRINTF(sc, RSU_DEBUG_FW, "FW V%d %02x-%02x %02x:%02x\n",
3388 le16toh(hdr->version), hdr->month, hdr->day, hdr->hour,
3391 /* Make sure that driver and firmware are in sync. */
3392 if (hdr->privsz != htole32(sizeof(*dmem))) {
3393 device_printf(sc->sc_dev, "unsupported firmware image\n");
3397 /* Get FW sections sizes. */
3398 imemsz = le32toh(hdr->imemsz);
3399 ememsz = le32toh(hdr->sramsz);
3400 /* Check that all FW sections fit in image. */
3401 if (imemsz > (size_t)(size - sizeof(*hdr)) ||
3402 ememsz > (size_t)(size - sizeof(*hdr) - imemsz)) {
3403 device_printf(sc->sc_dev, "firmware too short\n");
3407 imem = (const uint8_t *)&hdr[1];
3408 emem = imem + imemsz;
3410 /* Load IMEM section. */
3411 error = rsu_fw_loadsection(sc, imem, imemsz);
3413 device_printf(sc->sc_dev,
3414 "could not load firmware section %s\n", "IMEM");
3417 /* Wait for load to complete. */
3418 for (ntries = 0; ntries != 50; ntries++) {
3419 rsu_ms_delay(sc, 10);
3420 reg = rsu_read_1(sc, R92S_TCR);
3421 if (reg & R92S_TCR_IMEM_CODE_DONE)
3425 device_printf(sc->sc_dev, "timeout waiting for IMEM transfer\n");
3429 /* Load EMEM section. */
3430 error = rsu_fw_loadsection(sc, emem, ememsz);
3432 device_printf(sc->sc_dev,
3433 "could not load firmware section %s\n", "EMEM");
3436 /* Wait for load to complete. */
3437 for (ntries = 0; ntries != 50; ntries++) {
3438 rsu_ms_delay(sc, 10);
3439 reg = rsu_read_2(sc, R92S_TCR);
3440 if (reg & R92S_TCR_EMEM_CODE_DONE)
3444 device_printf(sc->sc_dev, "timeout waiting for EMEM transfer\n");
3449 rsu_write_1(sc, R92S_SYS_CLKR,
3450 rsu_read_1(sc, R92S_SYS_CLKR) | R92S_SYS_CPU_CLKSEL);
3451 if (!(rsu_read_1(sc, R92S_SYS_CLKR) & R92S_SYS_CPU_CLKSEL)) {
3452 device_printf(sc->sc_dev, "could not enable system clock\n");
3456 rsu_write_2(sc, R92S_SYS_FUNC_EN,
3457 rsu_read_2(sc, R92S_SYS_FUNC_EN) | R92S_FEN_CPUEN);
3458 if (!(rsu_read_2(sc, R92S_SYS_FUNC_EN) & R92S_FEN_CPUEN)) {
3459 device_printf(sc->sc_dev,
3460 "could not enable microcontroller\n");
3464 /* Wait for CPU to initialize. */
3465 for (ntries = 0; ntries < 100; ntries++) {
3466 if (rsu_read_1(sc, R92S_TCR) & R92S_TCR_IMEM_RDY)
3468 rsu_ms_delay(sc, 1);
3470 if (ntries == 100) {
3471 device_printf(sc->sc_dev,
3472 "timeout waiting for microcontroller\n");
3477 /* Update DMEM section before loading. */
3478 dmem = __DECONST(struct r92s_fw_priv *, &hdr->priv);
3479 memset(dmem, 0, sizeof(*dmem));
3480 dmem->hci_sel = R92S_HCI_SEL_USB | R92S_HCI_SEL_8172;
3481 dmem->nendpoints = sc->sc_nendpoints;
3482 dmem->chip_version = sc->cut;
3483 dmem->rf_config = sc->sc_rftype;
3484 dmem->vcs_type = R92S_VCS_TYPE_AUTO;
3485 dmem->vcs_mode = R92S_VCS_MODE_RTS_CTS;
3486 dmem->turbo_mode = 0;
3487 dmem->bw40_en = !! (ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40);
3488 dmem->amsdu2ampdu_en = !! (sc->sc_ht);
3489 dmem->ampdu_en = !! (sc->sc_ht);
3490 dmem->agg_offload = !! (sc->sc_ht);
3492 dmem->ps_offload = 1;
3493 dmem->lowpower_mode = 1; /* XXX TODO: configurable? */
3494 /* Load DMEM section. */
3495 error = rsu_fw_loadsection(sc, (uint8_t *)dmem, sizeof(*dmem));
3497 device_printf(sc->sc_dev,
3498 "could not load firmware section %s\n", "DMEM");
3501 /* Wait for load to complete. */
3502 for (ntries = 0; ntries < 100; ntries++) {
3503 if (rsu_read_1(sc, R92S_TCR) & R92S_TCR_DMEM_CODE_DONE)
3505 rsu_ms_delay(sc, 1);
3507 if (ntries == 100) {
3508 device_printf(sc->sc_dev, "timeout waiting for %s transfer\n",
3513 /* Wait for firmware readiness. */
3514 for (ntries = 0; ntries < 60; ntries++) {
3515 if (!(rsu_read_1(sc, R92S_TCR) & R92S_TCR_FWRDY))
3517 rsu_ms_delay(sc, 1);
3520 device_printf(sc->sc_dev,
3521 "timeout waiting for firmware readiness\n");
3526 firmware_put(fw, FIRMWARE_UNLOAD);
3532 rsu_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
3533 const struct ieee80211_bpf_params *params)
3535 struct ieee80211com *ic = ni->ni_ic;
3536 struct rsu_softc *sc = ic->ic_softc;
3537 struct rsu_data *bf;
3539 /* prevent management frames from being sent if we're not ready */
3540 if (!sc->sc_running) {
3545 bf = rsu_getbuf(sc);
3551 if (rsu_tx_start(sc, ni, m, bf) != 0) {
3553 rsu_freebuf(sc, bf);
3563 rsu_rxfilter_init(struct rsu_softc *sc)
3567 RSU_ASSERT_LOCKED(sc);
3569 /* Setup multicast filter. */
3572 /* Adjust Rx filter. */
3573 reg = rsu_read_4(sc, R92S_RCR);
3574 reg &= ~R92S_RCR_AICV;
3575 reg |= R92S_RCR_APP_PHYSTS;
3576 if (sc->sc_rx_checksum_enable)
3577 reg |= R92S_RCR_TCP_OFFLD_EN;
3578 rsu_write_4(sc, R92S_RCR, reg);
3580 /* Update dynamic Rx filter parts. */
3581 rsu_rxfilter_refresh(sc);
3585 rsu_rxfilter_set(struct rsu_softc *sc, uint32_t clear, uint32_t set)
3587 /* NB: firmware can touch this register too. */
3588 rsu_write_4(sc, R92S_RCR,
3589 (rsu_read_4(sc, R92S_RCR) & ~clear) | set);
3593 rsu_rxfilter_refresh(struct rsu_softc *sc)
3595 struct ieee80211com *ic = &sc->sc_ic;
3596 uint32_t mask_all, mask_min;
3598 RSU_ASSERT_LOCKED(sc);
3600 /* NB: RCR_AMF / RXFLTMAP_MGT are used by firmware. */
3601 mask_all = R92S_RCR_ACF | R92S_RCR_AAP;
3602 mask_min = R92S_RCR_APM;
3603 if (sc->sc_vap_is_running)
3604 mask_min |= R92S_RCR_CBSSID;
3606 mask_all |= R92S_RCR_ADF;
3608 if (ic->ic_opmode == IEEE80211_M_MONITOR) {
3610 if (sc->sc_vap_is_running)
3613 rxfltmap = R92S_RXFLTMAP_MGT_DEF;
3614 rsu_write_2(sc, R92S_RXFLTMAP_MGT, rxfltmap);
3617 if (ic->ic_promisc == 0 && ic->ic_opmode != IEEE80211_M_MONITOR)
3618 rsu_rxfilter_set(sc, mask_all, mask_min);
3620 rsu_rxfilter_set(sc, mask_min, mask_all);
3624 rsu_init(struct rsu_softc *sc)
3626 struct ieee80211com *ic = &sc->sc_ic;
3627 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
3628 uint8_t macaddr[IEEE80211_ADDR_LEN];
3634 if (sc->sc_running) {
3639 /* Ensure the mbuf queue is drained */
3640 rsu_drain_mbufq(sc);
3642 /* Reset power management state. */
3643 rsu_write_1(sc, R92S_USB_HRPWM, 0);
3645 /* Power on adapter. */
3647 rsu_power_on_acut(sc);
3649 rsu_power_on_bcut(sc);
3651 /* Load firmware. */
3652 error = rsu_load_firmware(sc);
3656 rsu_write_4(sc, R92S_CR,
3657 rsu_read_4(sc, R92S_CR) & ~0xff000000);
3659 /* Use 128 bytes pages. */
3660 rsu_write_1(sc, 0x00b5,
3661 rsu_read_1(sc, 0x00b5) | 0x01);
3662 /* Enable USB Rx aggregation. */
3663 rsu_write_1(sc, 0x00bd,
3664 rsu_read_1(sc, 0x00bd) | 0x80);
3665 /* Set USB Rx aggregation threshold. */
3666 rsu_write_1(sc, 0x00d9, 0x01);
3667 /* Set USB Rx aggregation timeout (1.7ms/4). */
3668 rsu_write_1(sc, 0xfe5b, 0x04);
3669 /* Fix USB Rx FIFO issue. */
3670 rsu_write_1(sc, 0xfe5c,
3671 rsu_read_1(sc, 0xfe5c) | 0x80);
3673 /* Set MAC address. */
3674 IEEE80211_ADDR_COPY(macaddr, vap ? vap->iv_myaddr : ic->ic_macaddr);
3675 rsu_write_region_1(sc, R92S_MACID, macaddr, IEEE80211_ADDR_LEN);
3677 /* It really takes 1.5 seconds for the firmware to boot: */
3678 usb_pause_mtx(&sc->sc_mtx, USB_MS_TO_TICKS(2000));
3680 RSU_DPRINTF(sc, RSU_DEBUG_RESET, "%s: setting MAC address to %s\n",
3682 ether_sprintf(macaddr));
3683 error = rsu_fw_cmd(sc, R92S_CMD_SET_MAC_ADDRESS, macaddr,
3684 IEEE80211_ADDR_LEN);
3686 device_printf(sc->sc_dev, "could not set MAC address\n");
3690 /* Initialize Rx filter. */
3691 rsu_rxfilter_init(sc);
3693 /* Set PS mode fully active */
3694 error = rsu_set_fw_power_state(sc, RSU_PWR_ACTIVE);
3696 device_printf(sc->sc_dev, "could not set PS mode\n");
3700 /* Install static keys (if any). */
3701 error = rsu_reinit_static_keys(sc);
3705 sc->sc_extra_scan = 0;
3706 usbd_transfer_start(sc->sc_xfer[RSU_BULK_RX]);
3708 /* We're ready to go. */
3714 /* Need to stop all failed transfers, if any */
3715 for (i = 0; i != RSU_N_TRANSFER; i++)
3716 usbd_transfer_stop(sc->sc_xfer[i]);
3723 rsu_stop(struct rsu_softc *sc)
3728 if (!sc->sc_running) {
3734 sc->sc_vap_is_running = 0;
3735 sc->sc_calibrating = 0;
3736 taskqueue_cancel_timeout(taskqueue_thread, &sc->calib_task, NULL);
3737 taskqueue_cancel(taskqueue_thread, &sc->tx_task, NULL);
3739 /* Power off adapter. */
3743 * CAM is not accessible after shutdown;
3744 * all entries are marked (by firmware?) as invalid.
3746 memset(sc->free_keys_bmap, 0, sizeof(sc->free_keys_bmap));
3747 memset(sc->keys_bmap, 0, sizeof(sc->keys_bmap));
3749 for (i = 0; i < RSU_N_TRANSFER; i++)
3750 usbd_transfer_stop(sc->sc_xfer[i]);
3752 /* Ensure the mbuf queue is drained */
3753 rsu_drain_mbufq(sc);
3758 * Note: usb_pause_mtx() actually releases the mutex before calling pause(),
3759 * which breaks any kind of driver serialisation.
3762 rsu_ms_delay(struct rsu_softc *sc, int ms)
3765 //usb_pause_mtx(&sc->sc_mtx, hz / 1000);