xref: /netbsd-src/sys/dev/usb/if_rum.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$OpenBSD: if_rum.c,v 1.40 2006/09/18 16:20:20 damien Exp $	*/
2 /*	$NetBSD: if_rum.c,v 1.32 2010/04/05 07:21:48 joerg 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.32 2010/04/05 07:21:48 joerg 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_attach2(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_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_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_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_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 			bpf_mtap3(ic->ic_rawbpf, m0);
1280 			if (rum_tx_mgt(sc, m0, ni) != 0)
1281 				break;
1282 
1283 		} else {
1284 			if (ic->ic_state != IEEE80211_S_RUN)
1285 				break;
1286 			IFQ_POLL(&ifp->if_snd, m0);
1287 			if (m0 == NULL)
1288 				break;
1289 			if (sc->tx_queued >= RUM_TX_LIST_COUNT) {
1290 				ifp->if_flags |= IFF_OACTIVE;
1291 				break;
1292 			}
1293 			IFQ_DEQUEUE(&ifp->if_snd, m0);
1294 			if (m0->m_len < sizeof(struct ether_header) &&
1295 			    !(m0 = m_pullup(m0, sizeof(struct ether_header))))
1296 				continue;
1297 
1298 			eh = mtod(m0, struct ether_header *);
1299 			ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1300 			if (ni == NULL) {
1301 				m_freem(m0);
1302 				continue;
1303 			}
1304 			bpf_mtap(ifp, m0);
1305 			m0 = ieee80211_encap(ic, m0, ni);
1306 			if (m0 == NULL) {
1307 				ieee80211_free_node(ni);
1308 				continue;
1309 			}
1310 			bpf_mtap3(ic->ic_rawbpf, m0);
1311 			if (rum_tx_data(sc, m0, ni) != 0) {
1312 				ieee80211_free_node(ni);
1313 				ifp->if_oerrors++;
1314 				break;
1315 			}
1316 		}
1317 
1318 		sc->sc_tx_timer = 5;
1319 		ifp->if_timer = 1;
1320 	}
1321 }
1322 
1323 Static void
1324 rum_watchdog(struct ifnet *ifp)
1325 {
1326 	struct rum_softc *sc = ifp->if_softc;
1327 	struct ieee80211com *ic = &sc->sc_ic;
1328 
1329 	ifp->if_timer = 0;
1330 
1331 	if (sc->sc_tx_timer > 0) {
1332 		if (--sc->sc_tx_timer == 0) {
1333 			printf("%s: device timeout\n", USBDEVNAME(sc->sc_dev));
1334 			/*rum_init(ifp); XXX needs a process context! */
1335 			ifp->if_oerrors++;
1336 			return;
1337 		}
1338 		ifp->if_timer = 1;
1339 	}
1340 
1341 	ieee80211_watchdog(ic);
1342 }
1343 
1344 Static int
1345 rum_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1346 {
1347 	struct rum_softc *sc = ifp->if_softc;
1348 	struct ieee80211com *ic = &sc->sc_ic;
1349 	int s, error = 0;
1350 
1351 	s = splnet();
1352 
1353 	switch (cmd) {
1354 	case SIOCSIFFLAGS:
1355 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
1356 			break;
1357 		/* XXX re-use ether_ioctl() */
1358 		switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
1359 		case IFF_UP|IFF_RUNNING:
1360 			rum_update_promisc(sc);
1361 			break;
1362 		case IFF_UP:
1363 			rum_init(ifp);
1364 			break;
1365 		case IFF_RUNNING:
1366 			rum_stop(ifp, 1);
1367 			break;
1368 		case 0:
1369 			break;
1370 		}
1371 		break;
1372 
1373 	default:
1374 		error = ieee80211_ioctl(ic, cmd, data);
1375 	}
1376 
1377 	if (error == ENETRESET) {
1378 		if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
1379 		    (IFF_UP | IFF_RUNNING))
1380 			rum_init(ifp);
1381 		error = 0;
1382 	}
1383 
1384 	splx(s);
1385 
1386 	return error;
1387 }
1388 
1389 Static void
1390 rum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len)
1391 {
1392 	usb_device_request_t req;
1393 	usbd_status error;
1394 
1395 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1396 	req.bRequest = RT2573_READ_EEPROM;
1397 	USETW(req.wValue, 0);
1398 	USETW(req.wIndex, addr);
1399 	USETW(req.wLength, len);
1400 
1401 	error = usbd_do_request(sc->sc_udev, &req, buf);
1402 	if (error != 0) {
1403 		printf("%s: could not read EEPROM: %s\n",
1404 		    USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1405 	}
1406 }
1407 
1408 Static uint32_t
1409 rum_read(struct rum_softc *sc, uint16_t reg)
1410 {
1411 	uint32_t val;
1412 
1413 	rum_read_multi(sc, reg, &val, sizeof val);
1414 
1415 	return le32toh(val);
1416 }
1417 
1418 Static void
1419 rum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len)
1420 {
1421 	usb_device_request_t req;
1422 	usbd_status error;
1423 
1424 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1425 	req.bRequest = RT2573_READ_MULTI_MAC;
1426 	USETW(req.wValue, 0);
1427 	USETW(req.wIndex, reg);
1428 	USETW(req.wLength, len);
1429 
1430 	error = usbd_do_request(sc->sc_udev, &req, buf);
1431 	if (error != 0) {
1432 		printf("%s: could not multi read MAC register: %s\n",
1433 		    USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1434 	}
1435 }
1436 
1437 Static void
1438 rum_write(struct rum_softc *sc, uint16_t reg, uint32_t val)
1439 {
1440 	uint32_t tmp = htole32(val);
1441 
1442 	rum_write_multi(sc, reg, &tmp, sizeof tmp);
1443 }
1444 
1445 Static void
1446 rum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len)
1447 {
1448 	usb_device_request_t req;
1449 	usbd_status error;
1450 
1451 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1452 	req.bRequest = RT2573_WRITE_MULTI_MAC;
1453 	USETW(req.wValue, 0);
1454 	USETW(req.wIndex, reg);
1455 	USETW(req.wLength, len);
1456 
1457 	error = usbd_do_request(sc->sc_udev, &req, buf);
1458 	if (error != 0) {
1459 		printf("%s: could not multi write MAC register: %s\n",
1460 		    USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1461 	}
1462 }
1463 
1464 Static void
1465 rum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val)
1466 {
1467 	uint32_t tmp;
1468 	int ntries;
1469 
1470 	for (ntries = 0; ntries < 5; ntries++) {
1471 		if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1472 			break;
1473 	}
1474 	if (ntries == 5) {
1475 		printf("%s: could not write to BBP\n", USBDEVNAME(sc->sc_dev));
1476 		return;
1477 	}
1478 
1479 	tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val;
1480 	rum_write(sc, RT2573_PHY_CSR3, tmp);
1481 }
1482 
1483 Static uint8_t
1484 rum_bbp_read(struct rum_softc *sc, uint8_t reg)
1485 {
1486 	uint32_t val;
1487 	int ntries;
1488 
1489 	for (ntries = 0; ntries < 5; ntries++) {
1490 		if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1491 			break;
1492 	}
1493 	if (ntries == 5) {
1494 		printf("%s: could not read BBP\n", USBDEVNAME(sc->sc_dev));
1495 		return 0;
1496 	}
1497 
1498 	val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8;
1499 	rum_write(sc, RT2573_PHY_CSR3, val);
1500 
1501 	for (ntries = 0; ntries < 100; ntries++) {
1502 		val = rum_read(sc, RT2573_PHY_CSR3);
1503 		if (!(val & RT2573_BBP_BUSY))
1504 			return val & 0xff;
1505 		DELAY(1);
1506 	}
1507 
1508 	printf("%s: could not read BBP\n", USBDEVNAME(sc->sc_dev));
1509 	return 0;
1510 }
1511 
1512 Static void
1513 rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val)
1514 {
1515 	uint32_t tmp;
1516 	int ntries;
1517 
1518 	for (ntries = 0; ntries < 5; ntries++) {
1519 		if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY))
1520 			break;
1521 	}
1522 	if (ntries == 5) {
1523 		printf("%s: could not write to RF\n", USBDEVNAME(sc->sc_dev));
1524 		return;
1525 	}
1526 
1527 	tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 |
1528 	    (reg & 3);
1529 	rum_write(sc, RT2573_PHY_CSR4, tmp);
1530 
1531 	/* remember last written value in sc */
1532 	sc->rf_regs[reg] = val;
1533 
1534 	DPRINTFN(15, ("RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff));
1535 }
1536 
1537 Static void
1538 rum_select_antenna(struct rum_softc *sc)
1539 {
1540 	uint8_t bbp4, bbp77;
1541 	uint32_t tmp;
1542 
1543 	bbp4  = rum_bbp_read(sc, 4);
1544 	bbp77 = rum_bbp_read(sc, 77);
1545 
1546 	/* TBD */
1547 
1548 	/* make sure Rx is disabled before switching antenna */
1549 	tmp = rum_read(sc, RT2573_TXRX_CSR0);
1550 	rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
1551 
1552 	rum_bbp_write(sc,  4, bbp4);
1553 	rum_bbp_write(sc, 77, bbp77);
1554 
1555 	rum_write(sc, RT2573_TXRX_CSR0, tmp);
1556 }
1557 
1558 /*
1559  * Enable multi-rate retries for frames sent at OFDM rates.
1560  * In 802.11b/g mode, allow fallback to CCK rates.
1561  */
1562 Static void
1563 rum_enable_mrr(struct rum_softc *sc)
1564 {
1565 	struct ieee80211com *ic = &sc->sc_ic;
1566 	uint32_t tmp;
1567 
1568 	tmp = rum_read(sc, RT2573_TXRX_CSR4);
1569 
1570 	tmp &= ~RT2573_MRR_CCK_FALLBACK;
1571 	if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan))
1572 		tmp |= RT2573_MRR_CCK_FALLBACK;
1573 	tmp |= RT2573_MRR_ENABLED;
1574 
1575 	rum_write(sc, RT2573_TXRX_CSR4, tmp);
1576 }
1577 
1578 Static void
1579 rum_set_txpreamble(struct rum_softc *sc)
1580 {
1581 	uint32_t tmp;
1582 
1583 	tmp = rum_read(sc, RT2573_TXRX_CSR4);
1584 
1585 	tmp &= ~RT2573_SHORT_PREAMBLE;
1586 	if (sc->sc_ic.ic_flags & IEEE80211_F_SHPREAMBLE)
1587 		tmp |= RT2573_SHORT_PREAMBLE;
1588 
1589 	rum_write(sc, RT2573_TXRX_CSR4, tmp);
1590 }
1591 
1592 Static void
1593 rum_set_basicrates(struct rum_softc *sc)
1594 {
1595 	struct ieee80211com *ic = &sc->sc_ic;
1596 
1597 	/* update basic rate set */
1598 	if (ic->ic_curmode == IEEE80211_MODE_11B) {
1599 		/* 11b basic rates: 1, 2Mbps */
1600 		rum_write(sc, RT2573_TXRX_CSR5, 0x3);
1601 	} else if (ic->ic_curmode == IEEE80211_MODE_11A) {
1602 		/* 11a basic rates: 6, 12, 24Mbps */
1603 		rum_write(sc, RT2573_TXRX_CSR5, 0x150);
1604 	} else {
1605 		/* 11b/g basic rates: 1, 2, 5.5, 11Mbps */
1606 		rum_write(sc, RT2573_TXRX_CSR5, 0xf);
1607 	}
1608 }
1609 
1610 /*
1611  * Reprogram MAC/BBP to switch to a new band.  Values taken from the reference
1612  * driver.
1613  */
1614 Static void
1615 rum_select_band(struct rum_softc *sc, struct ieee80211_channel *c)
1616 {
1617 	uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104;
1618 	uint32_t tmp;
1619 
1620 	/* update all BBP registers that depend on the band */
1621 	bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c;
1622 	bbp35 = 0x50; bbp97 = 0x48; bbp98  = 0x48;
1623 	if (IEEE80211_IS_CHAN_5GHZ(c)) {
1624 		bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c;
1625 		bbp35 += 0x10; bbp97 += 0x10; bbp98  += 0x10;
1626 	}
1627 	if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1628 	    (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1629 		bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10;
1630 	}
1631 
1632 	sc->bbp17 = bbp17;
1633 	rum_bbp_write(sc,  17, bbp17);
1634 	rum_bbp_write(sc,  96, bbp96);
1635 	rum_bbp_write(sc, 104, bbp104);
1636 
1637 	if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1638 	    (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1639 		rum_bbp_write(sc, 75, 0x80);
1640 		rum_bbp_write(sc, 86, 0x80);
1641 		rum_bbp_write(sc, 88, 0x80);
1642 	}
1643 
1644 	rum_bbp_write(sc, 35, bbp35);
1645 	rum_bbp_write(sc, 97, bbp97);
1646 	rum_bbp_write(sc, 98, bbp98);
1647 
1648 	tmp = rum_read(sc, RT2573_PHY_CSR0);
1649 	tmp &= ~(RT2573_PA_PE_2GHZ | RT2573_PA_PE_5GHZ);
1650 	if (IEEE80211_IS_CHAN_2GHZ(c))
1651 		tmp |= RT2573_PA_PE_2GHZ;
1652 	else
1653 		tmp |= RT2573_PA_PE_5GHZ;
1654 	rum_write(sc, RT2573_PHY_CSR0, tmp);
1655 
1656 	/* 802.11a uses a 16 microseconds short interframe space */
1657 	sc->sifs = IEEE80211_IS_CHAN_5GHZ(c) ? 16 : 10;
1658 }
1659 
1660 Static void
1661 rum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c)
1662 {
1663 	struct ieee80211com *ic = &sc->sc_ic;
1664 	const struct rfprog *rfprog;
1665 	uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT;
1666 	int8_t power;
1667 	u_int i, chan;
1668 
1669 	chan = ieee80211_chan2ieee(ic, c);
1670 	if (chan == 0 || chan == IEEE80211_CHAN_ANY)
1671 		return;
1672 
1673 	/* select the appropriate RF settings based on what EEPROM says */
1674 	rfprog = (sc->rf_rev == RT2573_RF_5225 ||
1675 		  sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226;
1676 
1677 	/* find the settings for this channel (we know it exists) */
1678 	for (i = 0; rfprog[i].chan != chan; i++);
1679 
1680 	power = sc->txpow[i];
1681 	if (power < 0) {
1682 		bbp94 += power;
1683 		power = 0;
1684 	} else if (power > 31) {
1685 		bbp94 += power - 31;
1686 		power = 31;
1687 	}
1688 
1689 	/*
1690 	 * If we are switching from the 2GHz band to the 5GHz band or
1691 	 * vice-versa, BBP registers need to be reprogrammed.
1692 	 */
1693 	if (c->ic_flags != ic->ic_curchan->ic_flags) {
1694 		rum_select_band(sc, c);
1695 		rum_select_antenna(sc);
1696 	}
1697 	ic->ic_curchan = c;
1698 
1699 	rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1700 	rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1701 	rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1702 	rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1703 
1704 	rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1705 	rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1706 	rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7 | 1);
1707 	rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1708 
1709 	rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1710 	rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1711 	rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1712 	rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1713 
1714 	DELAY(10);
1715 
1716 	/* enable smart mode for MIMO-capable RFs */
1717 	bbp3 = rum_bbp_read(sc, 3);
1718 
1719 	bbp3 &= ~RT2573_SMART_MODE;
1720 	if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527)
1721 		bbp3 |= RT2573_SMART_MODE;
1722 
1723 	rum_bbp_write(sc, 3, bbp3);
1724 
1725 	if (bbp94 != RT2573_BBPR94_DEFAULT)
1726 		rum_bbp_write(sc, 94, bbp94);
1727 }
1728 
1729 /*
1730  * Enable TSF synchronization and tell h/w to start sending beacons for IBSS
1731  * and HostAP operating modes.
1732  */
1733 Static void
1734 rum_enable_tsf_sync(struct rum_softc *sc)
1735 {
1736 	struct ieee80211com *ic = &sc->sc_ic;
1737 	uint32_t tmp;
1738 
1739 	if (ic->ic_opmode != IEEE80211_M_STA) {
1740 		/*
1741 		 * Change default 16ms TBTT adjustment to 8ms.
1742 		 * Must be done before enabling beacon generation.
1743 		 */
1744 		rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8);
1745 	}
1746 
1747 	tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000;
1748 
1749 	/* set beacon interval (in 1/16ms unit) */
1750 	tmp |= ic->ic_bss->ni_intval * 16;
1751 
1752 	tmp |= RT2573_TSF_TICKING | RT2573_ENABLE_TBTT;
1753 	if (ic->ic_opmode == IEEE80211_M_STA)
1754 		tmp |= RT2573_TSF_MODE(1);
1755 	else
1756 		tmp |= RT2573_TSF_MODE(2) | RT2573_GENERATE_BEACON;
1757 
1758 	rum_write(sc, RT2573_TXRX_CSR9, tmp);
1759 }
1760 
1761 Static void
1762 rum_update_slot(struct rum_softc *sc)
1763 {
1764 	struct ieee80211com *ic = &sc->sc_ic;
1765 	uint8_t slottime;
1766 	uint32_t tmp;
1767 
1768 	slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
1769 
1770 	tmp = rum_read(sc, RT2573_MAC_CSR9);
1771 	tmp = (tmp & ~0xff) | slottime;
1772 	rum_write(sc, RT2573_MAC_CSR9, tmp);
1773 
1774 	DPRINTF(("setting slot time to %uus\n", slottime));
1775 }
1776 
1777 Static void
1778 rum_set_bssid(struct rum_softc *sc, const uint8_t *bssid)
1779 {
1780 	uint32_t tmp;
1781 
1782 	tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24;
1783 	rum_write(sc, RT2573_MAC_CSR4, tmp);
1784 
1785 	tmp = bssid[4] | bssid[5] << 8 | RT2573_ONE_BSSID << 16;
1786 	rum_write(sc, RT2573_MAC_CSR5, tmp);
1787 }
1788 
1789 Static void
1790 rum_set_macaddr(struct rum_softc *sc, const uint8_t *addr)
1791 {
1792 	uint32_t tmp;
1793 
1794 	tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24;
1795 	rum_write(sc, RT2573_MAC_CSR2, tmp);
1796 
1797 	tmp = addr[4] | addr[5] << 8 | 0xff << 16;
1798 	rum_write(sc, RT2573_MAC_CSR3, tmp);
1799 }
1800 
1801 Static void
1802 rum_update_promisc(struct rum_softc *sc)
1803 {
1804 	struct ifnet *ifp = sc->sc_ic.ic_ifp;
1805 	uint32_t tmp;
1806 
1807 	tmp = rum_read(sc, RT2573_TXRX_CSR0);
1808 
1809 	tmp &= ~RT2573_DROP_NOT_TO_ME;
1810 	if (!(ifp->if_flags & IFF_PROMISC))
1811 		tmp |= RT2573_DROP_NOT_TO_ME;
1812 
1813 	rum_write(sc, RT2573_TXRX_CSR0, tmp);
1814 
1815 	DPRINTF(("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
1816 	    "entering" : "leaving"));
1817 }
1818 
1819 Static const char *
1820 rum_get_rf(int rev)
1821 {
1822 	switch (rev) {
1823 	case RT2573_RF_2527:	return "RT2527 (MIMO XR)";
1824 	case RT2573_RF_2528:	return "RT2528";
1825 	case RT2573_RF_5225:	return "RT5225 (MIMO XR)";
1826 	case RT2573_RF_5226:	return "RT5226";
1827 	default:		return "unknown";
1828 	}
1829 }
1830 
1831 Static void
1832 rum_read_eeprom(struct rum_softc *sc)
1833 {
1834 	struct ieee80211com *ic = &sc->sc_ic;
1835 	uint16_t val;
1836 #ifdef RUM_DEBUG
1837 	int i;
1838 #endif
1839 
1840 	/* read MAC/BBP type */
1841 	rum_eeprom_read(sc, RT2573_EEPROM_MACBBP, &val, 2);
1842 	sc->macbbp_rev = le16toh(val);
1843 
1844 	/* read MAC address */
1845 	rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, ic->ic_myaddr, 6);
1846 
1847 	rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2);
1848 	val = le16toh(val);
1849 	sc->rf_rev =   (val >> 11) & 0x1f;
1850 	sc->hw_radio = (val >> 10) & 0x1;
1851 	sc->rx_ant =   (val >> 4)  & 0x3;
1852 	sc->tx_ant =   (val >> 2)  & 0x3;
1853 	sc->nb_ant =   val & 0x3;
1854 
1855 	DPRINTF(("RF revision=%d\n", sc->rf_rev));
1856 
1857 	rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2);
1858 	val = le16toh(val);
1859 	sc->ext_5ghz_lna = (val >> 6) & 0x1;
1860 	sc->ext_2ghz_lna = (val >> 4) & 0x1;
1861 
1862 	DPRINTF(("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n",
1863 	    sc->ext_2ghz_lna, sc->ext_5ghz_lna));
1864 
1865 	rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2);
1866 	val = le16toh(val);
1867 	if ((val & 0xff) != 0xff)
1868 		sc->rssi_2ghz_corr = (int8_t)(val & 0xff);	/* signed */
1869 
1870 	rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2);
1871 	val = le16toh(val);
1872 	if ((val & 0xff) != 0xff)
1873 		sc->rssi_5ghz_corr = (int8_t)(val & 0xff);	/* signed */
1874 
1875 	DPRINTF(("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n",
1876 	    sc->rssi_2ghz_corr, sc->rssi_5ghz_corr));
1877 
1878 	rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2);
1879 	val = le16toh(val);
1880 	if ((val & 0xff) != 0xff)
1881 		sc->rffreq = val & 0xff;
1882 
1883 	DPRINTF(("RF freq=%d\n", sc->rffreq));
1884 
1885 	/* read Tx power for all a/b/g channels */
1886 	rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14);
1887 	/* XXX default Tx power for 802.11a channels */
1888 	memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14);
1889 #ifdef RUM_DEBUG
1890 	for (i = 0; i < 14; i++)
1891 		DPRINTF(("Channel=%d Tx power=%d\n", i + 1,  sc->txpow[i]));
1892 #endif
1893 
1894 	/* read default values for BBP registers */
1895 	rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
1896 #ifdef RUM_DEBUG
1897 	for (i = 0; i < 14; i++) {
1898 		if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
1899 			continue;
1900 		DPRINTF(("BBP R%d=%02x\n", sc->bbp_prom[i].reg,
1901 		    sc->bbp_prom[i].val));
1902 	}
1903 #endif
1904 }
1905 
1906 Static int
1907 rum_bbp_init(struct rum_softc *sc)
1908 {
1909 #define N(a)	(sizeof (a) / sizeof ((a)[0]))
1910 	int i, ntries;
1911 	uint8_t val;
1912 
1913 	/* wait for BBP to be ready */
1914 	for (ntries = 0; ntries < 100; ntries++) {
1915 		val = rum_bbp_read(sc, 0);
1916 		if (val != 0 && val != 0xff)
1917 			break;
1918 		DELAY(1000);
1919 	}
1920 	if (ntries == 100) {
1921 		printf("%s: timeout waiting for BBP\n",
1922 		    USBDEVNAME(sc->sc_dev));
1923 		return EIO;
1924 	}
1925 
1926 	/* initialize BBP registers to default values */
1927 	for (i = 0; i < N(rum_def_bbp); i++)
1928 		rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val);
1929 
1930 	/* write vendor-specific BBP values (from EEPROM) */
1931 	for (i = 0; i < 16; i++) {
1932 		if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
1933 			continue;
1934 		rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
1935 	}
1936 
1937 	return 0;
1938 #undef N
1939 }
1940 
1941 Static int
1942 rum_init(struct ifnet *ifp)
1943 {
1944 #define N(a)	(sizeof (a) / sizeof ((a)[0]))
1945 	struct rum_softc *sc = ifp->if_softc;
1946 	struct ieee80211com *ic = &sc->sc_ic;
1947 	struct rum_rx_data *data;
1948 	uint32_t tmp;
1949 	usbd_status error = 0;
1950 	int i, ntries;
1951 
1952 	if ((sc->sc_flags & RT2573_FWLOADED) == 0) {
1953 		if (rum_attachhook(sc))
1954 			goto fail;
1955 	}
1956 
1957 	rum_stop(ifp, 0);
1958 
1959 	/* initialize MAC registers to default values */
1960 	for (i = 0; i < N(rum_def_mac); i++)
1961 		rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val);
1962 
1963 	/* set host ready */
1964 	rum_write(sc, RT2573_MAC_CSR1, 3);
1965 	rum_write(sc, RT2573_MAC_CSR1, 0);
1966 
1967 	/* wait for BBP/RF to wakeup */
1968 	for (ntries = 0; ntries < 1000; ntries++) {
1969 		if (rum_read(sc, RT2573_MAC_CSR12) & 8)
1970 			break;
1971 		rum_write(sc, RT2573_MAC_CSR12, 4);	/* force wakeup */
1972 		DELAY(1000);
1973 	}
1974 	if (ntries == 1000) {
1975 		printf("%s: timeout waiting for BBP/RF to wakeup\n",
1976 		    USBDEVNAME(sc->sc_dev));
1977 		goto fail;
1978 	}
1979 
1980 	if ((error = rum_bbp_init(sc)) != 0)
1981 		goto fail;
1982 
1983 	/* select default channel */
1984 	rum_select_band(sc, ic->ic_curchan);
1985 	rum_select_antenna(sc);
1986 	rum_set_chan(sc, ic->ic_curchan);
1987 
1988 	/* clear STA registers */
1989 	rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
1990 
1991 	IEEE80211_ADDR_COPY(ic->ic_myaddr, CLLADDR(ifp->if_sadl));
1992 	rum_set_macaddr(sc, ic->ic_myaddr);
1993 
1994 	/* initialize ASIC */
1995 	rum_write(sc, RT2573_MAC_CSR1, 4);
1996 
1997 	/*
1998 	 * Allocate xfer for AMRR statistics requests.
1999 	 */
2000 	sc->amrr_xfer = usbd_alloc_xfer(sc->sc_udev);
2001 	if (sc->amrr_xfer == NULL) {
2002 		printf("%s: could not allocate AMRR xfer\n",
2003 		    USBDEVNAME(sc->sc_dev));
2004 		goto fail;
2005 	}
2006 
2007 	/*
2008 	 * Open Tx and Rx USB bulk pipes.
2009 	 */
2010 	error = usbd_open_pipe(sc->sc_iface, sc->sc_tx_no, USBD_EXCLUSIVE_USE,
2011 	    &sc->sc_tx_pipeh);
2012 	if (error != 0) {
2013 		printf("%s: could not open Tx pipe: %s\n",
2014 		    USBDEVNAME(sc->sc_dev), usbd_errstr(error));
2015 		goto fail;
2016 	}
2017 
2018 	error = usbd_open_pipe(sc->sc_iface, sc->sc_rx_no, USBD_EXCLUSIVE_USE,
2019 	    &sc->sc_rx_pipeh);
2020 	if (error != 0) {
2021 		printf("%s: could not open Rx pipe: %s\n",
2022 		    USBDEVNAME(sc->sc_dev), usbd_errstr(error));
2023 		goto fail;
2024 	}
2025 
2026 	/*
2027 	 * Allocate Tx and Rx xfer queues.
2028 	 */
2029 	error = rum_alloc_tx_list(sc);
2030 	if (error != 0) {
2031 		printf("%s: could not allocate Tx list\n",
2032 		    USBDEVNAME(sc->sc_dev));
2033 		goto fail;
2034 	}
2035 
2036 	error = rum_alloc_rx_list(sc);
2037 	if (error != 0) {
2038 		printf("%s: could not allocate Rx list\n",
2039 		    USBDEVNAME(sc->sc_dev));
2040 		goto fail;
2041 	}
2042 
2043 	/*
2044 	 * Start up the receive pipe.
2045 	 */
2046 	for (i = 0; i < RUM_RX_LIST_COUNT; i++) {
2047 		data = &sc->rx_data[i];
2048 
2049 		usbd_setup_xfer(data->xfer, sc->sc_rx_pipeh, data, data->buf,
2050 		    MCLBYTES, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rum_rxeof);
2051 		error = usbd_transfer(data->xfer);
2052 		if (error != USBD_NORMAL_COMPLETION &&
2053 		    error != USBD_IN_PROGRESS) {
2054 			printf("%s: could not queue Rx transfer\n",
2055 			    USBDEVNAME(sc->sc_dev));
2056 			goto fail;
2057 		}
2058 	}
2059 
2060 	/* update Rx filter */
2061 	tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff;
2062 
2063 	tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR;
2064 	if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2065 		tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR |
2066 		       RT2573_DROP_ACKCTS;
2067 		if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2068 			tmp |= RT2573_DROP_TODS;
2069 		if (!(ifp->if_flags & IFF_PROMISC))
2070 			tmp |= RT2573_DROP_NOT_TO_ME;
2071 	}
2072 	rum_write(sc, RT2573_TXRX_CSR0, tmp);
2073 
2074 	ifp->if_flags &= ~IFF_OACTIVE;
2075 	ifp->if_flags |= IFF_RUNNING;
2076 
2077 	if (ic->ic_opmode == IEEE80211_M_MONITOR)
2078 		ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2079 	else
2080 		ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2081 
2082 	return 0;
2083 
2084 fail:	rum_stop(ifp, 1);
2085 	return error;
2086 #undef N
2087 }
2088 
2089 Static void
2090 rum_stop(struct ifnet *ifp, int disable)
2091 {
2092 	struct rum_softc *sc = ifp->if_softc;
2093 	struct ieee80211com *ic = &sc->sc_ic;
2094 	uint32_t tmp;
2095 
2096 	ieee80211_new_state(ic, IEEE80211_S_INIT, -1);	/* free all nodes */
2097 
2098 	sc->sc_tx_timer = 0;
2099 	ifp->if_timer = 0;
2100 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2101 
2102 	/* disable Rx */
2103 	tmp = rum_read(sc, RT2573_TXRX_CSR0);
2104 	rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
2105 
2106 	/* reset ASIC */
2107 	rum_write(sc, RT2573_MAC_CSR1, 3);
2108 	rum_write(sc, RT2573_MAC_CSR1, 0);
2109 
2110 	if (sc->sc_rx_pipeh != NULL) {
2111 		usbd_abort_pipe(sc->sc_rx_pipeh);
2112 		usbd_close_pipe(sc->sc_rx_pipeh);
2113 		sc->sc_rx_pipeh = NULL;
2114 	}
2115 
2116 	if (sc->sc_tx_pipeh != NULL) {
2117 		usbd_abort_pipe(sc->sc_tx_pipeh);
2118 		usbd_close_pipe(sc->sc_tx_pipeh);
2119 		sc->sc_tx_pipeh = NULL;
2120 	}
2121 
2122 	rum_free_rx_list(sc);
2123 	rum_free_tx_list(sc);
2124 }
2125 
2126 Static int
2127 rum_load_microcode(struct rum_softc *sc, const u_char *ucode, size_t size)
2128 {
2129 	usb_device_request_t req;
2130 	uint16_t reg = RT2573_MCU_CODE_BASE;
2131 	usbd_status error;
2132 
2133 	/* copy firmware image into NIC */
2134 	for (; size >= 4; reg += 4, ucode += 4, size -= 4)
2135 		rum_write(sc, reg, UGETDW(ucode));
2136 
2137 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2138 	req.bRequest = RT2573_MCU_CNTL;
2139 	USETW(req.wValue, RT2573_MCU_RUN);
2140 	USETW(req.wIndex, 0);
2141 	USETW(req.wLength, 0);
2142 
2143 	error = usbd_do_request(sc->sc_udev, &req, NULL);
2144 	if (error != 0) {
2145 		printf("%s: could not run firmware: %s\n",
2146 		    USBDEVNAME(sc->sc_dev), usbd_errstr(error));
2147 	}
2148 	return error;
2149 }
2150 
2151 Static int
2152 rum_prepare_beacon(struct rum_softc *sc)
2153 {
2154 	struct ieee80211com *ic = &sc->sc_ic;
2155 	struct rum_tx_desc desc;
2156 	struct mbuf *m0;
2157 	int rate;
2158 
2159 	m0 = ieee80211_beacon_alloc(ic, ic->ic_bss, &sc->sc_bo);
2160 	if (m0 == NULL) {
2161 		aprint_error_dev(sc->sc_dev,
2162 		    "could not allocate beacon frame\n");
2163 		return ENOBUFS;
2164 	}
2165 
2166 	/* send beacons at the lowest available rate */
2167 	rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
2168 
2169 	rum_setup_tx_desc(sc, &desc, RT2573_TX_TIMESTAMP, RT2573_TX_HWSEQ,
2170 	    m0->m_pkthdr.len, rate);
2171 
2172 	/* copy the first 24 bytes of Tx descriptor into NIC memory */
2173 	rum_write_multi(sc, RT2573_HW_BEACON_BASE0, (uint8_t *)&desc, 24);
2174 
2175 	/* copy beacon header and payload into NIC memory */
2176 	rum_write_multi(sc, RT2573_HW_BEACON_BASE0 + 24, mtod(m0, uint8_t *),
2177 	    m0->m_pkthdr.len);
2178 
2179 	m_freem(m0);
2180 
2181 	return 0;
2182 }
2183 
2184 Static void
2185 rum_newassoc(struct ieee80211_node *ni, int isnew)
2186 {
2187 	/* start with lowest Tx rate */
2188 	ni->ni_txrate = 0;
2189 }
2190 
2191 Static void
2192 rum_amrr_start(struct rum_softc *sc, struct ieee80211_node *ni)
2193 {
2194 	int i;
2195 
2196 	/* clear statistic registers (STA_CSR0 to STA_CSR5) */
2197 	rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
2198 
2199 	ieee80211_amrr_node_init(&sc->amrr, &sc->amn);
2200 
2201 	/* set rate to some reasonable initial value */
2202 	for (i = ni->ni_rates.rs_nrates - 1;
2203 	     i > 0 && (ni->ni_rates.rs_rates[i] & IEEE80211_RATE_VAL) > 72;
2204 	     i--);
2205 	ni->ni_txrate = i;
2206 
2207 	usb_callout(sc->sc_amrr_ch, hz, rum_amrr_timeout, sc);
2208 }
2209 
2210 Static void
2211 rum_amrr_timeout(void *arg)
2212 {
2213 	struct rum_softc *sc = arg;
2214 	usb_device_request_t req;
2215 
2216 	/*
2217 	 * Asynchronously read statistic registers (cleared by read).
2218 	 */
2219 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
2220 	req.bRequest = RT2573_READ_MULTI_MAC;
2221 	USETW(req.wValue, 0);
2222 	USETW(req.wIndex, RT2573_STA_CSR0);
2223 	USETW(req.wLength, sizeof sc->sta);
2224 
2225 	usbd_setup_default_xfer(sc->amrr_xfer, sc->sc_udev, sc,
2226 	    USBD_DEFAULT_TIMEOUT, &req, sc->sta, sizeof sc->sta, 0,
2227 	    rum_amrr_update);
2228 	(void)usbd_transfer(sc->amrr_xfer);
2229 }
2230 
2231 Static void
2232 rum_amrr_update(usbd_xfer_handle xfer, usbd_private_handle priv,
2233     usbd_status status)
2234 {
2235 	struct rum_softc *sc = (struct rum_softc *)priv;
2236 	struct ifnet *ifp = sc->sc_ic.ic_ifp;
2237 
2238 	if (status != USBD_NORMAL_COMPLETION) {
2239 		printf("%s: could not retrieve Tx statistics - cancelling "
2240 		    "automatic rate control\n", USBDEVNAME(sc->sc_dev));
2241 		return;
2242 	}
2243 
2244 	/* count TX retry-fail as Tx errors */
2245 	ifp->if_oerrors += le32toh(sc->sta[5]) >> 16;
2246 
2247 	sc->amn.amn_retrycnt =
2248 	    (le32toh(sc->sta[4]) >> 16) +	/* TX one-retry ok count */
2249 	    (le32toh(sc->sta[5]) & 0xffff) +	/* TX more-retry ok count */
2250 	    (le32toh(sc->sta[5]) >> 16);	/* TX retry-fail count */
2251 
2252 	sc->amn.amn_txcnt =
2253 	    sc->amn.amn_retrycnt +
2254 	    (le32toh(sc->sta[4]) & 0xffff);	/* TX no-retry ok count */
2255 
2256 	ieee80211_amrr_choose(&sc->amrr, sc->sc_ic.ic_bss, &sc->amn);
2257 
2258 	usb_callout(sc->sc_amrr_ch, hz, rum_amrr_timeout, sc);
2259 }
2260 
2261 int
2262 rum_activate(device_ptr_t self, enum devact act)
2263 {
2264 	switch (act) {
2265 	case DVACT_DEACTIVATE:
2266 		/*if_deactivate(&sc->sc_ic.ic_if);*/
2267 		return 0;
2268 	default:
2269 		return EOPNOTSUPP;
2270 	}
2271 }
2272