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