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