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>
43 #include <sys/firmware.h>
44 #include <sys/module.h>
46 #include <machine/bus.h>
47 #include <machine/resource.h>
51 #include <net/if_var.h>
52 #include <net/if_arp.h>
53 #include <net/if_dl.h>
54 #include <net/if_media.h>
55 #include <net/if_types.h>
57 #include <netinet/in.h>
58 #include <netinet/in_systm.h>
59 #include <netinet/in_var.h>
60 #include <netinet/if_ether.h>
61 #include <netinet/ip.h>
63 #include <net80211/ieee80211_var.h>
64 #include <net80211/ieee80211_regdomain.h>
65 #include <net80211/ieee80211_radiotap.h>
67 #include <dev/usb/usb.h>
68 #include <dev/usb/usbdi.h>
71 #include <dev/rtwn/if_rtwn_ridx.h> /* XXX */
72 #include <dev/usb/wlan/if_rsureg.h>
74 #define RSU_RATE_IS_CCK RTWN_RATE_IS_CCK
77 static int rsu_debug = 0;
78 SYSCTL_NODE(_hw_usb, OID_AUTO, rsu, CTLFLAG_RW, 0, "USB rsu");
79 SYSCTL_INT(_hw_usb_rsu, OID_AUTO, debug, CTLFLAG_RWTUN, &rsu_debug, 0,
81 #define RSU_DPRINTF(_sc, _flg, ...) \
83 if (((_flg) == (RSU_DEBUG_ANY)) || (rsu_debug & (_flg))) \
84 device_printf((_sc)->sc_dev, __VA_ARGS__); \
87 #define RSU_DPRINTF(_sc, _flg, ...)
90 static int rsu_enable_11n = 1;
91 TUNABLE_INT("hw.usb.rsu.enable_11n", &rsu_enable_11n);
93 #define RSU_DEBUG_ANY 0xffffffff
94 #define RSU_DEBUG_TX 0x00000001
95 #define RSU_DEBUG_RX 0x00000002
96 #define RSU_DEBUG_RESET 0x00000004
97 #define RSU_DEBUG_CALIB 0x00000008
98 #define RSU_DEBUG_STATE 0x00000010
99 #define RSU_DEBUG_SCAN 0x00000020
100 #define RSU_DEBUG_FWCMD 0x00000040
101 #define RSU_DEBUG_TXDONE 0x00000080
102 #define RSU_DEBUG_FW 0x00000100
103 #define RSU_DEBUG_FWDBG 0x00000200
104 #define RSU_DEBUG_AMPDU 0x00000400
105 #define RSU_DEBUG_KEY 0x00000800
106 #define RSU_DEBUG_USB 0x00001000
108 static const STRUCT_USB_HOST_ID rsu_devs[] = {
109 #define RSU_HT_NOT_SUPPORTED 0
110 #define RSU_HT_SUPPORTED 1
111 #define RSU_DEV_HT(v,p) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, \
113 #define RSU_DEV(v,p) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, \
114 RSU_HT_NOT_SUPPORTED) }
115 RSU_DEV(ASUS, RTL8192SU),
116 RSU_DEV(AZUREWAVE, RTL8192SU_4),
117 RSU_DEV_HT(ACCTON, RTL8192SU),
118 RSU_DEV_HT(ASUS, USBN10),
119 RSU_DEV_HT(AZUREWAVE, RTL8192SU_1),
120 RSU_DEV_HT(AZUREWAVE, RTL8192SU_2),
121 RSU_DEV_HT(AZUREWAVE, RTL8192SU_3),
122 RSU_DEV_HT(AZUREWAVE, RTL8192SU_5),
123 RSU_DEV_HT(BELKIN, RTL8192SU_1),
124 RSU_DEV_HT(BELKIN, RTL8192SU_2),
125 RSU_DEV_HT(BELKIN, RTL8192SU_3),
126 RSU_DEV_HT(CONCEPTRONIC2, RTL8192SU_1),
127 RSU_DEV_HT(CONCEPTRONIC2, RTL8192SU_2),
128 RSU_DEV_HT(CONCEPTRONIC2, RTL8192SU_3),
129 RSU_DEV_HT(COREGA, RTL8192SU),
130 RSU_DEV_HT(DLINK2, DWA131A1),
131 RSU_DEV_HT(DLINK2, RTL8192SU_1),
132 RSU_DEV_HT(DLINK2, RTL8192SU_2),
133 RSU_DEV_HT(EDIMAX, RTL8192SU_1),
134 RSU_DEV_HT(EDIMAX, RTL8192SU_2),
135 RSU_DEV_HT(EDIMAX, EW7622UMN),
136 RSU_DEV_HT(GUILLEMOT, HWGUN54),
137 RSU_DEV_HT(GUILLEMOT, HWNUM300),
138 RSU_DEV_HT(HAWKING, RTL8192SU_1),
139 RSU_DEV_HT(HAWKING, RTL8192SU_2),
140 RSU_DEV_HT(PLANEX2, GWUSNANO),
141 RSU_DEV_HT(REALTEK, RTL8171),
142 RSU_DEV_HT(REALTEK, RTL8172),
143 RSU_DEV_HT(REALTEK, RTL8173),
144 RSU_DEV_HT(REALTEK, RTL8174),
145 RSU_DEV_HT(REALTEK, RTL8192SU),
146 RSU_DEV_HT(REALTEK, RTL8712),
147 RSU_DEV_HT(REALTEK, RTL8713),
148 RSU_DEV_HT(SENAO, RTL8192SU_1),
149 RSU_DEV_HT(SENAO, RTL8192SU_2),
150 RSU_DEV_HT(SITECOMEU, WL349V1),
151 RSU_DEV_HT(SITECOMEU, WL353),
152 RSU_DEV_HT(SWEEX2, LW154),
153 RSU_DEV_HT(TRENDNET, TEW646UBH),
158 static device_probe_t rsu_match;
159 static device_attach_t rsu_attach;
160 static device_detach_t rsu_detach;
161 static usb_callback_t rsu_bulk_tx_callback_be_bk;
162 static usb_callback_t rsu_bulk_tx_callback_vi_vo;
163 static usb_callback_t rsu_bulk_tx_callback_h2c;
164 static usb_callback_t rsu_bulk_rx_callback;
165 static usb_error_t rsu_do_request(struct rsu_softc *,
166 struct usb_device_request *, void *);
167 static struct ieee80211vap *
168 rsu_vap_create(struct ieee80211com *, const char name[],
169 int, enum ieee80211_opmode, int, const uint8_t bssid[],
170 const uint8_t mac[]);
171 static void rsu_vap_delete(struct ieee80211vap *);
172 static void rsu_scan_start(struct ieee80211com *);
173 static void rsu_scan_end(struct ieee80211com *);
174 static void rsu_getradiocaps(struct ieee80211com *, int, int *,
175 struct ieee80211_channel[]);
176 static void rsu_set_channel(struct ieee80211com *);
177 static void rsu_scan_curchan(struct ieee80211_scan_state *, unsigned long);
178 static void rsu_scan_mindwell(struct ieee80211_scan_state *);
179 static void rsu_update_promisc(struct ieee80211com *);
180 static uint8_t rsu_get_multi_pos(const uint8_t[]);
181 static void rsu_set_multi(struct rsu_softc *);
182 static void rsu_update_mcast(struct ieee80211com *);
183 static int rsu_alloc_rx_list(struct rsu_softc *);
184 static void rsu_free_rx_list(struct rsu_softc *);
185 static int rsu_alloc_tx_list(struct rsu_softc *);
186 static void rsu_free_tx_list(struct rsu_softc *);
187 static void rsu_free_list(struct rsu_softc *, struct rsu_data [], int);
188 static struct rsu_data *_rsu_getbuf(struct rsu_softc *);
189 static struct rsu_data *rsu_getbuf(struct rsu_softc *);
190 static void rsu_freebuf(struct rsu_softc *, struct rsu_data *);
191 static int rsu_write_region_1(struct rsu_softc *, uint16_t, uint8_t *,
193 static void rsu_write_1(struct rsu_softc *, uint16_t, uint8_t);
194 static void rsu_write_2(struct rsu_softc *, uint16_t, uint16_t);
195 static void rsu_write_4(struct rsu_softc *, uint16_t, uint32_t);
196 static int rsu_read_region_1(struct rsu_softc *, uint16_t, uint8_t *,
198 static uint8_t rsu_read_1(struct rsu_softc *, uint16_t);
199 static uint16_t rsu_read_2(struct rsu_softc *, uint16_t);
200 static uint32_t rsu_read_4(struct rsu_softc *, uint16_t);
201 static int rsu_fw_iocmd(struct rsu_softc *, uint32_t);
202 static uint8_t rsu_efuse_read_1(struct rsu_softc *, uint16_t);
203 static int rsu_read_rom(struct rsu_softc *);
204 static int rsu_fw_cmd(struct rsu_softc *, uint8_t, void *, int);
205 static void rsu_calib_task(void *, int);
206 static void rsu_tx_task(void *, int);
207 static void rsu_set_led(struct rsu_softc *, int);
208 static int rsu_monitor_newstate(struct ieee80211vap *,
209 enum ieee80211_state, int);
210 static int rsu_newstate(struct ieee80211vap *, enum ieee80211_state, int);
211 static int rsu_key_alloc(struct ieee80211vap *, struct ieee80211_key *,
212 ieee80211_keyix *, ieee80211_keyix *);
213 static int rsu_process_key(struct ieee80211vap *,
214 const struct ieee80211_key *, int);
215 static int rsu_key_set(struct ieee80211vap *,
216 const struct ieee80211_key *);
217 static int rsu_key_delete(struct ieee80211vap *,
218 const struct ieee80211_key *);
219 static int rsu_cam_read(struct rsu_softc *, uint8_t, uint32_t *);
220 static void rsu_cam_write(struct rsu_softc *, uint8_t, uint32_t);
221 static int rsu_key_check(struct rsu_softc *, ieee80211_keyix, int);
222 static uint8_t rsu_crypto_mode(struct rsu_softc *, u_int, int);
223 static int rsu_set_key_group(struct rsu_softc *,
224 const struct ieee80211_key *);
225 static int rsu_set_key_pair(struct rsu_softc *,
226 const struct ieee80211_key *);
227 static int rsu_reinit_static_keys(struct rsu_softc *);
228 static int rsu_delete_key(struct rsu_softc *sc, ieee80211_keyix);
229 static void rsu_delete_key_pair_cb(void *, int);
230 static int rsu_site_survey(struct rsu_softc *,
231 struct ieee80211_scan_ssid *);
232 static int rsu_join_bss(struct rsu_softc *, struct ieee80211_node *);
233 static int rsu_disconnect(struct rsu_softc *);
234 static int rsu_hwrssi_to_rssi(struct rsu_softc *, int hw_rssi);
235 static void rsu_event_survey(struct rsu_softc *, uint8_t *, int);
236 static void rsu_event_join_bss(struct rsu_softc *, uint8_t *, int);
237 static void rsu_rx_event(struct rsu_softc *, uint8_t, uint8_t *, int);
238 static void rsu_rx_multi_event(struct rsu_softc *, uint8_t *, int);
239 static int8_t rsu_get_rssi(struct rsu_softc *, int, void *);
240 static struct mbuf * rsu_rx_copy_to_mbuf(struct rsu_softc *,
241 struct r92s_rx_stat *, int);
242 static uint32_t rsu_get_tsf_low(struct rsu_softc *);
243 static uint32_t rsu_get_tsf_high(struct rsu_softc *);
244 static struct ieee80211_node * rsu_rx_frame(struct rsu_softc *, struct mbuf *);
245 static struct mbuf * rsu_rx_multi_frame(struct rsu_softc *, uint8_t *, int);
247 rsu_rxeof(struct usb_xfer *, struct rsu_data *);
248 static void rsu_txeof(struct usb_xfer *, struct rsu_data *);
249 static int rsu_raw_xmit(struct ieee80211_node *, struct mbuf *,
250 const struct ieee80211_bpf_params *);
251 static void rsu_rxfilter_init(struct rsu_softc *);
252 static void rsu_rxfilter_set(struct rsu_softc *, uint32_t, uint32_t);
253 static void rsu_rxfilter_refresh(struct rsu_softc *);
254 static int rsu_init(struct rsu_softc *);
255 static int rsu_tx_start(struct rsu_softc *, struct ieee80211_node *,
256 struct mbuf *, struct rsu_data *);
257 static int rsu_transmit(struct ieee80211com *, struct mbuf *);
258 static void rsu_start(struct rsu_softc *);
259 static void _rsu_start(struct rsu_softc *);
260 static int rsu_ioctl_net(struct ieee80211com *, u_long, void *);
261 static void rsu_parent(struct ieee80211com *);
262 static void rsu_stop(struct rsu_softc *);
263 static void rsu_ms_delay(struct rsu_softc *, int);
265 static device_method_t rsu_methods[] = {
266 DEVMETHOD(device_probe, rsu_match),
267 DEVMETHOD(device_attach, rsu_attach),
268 DEVMETHOD(device_detach, rsu_detach),
273 static driver_t rsu_driver = {
275 .methods = rsu_methods,
276 .size = sizeof(struct rsu_softc)
279 static devclass_t rsu_devclass;
281 DRIVER_MODULE(rsu, uhub, rsu_driver, rsu_devclass, NULL, 0);
282 MODULE_DEPEND(rsu, wlan, 1, 1, 1);
283 MODULE_DEPEND(rsu, usb, 1, 1, 1);
284 MODULE_DEPEND(rsu, firmware, 1, 1, 1);
285 MODULE_VERSION(rsu, 1);
286 USB_PNP_HOST_INFO(rsu_devs);
288 static const uint8_t rsu_chan_2ghz[] =
289 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };
291 static uint8_t rsu_wme_ac_xfer_map[4] = {
292 [WME_AC_BE] = RSU_BULK_TX_BE_BK,
293 [WME_AC_BK] = RSU_BULK_TX_BE_BK,
294 [WME_AC_VI] = RSU_BULK_TX_VI_VO,
295 [WME_AC_VO] = RSU_BULK_TX_VI_VO,
299 #define RSU_H2C_ENDPOINT 3
301 static const struct usb_config rsu_config[RSU_N_TRANSFER] = {
304 .endpoint = UE_ADDR_ANY,
305 .direction = UE_DIR_IN,
306 .bufsize = RSU_RXBUFSZ,
311 .callback = rsu_bulk_rx_callback
313 [RSU_BULK_TX_BE_BK] = {
316 .direction = UE_DIR_OUT,
317 .bufsize = RSU_TXBUFSZ,
321 .force_short_xfer = 1
323 .callback = rsu_bulk_tx_callback_be_bk,
324 .timeout = RSU_TX_TIMEOUT
326 [RSU_BULK_TX_VI_VO] = {
329 .direction = UE_DIR_OUT,
330 .bufsize = RSU_TXBUFSZ,
334 .force_short_xfer = 1
336 .callback = rsu_bulk_tx_callback_vi_vo,
337 .timeout = RSU_TX_TIMEOUT
339 [RSU_BULK_TX_H2C] = {
342 .direction = UE_DIR_OUT,
343 .bufsize = RSU_TXBUFSZ,
349 .callback = rsu_bulk_tx_callback_h2c,
350 .timeout = RSU_TX_TIMEOUT
355 rsu_match(device_t self)
357 struct usb_attach_arg *uaa = device_get_ivars(self);
359 if (uaa->usb_mode != USB_MODE_HOST ||
360 uaa->info.bIfaceIndex != 0 ||
361 uaa->info.bConfigIndex != 0)
364 return (usbd_lookup_id_by_uaa(rsu_devs, sizeof(rsu_devs), uaa));
368 rsu_send_mgmt(struct ieee80211_node *ni, int type, int arg)
375 rsu_update_chw(struct ieee80211com *ic)
381 * notification from net80211 that it'd like to do A-MPDU on the given TID.
383 * Note: this actually hangs traffic at the present moment, so don't use it.
384 * The firmware debug does indiciate it's sending and establishing a TX AMPDU
385 * session, but then no traffic flows.
388 rsu_ampdu_enable(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
391 struct rsu_softc *sc = ni->ni_ic->ic_softc;
392 struct r92s_add_ba_req req;
394 /* Don't enable if it's requested or running */
395 if (IEEE80211_AMPDU_REQUESTED(tap))
397 if (IEEE80211_AMPDU_RUNNING(tap))
400 /* We've decided to send addba; so send it */
401 req.tid = htole32(tap->txa_tid);
403 /* Attempt net80211 state */
404 if (ieee80211_ampdu_tx_request_ext(ni, tap->txa_tid) != 1)
407 /* Send the firmware command */
408 RSU_DPRINTF(sc, RSU_DEBUG_AMPDU, "%s: establishing AMPDU TX for TID %d\n",
413 if (rsu_fw_cmd(sc, R92S_CMD_ADDBA_REQ, &req, sizeof(req)) != 1) {
416 (void) ieee80211_ampdu_tx_request_active_ext(ni, tap->txa_tid, 0);
421 /* Mark success; we don't get any further notifications */
422 (void) ieee80211_ampdu_tx_request_active_ext(ni, tap->txa_tid, 1);
424 /* Return 0, we're driving this ourselves */
429 rsu_wme_update(struct ieee80211com *ic)
432 /* Firmware handles this; not our problem */
437 rsu_attach(device_t self)
439 struct usb_attach_arg *uaa = device_get_ivars(self);
440 struct rsu_softc *sc = device_get_softc(self);
441 struct ieee80211com *ic = &sc->sc_ic;
444 struct usb_interface *iface;
447 device_set_usb_desc(self);
448 sc->sc_udev = uaa->device;
450 sc->sc_rx_checksum_enable = 1;
452 sc->sc_ht = !! (USB_GET_DRIVER_INFO(uaa) & RSU_HT_SUPPORTED);
454 /* Get number of endpoints */
455 iface = usbd_get_iface(sc->sc_udev, 0);
456 sc->sc_nendpoints = iface->idesc->bNumEndpoints;
458 /* Endpoints are hard-coded for now, so enforce 4-endpoint only */
459 if (sc->sc_nendpoints != 4) {
460 device_printf(sc->sc_dev,
461 "the driver currently only supports 4-endpoint devices\n");
465 mtx_init(&sc->sc_mtx, device_get_nameunit(self), MTX_NETWORK_LOCK,
467 RSU_DELKEY_BMAP_LOCK_INIT(sc);
468 TIMEOUT_TASK_INIT(taskqueue_thread, &sc->calib_task, 0,
470 TASK_INIT(&sc->del_key_task, 0, rsu_delete_key_pair_cb, sc);
471 TASK_INIT(&sc->tx_task, 0, rsu_tx_task, sc);
472 mbufq_init(&sc->sc_snd, ifqmaxlen);
474 /* Allocate Tx/Rx buffers. */
475 error = rsu_alloc_rx_list(sc);
477 device_printf(sc->sc_dev, "could not allocate Rx buffers\n");
481 error = rsu_alloc_tx_list(sc);
483 device_printf(sc->sc_dev, "could not allocate Tx buffers\n");
484 rsu_free_rx_list(sc);
489 error = usbd_transfer_setup(uaa->device, &iface_index, sc->sc_xfer,
490 rsu_config, RSU_N_TRANSFER, sc, &sc->sc_mtx);
492 device_printf(sc->sc_dev,
493 "could not allocate USB transfers, err=%s\n",
498 /* Read chip revision. */
499 sc->cut = MS(rsu_read_4(sc, R92S_PMC_FSM), R92S_PMC_FSM_CUT);
501 sc->cut = (sc->cut >> 1) + 1;
502 error = rsu_read_rom(sc);
505 device_printf(self, "could not read ROM\n");
509 /* Figure out TX/RX streams */
510 switch (sc->rom[84]) {
512 sc->sc_rftype = RTL8712_RFCONFIG_1T1R;
513 sc->sc_nrxstream = 1;
514 sc->sc_ntxstream = 1;
518 sc->sc_rftype = RTL8712_RFCONFIG_1T2R;
519 sc->sc_nrxstream = 2;
520 sc->sc_ntxstream = 1;
524 sc->sc_rftype = RTL8712_RFCONFIG_2T2R;
525 sc->sc_nrxstream = 2;
526 sc->sc_ntxstream = 2;
529 case 0x3: /* "green" NIC */
530 sc->sc_rftype = RTL8712_RFCONFIG_1T2R;
531 sc->sc_nrxstream = 2;
532 sc->sc_ntxstream = 1;
533 rft = "1T2R ('green')";
536 device_printf(sc->sc_dev,
537 "%s: unknown board type (rfconfig=0x%02x)\n",
543 IEEE80211_ADDR_COPY(ic->ic_macaddr, &sc->rom[0x12]);
544 device_printf(self, "MAC/BB RTL8712 cut %d %s\n", sc->cut, rft);
547 ic->ic_name = device_get_nameunit(self);
548 ic->ic_phytype = IEEE80211_T_OFDM; /* Not only, but not used. */
549 ic->ic_opmode = IEEE80211_M_STA; /* Default to BSS mode. */
551 /* Set device capabilities. */
553 IEEE80211_C_STA | /* station mode */
554 IEEE80211_C_MONITOR | /* monitor mode supported */
556 IEEE80211_C_BGSCAN | /* Background scan. */
558 IEEE80211_C_SHPREAMBLE | /* Short preamble supported. */
559 IEEE80211_C_WME | /* WME/QoS */
560 IEEE80211_C_SHSLOT | /* Short slot time supported. */
561 IEEE80211_C_WPA; /* WPA/RSN. */
564 IEEE80211_CRYPTO_WEP |
565 IEEE80211_CRYPTO_TKIP |
566 IEEE80211_CRYPTO_AES_CCM;
568 /* Check if HT support is present. */
570 device_printf(sc->sc_dev, "%s: enabling 11n\n", __func__);
572 /* Enable basic HT */
573 ic->ic_htcaps = IEEE80211_HTC_HT |
575 IEEE80211_HTC_AMPDU |
577 IEEE80211_HTC_AMSDU |
578 IEEE80211_HTCAP_MAXAMSDU_3839 |
579 IEEE80211_HTCAP_SMPS_OFF;
580 ic->ic_htcaps |= IEEE80211_HTCAP_CHWIDTH40;
582 /* set number of spatial streams */
583 ic->ic_txstream = sc->sc_ntxstream;
584 ic->ic_rxstream = sc->sc_nrxstream;
586 ic->ic_flags_ext |= IEEE80211_FEXT_SCAN_OFFLOAD;
588 rsu_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
591 ieee80211_ifattach(ic);
592 ic->ic_raw_xmit = rsu_raw_xmit;
593 ic->ic_scan_start = rsu_scan_start;
594 ic->ic_scan_end = rsu_scan_end;
595 ic->ic_getradiocaps = rsu_getradiocaps;
596 ic->ic_set_channel = rsu_set_channel;
597 ic->ic_scan_curchan = rsu_scan_curchan;
598 ic->ic_scan_mindwell = rsu_scan_mindwell;
599 ic->ic_vap_create = rsu_vap_create;
600 ic->ic_vap_delete = rsu_vap_delete;
601 ic->ic_update_promisc = rsu_update_promisc;
602 ic->ic_update_mcast = rsu_update_mcast;
603 ic->ic_ioctl = rsu_ioctl_net;
604 ic->ic_parent = rsu_parent;
605 ic->ic_transmit = rsu_transmit;
606 ic->ic_send_mgmt = rsu_send_mgmt;
607 ic->ic_update_chw = rsu_update_chw;
608 ic->ic_ampdu_enable = rsu_ampdu_enable;
609 ic->ic_wme.wme_update = rsu_wme_update;
611 ieee80211_radiotap_attach(ic, &sc->sc_txtap.wt_ihdr,
612 sizeof(sc->sc_txtap), RSU_TX_RADIOTAP_PRESENT,
613 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
614 RSU_RX_RADIOTAP_PRESENT);
617 ieee80211_announce(ic);
622 usbd_transfer_unsetup(sc->sc_xfer, RSU_N_TRANSFER);
624 mtx_destroy(&sc->sc_mtx);
629 rsu_detach(device_t self)
631 struct rsu_softc *sc = device_get_softc(self);
632 struct ieee80211com *ic = &sc->sc_ic;
636 usbd_transfer_unsetup(sc->sc_xfer, RSU_N_TRANSFER);
639 * Free buffers /before/ we detach from net80211, else node
640 * references to destroyed vaps will lead to a panic.
642 /* Free Tx/Rx buffers. */
644 rsu_free_tx_list(sc);
645 rsu_free_rx_list(sc);
648 /* Frames are freed; detach from net80211 */
649 ieee80211_ifdetach(ic);
651 taskqueue_drain_timeout(taskqueue_thread, &sc->calib_task);
652 taskqueue_drain(taskqueue_thread, &sc->del_key_task);
653 taskqueue_drain(taskqueue_thread, &sc->tx_task);
655 RSU_DELKEY_BMAP_LOCK_DESTROY(sc);
656 mtx_destroy(&sc->sc_mtx);
662 rsu_do_request(struct rsu_softc *sc, struct usb_device_request *req,
668 RSU_ASSERT_LOCKED(sc);
671 err = usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
672 req, data, 0, NULL, 250 /* ms */);
673 if (err == 0 || err == USB_ERR_NOT_CONFIGURED)
675 RSU_DPRINTF(sc, RSU_DEBUG_USB,
676 "Control request failed, %s (retries left: %d)\n",
677 usbd_errstr(err), ntries);
678 rsu_ms_delay(sc, 10);
684 static struct ieee80211vap *
685 rsu_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
686 enum ieee80211_opmode opmode, int flags,
687 const uint8_t bssid[IEEE80211_ADDR_LEN],
688 const uint8_t mac[IEEE80211_ADDR_LEN])
690 struct rsu_softc *sc = ic->ic_softc;
692 struct ieee80211vap *vap;
695 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */
698 uvp = malloc(sizeof(struct rsu_vap), M_80211_VAP, M_WAITOK | M_ZERO);
701 if (ieee80211_vap_setup(ic, vap, name, unit, opmode,
702 flags, bssid) != 0) {
704 free(uvp, M_80211_VAP);
709 ifp->if_capabilities = IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6;
711 if (sc->sc_rx_checksum_enable)
712 ifp->if_capenable |= IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6;
715 /* override state transition machine */
716 uvp->newstate = vap->iv_newstate;
717 if (opmode == IEEE80211_M_MONITOR)
718 vap->iv_newstate = rsu_monitor_newstate;
720 vap->iv_newstate = rsu_newstate;
721 vap->iv_key_alloc = rsu_key_alloc;
722 vap->iv_key_set = rsu_key_set;
723 vap->iv_key_delete = rsu_key_delete;
725 /* Limits from the r92su driver */
726 vap->iv_ampdu_density = IEEE80211_HTCAP_MPDUDENSITY_16;
727 vap->iv_ampdu_rxmax = IEEE80211_HTCAP_MAXRXAMPDU_32K;
730 ieee80211_vap_attach(vap, ieee80211_media_change,
731 ieee80211_media_status, mac);
732 ic->ic_opmode = opmode;
738 rsu_vap_delete(struct ieee80211vap *vap)
740 struct rsu_vap *uvp = RSU_VAP(vap);
742 ieee80211_vap_detach(vap);
743 free(uvp, M_80211_VAP);
747 rsu_scan_start(struct ieee80211com *ic)
749 struct rsu_softc *sc = ic->ic_softc;
750 struct ieee80211_scan_state *ss = ic->ic_scan;
751 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
754 /* Scanning is done by the firmware. */
756 sc->sc_active_scan = !!(ss->ss_flags & IEEE80211_SCAN_ACTIVE);
757 /* XXX TODO: force awake if in network-sleep? */
758 error = rsu_site_survey(sc, ss->ss_nssid > 0 ? &ss->ss_ssid[0] : NULL);
761 device_printf(sc->sc_dev,
762 "could not send site survey command\n");
763 ieee80211_cancel_scan(vap);
768 rsu_scan_end(struct ieee80211com *ic)
770 /* Nothing to do here. */
774 rsu_getradiocaps(struct ieee80211com *ic,
775 int maxchans, int *nchans, struct ieee80211_channel chans[])
777 struct rsu_softc *sc = ic->ic_softc;
778 uint8_t bands[IEEE80211_MODE_BYTES];
780 /* Set supported .11b and .11g rates. */
781 memset(bands, 0, sizeof(bands));
782 setbit(bands, IEEE80211_MODE_11B);
783 setbit(bands, IEEE80211_MODE_11G);
785 setbit(bands, IEEE80211_MODE_11NG);
786 ieee80211_add_channel_list_2ghz(chans, maxchans, nchans,
787 rsu_chan_2ghz, nitems(rsu_chan_2ghz), bands,
788 (ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) != 0);
792 rsu_set_channel(struct ieee80211com *ic)
794 struct rsu_softc *sc = ic->ic_softc;
797 * Only need to set the channel in Monitor mode. AP scanning and auth
798 * are already taken care of by their respective firmware commands.
800 if (ic->ic_opmode == IEEE80211_M_MONITOR) {
801 struct r92s_set_channel cmd;
804 cmd.channel = IEEE80211_CHAN2IEEE(ic->ic_curchan);
807 error = rsu_fw_cmd(sc, R92S_CMD_SET_CHANNEL, &cmd,
810 device_printf(sc->sc_dev,
811 "%s: error %d setting channel\n", __func__,
819 rsu_scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell)
821 /* Scan is done in rsu_scan_start(). */
825 * Called by the net80211 framework to indicate
826 * the minimum dwell time has been met, terminate the scan.
827 * We don't actually terminate the scan as the firmware will notify
828 * us when it's finished and we have no way to interrupt it.
831 rsu_scan_mindwell(struct ieee80211_scan_state *ss)
833 /* NB: don't try to abort scan; wait for firmware to finish */
837 rsu_update_promisc(struct ieee80211com *ic)
839 struct rsu_softc *sc = ic->ic_softc;
843 rsu_rxfilter_refresh(sc);
848 * The same as rtwn_get_multi_pos() / rtwn_set_multi().
851 rsu_get_multi_pos(const uint8_t maddr[])
853 uint64_t mask = 0x00004d101df481b4;
854 uint8_t pos = 0x27; /* initial value */
857 for (i = 0; i < IEEE80211_ADDR_LEN; i++)
858 for (j = (i == 0) ? 1 : 0; j < 8; j++)
859 if ((maddr[i] >> j) & 1)
860 pos ^= (mask >> (i * 8 + j - 1));
868 rsu_set_multi(struct rsu_softc *sc)
870 struct ieee80211com *ic = &sc->sc_ic;
873 RSU_ASSERT_LOCKED(sc);
875 /* general structure was copied from ath(4). */
876 if (ic->ic_allmulti == 0) {
877 struct ieee80211vap *vap;
879 struct ifmultiaddr *ifma;
882 * Merge multicast addresses to form the hardware filter.
884 mfilt[0] = mfilt[1] = 0;
885 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
888 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
892 dl = LLADDR((struct sockaddr_dl *)
894 pos = rsu_get_multi_pos(dl);
896 mfilt[pos / 32] |= (1 << (pos % 32));
898 if_maddr_runlock(ifp);
901 mfilt[0] = mfilt[1] = ~0;
903 rsu_write_4(sc, R92S_MAR + 0, mfilt[0]);
904 rsu_write_4(sc, R92S_MAR + 4, mfilt[1]);
906 RSU_DPRINTF(sc, RSU_DEBUG_STATE, "%s: MC filter %08x:%08x\n",
907 __func__, mfilt[0], mfilt[1]);
911 rsu_update_mcast(struct ieee80211com *ic)
913 struct rsu_softc *sc = ic->ic_softc;
922 rsu_alloc_list(struct rsu_softc *sc, struct rsu_data data[],
923 int ndata, int maxsz)
927 for (i = 0; i < ndata; i++) {
928 struct rsu_data *dp = &data[i];
931 dp->buf = malloc(maxsz, M_USBDEV, M_NOWAIT);
932 if (dp->buf == NULL) {
933 device_printf(sc->sc_dev,
934 "could not allocate buffer\n");
943 rsu_free_list(sc, data, ndata);
948 rsu_alloc_rx_list(struct rsu_softc *sc)
952 error = rsu_alloc_list(sc, sc->sc_rx, RSU_RX_LIST_COUNT,
957 STAILQ_INIT(&sc->sc_rx_active);
958 STAILQ_INIT(&sc->sc_rx_inactive);
960 for (i = 0; i < RSU_RX_LIST_COUNT; i++)
961 STAILQ_INSERT_HEAD(&sc->sc_rx_inactive, &sc->sc_rx[i], next);
967 rsu_alloc_tx_list(struct rsu_softc *sc)
971 error = rsu_alloc_list(sc, sc->sc_tx, RSU_TX_LIST_COUNT,
976 STAILQ_INIT(&sc->sc_tx_inactive);
978 for (i = 0; i != RSU_N_TRANSFER; i++) {
979 STAILQ_INIT(&sc->sc_tx_active[i]);
980 STAILQ_INIT(&sc->sc_tx_pending[i]);
983 for (i = 0; i < RSU_TX_LIST_COUNT; i++) {
984 STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, &sc->sc_tx[i], next);
991 rsu_free_tx_list(struct rsu_softc *sc)
995 /* prevent further allocations from TX list(s) */
996 STAILQ_INIT(&sc->sc_tx_inactive);
998 for (i = 0; i != RSU_N_TRANSFER; i++) {
999 STAILQ_INIT(&sc->sc_tx_active[i]);
1000 STAILQ_INIT(&sc->sc_tx_pending[i]);
1003 rsu_free_list(sc, sc->sc_tx, RSU_TX_LIST_COUNT);
1007 rsu_free_rx_list(struct rsu_softc *sc)
1009 /* prevent further allocations from RX list(s) */
1010 STAILQ_INIT(&sc->sc_rx_inactive);
1011 STAILQ_INIT(&sc->sc_rx_active);
1013 rsu_free_list(sc, sc->sc_rx, RSU_RX_LIST_COUNT);
1017 rsu_free_list(struct rsu_softc *sc, struct rsu_data data[], int ndata)
1021 for (i = 0; i < ndata; i++) {
1022 struct rsu_data *dp = &data[i];
1024 if (dp->buf != NULL) {
1025 free(dp->buf, M_USBDEV);
1028 if (dp->ni != NULL) {
1029 ieee80211_free_node(dp->ni);
1035 static struct rsu_data *
1036 _rsu_getbuf(struct rsu_softc *sc)
1038 struct rsu_data *bf;
1040 bf = STAILQ_FIRST(&sc->sc_tx_inactive);
1042 STAILQ_REMOVE_HEAD(&sc->sc_tx_inactive, next);
1048 static struct rsu_data *
1049 rsu_getbuf(struct rsu_softc *sc)
1051 struct rsu_data *bf;
1053 RSU_ASSERT_LOCKED(sc);
1055 bf = _rsu_getbuf(sc);
1057 RSU_DPRINTF(sc, RSU_DEBUG_TX, "%s: no buffers\n", __func__);
1063 rsu_freebuf(struct rsu_softc *sc, struct rsu_data *bf)
1066 RSU_ASSERT_LOCKED(sc);
1067 STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, bf, next);
1071 rsu_write_region_1(struct rsu_softc *sc, uint16_t addr, uint8_t *buf,
1074 usb_device_request_t req;
1076 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1077 req.bRequest = R92S_REQ_REGS;
1078 USETW(req.wValue, addr);
1079 USETW(req.wIndex, 0);
1080 USETW(req.wLength, len);
1082 return (rsu_do_request(sc, &req, buf));
1086 rsu_write_1(struct rsu_softc *sc, uint16_t addr, uint8_t val)
1088 rsu_write_region_1(sc, addr, &val, 1);
1092 rsu_write_2(struct rsu_softc *sc, uint16_t addr, uint16_t val)
1095 rsu_write_region_1(sc, addr, (uint8_t *)&val, 2);
1099 rsu_write_4(struct rsu_softc *sc, uint16_t addr, uint32_t val)
1102 rsu_write_region_1(sc, addr, (uint8_t *)&val, 4);
1106 rsu_read_region_1(struct rsu_softc *sc, uint16_t addr, uint8_t *buf,
1109 usb_device_request_t req;
1111 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1112 req.bRequest = R92S_REQ_REGS;
1113 USETW(req.wValue, addr);
1114 USETW(req.wIndex, 0);
1115 USETW(req.wLength, len);
1117 return (rsu_do_request(sc, &req, buf));
1121 rsu_read_1(struct rsu_softc *sc, uint16_t addr)
1125 if (rsu_read_region_1(sc, addr, &val, 1) != 0)
1131 rsu_read_2(struct rsu_softc *sc, uint16_t addr)
1135 if (rsu_read_region_1(sc, addr, (uint8_t *)&val, 2) != 0)
1137 return (le16toh(val));
1141 rsu_read_4(struct rsu_softc *sc, uint16_t addr)
1145 if (rsu_read_region_1(sc, addr, (uint8_t *)&val, 4) != 0)
1146 return (0xffffffff);
1147 return (le32toh(val));
1151 rsu_fw_iocmd(struct rsu_softc *sc, uint32_t iocmd)
1155 rsu_write_4(sc, R92S_IOCMD_CTRL, iocmd);
1156 rsu_ms_delay(sc, 1);
1157 for (ntries = 0; ntries < 50; ntries++) {
1158 if (rsu_read_4(sc, R92S_IOCMD_CTRL) == 0)
1160 rsu_ms_delay(sc, 1);
1166 rsu_efuse_read_1(struct rsu_softc *sc, uint16_t addr)
1171 reg = rsu_read_4(sc, R92S_EFUSE_CTRL);
1172 reg = RW(reg, R92S_EFUSE_CTRL_ADDR, addr);
1173 reg &= ~R92S_EFUSE_CTRL_VALID;
1174 rsu_write_4(sc, R92S_EFUSE_CTRL, reg);
1175 /* Wait for read operation to complete. */
1176 for (ntries = 0; ntries < 100; ntries++) {
1177 reg = rsu_read_4(sc, R92S_EFUSE_CTRL);
1178 if (reg & R92S_EFUSE_CTRL_VALID)
1179 return (MS(reg, R92S_EFUSE_CTRL_DATA));
1180 rsu_ms_delay(sc, 1);
1182 device_printf(sc->sc_dev,
1183 "could not read efuse byte at address 0x%x\n", addr);
1188 rsu_read_rom(struct rsu_softc *sc)
1190 uint8_t *rom = sc->rom;
1196 /* Make sure that ROM type is eFuse and that autoload succeeded. */
1197 reg = rsu_read_1(sc, R92S_EE_9346CR);
1198 if ((reg & (R92S_9356SEL | R92S_EEPROM_EN)) != R92S_EEPROM_EN)
1201 /* Turn on 2.5V to prevent eFuse leakage. */
1202 reg = rsu_read_1(sc, R92S_EFUSE_TEST + 3);
1203 rsu_write_1(sc, R92S_EFUSE_TEST + 3, reg | 0x80);
1204 rsu_ms_delay(sc, 1);
1205 rsu_write_1(sc, R92S_EFUSE_TEST + 3, reg & ~0x80);
1207 /* Read full ROM image. */
1208 memset(&sc->rom, 0xff, sizeof(sc->rom));
1209 while (addr < 512) {
1210 reg = rsu_efuse_read_1(sc, addr);
1216 for (i = 0; i < 4; i++) {
1219 rom[off * 8 + i * 2 + 0] =
1220 rsu_efuse_read_1(sc, addr);
1222 rom[off * 8 + i * 2 + 1] =
1223 rsu_efuse_read_1(sc, addr);
1228 if (rsu_debug & RSU_DEBUG_RESET) {
1229 /* Dump ROM content. */
1231 for (i = 0; i < sizeof(sc->rom); i++)
1232 printf("%02x:", rom[i]);
1240 rsu_fw_cmd(struct rsu_softc *sc, uint8_t code, void *buf, int len)
1242 const uint8_t which = RSU_H2C_ENDPOINT;
1243 struct rsu_data *data;
1244 struct r92s_tx_desc *txd;
1245 struct r92s_fw_cmd_hdr *cmd;
1249 RSU_ASSERT_LOCKED(sc);
1251 data = rsu_getbuf(sc);
1255 /* Blank the entire payload, just to be safe */
1256 memset(data->buf, '\0', RSU_TXBUFSZ);
1258 /* Round-up command length to a multiple of 8 bytes. */
1259 /* XXX TODO: is this required? */
1260 cmdsz = (len + 7) & ~7;
1262 xferlen = sizeof(*txd) + sizeof(*cmd) + cmdsz;
1263 KASSERT(xferlen <= RSU_TXBUFSZ, ("%s: invalid length", __func__));
1264 memset(data->buf, 0, xferlen);
1266 /* Setup Tx descriptor. */
1267 txd = (struct r92s_tx_desc *)data->buf;
1268 txd->txdw0 = htole32(
1269 SM(R92S_TXDW0_OFFSET, sizeof(*txd)) |
1270 SM(R92S_TXDW0_PKTLEN, sizeof(*cmd) + cmdsz) |
1271 R92S_TXDW0_OWN | R92S_TXDW0_FSG | R92S_TXDW0_LSG);
1272 txd->txdw1 = htole32(SM(R92S_TXDW1_QSEL, R92S_TXDW1_QSEL_H2C));
1274 /* Setup command header. */
1275 cmd = (struct r92s_fw_cmd_hdr *)&txd[1];
1276 cmd->len = htole16(cmdsz);
1278 cmd->seq = sc->cmd_seq;
1279 sc->cmd_seq = (sc->cmd_seq + 1) & 0x7f;
1281 /* Copy command payload. */
1282 memcpy(&cmd[1], buf, len);
1284 RSU_DPRINTF(sc, RSU_DEBUG_TX | RSU_DEBUG_FWCMD,
1285 "%s: Tx cmd code=0x%x len=0x%x\n",
1286 __func__, code, cmdsz);
1287 data->buflen = xferlen;
1288 STAILQ_INSERT_TAIL(&sc->sc_tx_pending[which], data, next);
1289 usbd_transfer_start(sc->sc_xfer[which]);
1296 rsu_calib_task(void *arg, int pending __unused)
1298 struct rsu_softc *sc = arg;
1303 RSU_DPRINTF(sc, RSU_DEBUG_CALIB, "%s: running calibration task\n",
1308 /* Read WPS PBC status. */
1309 rsu_write_1(sc, R92S_MAC_PINMUX_CTRL,
1310 R92S_GPIOMUX_EN | SM(R92S_GPIOSEL_GPIO, R92S_GPIOSEL_GPIO_JTAG));
1311 rsu_write_1(sc, R92S_GPIO_IO_SEL,
1312 rsu_read_1(sc, R92S_GPIO_IO_SEL) & ~R92S_GPIO_WPS);
1313 reg = rsu_read_1(sc, R92S_GPIO_CTRL);
1314 if (reg != 0xff && (reg & R92S_GPIO_WPS))
1315 RSU_DPRINTF(sc, RSU_DEBUG_CALIB, "WPS PBC is pushed\n");
1317 /* Read current signal level. */
1318 if (rsu_fw_iocmd(sc, 0xf4000001) == 0) {
1319 sc->sc_currssi = rsu_read_4(sc, R92S_IOCMD_DATA);
1320 RSU_DPRINTF(sc, RSU_DEBUG_CALIB, "%s: RSSI=%d (%d)\n",
1321 __func__, sc->sc_currssi,
1322 rsu_hwrssi_to_rssi(sc, sc->sc_currssi));
1324 if (sc->sc_calibrating)
1325 taskqueue_enqueue_timeout(taskqueue_thread, &sc->calib_task, hz);
1330 rsu_tx_task(void *arg, int pending __unused)
1332 struct rsu_softc *sc = arg;
1339 #define RSU_PWR_UNKNOWN 0x0
1340 #define RSU_PWR_ACTIVE 0x1
1341 #define RSU_PWR_OFF 0x2
1342 #define RSU_PWR_SLEEP 0x3
1345 * Set the current power state.
1347 * The rtlwifi code doesn't do this so aggressively; it
1348 * waits for an idle period after association with
1349 * no traffic before doing this.
1351 * For now - it's on in all states except RUN, and
1352 * in RUN it'll transition to allow sleep.
1355 struct r92s_pwr_cmd {
1358 uint8_t bcn_pass_time;
1362 rsu_set_fw_power_state(struct rsu_softc *sc, int state)
1364 struct r92s_set_pwr_mode cmd;
1365 //struct r92s_pwr_cmd cmd;
1368 RSU_ASSERT_LOCKED(sc);
1370 /* only change state if required */
1371 if (sc->sc_curpwrstate == state)
1374 memset(&cmd, 0, sizeof(cmd));
1377 case RSU_PWR_ACTIVE:
1378 /* Force the hardware awake */
1379 rsu_write_1(sc, R92S_USB_HRPWM,
1380 R92S_USB_HRPWM_PS_ST_ACTIVE | R92S_USB_HRPWM_PS_ALL_ON);
1381 cmd.mode = R92S_PS_MODE_ACTIVE;
1384 cmd.mode = R92S_PS_MODE_DTIM; /* XXX configurable? */
1385 cmd.smart_ps = 1; /* XXX 2 if doing p2p */
1386 cmd.bcn_pass_time = 5; /* in 100mS usb.c, linux/rtlwifi */
1389 cmd.mode = R92S_PS_MODE_RADIOOFF;
1392 device_printf(sc->sc_dev, "%s: unknown ps mode (%d)\n",
1398 RSU_DPRINTF(sc, RSU_DEBUG_RESET,
1399 "%s: setting ps mode to %d (mode %d)\n",
1400 __func__, state, cmd.mode);
1401 error = rsu_fw_cmd(sc, R92S_CMD_SET_PWR_MODE, &cmd, sizeof(cmd));
1403 sc->sc_curpwrstate = state;
1409 rsu_set_led(struct rsu_softc *sc, int on)
1411 rsu_write_1(sc, R92S_LEDCFG,
1412 (rsu_read_1(sc, R92S_LEDCFG) & 0xf0) | (!on << 3));
1416 rsu_monitor_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate,
1419 struct ieee80211com *ic = vap->iv_ic;
1420 struct rsu_softc *sc = ic->ic_softc;
1421 struct rsu_vap *uvp = RSU_VAP(vap);
1423 if (vap->iv_state != nstate) {
1424 IEEE80211_UNLOCK(ic);
1428 case IEEE80211_S_INIT:
1429 sc->sc_vap_is_running = 0;
1432 case IEEE80211_S_RUN:
1433 sc->sc_vap_is_running = 1;
1440 rsu_rxfilter_refresh(sc);
1446 return (uvp->newstate(vap, nstate, arg));
1450 rsu_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1452 struct rsu_vap *uvp = RSU_VAP(vap);
1453 struct ieee80211com *ic = vap->iv_ic;
1454 struct rsu_softc *sc = ic->ic_softc;
1455 struct ieee80211_node *ni;
1456 struct ieee80211_rateset *rs;
1457 enum ieee80211_state ostate;
1458 int error, startcal = 0;
1460 ostate = vap->iv_state;
1461 RSU_DPRINTF(sc, RSU_DEBUG_STATE, "%s: %s -> %s\n",
1463 ieee80211_state_name[ostate],
1464 ieee80211_state_name[nstate]);
1466 IEEE80211_UNLOCK(ic);
1467 if (ostate == IEEE80211_S_RUN) {
1469 /* Stop calibration. */
1470 sc->sc_calibrating = 0;
1472 /* Pause Tx for AC queues. */
1473 rsu_write_1(sc, R92S_TXPAUSE, R92S_TXPAUSE_AC);
1474 usb_pause_mtx(&sc->sc_mtx, USB_MS_TO_TICKS(10));
1477 taskqueue_drain_timeout(taskqueue_thread, &sc->calib_task);
1478 taskqueue_drain(taskqueue_thread, &sc->tx_task);
1480 /* Disassociate from our current BSS. */
1482 usb_pause_mtx(&sc->sc_mtx, USB_MS_TO_TICKS(10));
1484 /* Refresh Rx filter (may be modified by firmware). */
1485 sc->sc_vap_is_running = 0;
1486 rsu_rxfilter_refresh(sc);
1488 /* Reinstall static keys. */
1490 rsu_reinit_static_keys(sc);
1494 case IEEE80211_S_INIT:
1495 (void) rsu_set_fw_power_state(sc, RSU_PWR_ACTIVE);
1497 case IEEE80211_S_AUTH:
1498 ni = ieee80211_ref_node(vap->iv_bss);
1499 (void) rsu_set_fw_power_state(sc, RSU_PWR_ACTIVE);
1500 error = rsu_join_bss(sc, ni);
1501 ieee80211_free_node(ni);
1503 device_printf(sc->sc_dev,
1504 "could not send join command\n");
1507 case IEEE80211_S_RUN:
1508 /* Flush all AC queues. */
1509 rsu_write_1(sc, R92S_TXPAUSE, 0);
1511 ni = ieee80211_ref_node(vap->iv_bss);
1513 /* Indicate highest supported rate. */
1514 ni->ni_txrate = rs->rs_rates[rs->rs_nrates - 1];
1515 (void) rsu_set_fw_power_state(sc, RSU_PWR_SLEEP);
1516 ieee80211_free_node(ni);
1522 if (startcal != 0) {
1523 sc->sc_calibrating = 1;
1524 /* Start periodic calibration. */
1525 taskqueue_enqueue_timeout(taskqueue_thread, &sc->calib_task,
1530 return (uvp->newstate(vap, nstate, arg));
1534 rsu_key_alloc(struct ieee80211vap *vap, struct ieee80211_key *k,
1535 ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
1537 struct rsu_softc *sc = vap->iv_ic->ic_softc;
1540 if (&vap->iv_nw_keys[0] <= k &&
1541 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID]) {
1542 *keyix = ieee80211_crypto_get_key_wepidx(vap, k);
1544 if (vap->iv_opmode != IEEE80211_M_STA) {
1546 /* TODO: obtain keyix from node id */
1548 k->wk_flags |= IEEE80211_KEY_SWCRYPT;
1550 *keyix = R92S_MACID_BSS;
1555 if (isset(sc->keys_bmap, *keyix)) {
1556 device_printf(sc->sc_dev,
1557 "%s: key slot %d is already used!\n",
1562 setbit(sc->keys_bmap, *keyix);
1572 rsu_process_key(struct ieee80211vap *vap, const struct ieee80211_key *k,
1575 struct rsu_softc *sc = vap->iv_ic->ic_softc;
1578 if (k->wk_flags & IEEE80211_KEY_SWCRYPT) {
1583 /* Handle group keys. */
1584 if (&vap->iv_nw_keys[0] <= k &&
1585 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID]) {
1586 KASSERT(k->wk_keyix < nitems(sc->group_keys),
1587 ("keyix %u > %zu\n", k->wk_keyix, nitems(sc->group_keys)));
1590 sc->group_keys[k->wk_keyix] = (set ? k : NULL);
1591 if (!sc->sc_running) {
1592 /* Static keys will be set during device startup. */
1598 ret = rsu_set_key_group(sc, k);
1600 ret = rsu_delete_key(sc, k->wk_keyix);
1607 /* wait for pending key removal */
1608 taskqueue_drain(taskqueue_thread, &sc->del_key_task);
1611 ret = rsu_set_key_pair(sc, k);
1614 RSU_DELKEY_BMAP_LOCK(sc);
1615 setbit(sc->free_keys_bmap, k->wk_keyix);
1616 RSU_DELKEY_BMAP_UNLOCK(sc);
1618 /* workaround ieee80211_node_delucastkey() locking */
1619 taskqueue_enqueue(taskqueue_thread, &sc->del_key_task);
1620 ret = 0; /* fake success */
1627 rsu_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
1629 return (rsu_process_key(vap, k, 1));
1633 rsu_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
1635 return (rsu_process_key(vap, k, 0));
1639 rsu_cam_read(struct rsu_softc *sc, uint8_t addr, uint32_t *val)
1643 rsu_write_4(sc, R92S_CAMCMD,
1644 R92S_CAMCMD_POLLING | SM(R92S_CAMCMD_ADDR, addr));
1645 for (ntries = 0; ntries < 10; ntries++) {
1646 if (!(rsu_read_4(sc, R92S_CAMCMD) & R92S_CAMCMD_POLLING))
1649 usb_pause_mtx(&sc->sc_mtx, USB_MS_TO_TICKS(1));
1652 device_printf(sc->sc_dev,
1653 "%s: cannot read CAM entry at address %02X\n",
1658 *val = rsu_read_4(sc, R92S_CAMREAD);
1664 rsu_cam_write(struct rsu_softc *sc, uint8_t addr, uint32_t data)
1667 rsu_write_4(sc, R92S_CAMWRITE, data);
1668 rsu_write_4(sc, R92S_CAMCMD,
1669 R92S_CAMCMD_POLLING | R92S_CAMCMD_WRITE |
1670 SM(R92S_CAMCMD_ADDR, addr));
1674 rsu_key_check(struct rsu_softc *sc, ieee80211_keyix keyix, int is_valid)
1679 for (ntries = 0; ntries < 20; ntries++) {
1680 usb_pause_mtx(&sc->sc_mtx, USB_MS_TO_TICKS(1));
1682 error = rsu_cam_read(sc, R92S_CAM_CTL0(keyix), &val);
1684 device_printf(sc->sc_dev,
1685 "%s: cannot check key status!\n", __func__);
1688 if (((val & R92S_CAM_VALID) == 0) ^ is_valid)
1692 device_printf(sc->sc_dev,
1693 "%s: key %d is %s marked as valid, rejecting request\n",
1694 __func__, keyix, is_valid ? "not" : "still");
1702 * Map net80211 cipher to RTL8712 security mode.
1705 rsu_crypto_mode(struct rsu_softc *sc, u_int cipher, int keylen)
1708 case IEEE80211_CIPHER_WEP:
1709 return keylen < 8 ? R92S_KEY_ALGO_WEP40 : R92S_KEY_ALGO_WEP104;
1710 case IEEE80211_CIPHER_TKIP:
1711 return R92S_KEY_ALGO_TKIP;
1712 case IEEE80211_CIPHER_AES_CCM:
1713 return R92S_KEY_ALGO_AES;
1715 device_printf(sc->sc_dev, "unknown cipher %d\n", cipher);
1716 return R92S_KEY_ALGO_INVALID;
1721 rsu_set_key_group(struct rsu_softc *sc, const struct ieee80211_key *k)
1723 struct r92s_fw_cmd_set_key key;
1727 RSU_ASSERT_LOCKED(sc);
1729 /* Map net80211 cipher to HW crypto algorithm. */
1730 algo = rsu_crypto_mode(sc, k->wk_cipher->ic_cipher, k->wk_keylen);
1731 if (algo == R92S_KEY_ALGO_INVALID)
1734 memset(&key, 0, sizeof(key));
1736 key.cam_id = k->wk_keyix;
1737 key.grpkey = (k->wk_flags & IEEE80211_KEY_GROUP) != 0;
1738 memcpy(key.key, k->wk_key, MIN(k->wk_keylen, sizeof(key.key)));
1740 RSU_DPRINTF(sc, RSU_DEBUG_KEY | RSU_DEBUG_FWCMD,
1741 "%s: keyix %u, group %u, algo %u/%u, flags %04X, len %u, "
1742 "macaddr %s\n", __func__, key.cam_id, key.grpkey,
1743 k->wk_cipher->ic_cipher, key.algo, k->wk_flags, k->wk_keylen,
1744 ether_sprintf(k->wk_macaddr));
1746 error = rsu_fw_cmd(sc, R92S_CMD_SET_KEY, &key, sizeof(key));
1748 device_printf(sc->sc_dev,
1749 "%s: cannot send firmware command, error %d\n",
1754 return (rsu_key_check(sc, k->wk_keyix, 1));
1758 rsu_set_key_pair(struct rsu_softc *sc, const struct ieee80211_key *k)
1760 struct r92s_fw_cmd_set_key_mac key;
1764 RSU_ASSERT_LOCKED(sc);
1766 if (!sc->sc_running)
1769 /* Map net80211 cipher to HW crypto algorithm. */
1770 algo = rsu_crypto_mode(sc, k->wk_cipher->ic_cipher, k->wk_keylen);
1771 if (algo == R92S_KEY_ALGO_INVALID)
1774 memset(&key, 0, sizeof(key));
1776 memcpy(key.macaddr, k->wk_macaddr, sizeof(key.macaddr));
1777 memcpy(key.key, k->wk_key, MIN(k->wk_keylen, sizeof(key.key)));
1779 RSU_DPRINTF(sc, RSU_DEBUG_KEY | RSU_DEBUG_FWCMD,
1780 "%s: keyix %u, algo %u/%u, flags %04X, len %u, macaddr %s\n",
1781 __func__, k->wk_keyix, k->wk_cipher->ic_cipher, key.algo,
1782 k->wk_flags, k->wk_keylen, ether_sprintf(key.macaddr));
1784 error = rsu_fw_cmd(sc, R92S_CMD_SET_STA_KEY, &key, sizeof(key));
1786 device_printf(sc->sc_dev,
1787 "%s: cannot send firmware command, error %d\n",
1792 return (rsu_key_check(sc, k->wk_keyix, 1));
1796 rsu_reinit_static_keys(struct rsu_softc *sc)
1800 for (i = 0; i < nitems(sc->group_keys); i++) {
1801 if (sc->group_keys[i] != NULL) {
1802 error = rsu_set_key_group(sc, sc->group_keys[i]);
1804 device_printf(sc->sc_dev,
1805 "%s: failed to set static key %d, "
1806 "error %d\n", __func__, i, error);
1816 rsu_delete_key(struct rsu_softc *sc, ieee80211_keyix keyix)
1818 struct r92s_fw_cmd_set_key key;
1822 RSU_ASSERT_LOCKED(sc);
1824 if (!sc->sc_running)
1827 /* check if it was automatically removed by firmware */
1828 error = rsu_cam_read(sc, R92S_CAM_CTL0(keyix), &val);
1829 if (error == 0 && (val & R92S_CAM_VALID) == 0) {
1830 RSU_DPRINTF(sc, RSU_DEBUG_KEY,
1831 "%s: key %u does not exist\n", __func__, keyix);
1832 clrbit(sc->keys_bmap, keyix);
1836 memset(&key, 0, sizeof(key));
1839 RSU_DPRINTF(sc, RSU_DEBUG_KEY | RSU_DEBUG_FWCMD,
1840 "%s: removing key %u\n", __func__, key.cam_id);
1842 error = rsu_fw_cmd(sc, R92S_CMD_SET_KEY, &key, sizeof(key));
1844 device_printf(sc->sc_dev,
1845 "%s: cannot send firmware command, error %d\n",
1850 usb_pause_mtx(&sc->sc_mtx, USB_MS_TO_TICKS(5));
1853 * Clear 'valid' bit manually (cannot be done via firmware command).
1854 * Used for key check + when firmware command cannot be sent.
1857 rsu_cam_write(sc, R92S_CAM_CTL0(keyix), 0);
1859 clrbit(sc->keys_bmap, keyix);
1861 return (rsu_key_check(sc, keyix, 0));
1865 rsu_delete_key_pair_cb(void *arg, int pending __unused)
1867 struct rsu_softc *sc = arg;
1870 RSU_DELKEY_BMAP_LOCK(sc);
1871 for (i = IEEE80211_WEP_NKID; i < R92S_CAM_ENTRY_LIMIT; i++) {
1872 if (isset(sc->free_keys_bmap, i)) {
1873 RSU_DELKEY_BMAP_UNLOCK(sc);
1876 RSU_DPRINTF(sc, RSU_DEBUG_KEY,
1877 "%s: calling rsu_delete_key() with keyix = %d\n",
1879 (void) rsu_delete_key(sc, i);
1882 RSU_DELKEY_BMAP_LOCK(sc);
1883 clrbit(sc->free_keys_bmap, i);
1885 /* bmap can be changed */
1886 i = IEEE80211_WEP_NKID - 1;
1890 RSU_DELKEY_BMAP_UNLOCK(sc);
1894 rsu_site_survey(struct rsu_softc *sc, struct ieee80211_scan_ssid *ssid)
1896 struct r92s_fw_cmd_sitesurvey cmd;
1898 RSU_ASSERT_LOCKED(sc);
1900 memset(&cmd, 0, sizeof(cmd));
1901 /* TODO: passive channels? */
1902 if (sc->sc_active_scan)
1903 cmd.active = htole32(1);
1904 cmd.limit = htole32(48);
1907 sc->sc_extra_scan = 1;
1908 cmd.ssidlen = htole32(ssid->len);
1909 memcpy(cmd.ssid, ssid->ssid, ssid->len);
1912 if (rsu_debug & (RSU_DEBUG_SCAN | RSU_DEBUG_FWCMD)) {
1913 device_printf(sc->sc_dev,
1914 "sending site survey command, active %d",
1915 le32toh(cmd.active));
1918 ieee80211_print_essid(cmd.ssid, le32toh(cmd.ssidlen));
1923 return (rsu_fw_cmd(sc, R92S_CMD_SITE_SURVEY, &cmd, sizeof(cmd)));
1927 rsu_join_bss(struct rsu_softc *sc, struct ieee80211_node *ni)
1929 struct ieee80211com *ic = &sc->sc_ic;
1930 struct ieee80211vap *vap = ni->ni_vap;
1931 struct ndis_wlan_bssid_ex *bss;
1932 struct ndis_802_11_fixed_ies *fixed;
1933 struct r92s_fw_cmd_auth auth;
1934 uint8_t buf[sizeof(*bss) + 128] __aligned(4);
1939 RSU_ASSERT_LOCKED(sc);
1941 /* Let the FW decide the opmode based on the capinfo field. */
1942 opmode = NDIS802_11AUTOUNKNOWN;
1943 RSU_DPRINTF(sc, RSU_DEBUG_RESET,
1944 "%s: setting operating mode to %d\n",
1946 error = rsu_fw_cmd(sc, R92S_CMD_SET_OPMODE, &opmode, sizeof(opmode));
1950 memset(&auth, 0, sizeof(auth));
1951 if (vap->iv_flags & IEEE80211_F_WPA) {
1952 auth.mode = R92S_AUTHMODE_WPA;
1953 auth.dot1x = (ni->ni_authmode == IEEE80211_AUTH_8021X);
1955 auth.mode = R92S_AUTHMODE_OPEN;
1956 RSU_DPRINTF(sc, RSU_DEBUG_RESET,
1957 "%s: setting auth mode to %d\n",
1958 __func__, auth.mode);
1959 error = rsu_fw_cmd(sc, R92S_CMD_SET_AUTH, &auth, sizeof(auth));
1963 memset(buf, 0, sizeof(buf));
1964 bss = (struct ndis_wlan_bssid_ex *)buf;
1965 IEEE80211_ADDR_COPY(bss->macaddr, ni->ni_bssid);
1966 bss->ssid.ssidlen = htole32(ni->ni_esslen);
1967 memcpy(bss->ssid.ssid, ni->ni_essid, ni->ni_esslen);
1968 if (vap->iv_flags & (IEEE80211_F_PRIVACY | IEEE80211_F_WPA))
1969 bss->privacy = htole32(1);
1970 bss->rssi = htole32(ni->ni_avgrssi);
1971 if (ic->ic_curmode == IEEE80211_MODE_11B)
1972 bss->networktype = htole32(NDIS802_11DS);
1974 bss->networktype = htole32(NDIS802_11OFDM24);
1975 bss->config.len = htole32(sizeof(bss->config));
1976 bss->config.bintval = htole32(ni->ni_intval);
1977 bss->config.dsconfig = htole32(ieee80211_chan2ieee(ic, ni->ni_chan));
1978 bss->inframode = htole32(NDIS802_11INFRASTRUCTURE);
1979 /* XXX verify how this is supposed to look! */
1980 memcpy(bss->supprates, ni->ni_rates.rs_rates,
1981 ni->ni_rates.rs_nrates);
1982 /* Write the fixed fields of the beacon frame. */
1983 fixed = (struct ndis_802_11_fixed_ies *)&bss[1];
1984 memcpy(&fixed->tstamp, ni->ni_tstamp.data, 8);
1985 fixed->bintval = htole16(ni->ni_intval);
1986 fixed->capabilities = htole16(ni->ni_capinfo);
1987 /* Write IEs to be included in the association request. */
1988 frm = (uint8_t *)&fixed[1];
1989 frm = ieee80211_add_rsn(frm, vap);
1990 frm = ieee80211_add_wpa(frm, vap);
1991 frm = ieee80211_add_qos(frm, ni);
1992 if ((ic->ic_flags & IEEE80211_F_WME) &&
1993 (ni->ni_ies.wme_ie != NULL))
1994 frm = ieee80211_add_wme_info(frm, &ic->ic_wme);
1995 if (ni->ni_flags & IEEE80211_NODE_HT) {
1996 frm = ieee80211_add_htcap(frm, ni);
1997 frm = ieee80211_add_htinfo(frm, ni);
1999 bss->ieslen = htole32(frm - (uint8_t *)fixed);
2000 bss->len = htole32(((frm - buf) + 3) & ~3);
2001 RSU_DPRINTF(sc, RSU_DEBUG_RESET | RSU_DEBUG_FWCMD,
2002 "%s: sending join bss command to %s chan %d\n",
2004 ether_sprintf(bss->macaddr), le32toh(bss->config.dsconfig));
2005 return (rsu_fw_cmd(sc, R92S_CMD_JOIN_BSS, buf, sizeof(buf)));
2009 rsu_disconnect(struct rsu_softc *sc)
2011 uint32_t zero = 0; /* :-) */
2013 /* Disassociate from our current BSS. */
2014 RSU_DPRINTF(sc, RSU_DEBUG_STATE | RSU_DEBUG_FWCMD,
2015 "%s: sending disconnect command\n", __func__);
2016 return (rsu_fw_cmd(sc, R92S_CMD_DISCONNECT, &zero, sizeof(zero)));
2020 * Map the hardware provided RSSI value to a signal level.
2021 * For the most part it's just something we divide by and cap
2022 * so it doesn't overflow the representation by net80211.
2025 rsu_hwrssi_to_rssi(struct rsu_softc *sc, int hw_rssi)
2037 CTASSERT(MCLBYTES > sizeof(struct ieee80211_frame));
2040 rsu_event_survey(struct rsu_softc *sc, uint8_t *buf, int len)
2042 struct ieee80211com *ic = &sc->sc_ic;
2043 struct ieee80211_frame *wh;
2044 struct ndis_wlan_bssid_ex *bss;
2045 struct ieee80211_rx_stats rxs;
2050 if (__predict_false(len < sizeof(*bss)))
2052 bss = (struct ndis_wlan_bssid_ex *)buf;
2053 ieslen = le32toh(bss->ieslen);
2054 /* range check length of information element */
2055 if (__predict_false(ieslen > (uint32_t)(len - sizeof(*bss))))
2058 RSU_DPRINTF(sc, RSU_DEBUG_SCAN,
2059 "%s: found BSS %s: len=%d chan=%d inframode=%d "
2060 "networktype=%d privacy=%d, RSSI=%d\n",
2062 ether_sprintf(bss->macaddr), ieslen,
2063 le32toh(bss->config.dsconfig), le32toh(bss->inframode),
2064 le32toh(bss->networktype), le32toh(bss->privacy),
2065 le32toh(bss->rssi));
2067 /* Build a fake beacon frame to let net80211 do all the parsing. */
2068 /* XXX TODO: just call the new scan API methods! */
2069 if (__predict_false(ieslen > (size_t)(MCLBYTES - sizeof(*wh))))
2071 pktlen = sizeof(*wh) + ieslen;
2072 m = m_get2(pktlen, M_NOWAIT, MT_DATA, M_PKTHDR);
2073 if (__predict_false(m == NULL))
2075 wh = mtod(m, struct ieee80211_frame *);
2076 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
2077 IEEE80211_FC0_SUBTYPE_BEACON;
2078 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2079 USETW(wh->i_dur, 0);
2080 IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr);
2081 IEEE80211_ADDR_COPY(wh->i_addr2, bss->macaddr);
2082 IEEE80211_ADDR_COPY(wh->i_addr3, bss->macaddr);
2083 *(uint16_t *)wh->i_seq = 0;
2084 memcpy(&wh[1], (uint8_t *)&bss[1], ieslen);
2086 /* Finalize mbuf. */
2087 m->m_pkthdr.len = m->m_len = pktlen;
2089 /* Set channel flags for input path */
2090 bzero(&rxs, sizeof(rxs));
2091 rxs.r_flags |= IEEE80211_R_IEEE | IEEE80211_R_FREQ;
2092 rxs.r_flags |= IEEE80211_R_NF | IEEE80211_R_RSSI;
2093 rxs.c_ieee = le32toh(bss->config.dsconfig);
2094 rxs.c_freq = ieee80211_ieee2mhz(rxs.c_ieee, IEEE80211_CHAN_2GHZ);
2095 /* This is a number from 0..100; so let's just divide it down a bit */
2096 rxs.c_rssi = le32toh(bss->rssi) / 2;
2098 if (ieee80211_add_rx_params(m, &rxs) == 0)
2101 /* XXX avoid a LOR */
2103 ieee80211_input_mimo_all(ic, m);
2108 rsu_event_join_bss(struct rsu_softc *sc, uint8_t *buf, int len)
2110 struct ieee80211com *ic = &sc->sc_ic;
2111 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2112 struct ieee80211_node *ni = vap->iv_bss;
2113 struct r92s_event_join_bss *rsp;
2117 if (__predict_false(len < sizeof(*rsp)))
2119 rsp = (struct r92s_event_join_bss *)buf;
2120 res = (int)le32toh(rsp->join_res);
2122 RSU_DPRINTF(sc, RSU_DEBUG_STATE | RSU_DEBUG_FWCMD,
2123 "%s: Rx join BSS event len=%d res=%d\n",
2124 __func__, len, res);
2127 * XXX Don't do this; there's likely a better way to tell
2128 * the caller we failed.
2132 ieee80211_new_state(vap, IEEE80211_S_SCAN, -1);
2137 tmp = le32toh(rsp->associd);
2138 if (tmp >= vap->iv_max_aid) {
2139 RSU_DPRINTF(sc, RSU_DEBUG_ANY, "Assoc ID overflow\n");
2142 RSU_DPRINTF(sc, RSU_DEBUG_STATE | RSU_DEBUG_FWCMD,
2143 "%s: associated with %s associd=%d\n",
2144 __func__, ether_sprintf(rsp->bss.macaddr), tmp);
2145 /* XXX is this required? What's the top two bits for again? */
2146 ni->ni_associd = tmp | 0xc000;
2148 /* Refresh Rx filter (was changed by firmware). */
2149 sc->sc_vap_is_running = 1;
2150 rsu_rxfilter_refresh(sc);
2153 ieee80211_new_state(vap, IEEE80211_S_RUN,
2154 IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
2159 rsu_event_addba_req_report(struct rsu_softc *sc, uint8_t *buf, int len)
2161 struct ieee80211com *ic = &sc->sc_ic;
2162 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2163 struct r92s_add_ba_event *ba = (void *) buf;
2164 struct ieee80211_node *ni;
2166 if (len < sizeof(*ba)) {
2167 device_printf(sc->sc_dev, "%s: short read (%d)\n", __func__, len);
2174 RSU_DPRINTF(sc, RSU_DEBUG_AMPDU, "%s: mac=%s, tid=%d, ssn=%d\n",
2176 ether_sprintf(ba->mac_addr),
2178 (int) le16toh(ba->ssn));
2180 /* XXX do node lookup; this is STA specific */
2182 ni = ieee80211_ref_node(vap->iv_bss);
2183 ieee80211_ampdu_rx_start_ext(ni, ba->tid, le16toh(ba->ssn) >> 4, 32);
2184 ieee80211_free_node(ni);
2188 rsu_rx_event(struct rsu_softc *sc, uint8_t code, uint8_t *buf, int len)
2190 struct ieee80211com *ic = &sc->sc_ic;
2191 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2193 RSU_DPRINTF(sc, RSU_DEBUG_RX | RSU_DEBUG_FWCMD,
2194 "%s: Rx event code=%d len=%d\n", __func__, code, len);
2196 case R92S_EVT_SURVEY:
2197 rsu_event_survey(sc, buf, len);
2199 case R92S_EVT_SURVEY_DONE:
2200 RSU_DPRINTF(sc, RSU_DEBUG_SCAN,
2201 "%s: %s scan done, found %d BSS\n",
2202 __func__, sc->sc_extra_scan ? "direct" : "broadcast",
2203 le32toh(*(uint32_t *)buf));
2204 if (sc->sc_extra_scan == 1) {
2205 /* Send broadcast probe request. */
2206 sc->sc_extra_scan = 0;
2207 if (vap != NULL && rsu_site_survey(sc, NULL) != 0) {
2209 ieee80211_cancel_scan(vap);
2216 ieee80211_scan_done(vap);
2220 case R92S_EVT_JOIN_BSS:
2221 if (vap->iv_state == IEEE80211_S_AUTH)
2222 rsu_event_join_bss(sc, buf, len);
2224 case R92S_EVT_DEL_STA:
2225 RSU_DPRINTF(sc, RSU_DEBUG_FWCMD | RSU_DEBUG_STATE,
2226 "%s: disassociated from %s\n", __func__,
2227 ether_sprintf(buf));
2228 if (vap->iv_state == IEEE80211_S_RUN &&
2229 IEEE80211_ADDR_EQ(vap->iv_bss->ni_bssid, buf)) {
2231 ieee80211_new_state(vap, IEEE80211_S_SCAN, -1);
2235 case R92S_EVT_WPS_PBC:
2236 RSU_DPRINTF(sc, RSU_DEBUG_RX | RSU_DEBUG_FWCMD,
2237 "%s: WPS PBC pushed.\n", __func__);
2239 case R92S_EVT_FWDBG:
2241 RSU_DPRINTF(sc, RSU_DEBUG_FWDBG, "FWDBG: %s\n", (char *)buf);
2243 case R92S_EVT_ADDBA_REQ_REPORT:
2244 rsu_event_addba_req_report(sc, buf, len);
2247 device_printf(sc->sc_dev, "%s: unhandled code (%d)\n", __func__, code);
2253 rsu_rx_multi_event(struct rsu_softc *sc, uint8_t *buf, int len)
2255 struct r92s_fw_cmd_hdr *cmd;
2258 RSU_DPRINTF(sc, RSU_DEBUG_RX, "%s: Rx events len=%d\n", __func__, len);
2260 /* Skip Rx status. */
2261 buf += sizeof(struct r92s_rx_stat);
2262 len -= sizeof(struct r92s_rx_stat);
2264 /* Process all events. */
2266 /* Check that command header fits. */
2267 if (__predict_false(len < sizeof(*cmd)))
2269 cmd = (struct r92s_fw_cmd_hdr *)buf;
2270 /* Check that command payload fits. */
2271 cmdsz = le16toh(cmd->len);
2272 if (__predict_false(len < sizeof(*cmd) + cmdsz))
2275 /* Process firmware event. */
2276 rsu_rx_event(sc, cmd->code, (uint8_t *)&cmd[1], cmdsz);
2278 if (!(cmd->seq & R92S_FW_CMD_MORE))
2280 buf += sizeof(*cmd) + cmdsz;
2281 len -= sizeof(*cmd) + cmdsz;
2286 rsu_get_rssi(struct rsu_softc *sc, int rate, void *physt)
2288 static const int8_t cckoff[] = { 14, -2, -20, -40 };
2289 struct r92s_rx_phystat *phy;
2290 struct r92s_rx_cck *cck;
2295 cck = (struct r92s_rx_cck *)physt;
2296 rpt = (cck->agc_rpt >> 6) & 0x3;
2297 rssi = cck->agc_rpt & 0x3e;
2298 rssi = cckoff[rpt] - rssi;
2299 } else { /* OFDM/HT. */
2300 phy = (struct r92s_rx_phystat *)physt;
2301 rssi = ((le32toh(phy->phydw1) >> 1) & 0x7f) - 106;
2306 static struct mbuf *
2307 rsu_rx_copy_to_mbuf(struct rsu_softc *sc, struct r92s_rx_stat *stat,
2310 struct ieee80211com *ic = &sc->sc_ic;
2315 rxdw0 = le32toh(stat->rxdw0);
2316 if (__predict_false(rxdw0 & (R92S_RXDW0_CRCERR | R92S_RXDW0_ICVERR))) {
2317 RSU_DPRINTF(sc, RSU_DEBUG_RX,
2318 "%s: RX flags error (%s)\n", __func__,
2319 rxdw0 & R92S_RXDW0_CRCERR ? "CRC" : "ICV");
2323 pktlen = MS(rxdw0, R92S_RXDW0_PKTLEN);
2324 if (__predict_false(pktlen < sizeof (struct ieee80211_frame_ack))) {
2325 RSU_DPRINTF(sc, RSU_DEBUG_RX,
2326 "%s: frame is too short: %d\n", __func__, pktlen);
2330 m = m_get2(totlen, M_NOWAIT, MT_DATA, M_PKTHDR);
2331 if (__predict_false(m == NULL)) {
2332 device_printf(sc->sc_dev,
2333 "%s: could not allocate RX mbuf, totlen %d\n",
2338 /* Finalize mbuf. */
2339 memcpy(mtod(m, uint8_t *), (uint8_t *)stat, totlen);
2340 m->m_pkthdr.len = m->m_len = totlen;
2344 counter_u64_add(ic->ic_ierrors, 1);
2349 rsu_get_tsf_low(struct rsu_softc *sc)
2351 return (rsu_read_4(sc, R92S_TSFTR));
2355 rsu_get_tsf_high(struct rsu_softc *sc)
2357 return (rsu_read_4(sc, R92S_TSFTR + 4));
2360 static struct ieee80211_node *
2361 rsu_rx_frame(struct rsu_softc *sc, struct mbuf *m)
2363 struct ieee80211com *ic = &sc->sc_ic;
2364 struct ieee80211_frame_min *wh;
2365 struct ieee80211_rx_stats rxs;
2366 struct r92s_rx_stat *stat;
2367 uint32_t rxdw0, rxdw3;
2368 uint8_t cipher, rate;
2372 stat = mtod(m, struct r92s_rx_stat *);
2373 rxdw0 = le32toh(stat->rxdw0);
2374 rxdw3 = le32toh(stat->rxdw3);
2376 rate = MS(rxdw3, R92S_RXDW3_RATE);
2377 cipher = MS(rxdw0, R92S_RXDW0_CIPHER);
2378 infosz = MS(rxdw0, R92S_RXDW0_INFOSZ) * 8;
2380 /* Get RSSI from PHY status descriptor if present. */
2381 if (infosz != 0 && (rxdw0 & R92S_RXDW0_PHYST))
2382 rssi = rsu_get_rssi(sc, rate, &stat[1]);
2384 /* Cheat and get the last calibrated RSSI */
2385 rssi = rsu_hwrssi_to_rssi(sc, sc->sc_currssi);
2388 /* Hardware does Rx TCP checksum offload. */
2390 * This flag can be set for some other
2391 * (e.g., EAPOL) frame types, so don't rely on it.
2393 if (rxdw3 & R92S_RXDW3_TCPCHKVALID) {
2394 RSU_DPRINTF(sc, RSU_DEBUG_RX,
2395 "%s: TCP/IP checksums: %schecked / %schecked\n",
2397 (rxdw3 & R92S_RXDW3_TCPCHKRPT) ? "" : "not ",
2398 (rxdw3 & R92S_RXDW3_IPCHKRPT) ? "" : "not ");
2401 * 'IP header checksum valid' bit will not be set if
2402 * the frame was not checked / has incorrect checksum /
2403 * does not have checksum (IPv6).
2405 * NB: if DF bit is not set then frame will not be checked.
2407 if (rxdw3 & R92S_RXDW3_IPCHKRPT) {
2408 m->m_pkthdr.csum_flags = CSUM_IP_CHECKED;
2409 m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
2413 * This is independent of the above check.
2415 if (rxdw3 & R92S_RXDW3_TCPCHKRPT) {
2416 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID;
2417 m->m_pkthdr.csum_flags |= CSUM_PSEUDO_HDR;
2418 m->m_pkthdr.csum_data = 0xffff;
2424 /* Set channel flags for input path */
2425 bzero(&rxs, sizeof(rxs));
2428 rxs.r_flags |= IEEE80211_R_NF | IEEE80211_R_RSSI;
2434 rxs.c_rate = ridx2rate[rate];
2435 if (RSU_RATE_IS_CCK(rate))
2436 rxs.c_pktflags |= IEEE80211_RX_F_CCK;
2438 rxs.c_pktflags |= IEEE80211_RX_F_OFDM;
2440 rxs.c_rate = IEEE80211_RATE_MCS | (rate - 12);
2441 rxs.c_pktflags |= IEEE80211_RX_F_HT;
2444 if (ieee80211_radiotap_active(ic)) {
2445 struct rsu_rx_radiotap_header *tap = &sc->sc_rxtap;
2447 /* Map HW rate index to 802.11 rate. */
2448 tap->wr_flags = 0; /* TODO */
2449 tap->wr_tsft = rsu_get_tsf_high(sc);
2450 if (le32toh(stat->tsf_low) > rsu_get_tsf_low(sc))
2452 tap->wr_tsft = (uint64_t)htole32(tap->wr_tsft) << 32;
2453 tap->wr_tsft += stat->tsf_low;
2455 tap->wr_rate = rxs.c_rate;
2456 tap->wr_dbm_antsignal = rssi;
2457 tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
2458 tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
2461 (void) ieee80211_add_rx_params(m, &rxs);
2463 /* Drop descriptor. */
2464 m_adj(m, sizeof(*stat) + infosz);
2465 wh = mtod(m, struct ieee80211_frame_min *);
2466 if ((wh->i_fc[1] & IEEE80211_FC1_PROTECTED) &&
2467 cipher != R92S_KEY_ALGO_NONE) {
2468 m->m_flags |= M_WEP;
2471 RSU_DPRINTF(sc, RSU_DEBUG_RX,
2472 "%s: Rx frame len %d, rate %d, infosz %d\n",
2473 __func__, m->m_len, rate, infosz);
2475 if (m->m_len >= sizeof(*wh))
2476 return (ieee80211_find_rxnode(ic, wh));
2481 static struct mbuf *
2482 rsu_rx_multi_frame(struct rsu_softc *sc, uint8_t *buf, int len)
2484 struct r92s_rx_stat *stat;
2486 int totlen, pktlen, infosz, npkts;
2487 struct mbuf *m, *m0 = NULL, *prevm = NULL;
2490 * don't pass packets to the ieee80211 framework if the driver isn't
2493 if (!sc->sc_running)
2496 /* Get the number of encapsulated frames. */
2497 stat = (struct r92s_rx_stat *)buf;
2498 npkts = MS(le32toh(stat->rxdw2), R92S_RXDW2_PKTCNT);
2499 RSU_DPRINTF(sc, RSU_DEBUG_RX,
2500 "%s: Rx %d frames in one chunk\n", __func__, npkts);
2502 /* Process all of them. */
2503 while (npkts-- > 0) {
2504 if (__predict_false(len < sizeof(*stat)))
2506 stat = (struct r92s_rx_stat *)buf;
2507 rxdw0 = le32toh(stat->rxdw0);
2509 pktlen = MS(rxdw0, R92S_RXDW0_PKTLEN);
2510 if (__predict_false(pktlen == 0))
2513 infosz = MS(rxdw0, R92S_RXDW0_INFOSZ) * 8;
2515 /* Make sure everything fits in xfer. */
2516 totlen = sizeof(*stat) + infosz + pktlen;
2517 if (__predict_false(totlen > len))
2520 /* Process 802.11 frame. */
2521 m = rsu_rx_copy_to_mbuf(sc, stat, totlen);
2530 /* Next chunk is 128-byte aligned. */
2531 totlen = (totlen + 127) & ~127;
2539 static struct mbuf *
2540 rsu_rxeof(struct usb_xfer *xfer, struct rsu_data *data)
2542 struct rsu_softc *sc = data->sc;
2543 struct ieee80211com *ic = &sc->sc_ic;
2544 struct r92s_rx_stat *stat;
2547 usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
2549 if (__predict_false(len < sizeof(*stat))) {
2550 RSU_DPRINTF(sc, RSU_DEBUG_RX, "xfer too short %d\n", len);
2551 counter_u64_add(ic->ic_ierrors, 1);
2554 /* Determine if it is a firmware C2H event or an 802.11 frame. */
2555 stat = (struct r92s_rx_stat *)data->buf;
2556 if ((le32toh(stat->rxdw1) & 0x1ff) == 0x1ff) {
2557 rsu_rx_multi_event(sc, data->buf, len);
2558 /* No packets to process. */
2561 return (rsu_rx_multi_frame(sc, data->buf, len));
2565 rsu_bulk_rx_callback(struct usb_xfer *xfer, usb_error_t error)
2567 struct rsu_softc *sc = usbd_xfer_softc(xfer);
2568 struct ieee80211com *ic = &sc->sc_ic;
2569 struct ieee80211_node *ni;
2570 struct mbuf *m = NULL, *next;
2571 struct rsu_data *data;
2573 RSU_ASSERT_LOCKED(sc);
2575 switch (USB_GET_STATE(xfer)) {
2576 case USB_ST_TRANSFERRED:
2577 data = STAILQ_FIRST(&sc->sc_rx_active);
2580 STAILQ_REMOVE_HEAD(&sc->sc_rx_active, next);
2581 m = rsu_rxeof(xfer, data);
2582 STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next);
2586 data = STAILQ_FIRST(&sc->sc_rx_inactive);
2588 KASSERT(m == NULL, ("mbuf isn't NULL"));
2591 STAILQ_REMOVE_HEAD(&sc->sc_rx_inactive, next);
2592 STAILQ_INSERT_TAIL(&sc->sc_rx_active, data, next);
2593 usbd_xfer_set_frame_data(xfer, 0, data->buf,
2594 usbd_xfer_max_len(xfer));
2595 usbd_transfer_submit(xfer);
2597 * To avoid LOR we should unlock our private mutex here to call
2598 * ieee80211_input() because here is at the end of a USB
2599 * callback and safe to unlock.
2605 ni = rsu_rx_frame(sc, m);
2609 if (ni->ni_flags & IEEE80211_NODE_HT)
2610 m->m_flags |= M_AMPDU;
2611 (void)ieee80211_input_mimo(ni, m);
2612 ieee80211_free_node(ni);
2614 (void)ieee80211_input_mimo_all(ic, m);
2621 /* needs it to the inactive queue due to a error. */
2622 data = STAILQ_FIRST(&sc->sc_rx_active);
2624 STAILQ_REMOVE_HEAD(&sc->sc_rx_active, next);
2625 STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next);
2627 if (error != USB_ERR_CANCELLED) {
2628 usbd_xfer_set_stall(xfer);
2629 counter_u64_add(ic->ic_ierrors, 1);
2638 rsu_txeof(struct usb_xfer *xfer, struct rsu_data *data)
2641 struct rsu_softc *sc = usbd_xfer_softc(xfer);
2644 RSU_DPRINTF(sc, RSU_DEBUG_TXDONE, "%s: called; data=%p\n",
2650 ieee80211_tx_complete(data->ni, data->m, 0);
2657 rsu_bulk_tx_callback_sub(struct usb_xfer *xfer, usb_error_t error,
2660 struct rsu_softc *sc = usbd_xfer_softc(xfer);
2661 struct ieee80211com *ic = &sc->sc_ic;
2662 struct rsu_data *data;
2664 RSU_ASSERT_LOCKED(sc);
2666 switch (USB_GET_STATE(xfer)) {
2667 case USB_ST_TRANSFERRED:
2668 data = STAILQ_FIRST(&sc->sc_tx_active[which]);
2671 RSU_DPRINTF(sc, RSU_DEBUG_TXDONE, "%s: transfer done %p\n",
2673 STAILQ_REMOVE_HEAD(&sc->sc_tx_active[which], next);
2674 rsu_txeof(xfer, data);
2675 rsu_freebuf(sc, data);
2679 data = STAILQ_FIRST(&sc->sc_tx_pending[which]);
2681 RSU_DPRINTF(sc, RSU_DEBUG_TXDONE,
2682 "%s: empty pending queue sc %p\n", __func__, sc);
2685 STAILQ_REMOVE_HEAD(&sc->sc_tx_pending[which], next);
2686 STAILQ_INSERT_TAIL(&sc->sc_tx_active[which], data, next);
2687 usbd_xfer_set_frame_data(xfer, 0, data->buf, data->buflen);
2688 RSU_DPRINTF(sc, RSU_DEBUG_TXDONE,
2689 "%s: submitting transfer %p\n",
2692 usbd_transfer_submit(xfer);
2695 data = STAILQ_FIRST(&sc->sc_tx_active[which]);
2697 STAILQ_REMOVE_HEAD(&sc->sc_tx_active[which], next);
2698 rsu_txeof(xfer, data);
2699 rsu_freebuf(sc, data);
2701 counter_u64_add(ic->ic_oerrors, 1);
2703 if (error != USB_ERR_CANCELLED) {
2704 usbd_xfer_set_stall(xfer);
2711 * XXX TODO: if the queue is low, flush out FF TX frames.
2712 * Remember to unlock the driver for now; net80211 doesn't
2718 rsu_bulk_tx_callback_be_bk(struct usb_xfer *xfer, usb_error_t error)
2720 struct rsu_softc *sc = usbd_xfer_softc(xfer);
2722 rsu_bulk_tx_callback_sub(xfer, error, RSU_BULK_TX_BE_BK);
2724 /* This kicks the TX taskqueue */
2729 rsu_bulk_tx_callback_vi_vo(struct usb_xfer *xfer, usb_error_t error)
2731 struct rsu_softc *sc = usbd_xfer_softc(xfer);
2733 rsu_bulk_tx_callback_sub(xfer, error, RSU_BULK_TX_VI_VO);
2735 /* This kicks the TX taskqueue */
2740 rsu_bulk_tx_callback_h2c(struct usb_xfer *xfer, usb_error_t error)
2742 struct rsu_softc *sc = usbd_xfer_softc(xfer);
2744 rsu_bulk_tx_callback_sub(xfer, error, RSU_BULK_TX_H2C);
2746 /* This kicks the TX taskqueue */
2751 * Transmit the given frame.
2753 * This doesn't free the node or mbuf upon failure.
2756 rsu_tx_start(struct rsu_softc *sc, struct ieee80211_node *ni,
2757 struct mbuf *m0, struct rsu_data *data)
2759 struct ieee80211com *ic = &sc->sc_ic;
2760 struct ieee80211vap *vap = ni->ni_vap;
2761 struct ieee80211_frame *wh;
2762 struct ieee80211_key *k = NULL;
2763 struct r92s_tx_desc *txd;
2764 uint8_t type, cipher;
2771 RSU_ASSERT_LOCKED(sc);
2773 wh = mtod(m0, struct ieee80211_frame *);
2774 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
2776 RSU_DPRINTF(sc, RSU_DEBUG_TX, "%s: data=%p, m=%p\n",
2777 __func__, data, m0);
2779 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
2780 k = ieee80211_crypto_encap(ni, m0);
2782 device_printf(sc->sc_dev,
2783 "ieee80211_crypto_encap returns NULL.\n");
2784 /* XXX we don't expect the fragmented frames */
2787 wh = mtod(m0, struct ieee80211_frame *);
2789 /* If we have QoS then use it */
2790 /* XXX TODO: mbuf WME/PRI versus TID? */
2791 if (IEEE80211_QOS_HAS_SEQ(wh)) {
2793 prio = M_WME_GETAC(m0);
2794 which = rsu_wme_ac_xfer_map[prio];
2798 /* XXX TODO: tid=0 for non-qos TID? */
2799 which = rsu_wme_ac_xfer_map[WME_AC_BE];
2804 qid = rsu_ac2qid[prio];
2807 case IEEE80211_FC0_TYPE_CTL:
2808 case IEEE80211_FC0_TYPE_MGT:
2809 which = rsu_wme_ac_xfer_map[WME_AC_VO];
2812 which = rsu_wme_ac_xfer_map[M_WME_GETAC(m0)];
2818 RSU_DPRINTF(sc, RSU_DEBUG_TX, "%s: pri=%d, which=%d, hasqos=%d\n",
2824 /* Fill Tx descriptor. */
2825 txd = (struct r92s_tx_desc *)data->buf;
2826 memset(txd, 0, sizeof(*txd));
2828 txd->txdw0 |= htole32(
2829 SM(R92S_TXDW0_PKTLEN, m0->m_pkthdr.len) |
2830 SM(R92S_TXDW0_OFFSET, sizeof(*txd)) |
2831 R92S_TXDW0_OWN | R92S_TXDW0_FSG | R92S_TXDW0_LSG);
2833 txd->txdw1 |= htole32(
2834 SM(R92S_TXDW1_MACID, R92S_MACID_BSS) | SM(R92S_TXDW1_QSEL, qid));
2836 txd->txdw1 |= htole32(R92S_TXDW1_NONQOS);
2837 if (k != NULL && !(k->wk_flags & IEEE80211_KEY_SWENCRYPT)) {
2838 switch (k->wk_cipher->ic_cipher) {
2839 case IEEE80211_CIPHER_WEP:
2840 cipher = R92S_TXDW1_CIPHER_WEP;
2842 case IEEE80211_CIPHER_TKIP:
2843 cipher = R92S_TXDW1_CIPHER_TKIP;
2845 case IEEE80211_CIPHER_AES_CCM:
2846 cipher = R92S_TXDW1_CIPHER_AES;
2849 cipher = R92S_TXDW1_CIPHER_NONE;
2851 txd->txdw1 |= htole32(
2852 SM(R92S_TXDW1_CIPHER, cipher) |
2853 SM(R92S_TXDW1_KEYIDX, k->wk_keyix));
2855 /* XXX todo: set AGGEN bit if appropriate? */
2856 txd->txdw2 |= htole32(R92S_TXDW2_BK);
2857 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
2858 txd->txdw2 |= htole32(R92S_TXDW2_BMCAST);
2860 * Firmware will use and increment the sequence number for the
2861 * specified priority.
2863 txd->txdw3 |= htole32(SM(R92S_TXDW3_SEQ, prio));
2865 if (ieee80211_radiotap_active_vap(vap)) {
2866 struct rsu_tx_radiotap_header *tap = &sc->sc_txtap;
2869 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
2870 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
2871 ieee80211_radiotap_tx(vap, m0);
2874 xferlen = sizeof(*txd) + m0->m_pkthdr.len;
2875 m_copydata(m0, 0, m0->m_pkthdr.len, (caddr_t)&txd[1]);
2877 data->buflen = xferlen;
2880 STAILQ_INSERT_TAIL(&sc->sc_tx_pending[which], data, next);
2882 /* start transfer, if any */
2883 usbd_transfer_start(sc->sc_xfer[which]);
2888 rsu_transmit(struct ieee80211com *ic, struct mbuf *m)
2890 struct rsu_softc *sc = ic->ic_softc;
2894 if (!sc->sc_running) {
2900 * XXX TODO: ensure that we treat 'm' as a list of frames
2903 error = mbufq_enqueue(&sc->sc_snd, m);
2905 RSU_DPRINTF(sc, RSU_DEBUG_TX,
2906 "%s: mbufq_enable: failed (%d)\n",
2914 /* This kicks the TX taskqueue */
2921 rsu_drain_mbufq(struct rsu_softc *sc)
2924 struct ieee80211_node *ni;
2926 RSU_ASSERT_LOCKED(sc);
2927 while ((m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
2928 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
2929 m->m_pkthdr.rcvif = NULL;
2930 ieee80211_free_node(ni);
2936 _rsu_start(struct rsu_softc *sc)
2938 struct ieee80211_node *ni;
2939 struct rsu_data *bf;
2942 RSU_ASSERT_LOCKED(sc);
2944 while ((m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
2945 bf = rsu_getbuf(sc);
2947 RSU_DPRINTF(sc, RSU_DEBUG_TX,
2948 "%s: failed to get buffer\n", __func__);
2949 mbufq_prepend(&sc->sc_snd, m);
2953 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
2954 m->m_pkthdr.rcvif = NULL;
2956 if (rsu_tx_start(sc, ni, m, bf) != 0) {
2957 RSU_DPRINTF(sc, RSU_DEBUG_TX,
2958 "%s: failed to transmit\n", __func__);
2959 if_inc_counter(ni->ni_vap->iv_ifp,
2960 IFCOUNTER_OERRORS, 1);
2961 rsu_freebuf(sc, bf);
2962 ieee80211_free_node(ni);
2970 rsu_start(struct rsu_softc *sc)
2973 taskqueue_enqueue(taskqueue_thread, &sc->tx_task);
2977 rsu_ioctl_net(struct ieee80211com *ic, u_long cmd, void *data)
2979 struct rsu_softc *sc = ic->ic_softc;
2980 struct ifreq *ifr = (struct ifreq *)data;
2987 struct ieee80211vap *vap;
2990 rxmask = ifr->ifr_reqcap & (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6);
2993 /* Both RXCSUM bits must be set (or unset). */
2994 if (sc->sc_rx_checksum_enable &&
2995 rxmask != (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6)) {
2997 sc->sc_rx_checksum_enable = 0;
2998 rsu_rxfilter_set(sc, R92S_RCR_TCP_OFFLD_EN, 0);
2999 } else if (!sc->sc_rx_checksum_enable && rxmask != 0) {
3000 rxmask = IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6;
3001 sc->sc_rx_checksum_enable = 1;
3002 rsu_rxfilter_set(sc, 0, R92S_RCR_TCP_OFFLD_EN);
3004 /* Nothing to do. */
3010 IEEE80211_LOCK(ic); /* XXX */
3011 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
3012 struct ifnet *ifp = vap->iv_ifp;
3014 ifp->if_capenable &=
3015 ~(IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6);
3016 ifp->if_capenable |= rxmask;
3018 IEEE80211_UNLOCK(ic);
3022 error = ENOTTY; /* for net80211 */
3030 rsu_parent(struct ieee80211com *ic)
3032 struct rsu_softc *sc = ic->ic_softc;
3034 if (ic->ic_nrunning > 0) {
3035 if (rsu_init(sc) == 0)
3036 ieee80211_start_all(ic);
3038 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
3040 ieee80211_stop(vap);
3047 * Power on sequence for A-cut adapters.
3050 rsu_power_on_acut(struct rsu_softc *sc)
3054 rsu_write_1(sc, R92S_SPS0_CTRL + 1, 0x53);
3055 rsu_write_1(sc, R92S_SPS0_CTRL + 0, 0x57);
3057 /* Enable AFE macro block's bandgap and Mbias. */
3058 rsu_write_1(sc, R92S_AFE_MISC,
3059 rsu_read_1(sc, R92S_AFE_MISC) |
3060 R92S_AFE_MISC_BGEN | R92S_AFE_MISC_MBEN);
3061 /* Enable LDOA15 block. */
3062 rsu_write_1(sc, R92S_LDOA15_CTRL,
3063 rsu_read_1(sc, R92S_LDOA15_CTRL) | R92S_LDA15_EN);
3065 rsu_write_1(sc, R92S_SPS1_CTRL,
3066 rsu_read_1(sc, R92S_SPS1_CTRL) | R92S_SPS1_LDEN);
3067 rsu_ms_delay(sc, 2000);
3068 /* Enable switch regulator block. */
3069 rsu_write_1(sc, R92S_SPS1_CTRL,
3070 rsu_read_1(sc, R92S_SPS1_CTRL) | R92S_SPS1_SWEN);
3072 rsu_write_4(sc, R92S_SPS1_CTRL, 0x00a7b267);
3074 rsu_write_1(sc, R92S_SYS_ISO_CTRL + 1,
3075 rsu_read_1(sc, R92S_SYS_ISO_CTRL + 1) | 0x08);
3077 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1,
3078 rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) | 0x20);
3080 rsu_write_1(sc, R92S_SYS_ISO_CTRL + 1,
3081 rsu_read_1(sc, R92S_SYS_ISO_CTRL + 1) & ~0x90);
3083 /* Enable AFE clock. */
3084 rsu_write_1(sc, R92S_AFE_XTAL_CTRL + 1,
3085 rsu_read_1(sc, R92S_AFE_XTAL_CTRL + 1) & ~0x04);
3086 /* Enable AFE PLL macro block. */
3087 rsu_write_1(sc, R92S_AFE_PLL_CTRL,
3088 rsu_read_1(sc, R92S_AFE_PLL_CTRL) | 0x11);
3089 /* Attach AFE PLL to MACTOP/BB. */
3090 rsu_write_1(sc, R92S_SYS_ISO_CTRL,
3091 rsu_read_1(sc, R92S_SYS_ISO_CTRL) & ~0x11);
3093 /* Switch to 40MHz clock instead of 80MHz. */
3094 rsu_write_2(sc, R92S_SYS_CLKR,
3095 rsu_read_2(sc, R92S_SYS_CLKR) & ~R92S_SYS_CLKSEL);
3097 /* Enable MAC clock. */
3098 rsu_write_2(sc, R92S_SYS_CLKR,
3099 rsu_read_2(sc, R92S_SYS_CLKR) |
3100 R92S_MAC_CLK_EN | R92S_SYS_CLK_EN);
3102 rsu_write_1(sc, R92S_PMC_FSM, 0x02);
3104 /* Enable digital core and IOREG R/W. */
3105 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1,
3106 rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) | 0x08);
3108 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1,
3109 rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) | 0x80);
3111 /* Switch the control path to firmware. */
3112 reg = rsu_read_2(sc, R92S_SYS_CLKR);
3113 reg = (reg & ~R92S_SWHW_SEL) | R92S_FWHW_SEL;
3114 rsu_write_2(sc, R92S_SYS_CLKR, reg);
3116 rsu_write_2(sc, R92S_CR, 0x37fc);
3118 /* Fix USB RX FIFO issue. */
3119 rsu_write_1(sc, 0xfe5c,
3120 rsu_read_1(sc, 0xfe5c) | 0x80);
3121 rsu_write_1(sc, 0x00ab,
3122 rsu_read_1(sc, 0x00ab) | 0xc0);
3124 rsu_write_1(sc, R92S_SYS_CLKR,
3125 rsu_read_1(sc, R92S_SYS_CLKR) & ~R92S_SYS_CPU_CLKSEL);
3129 * Power on sequence for B-cut and C-cut adapters.
3132 rsu_power_on_bcut(struct rsu_softc *sc)
3137 /* Prevent eFuse leakage. */
3138 rsu_write_1(sc, 0x37, 0xb0);
3139 rsu_ms_delay(sc, 10);
3140 rsu_write_1(sc, 0x37, 0x30);
3142 /* Switch the control path to hardware. */
3143 reg = rsu_read_2(sc, R92S_SYS_CLKR);
3144 if (reg & R92S_FWHW_SEL) {
3145 rsu_write_2(sc, R92S_SYS_CLKR,
3146 reg & ~(R92S_SWHW_SEL | R92S_FWHW_SEL));
3148 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1,
3149 rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) & ~0x8c);
3150 rsu_ms_delay(sc, 1);
3152 rsu_write_1(sc, R92S_SPS0_CTRL + 1, 0x53);
3153 rsu_write_1(sc, R92S_SPS0_CTRL + 0, 0x57);
3155 reg = rsu_read_1(sc, R92S_AFE_MISC);
3156 rsu_write_1(sc, R92S_AFE_MISC, reg | R92S_AFE_MISC_BGEN);
3157 rsu_write_1(sc, R92S_AFE_MISC, reg | R92S_AFE_MISC_BGEN |
3158 R92S_AFE_MISC_MBEN | R92S_AFE_MISC_I32_EN);
3161 rsu_write_1(sc, R92S_LDOA15_CTRL,
3162 rsu_read_1(sc, R92S_LDOA15_CTRL) | R92S_LDA15_EN);
3164 rsu_write_1(sc, R92S_LDOV12D_CTRL,
3165 rsu_read_1(sc, R92S_LDOV12D_CTRL) | R92S_LDV12_EN);
3167 rsu_write_1(sc, R92S_SYS_ISO_CTRL + 1,
3168 rsu_read_1(sc, R92S_SYS_ISO_CTRL + 1) | 0x08);
3170 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1,
3171 rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) | 0x20);
3173 /* Support 64KB IMEM. */
3174 rsu_write_1(sc, R92S_SYS_ISO_CTRL + 1,
3175 rsu_read_1(sc, R92S_SYS_ISO_CTRL + 1) & ~0x97);
3177 /* Enable AFE clock. */
3178 rsu_write_1(sc, R92S_AFE_XTAL_CTRL + 1,
3179 rsu_read_1(sc, R92S_AFE_XTAL_CTRL + 1) & ~0x04);
3180 /* Enable AFE PLL macro block. */
3181 reg = rsu_read_1(sc, R92S_AFE_PLL_CTRL);
3182 rsu_write_1(sc, R92S_AFE_PLL_CTRL, reg | 0x11);
3183 rsu_ms_delay(sc, 1);
3184 rsu_write_1(sc, R92S_AFE_PLL_CTRL, reg | 0x51);
3185 rsu_ms_delay(sc, 1);
3186 rsu_write_1(sc, R92S_AFE_PLL_CTRL, reg | 0x11);
3187 rsu_ms_delay(sc, 1);
3189 /* Attach AFE PLL to MACTOP/BB. */
3190 rsu_write_1(sc, R92S_SYS_ISO_CTRL,
3191 rsu_read_1(sc, R92S_SYS_ISO_CTRL) & ~0x11);
3193 /* Switch to 40MHz clock. */
3194 rsu_write_1(sc, R92S_SYS_CLKR, 0x00);
3195 /* Disable CPU clock and 80MHz SSC. */
3196 rsu_write_1(sc, R92S_SYS_CLKR,
3197 rsu_read_1(sc, R92S_SYS_CLKR) | 0xa0);
3198 /* Enable MAC clock. */
3199 rsu_write_2(sc, R92S_SYS_CLKR,
3200 rsu_read_2(sc, R92S_SYS_CLKR) |
3201 R92S_MAC_CLK_EN | R92S_SYS_CLK_EN);
3203 rsu_write_1(sc, R92S_PMC_FSM, 0x02);
3205 /* Enable digital core and IOREG R/W. */
3206 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1,
3207 rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) | 0x08);
3209 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1,
3210 rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) | 0x80);
3212 /* Switch the control path to firmware. */
3213 reg = rsu_read_2(sc, R92S_SYS_CLKR);
3214 reg = (reg & ~R92S_SWHW_SEL) | R92S_FWHW_SEL;
3215 rsu_write_2(sc, R92S_SYS_CLKR, reg);
3217 rsu_write_2(sc, R92S_CR, 0x37fc);
3219 /* Fix USB RX FIFO issue. */
3220 rsu_write_1(sc, 0xfe5c,
3221 rsu_read_1(sc, 0xfe5c) | 0x80);
3223 rsu_write_1(sc, R92S_SYS_CLKR,
3224 rsu_read_1(sc, R92S_SYS_CLKR) & ~R92S_SYS_CPU_CLKSEL);
3226 rsu_write_1(sc, 0xfe1c, 0x80);
3228 /* Make sure TxDMA is ready to download firmware. */
3229 for (ntries = 0; ntries < 20; ntries++) {
3230 reg = rsu_read_1(sc, R92S_TCR);
3231 if ((reg & (R92S_TCR_IMEM_CHK_RPT | R92S_TCR_EMEM_CHK_RPT)) ==
3232 (R92S_TCR_IMEM_CHK_RPT | R92S_TCR_EMEM_CHK_RPT))
3234 rsu_ms_delay(sc, 1);
3237 RSU_DPRINTF(sc, RSU_DEBUG_RESET | RSU_DEBUG_TX,
3238 "%s: TxDMA is not ready\n",
3241 reg = rsu_read_1(sc, R92S_CR);
3242 rsu_write_1(sc, R92S_CR, reg & ~R92S_CR_TXDMA_EN);
3243 rsu_ms_delay(sc, 1);
3244 rsu_write_1(sc, R92S_CR, reg | R92S_CR_TXDMA_EN);
3249 rsu_power_off(struct rsu_softc *sc)
3252 rsu_write_1(sc, R92S_RF_CTRL, 0x00);
3253 rsu_ms_delay(sc, 5);
3256 /* Switch control path. */
3257 rsu_write_1(sc, R92S_SYS_CLKR + 1, 0x38);
3259 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1, 0x70);
3260 rsu_write_1(sc, R92S_PMC_FSM, 0x06);
3261 rsu_write_1(sc, R92S_SYS_ISO_CTRL + 0, 0xf9);
3262 rsu_write_1(sc, R92S_SYS_ISO_CTRL + 1, 0xe8);
3264 /* Disable AFE PLL. */
3265 rsu_write_1(sc, R92S_AFE_PLL_CTRL, 0x00);
3267 rsu_write_1(sc, R92S_LDOA15_CTRL, 0x54);
3268 /* Disable eFuse 1.2V. */
3269 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1, 0x50);
3270 rsu_write_1(sc, R92S_LDOV12D_CTRL, 0x24);
3271 /* Enable AFE macro block's bandgap and Mbias. */
3272 rsu_write_1(sc, R92S_AFE_MISC, 0x30);
3273 /* Disable 1.6V LDO. */
3274 rsu_write_1(sc, R92S_SPS0_CTRL + 0, 0x56);
3275 rsu_write_1(sc, R92S_SPS0_CTRL + 1, 0x43);
3277 /* Firmware - tell it to switch things off */
3278 (void) rsu_set_fw_power_state(sc, RSU_PWR_OFF);
3282 rsu_fw_loadsection(struct rsu_softc *sc, const uint8_t *buf, int len)
3284 const uint8_t which = rsu_wme_ac_xfer_map[WME_AC_VO];
3285 struct rsu_data *data;
3286 struct r92s_tx_desc *txd;
3290 data = rsu_getbuf(sc);
3293 txd = (struct r92s_tx_desc *)data->buf;
3294 memset(txd, 0, sizeof(*txd));
3295 if (len <= RSU_TXBUFSZ - sizeof(*txd)) {
3297 txd->txdw0 |= htole32(R92S_TXDW0_LINIP);
3300 mlen = RSU_TXBUFSZ - sizeof(*txd);
3301 txd->txdw0 |= htole32(SM(R92S_TXDW0_PKTLEN, mlen));
3302 memcpy(&txd[1], buf, mlen);
3303 data->buflen = sizeof(*txd) + mlen;
3304 RSU_DPRINTF(sc, RSU_DEBUG_TX | RSU_DEBUG_FW | RSU_DEBUG_RESET,
3305 "%s: starting transfer %p\n",
3307 STAILQ_INSERT_TAIL(&sc->sc_tx_pending[which], data, next);
3311 usbd_transfer_start(sc->sc_xfer[which]);
3315 CTASSERT(sizeof(size_t) >= sizeof(uint32_t));
3318 rsu_load_firmware(struct rsu_softc *sc)
3320 const struct r92s_fw_hdr *hdr;
3321 struct r92s_fw_priv *dmem;
3322 struct ieee80211com *ic = &sc->sc_ic;
3323 const uint8_t *imem, *emem;
3324 uint32_t imemsz, ememsz;
3325 const struct firmware *fw;
3330 if (rsu_read_1(sc, R92S_TCR) & R92S_TCR_FWRDY) {
3331 RSU_DPRINTF(sc, RSU_DEBUG_ANY,
3332 "%s: Firmware already loaded\n",
3338 /* Read firmware image from the filesystem. */
3339 if ((fw = firmware_get("rsu-rtl8712fw")) == NULL) {
3340 device_printf(sc->sc_dev,
3341 "%s: failed load firmware of file rsu-rtl8712fw\n",
3347 size = fw->datasize;
3348 if (size < sizeof(*hdr)) {
3349 device_printf(sc->sc_dev, "firmware too short\n");
3353 hdr = (const struct r92s_fw_hdr *)fw->data;
3354 if (hdr->signature != htole16(0x8712) &&
3355 hdr->signature != htole16(0x8192)) {
3356 device_printf(sc->sc_dev,
3357 "invalid firmware signature 0x%x\n",
3358 le16toh(hdr->signature));
3362 RSU_DPRINTF(sc, RSU_DEBUG_FW, "FW V%d %02x-%02x %02x:%02x\n",
3363 le16toh(hdr->version), hdr->month, hdr->day, hdr->hour,
3366 /* Make sure that driver and firmware are in sync. */
3367 if (hdr->privsz != htole32(sizeof(*dmem))) {
3368 device_printf(sc->sc_dev, "unsupported firmware image\n");
3372 /* Get FW sections sizes. */
3373 imemsz = le32toh(hdr->imemsz);
3374 ememsz = le32toh(hdr->sramsz);
3375 /* Check that all FW sections fit in image. */
3376 if (imemsz > (size_t)(size - sizeof(*hdr)) ||
3377 ememsz > (size_t)(size - sizeof(*hdr) - imemsz)) {
3378 device_printf(sc->sc_dev, "firmware too short\n");
3382 imem = (const uint8_t *)&hdr[1];
3383 emem = imem + imemsz;
3385 /* Load IMEM section. */
3386 error = rsu_fw_loadsection(sc, imem, imemsz);
3388 device_printf(sc->sc_dev,
3389 "could not load firmware section %s\n", "IMEM");
3392 /* Wait for load to complete. */
3393 for (ntries = 0; ntries != 50; ntries++) {
3394 rsu_ms_delay(sc, 10);
3395 reg = rsu_read_1(sc, R92S_TCR);
3396 if (reg & R92S_TCR_IMEM_CODE_DONE)
3400 device_printf(sc->sc_dev, "timeout waiting for IMEM transfer\n");
3404 /* Load EMEM section. */
3405 error = rsu_fw_loadsection(sc, emem, ememsz);
3407 device_printf(sc->sc_dev,
3408 "could not load firmware section %s\n", "EMEM");
3411 /* Wait for load to complete. */
3412 for (ntries = 0; ntries != 50; ntries++) {
3413 rsu_ms_delay(sc, 10);
3414 reg = rsu_read_2(sc, R92S_TCR);
3415 if (reg & R92S_TCR_EMEM_CODE_DONE)
3419 device_printf(sc->sc_dev, "timeout waiting for EMEM transfer\n");
3424 rsu_write_1(sc, R92S_SYS_CLKR,
3425 rsu_read_1(sc, R92S_SYS_CLKR) | R92S_SYS_CPU_CLKSEL);
3426 if (!(rsu_read_1(sc, R92S_SYS_CLKR) & R92S_SYS_CPU_CLKSEL)) {
3427 device_printf(sc->sc_dev, "could not enable system clock\n");
3431 rsu_write_2(sc, R92S_SYS_FUNC_EN,
3432 rsu_read_2(sc, R92S_SYS_FUNC_EN) | R92S_FEN_CPUEN);
3433 if (!(rsu_read_2(sc, R92S_SYS_FUNC_EN) & R92S_FEN_CPUEN)) {
3434 device_printf(sc->sc_dev,
3435 "could not enable microcontroller\n");
3439 /* Wait for CPU to initialize. */
3440 for (ntries = 0; ntries < 100; ntries++) {
3441 if (rsu_read_1(sc, R92S_TCR) & R92S_TCR_IMEM_RDY)
3443 rsu_ms_delay(sc, 1);
3445 if (ntries == 100) {
3446 device_printf(sc->sc_dev,
3447 "timeout waiting for microcontroller\n");
3452 /* Update DMEM section before loading. */
3453 dmem = __DECONST(struct r92s_fw_priv *, &hdr->priv);
3454 memset(dmem, 0, sizeof(*dmem));
3455 dmem->hci_sel = R92S_HCI_SEL_USB | R92S_HCI_SEL_8172;
3456 dmem->nendpoints = sc->sc_nendpoints;
3457 dmem->chip_version = sc->cut;
3458 dmem->rf_config = sc->sc_rftype;
3459 dmem->vcs_type = R92S_VCS_TYPE_AUTO;
3460 dmem->vcs_mode = R92S_VCS_MODE_RTS_CTS;
3461 dmem->turbo_mode = 0;
3462 dmem->bw40_en = !! (ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40);
3463 dmem->amsdu2ampdu_en = !! (sc->sc_ht);
3464 dmem->ampdu_en = !! (sc->sc_ht);
3465 dmem->agg_offload = !! (sc->sc_ht);
3467 dmem->ps_offload = 1;
3468 dmem->lowpower_mode = 1; /* XXX TODO: configurable? */
3469 /* Load DMEM section. */
3470 error = rsu_fw_loadsection(sc, (uint8_t *)dmem, sizeof(*dmem));
3472 device_printf(sc->sc_dev,
3473 "could not load firmware section %s\n", "DMEM");
3476 /* Wait for load to complete. */
3477 for (ntries = 0; ntries < 100; ntries++) {
3478 if (rsu_read_1(sc, R92S_TCR) & R92S_TCR_DMEM_CODE_DONE)
3480 rsu_ms_delay(sc, 1);
3482 if (ntries == 100) {
3483 device_printf(sc->sc_dev, "timeout waiting for %s transfer\n",
3488 /* Wait for firmware readiness. */
3489 for (ntries = 0; ntries < 60; ntries++) {
3490 if (!(rsu_read_1(sc, R92S_TCR) & R92S_TCR_FWRDY))
3492 rsu_ms_delay(sc, 1);
3495 device_printf(sc->sc_dev,
3496 "timeout waiting for firmware readiness\n");
3501 firmware_put(fw, FIRMWARE_UNLOAD);
3507 rsu_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
3508 const struct ieee80211_bpf_params *params)
3510 struct ieee80211com *ic = ni->ni_ic;
3511 struct rsu_softc *sc = ic->ic_softc;
3512 struct rsu_data *bf;
3514 /* prevent management frames from being sent if we're not ready */
3515 if (!sc->sc_running) {
3520 bf = rsu_getbuf(sc);
3526 if (rsu_tx_start(sc, ni, m, bf) != 0) {
3528 rsu_freebuf(sc, bf);
3538 rsu_rxfilter_init(struct rsu_softc *sc)
3542 RSU_ASSERT_LOCKED(sc);
3544 /* Setup multicast filter. */
3547 /* Adjust Rx filter. */
3548 reg = rsu_read_4(sc, R92S_RCR);
3549 reg &= ~R92S_RCR_AICV;
3550 reg |= R92S_RCR_APP_PHYSTS;
3551 if (sc->sc_rx_checksum_enable)
3552 reg |= R92S_RCR_TCP_OFFLD_EN;
3553 rsu_write_4(sc, R92S_RCR, reg);
3555 /* Update dynamic Rx filter parts. */
3556 rsu_rxfilter_refresh(sc);
3560 rsu_rxfilter_set(struct rsu_softc *sc, uint32_t clear, uint32_t set)
3562 /* NB: firmware can touch this register too. */
3563 rsu_write_4(sc, R92S_RCR,
3564 (rsu_read_4(sc, R92S_RCR) & ~clear) | set);
3568 rsu_rxfilter_refresh(struct rsu_softc *sc)
3570 struct ieee80211com *ic = &sc->sc_ic;
3571 uint32_t mask_all, mask_min;
3573 RSU_ASSERT_LOCKED(sc);
3575 /* NB: RCR_AMF / RXFLTMAP_MGT are used by firmware. */
3576 mask_all = R92S_RCR_ACF | R92S_RCR_AAP;
3577 mask_min = R92S_RCR_APM;
3578 if (sc->sc_vap_is_running)
3579 mask_min |= R92S_RCR_CBSSID;
3581 mask_all |= R92S_RCR_ADF;
3583 if (ic->ic_opmode == IEEE80211_M_MONITOR) {
3585 if (sc->sc_vap_is_running)
3588 rxfltmap = R92S_RXFLTMAP_MGT_DEF;
3589 rsu_write_2(sc, R92S_RXFLTMAP_MGT, rxfltmap);
3592 if (ic->ic_promisc == 0 && ic->ic_opmode != IEEE80211_M_MONITOR)
3593 rsu_rxfilter_set(sc, mask_all, mask_min);
3595 rsu_rxfilter_set(sc, mask_min, mask_all);
3599 rsu_init(struct rsu_softc *sc)
3601 struct ieee80211com *ic = &sc->sc_ic;
3602 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
3603 uint8_t macaddr[IEEE80211_ADDR_LEN];
3609 if (sc->sc_running) {
3614 /* Ensure the mbuf queue is drained */
3615 rsu_drain_mbufq(sc);
3617 /* Reset power management state. */
3618 rsu_write_1(sc, R92S_USB_HRPWM, 0);
3620 /* Power on adapter. */
3622 rsu_power_on_acut(sc);
3624 rsu_power_on_bcut(sc);
3626 /* Load firmware. */
3627 error = rsu_load_firmware(sc);
3631 rsu_write_4(sc, R92S_CR,
3632 rsu_read_4(sc, R92S_CR) & ~0xff000000);
3634 /* Use 128 bytes pages. */
3635 rsu_write_1(sc, 0x00b5,
3636 rsu_read_1(sc, 0x00b5) | 0x01);
3637 /* Enable USB Rx aggregation. */
3638 rsu_write_1(sc, 0x00bd,
3639 rsu_read_1(sc, 0x00bd) | 0x80);
3640 /* Set USB Rx aggregation threshold. */
3641 rsu_write_1(sc, 0x00d9, 0x01);
3642 /* Set USB Rx aggregation timeout (1.7ms/4). */
3643 rsu_write_1(sc, 0xfe5b, 0x04);
3644 /* Fix USB Rx FIFO issue. */
3645 rsu_write_1(sc, 0xfe5c,
3646 rsu_read_1(sc, 0xfe5c) | 0x80);
3648 /* Set MAC address. */
3649 IEEE80211_ADDR_COPY(macaddr, vap ? vap->iv_myaddr : ic->ic_macaddr);
3650 rsu_write_region_1(sc, R92S_MACID, macaddr, IEEE80211_ADDR_LEN);
3652 /* It really takes 1.5 seconds for the firmware to boot: */
3653 usb_pause_mtx(&sc->sc_mtx, USB_MS_TO_TICKS(2000));
3655 RSU_DPRINTF(sc, RSU_DEBUG_RESET, "%s: setting MAC address to %s\n",
3657 ether_sprintf(macaddr));
3658 error = rsu_fw_cmd(sc, R92S_CMD_SET_MAC_ADDRESS, macaddr,
3659 IEEE80211_ADDR_LEN);
3661 device_printf(sc->sc_dev, "could not set MAC address\n");
3665 /* Initialize Rx filter. */
3666 rsu_rxfilter_init(sc);
3668 /* Set PS mode fully active */
3669 error = rsu_set_fw_power_state(sc, RSU_PWR_ACTIVE);
3671 device_printf(sc->sc_dev, "could not set PS mode\n");
3675 /* Install static keys (if any). */
3676 error = rsu_reinit_static_keys(sc);
3680 sc->sc_extra_scan = 0;
3681 usbd_transfer_start(sc->sc_xfer[RSU_BULK_RX]);
3683 /* We're ready to go. */
3689 /* Need to stop all failed transfers, if any */
3690 for (i = 0; i != RSU_N_TRANSFER; i++)
3691 usbd_transfer_stop(sc->sc_xfer[i]);
3698 rsu_stop(struct rsu_softc *sc)
3703 if (!sc->sc_running) {
3709 sc->sc_vap_is_running = 0;
3710 sc->sc_calibrating = 0;
3711 taskqueue_cancel_timeout(taskqueue_thread, &sc->calib_task, NULL);
3712 taskqueue_cancel(taskqueue_thread, &sc->tx_task, NULL);
3714 /* Power off adapter. */
3718 * CAM is not accessible after shutdown;
3719 * all entries are marked (by firmware?) as invalid.
3721 memset(sc->free_keys_bmap, 0, sizeof(sc->free_keys_bmap));
3722 memset(sc->keys_bmap, 0, sizeof(sc->keys_bmap));
3724 for (i = 0; i < RSU_N_TRANSFER; i++)
3725 usbd_transfer_stop(sc->sc_xfer[i]);
3727 /* Ensure the mbuf queue is drained */
3728 rsu_drain_mbufq(sc);
3733 * Note: usb_pause_mtx() actually releases the mutex before calling pause(),
3734 * which breaks any kind of driver serialisation.
3737 rsu_ms_delay(struct rsu_softc *sc, int ms)
3740 //usb_pause_mtx(&sc->sc_mtx, hz / 1000);