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