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