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.18 2007/07/27 18:07:21 hasso 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) kprintf x; } while (0)
64 #define DPRINTFN(n, x) do { if (rum_debug >= (n)) kprintf 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_HWU54DM
},
74 { USB_VENDOR_ABOCOM
, USB_PRODUCT_ABOCOM_RT2573_2
},
75 { USB_VENDOR_ABOCOM
, USB_PRODUCT_ABOCOM_RT2573_3
},
76 { USB_VENDOR_ABOCOM
, USB_PRODUCT_ABOCOM_RT2573_4
},
77 { USB_VENDOR_ABOCOM
, USB_PRODUCT_ABOCOM_WUG2700
},
78 { USB_VENDOR_AMIT
, USB_PRODUCT_AMIT_CGWLUSB2GO
},
79 { USB_VENDOR_ASUS
, USB_PRODUCT_ASUS_WL167G_2
},
80 { USB_VENDOR_ASUS
, USB_PRODUCT_ASUS_WL167G_3
},
81 { USB_VENDOR_BELKIN
, USB_PRODUCT_BELKIN_F5D7050A
},
82 { USB_VENDOR_BELKIN
, USB_PRODUCT_BELKIN_F5D9050V3
},
83 { USB_VENDOR_LINKSYS4
, USB_PRODUCT_LINKSYS4_WUSB54GC
},
84 { USB_VENDOR_LINKSYS4
, USB_PRODUCT_LINKSYS4_WUSB54GR
},
85 { USB_VENDOR_CONCEPTRONIC2
, USB_PRODUCT_CONCEPTRONIC2_C54RU2
},
86 { USB_VENDOR_DICKSMITH
, USB_PRODUCT_DICKSMITH_CWD854F
},
87 { USB_VENDOR_DICKSMITH
, USB_PRODUCT_DICKSMITH_RT2573
},
88 { USB_VENDOR_DLINK2
, USB_PRODUCT_DLINK2_DWLG122C1
},
89 { USB_VENDOR_DLINK2
, USB_PRODUCT_DLINK2_WUA1340
},
90 { USB_VENDOR_GIGABYTE
, USB_PRODUCT_GIGABYTE_GNWB01GS
},
91 { USB_VENDOR_GIGABYTE
, USB_PRODUCT_GIGABYTE_GNWI05GS
},
92 { USB_VENDOR_GIGASET
, USB_PRODUCT_GIGASET_RT2573
},
93 { USB_VENDOR_GOODWAY
, USB_PRODUCT_GOODWAY_RT2573
},
94 { USB_VENDOR_GUILLEMOT
, USB_PRODUCT_GUILLEMOT_HWGUSB254LB
},
95 { USB_VENDOR_GUILLEMOT
, USB_PRODUCT_GUILLEMOT_HWGUSB254V2AP
},
96 { USB_VENDOR_HUAWEI3COM
, USB_PRODUCT_HUAWEI3COM_RT2573
},
97 { USB_VENDOR_MELCO
, USB_PRODUCT_MELCO_G54HP
},
98 { USB_VENDOR_MELCO
, USB_PRODUCT_MELCO_SG54HP
},
99 { USB_VENDOR_MSI
, USB_PRODUCT_MSI_RT2573_1
},
100 { USB_VENDOR_MSI
, USB_PRODUCT_MSI_RT2573_2
},
101 { USB_VENDOR_MSI
, USB_PRODUCT_MSI_RT2573_3
},
102 { USB_VENDOR_MSI
, USB_PRODUCT_MSI_RT2573_4
},
103 { USB_VENDOR_NOVATECH
, USB_PRODUCT_NOVATECH_RT2573
},
104 { USB_VENDOR_PLANEX2
, USB_PRODUCT_PLANEX2_GWUS54HP
},
105 { USB_VENDOR_PLANEX2
, USB_PRODUCT_PLANEX2_GWUS54MINI2
},
106 { USB_VENDOR_PLANEX2
, USB_PRODUCT_PLANEX2_GWUSMM
},
107 { USB_VENDOR_QCOM
, USB_PRODUCT_QCOM_RT2573
},
108 { USB_VENDOR_QCOM
, USB_PRODUCT_QCOM_RT2573_2
},
109 { USB_VENDOR_RALINK
, USB_PRODUCT_RALINK_RT2573
},
110 { USB_VENDOR_RALINK
, USB_PRODUCT_RALINK_RT2671
},
111 { USB_VENDOR_RALINK
, USB_PRODUCT_RALINK_RT2573_2
},
112 { USB_VENDOR_SITECOMEU
, USB_PRODUCT_SITECOMEU_WL113R2
},
113 { USB_VENDOR_SITECOMEU
, USB_PRODUCT_SITECOMEU_WL172
},
114 { USB_VENDOR_SURECOM
, USB_PRODUCT_SURECOM_RT2573
}
117 static int rum_alloc_tx_list(struct rum_softc
*);
118 static void rum_free_tx_list(struct rum_softc
*);
119 static int rum_alloc_rx_list(struct rum_softc
*);
120 static void rum_free_rx_list(struct rum_softc
*);
121 static int rum_media_change(struct ifnet
*);
122 static void rum_next_scan(void *);
123 static void rum_task(void *);
124 static int rum_newstate(struct ieee80211com
*,
125 enum ieee80211_state
, int);
126 static void rum_txeof(usbd_xfer_handle
, usbd_private_handle
,
128 static void rum_rxeof(usbd_xfer_handle
, usbd_private_handle
,
130 static uint8_t rum_rxrate(struct rum_rx_desc
*);
131 static uint8_t rum_plcp_signal(int);
132 static void rum_setup_tx_desc(struct rum_softc
*,
133 struct rum_tx_desc
*, uint32_t, uint16_t, int,
135 static int rum_tx_data(struct rum_softc
*, struct mbuf
*,
136 struct ieee80211_node
*);
137 static void rum_start(struct ifnet
*);
138 static void rum_watchdog(struct ifnet
*);
139 static int rum_ioctl(struct ifnet
*, u_long
, caddr_t
,
141 static void rum_eeprom_read(struct rum_softc
*, uint16_t, void *,
143 static uint32_t rum_read(struct rum_softc
*, uint16_t);
144 static void rum_read_multi(struct rum_softc
*, uint16_t, void *,
146 static void rum_write(struct rum_softc
*, uint16_t, uint32_t);
147 static void rum_write_multi(struct rum_softc
*, uint16_t, void *,
149 static void rum_bbp_write(struct rum_softc
*, uint8_t, uint8_t);
150 static uint8_t rum_bbp_read(struct rum_softc
*, uint8_t);
151 static void rum_rf_write(struct rum_softc
*, uint8_t, uint32_t);
152 static void rum_select_antenna(struct rum_softc
*);
153 static void rum_enable_mrr(struct rum_softc
*);
154 static void rum_set_txpreamble(struct rum_softc
*);
155 static void rum_set_basicrates(struct rum_softc
*);
156 static void rum_select_band(struct rum_softc
*,
157 struct ieee80211_channel
*);
158 static void rum_set_chan(struct rum_softc
*,
159 struct ieee80211_channel
*);
160 static void rum_enable_tsf_sync(struct rum_softc
*);
161 static void rum_update_slot(struct rum_softc
*);
162 static void rum_set_bssid(struct rum_softc
*, const uint8_t *);
163 static void rum_set_macaddr(struct rum_softc
*, const uint8_t *);
164 static void rum_update_promisc(struct rum_softc
*);
165 static const char *rum_get_rf(int);
166 static void rum_read_eeprom(struct rum_softc
*);
167 static int rum_bbp_init(struct rum_softc
*);
168 static void rum_init(void *);
169 static void rum_stop(struct rum_softc
*);
170 static int rum_load_microcode(struct rum_softc
*, const uint8_t *,
172 static int rum_prepare_beacon(struct rum_softc
*);
174 static void rum_stats_timeout(void *);
175 static void rum_stats_update(usbd_xfer_handle
, usbd_private_handle
,
177 static void rum_stats(struct ieee80211com
*,
178 struct ieee80211_node
*,
179 struct ieee80211_ratectl_stats
*);
180 static void rum_ratectl_change(struct ieee80211com
*ic
, u_int
,
182 static int rum_get_rssi(struct rum_softc
*, uint8_t);
185 * Supported rates for 802.11a/b/g modes (in 500Kbps unit).
187 static const struct ieee80211_rateset rum_rateset_11a
=
188 { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
190 static const struct ieee80211_rateset rum_rateset_11b
=
191 { 4, { 2, 4, 11, 22 } };
193 static const struct ieee80211_rateset rum_rateset_11g
=
194 { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
196 static const struct {
203 static const struct {
210 static const struct rfprog
{
212 uint32_t r1
, r2
, r3
, r4
;
219 static device_probe_t rum_match
;
220 static device_attach_t rum_attach
;
221 static device_detach_t rum_detach
;
223 static devclass_t rum_devclass
;
225 static kobj_method_t rum_methods
[] = {
226 DEVMETHOD(device_probe
, rum_match
),
227 DEVMETHOD(device_attach
, rum_attach
),
228 DEVMETHOD(device_detach
, rum_detach
),
232 static driver_t rum_driver
= {
235 sizeof(struct rum_softc
)
238 MODULE_DEPEND(rum
, usb
, 1, 1, 1);
239 DRIVER_MODULE(rum
, uhub
, rum_driver
, rum_devclass
, usbd_driver_load
, 0);
242 rum_match(device_t self
)
244 struct usb_attach_arg
*uaa
= device_get_ivars(self
);
246 if (uaa
->iface
!= NULL
)
249 return (usb_lookup(rum_devs
, uaa
->vendor
, uaa
->product
) != NULL
) ?
250 UMATCH_VENDOR_PRODUCT
: UMATCH_NONE
;
254 rum_attach(device_t self
)
256 struct rum_softc
*sc
= device_get_softc(self
);
257 struct usb_attach_arg
*uaa
= device_get_ivars(self
);
258 struct ieee80211com
*ic
= &sc
->sc_ic
;
259 struct ifnet
*ifp
= &ic
->ic_if
;
260 usb_interface_descriptor_t
*id
;
261 usb_endpoint_descriptor_t
*ed
;
267 sc
->sc_udev
= uaa
->device
;
269 usbd_devinfo(uaa
->device
, 0, devinfo
);
271 device_set_desc_copy(self
, devinfo
);
273 if (usbd_set_config_no(sc
->sc_udev
, RT2573_CONFIG_NO
, 0) != 0) {
274 kprintf("%s: could not set configuration no\n",
275 device_get_nameunit(sc
->sc_dev
));
279 /* get the first interface handle */
280 error
= usbd_device2interface_handle(sc
->sc_udev
, RT2573_IFACE_INDEX
,
283 kprintf("%s: could not get interface handle\n",
284 device_get_nameunit(sc
->sc_dev
));
291 id
= usbd_get_interface_descriptor(sc
->sc_iface
);
293 sc
->sc_rx_no
= sc
->sc_tx_no
= -1;
294 for (i
= 0; i
< id
->bNumEndpoints
; i
++) {
295 ed
= usbd_interface2endpoint_descriptor(sc
->sc_iface
, i
);
297 kprintf("%s: no endpoint descriptor for iface %d\n",
298 device_get_nameunit(sc
->sc_dev
), i
);
302 if (UE_GET_DIR(ed
->bEndpointAddress
) == UE_DIR_IN
&&
303 UE_GET_XFERTYPE(ed
->bmAttributes
) == UE_BULK
)
304 sc
->sc_rx_no
= ed
->bEndpointAddress
;
305 else if (UE_GET_DIR(ed
->bEndpointAddress
) == UE_DIR_OUT
&&
306 UE_GET_XFERTYPE(ed
->bmAttributes
) == UE_BULK
)
307 sc
->sc_tx_no
= ed
->bEndpointAddress
;
309 if (sc
->sc_rx_no
== -1 || sc
->sc_tx_no
== -1) {
310 kprintf("%s: missing endpoint\n", device_get_nameunit(sc
->sc_dev
));
314 usb_init_task(&sc
->sc_task
, rum_task
, sc
);
316 callout_init(&sc
->scan_ch
);
317 callout_init(&sc
->stats_ch
);
319 /* retrieve RT2573 rev. no */
320 for (ntries
= 0; ntries
< 1000; ntries
++) {
321 if ((tmp
= rum_read(sc
, RT2573_MAC_CSR0
)) != 0)
325 if (ntries
== 1000) {
326 kprintf("%s: timeout waiting for chip to settle\n",
327 device_get_nameunit(sc
->sc_dev
));
331 /* retrieve MAC address and various other things from EEPROM */
334 kprintf("%s: MAC/BBP RT%04x (rev 0x%05x), RF %s, address %6D\n",
335 device_get_nameunit(sc
->sc_dev
), sc
->macbbp_rev
, tmp
,
336 rum_get_rf(sc
->rf_rev
), ic
->ic_myaddr
, ":");
338 error
= rum_load_microcode(sc
, rt2573
, sizeof(rt2573
));
340 device_printf(self
, "can't load microcode\n");
344 ic
->ic_phytype
= IEEE80211_T_OFDM
; /* not only, but not used */
345 ic
->ic_opmode
= IEEE80211_M_STA
; /* default to BSS mode */
346 ic
->ic_state
= IEEE80211_S_INIT
;
348 /* set device capabilities */
350 IEEE80211_C_IBSS
| /* IBSS mode supported */
351 IEEE80211_C_MONITOR
| /* monitor mode supported */
352 IEEE80211_C_HOSTAP
| /* HostAp mode supported */
353 IEEE80211_C_TXPMGT
| /* tx power management */
354 IEEE80211_C_SHPREAMBLE
| /* short preamble supported */
355 IEEE80211_C_SHSLOT
| /* short slot time supported */
356 IEEE80211_C_WPA
; /* WPA 1+2 */
358 if (sc
->rf_rev
== RT2573_RF_5225
|| sc
->rf_rev
== RT2573_RF_5226
) {
359 /* set supported .11a rates */
360 ic
->ic_sup_rates
[IEEE80211_MODE_11A
] = rum_rateset_11a
;
362 /* set supported .11a channels */
363 for (i
= 34; i
<= 46; i
+= 4) {
364 ic
->ic_channels
[i
].ic_freq
=
365 ieee80211_ieee2mhz(i
, IEEE80211_CHAN_5GHZ
);
366 ic
->ic_channels
[i
].ic_flags
= IEEE80211_CHAN_A
;
368 for (i
= 36; i
<= 64; i
+= 4) {
369 ic
->ic_channels
[i
].ic_freq
=
370 ieee80211_ieee2mhz(i
, IEEE80211_CHAN_5GHZ
);
371 ic
->ic_channels
[i
].ic_flags
= IEEE80211_CHAN_A
;
373 for (i
= 100; i
<= 140; i
+= 4) {
374 ic
->ic_channels
[i
].ic_freq
=
375 ieee80211_ieee2mhz(i
, IEEE80211_CHAN_5GHZ
);
376 ic
->ic_channels
[i
].ic_flags
= IEEE80211_CHAN_A
;
378 for (i
= 149; i
<= 165; i
+= 4) {
379 ic
->ic_channels
[i
].ic_freq
=
380 ieee80211_ieee2mhz(i
, IEEE80211_CHAN_5GHZ
);
381 ic
->ic_channels
[i
].ic_flags
= IEEE80211_CHAN_A
;
385 /* set supported .11b and .11g rates */
386 ic
->ic_sup_rates
[IEEE80211_MODE_11B
] = rum_rateset_11b
;
387 ic
->ic_sup_rates
[IEEE80211_MODE_11G
] = rum_rateset_11g
;
389 /* set supported .11b and .11g channels (1 through 14) */
390 for (i
= 1; i
<= 14; i
++) {
391 ic
->ic_channels
[i
].ic_freq
=
392 ieee80211_ieee2mhz(i
, IEEE80211_CHAN_2GHZ
);
393 ic
->ic_channels
[i
].ic_flags
=
394 IEEE80211_CHAN_CCK
| IEEE80211_CHAN_OFDM
|
395 IEEE80211_CHAN_DYN
| IEEE80211_CHAN_2GHZ
;
398 sc
->sc_sifs
= IEEE80211_DUR_SIFS
; /* Default SIFS */
400 if_initname(ifp
, device_get_name(self
), device_get_unit(self
));
402 ifp
->if_flags
= IFF_BROADCAST
| IFF_SIMPLEX
| IFF_MULTICAST
;
403 ifp
->if_init
= rum_init
;
404 ifp
->if_ioctl
= rum_ioctl
;
405 ifp
->if_start
= rum_start
;
406 ifp
->if_watchdog
= rum_watchdog
;
407 ifq_set_maxlen(&ifp
->if_snd
, IFQ_MAXLEN
);
408 ifq_set_ready(&ifp
->if_snd
);
410 ic
->ic_ratectl
.rc_st_ratectl_cap
= IEEE80211_RATECTL_CAP_ONOE
;
411 ic
->ic_ratectl
.rc_st_ratectl
= IEEE80211_RATECTL_ONOE
;
412 ic
->ic_ratectl
.rc_st_valid_stats
=
413 IEEE80211_RATECTL_STATS_PKT_NORETRY
|
414 IEEE80211_RATECTL_STATS_PKT_OK
|
415 IEEE80211_RATECTL_STATS_PKT_ERR
|
416 IEEE80211_RATECTL_STATS_RETRIES
;
417 ic
->ic_ratectl
.rc_st_stats
= rum_stats
;
418 ic
->ic_ratectl
.rc_st_change
= rum_ratectl_change
;
420 ieee80211_ifattach(ic
);
422 /* Enable software beacon missing handling. */
423 ic
->ic_flags_ext
|= IEEE80211_FEXT_SWBMISS
;
425 /* override state transition machine */
426 sc
->sc_newstate
= ic
->ic_newstate
;
427 ic
->ic_newstate
= rum_newstate
;
428 ieee80211_media_init(ic
, rum_media_change
, ieee80211_media_status
);
430 bpfattach_dlt(ifp
, DLT_IEEE802_11_RADIO
,
431 sizeof(struct ieee80211_frame
) + IEEE80211_RADIOTAP_HDRLEN
,
434 sc
->sc_rxtap_len
= sizeof sc
->sc_rxtapu
;
435 sc
->sc_rxtap
.wr_ihdr
.it_len
= htole16(sc
->sc_rxtap_len
);
436 sc
->sc_rxtap
.wr_ihdr
.it_present
= htole32(RT2573_RX_RADIOTAP_PRESENT
);
438 sc
->sc_txtap_len
= sizeof sc
->sc_txtapu
;
439 sc
->sc_txtap
.wt_ihdr
.it_len
= htole16(sc
->sc_txtap_len
);
440 sc
->sc_txtap
.wt_ihdr
.it_present
= htole32(RT2573_TX_RADIOTAP_PRESENT
);
443 ieee80211_announce(ic
);
449 rum_detach(device_t self
)
451 struct rum_softc
*sc
= device_get_softc(self
);
452 struct ifnet
*ifp
= &sc
->sc_ic
.ic_if
;
459 callout_stop(&sc
->scan_ch
);
460 callout_stop(&sc
->stats_ch
);
462 lwkt_serialize_enter(ifp
->if_serializer
);
464 lwkt_serialize_exit(ifp
->if_serializer
);
466 usb_rem_task(sc
->sc_udev
, &sc
->sc_task
);
469 ieee80211_ifdetach(&sc
->sc_ic
); /* free all nodes */
473 KKASSERT(sc
->stats_xfer
== NULL
);
474 KKASSERT(sc
->sc_rx_pipeh
== NULL
);
475 KKASSERT(sc
->sc_tx_pipeh
== NULL
);
479 * Make sure TX/RX list is empty
481 for (i
= 0; i
< RT2573_TX_LIST_COUNT
; i
++) {
482 struct rum_tx_data
*data
= &sc
->tx_data
[i
];
484 KKASSERT(data
->xfer
== NULL
);
485 KKASSERT(data
->ni
== NULL
);
486 KKASSERT(data
->m
== NULL
);
488 for (i
= 0; i
< RT2573_RX_LIST_COUNT
; i
++) {
489 struct rum_rx_data
*data
= &sc
->rx_data
[i
];
491 KKASSERT(data
->xfer
== NULL
);
492 KKASSERT(data
->m
== NULL
);
499 rum_alloc_tx_list(struct rum_softc
*sc
)
504 for (i
= 0; i
< RT2573_TX_LIST_COUNT
; i
++) {
505 struct rum_tx_data
*data
= &sc
->tx_data
[i
];
509 data
->xfer
= usbd_alloc_xfer(sc
->sc_udev
);
510 if (data
->xfer
== NULL
) {
511 kprintf("%s: could not allocate tx xfer\n",
512 device_get_nameunit(sc
->sc_dev
));
516 data
->buf
= usbd_alloc_buffer(data
->xfer
,
517 RT2573_TX_DESC_SIZE
+ IEEE80211_MAX_LEN
);
518 if (data
->buf
== NULL
) {
519 kprintf("%s: could not allocate tx buffer\n",
520 device_get_nameunit(sc
->sc_dev
));
524 /* clean Tx descriptor */
525 bzero(data
->buf
, RT2573_TX_DESC_SIZE
);
531 rum_free_tx_list(struct rum_softc
*sc
)
535 for (i
= 0; i
< RT2573_TX_LIST_COUNT
; i
++) {
536 struct rum_tx_data
*data
= &sc
->tx_data
[i
];
538 if (data
->xfer
!= NULL
) {
539 usbd_free_xfer(data
->xfer
);
542 if (data
->ni
!= NULL
) {
543 ieee80211_free_node(data
->ni
);
546 if (data
->m
!= NULL
) {
555 rum_alloc_rx_list(struct rum_softc
*sc
)
559 for (i
= 0; i
< RT2573_RX_LIST_COUNT
; i
++) {
560 struct rum_rx_data
*data
= &sc
->rx_data
[i
];
564 data
->xfer
= usbd_alloc_xfer(sc
->sc_udev
);
565 if (data
->xfer
== NULL
) {
566 kprintf("%s: could not allocate rx xfer\n",
567 device_get_nameunit(sc
->sc_dev
));
571 if (usbd_alloc_buffer(data
->xfer
, MCLBYTES
) == NULL
) {
572 kprintf("%s: could not allocate rx buffer\n",
573 device_get_nameunit(sc
->sc_dev
));
577 data
->m
= m_getcl(MB_WAIT
, MT_DATA
, M_PKTHDR
);
579 data
->buf
= mtod(data
->m
, uint8_t *);
580 bzero(data
->buf
, sizeof(struct rum_rx_desc
));
586 rum_free_rx_list(struct rum_softc
*sc
)
590 for (i
= 0; i
< RT2573_RX_LIST_COUNT
; i
++) {
591 struct rum_rx_data
*data
= &sc
->rx_data
[i
];
593 if (data
->xfer
!= NULL
) {
594 usbd_free_xfer(data
->xfer
);
597 if (data
->m
!= NULL
) {
605 rum_media_change(struct ifnet
*ifp
)
609 error
= ieee80211_media_change(ifp
);
610 if (error
!= ENETRESET
)
613 if ((ifp
->if_flags
& (IFF_UP
| IFF_RUNNING
)) == (IFF_UP
| IFF_RUNNING
))
614 rum_init(ifp
->if_softc
);
620 * This function is called periodically (every 200ms) during scanning to
621 * switch from one channel to another.
624 rum_next_scan(void *arg
)
626 struct rum_softc
*sc
= arg
;
627 struct ieee80211com
*ic
= &sc
->sc_ic
;
628 struct ifnet
*ifp
= &ic
->ic_if
;
635 if (ic
->ic_state
== IEEE80211_S_SCAN
) {
636 lwkt_serialize_enter(ifp
->if_serializer
);
637 ieee80211_next_scan(ic
);
638 lwkt_serialize_exit(ifp
->if_serializer
);
647 struct rum_softc
*sc
= xarg
;
648 struct ieee80211com
*ic
= &sc
->sc_ic
;
649 struct ifnet
*ifp
= &ic
->ic_if
;
650 enum ieee80211_state nstate
;
651 struct ieee80211_node
*ni
;
659 nstate
= sc
->sc_state
;
662 KASSERT(nstate
!= IEEE80211_S_INIT
,
663 ("->INIT state transition should not be defered\n"));
664 rum_set_chan(sc
, ic
->ic_curchan
);
667 case IEEE80211_S_RUN
:
670 if (ic
->ic_opmode
!= IEEE80211_M_MONITOR
) {
673 rum_set_txpreamble(sc
);
674 rum_set_basicrates(sc
);
675 rum_set_bssid(sc
, ni
->ni_bssid
);
678 if (ic
->ic_opmode
== IEEE80211_M_HOSTAP
||
679 ic
->ic_opmode
== IEEE80211_M_IBSS
)
680 rum_prepare_beacon(sc
);
682 if (ic
->ic_opmode
!= IEEE80211_M_MONITOR
)
683 rum_enable_tsf_sync(sc
);
685 /* clear statistic registers (STA_CSR0 to STA_CSR5) */
686 rum_read_multi(sc
, RT2573_STA_CSR0
, sc
->sta
, sizeof(sc
->sta
));
687 callout_reset(&sc
->stats_ch
, 4 * hz
/ 5, rum_stats_timeout
, sc
);
690 case IEEE80211_S_SCAN
:
691 callout_reset(&sc
->scan_ch
, hz
/ 5, rum_next_scan
, sc
);
698 lwkt_serialize_enter(ifp
->if_serializer
);
699 ieee80211_ratectl_newstate(ic
, nstate
);
700 sc
->sc_newstate(ic
, nstate
, arg
);
701 lwkt_serialize_exit(ifp
->if_serializer
);
707 rum_newstate(struct ieee80211com
*ic
, enum ieee80211_state nstate
, int arg
)
709 struct rum_softc
*sc
= ic
->ic_if
.if_softc
;
710 struct ifnet
*ifp
= &ic
->ic_if
;
714 ASSERT_SERIALIZED(ifp
->if_serializer
);
716 callout_stop(&sc
->scan_ch
);
717 callout_stop(&sc
->stats_ch
);
719 /* do it in a process context */
720 sc
->sc_state
= nstate
;
723 lwkt_serialize_exit(ifp
->if_serializer
);
724 usb_rem_task(sc
->sc_udev
, &sc
->sc_task
);
726 if (nstate
== IEEE80211_S_INIT
) {
727 lwkt_serialize_enter(ifp
->if_serializer
);
728 ieee80211_ratectl_newstate(ic
, nstate
);
729 sc
->sc_newstate(ic
, nstate
, arg
);
731 usb_add_task(sc
->sc_udev
, &sc
->sc_task
, USB_TASKQ_DRIVER
);
732 lwkt_serialize_enter(ifp
->if_serializer
);
739 /* quickly determine if a given rate is CCK or OFDM */
740 #define RUM_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
742 #define RUM_ACK_SIZE (sizeof(struct ieee80211_frame_ack) + IEEE80211_FCS_LEN)
745 rum_txeof(usbd_xfer_handle xfer
, usbd_private_handle priv
, usbd_status status
)
747 struct rum_tx_data
*data
= priv
;
748 struct rum_softc
*sc
= data
->sc
;
749 struct ieee80211com
*ic
= &sc
->sc_ic
;
750 struct ifnet
*ifp
= &ic
->ic_if
;
751 struct ieee80211_node
*ni
;
758 if (status
!= USBD_NORMAL_COMPLETION
) {
759 if (status
== USBD_NOT_STARTED
|| status
== USBD_CANCELLED
) {
764 kprintf("%s: could not transmit buffer: %s\n",
765 device_get_nameunit(sc
->sc_dev
), usbd_errstr(status
));
767 if (status
== USBD_STALLED
)
768 usbd_clear_endpoint_stall_async(sc
->sc_tx_pipeh
);
780 bzero(data
->buf
, sizeof(struct rum_tx_data
));
782 ifp
->if_opackets
++; /* XXX may fail too */
784 DPRINTFN(10, ("tx done\n"));
787 ifp
->if_flags
&= ~IFF_OACTIVE
;
789 lwkt_serialize_enter(ifp
->if_serializer
);
790 ieee80211_free_node(ni
);
792 lwkt_serialize_exit(ifp
->if_serializer
);
798 rum_rxeof(usbd_xfer_handle xfer
, usbd_private_handle priv
, usbd_status status
)
800 struct rum_rx_data
*data
= priv
;
801 struct rum_softc
*sc
= data
->sc
;
802 struct ieee80211com
*ic
= &sc
->sc_ic
;
803 struct ifnet
*ifp
= &ic
->ic_if
;
804 struct rum_rx_desc
*desc
;
805 struct ieee80211_frame_min
*wh
;
806 struct ieee80211_node
*ni
;
807 struct mbuf
*mnew
, *m
;
815 if (status
!= USBD_NORMAL_COMPLETION
) {
816 if (status
== USBD_NOT_STARTED
|| status
== USBD_CANCELLED
) {
821 if (status
== USBD_STALLED
)
822 usbd_clear_endpoint_stall_async(sc
->sc_rx_pipeh
);
826 usbd_get_xfer_status(xfer
, NULL
, NULL
, &len
, NULL
);
828 if (len
< RT2573_RX_DESC_SIZE
+ sizeof(struct ieee80211_frame_min
)) {
829 DPRINTF(("%s: xfer too short %d\n", device_get_nameunit(sc
->sc_dev
),
835 desc
= (struct rum_rx_desc
*)data
->buf
;
837 if (le32toh(desc
->flags
) & RT2573_RX_CRC_ERROR
) {
839 * This should not happen since we did not request to receive
840 * those frames when we filled RT2573_TXRX_CSR0.
842 DPRINTFN(5, ("CRC error\n"));
847 mnew
= m_getcl(MB_DONTWAIT
, MT_DATA
, M_PKTHDR
);
849 kprintf("%s: could not allocate rx mbuf\n",
850 device_get_nameunit(sc
->sc_dev
));
859 lwkt_serialize_enter(ifp
->if_serializer
);
862 m
->m_pkthdr
.rcvif
= ifp
;
863 m
->m_data
= (caddr_t
)(desc
+ 1);
864 m
->m_pkthdr
.len
= m
->m_len
= (le32toh(desc
->flags
) >> 16) & 0xfff;
866 rssi
= rum_get_rssi(sc
, desc
->rssi
);
868 wh
= mtod(m
, struct ieee80211_frame_min
*);
869 ni
= ieee80211_find_rxnode(ic
, wh
);
871 /* Error happened during RSSI conversion. */
875 if (sc
->sc_drvbpf
!= NULL
) {
876 struct rum_rx_radiotap_header
*tap
= &sc
->sc_rxtap
;
879 tap
->wr_rate
= rum_rxrate(desc
);
880 tap
->wr_chan_freq
= htole16(ic
->ic_bss
->ni_chan
->ic_freq
);
881 tap
->wr_chan_flags
= htole16(ic
->ic_bss
->ni_chan
->ic_flags
);
882 tap
->wr_antenna
= sc
->rx_ant
;
883 tap
->wr_antsignal
= rssi
;
885 bpf_ptap(sc
->sc_drvbpf
, m
, tap
, sc
->sc_rxtap_len
);
888 /* send the frame to the 802.11 layer */
889 ieee80211_input(ic
, m
, ni
, rssi
, 0);
891 /* node is no longer needed */
892 ieee80211_free_node(ni
);
894 if ((ifp
->if_flags
& IFF_OACTIVE
) == 0)
897 lwkt_serialize_exit(ifp
->if_serializer
);
900 data
->buf
= mtod(data
->m
, uint8_t *);
902 DPRINTFN(15, ("rx done\n"));
904 skip
: /* setup a new transfer */
905 bzero(data
->buf
, sizeof(struct rum_rx_desc
));
906 usbd_setup_xfer(xfer
, sc
->sc_rx_pipeh
, data
, data
->buf
, MCLBYTES
,
907 USBD_SHORT_XFER_OK
, USBD_NO_TIMEOUT
, rum_rxeof
);
914 * This function is only used by the Rx radiotap code. It returns the rate at
915 * which a given frame was received.
918 rum_rxrate(struct rum_rx_desc
*desc
)
920 if (le32toh(desc
->flags
) & RT2573_RX_OFDM
) {
921 /* reverse function of rum_plcp_signal */
922 switch (desc
->rate
) {
930 case 0xc: return 108;
933 if (desc
->rate
== 10)
935 if (desc
->rate
== 20)
937 if (desc
->rate
== 55)
939 if (desc
->rate
== 110)
942 return 2; /* should not get there */
946 rum_plcp_signal(int rate
)
949 /* CCK rates (returned values are device-dependent) */
955 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
963 case 108: return 0xc;
965 /* unsupported rates (should not get there) */
966 default: return 0xff;
971 rum_setup_tx_desc(struct rum_softc
*sc
, struct rum_tx_desc
*desc
,
972 uint32_t flags
, uint16_t xflags
, int len
, int rate
)
974 struct ieee80211com
*ic
= &sc
->sc_ic
;
975 uint16_t plcp_length
;
978 desc
->flags
= htole32(flags
);
979 desc
->flags
|= htole32(len
<< 16);
981 desc
->xflags
= htole16(xflags
);
987 RT2573_LOGCWMAX(10));
989 /* setup PLCP fields */
990 desc
->plcp_signal
= rum_plcp_signal(rate
);
991 desc
->plcp_service
= 4;
993 len
+= IEEE80211_CRC_LEN
;
994 if (RUM_RATE_IS_OFDM(rate
)) {
995 desc
->flags
|= htole32(RT2573_TX_OFDM
);
997 plcp_length
= len
& 0xfff;
998 desc
->plcp_length_hi
= plcp_length
>> 6;
999 desc
->plcp_length_lo
= plcp_length
& 0x3f;
1001 plcp_length
= (16 * len
+ rate
- 1) / rate
;
1003 remainder
= (16 * len
) % 22;
1004 if (remainder
!= 0 && remainder
< 7)
1005 desc
->plcp_service
|= RT2573_PLCP_LENGEXT
;
1007 desc
->plcp_length_hi
= plcp_length
>> 8;
1008 desc
->plcp_length_lo
= plcp_length
& 0xff;
1010 if (rate
!= 2 && (ic
->ic_flags
& IEEE80211_F_SHPREAMBLE
))
1011 desc
->plcp_signal
|= 0x08;
1013 desc
->flags
|= htole32(RT2573_TX_VALID
);
1016 #define RUM_TX_TIMEOUT 5000
1019 rum_tx_data(struct rum_softc
*sc
, struct mbuf
*m0
, struct ieee80211_node
*ni
)
1021 struct ieee80211com
*ic
= &sc
->sc_ic
;
1022 struct ifnet
*ifp
= &ic
->ic_if
;
1023 struct rum_tx_desc
*desc
;
1024 struct rum_tx_data
*data
;
1025 struct ieee80211_frame
*wh
;
1029 int xferlen
, rate
, rateidx
;
1031 wh
= mtod(m0
, struct ieee80211_frame
*);
1033 if (wh
->i_fc
[1] & IEEE80211_FC1_WEP
) {
1034 if (ieee80211_crypto_encap(ic
, ni
, m0
) == NULL
) {
1039 /* packet header may have moved, reset our local pointer */
1040 wh
= mtod(m0
, struct ieee80211_frame
*);
1044 if ((wh
->i_fc
[0] & IEEE80211_FC0_TYPE_MASK
) ==
1045 IEEE80211_FC0_TYPE_MGT
) {
1046 /* mgmt frames are sent at the lowest available bit-rate */
1049 ieee80211_ratectl_findrate(ni
, m0
->m_pkthdr
.len
, &rateidx
, 1);
1051 rate
= IEEE80211_RS_RATE(&ni
->ni_rates
, rateidx
);
1053 data
= &sc
->tx_data
[0];
1054 desc
= (struct rum_tx_desc
*)data
->buf
;
1059 if (!IEEE80211_IS_MULTICAST(wh
->i_addr1
)) {
1060 flags
|= RT2573_TX_ACK
;
1062 dur
= ieee80211_txtime(ni
, RUM_ACK_SIZE
,
1063 ieee80211_ack_rate(ni
, rate
), ic
->ic_flags
) +
1065 *(uint16_t *)wh
->i_dur
= htole16(dur
);
1067 /* tell hardware to set timestamp in probe responses */
1069 (IEEE80211_FC0_TYPE_MASK
| IEEE80211_FC0_SUBTYPE_MASK
)) ==
1070 (IEEE80211_FC0_TYPE_MGT
| IEEE80211_FC0_SUBTYPE_PROBE_RESP
))
1071 flags
|= RT2573_TX_TIMESTAMP
;
1074 if (sc
->sc_drvbpf
!= NULL
) {
1075 struct rum_tx_radiotap_header
*tap
= &sc
->sc_txtap
;
1078 tap
->wt_rate
= rate
;
1079 tap
->wt_chan_freq
= htole16(ic
->ic_bss
->ni_chan
->ic_freq
);
1080 tap
->wt_chan_flags
= htole16(ic
->ic_bss
->ni_chan
->ic_flags
);
1081 tap
->wt_antenna
= sc
->tx_ant
;
1083 bpf_ptap(sc
->sc_drvbpf
, m0
, tap
, sc
->sc_txtap_len
);
1086 m_copydata(m0
, 0, m0
->m_pkthdr
.len
, data
->buf
+ RT2573_TX_DESC_SIZE
);
1087 rum_setup_tx_desc(sc
, desc
, flags
, 0, m0
->m_pkthdr
.len
, rate
);
1089 /* Align end on a 4-bytes boundary */
1090 xferlen
= roundup(RT2573_TX_DESC_SIZE
+ m0
->m_pkthdr
.len
, 4);
1093 * No space left in the last URB to store the extra 4 bytes, force
1094 * sending of another URB.
1096 if ((xferlen
% 64) == 0)
1099 DPRINTFN(10, ("sending frame len=%u rate=%u xfer len=%u\n",
1100 m0
->m_pkthdr
.len
+ RT2573_TX_DESC_SIZE
, rate
, xferlen
));
1102 lwkt_serialize_exit(ifp
->if_serializer
);
1104 usbd_setup_xfer(data
->xfer
, sc
->sc_tx_pipeh
, data
, data
->buf
, xferlen
,
1105 USBD_FORCE_SHORT_XFER
| USBD_NO_COPY
, RUM_TX_TIMEOUT
, rum_txeof
);
1107 error
= usbd_transfer(data
->xfer
);
1108 if (error
!= USBD_NORMAL_COMPLETION
&& error
!= USBD_IN_PROGRESS
) {
1117 lwkt_serialize_enter(ifp
->if_serializer
);
1122 rum_start(struct ifnet
*ifp
)
1124 struct rum_softc
*sc
= ifp
->if_softc
;
1125 struct ieee80211com
*ic
= &sc
->sc_ic
;
1127 ASSERT_SERIALIZED(ifp
->if_serializer
);
1134 if ((ifp
->if_flags
& (IFF_RUNNING
| IFF_OACTIVE
)) != IFF_RUNNING
) {
1140 struct ieee80211_node
*ni
;
1143 if (!IF_QEMPTY(&ic
->ic_mgtq
)) {
1144 if (sc
->tx_queued
>= RT2573_TX_LIST_COUNT
) {
1145 ifp
->if_flags
|= IFF_OACTIVE
;
1148 IF_DEQUEUE(&ic
->ic_mgtq
, m0
);
1150 ni
= (struct ieee80211_node
*)m0
->m_pkthdr
.rcvif
;
1151 m0
->m_pkthdr
.rcvif
= NULL
;
1155 if (rum_tx_data(sc
, m0
, ni
) != 0) {
1156 ieee80211_free_node(ni
);
1160 struct ether_header
*eh
;
1162 if (ic
->ic_state
!= IEEE80211_S_RUN
)
1165 m0
= ifq_poll(&ifp
->if_snd
);
1168 if (sc
->tx_queued
>= RT2573_TX_LIST_COUNT
) {
1169 ifp
->if_flags
|= IFF_OACTIVE
;
1172 ifq_dequeue(&ifp
->if_snd
, m0
);
1174 if (m0
->m_len
< sizeof(struct ether_header
)) {
1175 m0
= m_pullup(m0
, sizeof(struct ether_header
));
1181 eh
= mtod(m0
, struct ether_header
*);
1183 ni
= ieee80211_find_txnode(ic
, eh
->ether_dhost
);
1191 m0
= ieee80211_encap(ic
, m0
, ni
);
1193 ieee80211_free_node(ni
);
1197 if (ic
->ic_rawbpf
!= NULL
)
1198 bpf_mtap(ic
->ic_rawbpf
, m0
);
1200 if (rum_tx_data(sc
, m0
, ni
) != 0) {
1201 ieee80211_free_node(ni
);
1207 sc
->sc_tx_timer
= 5;
1215 rum_watchdog(struct ifnet
*ifp
)
1217 struct rum_softc
*sc
= ifp
->if_softc
;
1219 ASSERT_SERIALIZED(ifp
->if_serializer
);
1225 if (sc
->sc_tx_timer
> 0) {
1226 if (--sc
->sc_tx_timer
== 0) {
1227 kprintf("%s: device timeout\n", device_get_nameunit(sc
->sc_dev
));
1228 /*rum_init(sc); XXX needs a process context! */
1237 ieee80211_watchdog(&sc
->sc_ic
);
1243 rum_ioctl(struct ifnet
*ifp
, u_long cmd
, caddr_t data
, struct ucred
*cr
)
1245 struct rum_softc
*sc
= ifp
->if_softc
;
1246 struct ieee80211com
*ic
= &sc
->sc_ic
;
1249 ASSERT_SERIALIZED(ifp
->if_serializer
);
1255 if (ifp
->if_flags
& IFF_UP
) {
1256 if (ifp
->if_flags
& IFF_RUNNING
) {
1257 lwkt_serialize_exit(ifp
->if_serializer
);
1258 rum_update_promisc(sc
);
1259 lwkt_serialize_enter(ifp
->if_serializer
);
1264 if (ifp
->if_flags
& IFF_RUNNING
)
1269 error
= ieee80211_ioctl(ic
, cmd
, data
, cr
);
1273 if (error
== ENETRESET
) {
1274 struct ieee80211req
*ireq
= (struct ieee80211req
*)data
;
1276 if (cmd
== SIOCS80211
&&
1277 ireq
->i_type
== IEEE80211_IOC_CHANNEL
&&
1278 ic
->ic_opmode
== IEEE80211_M_MONITOR
) {
1280 * This allows for fast channel switching in monitor
1281 * mode (used by kismet). In IBSS mode, we must
1282 * explicitly reset the interface to generate a new
1285 lwkt_serialize_exit(ifp
->if_serializer
);
1286 rum_set_chan(sc
, ic
->ic_ibss_chan
);
1287 lwkt_serialize_enter(ifp
->if_serializer
);
1288 } else if ((ifp
->if_flags
& (IFF_UP
| IFF_RUNNING
)) ==
1289 (IFF_UP
| IFF_RUNNING
)) {
1300 rum_eeprom_read(struct rum_softc
*sc
, uint16_t addr
, void *buf
, int len
)
1302 usb_device_request_t req
;
1305 req
.bmRequestType
= UT_READ_VENDOR_DEVICE
;
1306 req
.bRequest
= RT2573_READ_EEPROM
;
1307 USETW(req
.wValue
, 0);
1308 USETW(req
.wIndex
, addr
);
1309 USETW(req
.wLength
, len
);
1311 error
= usbd_do_request(sc
->sc_udev
, &req
, buf
);
1313 kprintf("%s: could not read EEPROM: %s\n",
1314 device_get_nameunit(sc
->sc_dev
), usbd_errstr(error
));
1319 rum_read(struct rum_softc
*sc
, uint16_t reg
)
1323 rum_read_multi(sc
, reg
, &val
, sizeof val
);
1325 return le32toh(val
);
1329 rum_read_multi(struct rum_softc
*sc
, uint16_t reg
, void *buf
, int len
)
1331 usb_device_request_t req
;
1334 req
.bmRequestType
= UT_READ_VENDOR_DEVICE
;
1335 req
.bRequest
= RT2573_READ_MULTI_MAC
;
1336 USETW(req
.wValue
, 0);
1337 USETW(req
.wIndex
, reg
);
1338 USETW(req
.wLength
, len
);
1340 error
= usbd_do_request(sc
->sc_udev
, &req
, buf
);
1342 kprintf("%s: could not multi read MAC register: %s\n",
1343 device_get_nameunit(sc
->sc_dev
), usbd_errstr(error
));
1348 rum_write(struct rum_softc
*sc
, uint16_t reg
, uint32_t val
)
1350 uint32_t tmp
= htole32(val
);
1352 rum_write_multi(sc
, reg
, &tmp
, sizeof tmp
);
1356 rum_write_multi(struct rum_softc
*sc
, uint16_t reg
, void *buf
, size_t len
)
1358 usb_device_request_t req
;
1361 req
.bmRequestType
= UT_WRITE_VENDOR_DEVICE
;
1362 req
.bRequest
= RT2573_WRITE_MULTI_MAC
;
1363 USETW(req
.wValue
, 0);
1364 USETW(req
.wIndex
, reg
);
1365 USETW(req
.wLength
, len
);
1367 error
= usbd_do_request(sc
->sc_udev
, &req
, buf
);
1369 kprintf("%s: could not multi write MAC register: %s\n",
1370 device_get_nameunit(sc
->sc_dev
), usbd_errstr(error
));
1375 rum_bbp_write(struct rum_softc
*sc
, uint8_t reg
, uint8_t val
)
1380 for (ntries
= 0; ntries
< 5; ntries
++) {
1381 if (!(rum_read(sc
, RT2573_PHY_CSR3
) & RT2573_BBP_BUSY
))
1385 kprintf("%s: could not write to BBP\n", device_get_nameunit(sc
->sc_dev
));
1389 tmp
= RT2573_BBP_BUSY
| (reg
& 0x7f) << 8 | val
;
1390 rum_write(sc
, RT2573_PHY_CSR3
, tmp
);
1394 rum_bbp_read(struct rum_softc
*sc
, uint8_t reg
)
1399 for (ntries
= 0; ntries
< 5; ntries
++) {
1400 if (!(rum_read(sc
, RT2573_PHY_CSR3
) & RT2573_BBP_BUSY
))
1404 kprintf("%s: could not read BBP\n", device_get_nameunit(sc
->sc_dev
));
1408 val
= RT2573_BBP_BUSY
| RT2573_BBP_READ
| reg
<< 8;
1409 rum_write(sc
, RT2573_PHY_CSR3
, val
);
1411 for (ntries
= 0; ntries
< 100; ntries
++) {
1412 val
= rum_read(sc
, RT2573_PHY_CSR3
);
1413 if (!(val
& RT2573_BBP_BUSY
))
1418 kprintf("%s: could not read BBP\n", device_get_nameunit(sc
->sc_dev
));
1423 rum_rf_write(struct rum_softc
*sc
, uint8_t reg
, uint32_t val
)
1428 for (ntries
= 0; ntries
< 5; ntries
++) {
1429 if (!(rum_read(sc
, RT2573_PHY_CSR4
) & RT2573_RF_BUSY
))
1433 kprintf("%s: could not write to RF\n", device_get_nameunit(sc
->sc_dev
));
1437 tmp
= RT2573_RF_BUSY
| RT2573_RF_20BIT
| (val
& 0xfffff) << 2 |
1439 rum_write(sc
, RT2573_PHY_CSR4
, tmp
);
1441 /* remember last written value in sc */
1442 sc
->rf_regs
[reg
] = val
;
1444 DPRINTFN(15, ("RF R[%u] <- 0x%05x\n", reg
& 3, val
& 0xfffff));
1448 rum_select_antenna(struct rum_softc
*sc
)
1450 uint8_t bbp4
, bbp77
;
1453 bbp4
= rum_bbp_read(sc
, 4);
1454 bbp77
= rum_bbp_read(sc
, 77);
1458 /* make sure Rx is disabled before switching antenna */
1459 tmp
= rum_read(sc
, RT2573_TXRX_CSR0
);
1460 rum_write(sc
, RT2573_TXRX_CSR0
, tmp
| RT2573_DISABLE_RX
);
1462 rum_bbp_write(sc
, 4, bbp4
);
1463 rum_bbp_write(sc
, 77, bbp77
);
1465 rum_write(sc
, RT2573_TXRX_CSR0
, tmp
);
1469 * Enable multi-rate retries for frames sent at OFDM rates.
1470 * In 802.11b/g mode, allow fallback to CCK rates.
1473 rum_enable_mrr(struct rum_softc
*sc
)
1475 struct ieee80211com
*ic
= &sc
->sc_ic
;
1478 tmp
= rum_read(sc
, RT2573_TXRX_CSR4
);
1480 tmp
&= ~RT2573_MRR_CCK_FALLBACK
;
1481 if (!IEEE80211_IS_CHAN_5GHZ(ic
->ic_curchan
))
1482 tmp
|= RT2573_MRR_CCK_FALLBACK
;
1483 tmp
|= RT2573_MRR_ENABLED
;
1485 rum_write(sc
, RT2573_TXRX_CSR4
, tmp
);
1489 rum_set_txpreamble(struct rum_softc
*sc
)
1493 tmp
= rum_read(sc
, RT2573_TXRX_CSR4
);
1495 tmp
&= ~RT2573_SHORT_PREAMBLE
;
1496 if (sc
->sc_ic
.ic_flags
& IEEE80211_F_SHPREAMBLE
)
1497 tmp
|= RT2573_SHORT_PREAMBLE
;
1499 rum_write(sc
, RT2573_TXRX_CSR4
, tmp
);
1503 rum_set_basicrates(struct rum_softc
*sc
)
1505 struct ieee80211com
*ic
= &sc
->sc_ic
;
1507 /* update basic rate set */
1508 if (ic
->ic_curmode
== IEEE80211_MODE_11B
) {
1509 /* 11b basic rates: 1, 2Mbps */
1510 rum_write(sc
, RT2573_TXRX_CSR5
, 0x3);
1511 } else if (IEEE80211_IS_CHAN_5GHZ(ic
->ic_bss
->ni_chan
)) {
1512 /* 11a basic rates: 6, 12, 24Mbps */
1513 rum_write(sc
, RT2573_TXRX_CSR5
, 0x150);
1515 /* 11g basic rates: 1, 2, 5.5, 11, 6, 12, 24Mbps */
1516 rum_write(sc
, RT2573_TXRX_CSR5
, 0x15f);
1521 * Reprogram MAC/BBP to switch to a new band. Values taken from the reference
1525 rum_select_band(struct rum_softc
*sc
, struct ieee80211_channel
*c
)
1527 uint8_t bbp17
, bbp35
, bbp96
, bbp97
, bbp98
, bbp104
;
1530 /* update all BBP registers that depend on the band */
1531 bbp17
= 0x20; bbp96
= 0x48; bbp104
= 0x2c;
1532 bbp35
= 0x50; bbp97
= 0x48; bbp98
= 0x48;
1533 if (IEEE80211_IS_CHAN_5GHZ(c
)) {
1534 bbp17
+= 0x08; bbp96
+= 0x10; bbp104
+= 0x0c;
1535 bbp35
+= 0x10; bbp97
+= 0x10; bbp98
+= 0x10;
1537 if ((IEEE80211_IS_CHAN_2GHZ(c
) && sc
->ext_2ghz_lna
) ||
1538 (IEEE80211_IS_CHAN_5GHZ(c
) && sc
->ext_5ghz_lna
)) {
1539 bbp17
+= 0x10; bbp96
+= 0x10; bbp104
+= 0x10;
1543 rum_bbp_write(sc
, 17, bbp17
);
1544 rum_bbp_write(sc
, 96, bbp96
);
1545 rum_bbp_write(sc
, 104, bbp104
);
1547 if ((IEEE80211_IS_CHAN_2GHZ(c
) && sc
->ext_2ghz_lna
) ||
1548 (IEEE80211_IS_CHAN_5GHZ(c
) && sc
->ext_5ghz_lna
)) {
1549 rum_bbp_write(sc
, 75, 0x80);
1550 rum_bbp_write(sc
, 86, 0x80);
1551 rum_bbp_write(sc
, 88, 0x80);
1554 rum_bbp_write(sc
, 35, bbp35
);
1555 rum_bbp_write(sc
, 97, bbp97
);
1556 rum_bbp_write(sc
, 98, bbp98
);
1558 tmp
= rum_read(sc
, RT2573_PHY_CSR0
);
1559 tmp
&= ~(RT2573_PA_PE_2GHZ
| RT2573_PA_PE_5GHZ
);
1560 if (IEEE80211_IS_CHAN_2GHZ(c
))
1561 tmp
|= RT2573_PA_PE_2GHZ
;
1563 tmp
|= RT2573_PA_PE_5GHZ
;
1564 rum_write(sc
, RT2573_PHY_CSR0
, tmp
);
1568 rum_set_chan(struct rum_softc
*sc
, struct ieee80211_channel
*c
)
1570 struct ieee80211com
*ic
= &sc
->sc_ic
;
1571 const struct rfprog
*rfprog
;
1572 uint8_t bbp3
, bbp94
= RT2573_BBPR94_DEFAULT
;
1576 chan
= ieee80211_chan2ieee(ic
, c
);
1577 if (chan
== 0 || chan
== IEEE80211_CHAN_ANY
)
1580 /* select the appropriate RF settings based on what EEPROM says */
1581 rfprog
= (sc
->rf_rev
== RT2573_RF_5225
||
1582 sc
->rf_rev
== RT2573_RF_2527
) ? rum_rf5225
: rum_rf5226
;
1584 /* find the settings for this channel (we know it exists) */
1585 for (i
= 0; rfprog
[i
].chan
!= chan
; i
++)
1588 power
= sc
->txpow
[i
];
1592 } else if (power
> 31) {
1593 bbp94
+= power
- 31;
1598 * If we are switching from the 2GHz band to the 5GHz band or
1599 * vice-versa, BBP registers need to be reprogrammed.
1601 if (c
->ic_flags
!= sc
->sc_curchan
->ic_flags
) {
1602 rum_select_band(sc
, c
);
1603 rum_select_antenna(sc
);
1607 rum_rf_write(sc
, RT2573_RF1
, rfprog
[i
].r1
);
1608 rum_rf_write(sc
, RT2573_RF2
, rfprog
[i
].r2
);
1609 rum_rf_write(sc
, RT2573_RF3
, rfprog
[i
].r3
| power
<< 7);
1610 rum_rf_write(sc
, RT2573_RF4
, rfprog
[i
].r4
| sc
->rffreq
<< 10);
1612 rum_rf_write(sc
, RT2573_RF1
, rfprog
[i
].r1
);
1613 rum_rf_write(sc
, RT2573_RF2
, rfprog
[i
].r2
);
1614 rum_rf_write(sc
, RT2573_RF3
, rfprog
[i
].r3
| power
<< 7 | 1);
1615 rum_rf_write(sc
, RT2573_RF4
, rfprog
[i
].r4
| sc
->rffreq
<< 10);
1617 rum_rf_write(sc
, RT2573_RF1
, rfprog
[i
].r1
);
1618 rum_rf_write(sc
, RT2573_RF2
, rfprog
[i
].r2
);
1619 rum_rf_write(sc
, RT2573_RF3
, rfprog
[i
].r3
| power
<< 7);
1620 rum_rf_write(sc
, RT2573_RF4
, rfprog
[i
].r4
| sc
->rffreq
<< 10);
1624 /* enable smart mode for MIMO-capable RFs */
1625 bbp3
= rum_bbp_read(sc
, 3);
1627 if (sc
->rf_rev
== RT2573_RF_5225
|| sc
->rf_rev
== RT2573_RF_2527
)
1628 bbp3
&= ~RT2573_SMART_MODE
;
1630 bbp3
|= RT2573_SMART_MODE
;
1632 rum_bbp_write(sc
, 3, bbp3
);
1634 if (bbp94
!= RT2573_BBPR94_DEFAULT
)
1635 rum_bbp_write(sc
, 94, bbp94
);
1637 sc
->sc_sifs
= IEEE80211_IS_CHAN_5GHZ(c
) ? IEEE80211_DUR_OFDM_SIFS
1638 : IEEE80211_DUR_SIFS
;
1642 * Enable TSF synchronization and tell h/w to start sending beacons for IBSS
1643 * and HostAP operating modes.
1646 rum_enable_tsf_sync(struct rum_softc
*sc
)
1648 struct ieee80211com
*ic
= &sc
->sc_ic
;
1651 if (ic
->ic_opmode
!= IEEE80211_M_STA
) {
1653 * Change default 16ms TBTT adjustment to 8ms.
1654 * Must be done before enabling beacon generation.
1656 rum_write(sc
, RT2573_TXRX_CSR10
, 1 << 12 | 8);
1659 tmp
= rum_read(sc
, RT2573_TXRX_CSR9
) & 0xff000000;
1661 /* set beacon interval (in 1/16ms unit) */
1662 tmp
|= ic
->ic_bss
->ni_intval
* 16;
1664 tmp
|= RT2573_TSF_TICKING
| RT2573_ENABLE_TBTT
;
1665 if (ic
->ic_opmode
== IEEE80211_M_STA
)
1666 tmp
|= RT2573_TSF_MODE(1);
1668 tmp
|= RT2573_TSF_MODE(2) | RT2573_GENERATE_BEACON
;
1670 rum_write(sc
, RT2573_TXRX_CSR9
, tmp
);
1674 rum_update_slot(struct rum_softc
*sc
)
1676 struct ieee80211com
*ic
= &sc
->sc_ic
;
1680 slottime
= (ic
->ic_flags
& IEEE80211_F_SHSLOT
) ? 9 : 20;
1682 tmp
= rum_read(sc
, RT2573_MAC_CSR9
);
1683 tmp
= (tmp
& ~0xff) | slottime
;
1684 rum_write(sc
, RT2573_MAC_CSR9
, tmp
);
1686 DPRINTF(("setting slot time to %uus\n", slottime
));
1690 rum_set_bssid(struct rum_softc
*sc
, const uint8_t *bssid
)
1694 tmp
= bssid
[0] | bssid
[1] << 8 | bssid
[2] << 16 | bssid
[3] << 24;
1695 rum_write(sc
, RT2573_MAC_CSR4
, tmp
);
1697 tmp
= bssid
[4] | bssid
[5] << 8 | RT2573_ONE_BSSID
<< 16;
1698 rum_write(sc
, RT2573_MAC_CSR5
, tmp
);
1702 rum_set_macaddr(struct rum_softc
*sc
, const uint8_t *addr
)
1706 tmp
= addr
[0] | addr
[1] << 8 | addr
[2] << 16 | addr
[3] << 24;
1707 rum_write(sc
, RT2573_MAC_CSR2
, tmp
);
1709 tmp
= addr
[4] | addr
[5] << 8 | 0xff << 16;
1710 rum_write(sc
, RT2573_MAC_CSR3
, tmp
);
1714 rum_update_promisc(struct rum_softc
*sc
)
1716 struct ifnet
*ifp
= &sc
->sc_ic
.ic_if
;
1719 tmp
= rum_read(sc
, RT2573_TXRX_CSR0
);
1721 tmp
&= ~RT2573_DROP_NOT_TO_ME
;
1722 if (!(ifp
->if_flags
& IFF_PROMISC
))
1723 tmp
|= RT2573_DROP_NOT_TO_ME
;
1725 rum_write(sc
, RT2573_TXRX_CSR0
, tmp
);
1727 DPRINTF(("%s promiscuous mode\n", (ifp
->if_flags
& IFF_PROMISC
) ?
1728 "entering" : "leaving"));
1735 case RT2573_RF_2527
: return "RT2527 (MIMO XR)";
1736 case RT2573_RF_2528
: return "RT2528";
1737 case RT2573_RF_5225
: return "RT5225 (MIMO XR)";
1738 case RT2573_RF_5226
: return "RT5226";
1739 default: return "unknown";
1744 rum_read_eeprom(struct rum_softc
*sc
)
1746 struct ieee80211com
*ic
= &sc
->sc_ic
;
1752 /* read MAC/BBP type */
1753 rum_eeprom_read(sc
, RT2573_EEPROM_MACBBP
, &val
, 2);
1754 sc
->macbbp_rev
= le16toh(val
);
1756 /* read MAC address */
1757 rum_eeprom_read(sc
, RT2573_EEPROM_ADDRESS
, ic
->ic_myaddr
, 6);
1759 rum_eeprom_read(sc
, RT2573_EEPROM_ANTENNA
, &val
, 2);
1761 sc
->rf_rev
= (val
>> 11) & 0x1f;
1762 sc
->hw_radio
= (val
>> 10) & 0x1;
1763 sc
->rx_ant
= (val
>> 4) & 0x3;
1764 sc
->tx_ant
= (val
>> 2) & 0x3;
1765 sc
->nb_ant
= val
& 0x3;
1767 DPRINTF(("RF revision=%d\n", sc
->rf_rev
));
1769 rum_eeprom_read(sc
, RT2573_EEPROM_CONFIG2
, &val
, 2);
1771 sc
->ext_5ghz_lna
= (val
>> 6) & 0x1;
1772 sc
->ext_2ghz_lna
= (val
>> 4) & 0x1;
1774 DPRINTF(("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n",
1775 sc
->ext_2ghz_lna
, sc
->ext_5ghz_lna
));
1777 rum_eeprom_read(sc
, RT2573_EEPROM_RSSI_2GHZ_OFFSET
, &val
, 2);
1779 if ((val
& 0xff) != 0xff)
1780 sc
->rssi_2ghz_corr
= (int8_t)(val
& 0xff); /* signed */
1782 /* Only [-10, 10] is valid */
1783 if (sc
->rssi_2ghz_corr
< -10 || sc
->rssi_2ghz_corr
> 10)
1784 sc
->rssi_2ghz_corr
= 0;
1786 rum_eeprom_read(sc
, RT2573_EEPROM_RSSI_5GHZ_OFFSET
, &val
, 2);
1788 if ((val
& 0xff) != 0xff)
1789 sc
->rssi_5ghz_corr
= (int8_t)(val
& 0xff); /* signed */
1791 /* Only [-10, 10] is valid */
1792 if (sc
->rssi_5ghz_corr
< -10 || sc
->rssi_5ghz_corr
> 10)
1793 sc
->rssi_5ghz_corr
= 0;
1795 if (sc
->ext_2ghz_lna
)
1796 sc
->rssi_2ghz_corr
-= 14;
1797 if (sc
->ext_5ghz_lna
)
1798 sc
->rssi_5ghz_corr
-= 14;
1800 DPRINTF(("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n",
1801 sc
->rssi_2ghz_corr
, sc
->rssi_5ghz_corr
));
1803 rum_eeprom_read(sc
, RT2573_EEPROM_FREQ_OFFSET
, &val
, 2);
1805 if ((val
& 0xff) != 0xff)
1806 sc
->rffreq
= val
& 0xff;
1808 DPRINTF(("RF freq=%d\n", sc
->rffreq
));
1810 /* read Tx power for all a/b/g channels */
1811 rum_eeprom_read(sc
, RT2573_EEPROM_TXPOWER
, sc
->txpow
, 14);
1812 /* XXX default Tx power for 802.11a channels */
1813 memset(sc
->txpow
+ 14, 24, sizeof (sc
->txpow
) - 14);
1815 for (i
= 0; i
< 14; i
++)
1816 DPRINTF(("Channel=%d Tx power=%d\n", i
+ 1, sc
->txpow
[i
]));
1819 /* read default values for BBP registers */
1820 rum_eeprom_read(sc
, RT2573_EEPROM_BBP_BASE
, sc
->bbp_prom
, 2 * 16);
1822 for (i
= 0; i
< 14; i
++) {
1823 if (sc
->bbp_prom
[i
].reg
== 0 || sc
->bbp_prom
[i
].reg
== 0xff)
1825 DPRINTF(("BBP R%d=%02x\n", sc
->bbp_prom
[i
].reg
,
1826 sc
->bbp_prom
[i
].val
));
1832 rum_bbp_init(struct rum_softc
*sc
)
1834 #define N(a) (sizeof (a) / sizeof ((a)[0]))
1838 /* wait for BBP to be ready */
1839 for (ntries
= 0; ntries
< 100; ntries
++) {
1840 val
= rum_bbp_read(sc
, 0);
1841 if (val
!= 0 && val
!= 0xff)
1845 if (ntries
== 100) {
1846 kprintf("%s: timeout waiting for BBP\n",
1847 device_get_nameunit(sc
->sc_dev
));
1851 /* initialize BBP registers to default values */
1852 for (i
= 0; i
< N(rum_def_bbp
); i
++)
1853 rum_bbp_write(sc
, rum_def_bbp
[i
].reg
, rum_def_bbp
[i
].val
);
1855 /* write vendor-specific BBP values (from EEPROM) */
1856 for (i
= 0; i
< 16; i
++) {
1857 if (sc
->bbp_prom
[i
].reg
== 0 || sc
->bbp_prom
[i
].reg
== 0xff)
1859 rum_bbp_write(sc
, sc
->bbp_prom
[i
].reg
, sc
->bbp_prom
[i
].val
);
1869 #define N(a) (sizeof(a) / sizeof((a)[0]))
1870 struct rum_softc
*sc
= xsc
;
1871 struct ieee80211com
*ic
= &sc
->sc_ic
;
1872 struct ifnet
*ifp
= &ic
->ic_if
;
1873 struct rum_rx_data
*data
;
1875 usbd_status usb_err
;
1876 int i
, ntries
, error
;
1878 ASSERT_SERIALIZED(ifp
->if_serializer
);
1885 lwkt_serialize_exit(ifp
->if_serializer
);
1887 /* initialize MAC registers to default values */
1888 for (i
= 0; i
< N(rum_def_mac
); i
++)
1889 rum_write(sc
, rum_def_mac
[i
].reg
, rum_def_mac
[i
].val
);
1891 /* set host ready */
1892 rum_write(sc
, RT2573_MAC_CSR1
, 3);
1893 rum_write(sc
, RT2573_MAC_CSR1
, 0);
1895 /* wait for BBP/RF to wakeup */
1896 for (ntries
= 0; ntries
< 1000; ntries
++) {
1897 if (rum_read(sc
, RT2573_MAC_CSR12
) & 8)
1899 rum_write(sc
, RT2573_MAC_CSR12
, 4); /* force wakeup */
1902 if (ntries
== 1000) {
1903 kprintf("%s: timeout waiting for BBP/RF to wakeup\n",
1904 device_get_nameunit(sc
->sc_dev
));
1909 error
= rum_bbp_init(sc
);
1913 /* select default channel */
1914 sc
->sc_curchan
= ic
->ic_curchan
= ic
->ic_ibss_chan
;
1916 rum_select_band(sc
, sc
->sc_curchan
);
1917 rum_select_antenna(sc
);
1918 rum_set_chan(sc
, sc
->sc_curchan
);
1920 /* clear STA registers */
1921 rum_read_multi(sc
, RT2573_STA_CSR0
, sc
->sta
, sizeof sc
->sta
);
1923 IEEE80211_ADDR_COPY(ic
->ic_myaddr
, IF_LLADDR(ifp
));
1924 rum_set_macaddr(sc
, ic
->ic_myaddr
);
1926 /* initialize ASIC */
1927 rum_write(sc
, RT2573_MAC_CSR1
, 4);
1930 * Allocate xfer for AMRR statistics requests.
1932 sc
->stats_xfer
= usbd_alloc_xfer(sc
->sc_udev
);
1933 if (sc
->stats_xfer
== NULL
) {
1934 kprintf("%s: could not allocate AMRR xfer\n",
1935 device_get_nameunit(sc
->sc_dev
));
1941 * Open Tx and Rx USB bulk pipes.
1943 usb_err
= usbd_open_pipe(sc
->sc_iface
, sc
->sc_tx_no
, USBD_EXCLUSIVE_USE
,
1945 if (usb_err
!= USBD_NORMAL_COMPLETION
) {
1946 kprintf("%s: could not open Tx pipe: %s\n",
1947 device_get_nameunit(sc
->sc_dev
), usbd_errstr(usb_err
));
1952 usb_err
= usbd_open_pipe(sc
->sc_iface
, sc
->sc_rx_no
, USBD_EXCLUSIVE_USE
,
1954 if (usb_err
!= USBD_NORMAL_COMPLETION
) {
1955 kprintf("%s: could not open Rx pipe: %s\n",
1956 device_get_nameunit(sc
->sc_dev
), usbd_errstr(usb_err
));
1962 * Allocate Tx and Rx xfer queues.
1964 error
= rum_alloc_tx_list(sc
);
1966 kprintf("%s: could not allocate Tx list\n",
1967 device_get_nameunit(sc
->sc_dev
));
1971 error
= rum_alloc_rx_list(sc
);
1973 kprintf("%s: could not allocate Rx list\n",
1974 device_get_nameunit(sc
->sc_dev
));
1979 * Start up the receive pipe.
1981 for (i
= 0; i
< RT2573_RX_LIST_COUNT
; i
++) {
1982 data
= &sc
->rx_data
[i
];
1984 usbd_setup_xfer(data
->xfer
, sc
->sc_rx_pipeh
, data
, data
->buf
,
1985 MCLBYTES
, USBD_SHORT_XFER_OK
, USBD_NO_TIMEOUT
, rum_rxeof
);
1986 usbd_transfer(data
->xfer
);
1989 /* update Rx filter */
1990 tmp
= rum_read(sc
, RT2573_TXRX_CSR0
) & 0xffff;
1992 tmp
|= RT2573_DROP_PHY_ERROR
| RT2573_DROP_CRC_ERROR
;
1993 if (ic
->ic_opmode
!= IEEE80211_M_MONITOR
) {
1994 tmp
|= RT2573_DROP_CTL
| RT2573_DROP_VER_ERROR
|
1996 if (ic
->ic_opmode
!= IEEE80211_M_HOSTAP
)
1997 tmp
|= RT2573_DROP_TODS
;
1998 if (!(ifp
->if_flags
& IFF_PROMISC
))
1999 tmp
|= RT2573_DROP_NOT_TO_ME
;
2001 rum_write(sc
, RT2573_TXRX_CSR0
, tmp
);
2003 lwkt_serialize_enter(ifp
->if_serializer
);
2008 ifp
->if_flags
&= ~IFF_OACTIVE
;
2009 ifp
->if_flags
|= IFF_RUNNING
;
2011 if (ic
->ic_opmode
!= IEEE80211_M_MONITOR
) {
2012 if (ic
->ic_roaming
!= IEEE80211_ROAMING_MANUAL
)
2013 ieee80211_new_state(ic
, IEEE80211_S_SCAN
, -1);
2015 ieee80211_new_state(ic
, IEEE80211_S_RUN
, -1);
2024 rum_stop(struct rum_softc
*sc
)
2026 struct ieee80211com
*ic
= &sc
->sc_ic
;
2027 struct ifnet
*ifp
= &ic
->ic_if
;
2030 ASSERT_SERIALIZED(ifp
->if_serializer
);
2034 ifp
->if_flags
&= ~(IFF_RUNNING
| IFF_OACTIVE
);
2037 ieee80211_new_state(ic
, IEEE80211_S_INIT
, -1); /* free all nodes */
2039 sc
->sc_tx_timer
= 0;
2042 lwkt_serialize_exit(ifp
->if_serializer
);
2045 tmp
= rum_read(sc
, RT2573_TXRX_CSR0
);
2046 rum_write(sc
, RT2573_TXRX_CSR0
, tmp
| RT2573_DISABLE_RX
);
2049 rum_write(sc
, RT2573_MAC_CSR1
, 3);
2050 rum_write(sc
, RT2573_MAC_CSR1
, 0);
2052 if (sc
->stats_xfer
!= NULL
) {
2053 usbd_free_xfer(sc
->stats_xfer
);
2054 sc
->stats_xfer
= NULL
;
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 lwkt_serialize_enter(ifp
->if_serializer
);
2071 rum_free_rx_list(sc
);
2072 rum_free_tx_list(sc
);
2078 rum_load_microcode(struct rum_softc
*sc
, const uint8_t *ucode
, size_t size
)
2080 usb_device_request_t req
;
2081 uint16_t reg
= RT2573_MCU_CODE_BASE
;
2084 /* copy firmware image into NIC */
2085 for (; size
>= 4; reg
+= 4, ucode
+= 4, size
-= 4)
2086 rum_write(sc
, reg
, UGETDW(ucode
));
2088 req
.bmRequestType
= UT_WRITE_VENDOR_DEVICE
;
2089 req
.bRequest
= RT2573_MCU_CNTL
;
2090 USETW(req
.wValue
, RT2573_MCU_RUN
);
2091 USETW(req
.wIndex
, 0);
2092 USETW(req
.wLength
, 0);
2094 error
= usbd_do_request(sc
->sc_udev
, &req
, NULL
);
2096 kprintf("%s: could not run firmware: %s\n",
2097 device_get_nameunit(sc
->sc_dev
), usbd_errstr(error
));
2103 rum_prepare_beacon(struct rum_softc
*sc
)
2105 struct ieee80211com
*ic
= &sc
->sc_ic
;
2106 struct ifnet
*ifp
= &ic
->ic_if
;
2107 struct ieee80211_beacon_offsets bo
;
2108 struct rum_tx_desc desc
;
2112 lwkt_serialize_enter(ifp
->if_serializer
);
2113 m0
= ieee80211_beacon_alloc(ic
, ic
->ic_bss
, &bo
);
2114 lwkt_serialize_exit(ifp
->if_serializer
);
2117 if_printf(&ic
->ic_if
, "could not allocate beacon frame\n");
2121 /* send beacons at the lowest available rate */
2122 rate
= IEEE80211_IS_CHAN_5GHZ(ic
->ic_bss
->ni_chan
) ? 12 : 2;
2124 rum_setup_tx_desc(sc
, &desc
, RT2573_TX_TIMESTAMP
, RT2573_TX_HWSEQ
,
2125 m0
->m_pkthdr
.len
, rate
);
2127 /* copy the first 24 bytes of Tx descriptor into NIC memory */
2128 rum_write_multi(sc
, RT2573_HW_BEACON_BASE0
, (uint8_t *)&desc
, 24);
2130 /* copy beacon header and payload into NIC memory */
2131 rum_write_multi(sc
, RT2573_HW_BEACON_BASE0
+ 24, mtod(m0
, uint8_t *),
2140 rum_stats_timeout(void *arg
)
2142 struct rum_softc
*sc
= arg
;
2143 usb_device_request_t req
;
2151 * Asynchronously read statistic registers (cleared by read).
2153 req
.bmRequestType
= UT_READ_VENDOR_DEVICE
;
2154 req
.bRequest
= RT2573_READ_MULTI_MAC
;
2155 USETW(req
.wValue
, 0);
2156 USETW(req
.wIndex
, RT2573_STA_CSR0
);
2157 USETW(req
.wLength
, sizeof(sc
->sta
));
2159 usbd_setup_default_xfer(sc
->stats_xfer
, sc
->sc_udev
, sc
,
2160 USBD_DEFAULT_TIMEOUT
, &req
,
2161 sc
->sta
, sizeof(sc
->sta
), 0,
2163 usbd_transfer(sc
->stats_xfer
);
2169 rum_stats_update(usbd_xfer_handle xfer
, usbd_private_handle priv
,
2172 struct rum_softc
*sc
= (struct rum_softc
*)priv
;
2173 struct ifnet
*ifp
= &sc
->sc_ic
.ic_if
;
2174 struct ieee80211_ratectl_stats
*stats
= &sc
->sc_stats
;
2176 if (status
!= USBD_NORMAL_COMPLETION
) {
2177 kprintf("%s: could not retrieve Tx statistics - cancelling "
2178 "automatic rate control\n", device_get_nameunit(sc
->sc_dev
));
2184 /* count TX retry-fail as Tx errors */
2185 ifp
->if_oerrors
+= RUM_TX_PKT_FAIL(sc
);
2187 stats
->stats_pkt_noretry
+= RUM_TX_PKT_NO_RETRY(sc
);
2188 stats
->stats_pkt_ok
+= RUM_TX_PKT_NO_RETRY(sc
) +
2189 RUM_TX_PKT_ONE_RETRY(sc
) +
2190 RUM_TX_PKT_MULTI_RETRY(sc
);
2191 stats
->stats_pkt_err
+= RUM_TX_PKT_FAIL(sc
);
2193 stats
->stats_retries
+= RUM_TX_PKT_ONE_RETRY(sc
);
2196 * XXX Estimated average:
2197 * Actual number of retries for each packet should belong to
2198 * [2, RUM_TX_SHORT_RETRY_MAX]
2200 stats
->stats_retries
+= RUM_TX_PKT_MULTI_RETRY(sc
) *
2201 ((2 + RUM_TX_SHORT_RETRY_MAX
) / 2);
2203 stats
->stats_retries
+= RUM_TX_PKT_MULTI_RETRY(sc
);
2205 stats
->stats_retries
+= RUM_TX_PKT_FAIL(sc
) * RUM_TX_SHORT_RETRY_MAX
;
2207 callout_reset(&sc
->stats_ch
, 4 * hz
/ 5, rum_stats_timeout
, sc
);
2213 rum_stats(struct ieee80211com
*ic
, struct ieee80211_node
*ni __unused
,
2214 struct ieee80211_ratectl_stats
*stats
)
2216 struct ifnet
*ifp
= &ic
->ic_if
;
2217 struct rum_softc
*sc
= ifp
->if_softc
;
2219 ASSERT_SERIALIZED(ifp
->if_serializer
);
2221 bcopy(&sc
->sc_stats
, stats
, sizeof(*stats
));
2222 bzero(&sc
->sc_stats
, sizeof(sc
->sc_stats
));
2226 rum_ratectl_change(struct ieee80211com
*ic
, u_int orc __unused
, u_int nrc
)
2228 struct ieee80211_ratectl_state
*st
= &ic
->ic_ratectl
;
2229 struct ieee80211_onoe_param
*oparam
;
2231 if (st
->rc_st_param
!= NULL
) {
2232 kfree(st
->rc_st_param
, M_DEVBUF
);
2233 st
->rc_st_param
= NULL
;
2237 case IEEE80211_RATECTL_ONOE
:
2238 oparam
= kmalloc(sizeof(*oparam
), M_DEVBUF
, M_INTWAIT
);
2240 IEEE80211_ONOE_PARAM_SETUP(oparam
);
2241 oparam
->onoe_raise
= 15;
2243 st
->rc_st_param
= oparam
;
2245 case IEEE80211_RATECTL_NONE
:
2246 /* This could only happen during detaching */
2249 panic("unknown rate control algo %u\n", nrc
);
2254 rum_get_rssi(struct rum_softc
*sc
, uint8_t raw
)
2258 lna
= (raw
>> 5) & 0x3;
2265 * NB: Since RSSI is relative to noise floor, -1 is
2266 * adequate for caller to know error happened.
2271 rssi
= (2 * agc
) - RT2573_NOISE_FLOOR
;
2273 if (IEEE80211_IS_CHAN_2GHZ(sc
->sc_curchan
)) {
2274 rssi
+= sc
->rssi_2ghz_corr
;
2283 rssi
+= sc
->rssi_5ghz_corr
;
2285 if (!sc
->ext_5ghz_lna
&& lna
!= 1)