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.
27 * o hostap / ibss / mesh
29 #include <sys/param.h>
30 #include <sys/endian.h>
31 #include <sys/sockio.h>
33 #include <sys/kernel.h>
34 #include <sys/socket.h>
35 #include <sys/systm.h>
39 #include <sys/firmware.h>
40 #include <sys/module.h>
42 #include <machine/bus.h>
43 #include <machine/resource.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 #define USB_DEBUG_VAR rsu_debug
68 #include <dev/usb/usb_debug.h>
70 #include <dev/usb/wlan/if_rsureg.h>
73 static int rsu_debug = 0;
74 SYSCTL_NODE(_hw_usb, OID_AUTO, rsu, CTLFLAG_RW, 0, "USB rsu");
75 SYSCTL_INT(_hw_usb_rsu, OID_AUTO, debug, CTLFLAG_RW, &rsu_debug, 0,
79 static const STRUCT_USB_HOST_ID rsu_devs[] = {
80 #define RSU_HT_NOT_SUPPORTED 0
81 #define RSU_HT_SUPPORTED 1
82 #define RSU_DEV_HT(v,p) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, \
84 #define RSU_DEV(v,p) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, \
85 RSU_HT_NOT_SUPPORTED) }
86 RSU_DEV(ASUS, RTL8192SU),
87 RSU_DEV(AZUREWAVE, RTL8192SU_4),
88 RSU_DEV_HT(ACCTON, RTL8192SU),
89 RSU_DEV_HT(ASUS, USBN10),
90 RSU_DEV_HT(AZUREWAVE, RTL8192SU_1),
91 RSU_DEV_HT(AZUREWAVE, RTL8192SU_2),
92 RSU_DEV_HT(AZUREWAVE, RTL8192SU_3),
93 RSU_DEV_HT(AZUREWAVE, RTL8192SU_5),
94 RSU_DEV_HT(BELKIN, RTL8192SU_1),
95 RSU_DEV_HT(BELKIN, RTL8192SU_2),
96 RSU_DEV_HT(BELKIN, RTL8192SU_3),
97 RSU_DEV_HT(CONCEPTRONIC2, RTL8192SU_1),
98 RSU_DEV_HT(CONCEPTRONIC2, RTL8192SU_2),
99 RSU_DEV_HT(CONCEPTRONIC2, RTL8192SU_3),
100 RSU_DEV_HT(COREGA, RTL8192SU),
101 RSU_DEV_HT(DLINK2, DWA131A1),
102 RSU_DEV_HT(DLINK2, RTL8192SU_1),
103 RSU_DEV_HT(DLINK2, RTL8192SU_2),
104 RSU_DEV_HT(EDIMAX, RTL8192SU_1),
105 RSU_DEV_HT(EDIMAX, RTL8192SU_2),
106 RSU_DEV_HT(EDIMAX, EW7622UMN),
107 RSU_DEV_HT(GUILLEMOT, HWGUN54),
108 RSU_DEV_HT(GUILLEMOT, HWNUM300),
109 RSU_DEV_HT(HAWKING, RTL8192SU_1),
110 RSU_DEV_HT(HAWKING, RTL8192SU_2),
111 RSU_DEV_HT(PLANEX2, GWUSNANO),
112 RSU_DEV_HT(REALTEK, RTL8171),
113 RSU_DEV_HT(REALTEK, RTL8172),
114 RSU_DEV_HT(REALTEK, RTL8173),
115 RSU_DEV_HT(REALTEK, RTL8174),
116 RSU_DEV_HT(REALTEK, RTL8192SU),
117 RSU_DEV_HT(REALTEK, RTL8712),
118 RSU_DEV_HT(REALTEK, RTL8713),
119 RSU_DEV_HT(SENAO, RTL8192SU_1),
120 RSU_DEV_HT(SENAO, RTL8192SU_2),
121 RSU_DEV_HT(SITECOMEU, WL349V1),
122 RSU_DEV_HT(SITECOMEU, WL353),
123 RSU_DEV_HT(SWEEX2, LW154),
128 static device_probe_t rsu_match;
129 static device_attach_t rsu_attach;
130 static device_detach_t rsu_detach;
131 static usb_callback_t rsu_bulk_tx_callback_be_bk;
132 static usb_callback_t rsu_bulk_tx_callback_vi_vo;
133 static usb_callback_t rsu_bulk_rx_callback;
134 static usb_error_t rsu_do_request(struct rsu_softc *,
135 struct usb_device_request *, void *);
136 static struct ieee80211vap *
137 rsu_vap_create(struct ieee80211com *, const char name[],
138 int, enum ieee80211_opmode, int, const uint8_t bssid[],
139 const uint8_t mac[]);
140 static void rsu_vap_delete(struct ieee80211vap *);
141 static void rsu_scan_start(struct ieee80211com *);
142 static void rsu_scan_end(struct ieee80211com *);
143 static void rsu_set_channel(struct ieee80211com *);
144 static void rsu_update_mcast(struct ifnet *);
145 static int rsu_alloc_rx_list(struct rsu_softc *);
146 static void rsu_free_rx_list(struct rsu_softc *);
147 static int rsu_alloc_tx_list(struct rsu_softc *);
148 static void rsu_free_tx_list(struct rsu_softc *);
149 static void rsu_free_list(struct rsu_softc *, struct rsu_data [], int);
150 static struct rsu_data *_rsu_getbuf(struct rsu_softc *);
151 static struct rsu_data *rsu_getbuf(struct rsu_softc *);
152 static int rsu_write_region_1(struct rsu_softc *, uint16_t, uint8_t *,
154 static void rsu_write_1(struct rsu_softc *, uint16_t, uint8_t);
155 static void rsu_write_2(struct rsu_softc *, uint16_t, uint16_t);
156 static void rsu_write_4(struct rsu_softc *, uint16_t, uint32_t);
157 static int rsu_read_region_1(struct rsu_softc *, uint16_t, uint8_t *,
159 static uint8_t rsu_read_1(struct rsu_softc *, uint16_t);
160 static uint16_t rsu_read_2(struct rsu_softc *, uint16_t);
161 static uint32_t rsu_read_4(struct rsu_softc *, uint16_t);
162 static int rsu_fw_iocmd(struct rsu_softc *, uint32_t);
163 static uint8_t rsu_efuse_read_1(struct rsu_softc *, uint16_t);
164 static int rsu_read_rom(struct rsu_softc *);
165 static int rsu_fw_cmd(struct rsu_softc *, uint8_t, void *, int);
166 static void rsu_calib_task(void *, int);
167 static int rsu_newstate(struct ieee80211vap *, enum ieee80211_state, int);
169 static void rsu_set_key(struct rsu_softc *, const struct ieee80211_key *);
170 static void rsu_delete_key(struct rsu_softc *, const struct ieee80211_key *);
172 static int rsu_site_survey(struct rsu_softc *, struct ieee80211vap *);
173 static int rsu_join_bss(struct rsu_softc *, struct ieee80211_node *);
174 static int rsu_disconnect(struct rsu_softc *);
175 static void rsu_event_survey(struct rsu_softc *, uint8_t *, int);
176 static void rsu_event_join_bss(struct rsu_softc *, uint8_t *, int);
177 static void rsu_rx_event(struct rsu_softc *, uint8_t, uint8_t *, int);
178 static void rsu_rx_multi_event(struct rsu_softc *, uint8_t *, int);
179 static int8_t rsu_get_rssi(struct rsu_softc *, int, void *);
181 rsu_rx_frame(struct rsu_softc *, uint8_t *, int, int *);
183 rsu_rx_multi_frame(struct rsu_softc *, uint8_t *, int, int *);
185 rsu_rxeof(struct usb_xfer *, struct rsu_data *, int *);
186 static void rsu_txeof(struct usb_xfer *, struct rsu_data *);
187 static int rsu_raw_xmit(struct ieee80211_node *, struct mbuf *,
188 const struct ieee80211_bpf_params *);
189 static void rsu_init(void *);
190 static void rsu_init_locked(struct rsu_softc *);
191 static int rsu_tx_start(struct rsu_softc *, struct ieee80211_node *,
192 struct mbuf *, struct rsu_data *);
193 static void rsu_start(struct ifnet *);
194 static void rsu_start_locked(struct ifnet *);
195 static int rsu_ioctl(struct ifnet *, u_long, caddr_t);
196 static void rsu_stop(struct ifnet *, int);
197 static void rsu_stop_locked(struct ifnet *, int);
198 static void rsu_ms_delay(struct rsu_softc *);
200 static device_method_t rsu_methods[] = {
201 DEVMETHOD(device_probe, rsu_match),
202 DEVMETHOD(device_attach, rsu_attach),
203 DEVMETHOD(device_detach, rsu_detach),
208 static driver_t rsu_driver = {
210 .methods = rsu_methods,
211 .size = sizeof(struct rsu_softc)
214 static devclass_t rsu_devclass;
216 DRIVER_MODULE(rsu, uhub, rsu_driver, rsu_devclass, NULL, 0);
217 MODULE_DEPEND(rsu, wlan, 1, 1, 1);
218 MODULE_DEPEND(rsu, usb, 1, 1, 1);
219 MODULE_DEPEND(rsu, firmware, 1, 1, 1);
220 MODULE_VERSION(rsu, 1);
222 static uint8_t rsu_wme_ac_xfer_map[4] = {
223 [WME_AC_BE] = RSU_BULK_TX_BE_BK,
224 [WME_AC_BK] = RSU_BULK_TX_BE_BK,
225 [WME_AC_VI] = RSU_BULK_TX_VI_VO,
226 [WME_AC_VO] = RSU_BULK_TX_VI_VO,
229 static const struct usb_config rsu_config[RSU_N_TRANSFER] = {
232 .endpoint = UE_ADDR_ANY,
233 .direction = UE_DIR_IN,
234 .bufsize = RSU_RXBUFSZ,
239 .callback = rsu_bulk_rx_callback
241 [RSU_BULK_TX_BE_BK] = {
244 .direction = UE_DIR_OUT,
245 .bufsize = RSU_TXBUFSZ,
249 .force_short_xfer = 1
251 .callback = rsu_bulk_tx_callback_be_bk,
252 .timeout = RSU_TX_TIMEOUT
254 [RSU_BULK_TX_VI_VO] = {
257 .direction = UE_DIR_OUT,
258 .bufsize = RSU_TXBUFSZ,
262 .force_short_xfer = 1
264 .callback = rsu_bulk_tx_callback_vi_vo,
265 .timeout = RSU_TX_TIMEOUT
270 rsu_match(device_t self)
272 struct usb_attach_arg *uaa = device_get_ivars(self);
274 if (uaa->usb_mode != USB_MODE_HOST ||
275 uaa->info.bIfaceIndex != 0 ||
276 uaa->info.bConfigIndex != 0)
279 return (usbd_lookup_id_by_uaa(rsu_devs, sizeof(rsu_devs), uaa));
283 rsu_attach(device_t self)
285 struct usb_attach_arg *uaa = device_get_ivars(self);
286 struct rsu_softc *sc = device_get_softc(self);
288 struct ieee80211com *ic;
290 uint8_t iface_index, bands;
292 device_set_usb_desc(self);
293 sc->sc_udev = uaa->device;
296 mtx_init(&sc->sc_mtx, device_get_nameunit(self), MTX_NETWORK_LOCK,
298 TIMEOUT_TASK_INIT(taskqueue_thread, &sc->calib_task, 0,
301 /* Allocate Tx/Rx buffers. */
302 error = rsu_alloc_rx_list(sc);
304 device_printf(sc->sc_dev, "could not allocate Rx buffers\n");
308 error = rsu_alloc_tx_list(sc);
310 device_printf(sc->sc_dev, "could not allocate Tx buffers\n");
311 rsu_free_rx_list(sc);
316 error = usbd_transfer_setup(uaa->device, &iface_index, sc->sc_xfer,
317 rsu_config, RSU_N_TRANSFER, sc, &sc->sc_mtx);
319 device_printf(sc->sc_dev,
320 "could not allocate USB transfers, err=%s\n",
325 /* Read chip revision. */
326 sc->cut = MS(rsu_read_4(sc, R92S_PMC_FSM), R92S_PMC_FSM_CUT);
328 sc->cut = (sc->cut >> 1) + 1;
329 error = rsu_read_rom(sc);
331 device_printf(self, "could not read ROM\n");
335 IEEE80211_ADDR_COPY(sc->sc_bssid, &sc->rom[0x12]);
336 device_printf(self, "MAC/BB RTL8712 cut %d\n", sc->cut);
337 ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
339 device_printf(self, "cannot allocate interface\n");
344 if_initname(ifp, "rsu", device_get_unit(self));
345 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
346 ifp->if_init = rsu_init;
347 ifp->if_ioctl = rsu_ioctl;
348 ifp->if_start = rsu_start;
349 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
350 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
351 IFQ_SET_READY(&ifp->if_snd);
352 ifp->if_capabilities |= IFCAP_RXCSUM;
353 ifp->if_capenable |= IFCAP_RXCSUM;
354 ifp->if_hwassist = CSUM_TCP;
357 ic->ic_phytype = IEEE80211_T_OFDM; /* Not only, but not used. */
358 ic->ic_opmode = IEEE80211_M_STA; /* Default to BSS mode. */
360 /* Set device capabilities. */
362 IEEE80211_C_STA | /* station mode */
363 IEEE80211_C_BGSCAN | /* Background scan. */
364 IEEE80211_C_SHPREAMBLE | /* Short preamble supported. */
365 IEEE80211_C_SHSLOT | /* Short slot time supported. */
366 IEEE80211_C_WPA; /* WPA/RSN. */
369 /* Check if HT support is present. */
370 if (usb_lookup(rsu_devs_noht, uaa->vendor, uaa->product) == NULL) {
371 /* Set HT capabilities. */
373 IEEE80211_HTCAP_CBW20_40 |
374 IEEE80211_HTCAP_DSSSCCK40;
375 /* Set supported HT rates. */
376 for (i = 0; i < 2; i++)
377 ic->ic_sup_mcs[i] = 0xff;
381 /* Set supported .11b and .11g rates. */
383 setbit(&bands, IEEE80211_MODE_11B);
384 setbit(&bands, IEEE80211_MODE_11G);
385 ieee80211_init_channels(ic, NULL, &bands);
387 ieee80211_ifattach(ic, sc->sc_bssid);
388 ic->ic_raw_xmit = rsu_raw_xmit;
389 ic->ic_scan_start = rsu_scan_start;
390 ic->ic_scan_end = rsu_scan_end;
391 ic->ic_set_channel = rsu_set_channel;
392 ic->ic_vap_create = rsu_vap_create;
393 ic->ic_vap_delete = rsu_vap_delete;
394 ic->ic_update_mcast = rsu_update_mcast;
396 ieee80211_radiotap_attach(ic, &sc->sc_txtap.wt_ihdr,
397 sizeof(sc->sc_txtap), RSU_TX_RADIOTAP_PRESENT,
398 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
399 RSU_RX_RADIOTAP_PRESENT);
402 ieee80211_announce(ic);
408 usbd_transfer_unsetup(sc->sc_xfer, RSU_N_TRANSFER);
410 mtx_destroy(&sc->sc_mtx);
415 rsu_detach(device_t self)
417 struct rsu_softc *sc = device_get_softc(self);
418 struct ifnet *ifp = sc->sc_ifp;
419 struct ieee80211com *ic = ifp->if_l2com;
422 usbd_transfer_unsetup(sc->sc_xfer, RSU_N_TRANSFER);
423 ieee80211_ifdetach(ic);
425 taskqueue_drain_timeout(taskqueue_thread, &sc->calib_task);
427 /* Free Tx/Rx buffers. */
428 rsu_free_tx_list(sc);
429 rsu_free_rx_list(sc);
432 mtx_destroy(&sc->sc_mtx);
438 rsu_do_request(struct rsu_softc *sc, struct usb_device_request *req,
444 RSU_ASSERT_LOCKED(sc);
447 err = usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
448 req, data, 0, NULL, 250 /* ms */);
449 if (err == 0 || err == USB_ERR_NOT_CONFIGURED)
451 DPRINTFN(1, "Control request failed, %s (retrying)\n",
453 usb_pause_mtx(&sc->sc_mtx, hz / 100);
459 static struct ieee80211vap *
460 rsu_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
461 enum ieee80211_opmode opmode, int flags,
462 const uint8_t bssid[IEEE80211_ADDR_LEN],
463 const uint8_t mac[IEEE80211_ADDR_LEN])
466 struct ieee80211vap *vap;
468 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */
471 uvp = (struct rsu_vap *) malloc(sizeof(struct rsu_vap),
472 M_80211_VAP, M_NOWAIT | M_ZERO);
477 if (ieee80211_vap_setup(ic, vap, name, unit, opmode,
478 flags, bssid, mac) != 0) {
480 free(uvp, M_80211_VAP);
484 /* override state transition machine */
485 uvp->newstate = vap->iv_newstate;
486 vap->iv_newstate = rsu_newstate;
489 ieee80211_vap_attach(vap, ieee80211_media_change,
490 ieee80211_media_status);
491 ic->ic_opmode = opmode;
497 rsu_vap_delete(struct ieee80211vap *vap)
499 struct rsu_vap *uvp = RSU_VAP(vap);
501 ieee80211_vap_detach(vap);
502 free(uvp, M_80211_VAP);
506 rsu_scan_start(struct ieee80211com *ic)
509 struct ifnet *ifp = ic->ic_ifp;
510 struct rsu_softc *sc = ifp->if_softc;
512 /* Scanning is done by the firmware. */
514 error = rsu_site_survey(sc, TAILQ_FIRST(&ic->ic_vaps));
517 device_printf(sc->sc_dev,
518 "could not send site survey command\n");
522 rsu_scan_end(struct ieee80211com *ic)
524 /* Nothing to do here. */
528 rsu_set_channel(struct ieee80211com *ic __unused)
530 /* We are unable to switch channels, yet. */
534 rsu_update_mcast(struct ifnet *ifp)
536 /* XXX do nothing? */
540 rsu_alloc_list(struct rsu_softc *sc, struct rsu_data data[],
541 int ndata, int maxsz)
545 for (i = 0; i < ndata; i++) {
546 struct rsu_data *dp = &data[i];
549 dp->buf = malloc(maxsz, M_USBDEV, M_NOWAIT);
550 if (dp->buf == NULL) {
551 device_printf(sc->sc_dev,
552 "could not allocate buffer\n");
561 rsu_free_list(sc, data, ndata);
566 rsu_alloc_rx_list(struct rsu_softc *sc)
570 error = rsu_alloc_list(sc, sc->sc_rx, RSU_RX_LIST_COUNT,
575 STAILQ_INIT(&sc->sc_rx_active);
576 STAILQ_INIT(&sc->sc_rx_inactive);
578 for (i = 0; i < RSU_RX_LIST_COUNT; i++)
579 STAILQ_INSERT_HEAD(&sc->sc_rx_inactive, &sc->sc_rx[i], next);
585 rsu_alloc_tx_list(struct rsu_softc *sc)
589 error = rsu_alloc_list(sc, sc->sc_tx, RSU_TX_LIST_COUNT,
594 STAILQ_INIT(&sc->sc_tx_inactive);
596 for (i = 0; i != RSU_N_TRANSFER; i++) {
597 STAILQ_INIT(&sc->sc_tx_active[i]);
598 STAILQ_INIT(&sc->sc_tx_pending[i]);
601 for (i = 0; i < RSU_TX_LIST_COUNT; i++) {
602 STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, &sc->sc_tx[i], next);
609 rsu_free_tx_list(struct rsu_softc *sc)
613 /* prevent further allocations from TX list(s) */
614 STAILQ_INIT(&sc->sc_tx_inactive);
616 for (i = 0; i != RSU_N_TRANSFER; i++) {
617 STAILQ_INIT(&sc->sc_tx_active[i]);
618 STAILQ_INIT(&sc->sc_tx_pending[i]);
621 rsu_free_list(sc, sc->sc_tx, RSU_TX_LIST_COUNT);
625 rsu_free_rx_list(struct rsu_softc *sc)
627 /* prevent further allocations from RX list(s) */
628 STAILQ_INIT(&sc->sc_rx_inactive);
629 STAILQ_INIT(&sc->sc_rx_active);
631 rsu_free_list(sc, sc->sc_rx, RSU_RX_LIST_COUNT);
635 rsu_free_list(struct rsu_softc *sc, struct rsu_data data[], int ndata)
639 for (i = 0; i < ndata; i++) {
640 struct rsu_data *dp = &data[i];
642 if (dp->buf != NULL) {
643 free(dp->buf, M_USBDEV);
646 if (dp->ni != NULL) {
647 ieee80211_free_node(dp->ni);
653 static struct rsu_data *
654 _rsu_getbuf(struct rsu_softc *sc)
658 bf = STAILQ_FIRST(&sc->sc_tx_inactive);
660 STAILQ_REMOVE_HEAD(&sc->sc_tx_inactive, next);
664 DPRINTF("out of xmit buffers\n");
668 static struct rsu_data *
669 rsu_getbuf(struct rsu_softc *sc)
673 RSU_ASSERT_LOCKED(sc);
675 bf = _rsu_getbuf(sc);
677 struct ifnet *ifp = sc->sc_ifp;
678 DPRINTF("stop queue\n");
679 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
685 rsu_write_region_1(struct rsu_softc *sc, uint16_t addr, uint8_t *buf,
688 usb_device_request_t req;
690 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
691 req.bRequest = R92S_REQ_REGS;
692 USETW(req.wValue, addr);
693 USETW(req.wIndex, 0);
694 USETW(req.wLength, len);
696 return (rsu_do_request(sc, &req, buf));
700 rsu_write_1(struct rsu_softc *sc, uint16_t addr, uint8_t val)
702 rsu_write_region_1(sc, addr, &val, 1);
706 rsu_write_2(struct rsu_softc *sc, uint16_t addr, uint16_t val)
709 rsu_write_region_1(sc, addr, (uint8_t *)&val, 2);
713 rsu_write_4(struct rsu_softc *sc, uint16_t addr, uint32_t val)
716 rsu_write_region_1(sc, addr, (uint8_t *)&val, 4);
720 rsu_read_region_1(struct rsu_softc *sc, uint16_t addr, uint8_t *buf,
723 usb_device_request_t req;
725 req.bmRequestType = UT_READ_VENDOR_DEVICE;
726 req.bRequest = R92S_REQ_REGS;
727 USETW(req.wValue, addr);
728 USETW(req.wIndex, 0);
729 USETW(req.wLength, len);
731 return (rsu_do_request(sc, &req, buf));
735 rsu_read_1(struct rsu_softc *sc, uint16_t addr)
739 if (rsu_read_region_1(sc, addr, &val, 1) != 0)
745 rsu_read_2(struct rsu_softc *sc, uint16_t addr)
749 if (rsu_read_region_1(sc, addr, (uint8_t *)&val, 2) != 0)
751 return (le16toh(val));
755 rsu_read_4(struct rsu_softc *sc, uint16_t addr)
759 if (rsu_read_region_1(sc, addr, (uint8_t *)&val, 4) != 0)
761 return (le32toh(val));
765 rsu_fw_iocmd(struct rsu_softc *sc, uint32_t iocmd)
769 rsu_write_4(sc, R92S_IOCMD_CTRL, iocmd);
771 for (ntries = 0; ntries < 50; ntries++) {
772 if (rsu_read_4(sc, R92S_IOCMD_CTRL) == 0)
780 rsu_efuse_read_1(struct rsu_softc *sc, uint16_t addr)
785 reg = rsu_read_4(sc, R92S_EFUSE_CTRL);
786 reg = RW(reg, R92S_EFUSE_CTRL_ADDR, addr);
787 reg &= ~R92S_EFUSE_CTRL_VALID;
788 rsu_write_4(sc, R92S_EFUSE_CTRL, reg);
789 /* Wait for read operation to complete. */
790 for (ntries = 0; ntries < 100; ntries++) {
791 reg = rsu_read_4(sc, R92S_EFUSE_CTRL);
792 if (reg & R92S_EFUSE_CTRL_VALID)
793 return (MS(reg, R92S_EFUSE_CTRL_DATA));
796 device_printf(sc->sc_dev,
797 "could not read efuse byte at address 0x%x\n", addr);
802 rsu_read_rom(struct rsu_softc *sc)
804 uint8_t *rom = sc->rom;
810 /* Make sure that ROM type is eFuse and that autoload succeeded. */
811 reg = rsu_read_1(sc, R92S_EE_9346CR);
812 if ((reg & (R92S_9356SEL | R92S_EEPROM_EN)) != R92S_EEPROM_EN)
815 /* Turn on 2.5V to prevent eFuse leakage. */
816 reg = rsu_read_1(sc, R92S_EFUSE_TEST + 3);
817 rsu_write_1(sc, R92S_EFUSE_TEST + 3, reg | 0x80);
819 rsu_write_1(sc, R92S_EFUSE_TEST + 3, reg & ~0x80);
821 /* Read full ROM image. */
822 memset(&sc->rom, 0xff, sizeof(sc->rom));
824 reg = rsu_efuse_read_1(sc, addr);
830 for (i = 0; i < 4; i++) {
833 rom[off * 8 + i * 2 + 0] =
834 rsu_efuse_read_1(sc, addr);
836 rom[off * 8 + i * 2 + 1] =
837 rsu_efuse_read_1(sc, addr);
842 if (rsu_debug >= 5) {
843 /* Dump ROM content. */
845 for (i = 0; i < sizeof(sc->rom); i++)
846 printf("%02x:", rom[i]);
854 rsu_fw_cmd(struct rsu_softc *sc, uint8_t code, void *buf, int len)
856 const uint8_t which = rsu_wme_ac_xfer_map[WME_AC_VO];
857 struct rsu_data *data;
858 struct r92s_tx_desc *txd;
859 struct r92s_fw_cmd_hdr *cmd;
863 data = rsu_getbuf(sc);
867 /* Round-up command length to a multiple of 8 bytes. */
868 cmdsz = (len + 7) & ~7;
870 xferlen = sizeof(*txd) + sizeof(*cmd) + cmdsz;
871 KASSERT(xferlen <= RSU_TXBUFSZ, ("%s: invalid length", __func__));
872 memset(data->buf, 0, xferlen);
874 /* Setup Tx descriptor. */
875 txd = (struct r92s_tx_desc *)data->buf;
876 txd->txdw0 = htole32(
877 SM(R92S_TXDW0_OFFSET, sizeof(*txd)) |
878 SM(R92S_TXDW0_PKTLEN, sizeof(*cmd) + cmdsz) |
879 R92S_TXDW0_OWN | R92S_TXDW0_FSG | R92S_TXDW0_LSG);
880 txd->txdw1 = htole32(SM(R92S_TXDW1_QSEL, R92S_TXDW1_QSEL_H2C));
882 /* Setup command header. */
883 cmd = (struct r92s_fw_cmd_hdr *)&txd[1];
884 cmd->len = htole16(cmdsz);
886 cmd->seq = sc->cmd_seq;
887 sc->cmd_seq = (sc->cmd_seq + 1) & 0x7f;
889 /* Copy command payload. */
890 memcpy(&cmd[1], buf, len);
892 DPRINTFN(2, "Tx cmd code=0x%x len=0x%x\n", code, cmdsz);
893 data->buflen = xferlen;
894 STAILQ_INSERT_TAIL(&sc->sc_tx_pending[which], data, next);
895 usbd_transfer_start(sc->sc_xfer[which]);
902 rsu_calib_task(void *arg, int pending __unused)
904 struct rsu_softc *sc = arg;
907 DPRINTFN(6, "running calibration task\n");
911 /* Read WPS PBC status. */
912 rsu_write_1(sc, R92S_MAC_PINMUX_CTRL,
913 R92S_GPIOMUX_EN | SM(R92S_GPIOSEL_GPIO, R92S_GPIOSEL_GPIO_JTAG));
914 rsu_write_1(sc, R92S_GPIO_IO_SEL,
915 rsu_read_1(sc, R92S_GPIO_IO_SEL) & ~R92S_GPIO_WPS);
916 reg = rsu_read_1(sc, R92S_GPIO_CTRL);
917 if (reg != 0xff && (reg & R92S_GPIO_WPS))
918 DPRINTF(("WPS PBC is pushed\n"));
920 /* Read current signal level. */
921 if (rsu_fw_iocmd(sc, 0xf4000001) == 0) {
922 reg = rsu_read_4(sc, R92S_IOCMD_DATA);
923 DPRINTFN(8, "RSSI=%d%%\n", reg >> 4);
925 if (sc->sc_calibrating)
926 taskqueue_enqueue_timeout(taskqueue_thread, &sc->calib_task, hz);
931 rsu_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
933 struct rsu_vap *uvp = RSU_VAP(vap);
934 struct ieee80211com *ic = vap->iv_ic;
935 struct rsu_softc *sc = ic->ic_ifp->if_softc;
936 struct ieee80211_node *ni;
937 struct ieee80211_rateset *rs;
938 enum ieee80211_state ostate;
939 int error, startcal = 0;
941 ostate = vap->iv_state;
942 DPRINTF("%s -> %s\n", ieee80211_state_name[ostate],
943 ieee80211_state_name[nstate]);
945 IEEE80211_UNLOCK(ic);
946 if (ostate == IEEE80211_S_RUN) {
948 /* Stop calibration. */
949 sc->sc_calibrating = 0;
951 taskqueue_drain_timeout(taskqueue_thread, &sc->calib_task);
952 /* Disassociate from our current BSS. */
958 case IEEE80211_S_INIT:
960 case IEEE80211_S_AUTH:
961 ni = ieee80211_ref_node(vap->iv_bss);
962 error = rsu_join_bss(sc, ni);
963 ieee80211_free_node(ni);
965 device_printf(sc->sc_dev,
966 "could not send join command\n");
969 case IEEE80211_S_RUN:
970 ni = ieee80211_ref_node(vap->iv_bss);
972 /* Indicate highest supported rate. */
973 ni->ni_txrate = rs->rs_rates[rs->rs_nrates - 1];
974 ieee80211_free_node(ni);
980 sc->sc_calibrating = 1;
981 /* Start periodic calibration. */
982 taskqueue_enqueue_timeout(taskqueue_thread, &sc->calib_task, hz);
985 return (uvp->newstate(vap, nstate, arg));
990 rsu_set_key(struct rsu_softc *sc, const struct ieee80211_key *k)
992 struct r92s_fw_cmd_set_key key;
994 memset(&key, 0, sizeof(key));
995 /* Map net80211 cipher to HW crypto algorithm. */
996 switch (k->wk_cipher->ic_cipher) {
997 case IEEE80211_CIPHER_WEP:
998 if (k->wk_keylen < 8)
999 key.algo = R92S_KEY_ALGO_WEP40;
1001 key.algo = R92S_KEY_ALGO_WEP104;
1003 case IEEE80211_CIPHER_TKIP:
1004 key.algo = R92S_KEY_ALGO_TKIP;
1006 case IEEE80211_CIPHER_AES_CCM:
1007 key.algo = R92S_KEY_ALGO_AES;
1012 key.id = k->wk_keyix;
1013 key.grpkey = (k->wk_flags & IEEE80211_KEY_GROUP) != 0;
1014 memcpy(key.key, k->wk_key, MIN(k->wk_keylen, sizeof(key.key)));
1015 (void)rsu_fw_cmd(sc, R92S_CMD_SET_KEY, &key, sizeof(key));
1019 rsu_delete_key(struct rsu_softc *sc, const struct ieee80211_key *k)
1021 struct r92s_fw_cmd_set_key key;
1023 memset(&key, 0, sizeof(key));
1024 key.id = k->wk_keyix;
1025 (void)rsu_fw_cmd(sc, R92S_CMD_SET_KEY, &key, sizeof(key));
1030 rsu_site_survey(struct rsu_softc *sc, struct ieee80211vap *vap)
1032 struct r92s_fw_cmd_sitesurvey cmd;
1033 struct ifnet *ifp = sc->sc_ifp;
1034 struct ieee80211com *ic = ifp->if_l2com;
1036 memset(&cmd, 0, sizeof(cmd));
1037 if ((ic->ic_flags & IEEE80211_F_ASCAN) || sc->scan_pass == 1)
1038 cmd.active = htole32(1);
1039 cmd.limit = htole32(48);
1040 if (sc->scan_pass == 1 && vap->iv_des_nssid > 0) {
1041 /* Do a directed scan for second pass. */
1042 cmd.ssidlen = htole32(vap->iv_des_ssid[0].len);
1043 memcpy(cmd.ssid, vap->iv_des_ssid[0].ssid,
1044 vap->iv_des_ssid[0].len);
1047 DPRINTF("sending site survey command, pass=%d\n", sc->scan_pass);
1048 return (rsu_fw_cmd(sc, R92S_CMD_SITE_SURVEY, &cmd, sizeof(cmd)));
1052 rsu_join_bss(struct rsu_softc *sc, struct ieee80211_node *ni)
1054 struct ifnet *ifp = sc->sc_ifp;
1055 struct ieee80211com *ic = ifp->if_l2com;
1056 struct ieee80211vap *vap = ni->ni_vap;
1057 struct ndis_wlan_bssid_ex *bss;
1058 struct ndis_802_11_fixed_ies *fixed;
1059 struct r92s_fw_cmd_auth auth;
1060 uint8_t buf[sizeof(*bss) + 128] __aligned(4);
1065 /* Let the FW decide the opmode based on the capinfo field. */
1066 opmode = NDIS802_11AUTOUNKNOWN;
1067 DPRINTF("setting operating mode to %d\n", opmode);
1068 error = rsu_fw_cmd(sc, R92S_CMD_SET_OPMODE, &opmode, sizeof(opmode));
1072 memset(&auth, 0, sizeof(auth));
1073 if (vap->iv_flags & IEEE80211_F_WPA) {
1074 auth.mode = R92S_AUTHMODE_WPA;
1075 auth.dot1x = (ni->ni_authmode == IEEE80211_AUTH_8021X);
1077 auth.mode = R92S_AUTHMODE_OPEN;
1078 DPRINTF("setting auth mode to %d\n", auth.mode);
1079 error = rsu_fw_cmd(sc, R92S_CMD_SET_AUTH, &auth, sizeof(auth));
1083 memset(buf, 0, sizeof(buf));
1084 bss = (struct ndis_wlan_bssid_ex *)buf;
1085 IEEE80211_ADDR_COPY(bss->macaddr, ni->ni_bssid);
1086 bss->ssid.ssidlen = htole32(ni->ni_esslen);
1087 memcpy(bss->ssid.ssid, ni->ni_essid, ni->ni_esslen);
1088 if (vap->iv_flags & (IEEE80211_F_PRIVACY | IEEE80211_F_WPA))
1089 bss->privacy = htole32(1);
1090 bss->rssi = htole32(ni->ni_avgrssi);
1091 if (ic->ic_curmode == IEEE80211_MODE_11B)
1092 bss->networktype = htole32(NDIS802_11DS);
1094 bss->networktype = htole32(NDIS802_11OFDM24);
1095 bss->config.len = htole32(sizeof(bss->config));
1096 bss->config.bintval = htole32(ni->ni_intval);
1097 bss->config.dsconfig = htole32(ieee80211_chan2ieee(ic, ni->ni_chan));
1098 bss->inframode = htole32(NDIS802_11INFRASTRUCTURE);
1099 memcpy(bss->supprates, ni->ni_rates.rs_rates,
1100 ni->ni_rates.rs_nrates);
1101 /* Write the fixed fields of the beacon frame. */
1102 fixed = (struct ndis_802_11_fixed_ies *)&bss[1];
1103 memcpy(&fixed->tstamp, ni->ni_tstamp.data, 8);
1104 fixed->bintval = htole16(ni->ni_intval);
1105 fixed->capabilities = htole16(ni->ni_capinfo);
1106 /* Write IEs to be included in the association request. */
1107 frm = (uint8_t *)&fixed[1];
1108 frm = ieee80211_add_rsn(frm, vap);
1109 frm = ieee80211_add_wpa(frm, vap);
1110 frm = ieee80211_add_qos(frm, ni);
1111 if (ni->ni_flags & IEEE80211_NODE_HT)
1112 frm = ieee80211_add_htcap(frm, ni);
1113 bss->ieslen = htole32(frm - (uint8_t *)fixed);
1114 bss->len = htole32(((frm - buf) + 3) & ~3);
1115 DPRINTF("sending join bss command to %s chan %d\n",
1116 ether_sprintf(bss->macaddr), le32toh(bss->config.dsconfig));
1117 return (rsu_fw_cmd(sc, R92S_CMD_JOIN_BSS, buf, sizeof(buf)));
1121 rsu_disconnect(struct rsu_softc *sc)
1123 uint32_t zero = 0; /* :-) */
1125 /* Disassociate from our current BSS. */
1126 DPRINTF("sending disconnect command\n");
1127 return (rsu_fw_cmd(sc, R92S_CMD_DISCONNECT, &zero, sizeof(zero)));
1131 rsu_event_survey(struct rsu_softc *sc, uint8_t *buf, int len)
1133 struct ifnet *ifp = sc->sc_ifp;
1134 struct ieee80211com *ic = ifp->if_l2com;
1135 struct ieee80211_frame *wh;
1136 struct ieee80211_channel *c;
1137 struct ndis_wlan_bssid_ex *bss;
1141 if (__predict_false(len < sizeof(*bss)))
1143 bss = (struct ndis_wlan_bssid_ex *)buf;
1144 if (__predict_false(len < sizeof(*bss) + le32toh(bss->ieslen)))
1147 DPRINTFN(2, "found BSS %s: len=%d chan=%d inframode=%d "
1148 "networktype=%d privacy=%d\n",
1149 ether_sprintf(bss->macaddr), le32toh(bss->len),
1150 le32toh(bss->config.dsconfig), le32toh(bss->inframode),
1151 le32toh(bss->networktype), le32toh(bss->privacy));
1153 /* Build a fake beacon frame to let net80211 do all the parsing. */
1154 pktlen = sizeof(*wh) + le32toh(bss->ieslen);
1155 if (__predict_false(pktlen > MCLBYTES))
1157 m = m_get2(pktlen, M_NOWAIT, MT_DATA, M_PKTHDR);
1158 if (__predict_false(m == NULL))
1160 wh = mtod(m, struct ieee80211_frame *);
1161 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
1162 IEEE80211_FC0_SUBTYPE_BEACON;
1163 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1164 USETW(wh->i_dur, 0);
1165 IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
1166 IEEE80211_ADDR_COPY(wh->i_addr2, bss->macaddr);
1167 IEEE80211_ADDR_COPY(wh->i_addr3, bss->macaddr);
1168 *(uint16_t *)wh->i_seq = 0;
1169 memcpy(&wh[1], (uint8_t *)&bss[1], le32toh(bss->ieslen));
1171 /* Finalize mbuf. */
1172 m->m_pkthdr.len = m->m_len = pktlen;
1173 m->m_pkthdr.rcvif = ifp;
1174 /* Fix the channel. */
1175 c = ieee80211_find_channel_byieee(ic,
1176 le32toh(bss->config.dsconfig),
1180 ieee80211_radiotap_chan_change(ic);
1182 /* XXX avoid a LOR */
1184 ieee80211_input_all(ic, m, le32toh(bss->rssi), 0);
1189 rsu_event_join_bss(struct rsu_softc *sc, uint8_t *buf, int len)
1191 struct ifnet *ifp = sc->sc_ifp;
1192 struct ieee80211com *ic = ifp->if_l2com;
1193 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1194 struct ieee80211_node *ni = vap->iv_bss;
1195 struct r92s_event_join_bss *rsp;
1199 if (__predict_false(len < sizeof(*rsp)))
1201 rsp = (struct r92s_event_join_bss *)buf;
1202 res = (int)le32toh(rsp->join_res);
1204 DPRINTF("Rx join BSS event len=%d res=%d\n", len, res);
1207 ieee80211_new_state(vap, IEEE80211_S_SCAN, -1);
1211 tmp = le32toh(rsp->associd);
1212 if (tmp >= vap->iv_max_aid) {
1213 DPRINTF("Assoc ID overflow\n");
1216 DPRINTF("associated with %s associd=%d\n",
1217 ether_sprintf(rsp->bss.macaddr), tmp);
1218 ni->ni_associd = tmp | 0xc000;
1220 ieee80211_new_state(vap, IEEE80211_S_RUN,
1221 IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
1226 rsu_rx_event(struct rsu_softc *sc, uint8_t code, uint8_t *buf, int len)
1228 struct ifnet *ifp = sc->sc_ifp;
1229 struct ieee80211com *ic = ifp->if_l2com;
1230 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1232 DPRINTFN(4, "Rx event code=%d len=%d\n", code, len);
1234 case R92S_EVT_SURVEY:
1235 if (vap->iv_state == IEEE80211_S_SCAN)
1236 rsu_event_survey(sc, buf, len);
1238 case R92S_EVT_SURVEY_DONE:
1239 DPRINTF("site survey pass %d done, found %d BSS\n",
1240 sc->scan_pass, le32toh(*(uint32_t *)buf));
1241 if (vap->iv_state != IEEE80211_S_SCAN)
1242 break; /* Ignore if not scanning. */
1243 if (sc->scan_pass == 0 && vap->iv_des_nssid != 0) {
1244 /* Schedule a directed scan for hidden APs. */
1247 ieee80211_new_state(vap, IEEE80211_S_SCAN, -1);
1253 case R92S_EVT_JOIN_BSS:
1254 if (vap->iv_state == IEEE80211_S_AUTH)
1255 rsu_event_join_bss(sc, buf, len);
1258 XXX This event is occurring regularly, possibly due to some power saving event
1259 XXX and disrupts the WLAN traffic. Disable for now.
1260 case R92S_EVT_DEL_STA:
1261 DPRINTF("disassociated from %s\n", ether_sprintf(buf));
1262 if (vap->iv_state == IEEE80211_S_RUN &&
1263 IEEE80211_ADDR_EQ(vap->iv_bss->ni_bssid, buf)) {
1265 ieee80211_new_state(vap, IEEE80211_S_SCAN, -1);
1270 case R92S_EVT_WPS_PBC:
1271 DPRINTF("WPS PBC pushed.\n");
1273 case R92S_EVT_FWDBG:
1274 if (ifp->if_flags & IFF_DEBUG) {
1276 printf("FWDBG: %s\n", (char *)buf);
1285 rsu_rx_multi_event(struct rsu_softc *sc, uint8_t *buf, int len)
1287 struct r92s_fw_cmd_hdr *cmd;
1290 DPRINTFN(6, "Rx events len=%d\n", len);
1292 /* Skip Rx status. */
1293 buf += sizeof(struct r92s_rx_stat);
1294 len -= sizeof(struct r92s_rx_stat);
1296 /* Process all events. */
1298 /* Check that command header fits. */
1299 if (__predict_false(len < sizeof(*cmd)))
1301 cmd = (struct r92s_fw_cmd_hdr *)buf;
1302 /* Check that command payload fits. */
1303 cmdsz = le16toh(cmd->len);
1304 if (__predict_false(len < sizeof(*cmd) + cmdsz))
1307 /* Process firmware event. */
1308 rsu_rx_event(sc, cmd->code, (uint8_t *)&cmd[1], cmdsz);
1310 if (!(cmd->seq & R92S_FW_CMD_MORE))
1312 buf += sizeof(*cmd) + cmdsz;
1313 len -= sizeof(*cmd) + cmdsz;
1318 rsu_get_rssi(struct rsu_softc *sc, int rate, void *physt)
1320 static const int8_t cckoff[] = { 14, -2, -20, -40 };
1321 struct r92s_rx_phystat *phy;
1322 struct r92s_rx_cck *cck;
1327 cck = (struct r92s_rx_cck *)physt;
1328 rpt = (cck->agc_rpt >> 6) & 0x3;
1329 rssi = cck->agc_rpt & 0x3e;
1330 rssi = cckoff[rpt] - rssi;
1331 } else { /* OFDM/HT. */
1332 phy = (struct r92s_rx_phystat *)physt;
1333 rssi = ((le32toh(phy->phydw1) >> 1) & 0x7f) - 106;
1338 static struct mbuf *
1339 rsu_rx_frame(struct rsu_softc *sc, uint8_t *buf, int pktlen, int *rssi)
1341 struct ifnet *ifp = sc->sc_ifp;
1342 struct ieee80211com *ic = ifp->if_l2com;
1343 struct ieee80211_frame *wh;
1344 struct r92s_rx_stat *stat;
1345 uint32_t rxdw0, rxdw3;
1350 stat = (struct r92s_rx_stat *)buf;
1351 rxdw0 = le32toh(stat->rxdw0);
1352 rxdw3 = le32toh(stat->rxdw3);
1354 if (__predict_false(rxdw0 & R92S_RXDW0_CRCERR)) {
1355 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1358 if (__predict_false(pktlen < sizeof(*wh) || pktlen > MCLBYTES)) {
1359 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1363 rate = MS(rxdw3, R92S_RXDW3_RATE);
1364 infosz = MS(rxdw0, R92S_RXDW0_INFOSZ) * 8;
1366 /* Get RSSI from PHY status descriptor if present. */
1368 *rssi = rsu_get_rssi(sc, rate, &stat[1]);
1372 DPRINTFN(5, "Rx frame len=%d rate=%d infosz=%d rssi=%d\n",
1373 pktlen, rate, infosz, *rssi);
1375 m = m_get2(pktlen, M_NOWAIT, MT_DATA, M_PKTHDR);
1376 if (__predict_false(m == NULL)) {
1377 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1380 /* Finalize mbuf. */
1381 m->m_pkthdr.rcvif = ifp;
1382 /* Hardware does Rx TCP checksum offload. */
1383 if (rxdw3 & R92S_RXDW3_TCPCHKVALID) {
1384 if (__predict_true(rxdw3 & R92S_RXDW3_TCPCHKRPT))
1385 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID;
1387 wh = (struct ieee80211_frame *)((uint8_t *)&stat[1] + infosz);
1388 memcpy(mtod(m, uint8_t *), wh, pktlen);
1389 m->m_pkthdr.len = m->m_len = pktlen;
1391 if (ieee80211_radiotap_active(ic)) {
1392 struct rsu_rx_radiotap_header *tap = &sc->sc_rxtap;
1394 /* Map HW rate index to 802.11 rate. */
1396 if (!(rxdw3 & R92S_RXDW3_HTC)) {
1399 case 0: tap->wr_rate = 2; break;
1400 case 1: tap->wr_rate = 4; break;
1401 case 2: tap->wr_rate = 11; break;
1402 case 3: tap->wr_rate = 22; break;
1404 case 4: tap->wr_rate = 12; break;
1405 case 5: tap->wr_rate = 18; break;
1406 case 6: tap->wr_rate = 24; break;
1407 case 7: tap->wr_rate = 36; break;
1408 case 8: tap->wr_rate = 48; break;
1409 case 9: tap->wr_rate = 72; break;
1410 case 10: tap->wr_rate = 96; break;
1411 case 11: tap->wr_rate = 108; break;
1413 } else if (rate >= 12) { /* MCS0~15. */
1414 /* Bit 7 set means HT MCS instead of rate. */
1415 tap->wr_rate = 0x80 | (rate - 12);
1417 tap->wr_dbm_antsignal = *rssi;
1418 tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
1419 tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
1425 static struct mbuf *
1426 rsu_rx_multi_frame(struct rsu_softc *sc, uint8_t *buf, int len, int *rssi)
1428 struct r92s_rx_stat *stat;
1430 int totlen, pktlen, infosz, npkts;
1431 struct mbuf *m, *m0 = NULL, *prevm = NULL;
1433 /* Get the number of encapsulated frames. */
1434 stat = (struct r92s_rx_stat *)buf;
1435 npkts = MS(le32toh(stat->rxdw2), R92S_RXDW2_PKTCNT);
1436 DPRINTFN(6, "Rx %d frames in one chunk\n", npkts);
1438 /* Process all of them. */
1439 while (npkts-- > 0) {
1440 if (__predict_false(len < sizeof(*stat)))
1442 stat = (struct r92s_rx_stat *)buf;
1443 rxdw0 = le32toh(stat->rxdw0);
1445 pktlen = MS(rxdw0, R92S_RXDW0_PKTLEN);
1446 if (__predict_false(pktlen == 0))
1449 infosz = MS(rxdw0, R92S_RXDW0_INFOSZ) * 8;
1451 /* Make sure everything fits in xfer. */
1452 totlen = sizeof(*stat) + infosz + pktlen;
1453 if (__predict_false(totlen > len))
1456 /* Process 802.11 frame. */
1457 m = rsu_rx_frame(sc, buf, pktlen, rssi);
1466 /* Next chunk is 128-byte aligned. */
1467 totlen = (totlen + 127) & ~127;
1475 static struct mbuf *
1476 rsu_rxeof(struct usb_xfer *xfer, struct rsu_data *data, int *rssi)
1478 struct rsu_softc *sc = data->sc;
1479 struct r92s_rx_stat *stat;
1482 usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
1484 if (__predict_false(len < sizeof(*stat))) {
1485 DPRINTF("xfer too short %d\n", len);
1486 if_inc_counter(sc->sc_ifp, IFCOUNTER_IERRORS, 1);
1489 /* Determine if it is a firmware C2H event or an 802.11 frame. */
1490 stat = (struct r92s_rx_stat *)data->buf;
1491 if ((le32toh(stat->rxdw1) & 0x1ff) == 0x1ff) {
1492 rsu_rx_multi_event(sc, data->buf, len);
1493 /* No packets to process. */
1496 return (rsu_rx_multi_frame(sc, data->buf, len, rssi));
1500 rsu_bulk_rx_callback(struct usb_xfer *xfer, usb_error_t error)
1502 struct rsu_softc *sc = usbd_xfer_softc(xfer);
1503 struct ifnet *ifp = sc->sc_ifp;
1504 struct ieee80211com *ic = ifp->if_l2com;
1505 struct ieee80211_frame *wh;
1506 struct ieee80211_node *ni;
1507 struct mbuf *m = NULL, *next;
1508 struct rsu_data *data;
1511 RSU_ASSERT_LOCKED(sc);
1513 switch (USB_GET_STATE(xfer)) {
1514 case USB_ST_TRANSFERRED:
1515 data = STAILQ_FIRST(&sc->sc_rx_active);
1518 STAILQ_REMOVE_HEAD(&sc->sc_rx_active, next);
1519 m = rsu_rxeof(xfer, data, &rssi);
1520 STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next);
1524 data = STAILQ_FIRST(&sc->sc_rx_inactive);
1526 KASSERT(m == NULL, ("mbuf isn't NULL"));
1529 STAILQ_REMOVE_HEAD(&sc->sc_rx_inactive, next);
1530 STAILQ_INSERT_TAIL(&sc->sc_rx_active, data, next);
1531 usbd_xfer_set_frame_data(xfer, 0, data->buf,
1532 usbd_xfer_max_len(xfer));
1533 usbd_transfer_submit(xfer);
1535 * To avoid LOR we should unlock our private mutex here to call
1536 * ieee80211_input() because here is at the end of a USB
1537 * callback and safe to unlock.
1543 wh = mtod(m, struct ieee80211_frame *);
1544 ni = ieee80211_find_rxnode(ic,
1545 (struct ieee80211_frame_min *)wh);
1547 (void)ieee80211_input(ni, m, rssi, 0);
1548 ieee80211_free_node(ni);
1550 (void)ieee80211_input_all(ic, m, rssi, 0);
1556 /* needs it to the inactive queue due to a error. */
1557 data = STAILQ_FIRST(&sc->sc_rx_active);
1559 STAILQ_REMOVE_HEAD(&sc->sc_rx_active, next);
1560 STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next);
1562 if (error != USB_ERR_CANCELLED) {
1563 usbd_xfer_set_stall(xfer);
1564 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1574 rsu_txeof(struct usb_xfer *xfer, struct rsu_data *data)
1576 struct rsu_softc *sc = usbd_xfer_softc(xfer);
1577 struct ifnet *ifp = sc->sc_ifp;
1580 RSU_ASSERT_LOCKED(sc);
1583 * Do any tx complete callback. Note this must be done before releasing
1584 * the node reference.
1588 if (m->m_flags & M_TXCB) {
1590 ieee80211_process_callback(data->ni, m, 0);
1596 ieee80211_free_node(data->ni);
1599 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1600 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1604 rsu_bulk_tx_callback_sub(struct usb_xfer *xfer, usb_error_t error,
1607 struct rsu_softc *sc = usbd_xfer_softc(xfer);
1608 struct ifnet *ifp = sc->sc_ifp;
1609 struct rsu_data *data;
1611 RSU_ASSERT_LOCKED(sc);
1613 switch (USB_GET_STATE(xfer)) {
1614 case USB_ST_TRANSFERRED:
1615 data = STAILQ_FIRST(&sc->sc_tx_active[which]);
1618 DPRINTF("transfer done %p\n", data);
1619 STAILQ_REMOVE_HEAD(&sc->sc_tx_active[which], next);
1620 rsu_txeof(xfer, data);
1621 STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data, next);
1625 data = STAILQ_FIRST(&sc->sc_tx_pending[which]);
1627 DPRINTF("empty pending queue sc %p\n", sc);
1630 STAILQ_REMOVE_HEAD(&sc->sc_tx_pending[which], next);
1631 STAILQ_INSERT_TAIL(&sc->sc_tx_active[which], data, next);
1632 usbd_xfer_set_frame_data(xfer, 0, data->buf, data->buflen);
1633 DPRINTF("submitting transfer %p\n", data);
1634 usbd_transfer_submit(xfer);
1637 data = STAILQ_FIRST(&sc->sc_tx_active[which]);
1639 STAILQ_REMOVE_HEAD(&sc->sc_tx_active[which], next);
1640 rsu_txeof(xfer, data);
1641 STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data, next);
1643 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1645 if (error != USB_ERR_CANCELLED) {
1646 usbd_xfer_set_stall(xfer);
1654 rsu_bulk_tx_callback_be_bk(struct usb_xfer *xfer, usb_error_t error)
1656 rsu_bulk_tx_callback_sub(xfer, error, RSU_BULK_TX_BE_BK);
1660 rsu_bulk_tx_callback_vi_vo(struct usb_xfer *xfer, usb_error_t error)
1662 rsu_bulk_tx_callback_sub(xfer, error, RSU_BULK_TX_VI_VO);
1666 rsu_tx_start(struct rsu_softc *sc, struct ieee80211_node *ni,
1667 struct mbuf *m0, struct rsu_data *data)
1669 struct ifnet *ifp = sc->sc_ifp;
1670 struct ieee80211com *ic = ifp->if_l2com;
1671 struct ieee80211vap *vap = ni->ni_vap;
1672 struct ieee80211_frame *wh;
1673 struct ieee80211_key *k = NULL;
1674 struct r92s_tx_desc *txd;
1681 RSU_ASSERT_LOCKED(sc);
1683 wh = mtod(m0, struct ieee80211_frame *);
1684 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1686 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1687 k = ieee80211_crypto_encap(ni, m0);
1689 device_printf(sc->sc_dev,
1690 "ieee80211_crypto_encap returns NULL.\n");
1691 /* XXX we don't expect the fragmented frames */
1695 wh = mtod(m0, struct ieee80211_frame *);
1698 case IEEE80211_FC0_TYPE_CTL:
1699 case IEEE80211_FC0_TYPE_MGT:
1700 which = rsu_wme_ac_xfer_map[WME_AC_VO];
1703 which = rsu_wme_ac_xfer_map[M_WME_GETAC(m0)];
1708 /* Fill Tx descriptor. */
1709 txd = (struct r92s_tx_desc *)data->buf;
1710 memset(txd, 0, sizeof(*txd));
1712 txd->txdw0 |= htole32(
1713 SM(R92S_TXDW0_PKTLEN, m0->m_pkthdr.len) |
1714 SM(R92S_TXDW0_OFFSET, sizeof(*txd)) |
1715 R92S_TXDW0_OWN | R92S_TXDW0_FSG | R92S_TXDW0_LSG);
1717 txd->txdw1 |= htole32(
1718 SM(R92S_TXDW1_MACID, R92S_MACID_BSS) |
1719 SM(R92S_TXDW1_QSEL, R92S_TXDW1_QSEL_BE));
1721 txd->txdw1 |= htole32(R92S_TXDW1_NONQOS);
1724 switch (k->wk_cipher->ic_cipher) {
1725 case IEEE80211_CIPHER_WEP:
1726 cipher = R92S_TXDW1_CIPHER_WEP;
1728 case IEEE80211_CIPHER_TKIP:
1729 cipher = R92S_TXDW1_CIPHER_TKIP;
1731 case IEEE80211_CIPHER_AES_CCM:
1732 cipher = R92S_TXDW1_CIPHER_AES;
1735 cipher = R92S_TXDW1_CIPHER_NONE;
1737 txd->txdw1 |= htole32(
1738 SM(R92S_TXDW1_CIPHER, cipher) |
1739 SM(R92S_TXDW1_KEYIDX, k->k_id));
1742 txd->txdw2 |= htole32(R92S_TXDW2_BK);
1743 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1744 txd->txdw2 |= htole32(R92S_TXDW2_BMCAST);
1746 * Firmware will use and increment the sequence number for the
1749 txd->txdw3 |= htole32(SM(R92S_TXDW3_SEQ, tid));
1751 if (ieee80211_radiotap_active_vap(vap)) {
1752 struct rsu_tx_radiotap_header *tap = &sc->sc_txtap;
1755 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1756 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1757 ieee80211_radiotap_tx(vap, m0);
1759 xferlen = sizeof(*txd) + m0->m_pkthdr.len;
1760 m_copydata(m0, 0, m0->m_pkthdr.len, (caddr_t)&txd[1]);
1762 data->buflen = xferlen;
1765 STAILQ_INSERT_TAIL(&sc->sc_tx_pending[which], data, next);
1767 /* start transfer, if any */
1768 usbd_transfer_start(sc->sc_xfer[which]);
1773 rsu_start(struct ifnet *ifp)
1775 struct rsu_softc *sc = ifp->if_softc;
1777 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1781 rsu_start_locked(ifp);
1786 rsu_start_locked(struct ifnet *ifp)
1788 struct rsu_softc *sc = ifp->if_softc;
1789 struct ieee80211_node *ni;
1790 struct rsu_data *bf;
1793 RSU_ASSERT_LOCKED(sc);
1796 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1799 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
1800 m->m_pkthdr.rcvif = NULL;
1802 bf = rsu_getbuf(sc);
1804 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1806 ieee80211_free_node(ni);
1807 } else if (rsu_tx_start(sc, ni, m, bf) != 0) {
1808 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1809 STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, bf, next);
1810 ieee80211_free_node(ni);
1816 rsu_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1818 struct ieee80211com *ic = ifp->if_l2com;
1819 struct ifreq *ifr = (struct ifreq *) data;
1820 int error = 0, startall = 0;
1824 if (ifp->if_flags & IFF_UP) {
1825 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1826 rsu_init(ifp->if_softc);
1830 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1834 ieee80211_start_all(ic);
1837 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
1840 error = ether_ioctl(ifp, cmd, data);
1851 * Power on sequence for A-cut adapters.
1854 rsu_power_on_acut(struct rsu_softc *sc)
1858 rsu_write_1(sc, R92S_SPS0_CTRL + 1, 0x53);
1859 rsu_write_1(sc, R92S_SPS0_CTRL + 0, 0x57);
1861 /* Enable AFE macro block's bandgap and Mbias. */
1862 rsu_write_1(sc, R92S_AFE_MISC,
1863 rsu_read_1(sc, R92S_AFE_MISC) |
1864 R92S_AFE_MISC_BGEN | R92S_AFE_MISC_MBEN);
1865 /* Enable LDOA15 block. */
1866 rsu_write_1(sc, R92S_LDOA15_CTRL,
1867 rsu_read_1(sc, R92S_LDOA15_CTRL) | R92S_LDA15_EN);
1869 rsu_write_1(sc, R92S_SPS1_CTRL,
1870 rsu_read_1(sc, R92S_SPS1_CTRL) | R92S_SPS1_LDEN);
1871 usb_pause_mtx(&sc->sc_mtx, 2 * hz);
1872 /* Enable switch regulator block. */
1873 rsu_write_1(sc, R92S_SPS1_CTRL,
1874 rsu_read_1(sc, R92S_SPS1_CTRL) | R92S_SPS1_SWEN);
1876 rsu_write_4(sc, R92S_SPS1_CTRL, 0x00a7b267);
1878 rsu_write_1(sc, R92S_SYS_ISO_CTRL + 1,
1879 rsu_read_1(sc, R92S_SYS_ISO_CTRL + 1) | 0x08);
1881 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1,
1882 rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) | 0x20);
1884 rsu_write_1(sc, R92S_SYS_ISO_CTRL + 1,
1885 rsu_read_1(sc, R92S_SYS_ISO_CTRL + 1) & ~0x90);
1887 /* Enable AFE clock. */
1888 rsu_write_1(sc, R92S_AFE_XTAL_CTRL + 1,
1889 rsu_read_1(sc, R92S_AFE_XTAL_CTRL + 1) & ~0x04);
1890 /* Enable AFE PLL macro block. */
1891 rsu_write_1(sc, R92S_AFE_PLL_CTRL,
1892 rsu_read_1(sc, R92S_AFE_PLL_CTRL) | 0x11);
1893 /* Attach AFE PLL to MACTOP/BB. */
1894 rsu_write_1(sc, R92S_SYS_ISO_CTRL,
1895 rsu_read_1(sc, R92S_SYS_ISO_CTRL) & ~0x11);
1897 /* Switch to 40MHz clock instead of 80MHz. */
1898 rsu_write_2(sc, R92S_SYS_CLKR,
1899 rsu_read_2(sc, R92S_SYS_CLKR) & ~R92S_SYS_CLKSEL);
1901 /* Enable MAC clock. */
1902 rsu_write_2(sc, R92S_SYS_CLKR,
1903 rsu_read_2(sc, R92S_SYS_CLKR) |
1904 R92S_MAC_CLK_EN | R92S_SYS_CLK_EN);
1906 rsu_write_1(sc, R92S_PMC_FSM, 0x02);
1908 /* Enable digital core and IOREG R/W. */
1909 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1,
1910 rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) | 0x08);
1912 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1,
1913 rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) | 0x80);
1915 /* Switch the control path to firmware. */
1916 reg = rsu_read_2(sc, R92S_SYS_CLKR);
1917 reg = (reg & ~R92S_SWHW_SEL) | R92S_FWHW_SEL;
1918 rsu_write_2(sc, R92S_SYS_CLKR, reg);
1920 rsu_write_2(sc, R92S_CR, 0x37fc);
1922 /* Fix USB RX FIFO issue. */
1923 rsu_write_1(sc, 0xfe5c,
1924 rsu_read_1(sc, 0xfe5c) | 0x80);
1925 rsu_write_1(sc, 0x00ab,
1926 rsu_read_1(sc, 0x00ab) | 0xc0);
1928 rsu_write_1(sc, R92S_SYS_CLKR,
1929 rsu_read_1(sc, R92S_SYS_CLKR) & ~R92S_SYS_CPU_CLKSEL);
1933 * Power on sequence for B-cut and C-cut adapters.
1936 rsu_power_on_bcut(struct rsu_softc *sc)
1941 /* Prevent eFuse leakage. */
1942 rsu_write_1(sc, 0x37, 0xb0);
1943 usb_pause_mtx(&sc->sc_mtx, hz / 100);
1944 rsu_write_1(sc, 0x37, 0x30);
1946 /* Switch the control path to hardware. */
1947 reg = rsu_read_2(sc, R92S_SYS_CLKR);
1948 if (reg & R92S_FWHW_SEL) {
1949 rsu_write_2(sc, R92S_SYS_CLKR,
1950 reg & ~(R92S_SWHW_SEL | R92S_FWHW_SEL));
1952 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1,
1953 rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) & ~0x8c);
1956 rsu_write_1(sc, R92S_SPS0_CTRL + 1, 0x53);
1957 rsu_write_1(sc, R92S_SPS0_CTRL + 0, 0x57);
1959 reg = rsu_read_1(sc, R92S_AFE_MISC);
1960 rsu_write_1(sc, R92S_AFE_MISC, reg | R92S_AFE_MISC_BGEN);
1961 rsu_write_1(sc, R92S_AFE_MISC, reg | R92S_AFE_MISC_BGEN |
1962 R92S_AFE_MISC_MBEN | R92S_AFE_MISC_I32_EN);
1965 rsu_write_1(sc, R92S_LDOA15_CTRL,
1966 rsu_read_1(sc, R92S_LDOA15_CTRL) | R92S_LDA15_EN);
1968 rsu_write_1(sc, R92S_LDOV12D_CTRL,
1969 rsu_read_1(sc, R92S_LDOV12D_CTRL) | R92S_LDV12_EN);
1971 rsu_write_1(sc, R92S_SYS_ISO_CTRL + 1,
1972 rsu_read_1(sc, R92S_SYS_ISO_CTRL + 1) | 0x08);
1974 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1,
1975 rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) | 0x20);
1977 /* Support 64KB IMEM. */
1978 rsu_write_1(sc, R92S_SYS_ISO_CTRL + 1,
1979 rsu_read_1(sc, R92S_SYS_ISO_CTRL + 1) & ~0x97);
1981 /* Enable AFE clock. */
1982 rsu_write_1(sc, R92S_AFE_XTAL_CTRL + 1,
1983 rsu_read_1(sc, R92S_AFE_XTAL_CTRL + 1) & ~0x04);
1984 /* Enable AFE PLL macro block. */
1985 reg = rsu_read_1(sc, R92S_AFE_PLL_CTRL);
1986 rsu_write_1(sc, R92S_AFE_PLL_CTRL, reg | 0x11);
1988 rsu_write_1(sc, R92S_AFE_PLL_CTRL, reg | 0x51);
1990 rsu_write_1(sc, R92S_AFE_PLL_CTRL, reg | 0x11);
1993 /* Attach AFE PLL to MACTOP/BB. */
1994 rsu_write_1(sc, R92S_SYS_ISO_CTRL,
1995 rsu_read_1(sc, R92S_SYS_ISO_CTRL) & ~0x11);
1997 /* Switch to 40MHz clock. */
1998 rsu_write_1(sc, R92S_SYS_CLKR, 0x00);
1999 /* Disable CPU clock and 80MHz SSC. */
2000 rsu_write_1(sc, R92S_SYS_CLKR,
2001 rsu_read_1(sc, R92S_SYS_CLKR) | 0xa0);
2002 /* Enable MAC clock. */
2003 rsu_write_2(sc, R92S_SYS_CLKR,
2004 rsu_read_2(sc, R92S_SYS_CLKR) |
2005 R92S_MAC_CLK_EN | R92S_SYS_CLK_EN);
2007 rsu_write_1(sc, R92S_PMC_FSM, 0x02);
2009 /* Enable digital core and IOREG R/W. */
2010 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1,
2011 rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) | 0x08);
2013 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1,
2014 rsu_read_1(sc, R92S_SYS_FUNC_EN + 1) | 0x80);
2016 /* Switch the control path to firmware. */
2017 reg = rsu_read_2(sc, R92S_SYS_CLKR);
2018 reg = (reg & ~R92S_SWHW_SEL) | R92S_FWHW_SEL;
2019 rsu_write_2(sc, R92S_SYS_CLKR, reg);
2021 rsu_write_2(sc, R92S_CR, 0x37fc);
2023 /* Fix USB RX FIFO issue. */
2024 rsu_write_1(sc, 0xfe5c,
2025 rsu_read_1(sc, 0xfe5c) | 0x80);
2027 rsu_write_1(sc, R92S_SYS_CLKR,
2028 rsu_read_1(sc, R92S_SYS_CLKR) & ~R92S_SYS_CPU_CLKSEL);
2030 rsu_write_1(sc, 0xfe1c, 0x80);
2032 /* Make sure TxDMA is ready to download firmware. */
2033 for (ntries = 0; ntries < 20; ntries++) {
2034 reg = rsu_read_1(sc, R92S_TCR);
2035 if ((reg & (R92S_TCR_IMEM_CHK_RPT | R92S_TCR_EMEM_CHK_RPT)) ==
2036 (R92S_TCR_IMEM_CHK_RPT | R92S_TCR_EMEM_CHK_RPT))
2041 DPRINTF("TxDMA is not ready\n");
2043 reg = rsu_read_1(sc, R92S_CR);
2044 rsu_write_1(sc, R92S_CR, reg & ~R92S_CR_TXDMA_EN);
2046 rsu_write_1(sc, R92S_CR, reg | R92S_CR_TXDMA_EN);
2051 rsu_power_off(struct rsu_softc *sc)
2054 rsu_write_1(sc, R92S_RF_CTRL, 0x00);
2055 usb_pause_mtx(&sc->sc_mtx, hz / 200);
2058 /* Switch control path. */
2059 rsu_write_1(sc, R92S_SYS_CLKR + 1, 0x38);
2061 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1, 0x70);
2062 rsu_write_1(sc, R92S_PMC_FSM, 0x06);
2063 rsu_write_1(sc, R92S_SYS_ISO_CTRL + 0, 0xf9);
2064 rsu_write_1(sc, R92S_SYS_ISO_CTRL + 1, 0xe8);
2066 /* Disable AFE PLL. */
2067 rsu_write_1(sc, R92S_AFE_PLL_CTRL, 0x00);
2069 rsu_write_1(sc, R92S_LDOA15_CTRL, 0x54);
2070 /* Disable eFuse 1.2V. */
2071 rsu_write_1(sc, R92S_SYS_FUNC_EN + 1, 0x50);
2072 rsu_write_1(sc, R92S_LDOV12D_CTRL, 0x24);
2073 /* Enable AFE macro block's bandgap and Mbias. */
2074 rsu_write_1(sc, R92S_AFE_MISC, 0x30);
2075 /* Disable 1.6V LDO. */
2076 rsu_write_1(sc, R92S_SPS0_CTRL + 0, 0x56);
2077 rsu_write_1(sc, R92S_SPS0_CTRL + 1, 0x43);
2081 rsu_fw_loadsection(struct rsu_softc *sc, const uint8_t *buf, int len)
2083 const uint8_t which = rsu_wme_ac_xfer_map[WME_AC_VO];
2084 struct rsu_data *data;
2085 struct r92s_tx_desc *txd;
2089 data = rsu_getbuf(sc);
2092 txd = (struct r92s_tx_desc *)data->buf;
2093 memset(txd, 0, sizeof(*txd));
2094 if (len <= RSU_TXBUFSZ - sizeof(*txd)) {
2096 txd->txdw0 |= htole32(R92S_TXDW0_LINIP);
2099 mlen = RSU_TXBUFSZ - sizeof(*txd);
2100 txd->txdw0 |= htole32(SM(R92S_TXDW0_PKTLEN, mlen));
2101 memcpy(&txd[1], buf, mlen);
2102 data->buflen = sizeof(*txd) + mlen;
2103 DPRINTF("starting transfer %p\n", data);
2104 STAILQ_INSERT_TAIL(&sc->sc_tx_pending[which], data, next);
2108 usbd_transfer_start(sc->sc_xfer[which]);
2113 rsu_load_firmware(struct rsu_softc *sc)
2115 const struct r92s_fw_hdr *hdr;
2116 struct r92s_fw_priv *dmem;
2117 const uint8_t *imem, *emem;
2119 const struct firmware *fw;
2124 if (rsu_read_1(sc, R92S_TCR) & R92S_TCR_FWRDY) {
2125 DPRINTF("Firmware already loaded\n");
2130 /* Read firmware image from the filesystem. */
2131 if ((fw = firmware_get("rsu-rtl8712fw")) == NULL) {
2132 device_printf(sc->sc_dev,
2133 "%s: failed load firmware of file rsu-rtl8712fw\n",
2139 size = fw->datasize;
2140 if (size < sizeof(*hdr)) {
2141 device_printf(sc->sc_dev, "firmware too short\n");
2145 hdr = (const struct r92s_fw_hdr *)fw->data;
2146 if (hdr->signature != htole16(0x8712) &&
2147 hdr->signature != htole16(0x8192)) {
2148 device_printf(sc->sc_dev,
2149 "invalid firmware signature 0x%x\n",
2150 le16toh(hdr->signature));
2154 DPRINTF("FW V%d %02x-%02x %02x:%02x\n", le16toh(hdr->version),
2155 hdr->month, hdr->day, hdr->hour, hdr->minute);
2157 /* Make sure that driver and firmware are in sync. */
2158 if (hdr->privsz != htole32(sizeof(*dmem))) {
2159 device_printf(sc->sc_dev, "unsupported firmware image\n");
2163 /* Get FW sections sizes. */
2164 imemsz = le32toh(hdr->imemsz);
2165 ememsz = le32toh(hdr->sramsz);
2166 /* Check that all FW sections fit in image. */
2167 if (size < sizeof(*hdr) + imemsz + ememsz) {
2168 device_printf(sc->sc_dev, "firmware too short\n");
2172 imem = (const uint8_t *)&hdr[1];
2173 emem = imem + imemsz;
2175 /* Load IMEM section. */
2176 error = rsu_fw_loadsection(sc, imem, imemsz);
2178 device_printf(sc->sc_dev,
2179 "could not load firmware section %s\n", "IMEM");
2182 /* Wait for load to complete. */
2183 for (ntries = 0; ntries != 50; ntries++) {
2184 usb_pause_mtx(&sc->sc_mtx, hz / 100);
2185 reg = rsu_read_1(sc, R92S_TCR);
2186 if (reg & R92S_TCR_IMEM_CODE_DONE)
2190 device_printf(sc->sc_dev, "timeout waiting for IMEM transfer\n");
2194 /* Load EMEM section. */
2195 error = rsu_fw_loadsection(sc, emem, ememsz);
2197 device_printf(sc->sc_dev,
2198 "could not load firmware section %s\n", "EMEM");
2201 /* Wait for load to complete. */
2202 for (ntries = 0; ntries != 50; ntries++) {
2203 usb_pause_mtx(&sc->sc_mtx, hz / 100);
2204 reg = rsu_read_2(sc, R92S_TCR);
2205 if (reg & R92S_TCR_EMEM_CODE_DONE)
2209 device_printf(sc->sc_dev, "timeout waiting for EMEM transfer\n");
2214 rsu_write_1(sc, R92S_SYS_CLKR,
2215 rsu_read_1(sc, R92S_SYS_CLKR) | R92S_SYS_CPU_CLKSEL);
2216 if (!(rsu_read_1(sc, R92S_SYS_CLKR) & R92S_SYS_CPU_CLKSEL)) {
2217 device_printf(sc->sc_dev, "could not enable system clock\n");
2221 rsu_write_2(sc, R92S_SYS_FUNC_EN,
2222 rsu_read_2(sc, R92S_SYS_FUNC_EN) | R92S_FEN_CPUEN);
2223 if (!(rsu_read_2(sc, R92S_SYS_FUNC_EN) & R92S_FEN_CPUEN)) {
2224 device_printf(sc->sc_dev,
2225 "could not enable microcontroller\n");
2229 /* Wait for CPU to initialize. */
2230 for (ntries = 0; ntries < 100; ntries++) {
2231 if (rsu_read_1(sc, R92S_TCR) & R92S_TCR_IMEM_RDY)
2235 if (ntries == 100) {
2236 device_printf(sc->sc_dev,
2237 "timeout waiting for microcontroller\n");
2242 /* Update DMEM section before loading. */
2243 dmem = __DECONST(struct r92s_fw_priv *, &hdr->priv);
2244 memset(dmem, 0, sizeof(*dmem));
2245 dmem->hci_sel = R92S_HCI_SEL_USB | R92S_HCI_SEL_8172;
2246 dmem->nendpoints = 0;
2247 dmem->rf_config = 0x12; /* 1T2R */
2248 dmem->vcs_type = R92S_VCS_TYPE_AUTO;
2249 dmem->vcs_mode = R92S_VCS_MODE_RTS_CTS;
2251 dmem->bw40_en = (ic->ic_htcaps & IEEE80211_HTCAP_CBW20_40) != 0;
2253 dmem->turbo_mode = 1;
2254 /* Load DMEM section. */
2255 error = rsu_fw_loadsection(sc, (uint8_t *)dmem, sizeof(*dmem));
2257 device_printf(sc->sc_dev,
2258 "could not load firmware section %s\n", "DMEM");
2261 /* Wait for load to complete. */
2262 for (ntries = 0; ntries < 100; ntries++) {
2263 if (rsu_read_1(sc, R92S_TCR) & R92S_TCR_DMEM_CODE_DONE)
2267 if (ntries == 100) {
2268 device_printf(sc->sc_dev, "timeout waiting for %s transfer\n",
2273 /* Wait for firmware readiness. */
2274 for (ntries = 0; ntries < 60; ntries++) {
2275 if (!(rsu_read_1(sc, R92S_TCR) & R92S_TCR_FWRDY))
2280 device_printf(sc->sc_dev,
2281 "timeout waiting for firmware readiness\n");
2286 firmware_put(fw, FIRMWARE_UNLOAD);
2292 rsu_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2293 const struct ieee80211_bpf_params *params)
2295 struct ieee80211com *ic = ni->ni_ic;
2296 struct ifnet *ifp = ic->ic_ifp;
2297 struct rsu_softc *sc = ifp->if_softc;
2298 struct rsu_data *bf;
2300 /* prevent management frames from being sent if we're not ready */
2301 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2303 ieee80211_free_node(ni);
2307 bf = rsu_getbuf(sc);
2309 ieee80211_free_node(ni);
2314 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
2315 if (rsu_tx_start(sc, ni, m, bf) != 0) {
2316 ieee80211_free_node(ni);
2317 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
2318 STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, bf, next);
2330 struct rsu_softc *sc = arg;
2333 rsu_init_locked(arg);
2338 rsu_init_locked(struct rsu_softc *sc)
2340 struct ifnet *ifp = sc->sc_ifp;
2341 struct r92s_set_pwr_mode cmd;
2345 /* Init host async commands ring. */
2346 sc->cmdq.cur = sc->cmdq.next = sc->cmdq.queued = 0;
2348 /* Power on adapter. */
2350 rsu_power_on_acut(sc);
2352 rsu_power_on_bcut(sc);
2354 /* Load firmware. */
2355 error = rsu_load_firmware(sc);
2359 /* Enable Rx TCP checksum offload. */
2360 rsu_write_4(sc, R92S_RCR,
2361 rsu_read_4(sc, R92S_RCR) | 0x04000000);
2362 /* Append PHY status. */
2363 rsu_write_4(sc, R92S_RCR,
2364 rsu_read_4(sc, R92S_RCR) | 0x02000000);
2366 rsu_write_4(sc, R92S_CR,
2367 rsu_read_4(sc, R92S_CR) & ~0xff000000);
2369 /* Use 128 bytes pages. */
2370 rsu_write_1(sc, 0x00b5,
2371 rsu_read_1(sc, 0x00b5) | 0x01);
2372 /* Enable USB Rx aggregation. */
2373 rsu_write_1(sc, 0x00bd,
2374 rsu_read_1(sc, 0x00bd) | 0x80);
2375 /* Set USB Rx aggregation threshold. */
2376 rsu_write_1(sc, 0x00d9, 0x01);
2377 /* Set USB Rx aggregation timeout (1.7ms/4). */
2378 rsu_write_1(sc, 0xfe5b, 0x04);
2379 /* Fix USB Rx FIFO issue. */
2380 rsu_write_1(sc, 0xfe5c,
2381 rsu_read_1(sc, 0xfe5c) | 0x80);
2383 /* Set MAC address. */
2384 rsu_write_region_1(sc, R92S_MACID, IF_LLADDR(ifp),
2385 IEEE80211_ADDR_LEN);
2387 /* It really takes 1.5 seconds for the firmware to boot: */
2388 usb_pause_mtx(&sc->sc_mtx, (3 * hz) / 2);
2390 DPRINTF("setting MAC address to %s\n", ether_sprintf(IF_LLADDR(ifp)));
2391 error = rsu_fw_cmd(sc, R92S_CMD_SET_MAC_ADDRESS, IF_LLADDR(ifp),
2392 IEEE80211_ADDR_LEN);
2394 device_printf(sc->sc_dev, "could not set MAC address\n");
2398 rsu_write_1(sc, R92S_USB_HRPWM,
2399 R92S_USB_HRPWM_PS_ST_ACTIVE | R92S_USB_HRPWM_PS_ALL_ON);
2401 memset(&cmd, 0, sizeof(cmd));
2402 cmd.mode = R92S_PS_MODE_ACTIVE;
2403 DPRINTF("setting ps mode to %d\n", cmd.mode);
2404 error = rsu_fw_cmd(sc, R92S_CMD_SET_PWR_MODE, &cmd, sizeof(cmd));
2406 device_printf(sc->sc_dev, "could not set PS mode\n");
2411 if (ic->ic_htcaps & IEEE80211_HTCAP_CBW20_40) {
2412 /* Enable 40MHz mode. */
2413 error = rsu_fw_iocmd(sc,
2414 SM(R92S_IOCMD_CLASS, 0xf4) |
2415 SM(R92S_IOCMD_INDEX, 0x00) |
2416 SM(R92S_IOCMD_VALUE, 0x0007));
2418 device_printf(sc->sc_dev,
2419 "could not enable 40MHz mode\n");
2424 /* Set default channel. */
2425 ic->ic_bss->ni_chan = ic->ic_ibss_chan;
2428 usbd_transfer_start(sc->sc_xfer[RSU_BULK_RX]);
2430 /* We're ready to go. */
2431 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2432 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2435 /* Need to stop all failed transfers, if any */
2436 for (i = 0; i != RSU_N_TRANSFER; i++)
2437 usbd_transfer_stop(sc->sc_xfer[i]);
2441 rsu_stop(struct ifnet *ifp, int disable)
2443 struct rsu_softc *sc = ifp->if_softc;
2446 rsu_stop_locked(ifp, disable);
2451 rsu_stop_locked(struct ifnet *ifp, int disable __unused)
2453 struct rsu_softc *sc = ifp->if_softc;
2456 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2457 sc->sc_calibrating = 0;
2458 taskqueue_cancel_timeout(taskqueue_thread, &sc->calib_task, NULL);
2460 /* Power off adapter. */
2463 for (i = 0; i < RSU_N_TRANSFER; i++)
2464 usbd_transfer_stop(sc->sc_xfer[i]);
2468 rsu_ms_delay(struct rsu_softc *sc)
2470 usb_pause_mtx(&sc->sc_mtx, hz / 1000);