1 /* $OpenBSD: if_rum.c,v 1.40 2006/09/18 16:20:20 damien Exp $ */
2 /* $DragonFly: src/sys/dev/netif/rum/if_rum.c,v 1.5 2007/02/06 14:33:39 sephe Exp $ */
5 * Copyright (c) 2005, 2006 Damien Bergamini <damien.bergamini@free.fr>
6 * Copyright (c) 2006 Niall O'Higgins <niallo@openbsd.org>
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 * Ralink Technology RT2501USB/RT2601USB chipset driver
23 * http://www.ralinktech.com/
26 #include <sys/param.h>
28 #include <sys/endian.h>
29 #include <sys/kernel.h>
30 #include <sys/malloc.h>
33 #include <sys/serialize.h>
34 #include <sys/socket.h>
35 #include <sys/sockio.h>
38 #include <net/ethernet.h>
40 #include <net/if_arp.h>
41 #include <net/if_dl.h>
42 #include <net/if_media.h>
43 #include <net/ifq_var.h>
45 #include <netproto/802_11/ieee80211_var.h>
46 #include <netproto/802_11/ieee80211_radiotap.h>
47 #include <netproto/802_11/wlan_ratectl/onoe/ieee80211_onoe_param.h>
49 #include <bus/usb/usb.h>
50 #include <bus/usb/usbdi.h>
51 #include <bus/usb/usbdi_util.h>
52 #include <bus/usb/usbdevs.h>
54 #include "if_rumreg.h"
55 #include "if_rumvar.h"
56 #include "rum_ucode.h"
63 #define DPRINTF(x) do { if (rum_debug) logprintf x; } while (0)
64 #define DPRINTFN(n, x) do { if (rum_debug >= (n)) logprintf x; } while (0)
68 #define DPRINTFN(n, x)
71 /* various supported device vendors/products */
72 static const struct usb_devno rum_devs[] = {
73 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_RT2573 },
74 { USB_VENDOR_ASUS, USB_PRODUCT_ASUS_WL167G_2 },
75 { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D7050A },
76 { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D9050V3 },
77 { USB_VENDOR_LINKSYS4, USB_PRODUCT_LINKSYS4_WUSB54GC },
78 { USB_VENDOR_CONCEPTRONIC, USB_PRODUCT_CONCEPTRONIC_C54RU2 },
79 { USB_VENDOR_DICKSMITH, USB_PRODUCT_DICKSMITH_CWD854F },
80 { USB_VENDOR_DICKSMITH, USB_PRODUCT_DICKSMITH_RT2573 },
81 { USB_VENDOR_DLINK2, USB_PRODUCT_DLINK2_DWLG122C1 },
82 { USB_VENDOR_DLINK2, USB_PRODUCT_DLINK2_WUA1340 },
83 { USB_VENDOR_GIGABYTE, USB_PRODUCT_GIGABYTE_GNWB01GS },
84 { USB_VENDOR_GIGASET, USB_PRODUCT_GIGASET_RT2573 },
85 { USB_VENDOR_GOODWAY, USB_PRODUCT_GOODWAY_RT2573 },
86 { USB_VENDOR_HUAWEI3COM, USB_PRODUCT_HUAWEI3COM_RT2573 },
87 { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2573 },
88 { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2573_2 },
89 { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2573_3 },
90 { USB_VENDOR_PLANEX2, USB_PRODUCT_PLANEX2_GWUSMM },
91 { USB_VENDOR_QCOM, USB_PRODUCT_QCOM_RT2573 },
92 { USB_VENDOR_QCOM, USB_PRODUCT_QCOM_RT2573_2 },
93 { USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2573 },
94 { USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2671 },
95 { USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_WL113R2 },
96 { USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_WL172 },
97 { USB_VENDOR_SURECOM, USB_PRODUCT_SURECOM_RT2573 }
101 Static void rum_attachhook(void *);
103 Static int rum_alloc_tx_list(struct rum_softc *);
104 Static void rum_free_tx_list(struct rum_softc *);
105 Static int rum_alloc_rx_list(struct rum_softc *);
106 Static void rum_free_rx_list(struct rum_softc *);
107 Static int rum_media_change(struct ifnet *);
108 Static void rum_next_scan(void *);
109 Static void rum_task(void *);
110 Static int rum_newstate(struct ieee80211com *,
111 enum ieee80211_state, int);
112 Static void rum_txeof(usbd_xfer_handle, usbd_private_handle,
114 Static void rum_rxeof(usbd_xfer_handle, usbd_private_handle,
116 Static uint8_t rum_rxrate(struct rum_rx_desc *);
117 Static int rum_ack_rate(struct ieee80211com *, int);
118 Static uint16_t rum_txtime(int, int, uint32_t);
119 Static uint8_t rum_plcp_signal(int);
120 Static void rum_setup_tx_desc(struct rum_softc *,
121 struct rum_tx_desc *, uint32_t, uint16_t, int,
123 Static int rum_tx_data(struct rum_softc *, struct mbuf *,
124 struct ieee80211_node *);
125 Static void rum_start(struct ifnet *);
126 Static void rum_watchdog(struct ifnet *);
127 Static int rum_ioctl(struct ifnet *, u_long, caddr_t,
129 Static void rum_eeprom_read(struct rum_softc *, uint16_t, void *,
131 Static uint32_t rum_read(struct rum_softc *, uint16_t);
132 Static void rum_read_multi(struct rum_softc *, uint16_t, void *,
134 Static void rum_write(struct rum_softc *, uint16_t, uint32_t);
135 Static void rum_write_multi(struct rum_softc *, uint16_t, void *,
137 Static void rum_bbp_write(struct rum_softc *, uint8_t, uint8_t);
138 Static uint8_t rum_bbp_read(struct rum_softc *, uint8_t);
139 Static void rum_rf_write(struct rum_softc *, uint8_t, uint32_t);
140 Static void rum_select_antenna(struct rum_softc *);
141 Static void rum_enable_mrr(struct rum_softc *);
142 Static void rum_set_txpreamble(struct rum_softc *);
143 Static void rum_set_basicrates(struct rum_softc *);
144 Static void rum_select_band(struct rum_softc *,
145 struct ieee80211_channel *);
146 Static void rum_set_chan(struct rum_softc *,
147 struct ieee80211_channel *);
148 Static void rum_enable_tsf_sync(struct rum_softc *);
149 Static void rum_update_slot(struct rum_softc *);
150 Static void rum_set_bssid(struct rum_softc *, const uint8_t *);
151 Static void rum_set_macaddr(struct rum_softc *, const uint8_t *);
152 Static void rum_update_promisc(struct rum_softc *);
153 Static const char *rum_get_rf(int);
154 Static void rum_read_eeprom(struct rum_softc *);
155 Static int rum_bbp_init(struct rum_softc *);
156 Static void rum_init(void *);
157 Static void rum_stop(struct rum_softc *);
158 Static int rum_load_microcode(struct rum_softc *, const uint8_t *,
160 Static int rum_prepare_beacon(struct rum_softc *);
162 Static void rum_stats_timeout(void *);
163 Static void rum_stats_update(usbd_xfer_handle, usbd_private_handle,
165 Static void rum_stats(struct ieee80211com *,
166 struct ieee80211_node *,
167 struct ieee80211_ratectl_stats *);
168 Static void rum_ratectl_change(struct ieee80211com *ic, u_int,
170 Static int rum_get_rssi(struct rum_softc *, uint8_t);
173 * Supported rates for 802.11a/b/g modes (in 500Kbps unit).
175 static const struct ieee80211_rateset rum_rateset_11a =
176 { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
178 static const struct ieee80211_rateset rum_rateset_11b =
179 { 4, { 2, 4, 11, 22 } };
181 static const struct ieee80211_rateset rum_rateset_11g =
182 { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
184 static const struct {
191 static const struct {
198 static const struct rfprog {
200 uint32_t r1, r2, r3, r4;
207 USB_DECLARE_DRIVER(rum);
208 DRIVER_MODULE(rum, uhub, rum_driver, rum_devclass, usbd_driver_load, 0);
212 USB_MATCH_START(rum, uaa);
214 if (uaa->iface != NULL)
217 return (usb_lookup(rum_devs, uaa->vendor, uaa->product) != NULL) ?
218 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
223 USB_ATTACH_START(rum, sc, uaa);
224 struct ieee80211com *ic = &sc->sc_ic;
225 struct ifnet *ifp = &ic->ic_if;
226 usb_interface_descriptor_t *id;
227 usb_endpoint_descriptor_t *ed;
233 sc->sc_udev = uaa->device;
235 usbd_devinfo(uaa->device, 0, devinfo);
238 if (usbd_set_config_no(sc->sc_udev, RT2573_CONFIG_NO, 0) != 0) {
239 kprintf("%s: could not set configuration no\n",
240 USBDEVNAME(sc->sc_dev));
241 USB_ATTACH_ERROR_RETURN;
244 /* get the first interface handle */
245 error = usbd_device2interface_handle(sc->sc_udev, RT2573_IFACE_INDEX,
248 kprintf("%s: could not get interface handle\n",
249 USBDEVNAME(sc->sc_dev));
250 USB_ATTACH_ERROR_RETURN;
256 id = usbd_get_interface_descriptor(sc->sc_iface);
258 sc->sc_rx_no = sc->sc_tx_no = -1;
259 for (i = 0; i < id->bNumEndpoints; i++) {
260 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
262 kprintf("%s: no endpoint descriptor for iface %d\n",
263 USBDEVNAME(sc->sc_dev), i);
264 USB_ATTACH_ERROR_RETURN;
267 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
268 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
269 sc->sc_rx_no = ed->bEndpointAddress;
270 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
271 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
272 sc->sc_tx_no = ed->bEndpointAddress;
274 if (sc->sc_rx_no == -1 || sc->sc_tx_no == -1) {
275 kprintf("%s: missing endpoint\n", USBDEVNAME(sc->sc_dev));
276 USB_ATTACH_ERROR_RETURN;
279 usb_init_task(&sc->sc_task, rum_task, sc);
281 callout_init(&sc->scan_ch);
282 callout_init(&sc->stats_ch);
284 /* retrieve RT2573 rev. no */
285 for (ntries = 0; ntries < 1000; ntries++) {
286 if ((tmp = rum_read(sc, RT2573_MAC_CSR0)) != 0)
290 if (ntries == 1000) {
291 kprintf("%s: timeout waiting for chip to settle\n",
292 USBDEVNAME(sc->sc_dev));
293 USB_ATTACH_ERROR_RETURN;
296 /* retrieve MAC address and various other things from EEPROM */
299 kprintf("%s: MAC/BBP RT%04x (rev 0x%05x), RF %s, address %6D\n",
300 USBDEVNAME(sc->sc_dev), sc->macbbp_rev, tmp,
301 rum_get_rf(sc->rf_rev), ic->ic_myaddr, ":");
303 error = rum_load_microcode(sc, rt2573, sizeof(rt2573));
305 device_printf(self, "can't load microcode\n");
306 USB_ATTACH_ERROR_RETURN;
309 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
310 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
311 ic->ic_state = IEEE80211_S_INIT;
313 /* set device capabilities */
315 IEEE80211_C_IBSS | /* IBSS mode supported */
316 IEEE80211_C_MONITOR | /* monitor mode supported */
317 IEEE80211_C_HOSTAP | /* HostAp mode supported */
318 IEEE80211_C_TXPMGT | /* tx power management */
319 IEEE80211_C_SHPREAMBLE | /* short preamble supported */
320 IEEE80211_C_SHSLOT | /* short slot time supported */
321 IEEE80211_C_WPA; /* WPA 1+2 */
323 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_5226) {
324 /* set supported .11a rates */
325 ic->ic_sup_rates[IEEE80211_MODE_11A] = rum_rateset_11a;
327 /* set supported .11a channels */
328 for (i = 34; i <= 46; i += 4) {
329 ic->ic_channels[i].ic_freq =
330 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
331 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
333 for (i = 36; i <= 64; i += 4) {
334 ic->ic_channels[i].ic_freq =
335 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
336 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
338 for (i = 100; i <= 140; i += 4) {
339 ic->ic_channels[i].ic_freq =
340 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
341 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
343 for (i = 149; i <= 165; i += 4) {
344 ic->ic_channels[i].ic_freq =
345 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
346 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
350 /* set supported .11b and .11g rates */
351 ic->ic_sup_rates[IEEE80211_MODE_11B] = rum_rateset_11b;
352 ic->ic_sup_rates[IEEE80211_MODE_11G] = rum_rateset_11g;
354 /* set supported .11b and .11g channels (1 through 14) */
355 for (i = 1; i <= 14; i++) {
356 ic->ic_channels[i].ic_freq =
357 ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
358 ic->ic_channels[i].ic_flags =
359 IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
360 IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
363 if_initname(ifp, device_get_name(self), device_get_unit(self));
365 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
366 ifp->if_init = rum_init;
367 ifp->if_ioctl = rum_ioctl;
368 ifp->if_start = rum_start;
369 ifp->if_watchdog = rum_watchdog;
370 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
371 ifq_set_ready(&ifp->if_snd);
373 ic->ic_ratectl.rc_st_ratectl_cap = IEEE80211_RATECTL_CAP_ONOE;
374 ic->ic_ratectl.rc_st_ratectl = IEEE80211_RATECTL_ONOE;
375 ic->ic_ratectl.rc_st_valid_stats =
376 IEEE80211_RATECTL_STATS_PKT_NORETRY |
377 IEEE80211_RATECTL_STATS_PKT_OK |
378 IEEE80211_RATECTL_STATS_PKT_ERR |
379 IEEE80211_RATECTL_STATS_RETRIES;
380 ic->ic_ratectl.rc_st_stats = rum_stats;
381 ic->ic_ratectl.rc_st_change = rum_ratectl_change;
383 ieee80211_ifattach(ic);
385 /* Enable software beacon missing handling. */
386 ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS;
388 /* override state transition machine */
389 sc->sc_newstate = ic->ic_newstate;
390 ic->ic_newstate = rum_newstate;
391 ieee80211_media_init(ic, rum_media_change, ieee80211_media_status);
393 bpfattach_dlt(ifp, DLT_IEEE802_11_RADIO,
394 sizeof(struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN,
397 sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
398 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
399 sc->sc_rxtap.wr_ihdr.it_present = htole32(RT2573_RX_RADIOTAP_PRESENT);
401 sc->sc_txtap_len = sizeof sc->sc_txtapu;
402 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
403 sc->sc_txtap.wt_ihdr.it_present = htole32(RT2573_TX_RADIOTAP_PRESENT);
406 ieee80211_announce(ic);
408 USB_ATTACH_SUCCESS_RETURN;
413 USB_DETACH_START(rum, sc);
414 struct ifnet *ifp = &sc->sc_ic.ic_if;
420 lwkt_serialize_enter(ifp->if_serializer);
422 callout_stop(&sc->scan_ch);
423 callout_stop(&sc->stats_ch);
425 sc->sc_flags |= RUM_FLAG_SYNCTASK;
428 lwkt_serialize_exit(ifp->if_serializer);
430 usb_rem_task(sc->sc_udev, &sc->sc_task);
433 ieee80211_ifdetach(&sc->sc_ic); /* free all nodes */
435 if (sc->stats_xfer != NULL) {
436 usbd_free_xfer(sc->stats_xfer);
437 sc->stats_xfer = NULL;
440 if (sc->sc_rx_pipeh != NULL) {
441 usbd_abort_pipe(sc->sc_rx_pipeh);
442 usbd_close_pipe(sc->sc_rx_pipeh);
445 if (sc->sc_tx_pipeh != NULL) {
446 usbd_abort_pipe(sc->sc_tx_pipeh);
447 usbd_close_pipe(sc->sc_tx_pipeh);
450 rum_free_rx_list(sc);
451 rum_free_tx_list(sc);
460 rum_alloc_tx_list(struct rum_softc *sc)
462 struct rum_tx_data *data;
467 for (i = 0; i < RT2573_TX_LIST_COUNT; i++) {
468 data = &sc->tx_data[i];
472 data->xfer = usbd_alloc_xfer(sc->sc_udev);
473 if (data->xfer == NULL) {
474 kprintf("%s: could not allocate tx xfer\n",
475 USBDEVNAME(sc->sc_dev));
480 data->buf = usbd_alloc_buffer(data->xfer,
481 RT2573_TX_DESC_SIZE + IEEE80211_MAX_LEN);
482 if (data->buf == NULL) {
483 kprintf("%s: could not allocate tx buffer\n",
484 USBDEVNAME(sc->sc_dev));
489 /* clean Tx descriptor */
490 bzero(data->buf, RT2573_TX_DESC_SIZE);
495 fail: rum_free_tx_list(sc);
500 rum_free_tx_list(struct rum_softc *sc)
502 struct rum_tx_data *data;
505 for (i = 0; i < RT2573_TX_LIST_COUNT; i++) {
506 data = &sc->tx_data[i];
508 if (data->xfer != NULL) {
509 usbd_free_xfer(data->xfer);
513 if (data->ni != NULL) {
514 ieee80211_free_node(data->ni);
521 rum_alloc_rx_list(struct rum_softc *sc)
523 struct rum_rx_data *data;
526 for (i = 0; i < RT2573_RX_LIST_COUNT; i++) {
527 data = &sc->rx_data[i];
531 data->xfer = usbd_alloc_xfer(sc->sc_udev);
532 if (data->xfer == NULL) {
533 kprintf("%s: could not allocate rx xfer\n",
534 USBDEVNAME(sc->sc_dev));
539 if (usbd_alloc_buffer(data->xfer, MCLBYTES) == NULL) {
540 kprintf("%s: could not allocate rx buffer\n",
541 USBDEVNAME(sc->sc_dev));
546 data->m = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
547 if (data->m == NULL) {
548 kprintf("%s: could not allocate rx mbuf\n",
549 USBDEVNAME(sc->sc_dev));
554 data->buf = mtod(data->m, uint8_t *);
555 bzero(data->buf, sizeof(struct rum_rx_desc));
560 fail: rum_free_tx_list(sc);
565 rum_free_rx_list(struct rum_softc *sc)
567 struct rum_rx_data *data;
570 for (i = 0; i < RT2573_RX_LIST_COUNT; i++) {
571 data = &sc->rx_data[i];
573 if (data->xfer != NULL) {
574 usbd_free_xfer(data->xfer);
578 if (data->m != NULL) {
586 rum_media_change(struct ifnet *ifp)
590 error = ieee80211_media_change(ifp);
591 if (error != ENETRESET)
594 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
595 rum_init(ifp->if_softc);
601 * This function is called periodically (every 200ms) during scanning to
602 * switch from one channel to another.
605 rum_next_scan(void *arg)
607 struct rum_softc *sc = arg;
608 struct ieee80211com *ic = &sc->sc_ic;
609 struct ifnet *ifp = &ic->ic_if;
611 lwkt_serialize_enter(ifp->if_serializer);
613 if (ic->ic_state == IEEE80211_S_SCAN)
614 ieee80211_next_scan(ic);
616 lwkt_serialize_exit(ifp->if_serializer);
622 struct rum_softc *sc = arg;
623 struct ieee80211com *ic = &sc->sc_ic;
624 struct ifnet *ifp = &ic->ic_if;
625 enum ieee80211_state ostate;
626 struct ieee80211_node *ni;
629 lwkt_serialize_enter(ifp->if_serializer);
631 ieee80211_ratectl_newstate(ic, sc->sc_state);
633 ostate = ic->ic_state;
635 switch (sc->sc_state) {
636 case IEEE80211_S_INIT:
637 if (ostate == IEEE80211_S_RUN) {
638 /* abort TSF synchronization */
639 tmp = rum_read(sc, RT2573_TXRX_CSR9);
640 rum_write(sc, RT2573_TXRX_CSR9, tmp & ~0x00ffffff);
644 case IEEE80211_S_SCAN:
645 rum_set_chan(sc, ic->ic_curchan);
646 callout_reset(&sc->scan_ch, hz / 5, rum_next_scan, sc);
649 case IEEE80211_S_AUTH:
650 rum_set_chan(sc, ic->ic_curchan);
653 case IEEE80211_S_ASSOC:
654 rum_set_chan(sc, ic->ic_curchan);
657 case IEEE80211_S_RUN:
658 rum_set_chan(sc, ic->ic_curchan);
662 lwkt_serialize_exit(ifp->if_serializer);
664 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
667 rum_set_txpreamble(sc);
668 rum_set_basicrates(sc);
669 rum_set_bssid(sc, ni->ni_bssid);
672 if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
673 ic->ic_opmode == IEEE80211_M_IBSS)
674 rum_prepare_beacon(sc);
676 if (ic->ic_opmode != IEEE80211_M_MONITOR)
677 rum_enable_tsf_sync(sc);
679 /* clear statistic registers (STA_CSR0 to STA_CSR5) */
680 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof(sc->sta));
682 lwkt_serialize_enter(ifp->if_serializer);
684 callout_reset(&sc->stats_ch, 4 * hz / 5, rum_stats_timeout, sc);
689 sc->sc_newstate(ic, sc->sc_state, sc->sc_arg);
691 lwkt_serialize_exit(ifp->if_serializer);
695 rum_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
697 struct rum_softc *sc = ic->ic_if.if_softc;
698 struct ifnet *ifp = &ic->ic_if;
700 ASSERT_SERIALIZED(ifp->if_serializer);
702 callout_stop(&sc->scan_ch);
703 callout_stop(&sc->stats_ch);
705 /* do it in a process context */
706 sc->sc_state = nstate;
709 lwkt_serialize_exit(ifp->if_serializer);
710 usb_rem_task(sc->sc_udev, &sc->sc_task);
712 if (sc->sc_flags & RUM_FLAG_SYNCTASK) {
713 usb_do_task(sc->sc_udev, &sc->sc_task, USB_TASKQ_DRIVER,
716 usb_add_task(sc->sc_udev, &sc->sc_task, USB_TASKQ_DRIVER);
718 lwkt_serialize_enter(ifp->if_serializer);
723 /* quickly determine if a given rate is CCK or OFDM */
724 #define RUM_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
726 #define RUM_ACK_SIZE 14 /* 10 + 4(FCS) */
727 #define RUM_CTS_SIZE 14 /* 10 + 4(FCS) */
730 rum_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
732 struct rum_tx_data *data = priv;
733 struct rum_softc *sc = data->sc;
734 struct ieee80211com *ic = &sc->sc_ic;
735 struct ifnet *ifp = &ic->ic_if;
737 if (status != USBD_NORMAL_COMPLETION) {
738 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
741 kprintf("%s: could not transmit buffer: %s\n",
742 USBDEVNAME(sc->sc_dev), usbd_errstr(status));
744 if (status == USBD_STALLED)
745 usbd_clear_endpoint_stall_async(sc->sc_tx_pipeh);
755 lwkt_serialize_enter(ifp->if_serializer);
759 ieee80211_free_node(data->ni);
762 bzero(data->buf, sizeof(struct rum_tx_data));
766 DPRINTFN(10, ("tx done\n"));
769 ifp->if_flags &= ~IFF_OACTIVE;
772 lwkt_serialize_exit(ifp->if_serializer);
780 rum_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
782 struct rum_rx_data *data = priv;
783 struct rum_softc *sc = data->sc;
784 struct ieee80211com *ic = &sc->sc_ic;
785 struct ifnet *ifp = &ic->ic_if;
786 struct rum_rx_desc *desc;
787 struct ieee80211_frame_min *wh;
788 struct ieee80211_node *ni;
789 struct mbuf *mnew, *m;
792 if (status != USBD_NORMAL_COMPLETION) {
793 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
796 if (status == USBD_STALLED)
797 usbd_clear_endpoint_stall_async(sc->sc_rx_pipeh);
801 usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
803 if (len < RT2573_RX_DESC_SIZE + sizeof (struct ieee80211_frame_min)) {
804 DPRINTF(("%s: xfer too short %d\n", USBDEVNAME(sc->sc_dev),
810 desc = (struct rum_rx_desc *)data->buf;
812 if (le32toh(desc->flags) & RT2573_RX_CRC_ERROR) {
814 * This should not happen since we did not request to receive
815 * those frames when we filled RT2573_TXRX_CSR0.
817 DPRINTFN(5, ("CRC error\n"));
822 mnew = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
824 kprintf("%s: could not allocate rx mbuf\n",
825 USBDEVNAME(sc->sc_dev));
832 data->buf = mtod(data->m, uint8_t *);
835 m->m_pkthdr.rcvif = ifp;
836 m->m_data = (caddr_t)(desc + 1);
837 m->m_pkthdr.len = m->m_len = (le32toh(desc->flags) >> 16) & 0xfff;
843 lwkt_serialize_enter(ifp->if_serializer);
845 rssi = rum_get_rssi(sc, desc->rssi);
847 if (sc->sc_drvbpf != NULL) {
848 struct rum_rx_radiotap_header *tap = &sc->sc_rxtap;
851 tap->wr_rate = rum_rxrate(desc);
852 tap->wr_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq);
853 tap->wr_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
854 tap->wr_antenna = sc->rx_ant;
855 tap->wr_antsignal = rssi;
857 bpf_ptap(sc->sc_drvbpf, m, tap, sc->sc_rxtap_len);
860 wh = mtod(m, struct ieee80211_frame_min *);
861 ni = ieee80211_find_rxnode(ic, wh);
863 /* send the frame to the 802.11 layer */
864 ieee80211_input(ic, m, ni, rssi, 0);
866 /* node is no longer needed */
867 ieee80211_free_node(ni);
870 * In HostAP mode, ieee80211_input() will enqueue packets in if_snd
871 * without calling if_start().
873 if (!ifq_is_empty(&ifp->if_snd) && !(ifp->if_flags & IFF_OACTIVE))
880 lwkt_serialize_exit(ifp->if_serializer);
882 DPRINTFN(15, ("rx done\n"));
884 skip: /* setup a new transfer */
885 bzero(data->buf, sizeof(struct rum_rx_desc));
886 usbd_setup_xfer(xfer, sc->sc_rx_pipeh, data, data->buf, MCLBYTES,
887 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rum_rxeof);
892 * This function is only used by the Rx radiotap code. It returns the rate at
893 * which a given frame was received.
896 rum_rxrate(struct rum_rx_desc *desc)
898 if (le32toh(desc->flags) & RT2573_RX_OFDM) {
899 /* reverse function of rum_plcp_signal */
900 switch (desc->rate) {
908 case 0xc: return 108;
911 if (desc->rate == 10)
913 if (desc->rate == 20)
915 if (desc->rate == 55)
917 if (desc->rate == 110)
920 return 2; /* should not get there */
924 * Return the expected ack rate for a frame transmitted at rate `rate'.
925 * XXX: this should depend on the destination node basic rate set.
928 rum_ack_rate(struct ieee80211com *ic, int rate)
937 return (ic->ic_curmode == IEEE80211_MODE_11B) ? 4 : rate;
953 /* default to 1Mbps */
958 * Compute the duration (in us) needed to transmit `len' bytes at rate `rate'.
959 * The function automatically determines the operating mode depending on the
960 * given rate. `flags' indicates whether short preamble is in use or not.
963 rum_txtime(int len, int rate, uint32_t flags)
967 if (RUM_RATE_IS_OFDM(rate)) {
968 /* IEEE Std 802.11a-1999, pp. 37 */
969 txtime = (8 + 4 * len + 3 + rate - 1) / rate;
970 txtime = 16 + 4 + 4 * txtime + 6;
972 /* IEEE Std 802.11b-1999, pp. 28 */
973 txtime = (16 * len + rate - 1) / rate;
974 if (rate != 2 && (flags & IEEE80211_F_SHPREAMBLE))
983 rum_plcp_signal(int rate)
986 /* CCK rates (returned values are device-dependent) */
992 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1000 case 108: return 0xc;
1002 /* unsupported rates (should not get there) */
1003 default: return 0xff;
1008 rum_setup_tx_desc(struct rum_softc *sc, struct rum_tx_desc *desc,
1009 uint32_t flags, uint16_t xflags, int len, int rate)
1011 struct ieee80211com *ic = &sc->sc_ic;
1012 uint16_t plcp_length;
1015 desc->flags = htole32(flags);
1016 desc->flags |= htole32(len << 16);
1018 desc->xflags = htole16(xflags);
1020 desc->wme = htole16(
1023 RT2573_LOGCWMIN(4) |
1024 RT2573_LOGCWMAX(10));
1026 /* setup PLCP fields */
1027 desc->plcp_signal = rum_plcp_signal(rate);
1028 desc->plcp_service = 4;
1030 len += IEEE80211_CRC_LEN;
1031 if (RUM_RATE_IS_OFDM(rate)) {
1032 desc->flags |= htole32(RT2573_TX_OFDM);
1034 plcp_length = len & 0xfff;
1035 desc->plcp_length_hi = plcp_length >> 6;
1036 desc->plcp_length_lo = plcp_length & 0x3f;
1038 plcp_length = (16 * len + rate - 1) / rate;
1040 remainder = (16 * len) % 22;
1041 if (remainder != 0 && remainder < 7)
1042 desc->plcp_service |= RT2573_PLCP_LENGEXT;
1044 desc->plcp_length_hi = plcp_length >> 8;
1045 desc->plcp_length_lo = plcp_length & 0xff;
1047 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1048 desc->plcp_signal |= 0x08;
1050 desc->flags |= htole32(RT2573_TX_VALID);
1053 #define RUM_TX_TIMEOUT 5000
1056 rum_tx_data(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1058 struct ieee80211com *ic = &sc->sc_ic;
1059 struct rum_tx_desc *desc;
1060 struct rum_tx_data *data;
1061 struct ieee80211_frame *wh;
1067 wh = mtod(m0, struct ieee80211_frame *);
1069 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1070 if (ieee80211_crypto_encap(ic, ni, m0) == NULL) {
1075 /* packet header may have moved, reset our local pointer */
1076 wh = mtod(m0, struct ieee80211_frame *);
1080 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
1081 IEEE80211_FC0_TYPE_MGT) {
1082 /* mgmt frames are sent at the lowest available bit-rate */
1083 rate = ni->ni_rates.rs_rates[0];
1085 if (ic->ic_fixed_rate != -1) {
1086 rate = ic->ic_sup_rates[ic->ic_curmode].
1087 rs_rates[ic->ic_fixed_rate];
1089 rate = ni->ni_rates.rs_rates[ni->ni_txrate];
1091 rate &= IEEE80211_RATE_VAL;
1093 rate = 2; /* fallback to 1Mbps; should not happen */
1095 data = &sc->tx_data[0];
1096 desc = (struct rum_tx_desc *)data->buf;
1101 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1102 flags |= RT2573_TX_ACK;
1104 dur = rum_txtime(RUM_ACK_SIZE, rum_ack_rate(ic, rate),
1105 ic->ic_flags) + sc->sifs;
1106 *(uint16_t *)wh->i_dur = htole16(dur);
1108 /* tell hardware to set timestamp in probe responses */
1110 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
1111 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
1112 flags |= RT2573_TX_TIMESTAMP;
1115 if (sc->sc_drvbpf != NULL) {
1116 struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
1119 tap->wt_rate = rate;
1120 tap->wt_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq);
1121 tap->wt_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
1122 tap->wt_antenna = sc->tx_ant;
1124 bpf_ptap(sc->sc_drvbpf, m0, tap, sc->sc_txtap_len);
1127 m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RT2573_TX_DESC_SIZE);
1128 rum_setup_tx_desc(sc, desc, flags, 0, m0->m_pkthdr.len, rate);
1130 /* align end on a 4-bytes boundary */
1131 xferlen = (RT2573_TX_DESC_SIZE + m0->m_pkthdr.len + 3) & ~3;
1134 * No space left in the last URB to store the extra 4 bytes, force
1135 * sending of another URB.
1137 if ((xferlen % 64) == 0)
1140 DPRINTFN(10, ("sending frame len=%u rate=%u xfer len=%u\n",
1141 m0->m_pkthdr.len + RT2573_TX_DESC_SIZE, rate, xferlen));
1143 usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf, xferlen,
1144 USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RUM_TX_TIMEOUT, rum_txeof);
1146 error = usbd_transfer(data->xfer);
1147 if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS) {
1158 rum_start(struct ifnet *ifp)
1160 struct rum_softc *sc = ifp->if_softc;
1161 struct ieee80211com *ic = &sc->sc_ic;
1162 struct ieee80211_node *ni;
1165 ASSERT_SERIALIZED(ifp->if_serializer);
1168 * net80211 may still try to send management frames even if the
1169 * IFF_RUNNING flag is not set...
1171 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
1175 if (!IF_QEMPTY(&ic->ic_mgtq)) {
1176 if (sc->tx_queued >= RT2573_TX_LIST_COUNT) {
1177 ifp->if_flags |= IFF_OACTIVE;
1180 IF_DEQUEUE(&ic->ic_mgtq, m0);
1182 ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
1183 m0->m_pkthdr.rcvif = NULL;
1187 if (rum_tx_data(sc, m0, ni) != 0)
1191 struct ether_header *eh;
1193 if (ic->ic_state != IEEE80211_S_RUN)
1196 m0 = ifq_poll(&ifp->if_snd);
1199 if (sc->tx_queued >= RT2573_TX_LIST_COUNT) {
1200 ifp->if_flags |= IFF_OACTIVE;
1203 ifq_dequeue(&ifp->if_snd, m0);
1205 if (m0->m_len < sizeof (struct ether_header) &&
1206 !(m0 = m_pullup(m0, sizeof (struct ether_header))))
1209 eh = mtod(m0, struct ether_header *);
1210 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1218 m0 = ieee80211_encap(ic, m0, ni);
1222 if (ic->ic_rawbpf != NULL)
1223 bpf_mtap(ic->ic_rawbpf, m0);
1225 if (rum_tx_data(sc, m0, ni) != 0) {
1227 ieee80211_free_node(ni);
1233 sc->sc_tx_timer = 5;
1239 rum_watchdog(struct ifnet *ifp)
1241 struct rum_softc *sc = ifp->if_softc;
1243 ASSERT_SERIALIZED(ifp->if_serializer);
1247 if (sc->sc_tx_timer > 0) {
1248 if (--sc->sc_tx_timer == 0) {
1249 kprintf("%s: device timeout\n", USBDEVNAME(sc->sc_dev));
1250 /*rum_init(sc); XXX needs a process context! */
1257 ieee80211_watchdog(&sc->sc_ic);
1261 rum_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
1263 struct rum_softc *sc = ifp->if_softc;
1264 struct ieee80211com *ic = &sc->sc_ic;
1267 ASSERT_SERIALIZED(ifp->if_serializer);
1275 if (ifp->if_flags & IFF_UP) {
1276 if (ifp->if_flags & IFF_RUNNING)
1277 rum_update_promisc(sc);
1281 if (ifp->if_flags & IFF_RUNNING)
1286 error = ieee80211_ioctl(ic, cmd, data, cr);
1290 if (error == ENETRESET) {
1291 struct ieee80211req *ireq = (struct ieee80211req *)data;
1293 if (cmd == SIOCS80211 &&
1294 ireq->i_type == IEEE80211_IOC_CHANNEL &&
1295 ic->ic_opmode == IEEE80211_M_MONITOR) {
1297 * This allows for fast channel switching in monitor mode
1298 * (used by kismet). In IBSS mode, we must explicitly reset
1299 * the interface to generate a new beacon frame.
1301 rum_set_chan(sc, ic->ic_ibss_chan);
1302 } else if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
1303 (IFF_UP | IFF_RUNNING)) {
1317 rum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len)
1319 usb_device_request_t req;
1322 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1323 req.bRequest = RT2573_READ_EEPROM;
1324 USETW(req.wValue, 0);
1325 USETW(req.wIndex, addr);
1326 USETW(req.wLength, len);
1328 error = usbd_do_request(sc->sc_udev, &req, buf);
1330 kprintf("%s: could not read EEPROM: %s\n",
1331 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1336 rum_read(struct rum_softc *sc, uint16_t reg)
1340 rum_read_multi(sc, reg, &val, sizeof val);
1342 return le32toh(val);
1346 rum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len)
1348 usb_device_request_t req;
1351 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1352 req.bRequest = RT2573_READ_MULTI_MAC;
1353 USETW(req.wValue, 0);
1354 USETW(req.wIndex, reg);
1355 USETW(req.wLength, len);
1357 error = usbd_do_request(sc->sc_udev, &req, buf);
1359 kprintf("%s: could not multi read MAC register: %s\n",
1360 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1365 rum_write(struct rum_softc *sc, uint16_t reg, uint32_t val)
1367 uint32_t tmp = htole32(val);
1369 rum_write_multi(sc, reg, &tmp, sizeof tmp);
1373 rum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len)
1375 usb_device_request_t req;
1378 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1379 req.bRequest = RT2573_WRITE_MULTI_MAC;
1380 USETW(req.wValue, 0);
1381 USETW(req.wIndex, reg);
1382 USETW(req.wLength, len);
1384 error = usbd_do_request(sc->sc_udev, &req, buf);
1386 kprintf("%s: could not multi write MAC register: %s\n",
1387 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1392 rum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val)
1397 for (ntries = 0; ntries < 5; ntries++) {
1398 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1402 kprintf("%s: could not write to BBP\n", USBDEVNAME(sc->sc_dev));
1406 tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val;
1407 rum_write(sc, RT2573_PHY_CSR3, tmp);
1411 rum_bbp_read(struct rum_softc *sc, uint8_t reg)
1416 for (ntries = 0; ntries < 5; ntries++) {
1417 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1421 kprintf("%s: could not read BBP\n", USBDEVNAME(sc->sc_dev));
1425 val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8;
1426 rum_write(sc, RT2573_PHY_CSR3, val);
1428 for (ntries = 0; ntries < 100; ntries++) {
1429 val = rum_read(sc, RT2573_PHY_CSR3);
1430 if (!(val & RT2573_BBP_BUSY))
1435 kprintf("%s: could not read BBP\n", USBDEVNAME(sc->sc_dev));
1440 rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val)
1445 for (ntries = 0; ntries < 5; ntries++) {
1446 if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY))
1450 kprintf("%s: could not write to RF\n", USBDEVNAME(sc->sc_dev));
1454 tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 |
1456 rum_write(sc, RT2573_PHY_CSR4, tmp);
1458 /* remember last written value in sc */
1459 sc->rf_regs[reg] = val;
1461 DPRINTFN(15, ("RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff));
1465 rum_select_antenna(struct rum_softc *sc)
1467 uint8_t bbp4, bbp77;
1470 bbp4 = rum_bbp_read(sc, 4);
1471 bbp77 = rum_bbp_read(sc, 77);
1475 /* make sure Rx is disabled before switching antenna */
1476 tmp = rum_read(sc, RT2573_TXRX_CSR0);
1477 rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
1479 rum_bbp_write(sc, 4, bbp4);
1480 rum_bbp_write(sc, 77, bbp77);
1482 rum_write(sc, RT2573_TXRX_CSR0, tmp);
1486 * Enable multi-rate retries for frames sent at OFDM rates.
1487 * In 802.11b/g mode, allow fallback to CCK rates.
1490 rum_enable_mrr(struct rum_softc *sc)
1492 struct ieee80211com *ic = &sc->sc_ic;
1495 tmp = rum_read(sc, RT2573_TXRX_CSR4);
1497 tmp &= ~RT2573_MRR_CCK_FALLBACK;
1498 if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan))
1499 tmp |= RT2573_MRR_CCK_FALLBACK;
1500 tmp |= RT2573_MRR_ENABLED;
1502 rum_write(sc, RT2573_TXRX_CSR4, tmp);
1506 rum_set_txpreamble(struct rum_softc *sc)
1510 tmp = rum_read(sc, RT2573_TXRX_CSR4);
1512 tmp &= ~RT2573_SHORT_PREAMBLE;
1513 if (sc->sc_ic.ic_flags & IEEE80211_F_SHPREAMBLE)
1514 tmp |= RT2573_SHORT_PREAMBLE;
1516 rum_write(sc, RT2573_TXRX_CSR4, tmp);
1520 rum_set_basicrates(struct rum_softc *sc)
1522 struct ieee80211com *ic = &sc->sc_ic;
1524 /* update basic rate set */
1525 if (ic->ic_curmode == IEEE80211_MODE_11B) {
1526 /* 11b basic rates: 1, 2Mbps */
1527 rum_write(sc, RT2573_TXRX_CSR5, 0x3);
1528 } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bss->ni_chan)) {
1529 /* 11a basic rates: 6, 12, 24Mbps */
1530 rum_write(sc, RT2573_TXRX_CSR5, 0x150);
1532 /* 11g basic rates: 1, 2, 5.5, 11, 6, 12, 24Mbps */
1533 rum_write(sc, RT2573_TXRX_CSR5, 0x15f);
1538 * Reprogram MAC/BBP to switch to a new band. Values taken from the reference
1542 rum_select_band(struct rum_softc *sc, struct ieee80211_channel *c)
1544 uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104;
1547 /* update all BBP registers that depend on the band */
1548 bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c;
1549 bbp35 = 0x50; bbp97 = 0x48; bbp98 = 0x48;
1550 if (IEEE80211_IS_CHAN_5GHZ(c)) {
1551 bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c;
1552 bbp35 += 0x10; bbp97 += 0x10; bbp98 += 0x10;
1554 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1555 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1556 bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10;
1560 rum_bbp_write(sc, 17, bbp17);
1561 rum_bbp_write(sc, 96, bbp96);
1562 rum_bbp_write(sc, 104, bbp104);
1564 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1565 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1566 rum_bbp_write(sc, 75, 0x80);
1567 rum_bbp_write(sc, 86, 0x80);
1568 rum_bbp_write(sc, 88, 0x80);
1571 rum_bbp_write(sc, 35, bbp35);
1572 rum_bbp_write(sc, 97, bbp97);
1573 rum_bbp_write(sc, 98, bbp98);
1575 tmp = rum_read(sc, RT2573_PHY_CSR0);
1576 tmp &= ~(RT2573_PA_PE_2GHZ | RT2573_PA_PE_5GHZ);
1577 if (IEEE80211_IS_CHAN_2GHZ(c))
1578 tmp |= RT2573_PA_PE_2GHZ;
1580 tmp |= RT2573_PA_PE_5GHZ;
1581 rum_write(sc, RT2573_PHY_CSR0, tmp);
1583 /* 802.11a uses a 16 microseconds short interframe space */
1584 sc->sifs = IEEE80211_IS_CHAN_5GHZ(c) ? 16 : 10;
1588 rum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c)
1590 struct ieee80211com *ic = &sc->sc_ic;
1591 struct ifnet *ifp = &ic->ic_if;
1592 const struct rfprog *rfprog;
1593 uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT;
1597 ASSERT_SERIALIZED(ifp->if_serializer);
1599 chan = ieee80211_chan2ieee(ic, c);
1600 if (chan == 0 || chan == IEEE80211_CHAN_ANY)
1603 lwkt_serialize_exit(ifp->if_serializer);
1605 /* select the appropriate RF settings based on what EEPROM says */
1606 rfprog = (sc->rf_rev == RT2573_RF_5225 ||
1607 sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226;
1609 /* find the settings for this channel (we know it exists) */
1610 for (i = 0; rfprog[i].chan != chan; i++)
1613 power = sc->txpow[i];
1617 } else if (power > 31) {
1618 bbp94 += power - 31;
1623 * If we are switching from the 2GHz band to the 5GHz band or
1624 * vice-versa, BBP registers need to be reprogrammed.
1626 if (c->ic_flags != sc->sc_curchan->ic_flags) {
1627 rum_select_band(sc, c);
1628 rum_select_antenna(sc);
1632 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1633 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1634 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1635 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1637 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1638 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1639 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7 | 1);
1640 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1642 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1643 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1644 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1645 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1649 /* enable smart mode for MIMO-capable RFs */
1650 bbp3 = rum_bbp_read(sc, 3);
1652 bbp3 &= ~RT2573_SMART_MODE;
1653 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527)
1654 bbp3 |= RT2573_SMART_MODE;
1656 rum_bbp_write(sc, 3, bbp3);
1658 if (bbp94 != RT2573_BBPR94_DEFAULT)
1659 rum_bbp_write(sc, 94, bbp94);
1661 lwkt_serialize_enter(ifp->if_serializer);
1665 * Enable TSF synchronization and tell h/w to start sending beacons for IBSS
1666 * and HostAP operating modes.
1669 rum_enable_tsf_sync(struct rum_softc *sc)
1671 struct ieee80211com *ic = &sc->sc_ic;
1674 if (ic->ic_opmode != IEEE80211_M_STA) {
1676 * Change default 16ms TBTT adjustment to 8ms.
1677 * Must be done before enabling beacon generation.
1679 rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8);
1682 tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000;
1684 /* set beacon interval (in 1/16ms unit) */
1685 tmp |= ic->ic_bss->ni_intval * 16;
1687 tmp |= RT2573_TSF_TICKING | RT2573_ENABLE_TBTT;
1688 if (ic->ic_opmode == IEEE80211_M_STA)
1689 tmp |= RT2573_TSF_MODE(1);
1691 tmp |= RT2573_TSF_MODE(2) | RT2573_GENERATE_BEACON;
1693 rum_write(sc, RT2573_TXRX_CSR9, tmp);
1697 rum_update_slot(struct rum_softc *sc)
1699 struct ieee80211com *ic = &sc->sc_ic;
1703 slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
1705 tmp = rum_read(sc, RT2573_MAC_CSR9);
1706 tmp = (tmp & ~0xff) | slottime;
1707 rum_write(sc, RT2573_MAC_CSR9, tmp);
1709 DPRINTF(("setting slot time to %uus\n", slottime));
1713 rum_set_bssid(struct rum_softc *sc, const uint8_t *bssid)
1717 tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24;
1718 rum_write(sc, RT2573_MAC_CSR4, tmp);
1720 tmp = bssid[4] | bssid[5] << 8 | RT2573_ONE_BSSID << 16;
1721 rum_write(sc, RT2573_MAC_CSR5, tmp);
1725 rum_set_macaddr(struct rum_softc *sc, const uint8_t *addr)
1729 tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24;
1730 rum_write(sc, RT2573_MAC_CSR2, tmp);
1732 tmp = addr[4] | addr[5] << 8 | 0xff << 16;
1733 rum_write(sc, RT2573_MAC_CSR3, tmp);
1737 rum_update_promisc(struct rum_softc *sc)
1739 struct ifnet *ifp = &sc->sc_ic.ic_if;
1742 tmp = rum_read(sc, RT2573_TXRX_CSR0);
1744 tmp &= ~RT2573_DROP_NOT_TO_ME;
1745 if (!(ifp->if_flags & IFF_PROMISC))
1746 tmp |= RT2573_DROP_NOT_TO_ME;
1748 rum_write(sc, RT2573_TXRX_CSR0, tmp);
1750 DPRINTF(("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
1751 "entering" : "leaving"));
1758 case RT2573_RF_2527: return "RT2527 (MIMO XR)";
1759 case RT2573_RF_2528: return "RT2528";
1760 case RT2573_RF_5225: return "RT5225 (MIMO XR)";
1761 case RT2573_RF_5226: return "RT5226";
1762 default: return "unknown";
1767 rum_read_eeprom(struct rum_softc *sc)
1769 struct ieee80211com *ic = &sc->sc_ic;
1775 /* read MAC/BBP type */
1776 rum_eeprom_read(sc, RT2573_EEPROM_MACBBP, &val, 2);
1777 sc->macbbp_rev = le16toh(val);
1779 /* read MAC address */
1780 rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, ic->ic_myaddr, 6);
1782 rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2);
1784 sc->rf_rev = (val >> 11) & 0x1f;
1785 sc->hw_radio = (val >> 10) & 0x1;
1786 sc->rx_ant = (val >> 4) & 0x3;
1787 sc->tx_ant = (val >> 2) & 0x3;
1788 sc->nb_ant = val & 0x3;
1790 DPRINTF(("RF revision=%d\n", sc->rf_rev));
1792 rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2);
1794 sc->ext_5ghz_lna = (val >> 6) & 0x1;
1795 sc->ext_2ghz_lna = (val >> 4) & 0x1;
1797 DPRINTF(("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n",
1798 sc->ext_2ghz_lna, sc->ext_5ghz_lna));
1800 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2);
1802 if ((val & 0xff) != 0xff)
1803 sc->rssi_2ghz_corr = (int8_t)(val & 0xff); /* signed */
1805 /* Only [-10, 10] is valid */
1806 if (sc->rssi_2ghz_corr < -10 || sc->rssi_2ghz_corr > 10)
1807 sc->rssi_2ghz_corr = 0;
1809 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2);
1811 if ((val & 0xff) != 0xff)
1812 sc->rssi_5ghz_corr = (int8_t)(val & 0xff); /* signed */
1814 /* Only [-10, 10] is valid */
1815 if (sc->rssi_5ghz_corr < -10 || sc->rssi_5ghz_corr > 10)
1816 sc->rssi_5ghz_corr = 0;
1818 if (sc->ext_2ghz_lna)
1819 sc->rssi_2ghz_corr -= 14;
1820 if (sc->ext_5ghz_lna)
1821 sc->rssi_5ghz_corr -= 14;
1823 DPRINTF(("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n",
1824 sc->rssi_2ghz_corr, sc->rssi_5ghz_corr));
1826 rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2);
1828 if ((val & 0xff) != 0xff)
1829 sc->rffreq = val & 0xff;
1831 DPRINTF(("RF freq=%d\n", sc->rffreq));
1833 /* read Tx power for all a/b/g channels */
1834 rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14);
1835 /* XXX default Tx power for 802.11a channels */
1836 memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14);
1838 for (i = 0; i < 14; i++)
1839 DPRINTF(("Channel=%d Tx power=%d\n", i + 1, sc->txpow[i]));
1842 /* read default values for BBP registers */
1843 rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
1845 for (i = 0; i < 14; i++) {
1846 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
1848 DPRINTF(("BBP R%d=%02x\n", sc->bbp_prom[i].reg,
1849 sc->bbp_prom[i].val));
1855 rum_bbp_init(struct rum_softc *sc)
1857 #define N(a) (sizeof (a) / sizeof ((a)[0]))
1861 /* wait for BBP to be ready */
1862 for (ntries = 0; ntries < 100; ntries++) {
1863 val = rum_bbp_read(sc, 0);
1864 if (val != 0 && val != 0xff)
1868 if (ntries == 100) {
1869 kprintf("%s: timeout waiting for BBP\n",
1870 USBDEVNAME(sc->sc_dev));
1874 /* initialize BBP registers to default values */
1875 for (i = 0; i < N(rum_def_bbp); i++)
1876 rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val);
1878 /* write vendor-specific BBP values (from EEPROM) */
1879 for (i = 0; i < 16; i++) {
1880 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
1882 rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
1892 #define N(a) (sizeof (a) / sizeof ((a)[0]))
1893 struct rum_softc *sc = xsc;
1894 struct ieee80211com *ic = &sc->sc_ic;
1895 struct ifnet *ifp = &ic->ic_if;
1896 struct rum_rx_data *data;
1901 ASSERT_SERIALIZED(ifp->if_serializer);
1905 /* initialize MAC registers to default values */
1906 for (i = 0; i < N(rum_def_mac); i++)
1907 rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val);
1909 /* set host ready */
1910 rum_write(sc, RT2573_MAC_CSR1, 3);
1911 rum_write(sc, RT2573_MAC_CSR1, 0);
1913 /* wait for BBP/RF to wakeup */
1914 for (ntries = 0; ntries < 1000; ntries++) {
1915 if (rum_read(sc, RT2573_MAC_CSR12) & 8)
1917 rum_write(sc, RT2573_MAC_CSR12, 4); /* force wakeup */
1920 if (ntries == 1000) {
1921 kprintf("%s: timeout waiting for BBP/RF to wakeup\n",
1922 USBDEVNAME(sc->sc_dev));
1926 if ((error = rum_bbp_init(sc)) != 0)
1929 /* select default channel */
1930 sc->sc_curchan = ic->ic_curchan = ic->ic_ibss_chan;
1931 rum_select_band(sc, sc->sc_curchan);
1932 rum_select_antenna(sc);
1933 rum_set_chan(sc, sc->sc_curchan);
1935 /* clear STA registers */
1936 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
1938 IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
1939 rum_set_macaddr(sc, ic->ic_myaddr);
1941 /* initialize ASIC */
1942 rum_write(sc, RT2573_MAC_CSR1, 4);
1945 * Allocate xfer for AMRR statistics requests.
1947 sc->stats_xfer = usbd_alloc_xfer(sc->sc_udev);
1948 if (sc->stats_xfer == NULL) {
1949 kprintf("%s: could not allocate AMRR xfer\n",
1950 USBDEVNAME(sc->sc_dev));
1955 * Open Tx and Rx USB bulk pipes.
1957 error = usbd_open_pipe(sc->sc_iface, sc->sc_tx_no, USBD_EXCLUSIVE_USE,
1960 kprintf("%s: could not open Tx pipe: %s\n",
1961 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1965 error = usbd_open_pipe(sc->sc_iface, sc->sc_rx_no, USBD_EXCLUSIVE_USE,
1968 kprintf("%s: could not open Rx pipe: %s\n",
1969 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1974 * Allocate Tx and Rx xfer queues.
1976 error = rum_alloc_tx_list(sc);
1978 kprintf("%s: could not allocate Tx list\n",
1979 USBDEVNAME(sc->sc_dev));
1983 error = rum_alloc_rx_list(sc);
1985 kprintf("%s: could not allocate Rx list\n",
1986 USBDEVNAME(sc->sc_dev));
1991 * Start up the receive pipe.
1993 for (i = 0; i < RT2573_RX_LIST_COUNT; i++) {
1994 data = &sc->rx_data[i];
1996 usbd_setup_xfer(data->xfer, sc->sc_rx_pipeh, data, data->buf,
1997 MCLBYTES, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rum_rxeof);
1998 usbd_transfer(data->xfer);
2001 /* update Rx filter */
2002 tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff;
2004 tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR;
2005 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2006 tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR |
2008 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2009 tmp |= RT2573_DROP_TODS;
2010 if (!(ifp->if_flags & IFF_PROMISC))
2011 tmp |= RT2573_DROP_NOT_TO_ME;
2013 rum_write(sc, RT2573_TXRX_CSR0, tmp);
2015 ifp->if_flags &= ~IFF_OACTIVE;
2016 ifp->if_flags |= IFF_RUNNING;
2018 if (ic->ic_opmode == IEEE80211_M_MONITOR)
2019 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2021 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2031 rum_stop(struct rum_softc *sc)
2033 struct ieee80211com *ic = &sc->sc_ic;
2034 struct ifnet *ifp = &ic->ic_if;
2037 ASSERT_SERIALIZED(ifp->if_serializer);
2039 ieee80211_new_state(ic, IEEE80211_S_INIT, -1); /* free all nodes */
2041 sc->sc_tx_timer = 0;
2043 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2046 tmp = rum_read(sc, RT2573_TXRX_CSR0);
2047 rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
2050 rum_write(sc, RT2573_MAC_CSR1, 3);
2051 rum_write(sc, RT2573_MAC_CSR1, 0);
2053 if (sc->sc_rx_pipeh != NULL) {
2054 usbd_abort_pipe(sc->sc_rx_pipeh);
2055 usbd_close_pipe(sc->sc_rx_pipeh);
2056 sc->sc_rx_pipeh = NULL;
2059 if (sc->sc_tx_pipeh != NULL) {
2060 usbd_abort_pipe(sc->sc_tx_pipeh);
2061 usbd_close_pipe(sc->sc_tx_pipeh);
2062 sc->sc_tx_pipeh = NULL;
2065 rum_free_rx_list(sc);
2066 rum_free_tx_list(sc);
2070 rum_load_microcode(struct rum_softc *sc, const uint8_t *ucode, size_t size)
2072 usb_device_request_t req;
2073 uint16_t reg = RT2573_MCU_CODE_BASE;
2076 /* copy firmware image into NIC */
2077 for (; size >= 4; reg += 4, ucode += 4, size -= 4)
2078 rum_write(sc, reg, UGETDW(ucode));
2080 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2081 req.bRequest = RT2573_MCU_CNTL;
2082 USETW(req.wValue, RT2573_MCU_RUN);
2083 USETW(req.wIndex, 0);
2084 USETW(req.wLength, 0);
2086 error = usbd_do_request(sc->sc_udev, &req, NULL);
2088 kprintf("%s: could not run firmware: %s\n",
2089 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
2095 rum_prepare_beacon(struct rum_softc *sc)
2097 struct ieee80211com *ic = &sc->sc_ic;
2098 struct ieee80211_beacon_offsets bo;
2099 struct rum_tx_desc desc;
2103 m0 = ieee80211_beacon_alloc(ic, ic->ic_bss, &bo);
2105 if_printf(&ic->ic_if, "could not allocate beacon frame\n");
2109 /* send beacons at the lowest available rate */
2110 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_bss->ni_chan) ? 12 : 2;
2112 rum_setup_tx_desc(sc, &desc, RT2573_TX_TIMESTAMP, RT2573_TX_HWSEQ,
2113 m0->m_pkthdr.len, rate);
2115 /* copy the first 24 bytes of Tx descriptor into NIC memory */
2116 rum_write_multi(sc, RT2573_HW_BEACON_BASE0, (uint8_t *)&desc, 24);
2118 /* copy beacon header and payload into NIC memory */
2119 rum_write_multi(sc, RT2573_HW_BEACON_BASE0 + 24, mtod(m0, uint8_t *),
2128 rum_stats_timeout(void *arg)
2130 struct rum_softc *sc = arg;
2131 struct ifnet *ifp = &sc->sc_ic.ic_if;
2132 usb_device_request_t req;
2134 lwkt_serialize_enter(ifp->if_serializer);
2137 * Asynchronously read statistic registers (cleared by read).
2139 req.bmRequestType = UT_READ_VENDOR_DEVICE;
2140 req.bRequest = RT2573_READ_MULTI_MAC;
2141 USETW(req.wValue, 0);
2142 USETW(req.wIndex, RT2573_STA_CSR0);
2143 USETW(req.wLength, sizeof(sc->sta));
2145 usbd_setup_default_xfer(sc->stats_xfer, sc->sc_udev, sc,
2146 USBD_DEFAULT_TIMEOUT, &req,
2147 sc->sta, sizeof(sc->sta), 0,
2149 usbd_transfer(sc->stats_xfer);
2151 lwkt_serialize_exit(ifp->if_serializer);
2155 rum_stats_update(usbd_xfer_handle xfer, usbd_private_handle priv,
2158 struct rum_softc *sc = (struct rum_softc *)priv;
2159 struct ifnet *ifp = &sc->sc_ic.ic_if;
2160 struct ieee80211_ratectl_stats *stats = &sc->sc_stats;
2162 if (status != USBD_NORMAL_COMPLETION) {
2163 kprintf("%s: could not retrieve Tx statistics - cancelling "
2164 "automatic rate control\n", USBDEVNAME(sc->sc_dev));
2168 lwkt_serialize_enter(ifp->if_serializer);
2170 /* count TX retry-fail as Tx errors */
2171 ifp->if_oerrors += RUM_TX_PKT_FAIL(sc);
2173 stats->stats_pkt_noretry += RUM_TX_PKT_NO_RETRY(sc);
2174 stats->stats_pkt_ok += RUM_TX_PKT_NO_RETRY(sc) +
2175 RUM_TX_PKT_ONE_RETRY(sc) +
2176 RUM_TX_PKT_MULTI_RETRY(sc);
2177 stats->stats_pkt_err += RUM_TX_PKT_FAIL(sc);
2179 stats->stats_short_retries += RUM_TX_PKT_ONE_RETRY(sc);
2182 * XXX Estimated average:
2183 * Actual number of retries for each packet should belong to
2184 * [2, RUM_TX_SHORT_RETRY_MAX]
2186 stats->stats_short_retries +=
2187 RUM_TX_PKT_MULTI_RETRY(sc) *
2188 ((2 + RUM_TX_SHORT_RETRY_MAX) / 2);
2190 stats->stats_short_retries += RUM_TX_PKT_MULTI_RETRY(sc);
2192 stats->stats_short_retries +=
2193 RUM_TX_PKT_FAIL(sc) * RUM_TX_SHORT_RETRY_MAX;
2195 callout_reset(&sc->stats_ch, 4 * hz / 5, rum_stats_timeout, sc);
2197 lwkt_serialize_exit(ifp->if_serializer);
2201 rum_stats(struct ieee80211com *ic, struct ieee80211_node *ni __unused,
2202 struct ieee80211_ratectl_stats *stats)
2204 struct ifnet *ifp = &ic->ic_if;
2205 struct rum_softc *sc = ifp->if_softc;
2207 ASSERT_SERIALIZED(ifp->if_serializer);
2209 bcopy(&sc->sc_stats, stats, sizeof(*stats));
2210 bzero(&sc->sc_stats, sizeof(sc->sc_stats));
2214 rum_ratectl_change(struct ieee80211com *ic, u_int orc __unused, u_int nrc)
2216 struct ieee80211_ratectl_state *st = &ic->ic_ratectl;
2217 struct ieee80211_onoe_param *oparam;
2219 if (st->rc_st_param != NULL) {
2220 kfree(st->rc_st_param, M_DEVBUF);
2221 st->rc_st_param = NULL;
2225 case IEEE80211_RATECTL_ONOE:
2226 oparam = kmalloc(sizeof(*oparam), M_DEVBUF, M_INTWAIT);
2228 IEEE80211_ONOE_PARAM_SETUP(oparam);
2229 oparam->onoe_raise = 15;
2231 st->rc_st_param = oparam;
2233 case IEEE80211_RATECTL_NONE:
2234 /* This could only happen during detaching */
2237 panic("unknown rate control algo %u\n", nrc);
2242 rum_get_rssi(struct rum_softc *sc, uint8_t raw)
2246 lna = (raw >> 5) & 0x3;
2249 rssi = (2 * agc) - RT2573_NOISE_FLOOR;
2251 if (IEEE80211_IS_CHAN_2GHZ(sc->sc_curchan)) {
2252 rssi += sc->rssi_2ghz_corr;
2261 rssi += sc->rssi_5ghz_corr;
2263 if (!sc->ext_5ghz_lna && lna != 1)