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.6 2007/02/07 12:34:26 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 wh = mtod(m, struct ieee80211_frame_min *);
848 ni = ieee80211_find_rxnode(ic, wh);
850 /* Error happened during RSSI conversion. */
854 if (sc->sc_drvbpf != NULL) {
855 struct rum_rx_radiotap_header *tap = &sc->sc_rxtap;
858 tap->wr_rate = rum_rxrate(desc);
859 tap->wr_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq);
860 tap->wr_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
861 tap->wr_antenna = sc->rx_ant;
862 tap->wr_antsignal = rssi;
864 bpf_ptap(sc->sc_drvbpf, m, tap, sc->sc_rxtap_len);
867 /* send the frame to the 802.11 layer */
868 ieee80211_input(ic, m, ni, rssi, 0);
870 /* node is no longer needed */
871 ieee80211_free_node(ni);
874 * In HostAP mode, ieee80211_input() will enqueue packets in if_snd
875 * without calling if_start().
877 if (!ifq_is_empty(&ifp->if_snd) && !(ifp->if_flags & IFF_OACTIVE))
884 lwkt_serialize_exit(ifp->if_serializer);
886 DPRINTFN(15, ("rx done\n"));
888 skip: /* setup a new transfer */
889 bzero(data->buf, sizeof(struct rum_rx_desc));
890 usbd_setup_xfer(xfer, sc->sc_rx_pipeh, data, data->buf, MCLBYTES,
891 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rum_rxeof);
896 * This function is only used by the Rx radiotap code. It returns the rate at
897 * which a given frame was received.
900 rum_rxrate(struct rum_rx_desc *desc)
902 if (le32toh(desc->flags) & RT2573_RX_OFDM) {
903 /* reverse function of rum_plcp_signal */
904 switch (desc->rate) {
912 case 0xc: return 108;
915 if (desc->rate == 10)
917 if (desc->rate == 20)
919 if (desc->rate == 55)
921 if (desc->rate == 110)
924 return 2; /* should not get there */
928 * Return the expected ack rate for a frame transmitted at rate `rate'.
929 * XXX: this should depend on the destination node basic rate set.
932 rum_ack_rate(struct ieee80211com *ic, int rate)
941 return (ic->ic_curmode == IEEE80211_MODE_11B) ? 4 : rate;
957 /* default to 1Mbps */
962 * Compute the duration (in us) needed to transmit `len' bytes at rate `rate'.
963 * The function automatically determines the operating mode depending on the
964 * given rate. `flags' indicates whether short preamble is in use or not.
967 rum_txtime(int len, int rate, uint32_t flags)
971 if (RUM_RATE_IS_OFDM(rate)) {
972 /* IEEE Std 802.11a-1999, pp. 37 */
973 txtime = (8 + 4 * len + 3 + rate - 1) / rate;
974 txtime = 16 + 4 + 4 * txtime + 6;
976 /* IEEE Std 802.11b-1999, pp. 28 */
977 txtime = (16 * len + rate - 1) / rate;
978 if (rate != 2 && (flags & IEEE80211_F_SHPREAMBLE))
987 rum_plcp_signal(int rate)
990 /* CCK rates (returned values are device-dependent) */
996 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1000 case 36: return 0xe;
1001 case 48: return 0x9;
1002 case 72: return 0xd;
1003 case 96: return 0x8;
1004 case 108: return 0xc;
1006 /* unsupported rates (should not get there) */
1007 default: return 0xff;
1012 rum_setup_tx_desc(struct rum_softc *sc, struct rum_tx_desc *desc,
1013 uint32_t flags, uint16_t xflags, int len, int rate)
1015 struct ieee80211com *ic = &sc->sc_ic;
1016 uint16_t plcp_length;
1019 desc->flags = htole32(flags);
1020 desc->flags |= htole32(len << 16);
1022 desc->xflags = htole16(xflags);
1024 desc->wme = htole16(
1027 RT2573_LOGCWMIN(4) |
1028 RT2573_LOGCWMAX(10));
1030 /* setup PLCP fields */
1031 desc->plcp_signal = rum_plcp_signal(rate);
1032 desc->plcp_service = 4;
1034 len += IEEE80211_CRC_LEN;
1035 if (RUM_RATE_IS_OFDM(rate)) {
1036 desc->flags |= htole32(RT2573_TX_OFDM);
1038 plcp_length = len & 0xfff;
1039 desc->plcp_length_hi = plcp_length >> 6;
1040 desc->plcp_length_lo = plcp_length & 0x3f;
1042 plcp_length = (16 * len + rate - 1) / rate;
1044 remainder = (16 * len) % 22;
1045 if (remainder != 0 && remainder < 7)
1046 desc->plcp_service |= RT2573_PLCP_LENGEXT;
1048 desc->plcp_length_hi = plcp_length >> 8;
1049 desc->plcp_length_lo = plcp_length & 0xff;
1051 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1052 desc->plcp_signal |= 0x08;
1054 desc->flags |= htole32(RT2573_TX_VALID);
1057 #define RUM_TX_TIMEOUT 5000
1060 rum_tx_data(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1062 struct ieee80211com *ic = &sc->sc_ic;
1063 struct rum_tx_desc *desc;
1064 struct rum_tx_data *data;
1065 struct ieee80211_frame *wh;
1071 wh = mtod(m0, struct ieee80211_frame *);
1073 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1074 if (ieee80211_crypto_encap(ic, ni, m0) == NULL) {
1079 /* packet header may have moved, reset our local pointer */
1080 wh = mtod(m0, struct ieee80211_frame *);
1084 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
1085 IEEE80211_FC0_TYPE_MGT) {
1086 /* mgmt frames are sent at the lowest available bit-rate */
1087 rate = ni->ni_rates.rs_rates[0];
1089 if (ic->ic_fixed_rate != -1) {
1090 rate = ic->ic_sup_rates[ic->ic_curmode].
1091 rs_rates[ic->ic_fixed_rate];
1093 rate = ni->ni_rates.rs_rates[ni->ni_txrate];
1095 rate &= IEEE80211_RATE_VAL;
1097 rate = 2; /* fallback to 1Mbps; should not happen */
1099 data = &sc->tx_data[0];
1100 desc = (struct rum_tx_desc *)data->buf;
1105 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1106 flags |= RT2573_TX_ACK;
1108 dur = rum_txtime(RUM_ACK_SIZE, rum_ack_rate(ic, rate),
1109 ic->ic_flags) + sc->sifs;
1110 *(uint16_t *)wh->i_dur = htole16(dur);
1112 /* tell hardware to set timestamp in probe responses */
1114 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
1115 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
1116 flags |= RT2573_TX_TIMESTAMP;
1119 if (sc->sc_drvbpf != NULL) {
1120 struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
1123 tap->wt_rate = rate;
1124 tap->wt_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq);
1125 tap->wt_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
1126 tap->wt_antenna = sc->tx_ant;
1128 bpf_ptap(sc->sc_drvbpf, m0, tap, sc->sc_txtap_len);
1131 m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RT2573_TX_DESC_SIZE);
1132 rum_setup_tx_desc(sc, desc, flags, 0, m0->m_pkthdr.len, rate);
1134 /* align end on a 4-bytes boundary */
1135 xferlen = (RT2573_TX_DESC_SIZE + m0->m_pkthdr.len + 3) & ~3;
1138 * No space left in the last URB to store the extra 4 bytes, force
1139 * sending of another URB.
1141 if ((xferlen % 64) == 0)
1144 DPRINTFN(10, ("sending frame len=%u rate=%u xfer len=%u\n",
1145 m0->m_pkthdr.len + RT2573_TX_DESC_SIZE, rate, xferlen));
1147 usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf, xferlen,
1148 USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RUM_TX_TIMEOUT, rum_txeof);
1150 error = usbd_transfer(data->xfer);
1151 if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS) {
1162 rum_start(struct ifnet *ifp)
1164 struct rum_softc *sc = ifp->if_softc;
1165 struct ieee80211com *ic = &sc->sc_ic;
1166 struct ieee80211_node *ni;
1169 ASSERT_SERIALIZED(ifp->if_serializer);
1172 * net80211 may still try to send management frames even if the
1173 * IFF_RUNNING flag is not set...
1175 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
1179 if (!IF_QEMPTY(&ic->ic_mgtq)) {
1180 if (sc->tx_queued >= RT2573_TX_LIST_COUNT) {
1181 ifp->if_flags |= IFF_OACTIVE;
1184 IF_DEQUEUE(&ic->ic_mgtq, m0);
1186 ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
1187 m0->m_pkthdr.rcvif = NULL;
1191 if (rum_tx_data(sc, m0, ni) != 0)
1195 struct ether_header *eh;
1197 if (ic->ic_state != IEEE80211_S_RUN)
1200 m0 = ifq_poll(&ifp->if_snd);
1203 if (sc->tx_queued >= RT2573_TX_LIST_COUNT) {
1204 ifp->if_flags |= IFF_OACTIVE;
1207 ifq_dequeue(&ifp->if_snd, m0);
1209 if (m0->m_len < sizeof (struct ether_header) &&
1210 !(m0 = m_pullup(m0, sizeof (struct ether_header))))
1213 eh = mtod(m0, struct ether_header *);
1214 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1222 m0 = ieee80211_encap(ic, m0, ni);
1226 if (ic->ic_rawbpf != NULL)
1227 bpf_mtap(ic->ic_rawbpf, m0);
1229 if (rum_tx_data(sc, m0, ni) != 0) {
1231 ieee80211_free_node(ni);
1237 sc->sc_tx_timer = 5;
1243 rum_watchdog(struct ifnet *ifp)
1245 struct rum_softc *sc = ifp->if_softc;
1247 ASSERT_SERIALIZED(ifp->if_serializer);
1251 if (sc->sc_tx_timer > 0) {
1252 if (--sc->sc_tx_timer == 0) {
1253 kprintf("%s: device timeout\n", USBDEVNAME(sc->sc_dev));
1254 /*rum_init(sc); XXX needs a process context! */
1261 ieee80211_watchdog(&sc->sc_ic);
1265 rum_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
1267 struct rum_softc *sc = ifp->if_softc;
1268 struct ieee80211com *ic = &sc->sc_ic;
1271 ASSERT_SERIALIZED(ifp->if_serializer);
1279 if (ifp->if_flags & IFF_UP) {
1280 if (ifp->if_flags & IFF_RUNNING)
1281 rum_update_promisc(sc);
1285 if (ifp->if_flags & IFF_RUNNING)
1290 error = ieee80211_ioctl(ic, cmd, data, cr);
1294 if (error == ENETRESET) {
1295 struct ieee80211req *ireq = (struct ieee80211req *)data;
1297 if (cmd == SIOCS80211 &&
1298 ireq->i_type == IEEE80211_IOC_CHANNEL &&
1299 ic->ic_opmode == IEEE80211_M_MONITOR) {
1301 * This allows for fast channel switching in monitor mode
1302 * (used by kismet). In IBSS mode, we must explicitly reset
1303 * the interface to generate a new beacon frame.
1305 rum_set_chan(sc, ic->ic_ibss_chan);
1306 } else if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
1307 (IFF_UP | IFF_RUNNING)) {
1321 rum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len)
1323 usb_device_request_t req;
1326 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1327 req.bRequest = RT2573_READ_EEPROM;
1328 USETW(req.wValue, 0);
1329 USETW(req.wIndex, addr);
1330 USETW(req.wLength, len);
1332 error = usbd_do_request(sc->sc_udev, &req, buf);
1334 kprintf("%s: could not read EEPROM: %s\n",
1335 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1340 rum_read(struct rum_softc *sc, uint16_t reg)
1344 rum_read_multi(sc, reg, &val, sizeof val);
1346 return le32toh(val);
1350 rum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len)
1352 usb_device_request_t req;
1355 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1356 req.bRequest = RT2573_READ_MULTI_MAC;
1357 USETW(req.wValue, 0);
1358 USETW(req.wIndex, reg);
1359 USETW(req.wLength, len);
1361 error = usbd_do_request(sc->sc_udev, &req, buf);
1363 kprintf("%s: could not multi read MAC register: %s\n",
1364 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1369 rum_write(struct rum_softc *sc, uint16_t reg, uint32_t val)
1371 uint32_t tmp = htole32(val);
1373 rum_write_multi(sc, reg, &tmp, sizeof tmp);
1377 rum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len)
1379 usb_device_request_t req;
1382 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1383 req.bRequest = RT2573_WRITE_MULTI_MAC;
1384 USETW(req.wValue, 0);
1385 USETW(req.wIndex, reg);
1386 USETW(req.wLength, len);
1388 error = usbd_do_request(sc->sc_udev, &req, buf);
1390 kprintf("%s: could not multi write MAC register: %s\n",
1391 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1396 rum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val)
1401 for (ntries = 0; ntries < 5; ntries++) {
1402 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1406 kprintf("%s: could not write to BBP\n", USBDEVNAME(sc->sc_dev));
1410 tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val;
1411 rum_write(sc, RT2573_PHY_CSR3, tmp);
1415 rum_bbp_read(struct rum_softc *sc, uint8_t reg)
1420 for (ntries = 0; ntries < 5; ntries++) {
1421 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1425 kprintf("%s: could not read BBP\n", USBDEVNAME(sc->sc_dev));
1429 val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8;
1430 rum_write(sc, RT2573_PHY_CSR3, val);
1432 for (ntries = 0; ntries < 100; ntries++) {
1433 val = rum_read(sc, RT2573_PHY_CSR3);
1434 if (!(val & RT2573_BBP_BUSY))
1439 kprintf("%s: could not read BBP\n", USBDEVNAME(sc->sc_dev));
1444 rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val)
1449 for (ntries = 0; ntries < 5; ntries++) {
1450 if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY))
1454 kprintf("%s: could not write to RF\n", USBDEVNAME(sc->sc_dev));
1458 tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 |
1460 rum_write(sc, RT2573_PHY_CSR4, tmp);
1462 /* remember last written value in sc */
1463 sc->rf_regs[reg] = val;
1465 DPRINTFN(15, ("RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff));
1469 rum_select_antenna(struct rum_softc *sc)
1471 uint8_t bbp4, bbp77;
1474 bbp4 = rum_bbp_read(sc, 4);
1475 bbp77 = rum_bbp_read(sc, 77);
1479 /* make sure Rx is disabled before switching antenna */
1480 tmp = rum_read(sc, RT2573_TXRX_CSR0);
1481 rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
1483 rum_bbp_write(sc, 4, bbp4);
1484 rum_bbp_write(sc, 77, bbp77);
1486 rum_write(sc, RT2573_TXRX_CSR0, tmp);
1490 * Enable multi-rate retries for frames sent at OFDM rates.
1491 * In 802.11b/g mode, allow fallback to CCK rates.
1494 rum_enable_mrr(struct rum_softc *sc)
1496 struct ieee80211com *ic = &sc->sc_ic;
1499 tmp = rum_read(sc, RT2573_TXRX_CSR4);
1501 tmp &= ~RT2573_MRR_CCK_FALLBACK;
1502 if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan))
1503 tmp |= RT2573_MRR_CCK_FALLBACK;
1504 tmp |= RT2573_MRR_ENABLED;
1506 rum_write(sc, RT2573_TXRX_CSR4, tmp);
1510 rum_set_txpreamble(struct rum_softc *sc)
1514 tmp = rum_read(sc, RT2573_TXRX_CSR4);
1516 tmp &= ~RT2573_SHORT_PREAMBLE;
1517 if (sc->sc_ic.ic_flags & IEEE80211_F_SHPREAMBLE)
1518 tmp |= RT2573_SHORT_PREAMBLE;
1520 rum_write(sc, RT2573_TXRX_CSR4, tmp);
1524 rum_set_basicrates(struct rum_softc *sc)
1526 struct ieee80211com *ic = &sc->sc_ic;
1528 /* update basic rate set */
1529 if (ic->ic_curmode == IEEE80211_MODE_11B) {
1530 /* 11b basic rates: 1, 2Mbps */
1531 rum_write(sc, RT2573_TXRX_CSR5, 0x3);
1532 } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bss->ni_chan)) {
1533 /* 11a basic rates: 6, 12, 24Mbps */
1534 rum_write(sc, RT2573_TXRX_CSR5, 0x150);
1536 /* 11g basic rates: 1, 2, 5.5, 11, 6, 12, 24Mbps */
1537 rum_write(sc, RT2573_TXRX_CSR5, 0x15f);
1542 * Reprogram MAC/BBP to switch to a new band. Values taken from the reference
1546 rum_select_band(struct rum_softc *sc, struct ieee80211_channel *c)
1548 uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104;
1551 /* update all BBP registers that depend on the band */
1552 bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c;
1553 bbp35 = 0x50; bbp97 = 0x48; bbp98 = 0x48;
1554 if (IEEE80211_IS_CHAN_5GHZ(c)) {
1555 bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c;
1556 bbp35 += 0x10; bbp97 += 0x10; bbp98 += 0x10;
1558 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1559 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1560 bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10;
1564 rum_bbp_write(sc, 17, bbp17);
1565 rum_bbp_write(sc, 96, bbp96);
1566 rum_bbp_write(sc, 104, bbp104);
1568 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1569 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1570 rum_bbp_write(sc, 75, 0x80);
1571 rum_bbp_write(sc, 86, 0x80);
1572 rum_bbp_write(sc, 88, 0x80);
1575 rum_bbp_write(sc, 35, bbp35);
1576 rum_bbp_write(sc, 97, bbp97);
1577 rum_bbp_write(sc, 98, bbp98);
1579 tmp = rum_read(sc, RT2573_PHY_CSR0);
1580 tmp &= ~(RT2573_PA_PE_2GHZ | RT2573_PA_PE_5GHZ);
1581 if (IEEE80211_IS_CHAN_2GHZ(c))
1582 tmp |= RT2573_PA_PE_2GHZ;
1584 tmp |= RT2573_PA_PE_5GHZ;
1585 rum_write(sc, RT2573_PHY_CSR0, tmp);
1587 /* 802.11a uses a 16 microseconds short interframe space */
1588 sc->sifs = IEEE80211_IS_CHAN_5GHZ(c) ? 16 : 10;
1592 rum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c)
1594 struct ieee80211com *ic = &sc->sc_ic;
1595 struct ifnet *ifp = &ic->ic_if;
1596 const struct rfprog *rfprog;
1597 uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT;
1601 ASSERT_SERIALIZED(ifp->if_serializer);
1603 chan = ieee80211_chan2ieee(ic, c);
1604 if (chan == 0 || chan == IEEE80211_CHAN_ANY)
1607 lwkt_serialize_exit(ifp->if_serializer);
1609 /* select the appropriate RF settings based on what EEPROM says */
1610 rfprog = (sc->rf_rev == RT2573_RF_5225 ||
1611 sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226;
1613 /* find the settings for this channel (we know it exists) */
1614 for (i = 0; rfprog[i].chan != chan; i++)
1617 power = sc->txpow[i];
1621 } else if (power > 31) {
1622 bbp94 += power - 31;
1627 * If we are switching from the 2GHz band to the 5GHz band or
1628 * vice-versa, BBP registers need to be reprogrammed.
1630 if (c->ic_flags != sc->sc_curchan->ic_flags) {
1631 rum_select_band(sc, c);
1632 rum_select_antenna(sc);
1636 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1637 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1638 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1639 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1641 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1642 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1643 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7 | 1);
1644 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1646 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1647 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1648 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1649 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1653 /* enable smart mode for MIMO-capable RFs */
1654 bbp3 = rum_bbp_read(sc, 3);
1656 bbp3 &= ~RT2573_SMART_MODE;
1657 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527)
1658 bbp3 |= RT2573_SMART_MODE;
1660 rum_bbp_write(sc, 3, bbp3);
1662 if (bbp94 != RT2573_BBPR94_DEFAULT)
1663 rum_bbp_write(sc, 94, bbp94);
1665 lwkt_serialize_enter(ifp->if_serializer);
1669 * Enable TSF synchronization and tell h/w to start sending beacons for IBSS
1670 * and HostAP operating modes.
1673 rum_enable_tsf_sync(struct rum_softc *sc)
1675 struct ieee80211com *ic = &sc->sc_ic;
1678 if (ic->ic_opmode != IEEE80211_M_STA) {
1680 * Change default 16ms TBTT adjustment to 8ms.
1681 * Must be done before enabling beacon generation.
1683 rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8);
1686 tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000;
1688 /* set beacon interval (in 1/16ms unit) */
1689 tmp |= ic->ic_bss->ni_intval * 16;
1691 tmp |= RT2573_TSF_TICKING | RT2573_ENABLE_TBTT;
1692 if (ic->ic_opmode == IEEE80211_M_STA)
1693 tmp |= RT2573_TSF_MODE(1);
1695 tmp |= RT2573_TSF_MODE(2) | RT2573_GENERATE_BEACON;
1697 rum_write(sc, RT2573_TXRX_CSR9, tmp);
1701 rum_update_slot(struct rum_softc *sc)
1703 struct ieee80211com *ic = &sc->sc_ic;
1707 slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
1709 tmp = rum_read(sc, RT2573_MAC_CSR9);
1710 tmp = (tmp & ~0xff) | slottime;
1711 rum_write(sc, RT2573_MAC_CSR9, tmp);
1713 DPRINTF(("setting slot time to %uus\n", slottime));
1717 rum_set_bssid(struct rum_softc *sc, const uint8_t *bssid)
1721 tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24;
1722 rum_write(sc, RT2573_MAC_CSR4, tmp);
1724 tmp = bssid[4] | bssid[5] << 8 | RT2573_ONE_BSSID << 16;
1725 rum_write(sc, RT2573_MAC_CSR5, tmp);
1729 rum_set_macaddr(struct rum_softc *sc, const uint8_t *addr)
1733 tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24;
1734 rum_write(sc, RT2573_MAC_CSR2, tmp);
1736 tmp = addr[4] | addr[5] << 8 | 0xff << 16;
1737 rum_write(sc, RT2573_MAC_CSR3, tmp);
1741 rum_update_promisc(struct rum_softc *sc)
1743 struct ifnet *ifp = &sc->sc_ic.ic_if;
1746 tmp = rum_read(sc, RT2573_TXRX_CSR0);
1748 tmp &= ~RT2573_DROP_NOT_TO_ME;
1749 if (!(ifp->if_flags & IFF_PROMISC))
1750 tmp |= RT2573_DROP_NOT_TO_ME;
1752 rum_write(sc, RT2573_TXRX_CSR0, tmp);
1754 DPRINTF(("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
1755 "entering" : "leaving"));
1762 case RT2573_RF_2527: return "RT2527 (MIMO XR)";
1763 case RT2573_RF_2528: return "RT2528";
1764 case RT2573_RF_5225: return "RT5225 (MIMO XR)";
1765 case RT2573_RF_5226: return "RT5226";
1766 default: return "unknown";
1771 rum_read_eeprom(struct rum_softc *sc)
1773 struct ieee80211com *ic = &sc->sc_ic;
1779 /* read MAC/BBP type */
1780 rum_eeprom_read(sc, RT2573_EEPROM_MACBBP, &val, 2);
1781 sc->macbbp_rev = le16toh(val);
1783 /* read MAC address */
1784 rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, ic->ic_myaddr, 6);
1786 rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2);
1788 sc->rf_rev = (val >> 11) & 0x1f;
1789 sc->hw_radio = (val >> 10) & 0x1;
1790 sc->rx_ant = (val >> 4) & 0x3;
1791 sc->tx_ant = (val >> 2) & 0x3;
1792 sc->nb_ant = val & 0x3;
1794 DPRINTF(("RF revision=%d\n", sc->rf_rev));
1796 rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2);
1798 sc->ext_5ghz_lna = (val >> 6) & 0x1;
1799 sc->ext_2ghz_lna = (val >> 4) & 0x1;
1801 DPRINTF(("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n",
1802 sc->ext_2ghz_lna, sc->ext_5ghz_lna));
1804 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2);
1806 if ((val & 0xff) != 0xff)
1807 sc->rssi_2ghz_corr = (int8_t)(val & 0xff); /* signed */
1809 /* Only [-10, 10] is valid */
1810 if (sc->rssi_2ghz_corr < -10 || sc->rssi_2ghz_corr > 10)
1811 sc->rssi_2ghz_corr = 0;
1813 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2);
1815 if ((val & 0xff) != 0xff)
1816 sc->rssi_5ghz_corr = (int8_t)(val & 0xff); /* signed */
1818 /* Only [-10, 10] is valid */
1819 if (sc->rssi_5ghz_corr < -10 || sc->rssi_5ghz_corr > 10)
1820 sc->rssi_5ghz_corr = 0;
1822 if (sc->ext_2ghz_lna)
1823 sc->rssi_2ghz_corr -= 14;
1824 if (sc->ext_5ghz_lna)
1825 sc->rssi_5ghz_corr -= 14;
1827 DPRINTF(("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n",
1828 sc->rssi_2ghz_corr, sc->rssi_5ghz_corr));
1830 rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2);
1832 if ((val & 0xff) != 0xff)
1833 sc->rffreq = val & 0xff;
1835 DPRINTF(("RF freq=%d\n", sc->rffreq));
1837 /* read Tx power for all a/b/g channels */
1838 rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14);
1839 /* XXX default Tx power for 802.11a channels */
1840 memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14);
1842 for (i = 0; i < 14; i++)
1843 DPRINTF(("Channel=%d Tx power=%d\n", i + 1, sc->txpow[i]));
1846 /* read default values for BBP registers */
1847 rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
1849 for (i = 0; i < 14; i++) {
1850 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
1852 DPRINTF(("BBP R%d=%02x\n", sc->bbp_prom[i].reg,
1853 sc->bbp_prom[i].val));
1859 rum_bbp_init(struct rum_softc *sc)
1861 #define N(a) (sizeof (a) / sizeof ((a)[0]))
1865 /* wait for BBP to be ready */
1866 for (ntries = 0; ntries < 100; ntries++) {
1867 val = rum_bbp_read(sc, 0);
1868 if (val != 0 && val != 0xff)
1872 if (ntries == 100) {
1873 kprintf("%s: timeout waiting for BBP\n",
1874 USBDEVNAME(sc->sc_dev));
1878 /* initialize BBP registers to default values */
1879 for (i = 0; i < N(rum_def_bbp); i++)
1880 rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val);
1882 /* write vendor-specific BBP values (from EEPROM) */
1883 for (i = 0; i < 16; i++) {
1884 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
1886 rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
1896 #define N(a) (sizeof (a) / sizeof ((a)[0]))
1897 struct rum_softc *sc = xsc;
1898 struct ieee80211com *ic = &sc->sc_ic;
1899 struct ifnet *ifp = &ic->ic_if;
1900 struct rum_rx_data *data;
1905 ASSERT_SERIALIZED(ifp->if_serializer);
1909 /* initialize MAC registers to default values */
1910 for (i = 0; i < N(rum_def_mac); i++)
1911 rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val);
1913 /* set host ready */
1914 rum_write(sc, RT2573_MAC_CSR1, 3);
1915 rum_write(sc, RT2573_MAC_CSR1, 0);
1917 /* wait for BBP/RF to wakeup */
1918 for (ntries = 0; ntries < 1000; ntries++) {
1919 if (rum_read(sc, RT2573_MAC_CSR12) & 8)
1921 rum_write(sc, RT2573_MAC_CSR12, 4); /* force wakeup */
1924 if (ntries == 1000) {
1925 kprintf("%s: timeout waiting for BBP/RF to wakeup\n",
1926 USBDEVNAME(sc->sc_dev));
1930 if ((error = rum_bbp_init(sc)) != 0)
1933 /* select default channel */
1934 sc->sc_curchan = ic->ic_curchan = ic->ic_ibss_chan;
1935 rum_select_band(sc, sc->sc_curchan);
1936 rum_select_antenna(sc);
1937 rum_set_chan(sc, sc->sc_curchan);
1939 /* clear STA registers */
1940 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
1942 IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
1943 rum_set_macaddr(sc, ic->ic_myaddr);
1945 /* initialize ASIC */
1946 rum_write(sc, RT2573_MAC_CSR1, 4);
1949 * Allocate xfer for AMRR statistics requests.
1951 sc->stats_xfer = usbd_alloc_xfer(sc->sc_udev);
1952 if (sc->stats_xfer == NULL) {
1953 kprintf("%s: could not allocate AMRR xfer\n",
1954 USBDEVNAME(sc->sc_dev));
1959 * Open Tx and Rx USB bulk pipes.
1961 error = usbd_open_pipe(sc->sc_iface, sc->sc_tx_no, USBD_EXCLUSIVE_USE,
1964 kprintf("%s: could not open Tx pipe: %s\n",
1965 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1969 error = usbd_open_pipe(sc->sc_iface, sc->sc_rx_no, USBD_EXCLUSIVE_USE,
1972 kprintf("%s: could not open Rx pipe: %s\n",
1973 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1978 * Allocate Tx and Rx xfer queues.
1980 error = rum_alloc_tx_list(sc);
1982 kprintf("%s: could not allocate Tx list\n",
1983 USBDEVNAME(sc->sc_dev));
1987 error = rum_alloc_rx_list(sc);
1989 kprintf("%s: could not allocate Rx list\n",
1990 USBDEVNAME(sc->sc_dev));
1995 * Start up the receive pipe.
1997 for (i = 0; i < RT2573_RX_LIST_COUNT; i++) {
1998 data = &sc->rx_data[i];
2000 usbd_setup_xfer(data->xfer, sc->sc_rx_pipeh, data, data->buf,
2001 MCLBYTES, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rum_rxeof);
2002 usbd_transfer(data->xfer);
2005 /* update Rx filter */
2006 tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff;
2008 tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR;
2009 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2010 tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR |
2012 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2013 tmp |= RT2573_DROP_TODS;
2014 if (!(ifp->if_flags & IFF_PROMISC))
2015 tmp |= RT2573_DROP_NOT_TO_ME;
2017 rum_write(sc, RT2573_TXRX_CSR0, tmp);
2019 ifp->if_flags &= ~IFF_OACTIVE;
2020 ifp->if_flags |= IFF_RUNNING;
2022 if (ic->ic_opmode == IEEE80211_M_MONITOR)
2023 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2025 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2035 rum_stop(struct rum_softc *sc)
2037 struct ieee80211com *ic = &sc->sc_ic;
2038 struct ifnet *ifp = &ic->ic_if;
2041 ASSERT_SERIALIZED(ifp->if_serializer);
2043 ieee80211_new_state(ic, IEEE80211_S_INIT, -1); /* free all nodes */
2045 sc->sc_tx_timer = 0;
2047 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2050 tmp = rum_read(sc, RT2573_TXRX_CSR0);
2051 rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
2054 rum_write(sc, RT2573_MAC_CSR1, 3);
2055 rum_write(sc, RT2573_MAC_CSR1, 0);
2057 if (sc->sc_rx_pipeh != NULL) {
2058 usbd_abort_pipe(sc->sc_rx_pipeh);
2059 usbd_close_pipe(sc->sc_rx_pipeh);
2060 sc->sc_rx_pipeh = NULL;
2063 if (sc->sc_tx_pipeh != NULL) {
2064 usbd_abort_pipe(sc->sc_tx_pipeh);
2065 usbd_close_pipe(sc->sc_tx_pipeh);
2066 sc->sc_tx_pipeh = NULL;
2069 rum_free_rx_list(sc);
2070 rum_free_tx_list(sc);
2074 rum_load_microcode(struct rum_softc *sc, const uint8_t *ucode, size_t size)
2076 usb_device_request_t req;
2077 uint16_t reg = RT2573_MCU_CODE_BASE;
2080 /* copy firmware image into NIC */
2081 for (; size >= 4; reg += 4, ucode += 4, size -= 4)
2082 rum_write(sc, reg, UGETDW(ucode));
2084 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2085 req.bRequest = RT2573_MCU_CNTL;
2086 USETW(req.wValue, RT2573_MCU_RUN);
2087 USETW(req.wIndex, 0);
2088 USETW(req.wLength, 0);
2090 error = usbd_do_request(sc->sc_udev, &req, NULL);
2092 kprintf("%s: could not run firmware: %s\n",
2093 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
2099 rum_prepare_beacon(struct rum_softc *sc)
2101 struct ieee80211com *ic = &sc->sc_ic;
2102 struct ieee80211_beacon_offsets bo;
2103 struct rum_tx_desc desc;
2107 m0 = ieee80211_beacon_alloc(ic, ic->ic_bss, &bo);
2109 if_printf(&ic->ic_if, "could not allocate beacon frame\n");
2113 /* send beacons at the lowest available rate */
2114 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_bss->ni_chan) ? 12 : 2;
2116 rum_setup_tx_desc(sc, &desc, RT2573_TX_TIMESTAMP, RT2573_TX_HWSEQ,
2117 m0->m_pkthdr.len, rate);
2119 /* copy the first 24 bytes of Tx descriptor into NIC memory */
2120 rum_write_multi(sc, RT2573_HW_BEACON_BASE0, (uint8_t *)&desc, 24);
2122 /* copy beacon header and payload into NIC memory */
2123 rum_write_multi(sc, RT2573_HW_BEACON_BASE0 + 24, mtod(m0, uint8_t *),
2132 rum_stats_timeout(void *arg)
2134 struct rum_softc *sc = arg;
2135 struct ifnet *ifp = &sc->sc_ic.ic_if;
2136 usb_device_request_t req;
2138 lwkt_serialize_enter(ifp->if_serializer);
2141 * Asynchronously read statistic registers (cleared by read).
2143 req.bmRequestType = UT_READ_VENDOR_DEVICE;
2144 req.bRequest = RT2573_READ_MULTI_MAC;
2145 USETW(req.wValue, 0);
2146 USETW(req.wIndex, RT2573_STA_CSR0);
2147 USETW(req.wLength, sizeof(sc->sta));
2149 usbd_setup_default_xfer(sc->stats_xfer, sc->sc_udev, sc,
2150 USBD_DEFAULT_TIMEOUT, &req,
2151 sc->sta, sizeof(sc->sta), 0,
2153 usbd_transfer(sc->stats_xfer);
2155 lwkt_serialize_exit(ifp->if_serializer);
2159 rum_stats_update(usbd_xfer_handle xfer, usbd_private_handle priv,
2162 struct rum_softc *sc = (struct rum_softc *)priv;
2163 struct ifnet *ifp = &sc->sc_ic.ic_if;
2164 struct ieee80211_ratectl_stats *stats = &sc->sc_stats;
2166 if (status != USBD_NORMAL_COMPLETION) {
2167 kprintf("%s: could not retrieve Tx statistics - cancelling "
2168 "automatic rate control\n", USBDEVNAME(sc->sc_dev));
2172 lwkt_serialize_enter(ifp->if_serializer);
2174 /* count TX retry-fail as Tx errors */
2175 ifp->if_oerrors += RUM_TX_PKT_FAIL(sc);
2177 stats->stats_pkt_noretry += RUM_TX_PKT_NO_RETRY(sc);
2178 stats->stats_pkt_ok += RUM_TX_PKT_NO_RETRY(sc) +
2179 RUM_TX_PKT_ONE_RETRY(sc) +
2180 RUM_TX_PKT_MULTI_RETRY(sc);
2181 stats->stats_pkt_err += RUM_TX_PKT_FAIL(sc);
2183 stats->stats_short_retries += RUM_TX_PKT_ONE_RETRY(sc);
2186 * XXX Estimated average:
2187 * Actual number of retries for each packet should belong to
2188 * [2, RUM_TX_SHORT_RETRY_MAX]
2190 stats->stats_short_retries +=
2191 RUM_TX_PKT_MULTI_RETRY(sc) *
2192 ((2 + RUM_TX_SHORT_RETRY_MAX) / 2);
2194 stats->stats_short_retries += RUM_TX_PKT_MULTI_RETRY(sc);
2196 stats->stats_short_retries +=
2197 RUM_TX_PKT_FAIL(sc) * RUM_TX_SHORT_RETRY_MAX;
2199 callout_reset(&sc->stats_ch, 4 * hz / 5, rum_stats_timeout, sc);
2201 lwkt_serialize_exit(ifp->if_serializer);
2205 rum_stats(struct ieee80211com *ic, struct ieee80211_node *ni __unused,
2206 struct ieee80211_ratectl_stats *stats)
2208 struct ifnet *ifp = &ic->ic_if;
2209 struct rum_softc *sc = ifp->if_softc;
2211 ASSERT_SERIALIZED(ifp->if_serializer);
2213 bcopy(&sc->sc_stats, stats, sizeof(*stats));
2214 bzero(&sc->sc_stats, sizeof(sc->sc_stats));
2218 rum_ratectl_change(struct ieee80211com *ic, u_int orc __unused, u_int nrc)
2220 struct ieee80211_ratectl_state *st = &ic->ic_ratectl;
2221 struct ieee80211_onoe_param *oparam;
2223 if (st->rc_st_param != NULL) {
2224 kfree(st->rc_st_param, M_DEVBUF);
2225 st->rc_st_param = NULL;
2229 case IEEE80211_RATECTL_ONOE:
2230 oparam = kmalloc(sizeof(*oparam), M_DEVBUF, M_INTWAIT);
2232 IEEE80211_ONOE_PARAM_SETUP(oparam);
2233 oparam->onoe_raise = 15;
2235 st->rc_st_param = oparam;
2237 case IEEE80211_RATECTL_NONE:
2238 /* This could only happen during detaching */
2241 panic("unknown rate control algo %u\n", nrc);
2246 rum_get_rssi(struct rum_softc *sc, uint8_t raw)
2250 lna = (raw >> 5) & 0x3;
2257 * NB: Since RSSI is relative to noise floor, -1 is
2258 * adequate for caller to know error happened.
2263 rssi = (2 * agc) - RT2573_NOISE_FLOOR;
2265 if (IEEE80211_IS_CHAN_2GHZ(sc->sc_curchan)) {
2266 rssi += sc->rssi_2ghz_corr;
2275 rssi += sc->rssi_5ghz_corr;
2277 if (!sc->ext_5ghz_lna && lna != 1)