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.28 2008/05/14 11:59:21 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>
53 #include "if_rumreg.h"
54 #include "if_rumvar.h"
55 #include "rum_ucode.h"
62 #define DPRINTF(x) do { if (rum_debug) kprintf x; } while (0)
63 #define DPRINTFN(n, x) do { if (rum_debug >= (n)) kprintf x; } while (0)
67 #define DPRINTFN(n, x)
70 /* various supported device vendors/products */
71 static const struct usb_devno rum_devs
[] = {
72 { USB_DEVICE(0x0411, 0x00d8) }, /* Melco WLI-U2-SG54HP */
73 { USB_DEVICE(0x0411, 0x00d9) }, /* Melco WLI-U2-G54HP */
74 { USB_DEVICE(0x050d, 0x705a) }, /* Belkin F5D7050A */
75 { USB_DEVICE(0x050d, 0x905b) }, /* Belkin F5D9050 ver3 */
76 { USB_DEVICE(0x0586, 0x3415) }, /* ZyXEL RT2573 */
77 { USB_DEVICE(0x06f8, 0xe010) }, /* Guillemot HWGUSB2-54-LB */
78 { USB_DEVICE(0x06f8, 0xe020) }, /* Guillemot HWGUSB2-54V2-AP */
79 { USB_DEVICE(0x0769, 0x31f3) }, /* Surecom RT2573 */
80 { USB_DEVICE(0x07b8, 0xb21b) }, /* AboCom HWU54DM */
81 { USB_DEVICE(0x07b8, 0xb21c) }, /* AboCom RT2573 */
82 { USB_DEVICE(0x07b8, 0xb21d) }, /* AboCom RT2573 */
83 { USB_DEVICE(0x07b8, 0xb21e) }, /* AboCom RT2573 */
84 { USB_DEVICE(0x07b8, 0xb21f) }, /* AboCom WUG2700 */
85 { USB_DEVICE(0x07d1, 0x3c03) }, /* D-Link DWL-G122 rev c1 */
86 { USB_DEVICE(0x07d1, 0x3c04) }, /* D-Link WUA-1340 */
87 { USB_DEVICE(0x0b05, 0x1723) }, /* Asus WL-167g */
88 { USB_DEVICE(0x0b05, 0x1724) }, /* Asus WL-167g */
89 { USB_DEVICE(0x0db0, 0x6874) }, /* MSI RT2573 */
90 { USB_DEVICE(0x0db0, 0x6877) }, /* MSI RT2573 */
91 { USB_DEVICE(0x0db0, 0xa861) }, /* MSI RT2573 */
92 { USB_DEVICE(0x0db0, 0xa874) }, /* MSI RT2573 */
93 { USB_DEVICE(0x0df6, 0x90ac) }, /* Sitecom WL-172 */
94 { USB_DEVICE(0x0df6, 0x9712) }, /* Sitecom WL-113 rev 2 */
95 { USB_DEVICE(0x0eb0, 0x9021) }, /* Nova Technology RT2573 */
96 { USB_DEVICE(0x1044, 0x8008) }, /* GIGABYTE GN-WB01GS */
97 { USB_DEVICE(0x1044, 0x800a) }, /* GIGABYTE GN-WI05GS */
98 { USB_DEVICE(0x1371, 0x9022) }, /* (really) C-Net RT2573 */
99 { USB_DEVICE(0x1371, 0x9032) }, /* (really) C-Net CWD854F */
100 { USB_DEVICE(0x13b1, 0x0020) }, /* Cisco-Linksys WUSB54GC */
101 { USB_DEVICE(0x13b1, 0x0023) }, /* Cisco-Linksys WUSB54GR */
102 { USB_DEVICE(0x1472, 0x0009) }, /* Huawei RT2573 */
103 { USB_DEVICE(0x148f, 0x2573) }, /* Ralink RT2573 */
104 { USB_DEVICE(0x148f, 0x2671) }, /* Ralink RT2671 */
105 { USB_DEVICE(0x148f, 0x9021) }, /* Ralink RT2573 */
106 { USB_DEVICE(0x14b2, 0x3c22) }, /* Conceptronic C54RU */
107 { USB_DEVICE(0x15a9, 0x0004) }, /* SparkLan RT2573 */
108 { USB_DEVICE(0x1631, 0xc019) }, /* Good Way Technology RT2573 */
109 { USB_DEVICE(0x1690, 0x0722) }, /* Gigaset RT2573 */
110 { USB_DEVICE(0x1737, 0x0020) }, /* Linksys WUSB54GC */
111 { USB_DEVICE(0x1737, 0x0023) }, /* Linksys WUSB54GR */
112 { USB_DEVICE(0x18c5, 0x0002) }, /* AMIT CG-WLUSB2GO */
113 { USB_DEVICE(0x18e8, 0x6196) }, /* Qcom RT2573 */
114 { USB_DEVICE(0x18e8, 0x6229) }, /* Qcom RT2573 */
115 { USB_DEVICE(0x18e8, 0x6238) }, /* Qcom RT2573 */
116 { USB_DEVICE(0x2019, 0xab01) }, /* Planex GW-US54HP */
117 { USB_DEVICE(0x2019, 0xab50) }, /* Planex GW-US54Mini2 */
118 { USB_DEVICE(0x2019, 0xed02) }, /* Planex GW-USMM */
121 static int rum_alloc_tx_list(struct rum_softc
*);
122 static void rum_free_tx_list(struct rum_softc
*);
123 static int rum_alloc_rx_list(struct rum_softc
*);
124 static void rum_free_rx_list(struct rum_softc
*);
125 static int rum_media_change(struct ifnet
*);
126 static void rum_next_scan(void *);
127 static void rum_task(void *);
128 static int rum_newstate(struct ieee80211com
*,
129 enum ieee80211_state
, int);
130 static void rum_txeof(usbd_xfer_handle
, usbd_private_handle
,
132 static void rum_rxeof(usbd_xfer_handle
, usbd_private_handle
,
134 static uint8_t rum_rxrate(struct rum_rx_desc
*);
135 static uint8_t rum_plcp_signal(int);
136 static void rum_setup_tx_desc(struct rum_softc
*,
137 struct rum_tx_desc
*, uint32_t, uint16_t, int,
139 static int rum_tx_data(struct rum_softc
*, struct mbuf
*,
140 struct ieee80211_node
*);
141 static void rum_start(struct ifnet
*);
142 static void rum_watchdog(struct ifnet
*);
143 static int rum_ioctl(struct ifnet
*, u_long
, caddr_t
,
145 static void rum_eeprom_read(struct rum_softc
*, uint16_t, void *,
147 static uint32_t rum_read(struct rum_softc
*, uint16_t);
148 static void rum_read_multi(struct rum_softc
*, uint16_t, void *,
150 static void rum_write(struct rum_softc
*, uint16_t, uint32_t);
151 static void rum_write_multi(struct rum_softc
*, uint16_t, void *,
153 static void rum_bbp_write(struct rum_softc
*, uint8_t, uint8_t);
154 static uint8_t rum_bbp_read(struct rum_softc
*, uint8_t);
155 static void rum_rf_write(struct rum_softc
*, uint8_t, uint32_t);
156 static void rum_select_antenna(struct rum_softc
*);
157 static void rum_enable_mrr(struct rum_softc
*);
158 static void rum_set_txpreamble(struct rum_softc
*);
159 static void rum_set_basicrates(struct rum_softc
*);
160 static void rum_select_band(struct rum_softc
*,
161 struct ieee80211_channel
*);
162 static void rum_set_chan(struct rum_softc
*,
163 struct ieee80211_channel
*);
164 static void rum_enable_tsf_sync(struct rum_softc
*);
165 static void rum_update_slot(struct rum_softc
*);
166 static void rum_set_bssid(struct rum_softc
*, const uint8_t *);
167 static void rum_set_macaddr(struct rum_softc
*, const uint8_t *);
168 static void rum_update_promisc(struct rum_softc
*);
169 static const char *rum_get_rf(int);
170 static void rum_read_eeprom(struct rum_softc
*);
171 static int rum_bbp_init(struct rum_softc
*);
172 static void rum_init(void *);
173 static void rum_stop(struct rum_softc
*);
174 static int rum_load_microcode(struct rum_softc
*, const uint8_t *,
176 static int rum_prepare_beacon(struct rum_softc
*);
178 static void rum_stats_timeout(void *);
179 static void rum_stats_update(usbd_xfer_handle
, usbd_private_handle
,
181 static void rum_stats(struct ieee80211com
*,
182 struct ieee80211_node
*,
183 struct ieee80211_ratectl_stats
*);
184 static void *rum_ratectl_attach(struct ieee80211com
*, u_int
);
185 static int rum_get_rssi(struct rum_softc
*, uint8_t);
188 * Supported rates for 802.11a/b/g modes (in 500Kbps unit).
190 static const struct ieee80211_rateset rum_rateset_11a
=
191 { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
193 static const struct ieee80211_rateset rum_rateset_11b
=
194 { 4, { 2, 4, 11, 22 } };
196 static const struct ieee80211_rateset rum_rateset_11g
=
197 { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
199 static const struct {
206 static const struct {
213 static const struct rfprog
{
215 uint32_t r1
, r2
, r3
, r4
;
222 static device_probe_t rum_match
;
223 static device_attach_t rum_attach
;
224 static device_detach_t rum_detach
;
226 static devclass_t rum_devclass
;
228 static kobj_method_t rum_methods
[] = {
229 DEVMETHOD(device_probe
, rum_match
),
230 DEVMETHOD(device_attach
, rum_attach
),
231 DEVMETHOD(device_detach
, rum_detach
),
235 static driver_t rum_driver
= {
238 sizeof(struct rum_softc
)
241 DRIVER_MODULE(rum
, uhub
, rum_driver
, rum_devclass
, usbd_driver_load
, 0);
243 MODULE_DEPEND(rum
, usb
, 1, 1, 1);
244 MODULE_DEPEND(rum
, wlan
, 1, 1, 1);
245 MODULE_DEPEND(rum
, wlan_ratectl_onoe
, 1, 1, 1);
248 rum_match(device_t self
)
250 struct usb_attach_arg
*uaa
= device_get_ivars(self
);
252 if (uaa
->iface
!= NULL
)
255 return (usb_lookup(rum_devs
, uaa
->vendor
, uaa
->product
) != NULL
) ?
256 UMATCH_VENDOR_PRODUCT
: UMATCH_NONE
;
260 rum_attach(device_t self
)
262 struct rum_softc
*sc
= device_get_softc(self
);
263 struct usb_attach_arg
*uaa
= device_get_ivars(self
);
264 struct ieee80211com
*ic
= &sc
->sc_ic
;
265 struct ifnet
*ifp
= &ic
->ic_if
;
266 usb_interface_descriptor_t
*id
;
267 usb_endpoint_descriptor_t
*ed
;
272 sc
->sc_udev
= uaa
->device
;
275 if (usbd_set_config_no(sc
->sc_udev
, RT2573_CONFIG_NO
, 0) != 0) {
276 kprintf("%s: could not set configuration no\n",
277 device_get_nameunit(sc
->sc_dev
));
281 /* get the first interface handle */
282 error
= usbd_device2interface_handle(sc
->sc_udev
, RT2573_IFACE_INDEX
,
285 kprintf("%s: could not get interface handle\n",
286 device_get_nameunit(sc
->sc_dev
));
293 id
= usbd_get_interface_descriptor(sc
->sc_iface
);
295 sc
->sc_rx_no
= sc
->sc_tx_no
= -1;
296 for (i
= 0; i
< id
->bNumEndpoints
; i
++) {
297 ed
= usbd_interface2endpoint_descriptor(sc
->sc_iface
, i
);
299 kprintf("%s: no endpoint descriptor for iface %d\n",
300 device_get_nameunit(sc
->sc_dev
), i
);
304 if (UE_GET_DIR(ed
->bEndpointAddress
) == UE_DIR_IN
&&
305 UE_GET_XFERTYPE(ed
->bmAttributes
) == UE_BULK
)
306 sc
->sc_rx_no
= ed
->bEndpointAddress
;
307 else if (UE_GET_DIR(ed
->bEndpointAddress
) == UE_DIR_OUT
&&
308 UE_GET_XFERTYPE(ed
->bmAttributes
) == UE_BULK
)
309 sc
->sc_tx_no
= ed
->bEndpointAddress
;
311 if (sc
->sc_rx_no
== -1 || sc
->sc_tx_no
== -1) {
312 kprintf("%s: missing endpoint\n", device_get_nameunit(sc
->sc_dev
));
316 usb_init_task(&sc
->sc_task
, rum_task
, sc
);
318 callout_init(&sc
->scan_ch
);
319 callout_init(&sc
->stats_ch
);
321 /* retrieve RT2573 rev. no */
322 for (ntries
= 0; ntries
< 1000; ntries
++) {
323 if ((tmp
= rum_read(sc
, RT2573_MAC_CSR0
)) != 0)
327 if (ntries
== 1000) {
328 kprintf("%s: timeout waiting for chip to settle\n",
329 device_get_nameunit(sc
->sc_dev
));
333 /* retrieve MAC address and various other things from EEPROM */
336 kprintf("%s: MAC/BBP RT%04x (rev 0x%05x), RF %s, address %6D\n",
337 device_get_nameunit(sc
->sc_dev
), sc
->macbbp_rev
, tmp
,
338 rum_get_rf(sc
->rf_rev
), ic
->ic_myaddr
, ":");
340 error
= rum_load_microcode(sc
, rt2573
, sizeof(rt2573
));
342 device_printf(self
, "can't load microcode\n");
346 ic
->ic_phytype
= IEEE80211_T_OFDM
; /* not only, but not used */
347 ic
->ic_opmode
= IEEE80211_M_STA
; /* default to BSS mode */
348 ic
->ic_state
= IEEE80211_S_INIT
;
350 /* set device capabilities */
352 IEEE80211_C_IBSS
| /* IBSS mode supported */
353 IEEE80211_C_MONITOR
| /* monitor mode supported */
354 IEEE80211_C_HOSTAP
| /* HostAp mode supported */
355 IEEE80211_C_TXPMGT
| /* tx power management */
356 IEEE80211_C_SHPREAMBLE
| /* short preamble supported */
357 IEEE80211_C_SHSLOT
| /* short slot time supported */
358 IEEE80211_C_WPA
; /* WPA 1+2 */
360 if (sc
->rf_rev
== RT2573_RF_5225
|| sc
->rf_rev
== RT2573_RF_5226
) {
361 /* set supported .11a rates */
362 ic
->ic_sup_rates
[IEEE80211_MODE_11A
] = rum_rateset_11a
;
364 /* set supported .11a channels */
365 for (i
= 34; i
<= 46; i
+= 4) {
366 ic
->ic_channels
[i
].ic_freq
=
367 ieee80211_ieee2mhz(i
, IEEE80211_CHAN_5GHZ
);
368 ic
->ic_channels
[i
].ic_flags
= IEEE80211_CHAN_A
;
370 for (i
= 36; i
<= 64; i
+= 4) {
371 ic
->ic_channels
[i
].ic_freq
=
372 ieee80211_ieee2mhz(i
, IEEE80211_CHAN_5GHZ
);
373 ic
->ic_channels
[i
].ic_flags
= IEEE80211_CHAN_A
;
375 for (i
= 100; i
<= 140; i
+= 4) {
376 ic
->ic_channels
[i
].ic_freq
=
377 ieee80211_ieee2mhz(i
, IEEE80211_CHAN_5GHZ
);
378 ic
->ic_channels
[i
].ic_flags
= IEEE80211_CHAN_A
;
380 for (i
= 149; i
<= 165; i
+= 4) {
381 ic
->ic_channels
[i
].ic_freq
=
382 ieee80211_ieee2mhz(i
, IEEE80211_CHAN_5GHZ
);
383 ic
->ic_channels
[i
].ic_flags
= IEEE80211_CHAN_A
;
387 /* set supported .11b and .11g rates */
388 ic
->ic_sup_rates
[IEEE80211_MODE_11B
] = rum_rateset_11b
;
389 ic
->ic_sup_rates
[IEEE80211_MODE_11G
] = rum_rateset_11g
;
391 /* set supported .11b and .11g channels (1 through 14) */
392 for (i
= 1; i
<= 14; i
++) {
393 ic
->ic_channels
[i
].ic_freq
=
394 ieee80211_ieee2mhz(i
, IEEE80211_CHAN_2GHZ
);
395 ic
->ic_channels
[i
].ic_flags
=
396 IEEE80211_CHAN_CCK
| IEEE80211_CHAN_OFDM
|
397 IEEE80211_CHAN_DYN
| IEEE80211_CHAN_2GHZ
;
400 sc
->sc_sifs
= IEEE80211_DUR_SIFS
; /* Default SIFS */
402 if_initname(ifp
, device_get_name(self
), device_get_unit(self
));
404 ifp
->if_flags
= IFF_BROADCAST
| IFF_SIMPLEX
| IFF_MULTICAST
;
405 ifp
->if_init
= rum_init
;
406 ifp
->if_ioctl
= rum_ioctl
;
407 ifp
->if_start
= rum_start
;
408 ifp
->if_watchdog
= rum_watchdog
;
409 ifq_set_maxlen(&ifp
->if_snd
, IFQ_MAXLEN
);
410 ifq_set_ready(&ifp
->if_snd
);
412 IEEE80211_ONOE_PARAM_SETUP(&sc
->sc_onoe_param
);
413 sc
->sc_onoe_param
.onoe_raise
= 15;
414 ic
->ic_ratectl
.rc_st_ratectl_cap
= IEEE80211_RATECTL_CAP_ONOE
;
415 ic
->ic_ratectl
.rc_st_ratectl
= IEEE80211_RATECTL_ONOE
;
416 ic
->ic_ratectl
.rc_st_stats
= rum_stats
;
417 ic
->ic_ratectl
.rc_st_attach
= rum_ratectl_attach
;
419 ieee80211_ifattach(ic
);
421 /* Enable software beacon missing handling. */
422 ic
->ic_flags_ext
|= IEEE80211_FEXT_SWBMISS
;
424 /* override state transition machine */
425 sc
->sc_newstate
= ic
->ic_newstate
;
426 ic
->ic_newstate
= rum_newstate
;
427 ieee80211_media_init(ic
, rum_media_change
, ieee80211_media_status
);
429 bpfattach_dlt(ifp
, DLT_IEEE802_11_RADIO
,
430 sizeof(struct ieee80211_frame
) + IEEE80211_RADIOTAP_HDRLEN
,
433 sc
->sc_rxtap_len
= sizeof sc
->sc_rxtapu
;
434 sc
->sc_rxtap
.wr_ihdr
.it_len
= htole16(sc
->sc_rxtap_len
);
435 sc
->sc_rxtap
.wr_ihdr
.it_present
= htole32(RT2573_RX_RADIOTAP_PRESENT
);
437 sc
->sc_txtap_len
= sizeof sc
->sc_txtapu
;
438 sc
->sc_txtap
.wt_ihdr
.it_len
= htole16(sc
->sc_txtap_len
);
439 sc
->sc_txtap
.wt_ihdr
.it_present
= htole32(RT2573_TX_RADIOTAP_PRESENT
);
442 ieee80211_announce(ic
);
448 rum_detach(device_t self
)
450 struct rum_softc
*sc
= device_get_softc(self
);
451 struct ifnet
*ifp
= &sc
->sc_ic
.ic_if
;
458 callout_stop(&sc
->scan_ch
);
459 callout_stop(&sc
->stats_ch
);
461 lwkt_serialize_enter(ifp
->if_serializer
);
463 lwkt_serialize_exit(ifp
->if_serializer
);
465 usb_rem_task(sc
->sc_udev
, &sc
->sc_task
);
468 ieee80211_ifdetach(&sc
->sc_ic
); /* free all nodes */
472 KKASSERT(sc
->stats_xfer
== NULL
);
473 KKASSERT(sc
->sc_rx_pipeh
== NULL
);
474 KKASSERT(sc
->sc_tx_pipeh
== NULL
);
478 * Make sure TX/RX list is empty
480 for (i
= 0; i
< RT2573_TX_LIST_COUNT
; i
++) {
481 struct rum_tx_data
*data
= &sc
->tx_data
[i
];
483 KKASSERT(data
->xfer
== NULL
);
484 KKASSERT(data
->ni
== NULL
);
485 KKASSERT(data
->m
== NULL
);
487 for (i
= 0; i
< RT2573_RX_LIST_COUNT
; i
++) {
488 struct rum_rx_data
*data
= &sc
->rx_data
[i
];
490 KKASSERT(data
->xfer
== NULL
);
491 KKASSERT(data
->m
== NULL
);
498 rum_alloc_tx_list(struct rum_softc
*sc
)
503 for (i
= 0; i
< RT2573_TX_LIST_COUNT
; i
++) {
504 struct rum_tx_data
*data
= &sc
->tx_data
[i
];
508 data
->xfer
= usbd_alloc_xfer(sc
->sc_udev
);
509 if (data
->xfer
== NULL
) {
510 kprintf("%s: could not allocate tx xfer\n",
511 device_get_nameunit(sc
->sc_dev
));
515 data
->buf
= usbd_alloc_buffer(data
->xfer
,
516 RT2573_TX_DESC_SIZE
+ IEEE80211_MAX_LEN
);
517 if (data
->buf
== NULL
) {
518 kprintf("%s: could not allocate tx buffer\n",
519 device_get_nameunit(sc
->sc_dev
));
523 /* clean Tx descriptor */
524 bzero(data
->buf
, RT2573_TX_DESC_SIZE
);
530 rum_free_tx_list(struct rum_softc
*sc
)
534 for (i
= 0; i
< RT2573_TX_LIST_COUNT
; i
++) {
535 struct rum_tx_data
*data
= &sc
->tx_data
[i
];
537 if (data
->xfer
!= NULL
) {
538 usbd_free_xfer(data
->xfer
);
541 if (data
->ni
!= NULL
) {
542 ieee80211_free_node(data
->ni
);
545 if (data
->m
!= NULL
) {
554 rum_alloc_rx_list(struct rum_softc
*sc
)
558 for (i
= 0; i
< RT2573_RX_LIST_COUNT
; i
++) {
559 struct rum_rx_data
*data
= &sc
->rx_data
[i
];
563 data
->xfer
= usbd_alloc_xfer(sc
->sc_udev
);
564 if (data
->xfer
== NULL
) {
565 kprintf("%s: could not allocate rx xfer\n",
566 device_get_nameunit(sc
->sc_dev
));
570 if (usbd_alloc_buffer(data
->xfer
, MCLBYTES
) == NULL
) {
571 kprintf("%s: could not allocate rx buffer\n",
572 device_get_nameunit(sc
->sc_dev
));
576 data
->m
= m_getcl(MB_WAIT
, MT_DATA
, M_PKTHDR
);
578 data
->buf
= mtod(data
->m
, uint8_t *);
579 bzero(data
->buf
, sizeof(struct rum_rx_desc
));
585 rum_free_rx_list(struct rum_softc
*sc
)
589 for (i
= 0; i
< RT2573_RX_LIST_COUNT
; i
++) {
590 struct rum_rx_data
*data
= &sc
->rx_data
[i
];
592 if (data
->xfer
!= NULL
) {
593 usbd_free_xfer(data
->xfer
);
596 if (data
->m
!= NULL
) {
604 rum_media_change(struct ifnet
*ifp
)
608 error
= ieee80211_media_change(ifp
);
609 if (error
!= ENETRESET
)
612 if ((ifp
->if_flags
& (IFF_UP
| IFF_RUNNING
)) == (IFF_UP
| IFF_RUNNING
))
613 rum_init(ifp
->if_softc
);
619 * This function is called periodically (every 200ms) during scanning to
620 * switch from one channel to another.
623 rum_next_scan(void *arg
)
625 struct rum_softc
*sc
= arg
;
626 struct ieee80211com
*ic
= &sc
->sc_ic
;
627 struct ifnet
*ifp
= &ic
->ic_if
;
634 if (ic
->ic_state
== IEEE80211_S_SCAN
) {
635 lwkt_serialize_enter(ifp
->if_serializer
);
636 ieee80211_next_scan(ic
);
637 lwkt_serialize_exit(ifp
->if_serializer
);
646 struct rum_softc
*sc
= xarg
;
647 struct ieee80211com
*ic
= &sc
->sc_ic
;
648 struct ifnet
*ifp
= &ic
->ic_if
;
649 enum ieee80211_state nstate
;
650 struct ieee80211_node
*ni
;
658 nstate
= sc
->sc_state
;
661 KASSERT(nstate
!= IEEE80211_S_INIT
,
662 ("->INIT state transition should not be defered\n"));
663 rum_set_chan(sc
, ic
->ic_curchan
);
666 case IEEE80211_S_RUN
:
669 if (ic
->ic_opmode
!= IEEE80211_M_MONITOR
) {
672 rum_set_txpreamble(sc
);
673 rum_set_basicrates(sc
);
674 rum_set_bssid(sc
, ni
->ni_bssid
);
677 if (ic
->ic_opmode
== IEEE80211_M_HOSTAP
||
678 ic
->ic_opmode
== IEEE80211_M_IBSS
)
679 rum_prepare_beacon(sc
);
681 if (ic
->ic_opmode
!= IEEE80211_M_MONITOR
)
682 rum_enable_tsf_sync(sc
);
684 /* clear statistic registers (STA_CSR0 to STA_CSR5) */
685 rum_read_multi(sc
, RT2573_STA_CSR0
, sc
->sta
, sizeof(sc
->sta
));
686 callout_reset(&sc
->stats_ch
, 4 * hz
/ 5, rum_stats_timeout
, sc
);
689 case IEEE80211_S_SCAN
:
690 callout_reset(&sc
->scan_ch
, hz
/ 5, rum_next_scan
, sc
);
697 lwkt_serialize_enter(ifp
->if_serializer
);
698 ieee80211_ratectl_newstate(ic
, nstate
);
699 sc
->sc_newstate(ic
, nstate
, arg
);
700 lwkt_serialize_exit(ifp
->if_serializer
);
706 rum_newstate(struct ieee80211com
*ic
, enum ieee80211_state nstate
, int arg
)
708 struct rum_softc
*sc
= ic
->ic_if
.if_softc
;
709 struct ifnet
*ifp
= &ic
->ic_if
;
713 ASSERT_SERIALIZED(ifp
->if_serializer
);
715 callout_stop(&sc
->scan_ch
);
716 callout_stop(&sc
->stats_ch
);
718 /* do it in a process context */
719 sc
->sc_state
= nstate
;
722 lwkt_serialize_exit(ifp
->if_serializer
);
723 usb_rem_task(sc
->sc_udev
, &sc
->sc_task
);
725 if (nstate
== IEEE80211_S_INIT
) {
726 lwkt_serialize_enter(ifp
->if_serializer
);
727 ieee80211_ratectl_newstate(ic
, nstate
);
728 sc
->sc_newstate(ic
, nstate
, arg
);
730 usb_add_task(sc
->sc_udev
, &sc
->sc_task
, USB_TASKQ_DRIVER
);
731 lwkt_serialize_enter(ifp
->if_serializer
);
738 /* quickly determine if a given rate is CCK or OFDM */
739 #define RUM_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
741 #define RUM_ACK_SIZE (sizeof(struct ieee80211_frame_ack) + IEEE80211_CRC_LEN)
744 rum_txeof(usbd_xfer_handle xfer
, usbd_private_handle priv
, usbd_status status
)
746 struct rum_tx_data
*data
= priv
;
747 struct rum_softc
*sc
= data
->sc
;
748 struct ieee80211com
*ic
= &sc
->sc_ic
;
749 struct ifnet
*ifp
= &ic
->ic_if
;
750 struct ieee80211_node
*ni
;
757 if (status
!= USBD_NORMAL_COMPLETION
) {
758 if (status
== USBD_NOT_STARTED
|| status
== USBD_CANCELLED
) {
763 kprintf("%s: could not transmit buffer: %s\n",
764 device_get_nameunit(sc
->sc_dev
), usbd_errstr(status
));
766 if (status
== USBD_STALLED
)
767 usbd_clear_endpoint_stall_async(sc
->sc_tx_pipeh
);
779 bzero(data
->buf
, sizeof(struct rum_tx_data
));
781 ifp
->if_opackets
++; /* XXX may fail too */
783 DPRINTFN(10, ("tx done\n"));
786 ifp
->if_flags
&= ~IFF_OACTIVE
;
788 lwkt_serialize_enter(ifp
->if_serializer
);
789 ieee80211_free_node(ni
);
791 lwkt_serialize_exit(ifp
->if_serializer
);
797 rum_rxeof(usbd_xfer_handle xfer
, usbd_private_handle priv
, usbd_status status
)
799 struct rum_rx_data
*data
= priv
;
800 struct rum_softc
*sc
= data
->sc
;
801 struct ieee80211com
*ic
= &sc
->sc_ic
;
802 struct ifnet
*ifp
= &ic
->ic_if
;
803 struct rum_rx_desc
*desc
;
804 struct ieee80211_frame_min
*wh
;
805 struct ieee80211_node
*ni
;
806 struct mbuf
*mnew
, *m
;
814 if (status
!= USBD_NORMAL_COMPLETION
) {
815 if (status
== USBD_NOT_STARTED
|| status
== USBD_CANCELLED
) {
820 if (status
== USBD_STALLED
)
821 usbd_clear_endpoint_stall_async(sc
->sc_rx_pipeh
);
825 usbd_get_xfer_status(xfer
, NULL
, NULL
, &len
, NULL
);
827 if (len
< RT2573_RX_DESC_SIZE
+ sizeof(struct ieee80211_frame_min
)) {
828 DPRINTF(("%s: xfer too short %d\n", device_get_nameunit(sc
->sc_dev
),
834 desc
= (struct rum_rx_desc
*)data
->buf
;
836 if (le32toh(desc
->flags
) & RT2573_RX_CRC_ERROR
) {
838 * This should not happen since we did not request to receive
839 * those frames when we filled RT2573_TXRX_CSR0.
841 DPRINTFN(5, ("CRC error\n"));
846 mnew
= m_getcl(MB_DONTWAIT
, MT_DATA
, M_PKTHDR
);
848 kprintf("%s: could not allocate rx mbuf\n",
849 device_get_nameunit(sc
->sc_dev
));
858 lwkt_serialize_enter(ifp
->if_serializer
);
861 m
->m_pkthdr
.rcvif
= ifp
;
862 m
->m_data
= (caddr_t
)(desc
+ 1);
863 m
->m_pkthdr
.len
= m
->m_len
= (le32toh(desc
->flags
) >> 16) & 0xfff;
865 rssi
= rum_get_rssi(sc
, desc
->rssi
);
867 wh
= mtod(m
, struct ieee80211_frame_min
*);
868 ni
= ieee80211_find_rxnode(ic
, wh
);
870 /* Error happened during RSSI conversion. */
874 if (sc
->sc_drvbpf
!= NULL
) {
875 struct rum_rx_radiotap_header
*tap
= &sc
->sc_rxtap
;
878 tap
->wr_rate
= rum_rxrate(desc
);
879 tap
->wr_chan_freq
= htole16(ic
->ic_bss
->ni_chan
->ic_freq
);
880 tap
->wr_chan_flags
= htole16(ic
->ic_bss
->ni_chan
->ic_flags
);
881 tap
->wr_antenna
= sc
->rx_ant
;
882 tap
->wr_antsignal
= rssi
;
884 bpf_ptap(sc
->sc_drvbpf
, m
, tap
, sc
->sc_rxtap_len
);
887 /* send the frame to the 802.11 layer */
888 ieee80211_input(ic
, m
, ni
, rssi
, 0);
890 /* node is no longer needed */
891 ieee80211_free_node(ni
);
893 if ((ifp
->if_flags
& IFF_OACTIVE
) == 0)
896 lwkt_serialize_exit(ifp
->if_serializer
);
899 data
->buf
= mtod(data
->m
, uint8_t *);
901 DPRINTFN(15, ("rx done\n"));
903 skip
: /* setup a new transfer */
904 bzero(data
->buf
, sizeof(struct rum_rx_desc
));
905 usbd_setup_xfer(xfer
, sc
->sc_rx_pipeh
, data
, data
->buf
, MCLBYTES
,
906 USBD_SHORT_XFER_OK
, USBD_NO_TIMEOUT
, rum_rxeof
);
913 * This function is only used by the Rx radiotap code. It returns the rate at
914 * which a given frame was received.
917 rum_rxrate(struct rum_rx_desc
*desc
)
919 if (le32toh(desc
->flags
) & RT2573_RX_OFDM
) {
920 /* reverse function of rum_plcp_signal */
921 switch (desc
->rate
) {
929 case 0xc: return 108;
932 if (desc
->rate
== 10)
934 if (desc
->rate
== 20)
936 if (desc
->rate
== 55)
938 if (desc
->rate
== 110)
941 return 2; /* should not get there */
945 rum_plcp_signal(int rate
)
948 /* CCK rates (returned values are device-dependent) */
954 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
962 case 108: return 0xc;
964 /* unsupported rates (should not get there) */
965 default: return 0xff;
970 rum_setup_tx_desc(struct rum_softc
*sc
, struct rum_tx_desc
*desc
,
971 uint32_t flags
, uint16_t xflags
, int len
, int rate
)
973 struct ieee80211com
*ic
= &sc
->sc_ic
;
974 uint16_t plcp_length
;
977 desc
->flags
= htole32(flags
);
978 desc
->flags
|= htole32(len
<< 16);
980 desc
->xflags
= htole16(xflags
);
986 RT2573_LOGCWMAX(10));
988 /* setup PLCP fields */
989 desc
->plcp_signal
= rum_plcp_signal(rate
);
990 desc
->plcp_service
= 4;
992 len
+= IEEE80211_CRC_LEN
;
993 if (RUM_RATE_IS_OFDM(rate
)) {
994 desc
->flags
|= htole32(RT2573_TX_OFDM
);
996 plcp_length
= len
& 0xfff;
997 desc
->plcp_length_hi
= plcp_length
>> 6;
998 desc
->plcp_length_lo
= plcp_length
& 0x3f;
1000 plcp_length
= (16 * len
+ rate
- 1) / rate
;
1002 remainder
= (16 * len
) % 22;
1003 if (remainder
!= 0 && remainder
< 7)
1004 desc
->plcp_service
|= RT2573_PLCP_LENGEXT
;
1006 desc
->plcp_length_hi
= plcp_length
>> 8;
1007 desc
->plcp_length_lo
= plcp_length
& 0xff;
1009 if (rate
!= 2 && (ic
->ic_flags
& IEEE80211_F_SHPREAMBLE
))
1010 desc
->plcp_signal
|= 0x08;
1012 desc
->flags
|= htole32(RT2573_TX_VALID
);
1015 #define RUM_TX_TIMEOUT 5000
1018 rum_tx_data(struct rum_softc
*sc
, struct mbuf
*m0
, struct ieee80211_node
*ni
)
1020 struct ieee80211com
*ic
= &sc
->sc_ic
;
1021 struct ifnet
*ifp
= &ic
->ic_if
;
1022 struct rum_tx_desc
*desc
;
1023 struct rum_tx_data
*data
;
1024 struct ieee80211_frame
*wh
;
1028 int xferlen
, rate
, rateidx
;
1030 wh
= mtod(m0
, struct ieee80211_frame
*);
1032 if (wh
->i_fc
[1] & IEEE80211_FC1_WEP
) {
1033 if (ieee80211_crypto_encap(ic
, ni
, m0
) == NULL
) {
1038 /* packet header may have moved, reset our local pointer */
1039 wh
= mtod(m0
, struct ieee80211_frame
*);
1043 if ((wh
->i_fc
[0] & IEEE80211_FC0_TYPE_MASK
) ==
1044 IEEE80211_FC0_TYPE_MGT
) {
1045 /* mgmt frames are sent at the lowest available bit-rate */
1048 ieee80211_ratectl_findrate(ni
, m0
->m_pkthdr
.len
, &rateidx
, 1);
1050 rate
= IEEE80211_RS_RATE(&ni
->ni_rates
, rateidx
);
1052 data
= &sc
->tx_data
[0];
1053 desc
= (struct rum_tx_desc
*)data
->buf
;
1058 if (!IEEE80211_IS_MULTICAST(wh
->i_addr1
)) {
1059 flags
|= RT2573_TX_ACK
;
1061 dur
= ieee80211_txtime(ni
, RUM_ACK_SIZE
,
1062 ieee80211_ack_rate(ni
, rate
), ic
->ic_flags
) +
1064 *(uint16_t *)wh
->i_dur
= htole16(dur
);
1066 /* tell hardware to set timestamp in probe responses */
1068 (IEEE80211_FC0_TYPE_MASK
| IEEE80211_FC0_SUBTYPE_MASK
)) ==
1069 (IEEE80211_FC0_TYPE_MGT
| IEEE80211_FC0_SUBTYPE_PROBE_RESP
))
1070 flags
|= RT2573_TX_TIMESTAMP
;
1073 if (sc
->sc_drvbpf
!= NULL
) {
1074 struct rum_tx_radiotap_header
*tap
= &sc
->sc_txtap
;
1077 tap
->wt_rate
= rate
;
1078 tap
->wt_chan_freq
= htole16(ic
->ic_bss
->ni_chan
->ic_freq
);
1079 tap
->wt_chan_flags
= htole16(ic
->ic_bss
->ni_chan
->ic_flags
);
1080 tap
->wt_antenna
= sc
->tx_ant
;
1082 bpf_ptap(sc
->sc_drvbpf
, m0
, tap
, sc
->sc_txtap_len
);
1085 m_copydata(m0
, 0, m0
->m_pkthdr
.len
, data
->buf
+ RT2573_TX_DESC_SIZE
);
1086 rum_setup_tx_desc(sc
, desc
, flags
, 0, m0
->m_pkthdr
.len
, rate
);
1088 /* Align end on a 4-bytes boundary */
1089 xferlen
= roundup(RT2573_TX_DESC_SIZE
+ m0
->m_pkthdr
.len
, 4);
1092 * No space left in the last URB to store the extra 4 bytes, force
1093 * sending of another URB.
1095 if ((xferlen
% 64) == 0)
1098 DPRINTFN(10, ("sending frame len=%u rate=%u xfer len=%u\n",
1099 m0
->m_pkthdr
.len
+ RT2573_TX_DESC_SIZE
, rate
, xferlen
));
1101 lwkt_serialize_exit(ifp
->if_serializer
);
1103 usbd_setup_xfer(data
->xfer
, sc
->sc_tx_pipeh
, data
, data
->buf
, xferlen
,
1104 USBD_FORCE_SHORT_XFER
| USBD_NO_COPY
, RUM_TX_TIMEOUT
, rum_txeof
);
1106 error
= usbd_transfer(data
->xfer
);
1107 if (error
!= USBD_NORMAL_COMPLETION
&& error
!= USBD_IN_PROGRESS
) {
1116 lwkt_serialize_enter(ifp
->if_serializer
);
1121 rum_start(struct ifnet
*ifp
)
1123 struct rum_softc
*sc
= ifp
->if_softc
;
1124 struct ieee80211com
*ic
= &sc
->sc_ic
;
1126 ASSERT_SERIALIZED(ifp
->if_serializer
);
1128 if (sc
->sc_stopped
) {
1129 ifq_purge(&ifp
->if_snd
);
1135 if ((ifp
->if_flags
& (IFF_RUNNING
| IFF_OACTIVE
)) != IFF_RUNNING
) {
1141 struct ieee80211_node
*ni
;
1144 if (!IF_QEMPTY(&ic
->ic_mgtq
)) {
1145 if (sc
->tx_queued
>= RT2573_TX_LIST_COUNT
) {
1146 ifp
->if_flags
|= IFF_OACTIVE
;
1149 IF_DEQUEUE(&ic
->ic_mgtq
, m0
);
1151 ni
= (struct ieee80211_node
*)m0
->m_pkthdr
.rcvif
;
1152 m0
->m_pkthdr
.rcvif
= NULL
;
1156 if (rum_tx_data(sc
, m0
, ni
) != 0) {
1157 ieee80211_free_node(ni
);
1161 struct ether_header
*eh
;
1163 if (ic
->ic_state
!= IEEE80211_S_RUN
) {
1164 ifq_purge(&ifp
->if_snd
);
1168 if (sc
->tx_queued
>= RT2573_TX_LIST_COUNT
) {
1169 ifp
->if_flags
|= IFF_OACTIVE
;
1173 m0
= ifq_dequeue(&ifp
->if_snd
, NULL
);
1177 if (m0
->m_len
< sizeof(struct ether_header
)) {
1178 m0
= m_pullup(m0
, sizeof(struct ether_header
));
1184 eh
= mtod(m0
, struct ether_header
*);
1186 ni
= ieee80211_find_txnode(ic
, eh
->ether_dhost
);
1194 m0
= ieee80211_encap(ic
, m0
, ni
);
1196 ieee80211_free_node(ni
);
1200 if (ic
->ic_rawbpf
!= NULL
)
1201 bpf_mtap(ic
->ic_rawbpf
, m0
);
1203 if (rum_tx_data(sc
, m0
, ni
) != 0) {
1204 ieee80211_free_node(ni
);
1210 sc
->sc_tx_timer
= 5;
1218 rum_watchdog(struct ifnet
*ifp
)
1220 struct rum_softc
*sc
= ifp
->if_softc
;
1222 ASSERT_SERIALIZED(ifp
->if_serializer
);
1228 if (sc
->sc_tx_timer
> 0) {
1229 if (--sc
->sc_tx_timer
== 0) {
1230 kprintf("%s: device timeout\n", device_get_nameunit(sc
->sc_dev
));
1231 /*rum_init(sc); XXX needs a process context! */
1240 ieee80211_watchdog(&sc
->sc_ic
);
1246 rum_ioctl(struct ifnet
*ifp
, u_long cmd
, caddr_t data
, struct ucred
*cr
)
1248 struct rum_softc
*sc
= ifp
->if_softc
;
1249 struct ieee80211com
*ic
= &sc
->sc_ic
;
1252 ASSERT_SERIALIZED(ifp
->if_serializer
);
1258 if (ifp
->if_flags
& IFF_UP
) {
1259 if (ifp
->if_flags
& IFF_RUNNING
) {
1260 lwkt_serialize_exit(ifp
->if_serializer
);
1261 rum_update_promisc(sc
);
1262 lwkt_serialize_enter(ifp
->if_serializer
);
1267 if (ifp
->if_flags
& IFF_RUNNING
)
1272 error
= ieee80211_ioctl(ic
, cmd
, data
, cr
);
1276 if (error
== ENETRESET
) {
1277 struct ieee80211req
*ireq
= (struct ieee80211req
*)data
;
1279 if (cmd
== SIOCS80211
&&
1280 ireq
->i_type
== IEEE80211_IOC_CHANNEL
&&
1281 ic
->ic_opmode
== IEEE80211_M_MONITOR
) {
1283 * This allows for fast channel switching in monitor
1284 * mode (used by kismet). In IBSS mode, we must
1285 * explicitly reset the interface to generate a new
1288 lwkt_serialize_exit(ifp
->if_serializer
);
1289 rum_set_chan(sc
, ic
->ic_ibss_chan
);
1290 lwkt_serialize_enter(ifp
->if_serializer
);
1291 } else if ((ifp
->if_flags
& (IFF_UP
| IFF_RUNNING
)) ==
1292 (IFF_UP
| IFF_RUNNING
)) {
1303 rum_eeprom_read(struct rum_softc
*sc
, uint16_t addr
, void *buf
, int len
)
1305 usb_device_request_t req
;
1308 req
.bmRequestType
= UT_READ_VENDOR_DEVICE
;
1309 req
.bRequest
= RT2573_READ_EEPROM
;
1310 USETW(req
.wValue
, 0);
1311 USETW(req
.wIndex
, addr
);
1312 USETW(req
.wLength
, len
);
1314 error
= usbd_do_request(sc
->sc_udev
, &req
, buf
);
1316 kprintf("%s: could not read EEPROM: %s\n",
1317 device_get_nameunit(sc
->sc_dev
), usbd_errstr(error
));
1322 rum_read(struct rum_softc
*sc
, uint16_t reg
)
1326 rum_read_multi(sc
, reg
, &val
, sizeof val
);
1328 return le32toh(val
);
1332 rum_read_multi(struct rum_softc
*sc
, uint16_t reg
, void *buf
, int len
)
1334 usb_device_request_t req
;
1337 req
.bmRequestType
= UT_READ_VENDOR_DEVICE
;
1338 req
.bRequest
= RT2573_READ_MULTI_MAC
;
1339 USETW(req
.wValue
, 0);
1340 USETW(req
.wIndex
, reg
);
1341 USETW(req
.wLength
, len
);
1343 error
= usbd_do_request(sc
->sc_udev
, &req
, buf
);
1345 kprintf("%s: could not multi read MAC register: %s\n",
1346 device_get_nameunit(sc
->sc_dev
), usbd_errstr(error
));
1351 rum_write(struct rum_softc
*sc
, uint16_t reg
, uint32_t val
)
1353 uint32_t tmp
= htole32(val
);
1355 rum_write_multi(sc
, reg
, &tmp
, sizeof tmp
);
1359 rum_write_multi(struct rum_softc
*sc
, uint16_t reg
, void *buf
, size_t len
)
1361 usb_device_request_t req
;
1364 req
.bmRequestType
= UT_WRITE_VENDOR_DEVICE
;
1365 req
.bRequest
= RT2573_WRITE_MULTI_MAC
;
1366 USETW(req
.wValue
, 0);
1367 USETW(req
.wIndex
, reg
);
1368 USETW(req
.wLength
, len
);
1370 error
= usbd_do_request(sc
->sc_udev
, &req
, buf
);
1372 kprintf("%s: could not multi write MAC register: %s\n",
1373 device_get_nameunit(sc
->sc_dev
), usbd_errstr(error
));
1378 rum_bbp_write(struct rum_softc
*sc
, uint8_t reg
, uint8_t val
)
1383 for (ntries
= 0; ntries
< 5; ntries
++) {
1384 if (!(rum_read(sc
, RT2573_PHY_CSR3
) & RT2573_BBP_BUSY
))
1388 kprintf("%s: could not write to BBP\n", device_get_nameunit(sc
->sc_dev
));
1392 tmp
= RT2573_BBP_BUSY
| (reg
& 0x7f) << 8 | val
;
1393 rum_write(sc
, RT2573_PHY_CSR3
, tmp
);
1397 rum_bbp_read(struct rum_softc
*sc
, uint8_t reg
)
1402 for (ntries
= 0; ntries
< 5; ntries
++) {
1403 if (!(rum_read(sc
, RT2573_PHY_CSR3
) & RT2573_BBP_BUSY
))
1407 kprintf("%s: could not read BBP\n", device_get_nameunit(sc
->sc_dev
));
1411 val
= RT2573_BBP_BUSY
| RT2573_BBP_READ
| reg
<< 8;
1412 rum_write(sc
, RT2573_PHY_CSR3
, val
);
1414 for (ntries
= 0; ntries
< 100; ntries
++) {
1415 val
= rum_read(sc
, RT2573_PHY_CSR3
);
1416 if (!(val
& RT2573_BBP_BUSY
))
1421 kprintf("%s: could not read BBP\n", device_get_nameunit(sc
->sc_dev
));
1426 rum_rf_write(struct rum_softc
*sc
, uint8_t reg
, uint32_t val
)
1431 for (ntries
= 0; ntries
< 5; ntries
++) {
1432 if (!(rum_read(sc
, RT2573_PHY_CSR4
) & RT2573_RF_BUSY
))
1436 kprintf("%s: could not write to RF\n", device_get_nameunit(sc
->sc_dev
));
1440 tmp
= RT2573_RF_BUSY
| RT2573_RF_20BIT
| (val
& 0xfffff) << 2 |
1442 rum_write(sc
, RT2573_PHY_CSR4
, tmp
);
1444 /* remember last written value in sc */
1445 sc
->rf_regs
[reg
] = val
;
1447 DPRINTFN(15, ("RF R[%u] <- 0x%05x\n", reg
& 3, val
& 0xfffff));
1451 rum_select_antenna(struct rum_softc
*sc
)
1453 uint8_t bbp4
, bbp77
;
1456 bbp4
= rum_bbp_read(sc
, 4);
1457 bbp77
= rum_bbp_read(sc
, 77);
1461 /* make sure Rx is disabled before switching antenna */
1462 tmp
= rum_read(sc
, RT2573_TXRX_CSR0
);
1463 rum_write(sc
, RT2573_TXRX_CSR0
, tmp
| RT2573_DISABLE_RX
);
1465 rum_bbp_write(sc
, 4, bbp4
);
1466 rum_bbp_write(sc
, 77, bbp77
);
1468 rum_write(sc
, RT2573_TXRX_CSR0
, tmp
);
1472 * Enable multi-rate retries for frames sent at OFDM rates.
1473 * In 802.11b/g mode, allow fallback to CCK rates.
1476 rum_enable_mrr(struct rum_softc
*sc
)
1478 struct ieee80211com
*ic
= &sc
->sc_ic
;
1481 tmp
= rum_read(sc
, RT2573_TXRX_CSR4
);
1483 tmp
&= ~RT2573_MRR_CCK_FALLBACK
;
1484 if (!IEEE80211_IS_CHAN_5GHZ(ic
->ic_curchan
))
1485 tmp
|= RT2573_MRR_CCK_FALLBACK
;
1486 tmp
|= RT2573_MRR_ENABLED
;
1488 rum_write(sc
, RT2573_TXRX_CSR4
, tmp
);
1492 rum_set_txpreamble(struct rum_softc
*sc
)
1496 tmp
= rum_read(sc
, RT2573_TXRX_CSR4
);
1498 tmp
&= ~RT2573_SHORT_PREAMBLE
;
1499 if (sc
->sc_ic
.ic_flags
& IEEE80211_F_SHPREAMBLE
)
1500 tmp
|= RT2573_SHORT_PREAMBLE
;
1502 rum_write(sc
, RT2573_TXRX_CSR4
, tmp
);
1506 rum_set_basicrates(struct rum_softc
*sc
)
1508 struct ieee80211com
*ic
= &sc
->sc_ic
;
1510 /* update basic rate set */
1511 if (ic
->ic_curmode
== IEEE80211_MODE_11B
) {
1512 /* 11b basic rates: 1, 2Mbps */
1513 rum_write(sc
, RT2573_TXRX_CSR5
, 0x3);
1514 } else if (IEEE80211_IS_CHAN_5GHZ(ic
->ic_bss
->ni_chan
)) {
1515 /* 11a basic rates: 6, 12, 24Mbps */
1516 rum_write(sc
, RT2573_TXRX_CSR5
, 0x150);
1518 /* 11g basic rates: 1, 2, 5.5, 11, 6, 12, 24Mbps */
1519 rum_write(sc
, RT2573_TXRX_CSR5
, 0x15f);
1524 * Reprogram MAC/BBP to switch to a new band. Values taken from the reference
1528 rum_select_band(struct rum_softc
*sc
, struct ieee80211_channel
*c
)
1530 uint8_t bbp17
, bbp35
, bbp96
, bbp97
, bbp98
, bbp104
;
1533 /* update all BBP registers that depend on the band */
1534 bbp17
= 0x20; bbp96
= 0x48; bbp104
= 0x2c;
1535 bbp35
= 0x50; bbp97
= 0x48; bbp98
= 0x48;
1536 if (IEEE80211_IS_CHAN_5GHZ(c
)) {
1537 bbp17
+= 0x08; bbp96
+= 0x10; bbp104
+= 0x0c;
1538 bbp35
+= 0x10; bbp97
+= 0x10; bbp98
+= 0x10;
1540 if ((IEEE80211_IS_CHAN_2GHZ(c
) && sc
->ext_2ghz_lna
) ||
1541 (IEEE80211_IS_CHAN_5GHZ(c
) && sc
->ext_5ghz_lna
)) {
1542 bbp17
+= 0x10; bbp96
+= 0x10; bbp104
+= 0x10;
1546 rum_bbp_write(sc
, 17, bbp17
);
1547 rum_bbp_write(sc
, 96, bbp96
);
1548 rum_bbp_write(sc
, 104, bbp104
);
1550 if ((IEEE80211_IS_CHAN_2GHZ(c
) && sc
->ext_2ghz_lna
) ||
1551 (IEEE80211_IS_CHAN_5GHZ(c
) && sc
->ext_5ghz_lna
)) {
1552 rum_bbp_write(sc
, 75, 0x80);
1553 rum_bbp_write(sc
, 86, 0x80);
1554 rum_bbp_write(sc
, 88, 0x80);
1557 rum_bbp_write(sc
, 35, bbp35
);
1558 rum_bbp_write(sc
, 97, bbp97
);
1559 rum_bbp_write(sc
, 98, bbp98
);
1561 tmp
= rum_read(sc
, RT2573_PHY_CSR0
);
1562 tmp
&= ~(RT2573_PA_PE_2GHZ
| RT2573_PA_PE_5GHZ
);
1563 if (IEEE80211_IS_CHAN_2GHZ(c
))
1564 tmp
|= RT2573_PA_PE_2GHZ
;
1566 tmp
|= RT2573_PA_PE_5GHZ
;
1567 rum_write(sc
, RT2573_PHY_CSR0
, tmp
);
1571 rum_set_chan(struct rum_softc
*sc
, struct ieee80211_channel
*c
)
1573 struct ieee80211com
*ic
= &sc
->sc_ic
;
1574 const struct rfprog
*rfprog
;
1575 uint8_t bbp3
, bbp94
= RT2573_BBPR94_DEFAULT
;
1579 chan
= ieee80211_chan2ieee(ic
, c
);
1580 if (chan
== 0 || chan
== IEEE80211_CHAN_ANY
)
1583 /* select the appropriate RF settings based on what EEPROM says */
1584 rfprog
= (sc
->rf_rev
== RT2573_RF_5225
||
1585 sc
->rf_rev
== RT2573_RF_2527
) ? rum_rf5225
: rum_rf5226
;
1587 /* find the settings for this channel (we know it exists) */
1588 for (i
= 0; rfprog
[i
].chan
!= chan
; i
++)
1591 power
= sc
->txpow
[i
];
1595 } else if (power
> 31) {
1596 bbp94
+= power
- 31;
1601 * If we are switching from the 2GHz band to the 5GHz band or
1602 * vice-versa, BBP registers need to be reprogrammed.
1604 if (c
->ic_flags
!= sc
->sc_curchan
->ic_flags
) {
1605 rum_select_band(sc
, c
);
1606 rum_select_antenna(sc
);
1610 rum_rf_write(sc
, RT2573_RF1
, rfprog
[i
].r1
);
1611 rum_rf_write(sc
, RT2573_RF2
, rfprog
[i
].r2
);
1612 rum_rf_write(sc
, RT2573_RF3
, rfprog
[i
].r3
| power
<< 7);
1613 rum_rf_write(sc
, RT2573_RF4
, rfprog
[i
].r4
| sc
->rffreq
<< 10);
1615 rum_rf_write(sc
, RT2573_RF1
, rfprog
[i
].r1
);
1616 rum_rf_write(sc
, RT2573_RF2
, rfprog
[i
].r2
);
1617 rum_rf_write(sc
, RT2573_RF3
, rfprog
[i
].r3
| power
<< 7 | 1);
1618 rum_rf_write(sc
, RT2573_RF4
, rfprog
[i
].r4
| sc
->rffreq
<< 10);
1620 rum_rf_write(sc
, RT2573_RF1
, rfprog
[i
].r1
);
1621 rum_rf_write(sc
, RT2573_RF2
, rfprog
[i
].r2
);
1622 rum_rf_write(sc
, RT2573_RF3
, rfprog
[i
].r3
| power
<< 7);
1623 rum_rf_write(sc
, RT2573_RF4
, rfprog
[i
].r4
| sc
->rffreq
<< 10);
1627 /* enable smart mode for MIMO-capable RFs */
1628 bbp3
= rum_bbp_read(sc
, 3);
1630 if (sc
->rf_rev
== RT2573_RF_5225
|| sc
->rf_rev
== RT2573_RF_2527
)
1631 bbp3
&= ~RT2573_SMART_MODE
;
1633 bbp3
|= RT2573_SMART_MODE
;
1635 rum_bbp_write(sc
, 3, bbp3
);
1637 if (bbp94
!= RT2573_BBPR94_DEFAULT
)
1638 rum_bbp_write(sc
, 94, bbp94
);
1640 sc
->sc_sifs
= IEEE80211_IS_CHAN_5GHZ(c
) ? IEEE80211_DUR_OFDM_SIFS
1641 : IEEE80211_DUR_SIFS
;
1645 * Enable TSF synchronization and tell h/w to start sending beacons for IBSS
1646 * and HostAP operating modes.
1649 rum_enable_tsf_sync(struct rum_softc
*sc
)
1651 struct ieee80211com
*ic
= &sc
->sc_ic
;
1654 if (ic
->ic_opmode
!= IEEE80211_M_STA
) {
1656 * Change default 16ms TBTT adjustment to 8ms.
1657 * Must be done before enabling beacon generation.
1659 rum_write(sc
, RT2573_TXRX_CSR10
, 1 << 12 | 8);
1662 tmp
= rum_read(sc
, RT2573_TXRX_CSR9
) & 0xff000000;
1664 /* set beacon interval (in 1/16ms unit) */
1665 tmp
|= ic
->ic_bss
->ni_intval
* 16;
1667 tmp
|= RT2573_TSF_TICKING
| RT2573_ENABLE_TBTT
;
1668 if (ic
->ic_opmode
== IEEE80211_M_STA
)
1669 tmp
|= RT2573_TSF_MODE(1);
1671 tmp
|= RT2573_TSF_MODE(2) | RT2573_GENERATE_BEACON
;
1673 rum_write(sc
, RT2573_TXRX_CSR9
, tmp
);
1677 rum_update_slot(struct rum_softc
*sc
)
1679 struct ieee80211com
*ic
= &sc
->sc_ic
;
1683 slottime
= (ic
->ic_flags
& IEEE80211_F_SHSLOT
) ? 9 : 20;
1685 tmp
= rum_read(sc
, RT2573_MAC_CSR9
);
1686 tmp
= (tmp
& ~0xff) | slottime
;
1687 rum_write(sc
, RT2573_MAC_CSR9
, tmp
);
1689 DPRINTF(("setting slot time to %uus\n", slottime
));
1693 rum_set_bssid(struct rum_softc
*sc
, const uint8_t *bssid
)
1697 tmp
= bssid
[0] | bssid
[1] << 8 | bssid
[2] << 16 | bssid
[3] << 24;
1698 rum_write(sc
, RT2573_MAC_CSR4
, tmp
);
1700 tmp
= bssid
[4] | bssid
[5] << 8 | RT2573_ONE_BSSID
<< 16;
1701 rum_write(sc
, RT2573_MAC_CSR5
, tmp
);
1705 rum_set_macaddr(struct rum_softc
*sc
, const uint8_t *addr
)
1709 tmp
= addr
[0] | addr
[1] << 8 | addr
[2] << 16 | addr
[3] << 24;
1710 rum_write(sc
, RT2573_MAC_CSR2
, tmp
);
1712 tmp
= addr
[4] | addr
[5] << 8 | 0xff << 16;
1713 rum_write(sc
, RT2573_MAC_CSR3
, tmp
);
1717 rum_update_promisc(struct rum_softc
*sc
)
1719 struct ifnet
*ifp
= &sc
->sc_ic
.ic_if
;
1722 tmp
= rum_read(sc
, RT2573_TXRX_CSR0
);
1724 tmp
&= ~RT2573_DROP_NOT_TO_ME
;
1725 if (!(ifp
->if_flags
& IFF_PROMISC
))
1726 tmp
|= RT2573_DROP_NOT_TO_ME
;
1728 rum_write(sc
, RT2573_TXRX_CSR0
, tmp
);
1730 DPRINTF(("%s promiscuous mode\n", (ifp
->if_flags
& IFF_PROMISC
) ?
1731 "entering" : "leaving"));
1738 case RT2573_RF_2527
: return "RT2527 (MIMO XR)";
1739 case RT2573_RF_2528
: return "RT2528";
1740 case RT2573_RF_5225
: return "RT5225 (MIMO XR)";
1741 case RT2573_RF_5226
: return "RT5226";
1742 default: return "unknown";
1747 rum_read_eeprom(struct rum_softc
*sc
)
1749 struct ieee80211com
*ic
= &sc
->sc_ic
;
1755 /* read MAC/BBP type */
1756 rum_eeprom_read(sc
, RT2573_EEPROM_MACBBP
, &val
, 2);
1757 sc
->macbbp_rev
= le16toh(val
);
1759 /* read MAC address */
1760 rum_eeprom_read(sc
, RT2573_EEPROM_ADDRESS
, ic
->ic_myaddr
, 6);
1762 rum_eeprom_read(sc
, RT2573_EEPROM_ANTENNA
, &val
, 2);
1764 sc
->rf_rev
= (val
>> 11) & 0x1f;
1765 sc
->hw_radio
= (val
>> 10) & 0x1;
1766 sc
->rx_ant
= (val
>> 4) & 0x3;
1767 sc
->tx_ant
= (val
>> 2) & 0x3;
1768 sc
->nb_ant
= val
& 0x3;
1770 DPRINTF(("RF revision=%d\n", sc
->rf_rev
));
1772 rum_eeprom_read(sc
, RT2573_EEPROM_CONFIG2
, &val
, 2);
1774 sc
->ext_5ghz_lna
= (val
>> 6) & 0x1;
1775 sc
->ext_2ghz_lna
= (val
>> 4) & 0x1;
1777 DPRINTF(("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n",
1778 sc
->ext_2ghz_lna
, sc
->ext_5ghz_lna
));
1780 rum_eeprom_read(sc
, RT2573_EEPROM_RSSI_2GHZ_OFFSET
, &val
, 2);
1782 if ((val
& 0xff) != 0xff)
1783 sc
->rssi_2ghz_corr
= (int8_t)(val
& 0xff); /* signed */
1785 /* Only [-10, 10] is valid */
1786 if (sc
->rssi_2ghz_corr
< -10 || sc
->rssi_2ghz_corr
> 10)
1787 sc
->rssi_2ghz_corr
= 0;
1789 rum_eeprom_read(sc
, RT2573_EEPROM_RSSI_5GHZ_OFFSET
, &val
, 2);
1791 if ((val
& 0xff) != 0xff)
1792 sc
->rssi_5ghz_corr
= (int8_t)(val
& 0xff); /* signed */
1794 /* Only [-10, 10] is valid */
1795 if (sc
->rssi_5ghz_corr
< -10 || sc
->rssi_5ghz_corr
> 10)
1796 sc
->rssi_5ghz_corr
= 0;
1798 if (sc
->ext_2ghz_lna
)
1799 sc
->rssi_2ghz_corr
-= 14;
1800 if (sc
->ext_5ghz_lna
)
1801 sc
->rssi_5ghz_corr
-= 14;
1803 DPRINTF(("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n",
1804 sc
->rssi_2ghz_corr
, sc
->rssi_5ghz_corr
));
1806 rum_eeprom_read(sc
, RT2573_EEPROM_FREQ_OFFSET
, &val
, 2);
1808 if ((val
& 0xff) != 0xff)
1809 sc
->rffreq
= val
& 0xff;
1811 DPRINTF(("RF freq=%d\n", sc
->rffreq
));
1813 /* read Tx power for all a/b/g channels */
1814 rum_eeprom_read(sc
, RT2573_EEPROM_TXPOWER
, sc
->txpow
, 14);
1815 /* XXX default Tx power for 802.11a channels */
1816 memset(sc
->txpow
+ 14, 24, sizeof (sc
->txpow
) - 14);
1818 for (i
= 0; i
< 14; i
++)
1819 DPRINTF(("Channel=%d Tx power=%d\n", i
+ 1, sc
->txpow
[i
]));
1822 /* read default values for BBP registers */
1823 rum_eeprom_read(sc
, RT2573_EEPROM_BBP_BASE
, sc
->bbp_prom
, 2 * 16);
1825 for (i
= 0; i
< 14; i
++) {
1826 if (sc
->bbp_prom
[i
].reg
== 0 || sc
->bbp_prom
[i
].reg
== 0xff)
1828 DPRINTF(("BBP R%d=%02x\n", sc
->bbp_prom
[i
].reg
,
1829 sc
->bbp_prom
[i
].val
));
1835 rum_bbp_init(struct rum_softc
*sc
)
1837 #define N(a) (sizeof (a) / sizeof ((a)[0]))
1841 /* wait for BBP to be ready */
1842 for (ntries
= 0; ntries
< 100; ntries
++) {
1843 val
= rum_bbp_read(sc
, 0);
1844 if (val
!= 0 && val
!= 0xff)
1848 if (ntries
== 100) {
1849 kprintf("%s: timeout waiting for BBP\n",
1850 device_get_nameunit(sc
->sc_dev
));
1854 /* initialize BBP registers to default values */
1855 for (i
= 0; i
< N(rum_def_bbp
); i
++)
1856 rum_bbp_write(sc
, rum_def_bbp
[i
].reg
, rum_def_bbp
[i
].val
);
1858 /* write vendor-specific BBP values (from EEPROM) */
1859 for (i
= 0; i
< 16; i
++) {
1860 if (sc
->bbp_prom
[i
].reg
== 0 || sc
->bbp_prom
[i
].reg
== 0xff)
1862 rum_bbp_write(sc
, sc
->bbp_prom
[i
].reg
, sc
->bbp_prom
[i
].val
);
1872 #define N(a) (sizeof(a) / sizeof((a)[0]))
1873 struct rum_softc
*sc
= xsc
;
1874 struct ieee80211com
*ic
= &sc
->sc_ic
;
1875 struct ifnet
*ifp
= &ic
->ic_if
;
1876 struct rum_rx_data
*data
;
1878 usbd_status usb_err
;
1879 int i
, ntries
, error
;
1881 ASSERT_SERIALIZED(ifp
->if_serializer
);
1888 lwkt_serialize_exit(ifp
->if_serializer
);
1890 /* initialize MAC registers to default values */
1891 for (i
= 0; i
< N(rum_def_mac
); i
++)
1892 rum_write(sc
, rum_def_mac
[i
].reg
, rum_def_mac
[i
].val
);
1894 /* set host ready */
1895 rum_write(sc
, RT2573_MAC_CSR1
, 3);
1896 rum_write(sc
, RT2573_MAC_CSR1
, 0);
1898 /* wait for BBP/RF to wakeup */
1899 for (ntries
= 0; ntries
< 1000; ntries
++) {
1900 if (rum_read(sc
, RT2573_MAC_CSR12
) & 8)
1902 rum_write(sc
, RT2573_MAC_CSR12
, 4); /* force wakeup */
1905 if (ntries
== 1000) {
1906 kprintf("%s: timeout waiting for BBP/RF to wakeup\n",
1907 device_get_nameunit(sc
->sc_dev
));
1912 error
= rum_bbp_init(sc
);
1916 /* select default channel */
1917 sc
->sc_curchan
= ic
->ic_curchan
= ic
->ic_ibss_chan
;
1919 rum_select_band(sc
, sc
->sc_curchan
);
1920 rum_select_antenna(sc
);
1921 rum_set_chan(sc
, sc
->sc_curchan
);
1923 /* clear STA registers */
1924 rum_read_multi(sc
, RT2573_STA_CSR0
, sc
->sta
, sizeof sc
->sta
);
1926 IEEE80211_ADDR_COPY(ic
->ic_myaddr
, IF_LLADDR(ifp
));
1927 rum_set_macaddr(sc
, ic
->ic_myaddr
);
1929 /* initialize ASIC */
1930 rum_write(sc
, RT2573_MAC_CSR1
, 4);
1933 * Allocate xfer for AMRR statistics requests.
1935 sc
->stats_xfer
= usbd_alloc_xfer(sc
->sc_udev
);
1936 if (sc
->stats_xfer
== NULL
) {
1937 kprintf("%s: could not allocate AMRR xfer\n",
1938 device_get_nameunit(sc
->sc_dev
));
1944 * Open Tx and Rx USB bulk pipes.
1946 usb_err
= usbd_open_pipe(sc
->sc_iface
, sc
->sc_tx_no
, USBD_EXCLUSIVE_USE
,
1948 if (usb_err
!= USBD_NORMAL_COMPLETION
) {
1949 kprintf("%s: could not open Tx pipe: %s\n",
1950 device_get_nameunit(sc
->sc_dev
), usbd_errstr(usb_err
));
1955 usb_err
= usbd_open_pipe(sc
->sc_iface
, sc
->sc_rx_no
, USBD_EXCLUSIVE_USE
,
1957 if (usb_err
!= USBD_NORMAL_COMPLETION
) {
1958 kprintf("%s: could not open Rx pipe: %s\n",
1959 device_get_nameunit(sc
->sc_dev
), usbd_errstr(usb_err
));
1965 * Allocate Tx and Rx xfer queues.
1967 error
= rum_alloc_tx_list(sc
);
1969 kprintf("%s: could not allocate Tx list\n",
1970 device_get_nameunit(sc
->sc_dev
));
1974 error
= rum_alloc_rx_list(sc
);
1976 kprintf("%s: could not allocate Rx list\n",
1977 device_get_nameunit(sc
->sc_dev
));
1982 * Start up the receive pipe.
1984 for (i
= 0; i
< RT2573_RX_LIST_COUNT
; i
++) {
1985 data
= &sc
->rx_data
[i
];
1987 usbd_setup_xfer(data
->xfer
, sc
->sc_rx_pipeh
, data
, data
->buf
,
1988 MCLBYTES
, USBD_SHORT_XFER_OK
, USBD_NO_TIMEOUT
, rum_rxeof
);
1989 usbd_transfer(data
->xfer
);
1992 /* update Rx filter */
1993 tmp
= rum_read(sc
, RT2573_TXRX_CSR0
) & 0xffff;
1995 tmp
|= RT2573_DROP_PHY_ERROR
| RT2573_DROP_CRC_ERROR
;
1996 if (ic
->ic_opmode
!= IEEE80211_M_MONITOR
) {
1997 tmp
|= RT2573_DROP_CTL
| RT2573_DROP_VER_ERROR
|
1999 if (ic
->ic_opmode
!= IEEE80211_M_HOSTAP
)
2000 tmp
|= RT2573_DROP_TODS
;
2001 if (!(ifp
->if_flags
& IFF_PROMISC
))
2002 tmp
|= RT2573_DROP_NOT_TO_ME
;
2004 rum_write(sc
, RT2573_TXRX_CSR0
, tmp
);
2006 lwkt_serialize_enter(ifp
->if_serializer
);
2011 ifp
->if_flags
&= ~IFF_OACTIVE
;
2012 ifp
->if_flags
|= IFF_RUNNING
;
2014 if (ic
->ic_opmode
!= IEEE80211_M_MONITOR
) {
2015 if (ic
->ic_roaming
!= IEEE80211_ROAMING_MANUAL
)
2016 ieee80211_new_state(ic
, IEEE80211_S_SCAN
, -1);
2018 ieee80211_new_state(ic
, IEEE80211_S_RUN
, -1);
2027 rum_stop(struct rum_softc
*sc
)
2029 struct ieee80211com
*ic
= &sc
->sc_ic
;
2030 struct ifnet
*ifp
= &ic
->ic_if
;
2033 ASSERT_SERIALIZED(ifp
->if_serializer
);
2037 ifp
->if_flags
&= ~(IFF_RUNNING
| IFF_OACTIVE
);
2040 ieee80211_new_state(ic
, IEEE80211_S_INIT
, -1); /* free all nodes */
2042 sc
->sc_tx_timer
= 0;
2045 lwkt_serialize_exit(ifp
->if_serializer
);
2048 tmp
= rum_read(sc
, RT2573_TXRX_CSR0
);
2049 rum_write(sc
, RT2573_TXRX_CSR0
, tmp
| RT2573_DISABLE_RX
);
2052 rum_write(sc
, RT2573_MAC_CSR1
, 3);
2053 rum_write(sc
, RT2573_MAC_CSR1
, 0);
2055 if (sc
->stats_xfer
!= NULL
) {
2056 usbd_free_xfer(sc
->stats_xfer
);
2057 sc
->stats_xfer
= NULL
;
2060 if (sc
->sc_rx_pipeh
!= NULL
) {
2061 usbd_abort_pipe(sc
->sc_rx_pipeh
);
2062 usbd_close_pipe(sc
->sc_rx_pipeh
);
2063 sc
->sc_rx_pipeh
= NULL
;
2066 if (sc
->sc_tx_pipeh
!= NULL
) {
2067 usbd_abort_pipe(sc
->sc_tx_pipeh
);
2068 usbd_close_pipe(sc
->sc_tx_pipeh
);
2069 sc
->sc_tx_pipeh
= NULL
;
2072 lwkt_serialize_enter(ifp
->if_serializer
);
2074 rum_free_rx_list(sc
);
2075 rum_free_tx_list(sc
);
2081 rum_load_microcode(struct rum_softc
*sc
, const uint8_t *ucode
, size_t size
)
2083 usb_device_request_t req
;
2084 uint16_t reg
= RT2573_MCU_CODE_BASE
;
2087 /* copy firmware image into NIC */
2088 for (; size
>= 4; reg
+= 4, ucode
+= 4, size
-= 4)
2089 rum_write(sc
, reg
, UGETDW(ucode
));
2091 req
.bmRequestType
= UT_WRITE_VENDOR_DEVICE
;
2092 req
.bRequest
= RT2573_MCU_CNTL
;
2093 USETW(req
.wValue
, RT2573_MCU_RUN
);
2094 USETW(req
.wIndex
, 0);
2095 USETW(req
.wLength
, 0);
2097 error
= usbd_do_request(sc
->sc_udev
, &req
, NULL
);
2099 kprintf("%s: could not run firmware: %s\n",
2100 device_get_nameunit(sc
->sc_dev
), usbd_errstr(error
));
2106 rum_prepare_beacon(struct rum_softc
*sc
)
2108 struct ieee80211com
*ic
= &sc
->sc_ic
;
2109 struct ifnet
*ifp
= &ic
->ic_if
;
2110 struct ieee80211_beacon_offsets bo
;
2111 struct rum_tx_desc desc
;
2115 lwkt_serialize_enter(ifp
->if_serializer
);
2116 m0
= ieee80211_beacon_alloc(ic
, ic
->ic_bss
, &bo
);
2117 lwkt_serialize_exit(ifp
->if_serializer
);
2120 if_printf(&ic
->ic_if
, "could not allocate beacon frame\n");
2124 /* send beacons at the lowest available rate */
2125 rate
= IEEE80211_IS_CHAN_5GHZ(ic
->ic_bss
->ni_chan
) ? 12 : 2;
2127 rum_setup_tx_desc(sc
, &desc
, RT2573_TX_TIMESTAMP
, RT2573_TX_HWSEQ
,
2128 m0
->m_pkthdr
.len
, rate
);
2130 /* copy the first 24 bytes of Tx descriptor into NIC memory */
2131 rum_write_multi(sc
, RT2573_HW_BEACON_BASE0
, (uint8_t *)&desc
, 24);
2133 /* copy beacon header and payload into NIC memory */
2134 rum_write_multi(sc
, RT2573_HW_BEACON_BASE0
+ 24, mtod(m0
, uint8_t *),
2143 rum_stats_timeout(void *arg
)
2145 struct rum_softc
*sc
= arg
;
2146 usb_device_request_t req
;
2154 * Asynchronously read statistic registers (cleared by read).
2156 req
.bmRequestType
= UT_READ_VENDOR_DEVICE
;
2157 req
.bRequest
= RT2573_READ_MULTI_MAC
;
2158 USETW(req
.wValue
, 0);
2159 USETW(req
.wIndex
, RT2573_STA_CSR0
);
2160 USETW(req
.wLength
, sizeof(sc
->sta
));
2162 usbd_setup_default_xfer(sc
->stats_xfer
, sc
->sc_udev
, sc
,
2163 USBD_DEFAULT_TIMEOUT
, &req
,
2164 sc
->sta
, sizeof(sc
->sta
), 0,
2166 usbd_transfer(sc
->stats_xfer
);
2172 rum_stats_update(usbd_xfer_handle xfer
, usbd_private_handle priv
,
2175 struct rum_softc
*sc
= (struct rum_softc
*)priv
;
2176 struct ifnet
*ifp
= &sc
->sc_ic
.ic_if
;
2177 struct ieee80211_ratectl_stats
*stats
= &sc
->sc_stats
;
2179 if (status
!= USBD_NORMAL_COMPLETION
) {
2180 kprintf("%s: could not retrieve Tx statistics - cancelling "
2181 "automatic rate control\n", device_get_nameunit(sc
->sc_dev
));
2187 /* count TX retry-fail as Tx errors */
2188 ifp
->if_oerrors
+= RUM_TX_PKT_FAIL(sc
);
2190 stats
->stats_pkt_noretry
+= RUM_TX_PKT_NO_RETRY(sc
);
2191 stats
->stats_pkt_ok
+= RUM_TX_PKT_NO_RETRY(sc
) +
2192 RUM_TX_PKT_ONE_RETRY(sc
) +
2193 RUM_TX_PKT_MULTI_RETRY(sc
);
2194 stats
->stats_pkt_err
+= RUM_TX_PKT_FAIL(sc
);
2196 stats
->stats_retries
+= RUM_TX_PKT_ONE_RETRY(sc
);
2199 * XXX Estimated average:
2200 * Actual number of retries for each packet should belong to
2201 * [2, RUM_TX_SHORT_RETRY_MAX]
2203 stats
->stats_retries
+= RUM_TX_PKT_MULTI_RETRY(sc
) *
2204 ((2 + RUM_TX_SHORT_RETRY_MAX
) / 2);
2206 stats
->stats_retries
+= RUM_TX_PKT_MULTI_RETRY(sc
);
2208 stats
->stats_retries
+= RUM_TX_PKT_FAIL(sc
) * RUM_TX_SHORT_RETRY_MAX
;
2210 callout_reset(&sc
->stats_ch
, 4 * hz
/ 5, rum_stats_timeout
, sc
);
2216 rum_stats(struct ieee80211com
*ic
, struct ieee80211_node
*ni __unused
,
2217 struct ieee80211_ratectl_stats
*stats
)
2219 struct ifnet
*ifp
= &ic
->ic_if
;
2220 struct rum_softc
*sc
= ifp
->if_softc
;
2222 ASSERT_SERIALIZED(ifp
->if_serializer
);
2224 bcopy(&sc
->sc_stats
, stats
, sizeof(*stats
));
2225 bzero(&sc
->sc_stats
, sizeof(sc
->sc_stats
));
2229 rum_ratectl_attach(struct ieee80211com
*ic
, u_int rc
)
2231 struct rum_softc
*sc
= ic
->ic_if
.if_softc
;
2234 case IEEE80211_RATECTL_ONOE
:
2235 return &sc
->sc_onoe_param
;
2236 case IEEE80211_RATECTL_NONE
:
2237 /* This could only happen during detaching */
2240 panic("unknown rate control algo %u\n", rc
);
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)