xref: /openbsd/sys/dev/usb/if_rum.c (revision 3cab2bb3)
1 /*	$OpenBSD: if_rum.c,v 1.126 2020/07/31 10:49:32 mglocker Exp $	*/
2 
3 /*-
4  * Copyright (c) 2005-2007 Damien Bergamini <damien.bergamini@free.fr>
5  * Copyright (c) 2006 Niall O'Higgins <niallo@openbsd.org>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /*-
21  * Ralink Technology RT2501USB/RT2601USB chipset driver
22  * http://www.ralinktech.com.tw/
23  */
24 
25 #include "bpfilter.h"
26 
27 #include <sys/param.h>
28 #include <sys/sockio.h>
29 #include <sys/mbuf.h>
30 #include <sys/kernel.h>
31 #include <sys/socket.h>
32 #include <sys/systm.h>
33 #include <sys/timeout.h>
34 #include <sys/conf.h>
35 #include <sys/device.h>
36 #include <sys/endian.h>
37 
38 #include <machine/intr.h>
39 
40 #if NBPFILTER > 0
41 #include <net/bpf.h>
42 #endif
43 #include <net/if.h>
44 #include <net/if_dl.h>
45 #include <net/if_media.h>
46 
47 #include <netinet/in.h>
48 #include <netinet/if_ether.h>
49 
50 #include <net80211/ieee80211_var.h>
51 #include <net80211/ieee80211_amrr.h>
52 #include <net80211/ieee80211_radiotap.h>
53 
54 #include <dev/usb/usb.h>
55 #include <dev/usb/usbdi.h>
56 #include <dev/usb/usbdi_util.h>
57 #include <dev/usb/usbdevs.h>
58 
59 #include <dev/usb/if_rumreg.h>
60 #include <dev/usb/if_rumvar.h>
61 
62 #ifdef RUM_DEBUG
63 #define DPRINTF(x)	do { if (rum_debug) printf x; } while (0)
64 #define DPRINTFN(n, x)	do { if (rum_debug >= (n)) printf x; } while (0)
65 int rum_debug = 0;
66 #else
67 #define DPRINTF(x)
68 #define DPRINTFN(n, x)
69 #endif
70 
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_RT2573_1 },
80 	{ USB_VENDOR_ASUS,		USB_PRODUCT_ASUS_RT2573_2 },
81 	{ USB_VENDOR_BELKIN,		USB_PRODUCT_BELKIN_F5D7050A },
82 	{ USB_VENDOR_BELKIN,		USB_PRODUCT_BELKIN_F5D9050V3 },
83 	{ USB_VENDOR_BELKIN,		USB_PRODUCT_BELKIN_F5D9050C },
84 	{ USB_VENDOR_CISCOLINKSYS,	USB_PRODUCT_CISCOLINKSYS_WUSB200 },
85 	{ USB_VENDOR_CISCOLINKSYS,	USB_PRODUCT_CISCOLINKSYS_WUSB54GC },
86 	{ USB_VENDOR_CISCOLINKSYS,	USB_PRODUCT_CISCOLINKSYS_WUSB54GR },
87 	{ USB_VENDOR_CONCEPTRONIC2,	USB_PRODUCT_CONCEPTRONIC2_C54RU2 },
88 	{ USB_VENDOR_CONCEPTRONIC2,	USB_PRODUCT_CONCEPTRONIC2_RT2573 },
89 	{ USB_VENDOR_COREGA,		USB_PRODUCT_COREGA_CGWLUSB2GL },
90 	{ USB_VENDOR_COREGA,		USB_PRODUCT_COREGA_CGWLUSB2GPX },
91 	{ USB_VENDOR_DICKSMITH,		USB_PRODUCT_DICKSMITH_CWD854F },
92 	{ USB_VENDOR_DICKSMITH,		USB_PRODUCT_DICKSMITH_RT2573 },
93 	{ USB_VENDOR_DLINK2,		USB_PRODUCT_DLINK2_DWA111 },
94 	{ USB_VENDOR_DLINK2,		USB_PRODUCT_DLINK2_DWA110 },
95 	{ USB_VENDOR_DLINK2,		USB_PRODUCT_DLINK2_DWLG122C1 },
96 	{ USB_VENDOR_DLINK2,		USB_PRODUCT_DLINK2_WUA1340 },
97 	{ USB_VENDOR_EDIMAX,		USB_PRODUCT_EDIMAX_EW7318 },
98 	{ USB_VENDOR_EDIMAX,		USB_PRODUCT_EDIMAX_EW7618 },
99 	{ USB_VENDOR_GIGABYTE,		USB_PRODUCT_GIGABYTE_GNWB01GS },
100 	{ USB_VENDOR_GIGABYTE,		USB_PRODUCT_GIGABYTE_GNWI05GS },
101 	{ USB_VENDOR_GIGASET,		USB_PRODUCT_GIGASET_RT2573 },
102 	{ USB_VENDOR_GOODWAY,		USB_PRODUCT_GOODWAY_RT2573 },
103 	{ USB_VENDOR_GUILLEMOT,		USB_PRODUCT_GUILLEMOT_HWGUSB254LB },
104 	{ USB_VENDOR_GUILLEMOT,		USB_PRODUCT_GUILLEMOT_HWGUSB254V2AP },
105 	{ USB_VENDOR_HUAWEI3COM,	USB_PRODUCT_HUAWEI3COM_WUB320G },
106 	{ USB_VENDOR_MELCO,		USB_PRODUCT_MELCO_G54HP },
107 	{ USB_VENDOR_MELCO,		USB_PRODUCT_MELCO_SG54HP },
108 	{ USB_VENDOR_MELCO,		USB_PRODUCT_MELCO_SG54HG },
109 	{ USB_VENDOR_MSI,		USB_PRODUCT_MSI_RT2573_1 },
110 	{ USB_VENDOR_MSI,		USB_PRODUCT_MSI_RT2573_2 },
111 	{ USB_VENDOR_MSI,		USB_PRODUCT_MSI_RT2573_3 },
112 	{ USB_VENDOR_MSI,		USB_PRODUCT_MSI_RT2573_4 },
113 	{ USB_VENDOR_NOVATECH,		USB_PRODUCT_NOVATECH_RT2573 },
114 	{ USB_VENDOR_PLANEX2,		USB_PRODUCT_PLANEX2_GWUS54HP },
115 	{ USB_VENDOR_PLANEX2,		USB_PRODUCT_PLANEX2_GWUS54MINI2 },
116 	{ USB_VENDOR_PLANEX2,		USB_PRODUCT_PLANEX2_GWUSMM },
117 	{ USB_VENDOR_QCOM,		USB_PRODUCT_QCOM_RT2573 },
118 	{ USB_VENDOR_QCOM,		USB_PRODUCT_QCOM_RT2573_2 },
119 	{ USB_VENDOR_QCOM,		USB_PRODUCT_QCOM_RT2573_3 },
120 	{ USB_VENDOR_RALINK,		USB_PRODUCT_RALINK_RT2573 },
121 	{ USB_VENDOR_RALINK,		USB_PRODUCT_RALINK_RT2573_2 },
122 	{ USB_VENDOR_RALINK,		USB_PRODUCT_RALINK_RT2671 },
123 	{ USB_VENDOR_SITECOMEU,		USB_PRODUCT_SITECOMEU_WL113R2 },
124 	{ USB_VENDOR_SITECOMEU,		USB_PRODUCT_SITECOMEU_WL172 },
125 	{ USB_VENDOR_SURECOM,		USB_PRODUCT_SURECOM_RT2573 },
126 	{ USB_VENDOR_SPARKLAN,		USB_PRODUCT_SPARKLAN_RT2573 },
127 	{ USB_VENDOR_ZYXEL,		USB_PRODUCT_ZYXEL_RT2573 }
128 };
129 
130 void		rum_attachhook(struct device *);
131 int		rum_alloc_tx_list(struct rum_softc *);
132 void		rum_free_tx_list(struct rum_softc *);
133 int		rum_alloc_rx_list(struct rum_softc *);
134 void		rum_free_rx_list(struct rum_softc *);
135 int		rum_media_change(struct ifnet *);
136 void		rum_next_scan(void *);
137 void		rum_task(void *);
138 int		rum_newstate(struct ieee80211com *, enum ieee80211_state, int);
139 void		rum_txeof(struct usbd_xfer *, void *, usbd_status);
140 void		rum_rxeof(struct usbd_xfer *, void *, usbd_status);
141 #if NBPFILTER > 0
142 uint8_t		rum_rxrate(const struct rum_rx_desc *);
143 #endif
144 int		rum_ack_rate(struct ieee80211com *, int);
145 uint16_t	rum_txtime(int, int, uint32_t);
146 uint8_t		rum_plcp_signal(int);
147 void		rum_setup_tx_desc(struct rum_softc *, struct rum_tx_desc *,
148 		    uint32_t, uint16_t, int, int);
149 int		rum_tx_data(struct rum_softc *, struct mbuf *,
150 		    struct ieee80211_node *);
151 void		rum_start(struct ifnet *);
152 void		rum_watchdog(struct ifnet *);
153 int		rum_ioctl(struct ifnet *, u_long, caddr_t);
154 void		rum_eeprom_read(struct rum_softc *, uint16_t, void *, int);
155 uint32_t	rum_read(struct rum_softc *, uint16_t);
156 void		rum_read_multi(struct rum_softc *, uint16_t, void *, int);
157 void		rum_write(struct rum_softc *, uint16_t, uint32_t);
158 void		rum_write_multi(struct rum_softc *, uint16_t, void *, size_t);
159 void		rum_bbp_write(struct rum_softc *, uint8_t, uint8_t);
160 uint8_t		rum_bbp_read(struct rum_softc *, uint8_t);
161 void		rum_rf_write(struct rum_softc *, uint8_t, uint32_t);
162 void		rum_select_antenna(struct rum_softc *);
163 void		rum_enable_mrr(struct rum_softc *);
164 void		rum_set_txpreamble(struct rum_softc *);
165 void		rum_set_basicrates(struct rum_softc *);
166 void		rum_select_band(struct rum_softc *,
167 		    struct ieee80211_channel *);
168 void		rum_set_chan(struct rum_softc *, struct ieee80211_channel *);
169 void		rum_enable_tsf_sync(struct rum_softc *);
170 void		rum_update_slot(struct rum_softc *);
171 void		rum_set_bssid(struct rum_softc *, const uint8_t *);
172 void		rum_set_macaddr(struct rum_softc *, const uint8_t *);
173 void		rum_update_promisc(struct rum_softc *);
174 const char	*rum_get_rf(int);
175 void		rum_read_eeprom(struct rum_softc *);
176 int		rum_bbp_init(struct rum_softc *);
177 int		rum_init(struct ifnet *);
178 void		rum_stop(struct ifnet *, int);
179 int		rum_load_microcode(struct rum_softc *, const u_char *, size_t);
180 #ifndef IEEE80211_STA_ONLY
181 int		rum_prepare_beacon(struct rum_softc *);
182 #endif
183 void		rum_newassoc(struct ieee80211com *, struct ieee80211_node *,
184 		    int);
185 void		rum_amrr_start(struct rum_softc *, struct ieee80211_node *);
186 void		rum_amrr_timeout(void *);
187 void		rum_amrr_update(struct usbd_xfer *, void *,
188 		    usbd_status status);
189 
190 static const struct {
191 	uint32_t	reg;
192 	uint32_t	val;
193 } rum_def_mac[] = {
194 	RT2573_DEF_MAC
195 };
196 
197 static const struct {
198 	uint8_t	reg;
199 	uint8_t	val;
200 } rum_def_bbp[] = {
201 	RT2573_DEF_BBP
202 };
203 
204 static const struct rfprog {
205 	uint8_t		chan;
206 	uint32_t	r1, r2, r3, r4;
207 }  rum_rf5226[] = {
208 	RT2573_RF5226
209 }, rum_rf5225[] = {
210 	RT2573_RF5225
211 };
212 
213 int rum_match(struct device *, void *, void *);
214 void rum_attach(struct device *, struct device *, void *);
215 int rum_detach(struct device *, int);
216 
217 struct cfdriver rum_cd = {
218 	NULL, "rum", DV_IFNET
219 };
220 
221 const struct cfattach rum_ca = {
222 	sizeof(struct rum_softc), rum_match, rum_attach, rum_detach
223 };
224 
225 int
226 rum_match(struct device *parent, void *match, void *aux)
227 {
228 	struct usb_attach_arg *uaa = aux;
229 
230 	if (uaa->iface == NULL || uaa->configno != 1)
231 		return UMATCH_NONE;
232 
233 	return (usb_lookup(rum_devs, uaa->vendor, uaa->product) != NULL) ?
234 	    UMATCH_VENDOR_PRODUCT_CONF_IFACE : UMATCH_NONE;
235 }
236 
237 void
238 rum_attachhook(struct device *self)
239 {
240 	struct rum_softc *sc = (struct rum_softc *)self;
241 	const char *name = "rum-rt2573";
242 	u_char *ucode;
243 	size_t size;
244 	int error;
245 
246 	if ((error = loadfirmware(name, &ucode, &size)) != 0) {
247 		printf("%s: failed loadfirmware of file %s (error %d)\n",
248 		    sc->sc_dev.dv_xname, name, error);
249 		return;
250 	}
251 
252 	if (rum_load_microcode(sc, ucode, size) != 0) {
253 		printf("%s: could not load 8051 microcode\n",
254 		    sc->sc_dev.dv_xname);
255 	}
256 
257 	free(ucode, M_DEVBUF, size);
258 }
259 
260 void
261 rum_attach(struct device *parent, struct device *self, void *aux)
262 {
263 	struct rum_softc *sc = (struct rum_softc *)self;
264 	struct usb_attach_arg *uaa = aux;
265 	struct ieee80211com *ic = &sc->sc_ic;
266 	struct ifnet *ifp = &ic->ic_if;
267 	usb_interface_descriptor_t *id;
268 	usb_endpoint_descriptor_t *ed;
269 	int i, ntries;
270 	uint32_t tmp;
271 
272 	sc->sc_udev = uaa->device;
273 	sc->sc_iface = uaa->iface;
274 
275 	/*
276 	 * Find endpoints.
277 	 */
278 	id = usbd_get_interface_descriptor(sc->sc_iface);
279 
280 	sc->sc_rx_no = sc->sc_tx_no = -1;
281 	for (i = 0; i < id->bNumEndpoints; i++) {
282 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
283 		if (ed == NULL) {
284 			printf("%s: no endpoint descriptor for iface %d\n",
285 			    sc->sc_dev.dv_xname, i);
286 			return;
287 		}
288 
289 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
290 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
291 			sc->sc_rx_no = ed->bEndpointAddress;
292 		else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
293 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
294 			sc->sc_tx_no = ed->bEndpointAddress;
295 	}
296 	if (sc->sc_rx_no == -1 || sc->sc_tx_no == -1) {
297 		printf("%s: missing endpoint\n", sc->sc_dev.dv_xname);
298 		return;
299 	}
300 
301 	usb_init_task(&sc->sc_task, rum_task, sc, USB_TASK_TYPE_GENERIC);
302 	timeout_set(&sc->scan_to, rum_next_scan, sc);
303 
304 	sc->amrr.amrr_min_success_threshold =  1;
305 	sc->amrr.amrr_max_success_threshold = 10;
306 	timeout_set(&sc->amrr_to, rum_amrr_timeout, sc);
307 
308 	/* retrieve RT2573 rev. no */
309 	for (ntries = 0; ntries < 1000; ntries++) {
310 		if ((tmp = rum_read(sc, RT2573_MAC_CSR0)) != 0)
311 			break;
312 		DELAY(1000);
313 	}
314 	if (ntries == 1000) {
315 		printf("%s: timeout waiting for chip to settle\n",
316 		    sc->sc_dev.dv_xname);
317 		return;
318 	}
319 
320 	/* retrieve MAC address and various other things from EEPROM */
321 	rum_read_eeprom(sc);
322 
323 	printf("%s: MAC/BBP RT%04x (rev 0x%05x), RF %s, address %s\n",
324 	    sc->sc_dev.dv_xname, sc->macbbp_rev, tmp,
325 	    rum_get_rf(sc->rf_rev), ether_sprintf(ic->ic_myaddr));
326 
327 	config_mountroot(self, rum_attachhook);
328 
329 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
330 	ic->ic_opmode = IEEE80211_M_STA;	/* default to BSS mode */
331 	ic->ic_state = IEEE80211_S_INIT;
332 
333 	/* set device capabilities */
334 	ic->ic_caps =
335 	    IEEE80211_C_MONITOR |	/* monitor mode supported */
336 #ifndef IEEE80211_STA_ONLY
337 	    IEEE80211_C_IBSS |		/* IBSS mode supported */
338 	    IEEE80211_C_HOSTAP |	/* HostAp mode supported */
339 #endif
340 	    IEEE80211_C_TXPMGT |	/* tx power management */
341 	    IEEE80211_C_SHPREAMBLE |	/* short preamble supported */
342 	    IEEE80211_C_SHSLOT |	/* short slot time supported */
343 	    IEEE80211_C_WEP |		/* s/w WEP */
344 	    IEEE80211_C_RSN;		/* WPA/RSN */
345 
346 	if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_5226) {
347 		/* set supported .11a rates */
348 		ic->ic_sup_rates[IEEE80211_MODE_11A] =
349 		    ieee80211_std_rateset_11a;
350 
351 		/* set supported .11a channels */
352 		for (i = 34; i <= 46; i += 4) {
353 			ic->ic_channels[i].ic_freq =
354 			    ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
355 			ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
356 		}
357 		for (i = 36; i <= 64; i += 4) {
358 			ic->ic_channels[i].ic_freq =
359 			    ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
360 			ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
361 		}
362 		for (i = 100; i <= 140; i += 4) {
363 			ic->ic_channels[i].ic_freq =
364 			    ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
365 			ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
366 		}
367 		for (i = 149; i <= 165; i += 4) {
368 			ic->ic_channels[i].ic_freq =
369 			    ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
370 			ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
371 		}
372 	}
373 
374 	/* set supported .11b and .11g rates */
375 	ic->ic_sup_rates[IEEE80211_MODE_11B] = ieee80211_std_rateset_11b;
376 	ic->ic_sup_rates[IEEE80211_MODE_11G] = ieee80211_std_rateset_11g;
377 
378 	/* set supported .11b and .11g channels (1 through 14) */
379 	for (i = 1; i <= 14; i++) {
380 		ic->ic_channels[i].ic_freq =
381 		    ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
382 		ic->ic_channels[i].ic_flags =
383 		    IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
384 		    IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
385 	}
386 
387 	ifp->if_softc = sc;
388 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
389 	ifp->if_ioctl = rum_ioctl;
390 	ifp->if_start = rum_start;
391 	ifp->if_watchdog = rum_watchdog;
392 	memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
393 
394 	if_attach(ifp);
395 	ieee80211_ifattach(ifp);
396 	ic->ic_newassoc = rum_newassoc;
397 
398 	/* override state transition machine */
399 	sc->sc_newstate = ic->ic_newstate;
400 	ic->ic_newstate = rum_newstate;
401 	ieee80211_media_init(ifp, rum_media_change, ieee80211_media_status);
402 
403 #if NBPFILTER > 0
404 	bpfattach(&sc->sc_drvbpf, ifp, DLT_IEEE802_11_RADIO,
405 	    sizeof (struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN);
406 
407 	sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
408 	sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
409 	sc->sc_rxtap.wr_ihdr.it_present = htole32(RT2573_RX_RADIOTAP_PRESENT);
410 
411 	sc->sc_txtap_len = sizeof sc->sc_txtapu;
412 	sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
413 	sc->sc_txtap.wt_ihdr.it_present = htole32(RT2573_TX_RADIOTAP_PRESENT);
414 #endif
415 }
416 
417 int
418 rum_detach(struct device *self, int flags)
419 {
420 	struct rum_softc *sc = (struct rum_softc *)self;
421 	struct ifnet *ifp = &sc->sc_ic.ic_if;
422 	int s;
423 
424 	s = splusb();
425 
426 	if (timeout_initialized(&sc->scan_to))
427 		timeout_del(&sc->scan_to);
428 	if (timeout_initialized(&sc->amrr_to))
429 		timeout_del(&sc->amrr_to);
430 
431 	usb_rem_wait_task(sc->sc_udev, &sc->sc_task);
432 
433 	usbd_ref_wait(sc->sc_udev);
434 
435 	if (ifp->if_softc != NULL) {
436 		ieee80211_ifdetach(ifp);	/* free all nodes */
437 		if_detach(ifp);
438 	}
439 
440 	if (sc->amrr_xfer != NULL) {
441 		usbd_free_xfer(sc->amrr_xfer);
442 		sc->amrr_xfer = NULL;
443 	}
444 	if (sc->sc_rx_pipeh != NULL)
445 		usbd_close_pipe(sc->sc_rx_pipeh);
446 	if (sc->sc_tx_pipeh != NULL)
447 		usbd_close_pipe(sc->sc_tx_pipeh);
448 
449 	rum_free_rx_list(sc);
450 	rum_free_tx_list(sc);
451 
452 	splx(s);
453 
454 	return 0;
455 }
456 
457 int
458 rum_alloc_tx_list(struct rum_softc *sc)
459 {
460 	int i, error;
461 
462 	sc->tx_cur = sc->tx_queued = 0;
463 
464 	for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
465 		struct rum_tx_data *data = &sc->tx_data[i];
466 
467 		data->sc = sc;
468 
469 		data->xfer = usbd_alloc_xfer(sc->sc_udev);
470 		if (data->xfer == NULL) {
471 			printf("%s: could not allocate tx xfer\n",
472 			    sc->sc_dev.dv_xname);
473 			error = ENOMEM;
474 			goto fail;
475 		}
476 		data->buf = usbd_alloc_buffer(data->xfer,
477 		    RT2573_TX_DESC_SIZE + IEEE80211_MAX_LEN);
478 		if (data->buf == NULL) {
479 			printf("%s: could not allocate tx buffer\n",
480 			    sc->sc_dev.dv_xname);
481 			error = ENOMEM;
482 			goto fail;
483 		}
484 		/* clean Tx descriptor */
485 		bzero(data->buf, RT2573_TX_DESC_SIZE);
486 	}
487 
488 	return 0;
489 
490 fail:	rum_free_tx_list(sc);
491 	return error;
492 }
493 
494 void
495 rum_free_tx_list(struct rum_softc *sc)
496 {
497 	int i;
498 
499 	for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
500 		struct rum_tx_data *data = &sc->tx_data[i];
501 
502 		if (data->xfer != NULL) {
503 			usbd_free_xfer(data->xfer);
504 			data->xfer = NULL;
505 		}
506 		/*
507 		 * The node has already been freed at that point so don't call
508 		 * ieee80211_release_node() here.
509 		 */
510 		data->ni = NULL;
511 	}
512 }
513 
514 int
515 rum_alloc_rx_list(struct rum_softc *sc)
516 {
517 	int i, error;
518 
519 	for (i = 0; i < RUM_RX_LIST_COUNT; i++) {
520 		struct rum_rx_data *data = &sc->rx_data[i];
521 
522 		data->sc = sc;
523 
524 		data->xfer = usbd_alloc_xfer(sc->sc_udev);
525 		if (data->xfer == NULL) {
526 			printf("%s: could not allocate rx xfer\n",
527 			    sc->sc_dev.dv_xname);
528 			error = ENOMEM;
529 			goto fail;
530 		}
531 		if (usbd_alloc_buffer(data->xfer, MCLBYTES) == NULL) {
532 			printf("%s: could not allocate rx buffer\n",
533 			    sc->sc_dev.dv_xname);
534 			error = ENOMEM;
535 			goto fail;
536 		}
537 
538 		MGETHDR(data->m, M_DONTWAIT, MT_DATA);
539 		if (data->m == NULL) {
540 			printf("%s: could not allocate rx mbuf\n",
541 			    sc->sc_dev.dv_xname);
542 			error = ENOMEM;
543 			goto fail;
544 		}
545 		MCLGET(data->m, M_DONTWAIT);
546 		if (!(data->m->m_flags & M_EXT)) {
547 			printf("%s: could not allocate rx mbuf cluster\n",
548 			    sc->sc_dev.dv_xname);
549 			error = ENOMEM;
550 			goto fail;
551 		}
552 		data->buf = mtod(data->m, uint8_t *);
553 	}
554 
555 	return 0;
556 
557 fail:	rum_free_rx_list(sc);
558 	return error;
559 }
560 
561 void
562 rum_free_rx_list(struct rum_softc *sc)
563 {
564 	int i;
565 
566 	for (i = 0; i < RUM_RX_LIST_COUNT; i++) {
567 		struct rum_rx_data *data = &sc->rx_data[i];
568 
569 		if (data->xfer != NULL) {
570 			usbd_free_xfer(data->xfer);
571 			data->xfer = NULL;
572 		}
573 		if (data->m != NULL) {
574 			m_freem(data->m);
575 			data->m = NULL;
576 		}
577 	}
578 }
579 
580 int
581 rum_media_change(struct ifnet *ifp)
582 {
583 	int error;
584 
585 	error = ieee80211_media_change(ifp);
586 	if (error != ENETRESET)
587 		return error;
588 
589 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
590 		error = rum_init(ifp);
591 
592 	return error;
593 }
594 
595 /*
596  * This function is called periodically (every 200ms) during scanning to
597  * switch from one channel to another.
598  */
599 void
600 rum_next_scan(void *arg)
601 {
602 	struct rum_softc *sc = arg;
603 	struct ieee80211com *ic = &sc->sc_ic;
604 	struct ifnet *ifp = &ic->ic_if;
605 
606 	if (usbd_is_dying(sc->sc_udev))
607 		return;
608 
609 	usbd_ref_incr(sc->sc_udev);
610 
611 	if (ic->ic_state == IEEE80211_S_SCAN)
612 		ieee80211_next_scan(ifp);
613 
614 	usbd_ref_decr(sc->sc_udev);
615 }
616 
617 void
618 rum_task(void *arg)
619 {
620 	struct rum_softc *sc = arg;
621 	struct ieee80211com *ic = &sc->sc_ic;
622 	enum ieee80211_state ostate;
623 	struct ieee80211_node *ni;
624 	uint32_t tmp;
625 
626 	if (usbd_is_dying(sc->sc_udev))
627 		return;
628 
629 	ostate = ic->ic_state;
630 
631 	switch (sc->sc_state) {
632 	case IEEE80211_S_INIT:
633 		if (ostate == IEEE80211_S_RUN) {
634 			/* abort TSF synchronization */
635 			tmp = rum_read(sc, RT2573_TXRX_CSR9);
636 			rum_write(sc, RT2573_TXRX_CSR9, tmp & ~0x00ffffff);
637 		}
638 		break;
639 
640 	case IEEE80211_S_SCAN:
641 		rum_set_chan(sc, ic->ic_bss->ni_chan);
642 		if (!usbd_is_dying(sc->sc_udev))
643 			timeout_add_msec(&sc->scan_to, 200);
644 		break;
645 
646 	case IEEE80211_S_AUTH:
647 		rum_set_chan(sc, ic->ic_bss->ni_chan);
648 		break;
649 
650 	case IEEE80211_S_ASSOC:
651 		rum_set_chan(sc, ic->ic_bss->ni_chan);
652 		break;
653 
654 	case IEEE80211_S_RUN:
655 		rum_set_chan(sc, ic->ic_bss->ni_chan);
656 
657 		ni = ic->ic_bss;
658 
659 		if (ic->ic_opmode != IEEE80211_M_MONITOR) {
660 			rum_update_slot(sc);
661 			rum_enable_mrr(sc);
662 			rum_set_txpreamble(sc);
663 			rum_set_basicrates(sc);
664 			rum_set_bssid(sc, ni->ni_bssid);
665 		}
666 
667 #ifndef IEEE80211_STA_ONLY
668 		if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
669 		    ic->ic_opmode == IEEE80211_M_IBSS)
670 			rum_prepare_beacon(sc);
671 #endif
672 
673 		if (ic->ic_opmode != IEEE80211_M_MONITOR)
674 			rum_enable_tsf_sync(sc);
675 
676 		if (ic->ic_opmode == IEEE80211_M_STA) {
677 			/* fake a join to init the tx rate */
678 			rum_newassoc(ic, ic->ic_bss, 1);
679 
680 			/* enable automatic rate control in STA mode */
681 			if (ic->ic_fixed_rate == -1)
682 				rum_amrr_start(sc, ni);
683 		}
684 		break;
685 	}
686 
687 	sc->sc_newstate(ic, sc->sc_state, sc->sc_arg);
688 }
689 
690 int
691 rum_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
692 {
693 	struct rum_softc *sc = ic->ic_if.if_softc;
694 
695 	usb_rem_task(sc->sc_udev, &sc->sc_task);
696 	timeout_del(&sc->scan_to);
697 	timeout_del(&sc->amrr_to);
698 
699 	/* do it in a process context */
700 	sc->sc_state = nstate;
701 	sc->sc_arg = arg;
702 	usb_add_task(sc->sc_udev, &sc->sc_task);
703 	return 0;
704 }
705 
706 /* quickly determine if a given rate is CCK or OFDM */
707 #define RUM_RATE_IS_OFDM(rate)	((rate) >= 12 && (rate) != 22)
708 
709 #define RUM_ACK_SIZE	14	/* 10 + 4(FCS) */
710 #define RUM_CTS_SIZE	14	/* 10 + 4(FCS) */
711 
712 void
713 rum_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
714 {
715 	struct rum_tx_data *data = priv;
716 	struct rum_softc *sc = data->sc;
717 	struct ieee80211com *ic = &sc->sc_ic;
718 	struct ifnet *ifp = &ic->ic_if;
719 	int s;
720 
721 	if (status != USBD_NORMAL_COMPLETION) {
722 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
723 			return;
724 
725 		printf("%s: could not transmit buffer: %s\n",
726 		    sc->sc_dev.dv_xname, usbd_errstr(status));
727 
728 		if (status == USBD_STALLED)
729 			usbd_clear_endpoint_stall_async(sc->sc_tx_pipeh);
730 
731 		ifp->if_oerrors++;
732 		return;
733 	}
734 
735 	s = splnet();
736 
737 	ieee80211_release_node(ic, data->ni);
738 	data->ni = NULL;
739 
740 	sc->tx_queued--;
741 
742 	DPRINTFN(10, ("tx done\n"));
743 
744 	sc->sc_tx_timer = 0;
745 	ifq_clr_oactive(&ifp->if_snd);
746 	rum_start(ifp);
747 
748 	splx(s);
749 }
750 
751 void
752 rum_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
753 {
754 	struct rum_rx_data *data = priv;
755 	struct rum_softc *sc = data->sc;
756 	struct ieee80211com *ic = &sc->sc_ic;
757 	struct ifnet *ifp = &ic->ic_if;
758 	const struct rum_rx_desc *desc;
759 	struct ieee80211_frame *wh;
760 	struct ieee80211_rxinfo rxi;
761 	struct ieee80211_node *ni;
762 	struct mbuf *mnew, *m;
763 	int s, len;
764 
765 	if (status != USBD_NORMAL_COMPLETION) {
766 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
767 			return;
768 
769 		if (status == USBD_STALLED)
770 			usbd_clear_endpoint_stall_async(sc->sc_rx_pipeh);
771 		goto skip;
772 	}
773 
774 	usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
775 
776 	if (len < RT2573_RX_DESC_SIZE + sizeof (struct ieee80211_frame_min)) {
777 		DPRINTF(("%s: xfer too short %d\n", sc->sc_dev.dv_xname,
778 		    len));
779 		ifp->if_ierrors++;
780 		goto skip;
781 	}
782 
783 	desc = (const struct rum_rx_desc *)data->buf;
784 
785 	if (letoh32(desc->flags) & RT2573_RX_CRC_ERROR) {
786 		/*
787 		 * This should not happen since we did not request to receive
788 		 * those frames when we filled RT2573_TXRX_CSR0.
789 		 */
790 		DPRINTFN(5, ("CRC error\n"));
791 		ifp->if_ierrors++;
792 		goto skip;
793 	}
794 
795 	MGETHDR(mnew, M_DONTWAIT, MT_DATA);
796 	if (mnew == NULL) {
797 		printf("%s: could not allocate rx mbuf\n",
798 		    sc->sc_dev.dv_xname);
799 		ifp->if_ierrors++;
800 		goto skip;
801 	}
802 	MCLGET(mnew, M_DONTWAIT);
803 	if (!(mnew->m_flags & M_EXT)) {
804 		printf("%s: could not allocate rx mbuf cluster\n",
805 		    sc->sc_dev.dv_xname);
806 		m_freem(mnew);
807 		ifp->if_ierrors++;
808 		goto skip;
809 	}
810 	m = data->m;
811 	data->m = mnew;
812 	data->buf = mtod(data->m, uint8_t *);
813 
814 	/* finalize mbuf */
815 	m->m_data = (caddr_t)(desc + 1);
816 	m->m_pkthdr.len = m->m_len = (letoh32(desc->flags) >> 16) & 0xfff;
817 
818 	s = splnet();
819 
820 #if NBPFILTER > 0
821 	if (sc->sc_drvbpf != NULL) {
822 		struct mbuf mb;
823 		struct rum_rx_radiotap_header *tap = &sc->sc_rxtap;
824 
825 		tap->wr_flags = 0;
826 		tap->wr_rate = rum_rxrate(desc);
827 		tap->wr_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq);
828 		tap->wr_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
829 		tap->wr_antenna = sc->rx_ant;
830 		tap->wr_antsignal = desc->rssi;
831 
832 		mb.m_data = (caddr_t)tap;
833 		mb.m_len = sc->sc_rxtap_len;
834 		mb.m_next = m;
835 		mb.m_nextpkt = NULL;
836 		mb.m_type = 0;
837 		mb.m_flags = 0;
838 		bpf_mtap(sc->sc_drvbpf, &mb, BPF_DIRECTION_IN);
839 	}
840 #endif
841 
842 	wh = mtod(m, struct ieee80211_frame *);
843 	ni = ieee80211_find_rxnode(ic, wh);
844 
845 	/* send the frame to the 802.11 layer */
846 	rxi.rxi_flags = 0;
847 	rxi.rxi_rssi = desc->rssi;
848 	rxi.rxi_tstamp = 0;	/* unused */
849 	ieee80211_input(ifp, m, ni, &rxi);
850 
851 	/* node is no longer needed */
852 	ieee80211_release_node(ic, ni);
853 
854 	splx(s);
855 
856 	DPRINTFN(15, ("rx done\n"));
857 
858 skip:	/* setup a new transfer */
859 	usbd_setup_xfer(xfer, sc->sc_rx_pipeh, data, data->buf, MCLBYTES,
860 	    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rum_rxeof);
861 	(void)usbd_transfer(xfer);
862 }
863 
864 /*
865  * This function is only used by the Rx radiotap code. It returns the rate at
866  * which a given frame was received.
867  */
868 #if NBPFILTER > 0
869 uint8_t
870 rum_rxrate(const struct rum_rx_desc *desc)
871 {
872 	if (letoh32(desc->flags) & RT2573_RX_OFDM) {
873 		/* reverse function of rum_plcp_signal */
874 		switch (desc->rate) {
875 		case 0xb:	return 12;
876 		case 0xf:	return 18;
877 		case 0xa:	return 24;
878 		case 0xe:	return 36;
879 		case 0x9:	return 48;
880 		case 0xd:	return 72;
881 		case 0x8:	return 96;
882 		case 0xc:	return 108;
883 		}
884 	} else {
885 		if (desc->rate == 10)
886 			return 2;
887 		if (desc->rate == 20)
888 			return 4;
889 		if (desc->rate == 55)
890 			return 11;
891 		if (desc->rate == 110)
892 			return 22;
893 	}
894 	return 2;	/* should not get there */
895 }
896 #endif
897 
898 /*
899  * Return the expected ack rate for a frame transmitted at rate `rate'.
900  */
901 int
902 rum_ack_rate(struct ieee80211com *ic, int rate)
903 {
904 	switch (rate) {
905 	/* CCK rates */
906 	case 2:
907 		return 2;
908 	case 4:
909 	case 11:
910 	case 22:
911 		return (ic->ic_curmode == IEEE80211_MODE_11B) ? 4 : rate;
912 
913 	/* OFDM rates */
914 	case 12:
915 	case 18:
916 		return 12;
917 	case 24:
918 	case 36:
919 		return 24;
920 	case 48:
921 	case 72:
922 	case 96:
923 	case 108:
924 		return 48;
925 	}
926 
927 	/* default to 1Mbps */
928 	return 2;
929 }
930 
931 /*
932  * Compute the duration (in us) needed to transmit `len' bytes at rate `rate'.
933  * The function automatically determines the operating mode depending on the
934  * given rate. `flags' indicates whether short preamble is in use or not.
935  */
936 uint16_t
937 rum_txtime(int len, int rate, uint32_t flags)
938 {
939 	uint16_t txtime;
940 
941 	if (RUM_RATE_IS_OFDM(rate)) {
942 		/* IEEE Std 802.11a-1999, pp. 37 */
943 		txtime = (8 + 4 * len + 3 + rate - 1) / rate;
944 		txtime = 16 + 4 + 4 * txtime + 6;
945 	} else {
946 		/* IEEE Std 802.11b-1999, pp. 28 */
947 		txtime = (16 * len + rate - 1) / rate;
948 		if (rate != 2 && (flags & IEEE80211_F_SHPREAMBLE))
949 			txtime +=  72 + 24;
950 		else
951 			txtime += 144 + 48;
952 	}
953 	return txtime;
954 }
955 
956 uint8_t
957 rum_plcp_signal(int rate)
958 {
959 	switch (rate) {
960 	/* CCK rates (returned values are device-dependent) */
961 	case 2:		return 0x0;
962 	case 4:		return 0x1;
963 	case 11:	return 0x2;
964 	case 22:	return 0x3;
965 
966 	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
967 	case 12:	return 0xb;
968 	case 18:	return 0xf;
969 	case 24:	return 0xa;
970 	case 36:	return 0xe;
971 	case 48:	return 0x9;
972 	case 72:	return 0xd;
973 	case 96:	return 0x8;
974 	case 108:	return 0xc;
975 
976 	/* unsupported rates (should not get there) */
977 	default:	return 0xff;
978 	}
979 }
980 
981 void
982 rum_setup_tx_desc(struct rum_softc *sc, struct rum_tx_desc *desc,
983     uint32_t flags, uint16_t xflags, int len, int rate)
984 {
985 	struct ieee80211com *ic = &sc->sc_ic;
986 	uint16_t plcp_length;
987 	int remainder;
988 
989 	desc->flags = htole32(flags);
990 	desc->flags |= htole32(RT2573_TX_VALID);
991 	desc->flags |= htole32(len << 16);
992 
993 	desc->xflags = htole16(xflags);
994 
995 	desc->wme = htole16(
996 	    RT2573_QID(0) |
997 	    RT2573_AIFSN(2) |
998 	    RT2573_LOGCWMIN(4) |
999 	    RT2573_LOGCWMAX(10));
1000 
1001 	/* setup PLCP fields */
1002 	desc->plcp_signal  = rum_plcp_signal(rate);
1003 	desc->plcp_service = 4;
1004 
1005 	len += IEEE80211_CRC_LEN;
1006 	if (RUM_RATE_IS_OFDM(rate)) {
1007 		desc->flags |= htole32(RT2573_TX_OFDM);
1008 
1009 		plcp_length = len & 0xfff;
1010 		desc->plcp_length_hi = plcp_length >> 6;
1011 		desc->plcp_length_lo = plcp_length & 0x3f;
1012 	} else {
1013 		plcp_length = (16 * len + rate - 1) / rate;
1014 		if (rate == 22) {
1015 			remainder = (16 * len) % 22;
1016 			if (remainder != 0 && remainder < 7)
1017 				desc->plcp_service |= RT2573_PLCP_LENGEXT;
1018 		}
1019 		desc->plcp_length_hi = plcp_length >> 8;
1020 		desc->plcp_length_lo = plcp_length & 0xff;
1021 
1022 		if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1023 			desc->plcp_signal |= 0x08;
1024 	}
1025 }
1026 
1027 #define RUM_TX_TIMEOUT	5000
1028 
1029 int
1030 rum_tx_data(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1031 {
1032 	struct ieee80211com *ic = &sc->sc_ic;
1033 	struct rum_tx_desc *desc;
1034 	struct rum_tx_data *data;
1035 	struct ieee80211_frame *wh;
1036 	struct ieee80211_key *k;
1037 	uint32_t flags = 0;
1038 	uint16_t dur;
1039 	usbd_status error;
1040 	int rate, xferlen, pktlen, needrts = 0, needcts = 0;
1041 
1042 	wh = mtod(m0, struct ieee80211_frame *);
1043 
1044 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1045 		k = ieee80211_get_txkey(ic, wh, ni);
1046 
1047 		if ((m0 = ieee80211_encrypt(ic, m0, k)) == NULL)
1048 			return ENOBUFS;
1049 
1050 		/* packet header may have moved, reset our local pointer */
1051 		wh = mtod(m0, struct ieee80211_frame *);
1052 	}
1053 
1054 	/* compute actual packet length (including CRC and crypto overhead) */
1055 	pktlen = m0->m_pkthdr.len + IEEE80211_CRC_LEN;
1056 
1057 	/* pickup a rate */
1058 	if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
1059 	    ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
1060 	     IEEE80211_FC0_TYPE_MGT)) {
1061 		/* mgmt/multicast frames are sent at the lowest avail. rate */
1062 		rate = ni->ni_rates.rs_rates[0];
1063 	} else if (ic->ic_fixed_rate != -1) {
1064 		rate = ic->ic_sup_rates[ic->ic_curmode].
1065 		    rs_rates[ic->ic_fixed_rate];
1066 	} else
1067 		rate = ni->ni_rates.rs_rates[ni->ni_txrate];
1068 	if (rate == 0)
1069 		rate = 2;	/* XXX should not happen */
1070 	rate &= IEEE80211_RATE_VAL;
1071 
1072 	/* check if RTS/CTS or CTS-to-self protection must be used */
1073 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1074 		/* multicast frames are not sent at OFDM rates in 802.11b/g */
1075 		if (pktlen > ic->ic_rtsthreshold) {
1076 			needrts = 1;	/* RTS/CTS based on frame length */
1077 		} else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
1078 		    RUM_RATE_IS_OFDM(rate)) {
1079 			if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
1080 				needcts = 1;	/* CTS-to-self */
1081 			else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
1082 				needrts = 1;	/* RTS/CTS */
1083 		}
1084 	}
1085 	if (needrts || needcts) {
1086 		struct mbuf *mprot;
1087 		int protrate, ackrate;
1088 		uint16_t dur;
1089 
1090 		protrate = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? 12 : 2;
1091 		ackrate  = rum_ack_rate(ic, rate);
1092 
1093 		dur = rum_txtime(pktlen, rate, ic->ic_flags) +
1094 		      rum_txtime(RUM_ACK_SIZE, ackrate, ic->ic_flags) +
1095 		      2 * sc->sifs;
1096 		if (needrts) {
1097 			dur += rum_txtime(RUM_CTS_SIZE, rum_ack_rate(ic,
1098 			    protrate), ic->ic_flags) + sc->sifs;
1099 			mprot = ieee80211_get_rts(ic, wh, dur);
1100 		} else {
1101 			mprot = ieee80211_get_cts_to_self(ic, dur);
1102 		}
1103 		if (mprot == NULL) {
1104 			printf("%s: could not allocate protection frame\n",
1105 			    sc->sc_dev.dv_xname);
1106 			m_freem(m0);
1107 			return ENOBUFS;
1108 		}
1109 
1110 		data = &sc->tx_data[sc->tx_cur];
1111 		desc = (struct rum_tx_desc *)data->buf;
1112 
1113 		/* avoid multiple free() of the same node for each fragment */
1114 		data->ni = ieee80211_ref_node(ni);
1115 
1116 		m_copydata(mprot, 0, mprot->m_pkthdr.len,
1117 		    data->buf + RT2573_TX_DESC_SIZE);
1118 		rum_setup_tx_desc(sc, desc,
1119 		    (needrts ? RT2573_TX_NEED_ACK : 0) | RT2573_TX_MORE_FRAG,
1120 		    0, mprot->m_pkthdr.len, protrate);
1121 
1122 		/* no roundup necessary here */
1123 		xferlen = RT2573_TX_DESC_SIZE + mprot->m_pkthdr.len;
1124 
1125 		/* XXX may want to pass the protection frame to BPF */
1126 
1127 		/* mbuf is no longer needed */
1128 		m_freem(mprot);
1129 
1130 		usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf,
1131 		    xferlen, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
1132 		    RUM_TX_TIMEOUT, rum_txeof);
1133 		error = usbd_transfer(data->xfer);
1134 		if (error != 0 && error != USBD_IN_PROGRESS) {
1135 			m_freem(m0);
1136 			return error;
1137 		}
1138 
1139 		sc->tx_queued++;
1140 		sc->tx_cur = (sc->tx_cur + 1) % RUM_TX_LIST_COUNT;
1141 
1142 		flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
1143 	}
1144 
1145 	data = &sc->tx_data[sc->tx_cur];
1146 	desc = (struct rum_tx_desc *)data->buf;
1147 
1148 	data->ni = ni;
1149 
1150 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1151 		flags |= RT2573_TX_NEED_ACK;
1152 
1153 		dur = rum_txtime(RUM_ACK_SIZE, rum_ack_rate(ic, rate),
1154 		    ic->ic_flags) + sc->sifs;
1155 		*(uint16_t *)wh->i_dur = htole16(dur);
1156 
1157 #ifndef IEEE80211_STA_ONLY
1158 		/* tell hardware to set timestamp in probe responses */
1159 		if ((wh->i_fc[0] &
1160 		    (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
1161 		    (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
1162 			flags |= RT2573_TX_TIMESTAMP;
1163 #endif
1164 	}
1165 
1166 #if NBPFILTER > 0
1167 	if (sc->sc_drvbpf != NULL) {
1168 		struct mbuf mb;
1169 		struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
1170 
1171 		tap->wt_flags = 0;
1172 		tap->wt_rate = rate;
1173 		tap->wt_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq);
1174 		tap->wt_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
1175 		tap->wt_antenna = sc->tx_ant;
1176 
1177 		mb.m_data = (caddr_t)tap;
1178 		mb.m_len = sc->sc_txtap_len;
1179 		mb.m_next = m0;
1180 		mb.m_nextpkt = NULL;
1181 		mb.m_type = 0;
1182 		mb.m_flags = 0;
1183 		bpf_mtap(sc->sc_drvbpf, &mb, BPF_DIRECTION_OUT);
1184 	}
1185 #endif
1186 
1187 	m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RT2573_TX_DESC_SIZE);
1188 	rum_setup_tx_desc(sc, desc, flags, 0, m0->m_pkthdr.len, rate);
1189 
1190 	/* align end on a 4-bytes boundary */
1191 	xferlen = (RT2573_TX_DESC_SIZE + m0->m_pkthdr.len + 3) & ~3;
1192 
1193 	/*
1194 	 * No space left in the last URB to store the extra 4 bytes, force
1195 	 * sending of another URB.
1196 	 */
1197 	if ((xferlen % 64) == 0)
1198 		xferlen += 4;
1199 
1200 	DPRINTFN(10, ("sending frame len=%u rate=%u xfer len=%u\n",
1201 	    m0->m_pkthdr.len + RT2573_TX_DESC_SIZE, rate, xferlen));
1202 
1203 	/* mbuf is no longer needed */
1204 	m_freem(m0);
1205 
1206 	usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf, xferlen,
1207 	    USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RUM_TX_TIMEOUT, rum_txeof);
1208 	error = usbd_transfer(data->xfer);
1209 	if (error != 0 && error != USBD_IN_PROGRESS)
1210 		return error;
1211 
1212 	sc->tx_queued++;
1213 	sc->tx_cur = (sc->tx_cur + 1) % RUM_TX_LIST_COUNT;
1214 
1215 	return 0;
1216 }
1217 
1218 void
1219 rum_start(struct ifnet *ifp)
1220 {
1221 	struct rum_softc *sc = ifp->if_softc;
1222 	struct ieee80211com *ic = &sc->sc_ic;
1223 	struct ieee80211_node *ni;
1224 	struct mbuf *m0;
1225 
1226 	/*
1227 	 * net80211 may still try to send management frames even if the
1228 	 * IFF_RUNNING flag is not set...
1229 	 */
1230 	if (!(ifp->if_flags & IFF_RUNNING) || ifq_is_oactive(&ifp->if_snd))
1231 		return;
1232 
1233 	for (;;) {
1234 		if (sc->tx_queued >= RUM_TX_LIST_COUNT - 1) {
1235 			ifq_set_oactive(&ifp->if_snd);
1236 			break;
1237 		}
1238 
1239 		m0 = mq_dequeue(&ic->ic_mgtq);
1240 		if (m0 != NULL) {
1241 			ni = m0->m_pkthdr.ph_cookie;
1242 #if NBPFILTER > 0
1243 			if (ic->ic_rawbpf != NULL)
1244 				bpf_mtap(ic->ic_rawbpf, m0, BPF_DIRECTION_OUT);
1245 #endif
1246 			if (rum_tx_data(sc, m0, ni) != 0)
1247 				break;
1248 
1249 		} else {
1250 			if (ic->ic_state != IEEE80211_S_RUN)
1251 				break;
1252 
1253 			m0 = ifq_dequeue(&ifp->if_snd);
1254 			if (m0 == NULL)
1255 				break;
1256 #if NBPFILTER > 0
1257 			if (ifp->if_bpf != NULL)
1258 				bpf_mtap(ifp->if_bpf, m0, BPF_DIRECTION_OUT);
1259 #endif
1260 			m0 = ieee80211_encap(ifp, m0, &ni);
1261 			if (m0 == NULL)
1262 				continue;
1263 #if NBPFILTER > 0
1264 			if (ic->ic_rawbpf != NULL)
1265 				bpf_mtap(ic->ic_rawbpf, m0, BPF_DIRECTION_OUT);
1266 #endif
1267 			if (rum_tx_data(sc, m0, ni) != 0) {
1268 				if (ni != NULL)
1269 					ieee80211_release_node(ic, ni);
1270 				ifp->if_oerrors++;
1271 				break;
1272 			}
1273 		}
1274 
1275 		sc->sc_tx_timer = 5;
1276 		ifp->if_timer = 1;
1277 	}
1278 }
1279 
1280 void
1281 rum_watchdog(struct ifnet *ifp)
1282 {
1283 	struct rum_softc *sc = ifp->if_softc;
1284 
1285 	ifp->if_timer = 0;
1286 
1287 	if (sc->sc_tx_timer > 0) {
1288 		if (--sc->sc_tx_timer == 0) {
1289 			printf("%s: device timeout\n", sc->sc_dev.dv_xname);
1290 			/*rum_init(ifp); XXX needs a process context! */
1291 			ifp->if_oerrors++;
1292 			return;
1293 		}
1294 		ifp->if_timer = 1;
1295 	}
1296 
1297 	ieee80211_watchdog(ifp);
1298 }
1299 
1300 int
1301 rum_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1302 {
1303 	struct rum_softc *sc = ifp->if_softc;
1304 	struct ieee80211com *ic = &sc->sc_ic;
1305 	int s, error = 0;
1306 
1307 	if (usbd_is_dying(sc->sc_udev))
1308 		return ENXIO;
1309 
1310 	usbd_ref_incr(sc->sc_udev);
1311 
1312 	s = splnet();
1313 
1314 	switch (cmd) {
1315 	case SIOCSIFADDR:
1316 		ifp->if_flags |= IFF_UP;
1317 		/* FALLTHROUGH */
1318 	case SIOCSIFFLAGS:
1319 		if (ifp->if_flags & IFF_UP) {
1320 			if (ifp->if_flags & IFF_RUNNING)
1321 				rum_update_promisc(sc);
1322 			else
1323 				rum_init(ifp);
1324 		} else {
1325 			if (ifp->if_flags & IFF_RUNNING)
1326 				rum_stop(ifp, 1);
1327 		}
1328 		break;
1329 
1330 	case SIOCS80211CHANNEL:
1331 		/*
1332 		 * This allows for fast channel switching in monitor mode
1333 		 * (used by kismet). In IBSS mode, we must explicitly reset
1334 		 * the interface to generate a new beacon frame.
1335 		 */
1336 		error = ieee80211_ioctl(ifp, cmd, data);
1337 		if (error == ENETRESET &&
1338 		    ic->ic_opmode == IEEE80211_M_MONITOR) {
1339 			if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
1340 			    (IFF_UP | IFF_RUNNING))
1341 				rum_set_chan(sc, ic->ic_ibss_chan);
1342 			error = 0;
1343 		}
1344 		break;
1345 
1346 	default:
1347 		error = ieee80211_ioctl(ifp, cmd, data);
1348 	}
1349 
1350 	if (error == ENETRESET) {
1351 		if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
1352 		    (IFF_UP | IFF_RUNNING))
1353 			rum_init(ifp);
1354 		error = 0;
1355 	}
1356 
1357 	splx(s);
1358 
1359 	usbd_ref_decr(sc->sc_udev);
1360 
1361 	return error;
1362 }
1363 
1364 void
1365 rum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len)
1366 {
1367 	usb_device_request_t req;
1368 	usbd_status error;
1369 
1370 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1371 	req.bRequest = RT2573_READ_EEPROM;
1372 	USETW(req.wValue, 0);
1373 	USETW(req.wIndex, addr);
1374 	USETW(req.wLength, len);
1375 
1376 	error = usbd_do_request(sc->sc_udev, &req, buf);
1377 	if (error != 0) {
1378 		printf("%s: could not read EEPROM: %s\n",
1379 		    sc->sc_dev.dv_xname, usbd_errstr(error));
1380 	}
1381 }
1382 
1383 uint32_t
1384 rum_read(struct rum_softc *sc, uint16_t reg)
1385 {
1386 	uint32_t val;
1387 
1388 	rum_read_multi(sc, reg, &val, sizeof val);
1389 
1390 	return letoh32(val);
1391 }
1392 
1393 void
1394 rum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len)
1395 {
1396 	usb_device_request_t req;
1397 	usbd_status error;
1398 
1399 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1400 	req.bRequest = RT2573_READ_MULTI_MAC;
1401 	USETW(req.wValue, 0);
1402 	USETW(req.wIndex, reg);
1403 	USETW(req.wLength, len);
1404 
1405 	error = usbd_do_request(sc->sc_udev, &req, buf);
1406 	if (error != 0) {
1407 		printf("%s: could not multi read MAC register: %s\n",
1408 		    sc->sc_dev.dv_xname, usbd_errstr(error));
1409 	}
1410 }
1411 
1412 void
1413 rum_write(struct rum_softc *sc, uint16_t reg, uint32_t val)
1414 {
1415 	uint32_t tmp = htole32(val);
1416 
1417 	rum_write_multi(sc, reg, &tmp, sizeof tmp);
1418 }
1419 
1420 void
1421 rum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len)
1422 {
1423 	usb_device_request_t req;
1424 	usbd_status error;
1425 	int offset;
1426 
1427 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1428 	req.bRequest = RT2573_WRITE_MULTI_MAC;
1429 	USETW(req.wValue, 0);
1430 
1431 	/* write at most 64 bytes at a time */
1432 	for (offset = 0; offset < len; offset += 64) {
1433 		USETW(req.wIndex, reg + offset);
1434 		USETW(req.wLength, MIN(len - offset, 64));
1435 
1436 		error = usbd_do_request(sc->sc_udev, &req, buf + offset);
1437 		if (error != 0) {
1438 			printf("%s: could not multi write MAC register: %s\n",
1439 			    sc->sc_dev.dv_xname, usbd_errstr(error));
1440 		}
1441 	}
1442 }
1443 
1444 void
1445 rum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val)
1446 {
1447 	uint32_t tmp;
1448 	int ntries;
1449 
1450 	for (ntries = 0; ntries < 5; ntries++) {
1451 		if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1452 			break;
1453 	}
1454 	if (ntries == 5) {
1455 		printf("%s: could not write to BBP\n", sc->sc_dev.dv_xname);
1456 		return;
1457 	}
1458 
1459 	tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val;
1460 	rum_write(sc, RT2573_PHY_CSR3, tmp);
1461 }
1462 
1463 uint8_t
1464 rum_bbp_read(struct rum_softc *sc, uint8_t reg)
1465 {
1466 	uint32_t val;
1467 	int ntries;
1468 
1469 	for (ntries = 0; ntries < 5; ntries++) {
1470 		if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1471 			break;
1472 	}
1473 	if (ntries == 5) {
1474 		printf("%s: could not read BBP\n", sc->sc_dev.dv_xname);
1475 		return 0;
1476 	}
1477 
1478 	val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8;
1479 	rum_write(sc, RT2573_PHY_CSR3, val);
1480 
1481 	for (ntries = 0; ntries < 100; ntries++) {
1482 		val = rum_read(sc, RT2573_PHY_CSR3);
1483 		if (!(val & RT2573_BBP_BUSY))
1484 			return val & 0xff;
1485 		DELAY(1);
1486 	}
1487 
1488 	printf("%s: could not read BBP\n", sc->sc_dev.dv_xname);
1489 	return 0;
1490 }
1491 
1492 void
1493 rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val)
1494 {
1495 	uint32_t tmp;
1496 	int ntries;
1497 
1498 	for (ntries = 0; ntries < 5; ntries++) {
1499 		if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY))
1500 			break;
1501 	}
1502 	if (ntries == 5) {
1503 		printf("%s: could not write to RF\n", sc->sc_dev.dv_xname);
1504 		return;
1505 	}
1506 
1507 	tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 |
1508 	    (reg & 3);
1509 	rum_write(sc, RT2573_PHY_CSR4, tmp);
1510 
1511 	/* remember last written value in sc */
1512 	sc->rf_regs[reg] = val;
1513 
1514 	DPRINTFN(15, ("RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff));
1515 }
1516 
1517 void
1518 rum_select_antenna(struct rum_softc *sc)
1519 {
1520 	uint8_t bbp4, bbp77;
1521 	uint32_t tmp;
1522 
1523 	bbp4  = rum_bbp_read(sc, 4);
1524 	bbp77 = rum_bbp_read(sc, 77);
1525 
1526 	/* TBD */
1527 
1528 	/* make sure Rx is disabled before switching antenna */
1529 	tmp = rum_read(sc, RT2573_TXRX_CSR0);
1530 	rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
1531 
1532 	rum_bbp_write(sc,  4, bbp4);
1533 	rum_bbp_write(sc, 77, bbp77);
1534 
1535 	rum_write(sc, RT2573_TXRX_CSR0, tmp);
1536 }
1537 
1538 /*
1539  * Enable multi-rate retries for frames sent at OFDM rates.
1540  * In 802.11b/g mode, allow fallback to CCK rates.
1541  */
1542 void
1543 rum_enable_mrr(struct rum_softc *sc)
1544 {
1545 	struct ieee80211com *ic = &sc->sc_ic;
1546 	uint32_t tmp;
1547 
1548 	tmp = rum_read(sc, RT2573_TXRX_CSR4);
1549 
1550 	tmp &= ~RT2573_MRR_CCK_FALLBACK;
1551 	if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_bss->ni_chan))
1552 		tmp |= RT2573_MRR_CCK_FALLBACK;
1553 	tmp |= RT2573_MRR_ENABLED;
1554 
1555 	rum_write(sc, RT2573_TXRX_CSR4, tmp);
1556 }
1557 
1558 void
1559 rum_set_txpreamble(struct rum_softc *sc)
1560 {
1561 	uint32_t tmp;
1562 
1563 	tmp = rum_read(sc, RT2573_TXRX_CSR4);
1564 
1565 	tmp &= ~RT2573_SHORT_PREAMBLE;
1566 	if (sc->sc_ic.ic_flags & IEEE80211_F_SHPREAMBLE)
1567 		tmp |= RT2573_SHORT_PREAMBLE;
1568 
1569 	rum_write(sc, RT2573_TXRX_CSR4, tmp);
1570 }
1571 
1572 void
1573 rum_set_basicrates(struct rum_softc *sc)
1574 {
1575 	struct ieee80211com *ic = &sc->sc_ic;
1576 
1577 	/* update basic rate set */
1578 	if (ic->ic_curmode == IEEE80211_MODE_11B) {
1579 		/* 11b basic rates: 1, 2Mbps */
1580 		rum_write(sc, RT2573_TXRX_CSR5, 0x3);
1581 	} else if (ic->ic_curmode == IEEE80211_MODE_11A) {
1582 		/* 11a basic rates: 6, 12, 24Mbps */
1583 		rum_write(sc, RT2573_TXRX_CSR5, 0x150);
1584 	} else {
1585 		/* 11b/g basic rates: 1, 2, 5.5, 11Mbps */
1586 		rum_write(sc, RT2573_TXRX_CSR5, 0xf);
1587 	}
1588 }
1589 
1590 /*
1591  * Reprogram MAC/BBP to switch to a new band.  Values taken from the reference
1592  * driver.
1593  */
1594 void
1595 rum_select_band(struct rum_softc *sc, struct ieee80211_channel *c)
1596 {
1597 	uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104;
1598 	uint32_t tmp;
1599 
1600 	/* update all BBP registers that depend on the band */
1601 	bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c;
1602 	bbp35 = 0x50; bbp97 = 0x48; bbp98  = 0x48;
1603 	if (IEEE80211_IS_CHAN_5GHZ(c)) {
1604 		bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c;
1605 		bbp35 += 0x10; bbp97 += 0x10; bbp98  += 0x10;
1606 	}
1607 	if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1608 	    (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1609 		bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10;
1610 	}
1611 
1612 	sc->bbp17 = bbp17;
1613 	rum_bbp_write(sc,  17, bbp17);
1614 	rum_bbp_write(sc,  96, bbp96);
1615 	rum_bbp_write(sc, 104, bbp104);
1616 
1617 	if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1618 	    (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1619 		rum_bbp_write(sc, 75, 0x80);
1620 		rum_bbp_write(sc, 86, 0x80);
1621 		rum_bbp_write(sc, 88, 0x80);
1622 	}
1623 
1624 	rum_bbp_write(sc, 35, bbp35);
1625 	rum_bbp_write(sc, 97, bbp97);
1626 	rum_bbp_write(sc, 98, bbp98);
1627 
1628 	tmp = rum_read(sc, RT2573_PHY_CSR0);
1629 	tmp &= ~(RT2573_PA_PE_2GHZ | RT2573_PA_PE_5GHZ);
1630 	if (IEEE80211_IS_CHAN_2GHZ(c))
1631 		tmp |= RT2573_PA_PE_2GHZ;
1632 	else
1633 		tmp |= RT2573_PA_PE_5GHZ;
1634 	rum_write(sc, RT2573_PHY_CSR0, tmp);
1635 
1636 	/* 802.11a uses a 16 microseconds short interframe space */
1637 	sc->sifs = IEEE80211_IS_CHAN_5GHZ(c) ? 16 : 10;
1638 }
1639 
1640 void
1641 rum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c)
1642 {
1643 	struct ieee80211com *ic = &sc->sc_ic;
1644 	const struct rfprog *rfprog;
1645 	uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT;
1646 	int8_t power;
1647 	u_int i, chan;
1648 
1649 	chan = ieee80211_chan2ieee(ic, c);
1650 	if (chan == 0 || chan == IEEE80211_CHAN_ANY)
1651 		return;
1652 
1653 	/* select the appropriate RF settings based on what EEPROM says */
1654 	rfprog = (sc->rf_rev == RT2573_RF_5225 ||
1655 		  sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226;
1656 
1657 	/* find the settings for this channel (we know it exists) */
1658 	for (i = 0; rfprog[i].chan != chan; i++);
1659 
1660 	power = sc->txpow[i];
1661 	if (power < 0) {
1662 		bbp94 += power;
1663 		power = 0;
1664 	} else if (power > 31) {
1665 		bbp94 += power - 31;
1666 		power = 31;
1667 	}
1668 
1669 	/*
1670 	 * If we are switching from the 2GHz band to the 5GHz band or
1671 	 * vice-versa, BBP registers need to be reprogrammed.
1672 	 */
1673 	if (c->ic_flags != sc->sc_curchan->ic_flags) {
1674 		rum_select_band(sc, c);
1675 		rum_select_antenna(sc);
1676 	}
1677 	sc->sc_curchan = c;
1678 
1679 	rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1680 	rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1681 	rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1682 	rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1683 
1684 	rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1685 	rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1686 	rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7 | 1);
1687 	rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1688 
1689 	rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1690 	rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1691 	rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1692 	rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1693 
1694 	DELAY(10);
1695 
1696 	/* enable smart mode for MIMO-capable RFs */
1697 	bbp3 = rum_bbp_read(sc, 3);
1698 
1699 	bbp3 &= ~RT2573_SMART_MODE;
1700 	if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527)
1701 		bbp3 |= RT2573_SMART_MODE;
1702 
1703 	rum_bbp_write(sc, 3, bbp3);
1704 
1705 	if (bbp94 != RT2573_BBPR94_DEFAULT)
1706 		rum_bbp_write(sc, 94, bbp94);
1707 }
1708 
1709 /*
1710  * Enable TSF synchronization and tell h/w to start sending beacons for IBSS
1711  * and HostAP operating modes.
1712  */
1713 void
1714 rum_enable_tsf_sync(struct rum_softc *sc)
1715 {
1716 	struct ieee80211com *ic = &sc->sc_ic;
1717 	uint32_t tmp;
1718 
1719 #ifndef IEEE80211_STA_ONLY
1720 	if (ic->ic_opmode != IEEE80211_M_STA) {
1721 		/*
1722 		 * Change default 16ms TBTT adjustment to 8ms.
1723 		 * Must be done before enabling beacon generation.
1724 		 */
1725 		rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8);
1726 	}
1727 #endif
1728 
1729 	tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000;
1730 
1731 	/* set beacon interval (in 1/16ms unit) */
1732 	tmp |= ic->ic_bss->ni_intval * 16;
1733 
1734 	tmp |= RT2573_TSF_TICKING | RT2573_ENABLE_TBTT;
1735 	if (ic->ic_opmode == IEEE80211_M_STA)
1736 		tmp |= RT2573_TSF_MODE(1);
1737 #ifndef IEEE80211_STA_ONLY
1738 	else
1739 		tmp |= RT2573_TSF_MODE(2) | RT2573_GENERATE_BEACON;
1740 #endif
1741 	rum_write(sc, RT2573_TXRX_CSR9, tmp);
1742 }
1743 
1744 void
1745 rum_update_slot(struct rum_softc *sc)
1746 {
1747 	struct ieee80211com *ic = &sc->sc_ic;
1748 	uint8_t slottime;
1749 	uint32_t tmp;
1750 
1751 	slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ?
1752 	    IEEE80211_DUR_DS_SHSLOT : IEEE80211_DUR_DS_SLOT;
1753 
1754 	tmp = rum_read(sc, RT2573_MAC_CSR9);
1755 	tmp = (tmp & ~0xff) | slottime;
1756 	rum_write(sc, RT2573_MAC_CSR9, tmp);
1757 
1758 	DPRINTF(("setting slot time to %uus\n", slottime));
1759 }
1760 
1761 void
1762 rum_set_bssid(struct rum_softc *sc, const uint8_t *bssid)
1763 {
1764 	uint32_t tmp;
1765 
1766 	tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24;
1767 	rum_write(sc, RT2573_MAC_CSR4, tmp);
1768 
1769 	tmp = bssid[4] | bssid[5] << 8 | RT2573_ONE_BSSID << 16;
1770 	rum_write(sc, RT2573_MAC_CSR5, tmp);
1771 }
1772 
1773 void
1774 rum_set_macaddr(struct rum_softc *sc, const uint8_t *addr)
1775 {
1776 	uint32_t tmp;
1777 
1778 	tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24;
1779 	rum_write(sc, RT2573_MAC_CSR2, tmp);
1780 
1781 	tmp = addr[4] | addr[5] << 8 | 0xff << 16;
1782 	rum_write(sc, RT2573_MAC_CSR3, tmp);
1783 }
1784 
1785 void
1786 rum_update_promisc(struct rum_softc *sc)
1787 {
1788 	struct ifnet *ifp = &sc->sc_ic.ic_if;
1789 	uint32_t tmp;
1790 
1791 	tmp = rum_read(sc, RT2573_TXRX_CSR0);
1792 
1793 	tmp &= ~RT2573_DROP_NOT_TO_ME;
1794 	if (!(ifp->if_flags & IFF_PROMISC))
1795 		tmp |= RT2573_DROP_NOT_TO_ME;
1796 
1797 	rum_write(sc, RT2573_TXRX_CSR0, tmp);
1798 
1799 	DPRINTF(("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
1800 	    "entering" : "leaving"));
1801 }
1802 
1803 const char *
1804 rum_get_rf(int rev)
1805 {
1806 	switch (rev) {
1807 	case RT2573_RF_2527:	return "RT2527 (MIMO XR)";
1808 	case RT2573_RF_2528:	return "RT2528";
1809 	case RT2573_RF_5225:	return "RT5225 (MIMO XR)";
1810 	case RT2573_RF_5226:	return "RT5226";
1811 	default:		return "unknown";
1812 	}
1813 }
1814 
1815 void
1816 rum_read_eeprom(struct rum_softc *sc)
1817 {
1818 	struct ieee80211com *ic = &sc->sc_ic;
1819 	uint16_t val;
1820 #ifdef RUM_DEBUG
1821 	int i;
1822 #endif
1823 
1824 	/* read MAC/BBP type */
1825 	rum_eeprom_read(sc, RT2573_EEPROM_MACBBP, &val, 2);
1826 	sc->macbbp_rev = letoh16(val);
1827 
1828 	/* read MAC address */
1829 	rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, ic->ic_myaddr, 6);
1830 
1831 	rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2);
1832 	val = letoh16(val);
1833 	sc->rf_rev =   (val >> 11) & 0x1f;
1834 	sc->hw_radio = (val >> 10) & 0x1;
1835 	sc->rx_ant =   (val >> 4)  & 0x3;
1836 	sc->tx_ant =   (val >> 2)  & 0x3;
1837 	sc->nb_ant =   val & 0x3;
1838 
1839 	DPRINTF(("RF revision=%d\n", sc->rf_rev));
1840 
1841 	rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2);
1842 	val = letoh16(val);
1843 	sc->ext_5ghz_lna = (val >> 6) & 0x1;
1844 	sc->ext_2ghz_lna = (val >> 4) & 0x1;
1845 
1846 	DPRINTF(("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n",
1847 	    sc->ext_2ghz_lna, sc->ext_5ghz_lna));
1848 
1849 	rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2);
1850 	val = letoh16(val);
1851 	if ((val & 0xff) != 0xff)
1852 		sc->rssi_2ghz_corr = (int8_t)(val & 0xff);	/* signed */
1853 
1854 	rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2);
1855 	val = letoh16(val);
1856 	if ((val & 0xff) != 0xff)
1857 		sc->rssi_5ghz_corr = (int8_t)(val & 0xff);	/* signed */
1858 
1859 	DPRINTF(("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n",
1860 	    sc->rssi_2ghz_corr, sc->rssi_5ghz_corr));
1861 
1862 	rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2);
1863 	val = letoh16(val);
1864 	if ((val & 0xff) != 0xff)
1865 		sc->rffreq = val & 0xff;
1866 
1867 	DPRINTF(("RF freq=%d\n", sc->rffreq));
1868 
1869 	/* read Tx power for all a/b/g channels */
1870 	rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14);
1871 	/* XXX default Tx power for 802.11a channels */
1872 	memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14);
1873 #ifdef RUM_DEBUG
1874 	for (i = 0; i < 14; i++)
1875 		DPRINTF(("Channel=%d Tx power=%d\n", i + 1,  sc->txpow[i]));
1876 #endif
1877 
1878 	/* read default values for BBP registers */
1879 	rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
1880 #ifdef RUM_DEBUG
1881 	for (i = 0; i < 14; i++) {
1882 		if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
1883 			continue;
1884 		DPRINTF(("BBP R%d=%02x\n", sc->bbp_prom[i].reg,
1885 		    sc->bbp_prom[i].val));
1886 	}
1887 #endif
1888 }
1889 
1890 int
1891 rum_bbp_init(struct rum_softc *sc)
1892 {
1893 	int i, ntries;
1894 
1895 	/* wait for BBP to be ready */
1896 	for (ntries = 0; ntries < 100; ntries++) {
1897 		const uint8_t val = rum_bbp_read(sc, 0);
1898 		if (val != 0 && val != 0xff)
1899 			break;
1900 		DELAY(1000);
1901 	}
1902 	if (ntries == 100) {
1903 		printf("%s: timeout waiting for BBP\n",
1904 		    sc->sc_dev.dv_xname);
1905 		return EIO;
1906 	}
1907 
1908 	/* initialize BBP registers to default values */
1909 	for (i = 0; i < nitems(rum_def_bbp); i++)
1910 		rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val);
1911 
1912 	/* write vendor-specific BBP values (from EEPROM) */
1913 	for (i = 0; i < 16; i++) {
1914 		if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
1915 			continue;
1916 		rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
1917 	}
1918 
1919 	return 0;
1920 }
1921 
1922 int
1923 rum_init(struct ifnet *ifp)
1924 {
1925 	struct rum_softc *sc = ifp->if_softc;
1926 	struct ieee80211com *ic = &sc->sc_ic;
1927 	uint32_t tmp;
1928 	usbd_status error;
1929 	int i, ntries;
1930 
1931 	rum_stop(ifp, 0);
1932 
1933 	/* initialize MAC registers to default values */
1934 	for (i = 0; i < nitems(rum_def_mac); i++)
1935 		rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val);
1936 
1937 	/* set host ready */
1938 	rum_write(sc, RT2573_MAC_CSR1, 3);
1939 	rum_write(sc, RT2573_MAC_CSR1, 0);
1940 
1941 	/* wait for BBP/RF to wakeup */
1942 	for (ntries = 0; ntries < 1000; ntries++) {
1943 		if (rum_read(sc, RT2573_MAC_CSR12) & 8)
1944 			break;
1945 		rum_write(sc, RT2573_MAC_CSR12, 4);	/* force wakeup */
1946 		DELAY(1000);
1947 	}
1948 	if (ntries == 1000) {
1949 		printf("%s: timeout waiting for BBP/RF to wakeup\n",
1950 		    sc->sc_dev.dv_xname);
1951 		error = ENODEV;
1952 		goto fail;
1953 	}
1954 
1955 	if ((error = rum_bbp_init(sc)) != 0)
1956 		goto fail;
1957 
1958 	/* select default channel */
1959 	sc->sc_curchan = ic->ic_bss->ni_chan = ic->ic_ibss_chan;
1960 	rum_select_band(sc, sc->sc_curchan);
1961 	rum_select_antenna(sc);
1962 	rum_set_chan(sc, sc->sc_curchan);
1963 
1964 	/* clear STA registers */
1965 	rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
1966 
1967 	IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl));
1968 	rum_set_macaddr(sc, ic->ic_myaddr);
1969 
1970 	/* initialize ASIC */
1971 	rum_write(sc, RT2573_MAC_CSR1, 4);
1972 
1973 	/*
1974 	 * Allocate xfer for AMRR statistics requests.
1975 	 */
1976 	sc->amrr_xfer = usbd_alloc_xfer(sc->sc_udev);
1977 	if (sc->amrr_xfer == NULL) {
1978 		printf("%s: could not allocate AMRR xfer\n",
1979 		    sc->sc_dev.dv_xname);
1980 		goto fail;
1981 	}
1982 
1983 	/*
1984 	 * Open Tx and Rx USB bulk pipes.
1985 	 */
1986 	error = usbd_open_pipe(sc->sc_iface, sc->sc_tx_no, USBD_EXCLUSIVE_USE,
1987 	    &sc->sc_tx_pipeh);
1988 	if (error != 0) {
1989 		printf("%s: could not open Tx pipe: %s\n",
1990 		    sc->sc_dev.dv_xname, usbd_errstr(error));
1991 		goto fail;
1992 	}
1993 	error = usbd_open_pipe(sc->sc_iface, sc->sc_rx_no, USBD_EXCLUSIVE_USE,
1994 	    &sc->sc_rx_pipeh);
1995 	if (error != 0) {
1996 		printf("%s: could not open Rx pipe: %s\n",
1997 		    sc->sc_dev.dv_xname, usbd_errstr(error));
1998 		goto fail;
1999 	}
2000 
2001 	/*
2002 	 * Allocate Tx and Rx xfer queues.
2003 	 */
2004 	error = rum_alloc_tx_list(sc);
2005 	if (error != 0) {
2006 		printf("%s: could not allocate Tx list\n",
2007 		    sc->sc_dev.dv_xname);
2008 		goto fail;
2009 	}
2010 	error = rum_alloc_rx_list(sc);
2011 	if (error != 0) {
2012 		printf("%s: could not allocate Rx list\n",
2013 		    sc->sc_dev.dv_xname);
2014 		goto fail;
2015 	}
2016 
2017 	/*
2018 	 * Start up the receive pipe.
2019 	 */
2020 	for (i = 0; i < RUM_RX_LIST_COUNT; i++) {
2021 		struct rum_rx_data *data = &sc->rx_data[i];
2022 
2023 		usbd_setup_xfer(data->xfer, sc->sc_rx_pipeh, data, data->buf,
2024 		    MCLBYTES, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rum_rxeof);
2025 		error = usbd_transfer(data->xfer);
2026 		if (error != 0 && error != USBD_IN_PROGRESS) {
2027 			printf("%s: could not queue Rx transfer\n",
2028 			    sc->sc_dev.dv_xname);
2029 			goto fail;
2030 		}
2031 	}
2032 
2033 	/* update Rx filter */
2034 	tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff;
2035 
2036 	tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR;
2037 	if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2038 		tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR |
2039 		       RT2573_DROP_ACKCTS;
2040 #ifndef IEEE80211_STA_ONLY
2041 		if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2042 #endif
2043 			tmp |= RT2573_DROP_TODS;
2044 		if (!(ifp->if_flags & IFF_PROMISC))
2045 			tmp |= RT2573_DROP_NOT_TO_ME;
2046 	}
2047 	rum_write(sc, RT2573_TXRX_CSR0, tmp);
2048 
2049 	ifq_clr_oactive(&ifp->if_snd);
2050 	ifp->if_flags |= IFF_RUNNING;
2051 
2052 	if (ic->ic_opmode == IEEE80211_M_MONITOR)
2053 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2054 	else
2055 		ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2056 
2057 	return 0;
2058 
2059 fail:	rum_stop(ifp, 1);
2060 	return error;
2061 }
2062 
2063 void
2064 rum_stop(struct ifnet *ifp, int disable)
2065 {
2066 	struct rum_softc *sc = ifp->if_softc;
2067 	struct ieee80211com *ic = &sc->sc_ic;
2068 	uint32_t tmp;
2069 
2070 	sc->sc_tx_timer = 0;
2071 	ifp->if_timer = 0;
2072 	ifp->if_flags &= ~IFF_RUNNING;
2073 	ifq_clr_oactive(&ifp->if_snd);
2074 
2075 	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);	/* free all nodes */
2076 
2077 	/* disable Rx */
2078 	tmp = rum_read(sc, RT2573_TXRX_CSR0);
2079 	rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
2080 
2081 	/* reset ASIC */
2082 	rum_write(sc, RT2573_MAC_CSR1, 3);
2083 	rum_write(sc, RT2573_MAC_CSR1, 0);
2084 
2085 	if (sc->amrr_xfer != NULL) {
2086 		usbd_free_xfer(sc->amrr_xfer);
2087 		sc->amrr_xfer = NULL;
2088 	}
2089 	if (sc->sc_rx_pipeh != NULL) {
2090 		usbd_close_pipe(sc->sc_rx_pipeh);
2091 		sc->sc_rx_pipeh = NULL;
2092 	}
2093 	if (sc->sc_tx_pipeh != NULL) {
2094 		usbd_close_pipe(sc->sc_tx_pipeh);
2095 		sc->sc_tx_pipeh = NULL;
2096 	}
2097 
2098 	rum_free_rx_list(sc);
2099 	rum_free_tx_list(sc);
2100 }
2101 
2102 int
2103 rum_load_microcode(struct rum_softc *sc, const u_char *ucode, size_t size)
2104 {
2105 	usb_device_request_t req;
2106 	uint16_t reg = RT2573_MCU_CODE_BASE;
2107 	usbd_status error;
2108 
2109 	/* copy firmware image into NIC */
2110 	for (; size >= 4; reg += 4, ucode += 4, size -= 4)
2111 		rum_write(sc, reg, UGETDW(ucode));
2112 
2113 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2114 	req.bRequest = RT2573_MCU_CNTL;
2115 	USETW(req.wValue, RT2573_MCU_RUN);
2116 	USETW(req.wIndex, 0);
2117 	USETW(req.wLength, 0);
2118 
2119 	error = usbd_do_request(sc->sc_udev, &req, NULL);
2120 	if (error != 0) {
2121 		printf("%s: could not run firmware: %s\n",
2122 		    sc->sc_dev.dv_xname, usbd_errstr(error));
2123 	}
2124 	return error;
2125 }
2126 
2127 #ifndef IEEE80211_STA_ONLY
2128 int
2129 rum_prepare_beacon(struct rum_softc *sc)
2130 {
2131 	struct ieee80211com *ic = &sc->sc_ic;
2132 	struct rum_tx_desc desc;
2133 	struct mbuf *m0;
2134 	int rate;
2135 
2136 	m0 = ieee80211_beacon_alloc(ic, ic->ic_bss);
2137 	if (m0 == NULL) {
2138 		printf("%s: could not allocate beacon frame\n",
2139 		    sc->sc_dev.dv_xname);
2140 		return ENOBUFS;
2141 	}
2142 
2143 	/* send beacons at the lowest available rate */
2144 	rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_bss->ni_chan) ? 12 : 2;
2145 
2146 	rum_setup_tx_desc(sc, &desc, RT2573_TX_TIMESTAMP, RT2573_TX_HWSEQ,
2147 	    m0->m_pkthdr.len, rate);
2148 
2149 	/* copy the first 24 bytes of Tx descriptor into NIC memory */
2150 	rum_write_multi(sc, RT2573_HW_BEACON_BASE0, (uint8_t *)&desc, 24);
2151 
2152 	/* copy beacon header and payload into NIC memory */
2153 	rum_write_multi(sc, RT2573_HW_BEACON_BASE0 + 24, mtod(m0, uint8_t *),
2154 	    m0->m_pkthdr.len);
2155 
2156 	m_freem(m0);
2157 
2158 	return 0;
2159 }
2160 #endif
2161 
2162 void
2163 rum_newassoc(struct ieee80211com *ic, struct ieee80211_node *ni, int isnew)
2164 {
2165 	/* start with lowest Tx rate */
2166 	ni->ni_txrate = 0;
2167 }
2168 
2169 void
2170 rum_amrr_start(struct rum_softc *sc, struct ieee80211_node *ni)
2171 {
2172 	int i;
2173 
2174 	/* clear statistic registers (STA_CSR0 to STA_CSR5) */
2175 	rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
2176 
2177 	ieee80211_amrr_node_init(&sc->amrr, &sc->amn);
2178 
2179 	/* set rate to some reasonable initial value */
2180 	for (i = ni->ni_rates.rs_nrates - 1;
2181 	     i > 0 && (ni->ni_rates.rs_rates[i] & IEEE80211_RATE_VAL) > 72;
2182 	     i--);
2183 	ni->ni_txrate = i;
2184 
2185 	if (!usbd_is_dying(sc->sc_udev))
2186 		timeout_add_sec(&sc->amrr_to, 1);
2187 }
2188 
2189 void
2190 rum_amrr_timeout(void *arg)
2191 {
2192 	struct rum_softc *sc = arg;
2193 	usb_device_request_t req;
2194 
2195 	if (usbd_is_dying(sc->sc_udev))
2196 		return;
2197 
2198 	/*
2199 	 * Asynchronously read statistic registers (cleared by read).
2200 	 */
2201 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
2202 	req.bRequest = RT2573_READ_MULTI_MAC;
2203 	USETW(req.wValue, 0);
2204 	USETW(req.wIndex, RT2573_STA_CSR0);
2205 	USETW(req.wLength, sizeof sc->sta);
2206 
2207 	usbd_setup_default_xfer(sc->amrr_xfer, sc->sc_udev, sc,
2208 	    USBD_DEFAULT_TIMEOUT, &req, sc->sta, sizeof sc->sta, 0,
2209 	    rum_amrr_update);
2210 	(void)usbd_transfer(sc->amrr_xfer);
2211 }
2212 
2213 void
2214 rum_amrr_update(struct usbd_xfer *xfer, void *priv,
2215     usbd_status status)
2216 {
2217 	struct rum_softc *sc = (struct rum_softc *)priv;
2218 	struct ifnet *ifp = &sc->sc_ic.ic_if;
2219 
2220 	if (status != USBD_NORMAL_COMPLETION) {
2221 		printf("%s: could not retrieve Tx statistics - cancelling "
2222 		    "automatic rate control\n", sc->sc_dev.dv_xname);
2223 		return;
2224 	}
2225 
2226 	/* count TX retry-fail as Tx errors */
2227 	ifp->if_oerrors += letoh32(sc->sta[5]) >> 16;
2228 
2229 	sc->amn.amn_retrycnt =
2230 	    (letoh32(sc->sta[4]) >> 16) +	/* TX one-retry ok count */
2231 	    (letoh32(sc->sta[5]) & 0xffff) +	/* TX more-retry ok count */
2232 	    (letoh32(sc->sta[5]) >> 16);	/* TX retry-fail count */
2233 
2234 	sc->amn.amn_txcnt =
2235 	    sc->amn.amn_retrycnt +
2236 	    (letoh32(sc->sta[4]) & 0xffff);	/* TX no-retry ok count */
2237 
2238 	ieee80211_amrr_choose(&sc->amrr, sc->sc_ic.ic_bss, &sc->amn);
2239 
2240 	if (!usbd_is_dying(sc->sc_udev))
2241 		timeout_add_sec(&sc->amrr_to, 1);
2242 }
2243