if_rum.c revision 288504
1/*	$FreeBSD: head/sys/dev/usb/wlan/if_rum.c 288504 2015-10-02 15:30:37Z adrian $	*/
2
3/*-
4 * Copyright (c) 2005-2007 Damien Bergamini <damien.bergamini@free.fr>
5 * Copyright (c) 2006 Niall O'Higgins <niallo@openbsd.org>
6 * Copyright (c) 2007-2008 Hans Petter Selasky <hselasky@FreeBSD.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#include <sys/cdefs.h>
22__FBSDID("$FreeBSD: head/sys/dev/usb/wlan/if_rum.c 288504 2015-10-02 15:30:37Z adrian $");
23
24/*-
25 * Ralink Technology RT2501USB/RT2601USB chipset driver
26 * http://www.ralinktech.com.tw/
27 */
28
29#include <sys/param.h>
30#include <sys/sockio.h>
31#include <sys/sysctl.h>
32#include <sys/lock.h>
33#include <sys/mutex.h>
34#include <sys/mbuf.h>
35#include <sys/kernel.h>
36#include <sys/socket.h>
37#include <sys/systm.h>
38#include <sys/malloc.h>
39#include <sys/module.h>
40#include <sys/bus.h>
41#include <sys/endian.h>
42#include <sys/kdb.h>
43
44#include <machine/bus.h>
45#include <machine/resource.h>
46#include <sys/rman.h>
47
48#include <net/bpf.h>
49#include <net/if.h>
50#include <net/if_var.h>
51#include <net/if_arp.h>
52#include <net/ethernet.h>
53#include <net/if_dl.h>
54#include <net/if_media.h>
55#include <net/if_types.h>
56
57#ifdef INET
58#include <netinet/in.h>
59#include <netinet/in_systm.h>
60#include <netinet/in_var.h>
61#include <netinet/if_ether.h>
62#include <netinet/ip.h>
63#endif
64
65#include <net80211/ieee80211_var.h>
66#include <net80211/ieee80211_regdomain.h>
67#include <net80211/ieee80211_radiotap.h>
68#include <net80211/ieee80211_ratectl.h>
69
70#include <dev/usb/usb.h>
71#include <dev/usb/usbdi.h>
72#include "usbdevs.h"
73
74#define	USB_DEBUG_VAR rum_debug
75#include <dev/usb/usb_debug.h>
76
77#include <dev/usb/wlan/if_rumreg.h>
78#include <dev/usb/wlan/if_rumvar.h>
79#include <dev/usb/wlan/if_rumfw.h>
80
81#ifdef USB_DEBUG
82static int rum_debug = 0;
83
84static SYSCTL_NODE(_hw_usb, OID_AUTO, rum, CTLFLAG_RW, 0, "USB rum");
85SYSCTL_INT(_hw_usb_rum, OID_AUTO, debug, CTLFLAG_RWTUN, &rum_debug, 0,
86    "Debug level");
87#endif
88
89#define N(a)	((int)(sizeof (a) / sizeof ((a)[0])))
90
91static const STRUCT_USB_HOST_ID rum_devs[] = {
92#define	RUM_DEV(v,p)  { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
93    RUM_DEV(ABOCOM, HWU54DM),
94    RUM_DEV(ABOCOM, RT2573_2),
95    RUM_DEV(ABOCOM, RT2573_3),
96    RUM_DEV(ABOCOM, RT2573_4),
97    RUM_DEV(ABOCOM, WUG2700),
98    RUM_DEV(AMIT, CGWLUSB2GO),
99    RUM_DEV(ASUS, RT2573_1),
100    RUM_DEV(ASUS, RT2573_2),
101    RUM_DEV(BELKIN, F5D7050A),
102    RUM_DEV(BELKIN, F5D9050V3),
103    RUM_DEV(CISCOLINKSYS, WUSB54GC),
104    RUM_DEV(CISCOLINKSYS, WUSB54GR),
105    RUM_DEV(CONCEPTRONIC2, C54RU2),
106    RUM_DEV(COREGA, CGWLUSB2GL),
107    RUM_DEV(COREGA, CGWLUSB2GPX),
108    RUM_DEV(DICKSMITH, CWD854F),
109    RUM_DEV(DICKSMITH, RT2573),
110    RUM_DEV(EDIMAX, EW7318USG),
111    RUM_DEV(DLINK2, DWLG122C1),
112    RUM_DEV(DLINK2, WUA1340),
113    RUM_DEV(DLINK2, DWA111),
114    RUM_DEV(DLINK2, DWA110),
115    RUM_DEV(GIGABYTE, GNWB01GS),
116    RUM_DEV(GIGABYTE, GNWI05GS),
117    RUM_DEV(GIGASET, RT2573),
118    RUM_DEV(GOODWAY, RT2573),
119    RUM_DEV(GUILLEMOT, HWGUSB254LB),
120    RUM_DEV(GUILLEMOT, HWGUSB254V2AP),
121    RUM_DEV(HUAWEI3COM, WUB320G),
122    RUM_DEV(MELCO, G54HP),
123    RUM_DEV(MELCO, SG54HP),
124    RUM_DEV(MELCO, SG54HG),
125    RUM_DEV(MELCO, WLIUCG),
126    RUM_DEV(MELCO, WLRUCG),
127    RUM_DEV(MELCO, WLRUCGAOSS),
128    RUM_DEV(MSI, RT2573_1),
129    RUM_DEV(MSI, RT2573_2),
130    RUM_DEV(MSI, RT2573_3),
131    RUM_DEV(MSI, RT2573_4),
132    RUM_DEV(NOVATECH, RT2573),
133    RUM_DEV(PLANEX2, GWUS54HP),
134    RUM_DEV(PLANEX2, GWUS54MINI2),
135    RUM_DEV(PLANEX2, GWUSMM),
136    RUM_DEV(QCOM, RT2573),
137    RUM_DEV(QCOM, RT2573_2),
138    RUM_DEV(QCOM, RT2573_3),
139    RUM_DEV(RALINK, RT2573),
140    RUM_DEV(RALINK, RT2573_2),
141    RUM_DEV(RALINK, RT2671),
142    RUM_DEV(SITECOMEU, WL113R2),
143    RUM_DEV(SITECOMEU, WL172),
144    RUM_DEV(SPARKLAN, RT2573),
145    RUM_DEV(SURECOM, RT2573),
146#undef RUM_DEV
147};
148
149static device_probe_t rum_match;
150static device_attach_t rum_attach;
151static device_detach_t rum_detach;
152
153static usb_callback_t rum_bulk_read_callback;
154static usb_callback_t rum_bulk_write_callback;
155
156static usb_error_t	rum_do_request(struct rum_softc *sc,
157			    struct usb_device_request *req, void *data);
158static struct ieee80211vap *rum_vap_create(struct ieee80211com *,
159			    const char [IFNAMSIZ], int, enum ieee80211_opmode,
160			    int, const uint8_t [IEEE80211_ADDR_LEN],
161			    const uint8_t [IEEE80211_ADDR_LEN]);
162static void		rum_vap_delete(struct ieee80211vap *);
163static void		rum_tx_free(struct rum_tx_data *, int);
164static void		rum_setup_tx_list(struct rum_softc *);
165static void		rum_unsetup_tx_list(struct rum_softc *);
166static int		rum_newstate(struct ieee80211vap *,
167			    enum ieee80211_state, int);
168static void		rum_setup_tx_desc(struct rum_softc *,
169			    struct rum_tx_desc *, uint32_t, uint16_t, int,
170			    int);
171static int		rum_tx_mgt(struct rum_softc *, struct mbuf *,
172			    struct ieee80211_node *);
173static int		rum_tx_raw(struct rum_softc *, struct mbuf *,
174			    struct ieee80211_node *,
175			    const struct ieee80211_bpf_params *);
176static int		rum_tx_data(struct rum_softc *, struct mbuf *,
177			    struct ieee80211_node *);
178static int		rum_transmit(struct ieee80211com *, struct mbuf *);
179static void		rum_start(struct rum_softc *);
180static void		rum_parent(struct ieee80211com *);
181static void		rum_eeprom_read(struct rum_softc *, uint16_t, void *,
182			    int);
183static uint32_t		rum_read(struct rum_softc *, uint16_t);
184static void		rum_read_multi(struct rum_softc *, uint16_t, void *,
185			    int);
186static usb_error_t	rum_write(struct rum_softc *, uint16_t, uint32_t);
187static usb_error_t	rum_write_multi(struct rum_softc *, uint16_t, void *,
188			    size_t);
189static usb_error_t	rum_setbits(struct rum_softc *, uint16_t, uint32_t);
190static usb_error_t	rum_clrbits(struct rum_softc *, uint16_t, uint32_t);
191static usb_error_t	rum_modbits(struct rum_softc *, uint16_t, uint32_t,
192			    uint32_t);
193static int		rum_bbp_busy(struct rum_softc *);
194static void		rum_bbp_write(struct rum_softc *, uint8_t, uint8_t);
195static uint8_t		rum_bbp_read(struct rum_softc *, uint8_t);
196static void		rum_rf_write(struct rum_softc *, uint8_t, uint32_t);
197static void		rum_select_antenna(struct rum_softc *);
198static void		rum_enable_mrr(struct rum_softc *);
199static void		rum_set_txpreamble(struct rum_softc *);
200static void		rum_set_basicrates(struct rum_softc *);
201static void		rum_select_band(struct rum_softc *,
202			    struct ieee80211_channel *);
203static void		rum_set_chan(struct rum_softc *,
204			    struct ieee80211_channel *);
205static void		rum_enable_tsf_sync(struct rum_softc *);
206static void		rum_enable_tsf(struct rum_softc *);
207static void		rum_abort_tsf_sync(struct rum_softc *);
208static void		rum_update_slot(struct rum_softc *);
209static void		rum_set_bssid(struct rum_softc *, const uint8_t *);
210static void		rum_set_macaddr(struct rum_softc *, const uint8_t *);
211static void		rum_update_mcast(struct ieee80211com *);
212static void		rum_update_promisc(struct ieee80211com *);
213static void		rum_setpromisc(struct rum_softc *);
214static const char	*rum_get_rf(int);
215static void		rum_read_eeprom(struct rum_softc *);
216static int		rum_bbp_init(struct rum_softc *);
217static void		rum_init(struct rum_softc *);
218static void		rum_stop(struct rum_softc *);
219static void		rum_load_microcode(struct rum_softc *, const uint8_t *,
220			    size_t);
221static void		rum_prepare_beacon(struct rum_softc *,
222			    struct ieee80211vap *);
223static int		rum_raw_xmit(struct ieee80211_node *, struct mbuf *,
224			    const struct ieee80211_bpf_params *);
225static void		rum_scan_start(struct ieee80211com *);
226static void		rum_scan_end(struct ieee80211com *);
227static void		rum_set_channel(struct ieee80211com *);
228static int		rum_get_rssi(struct rum_softc *, uint8_t);
229static void		rum_ratectl_start(struct rum_softc *,
230			    struct ieee80211_node *);
231static void		rum_ratectl_timeout(void *);
232static void		rum_ratectl_task(void *, int);
233static int		rum_pause(struct rum_softc *, int);
234
235static const struct {
236	uint32_t	reg;
237	uint32_t	val;
238} rum_def_mac[] = {
239	{ RT2573_TXRX_CSR0,  0x025fb032 },
240	{ RT2573_TXRX_CSR1,  0x9eaa9eaf },
241	{ RT2573_TXRX_CSR2,  0x8a8b8c8d },
242	{ RT2573_TXRX_CSR3,  0x00858687 },
243	{ RT2573_TXRX_CSR7,  0x2e31353b },
244	{ RT2573_TXRX_CSR8,  0x2a2a2a2c },
245	{ RT2573_TXRX_CSR15, 0x0000000f },
246	{ RT2573_MAC_CSR6,   0x00000fff },
247	{ RT2573_MAC_CSR8,   0x016c030a },
248	{ RT2573_MAC_CSR10,  0x00000718 },
249	{ RT2573_MAC_CSR12,  0x00000004 },
250	{ RT2573_MAC_CSR13,  0x00007f00 },
251	{ RT2573_SEC_CSR0,   0x00000000 },
252	{ RT2573_SEC_CSR1,   0x00000000 },
253	{ RT2573_SEC_CSR5,   0x00000000 },
254	{ RT2573_PHY_CSR1,   0x000023b0 },
255	{ RT2573_PHY_CSR5,   0x00040a06 },
256	{ RT2573_PHY_CSR6,   0x00080606 },
257	{ RT2573_PHY_CSR7,   0x00000408 },
258	{ RT2573_AIFSN_CSR,  0x00002273 },
259	{ RT2573_CWMIN_CSR,  0x00002344 },
260	{ RT2573_CWMAX_CSR,  0x000034aa }
261};
262
263static const struct {
264	uint8_t	reg;
265	uint8_t	val;
266} rum_def_bbp[] = {
267	{   3, 0x80 },
268	{  15, 0x30 },
269	{  17, 0x20 },
270	{  21, 0xc8 },
271	{  22, 0x38 },
272	{  23, 0x06 },
273	{  24, 0xfe },
274	{  25, 0x0a },
275	{  26, 0x0d },
276	{  32, 0x0b },
277	{  34, 0x12 },
278	{  37, 0x07 },
279	{  39, 0xf8 },
280	{  41, 0x60 },
281	{  53, 0x10 },
282	{  54, 0x18 },
283	{  60, 0x10 },
284	{  61, 0x04 },
285	{  62, 0x04 },
286	{  75, 0xfe },
287	{  86, 0xfe },
288	{  88, 0xfe },
289	{  90, 0x0f },
290	{  99, 0x00 },
291	{ 102, 0x16 },
292	{ 107, 0x04 }
293};
294
295static const struct rfprog {
296	uint8_t		chan;
297	uint32_t	r1, r2, r3, r4;
298}  rum_rf5226[] = {
299	{   1, 0x00b03, 0x001e1, 0x1a014, 0x30282 },
300	{   2, 0x00b03, 0x001e1, 0x1a014, 0x30287 },
301	{   3, 0x00b03, 0x001e2, 0x1a014, 0x30282 },
302	{   4, 0x00b03, 0x001e2, 0x1a014, 0x30287 },
303	{   5, 0x00b03, 0x001e3, 0x1a014, 0x30282 },
304	{   6, 0x00b03, 0x001e3, 0x1a014, 0x30287 },
305	{   7, 0x00b03, 0x001e4, 0x1a014, 0x30282 },
306	{   8, 0x00b03, 0x001e4, 0x1a014, 0x30287 },
307	{   9, 0x00b03, 0x001e5, 0x1a014, 0x30282 },
308	{  10, 0x00b03, 0x001e5, 0x1a014, 0x30287 },
309	{  11, 0x00b03, 0x001e6, 0x1a014, 0x30282 },
310	{  12, 0x00b03, 0x001e6, 0x1a014, 0x30287 },
311	{  13, 0x00b03, 0x001e7, 0x1a014, 0x30282 },
312	{  14, 0x00b03, 0x001e8, 0x1a014, 0x30284 },
313
314	{  34, 0x00b03, 0x20266, 0x36014, 0x30282 },
315	{  38, 0x00b03, 0x20267, 0x36014, 0x30284 },
316	{  42, 0x00b03, 0x20268, 0x36014, 0x30286 },
317	{  46, 0x00b03, 0x20269, 0x36014, 0x30288 },
318
319	{  36, 0x00b03, 0x00266, 0x26014, 0x30288 },
320	{  40, 0x00b03, 0x00268, 0x26014, 0x30280 },
321	{  44, 0x00b03, 0x00269, 0x26014, 0x30282 },
322	{  48, 0x00b03, 0x0026a, 0x26014, 0x30284 },
323	{  52, 0x00b03, 0x0026b, 0x26014, 0x30286 },
324	{  56, 0x00b03, 0x0026c, 0x26014, 0x30288 },
325	{  60, 0x00b03, 0x0026e, 0x26014, 0x30280 },
326	{  64, 0x00b03, 0x0026f, 0x26014, 0x30282 },
327
328	{ 100, 0x00b03, 0x0028a, 0x2e014, 0x30280 },
329	{ 104, 0x00b03, 0x0028b, 0x2e014, 0x30282 },
330	{ 108, 0x00b03, 0x0028c, 0x2e014, 0x30284 },
331	{ 112, 0x00b03, 0x0028d, 0x2e014, 0x30286 },
332	{ 116, 0x00b03, 0x0028e, 0x2e014, 0x30288 },
333	{ 120, 0x00b03, 0x002a0, 0x2e014, 0x30280 },
334	{ 124, 0x00b03, 0x002a1, 0x2e014, 0x30282 },
335	{ 128, 0x00b03, 0x002a2, 0x2e014, 0x30284 },
336	{ 132, 0x00b03, 0x002a3, 0x2e014, 0x30286 },
337	{ 136, 0x00b03, 0x002a4, 0x2e014, 0x30288 },
338	{ 140, 0x00b03, 0x002a6, 0x2e014, 0x30280 },
339
340	{ 149, 0x00b03, 0x002a8, 0x2e014, 0x30287 },
341	{ 153, 0x00b03, 0x002a9, 0x2e014, 0x30289 },
342	{ 157, 0x00b03, 0x002ab, 0x2e014, 0x30281 },
343	{ 161, 0x00b03, 0x002ac, 0x2e014, 0x30283 },
344	{ 165, 0x00b03, 0x002ad, 0x2e014, 0x30285 }
345}, rum_rf5225[] = {
346	{   1, 0x00b33, 0x011e1, 0x1a014, 0x30282 },
347	{   2, 0x00b33, 0x011e1, 0x1a014, 0x30287 },
348	{   3, 0x00b33, 0x011e2, 0x1a014, 0x30282 },
349	{   4, 0x00b33, 0x011e2, 0x1a014, 0x30287 },
350	{   5, 0x00b33, 0x011e3, 0x1a014, 0x30282 },
351	{   6, 0x00b33, 0x011e3, 0x1a014, 0x30287 },
352	{   7, 0x00b33, 0x011e4, 0x1a014, 0x30282 },
353	{   8, 0x00b33, 0x011e4, 0x1a014, 0x30287 },
354	{   9, 0x00b33, 0x011e5, 0x1a014, 0x30282 },
355	{  10, 0x00b33, 0x011e5, 0x1a014, 0x30287 },
356	{  11, 0x00b33, 0x011e6, 0x1a014, 0x30282 },
357	{  12, 0x00b33, 0x011e6, 0x1a014, 0x30287 },
358	{  13, 0x00b33, 0x011e7, 0x1a014, 0x30282 },
359	{  14, 0x00b33, 0x011e8, 0x1a014, 0x30284 },
360
361	{  34, 0x00b33, 0x01266, 0x26014, 0x30282 },
362	{  38, 0x00b33, 0x01267, 0x26014, 0x30284 },
363	{  42, 0x00b33, 0x01268, 0x26014, 0x30286 },
364	{  46, 0x00b33, 0x01269, 0x26014, 0x30288 },
365
366	{  36, 0x00b33, 0x01266, 0x26014, 0x30288 },
367	{  40, 0x00b33, 0x01268, 0x26014, 0x30280 },
368	{  44, 0x00b33, 0x01269, 0x26014, 0x30282 },
369	{  48, 0x00b33, 0x0126a, 0x26014, 0x30284 },
370	{  52, 0x00b33, 0x0126b, 0x26014, 0x30286 },
371	{  56, 0x00b33, 0x0126c, 0x26014, 0x30288 },
372	{  60, 0x00b33, 0x0126e, 0x26014, 0x30280 },
373	{  64, 0x00b33, 0x0126f, 0x26014, 0x30282 },
374
375	{ 100, 0x00b33, 0x0128a, 0x2e014, 0x30280 },
376	{ 104, 0x00b33, 0x0128b, 0x2e014, 0x30282 },
377	{ 108, 0x00b33, 0x0128c, 0x2e014, 0x30284 },
378	{ 112, 0x00b33, 0x0128d, 0x2e014, 0x30286 },
379	{ 116, 0x00b33, 0x0128e, 0x2e014, 0x30288 },
380	{ 120, 0x00b33, 0x012a0, 0x2e014, 0x30280 },
381	{ 124, 0x00b33, 0x012a1, 0x2e014, 0x30282 },
382	{ 128, 0x00b33, 0x012a2, 0x2e014, 0x30284 },
383	{ 132, 0x00b33, 0x012a3, 0x2e014, 0x30286 },
384	{ 136, 0x00b33, 0x012a4, 0x2e014, 0x30288 },
385	{ 140, 0x00b33, 0x012a6, 0x2e014, 0x30280 },
386
387	{ 149, 0x00b33, 0x012a8, 0x2e014, 0x30287 },
388	{ 153, 0x00b33, 0x012a9, 0x2e014, 0x30289 },
389	{ 157, 0x00b33, 0x012ab, 0x2e014, 0x30281 },
390	{ 161, 0x00b33, 0x012ac, 0x2e014, 0x30283 },
391	{ 165, 0x00b33, 0x012ad, 0x2e014, 0x30285 }
392};
393
394static const struct usb_config rum_config[RUM_N_TRANSFER] = {
395	[RUM_BULK_WR] = {
396		.type = UE_BULK,
397		.endpoint = UE_ADDR_ANY,
398		.direction = UE_DIR_OUT,
399		.bufsize = (MCLBYTES + RT2573_TX_DESC_SIZE + 8),
400		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
401		.callback = rum_bulk_write_callback,
402		.timeout = 5000,	/* ms */
403	},
404	[RUM_BULK_RD] = {
405		.type = UE_BULK,
406		.endpoint = UE_ADDR_ANY,
407		.direction = UE_DIR_IN,
408		.bufsize = (MCLBYTES + RT2573_RX_DESC_SIZE),
409		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
410		.callback = rum_bulk_read_callback,
411	},
412};
413
414static int
415rum_match(device_t self)
416{
417	struct usb_attach_arg *uaa = device_get_ivars(self);
418
419	if (uaa->usb_mode != USB_MODE_HOST)
420		return (ENXIO);
421	if (uaa->info.bConfigIndex != 0)
422		return (ENXIO);
423	if (uaa->info.bIfaceIndex != RT2573_IFACE_INDEX)
424		return (ENXIO);
425
426	return (usbd_lookup_id_by_uaa(rum_devs, sizeof(rum_devs), uaa));
427}
428
429static int
430rum_attach(device_t self)
431{
432	struct usb_attach_arg *uaa = device_get_ivars(self);
433	struct rum_softc *sc = device_get_softc(self);
434	struct ieee80211com *ic = &sc->sc_ic;
435	uint8_t iface_index, bands;
436	uint32_t tmp;
437	int error, ntries;
438
439	device_set_usb_desc(self);
440	sc->sc_udev = uaa->device;
441	sc->sc_dev = self;
442
443	mtx_init(&sc->sc_mtx, device_get_nameunit(self),
444	    MTX_NETWORK_LOCK, MTX_DEF);
445	mbufq_init(&sc->sc_snd, ifqmaxlen);
446
447	iface_index = RT2573_IFACE_INDEX;
448	error = usbd_transfer_setup(uaa->device, &iface_index,
449	    sc->sc_xfer, rum_config, RUM_N_TRANSFER, sc, &sc->sc_mtx);
450	if (error) {
451		device_printf(self, "could not allocate USB transfers, "
452		    "err=%s\n", usbd_errstr(error));
453		goto detach;
454	}
455
456	RUM_LOCK(sc);
457	/* retrieve RT2573 rev. no */
458	for (ntries = 0; ntries < 100; ntries++) {
459		if ((tmp = rum_read(sc, RT2573_MAC_CSR0)) != 0)
460			break;
461		if (rum_pause(sc, hz / 100))
462			break;
463	}
464	if (ntries == 100) {
465		device_printf(sc->sc_dev, "timeout waiting for chip to settle\n");
466		RUM_UNLOCK(sc);
467		goto detach;
468	}
469
470	/* retrieve MAC address and various other things from EEPROM */
471	rum_read_eeprom(sc);
472
473	device_printf(sc->sc_dev, "MAC/BBP RT2573 (rev 0x%05x), RF %s\n",
474	    tmp, rum_get_rf(sc->rf_rev));
475
476	rum_load_microcode(sc, rt2573_ucode, sizeof(rt2573_ucode));
477	RUM_UNLOCK(sc);
478
479	ic->ic_softc = sc;
480	ic->ic_name = device_get_nameunit(self);
481	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
482
483	/* set device capabilities */
484	ic->ic_caps =
485	      IEEE80211_C_STA		/* station mode supported */
486	    | IEEE80211_C_IBSS		/* IBSS mode supported */
487	    | IEEE80211_C_MONITOR	/* monitor mode supported */
488	    | IEEE80211_C_HOSTAP	/* HostAp mode supported */
489	    | IEEE80211_C_TXPMGT	/* tx power management */
490	    | IEEE80211_C_SHPREAMBLE	/* short preamble supported */
491	    | IEEE80211_C_SHSLOT	/* short slot time supported */
492	    | IEEE80211_C_BGSCAN	/* bg scanning supported */
493	    | IEEE80211_C_WPA		/* 802.11i */
494	    ;
495
496	bands = 0;
497	setbit(&bands, IEEE80211_MODE_11B);
498	setbit(&bands, IEEE80211_MODE_11G);
499	if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_5226)
500		setbit(&bands, IEEE80211_MODE_11A);
501	ieee80211_init_channels(ic, NULL, &bands);
502
503	ieee80211_ifattach(ic);
504	ic->ic_update_promisc = rum_update_promisc;
505	ic->ic_raw_xmit = rum_raw_xmit;
506	ic->ic_scan_start = rum_scan_start;
507	ic->ic_scan_end = rum_scan_end;
508	ic->ic_set_channel = rum_set_channel;
509	ic->ic_transmit = rum_transmit;
510	ic->ic_parent = rum_parent;
511	ic->ic_vap_create = rum_vap_create;
512	ic->ic_vap_delete = rum_vap_delete;
513	ic->ic_update_mcast = rum_update_mcast;
514
515	ieee80211_radiotap_attach(ic,
516	    &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
517		RT2573_TX_RADIOTAP_PRESENT,
518	    &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
519		RT2573_RX_RADIOTAP_PRESENT);
520
521	if (bootverbose)
522		ieee80211_announce(ic);
523
524	return (0);
525
526detach:
527	rum_detach(self);
528	return (ENXIO);			/* failure */
529}
530
531static int
532rum_detach(device_t self)
533{
534	struct rum_softc *sc = device_get_softc(self);
535
536	/* Prevent further ioctls */
537	RUM_LOCK(sc);
538	sc->sc_detached = 1;
539	RUM_UNLOCK(sc);
540
541	/* stop all USB transfers */
542	usbd_transfer_unsetup(sc->sc_xfer, RUM_N_TRANSFER);
543
544	/* free TX list, if any */
545	RUM_LOCK(sc);
546	rum_unsetup_tx_list(sc);
547	RUM_UNLOCK(sc);
548
549	if (sc->sc_ic.ic_softc == sc)
550		ieee80211_ifdetach(&sc->sc_ic);
551	mbufq_drain(&sc->sc_snd);
552	mtx_destroy(&sc->sc_mtx);
553	return (0);
554}
555
556static usb_error_t
557rum_do_request(struct rum_softc *sc,
558    struct usb_device_request *req, void *data)
559{
560	usb_error_t err;
561	int ntries = 10;
562
563	while (ntries--) {
564		err = usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
565		    req, data, 0, NULL, 250 /* ms */);
566		if (err == 0)
567			break;
568
569		DPRINTFN(1, "Control request failed, %s (retrying)\n",
570		    usbd_errstr(err));
571		if (rum_pause(sc, hz / 100))
572			break;
573	}
574	return (err);
575}
576
577static struct ieee80211vap *
578rum_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
579    enum ieee80211_opmode opmode, int flags,
580    const uint8_t bssid[IEEE80211_ADDR_LEN],
581    const uint8_t mac[IEEE80211_ADDR_LEN])
582{
583	struct rum_softc *sc = ic->ic_softc;
584	struct rum_vap *rvp;
585	struct ieee80211vap *vap;
586
587	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
588		return NULL;
589	rvp = malloc(sizeof(struct rum_vap), M_80211_VAP, M_WAITOK | M_ZERO);
590	vap = &rvp->vap;
591	/* enable s/w bmiss handling for sta mode */
592
593	if (ieee80211_vap_setup(ic, vap, name, unit, opmode,
594	    flags | IEEE80211_CLONE_NOBEACONS, bssid) != 0) {
595		/* out of memory */
596		free(rvp, M_80211_VAP);
597		return (NULL);
598	}
599
600	/* override state transition machine */
601	rvp->newstate = vap->iv_newstate;
602	vap->iv_newstate = rum_newstate;
603
604	usb_callout_init_mtx(&rvp->ratectl_ch, &sc->sc_mtx, 0);
605	TASK_INIT(&rvp->ratectl_task, 0, rum_ratectl_task, rvp);
606	ieee80211_ratectl_init(vap);
607	ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */);
608	/* complete setup */
609	ieee80211_vap_attach(vap, ieee80211_media_change,
610	    ieee80211_media_status, mac);
611	ic->ic_opmode = opmode;
612	return vap;
613}
614
615static void
616rum_vap_delete(struct ieee80211vap *vap)
617{
618	struct rum_vap *rvp = RUM_VAP(vap);
619	struct ieee80211com *ic = vap->iv_ic;
620
621	usb_callout_drain(&rvp->ratectl_ch);
622	ieee80211_draintask(ic, &rvp->ratectl_task);
623	ieee80211_ratectl_deinit(vap);
624	ieee80211_vap_detach(vap);
625	free(rvp, M_80211_VAP);
626}
627
628static void
629rum_tx_free(struct rum_tx_data *data, int txerr)
630{
631	struct rum_softc *sc = data->sc;
632
633	if (data->m != NULL) {
634		ieee80211_tx_complete(data->ni, data->m, txerr);
635		data->m = NULL;
636		data->ni = NULL;
637	}
638	STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
639	sc->tx_nfree++;
640}
641
642static void
643rum_setup_tx_list(struct rum_softc *sc)
644{
645	struct rum_tx_data *data;
646	int i;
647
648	sc->tx_nfree = 0;
649	STAILQ_INIT(&sc->tx_q);
650	STAILQ_INIT(&sc->tx_free);
651
652	for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
653		data = &sc->tx_data[i];
654
655		data->sc = sc;
656		STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
657		sc->tx_nfree++;
658	}
659}
660
661static void
662rum_unsetup_tx_list(struct rum_softc *sc)
663{
664	struct rum_tx_data *data;
665	int i;
666
667	/* make sure any subsequent use of the queues will fail */
668	sc->tx_nfree = 0;
669	STAILQ_INIT(&sc->tx_q);
670	STAILQ_INIT(&sc->tx_free);
671
672	/* free up all node references and mbufs */
673	for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
674		data = &sc->tx_data[i];
675
676		if (data->m != NULL) {
677			m_freem(data->m);
678			data->m = NULL;
679		}
680		if (data->ni != NULL) {
681			ieee80211_free_node(data->ni);
682			data->ni = NULL;
683		}
684	}
685}
686
687static int
688rum_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
689{
690	struct rum_vap *rvp = RUM_VAP(vap);
691	struct ieee80211com *ic = vap->iv_ic;
692	struct rum_softc *sc = ic->ic_softc;
693	const struct ieee80211_txparam *tp;
694	enum ieee80211_state ostate;
695	struct ieee80211_node *ni;
696
697	ostate = vap->iv_state;
698	DPRINTF("%s -> %s\n",
699		ieee80211_state_name[ostate],
700		ieee80211_state_name[nstate]);
701
702	IEEE80211_UNLOCK(ic);
703	RUM_LOCK(sc);
704	usb_callout_stop(&rvp->ratectl_ch);
705
706	switch (nstate) {
707	case IEEE80211_S_INIT:
708		if (ostate == IEEE80211_S_RUN)
709			rum_abort_tsf_sync(sc);
710
711		break;
712
713	case IEEE80211_S_RUN:
714		ni = ieee80211_ref_node(vap->iv_bss);
715
716		if (vap->iv_opmode != IEEE80211_M_MONITOR) {
717			if (ic->ic_bsschan == IEEE80211_CHAN_ANYC) {
718				RUM_UNLOCK(sc);
719				IEEE80211_LOCK(ic);
720				ieee80211_free_node(ni);
721				return (-1);
722			}
723			rum_update_slot(sc);
724			rum_enable_mrr(sc);
725			rum_set_txpreamble(sc);
726			rum_set_basicrates(sc);
727			IEEE80211_ADDR_COPY(ic->ic_macaddr, ni->ni_bssid);
728			rum_set_bssid(sc, ic->ic_macaddr);
729		}
730
731		if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
732		    vap->iv_opmode == IEEE80211_M_IBSS)
733			rum_prepare_beacon(sc, vap);
734
735		if (vap->iv_opmode != IEEE80211_M_MONITOR)
736			rum_enable_tsf_sync(sc);
737		else
738			rum_enable_tsf(sc);
739
740		/* enable automatic rate adaptation */
741		tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
742		if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
743			rum_ratectl_start(sc, ni);
744		ieee80211_free_node(ni);
745		break;
746	default:
747		break;
748	}
749	RUM_UNLOCK(sc);
750	IEEE80211_LOCK(ic);
751	return (rvp->newstate(vap, nstate, arg));
752}
753
754static void
755rum_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
756{
757	struct rum_softc *sc = usbd_xfer_softc(xfer);
758	struct ieee80211vap *vap;
759	struct rum_tx_data *data;
760	struct mbuf *m;
761	struct usb_page_cache *pc;
762	unsigned int len;
763	int actlen, sumlen;
764
765	usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
766
767	switch (USB_GET_STATE(xfer)) {
768	case USB_ST_TRANSFERRED:
769		DPRINTFN(11, "transfer complete, %d bytes\n", actlen);
770
771		/* free resources */
772		data = usbd_xfer_get_priv(xfer);
773		rum_tx_free(data, 0);
774		usbd_xfer_set_priv(xfer, NULL);
775
776		/* FALLTHROUGH */
777	case USB_ST_SETUP:
778tr_setup:
779		data = STAILQ_FIRST(&sc->tx_q);
780		if (data) {
781			STAILQ_REMOVE_HEAD(&sc->tx_q, next);
782			m = data->m;
783
784			if (m->m_pkthdr.len > (int)(MCLBYTES + RT2573_TX_DESC_SIZE)) {
785				DPRINTFN(0, "data overflow, %u bytes\n",
786				    m->m_pkthdr.len);
787				m->m_pkthdr.len = (MCLBYTES + RT2573_TX_DESC_SIZE);
788			}
789			pc = usbd_xfer_get_frame(xfer, 0);
790			usbd_copy_in(pc, 0, &data->desc, RT2573_TX_DESC_SIZE);
791			usbd_m_copy_in(pc, RT2573_TX_DESC_SIZE, m, 0,
792			    m->m_pkthdr.len);
793
794			vap = data->ni->ni_vap;
795			if (ieee80211_radiotap_active_vap(vap)) {
796				struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
797
798				tap->wt_flags = 0;
799				tap->wt_rate = data->rate;
800				tap->wt_antenna = sc->tx_ant;
801
802				ieee80211_radiotap_tx(vap, m);
803			}
804
805			/* align end on a 4-bytes boundary */
806			len = (RT2573_TX_DESC_SIZE + m->m_pkthdr.len + 3) & ~3;
807			if ((len % 64) == 0)
808				len += 4;
809
810			DPRINTFN(11, "sending frame len=%u xferlen=%u\n",
811			    m->m_pkthdr.len, len);
812
813			usbd_xfer_set_frame_len(xfer, 0, len);
814			usbd_xfer_set_priv(xfer, data);
815
816			usbd_transfer_submit(xfer);
817		}
818		rum_start(sc);
819		break;
820
821	default:			/* Error */
822		DPRINTFN(11, "transfer error, %s\n",
823		    usbd_errstr(error));
824
825		counter_u64_add(sc->sc_ic.ic_oerrors, 1);
826		data = usbd_xfer_get_priv(xfer);
827		if (data != NULL) {
828			rum_tx_free(data, error);
829			usbd_xfer_set_priv(xfer, NULL);
830		}
831
832		if (error != USB_ERR_CANCELLED) {
833			if (error == USB_ERR_TIMEOUT)
834				device_printf(sc->sc_dev, "device timeout\n");
835
836			/*
837			 * Try to clear stall first, also if other
838			 * errors occur, hence clearing stall
839			 * introduces a 50 ms delay:
840			 */
841			usbd_xfer_set_stall(xfer);
842			goto tr_setup;
843		}
844		break;
845	}
846}
847
848static void
849rum_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
850{
851	struct rum_softc *sc = usbd_xfer_softc(xfer);
852	struct ieee80211com *ic = &sc->sc_ic;
853	struct ieee80211_node *ni;
854	struct mbuf *m = NULL;
855	struct usb_page_cache *pc;
856	uint32_t flags;
857	uint8_t rssi = 0;
858	int len;
859
860	usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
861
862	switch (USB_GET_STATE(xfer)) {
863	case USB_ST_TRANSFERRED:
864
865		DPRINTFN(15, "rx done, actlen=%d\n", len);
866
867		if (len < (int)(RT2573_RX_DESC_SIZE + IEEE80211_MIN_LEN)) {
868			DPRINTF("%s: xfer too short %d\n",
869			    device_get_nameunit(sc->sc_dev), len);
870			counter_u64_add(ic->ic_ierrors, 1);
871			goto tr_setup;
872		}
873
874		len -= RT2573_RX_DESC_SIZE;
875		pc = usbd_xfer_get_frame(xfer, 0);
876		usbd_copy_out(pc, 0, &sc->sc_rx_desc, RT2573_RX_DESC_SIZE);
877
878		rssi = rum_get_rssi(sc, sc->sc_rx_desc.rssi);
879		flags = le32toh(sc->sc_rx_desc.flags);
880		if (flags & RT2573_RX_CRC_ERROR) {
881			/*
882		         * This should not happen since we did not
883		         * request to receive those frames when we
884		         * filled RUM_TXRX_CSR2:
885		         */
886			DPRINTFN(5, "PHY or CRC error\n");
887			counter_u64_add(ic->ic_ierrors, 1);
888			goto tr_setup;
889		}
890
891		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
892		if (m == NULL) {
893			DPRINTF("could not allocate mbuf\n");
894			counter_u64_add(ic->ic_ierrors, 1);
895			goto tr_setup;
896		}
897		usbd_copy_out(pc, RT2573_RX_DESC_SIZE,
898		    mtod(m, uint8_t *), len);
899
900		/* finalize mbuf */
901		m->m_pkthdr.len = m->m_len = (flags >> 16) & 0xfff;
902
903		if (ieee80211_radiotap_active(ic)) {
904			struct rum_rx_radiotap_header *tap = &sc->sc_rxtap;
905
906			/* XXX read tsf */
907			tap->wr_flags = 0;
908			tap->wr_rate = ieee80211_plcp2rate(sc->sc_rx_desc.rate,
909			    (flags & RT2573_RX_OFDM) ?
910			    IEEE80211_T_OFDM : IEEE80211_T_CCK);
911			tap->wr_antsignal = RT2573_NOISE_FLOOR + rssi;
912			tap->wr_antnoise = RT2573_NOISE_FLOOR;
913			tap->wr_antenna = sc->rx_ant;
914		}
915		/* FALLTHROUGH */
916	case USB_ST_SETUP:
917tr_setup:
918		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
919		usbd_transfer_submit(xfer);
920
921		/*
922		 * At the end of a USB callback it is always safe to unlock
923		 * the private mutex of a device! That is why we do the
924		 * "ieee80211_input" here, and not some lines up!
925		 */
926		RUM_UNLOCK(sc);
927		if (m) {
928			ni = ieee80211_find_rxnode(ic,
929			    mtod(m, struct ieee80211_frame_min *));
930			if (ni != NULL) {
931				(void) ieee80211_input(ni, m, rssi,
932				    RT2573_NOISE_FLOOR);
933				ieee80211_free_node(ni);
934			} else
935				(void) ieee80211_input_all(ic, m, rssi,
936				    RT2573_NOISE_FLOOR);
937		}
938		RUM_LOCK(sc);
939		rum_start(sc);
940		return;
941
942	default:			/* Error */
943		if (error != USB_ERR_CANCELLED) {
944			/* try to clear stall first */
945			usbd_xfer_set_stall(xfer);
946			goto tr_setup;
947		}
948		return;
949	}
950}
951
952static uint8_t
953rum_plcp_signal(int rate)
954{
955	switch (rate) {
956	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
957	case 12:	return 0xb;
958	case 18:	return 0xf;
959	case 24:	return 0xa;
960	case 36:	return 0xe;
961	case 48:	return 0x9;
962	case 72:	return 0xd;
963	case 96:	return 0x8;
964	case 108:	return 0xc;
965
966	/* CCK rates (NB: not IEEE std, device-specific) */
967	case 2:		return 0x0;
968	case 4:		return 0x1;
969	case 11:	return 0x2;
970	case 22:	return 0x3;
971	}
972	return 0xff;		/* XXX unsupported/unknown rate */
973}
974
975static void
976rum_setup_tx_desc(struct rum_softc *sc, struct rum_tx_desc *desc,
977    uint32_t flags, uint16_t xflags, int len, int rate)
978{
979	struct ieee80211com *ic = &sc->sc_ic;
980	uint16_t plcp_length;
981	int remainder;
982
983	desc->flags = htole32(flags);
984	desc->flags |= htole32(RT2573_TX_VALID);
985	desc->flags |= htole32(len << 16);
986
987	desc->xflags = htole16(xflags);
988
989	desc->wme = htole16(RT2573_QID(0) | RT2573_AIFSN(2) |
990	    RT2573_LOGCWMIN(4) | RT2573_LOGCWMAX(10));
991
992	/* setup PLCP fields */
993	desc->plcp_signal  = rum_plcp_signal(rate);
994	desc->plcp_service = 4;
995
996	len += IEEE80211_CRC_LEN;
997	if (ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) {
998		desc->flags |= htole32(RT2573_TX_OFDM);
999
1000		plcp_length = len & 0xfff;
1001		desc->plcp_length_hi = plcp_length >> 6;
1002		desc->plcp_length_lo = plcp_length & 0x3f;
1003	} else {
1004		if (rate == 0)
1005			rate = 2;	/* avoid division by zero */
1006		plcp_length = (16 * len + rate - 1) / rate;
1007		if (rate == 22) {
1008			remainder = (16 * len) % 22;
1009			if (remainder != 0 && remainder < 7)
1010				desc->plcp_service |= RT2573_PLCP_LENGEXT;
1011		}
1012		desc->plcp_length_hi = plcp_length >> 8;
1013		desc->plcp_length_lo = plcp_length & 0xff;
1014
1015		if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1016			desc->plcp_signal |= 0x08;
1017	}
1018}
1019
1020static int
1021rum_sendprot(struct rum_softc *sc,
1022    const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate)
1023{
1024	struct ieee80211com *ic = ni->ni_ic;
1025	const struct ieee80211_frame *wh;
1026	struct rum_tx_data *data;
1027	struct mbuf *mprot;
1028	int protrate, ackrate, pktlen, flags, isshort;
1029	uint16_t dur;
1030
1031	RUM_LOCK_ASSERT(sc, MA_OWNED);
1032	KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY,
1033	    ("protection %d", prot));
1034
1035	wh = mtod(m, const struct ieee80211_frame *);
1036	pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
1037
1038	protrate = ieee80211_ctl_rate(ic->ic_rt, rate);
1039	ackrate = ieee80211_ack_rate(ic->ic_rt, rate);
1040
1041	isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0;
1042	dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort)
1043	    + ieee80211_ack_duration(ic->ic_rt, rate, isshort);
1044	flags = RT2573_TX_MORE_FRAG;
1045	if (prot == IEEE80211_PROT_RTSCTS) {
1046		/* NB: CTS is the same size as an ACK */
1047		dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort);
1048		flags |= RT2573_TX_NEED_ACK;
1049		mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur);
1050	} else {
1051		mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur);
1052	}
1053	if (mprot == NULL) {
1054		/* XXX stat + msg */
1055		return (ENOBUFS);
1056	}
1057	data = STAILQ_FIRST(&sc->tx_free);
1058	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1059	sc->tx_nfree--;
1060
1061	data->m = mprot;
1062	data->ni = ieee80211_ref_node(ni);
1063	data->rate = protrate;
1064	rum_setup_tx_desc(sc, &data->desc, flags, 0, mprot->m_pkthdr.len, protrate);
1065
1066	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1067	usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1068
1069	return 0;
1070}
1071
1072static int
1073rum_tx_mgt(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1074{
1075	struct ieee80211vap *vap = ni->ni_vap;
1076	struct ieee80211com *ic = &sc->sc_ic;
1077	struct rum_tx_data *data;
1078	struct ieee80211_frame *wh;
1079	const struct ieee80211_txparam *tp;
1080	struct ieee80211_key *k;
1081	uint32_t flags = 0;
1082	uint16_t dur;
1083
1084	RUM_LOCK_ASSERT(sc, MA_OWNED);
1085
1086	data = STAILQ_FIRST(&sc->tx_free);
1087	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1088	sc->tx_nfree--;
1089
1090	wh = mtod(m0, struct ieee80211_frame *);
1091	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1092		k = ieee80211_crypto_encap(ni, m0);
1093		if (k == NULL) {
1094			m_freem(m0);
1095			return ENOBUFS;
1096		}
1097		wh = mtod(m0, struct ieee80211_frame *);
1098	}
1099
1100	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1101
1102	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1103		flags |= RT2573_TX_NEED_ACK;
1104
1105		dur = ieee80211_ack_duration(ic->ic_rt, tp->mgmtrate,
1106		    ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1107		USETW(wh->i_dur, dur);
1108
1109		/* tell hardware to add timestamp for probe responses */
1110		if ((wh->i_fc[0] &
1111		    (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
1112		    (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
1113			flags |= RT2573_TX_TIMESTAMP;
1114	}
1115
1116	data->m = m0;
1117	data->ni = ni;
1118	data->rate = tp->mgmtrate;
1119
1120	rum_setup_tx_desc(sc, &data->desc, flags, 0, m0->m_pkthdr.len, tp->mgmtrate);
1121
1122	DPRINTFN(10, "sending mgt frame len=%d rate=%d\n",
1123	    m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, tp->mgmtrate);
1124
1125	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1126	usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1127
1128	return (0);
1129}
1130
1131static int
1132rum_tx_raw(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
1133    const struct ieee80211_bpf_params *params)
1134{
1135	struct ieee80211com *ic = ni->ni_ic;
1136	struct rum_tx_data *data;
1137	uint32_t flags;
1138	int rate, error;
1139
1140	RUM_LOCK_ASSERT(sc, MA_OWNED);
1141	KASSERT(params != NULL, ("no raw xmit params"));
1142
1143	rate = params->ibp_rate0;
1144	if (!ieee80211_isratevalid(ic->ic_rt, rate)) {
1145		m_freem(m0);
1146		return EINVAL;
1147	}
1148	flags = 0;
1149	if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
1150		flags |= RT2573_TX_NEED_ACK;
1151	if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) {
1152		error = rum_sendprot(sc, m0, ni,
1153		    params->ibp_flags & IEEE80211_BPF_RTS ?
1154			 IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY,
1155		    rate);
1156		if (error || sc->tx_nfree == 0) {
1157			m_freem(m0);
1158			return ENOBUFS;
1159		}
1160		flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
1161	}
1162
1163	data = STAILQ_FIRST(&sc->tx_free);
1164	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1165	sc->tx_nfree--;
1166
1167	data->m = m0;
1168	data->ni = ni;
1169	data->rate = rate;
1170
1171	/* XXX need to setup descriptor ourself */
1172	rum_setup_tx_desc(sc, &data->desc, flags, 0, m0->m_pkthdr.len, rate);
1173
1174	DPRINTFN(10, "sending raw frame len=%u rate=%u\n",
1175	    m0->m_pkthdr.len, rate);
1176
1177	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1178	usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1179
1180	return 0;
1181}
1182
1183static int
1184rum_tx_data(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1185{
1186	struct ieee80211vap *vap = ni->ni_vap;
1187	struct ieee80211com *ic = &sc->sc_ic;
1188	struct rum_tx_data *data;
1189	struct ieee80211_frame *wh;
1190	const struct ieee80211_txparam *tp;
1191	struct ieee80211_key *k;
1192	uint32_t flags = 0;
1193	uint16_t dur;
1194	int error, rate;
1195
1196	RUM_LOCK_ASSERT(sc, MA_OWNED);
1197
1198	wh = mtod(m0, struct ieee80211_frame *);
1199
1200	tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
1201	if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1202		rate = tp->mcastrate;
1203	else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
1204		rate = tp->ucastrate;
1205	else
1206		rate = ni->ni_txrate;
1207
1208	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1209		k = ieee80211_crypto_encap(ni, m0);
1210		if (k == NULL) {
1211			m_freem(m0);
1212			return ENOBUFS;
1213		}
1214
1215		/* packet header may have moved, reset our local pointer */
1216		wh = mtod(m0, struct ieee80211_frame *);
1217	}
1218
1219	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1220		int prot = IEEE80211_PROT_NONE;
1221		if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold)
1222			prot = IEEE80211_PROT_RTSCTS;
1223		else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
1224		    ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM)
1225			prot = ic->ic_protmode;
1226		if (prot != IEEE80211_PROT_NONE) {
1227			error = rum_sendprot(sc, m0, ni, prot, rate);
1228			if (error || sc->tx_nfree == 0) {
1229				m_freem(m0);
1230				return ENOBUFS;
1231			}
1232			flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
1233		}
1234	}
1235
1236	data = STAILQ_FIRST(&sc->tx_free);
1237	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1238	sc->tx_nfree--;
1239
1240	data->m = m0;
1241	data->ni = ni;
1242	data->rate = rate;
1243
1244	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1245		flags |= RT2573_TX_NEED_ACK;
1246		flags |= RT2573_TX_MORE_FRAG;
1247
1248		dur = ieee80211_ack_duration(ic->ic_rt, rate,
1249		    ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1250		USETW(wh->i_dur, dur);
1251	}
1252
1253	rum_setup_tx_desc(sc, &data->desc, flags, 0, m0->m_pkthdr.len, rate);
1254
1255	DPRINTFN(10, "sending frame len=%d rate=%d\n",
1256	    m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, rate);
1257
1258	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1259	usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1260
1261	return 0;
1262}
1263
1264static int
1265rum_transmit(struct ieee80211com *ic, struct mbuf *m)
1266{
1267	struct rum_softc *sc = ic->ic_softc;
1268	int error;
1269
1270	RUM_LOCK(sc);
1271	if (!sc->sc_running) {
1272		RUM_UNLOCK(sc);
1273		return (ENXIO);
1274	}
1275	error = mbufq_enqueue(&sc->sc_snd, m);
1276	if (error) {
1277		RUM_UNLOCK(sc);
1278		return (error);
1279	}
1280	rum_start(sc);
1281	RUM_UNLOCK(sc);
1282
1283	return (0);
1284}
1285
1286static void
1287rum_start(struct rum_softc *sc)
1288{
1289	struct ieee80211_node *ni;
1290	struct mbuf *m;
1291
1292	RUM_LOCK_ASSERT(sc, MA_OWNED);
1293
1294	if (!sc->sc_running)
1295		return;
1296
1297	while (sc->tx_nfree >= RUM_TX_MINFREE &&
1298	    (m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
1299		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1300		if (rum_tx_data(sc, m, ni) != 0) {
1301			if_inc_counter(ni->ni_vap->iv_ifp,
1302			    IFCOUNTER_OERRORS, 1);
1303			ieee80211_free_node(ni);
1304			break;
1305		}
1306	}
1307}
1308
1309static void
1310rum_parent(struct ieee80211com *ic)
1311{
1312	struct rum_softc *sc = ic->ic_softc;
1313	int startall = 0;
1314
1315	RUM_LOCK(sc);
1316	if (sc->sc_detached) {
1317		RUM_UNLOCK(sc);
1318		return;
1319	}
1320	if (ic->ic_nrunning > 0) {
1321		if (!sc->sc_running) {
1322			rum_init(sc);
1323			startall = 1;
1324		} else
1325			rum_setpromisc(sc);
1326	} else if (sc->sc_running)
1327		rum_stop(sc);
1328	RUM_UNLOCK(sc);
1329	if (startall)
1330		ieee80211_start_all(ic);
1331}
1332
1333static void
1334rum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len)
1335{
1336	struct usb_device_request req;
1337	usb_error_t error;
1338
1339	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1340	req.bRequest = RT2573_READ_EEPROM;
1341	USETW(req.wValue, 0);
1342	USETW(req.wIndex, addr);
1343	USETW(req.wLength, len);
1344
1345	error = rum_do_request(sc, &req, buf);
1346	if (error != 0) {
1347		device_printf(sc->sc_dev, "could not read EEPROM: %s\n",
1348		    usbd_errstr(error));
1349	}
1350}
1351
1352static uint32_t
1353rum_read(struct rum_softc *sc, uint16_t reg)
1354{
1355	uint32_t val;
1356
1357	rum_read_multi(sc, reg, &val, sizeof val);
1358
1359	return le32toh(val);
1360}
1361
1362static void
1363rum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len)
1364{
1365	struct usb_device_request req;
1366	usb_error_t error;
1367
1368	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1369	req.bRequest = RT2573_READ_MULTI_MAC;
1370	USETW(req.wValue, 0);
1371	USETW(req.wIndex, reg);
1372	USETW(req.wLength, len);
1373
1374	error = rum_do_request(sc, &req, buf);
1375	if (error != 0) {
1376		device_printf(sc->sc_dev,
1377		    "could not multi read MAC register: %s\n",
1378		    usbd_errstr(error));
1379	}
1380}
1381
1382static usb_error_t
1383rum_write(struct rum_softc *sc, uint16_t reg, uint32_t val)
1384{
1385	uint32_t tmp = htole32(val);
1386
1387	return (rum_write_multi(sc, reg, &tmp, sizeof tmp));
1388}
1389
1390static usb_error_t
1391rum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len)
1392{
1393	struct usb_device_request req;
1394	usb_error_t error;
1395	size_t offset;
1396
1397	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1398	req.bRequest = RT2573_WRITE_MULTI_MAC;
1399	USETW(req.wValue, 0);
1400
1401	/* write at most 64 bytes at a time */
1402	for (offset = 0; offset < len; offset += 64) {
1403		USETW(req.wIndex, reg + offset);
1404		USETW(req.wLength, MIN(len - offset, 64));
1405
1406		error = rum_do_request(sc, &req, (char *)buf + offset);
1407		if (error != 0) {
1408			device_printf(sc->sc_dev,
1409			    "could not multi write MAC register: %s\n",
1410			    usbd_errstr(error));
1411			return (error);
1412		}
1413	}
1414
1415	return (USB_ERR_NORMAL_COMPLETION);
1416}
1417
1418static usb_error_t
1419rum_setbits(struct rum_softc *sc, uint16_t reg, uint32_t mask)
1420{
1421	return (rum_write(sc, reg, rum_read(sc, reg) | mask));
1422}
1423
1424static usb_error_t
1425rum_clrbits(struct rum_softc *sc, uint16_t reg, uint32_t mask)
1426{
1427	return (rum_write(sc, reg, rum_read(sc, reg) & ~mask));
1428}
1429
1430static usb_error_t
1431rum_modbits(struct rum_softc *sc, uint16_t reg, uint32_t set, uint32_t unset)
1432{
1433	return (rum_write(sc, reg, (rum_read(sc, reg) & ~unset) | set));
1434}
1435
1436static int
1437rum_bbp_busy(struct rum_softc *sc)
1438{
1439	int ntries;
1440
1441	for (ntries = 0; ntries < 100; ntries++) {
1442		if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1443			break;
1444		if (rum_pause(sc, hz / 100))
1445			break;
1446	}
1447	if (ntries == 100)
1448		return (ETIMEDOUT);
1449
1450	return (0);
1451}
1452
1453static void
1454rum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val)
1455{
1456	uint32_t tmp;
1457
1458	DPRINTFN(2, "reg=0x%08x\n", reg);
1459
1460	if (rum_bbp_busy(sc) != 0) {
1461		device_printf(sc->sc_dev, "could not write to BBP\n");
1462		return;
1463	}
1464
1465	tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val;
1466	rum_write(sc, RT2573_PHY_CSR3, tmp);
1467}
1468
1469static uint8_t
1470rum_bbp_read(struct rum_softc *sc, uint8_t reg)
1471{
1472	uint32_t val;
1473	int ntries;
1474
1475	DPRINTFN(2, "reg=0x%08x\n", reg);
1476
1477	if (rum_bbp_busy(sc) != 0) {
1478		device_printf(sc->sc_dev, "could not read BBP\n");
1479		return 0;
1480	}
1481
1482	val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8;
1483	rum_write(sc, RT2573_PHY_CSR3, val);
1484
1485	for (ntries = 0; ntries < 100; ntries++) {
1486		val = rum_read(sc, RT2573_PHY_CSR3);
1487		if (!(val & RT2573_BBP_BUSY))
1488			return val & 0xff;
1489		if (rum_pause(sc, hz / 100))
1490			break;
1491	}
1492
1493	device_printf(sc->sc_dev, "could not read BBP\n");
1494	return 0;
1495}
1496
1497static void
1498rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val)
1499{
1500	uint32_t tmp;
1501	int ntries;
1502
1503	for (ntries = 0; ntries < 100; ntries++) {
1504		if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY))
1505			break;
1506		if (rum_pause(sc, hz / 100))
1507			break;
1508	}
1509	if (ntries == 100) {
1510		device_printf(sc->sc_dev, "could not write to RF\n");
1511		return;
1512	}
1513
1514	tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 |
1515	    (reg & 3);
1516	rum_write(sc, RT2573_PHY_CSR4, tmp);
1517
1518	/* remember last written value in sc */
1519	sc->rf_regs[reg] = val;
1520
1521	DPRINTFN(15, "RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff);
1522}
1523
1524static void
1525rum_select_antenna(struct rum_softc *sc)
1526{
1527	uint8_t bbp4, bbp77;
1528	uint32_t tmp;
1529
1530	bbp4  = rum_bbp_read(sc, 4);
1531	bbp77 = rum_bbp_read(sc, 77);
1532
1533	/* TBD */
1534
1535	/* make sure Rx is disabled before switching antenna */
1536	tmp = rum_read(sc, RT2573_TXRX_CSR0);
1537	rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
1538
1539	rum_bbp_write(sc,  4, bbp4);
1540	rum_bbp_write(sc, 77, bbp77);
1541
1542	rum_write(sc, RT2573_TXRX_CSR0, tmp);
1543}
1544
1545/*
1546 * Enable multi-rate retries for frames sent at OFDM rates.
1547 * In 802.11b/g mode, allow fallback to CCK rates.
1548 */
1549static void
1550rum_enable_mrr(struct rum_softc *sc)
1551{
1552	struct ieee80211com *ic = &sc->sc_ic;
1553
1554	if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) {
1555		rum_setbits(sc, RT2573_TXRX_CSR4,
1556		    RT2573_MRR_ENABLED | RT2573_MRR_CCK_FALLBACK);
1557	} else {
1558		rum_modbits(sc, RT2573_TXRX_CSR4,
1559		    RT2573_MRR_ENABLED, RT2573_MRR_CCK_FALLBACK);
1560	}
1561}
1562
1563static void
1564rum_set_txpreamble(struct rum_softc *sc)
1565{
1566	struct ieee80211com *ic = &sc->sc_ic;
1567
1568	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
1569		rum_setbits(sc, RT2573_TXRX_CSR4, RT2573_SHORT_PREAMBLE);
1570	else
1571		rum_clrbits(sc, RT2573_TXRX_CSR4, RT2573_SHORT_PREAMBLE);
1572}
1573
1574static void
1575rum_set_basicrates(struct rum_softc *sc)
1576{
1577	struct ieee80211com *ic = &sc->sc_ic;
1578
1579	/* update basic rate set */
1580	if (ic->ic_curmode == IEEE80211_MODE_11B) {
1581		/* 11b basic rates: 1, 2Mbps */
1582		rum_write(sc, RT2573_TXRX_CSR5, 0x3);
1583	} else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) {
1584		/* 11a basic rates: 6, 12, 24Mbps */
1585		rum_write(sc, RT2573_TXRX_CSR5, 0x150);
1586	} else {
1587		/* 11b/g basic rates: 1, 2, 5.5, 11Mbps */
1588		rum_write(sc, RT2573_TXRX_CSR5, 0xf);
1589	}
1590}
1591
1592/*
1593 * Reprogram MAC/BBP to switch to a new band.  Values taken from the reference
1594 * driver.
1595 */
1596static void
1597rum_select_band(struct rum_softc *sc, struct ieee80211_channel *c)
1598{
1599	uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104;
1600
1601	/* update all BBP registers that depend on the band */
1602	bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c;
1603	bbp35 = 0x50; bbp97 = 0x48; bbp98  = 0x48;
1604	if (IEEE80211_IS_CHAN_5GHZ(c)) {
1605		bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c;
1606		bbp35 += 0x10; bbp97 += 0x10; bbp98  += 0x10;
1607	}
1608	if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1609	    (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1610		bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10;
1611	}
1612
1613	sc->bbp17 = bbp17;
1614	rum_bbp_write(sc,  17, bbp17);
1615	rum_bbp_write(sc,  96, bbp96);
1616	rum_bbp_write(sc, 104, bbp104);
1617
1618	if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1619	    (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1620		rum_bbp_write(sc, 75, 0x80);
1621		rum_bbp_write(sc, 86, 0x80);
1622		rum_bbp_write(sc, 88, 0x80);
1623	}
1624
1625	rum_bbp_write(sc, 35, bbp35);
1626	rum_bbp_write(sc, 97, bbp97);
1627	rum_bbp_write(sc, 98, bbp98);
1628
1629	if (IEEE80211_IS_CHAN_2GHZ(c)) {
1630		rum_modbits(sc, RT2573_PHY_CSR0, RT2573_PA_PE_2GHZ,
1631		    RT2573_PA_PE_5GHZ);
1632	} else {
1633		rum_modbits(sc, RT2573_PHY_CSR0, RT2573_PA_PE_5GHZ,
1634		    RT2573_PA_PE_2GHZ);
1635	}
1636}
1637
1638static void
1639rum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c)
1640{
1641	struct ieee80211com *ic = &sc->sc_ic;
1642	const struct rfprog *rfprog;
1643	uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT;
1644	int8_t power;
1645	int i, chan;
1646
1647	chan = ieee80211_chan2ieee(ic, c);
1648	if (chan == 0 || chan == IEEE80211_CHAN_ANY)
1649		return;
1650
1651	/* select the appropriate RF settings based on what EEPROM says */
1652	rfprog = (sc->rf_rev == RT2573_RF_5225 ||
1653		  sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226;
1654
1655	/* find the settings for this channel (we know it exists) */
1656	for (i = 0; rfprog[i].chan != chan; i++);
1657
1658	power = sc->txpow[i];
1659	if (power < 0) {
1660		bbp94 += power;
1661		power = 0;
1662	} else if (power > 31) {
1663		bbp94 += power - 31;
1664		power = 31;
1665	}
1666
1667	/*
1668	 * If we are switching from the 2GHz band to the 5GHz band or
1669	 * vice-versa, BBP registers need to be reprogrammed.
1670	 */
1671	if (c->ic_flags != ic->ic_curchan->ic_flags) {
1672		rum_select_band(sc, c);
1673		rum_select_antenna(sc);
1674	}
1675	ic->ic_curchan = c;
1676
1677	rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1678	rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1679	rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1680	rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1681
1682	rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1683	rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1684	rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7 | 1);
1685	rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1686
1687	rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1688	rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1689	rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1690	rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1691
1692	rum_pause(sc, hz / 100);
1693
1694	/* enable smart mode for MIMO-capable RFs */
1695	bbp3 = rum_bbp_read(sc, 3);
1696
1697	bbp3 &= ~RT2573_SMART_MODE;
1698	if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527)
1699		bbp3 |= RT2573_SMART_MODE;
1700
1701	rum_bbp_write(sc, 3, bbp3);
1702
1703	if (bbp94 != RT2573_BBPR94_DEFAULT)
1704		rum_bbp_write(sc, 94, bbp94);
1705
1706	/* give the chip some extra time to do the switchover */
1707	rum_pause(sc, hz / 100);
1708}
1709
1710/*
1711 * Enable TSF synchronization and tell h/w to start sending beacons for IBSS
1712 * and HostAP operating modes.
1713 */
1714static void
1715rum_enable_tsf_sync(struct rum_softc *sc)
1716{
1717	struct ieee80211com *ic = &sc->sc_ic;
1718	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1719	uint32_t tmp;
1720
1721	if (vap->iv_opmode != IEEE80211_M_STA) {
1722		/*
1723		 * Change default 16ms TBTT adjustment to 8ms.
1724		 * Must be done before enabling beacon generation.
1725		 */
1726		rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8);
1727	}
1728
1729	tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000;
1730
1731	/* set beacon interval (in 1/16ms unit) */
1732	tmp |= vap->iv_bss->ni_intval * 16;
1733	tmp |= RT2573_TSF_TIMER_EN | RT2573_TBTT_TIMER_EN;
1734
1735	switch (vap->iv_opmode) {
1736	case IEEE80211_M_STA:
1737		/*
1738		 * Local TSF is always updated with remote TSF on beacon
1739		 * reception.
1740		 */
1741		tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_STA);
1742		break;
1743	case IEEE80211_M_IBSS:
1744		/*
1745		 * Local TSF is updated with remote TSF on beacon reception
1746		 * only if the remote TSF is greater than local TSF.
1747		 */
1748		tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_IBSS);
1749		tmp |= RT2573_BCN_TX_EN;
1750		break;
1751	case IEEE80211_M_HOSTAP:
1752		/* SYNC with nobody */
1753		tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_HOSTAP);
1754		tmp |= RT2573_BCN_TX_EN;
1755		break;
1756	default:
1757		device_printf(sc->sc_dev,
1758		    "Enabling TSF failed. undefined opmode %d\n",
1759		    vap->iv_opmode);
1760		return;
1761	}
1762
1763	rum_write(sc, RT2573_TXRX_CSR9, tmp);
1764}
1765
1766static void
1767rum_enable_tsf(struct rum_softc *sc)
1768{
1769	rum_modbits(sc, RT2573_TXRX_CSR9, RT2573_TSF_TIMER_EN |
1770	    RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_DIS), 0x00ffffff);
1771}
1772
1773static void
1774rum_abort_tsf_sync(struct rum_softc *sc)
1775{
1776	rum_clrbits(sc, RT2573_TXRX_CSR9, 0x00ffffff);
1777}
1778
1779static void
1780rum_update_slot(struct rum_softc *sc)
1781{
1782	struct ieee80211com *ic = &sc->sc_ic;
1783	uint8_t slottime;
1784
1785	slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
1786
1787	rum_modbits(sc, RT2573_MAC_CSR9, slottime, 0xff);
1788
1789	DPRINTF("setting slot time to %uus\n", slottime);
1790}
1791
1792static void
1793rum_set_bssid(struct rum_softc *sc, const uint8_t *bssid)
1794{
1795	uint32_t tmp;
1796
1797	tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24;
1798	rum_write(sc, RT2573_MAC_CSR4, tmp);
1799
1800	tmp = bssid[4] | bssid[5] << 8 | RT2573_ONE_BSSID << 16;
1801	rum_write(sc, RT2573_MAC_CSR5, tmp);
1802}
1803
1804static void
1805rum_set_macaddr(struct rum_softc *sc, const uint8_t *addr)
1806{
1807	uint32_t tmp;
1808
1809	tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24;
1810	rum_write(sc, RT2573_MAC_CSR2, tmp);
1811
1812	tmp = addr[4] | addr[5] << 8 | 0xff << 16;
1813	rum_write(sc, RT2573_MAC_CSR3, tmp);
1814}
1815
1816static void
1817rum_setpromisc(struct rum_softc *sc)
1818{
1819	struct ieee80211com *ic = &sc->sc_ic;
1820
1821	if (ic->ic_promisc == 0)
1822		rum_setbits(sc, RT2573_TXRX_CSR0, RT2573_DROP_NOT_TO_ME);
1823	else
1824		rum_clrbits(sc, RT2573_TXRX_CSR0, RT2573_DROP_NOT_TO_ME);
1825
1826	DPRINTF("%s promiscuous mode\n", ic->ic_promisc > 0 ?
1827	    "entering" : "leaving");
1828}
1829
1830static void
1831rum_update_promisc(struct ieee80211com *ic)
1832{
1833	struct rum_softc *sc = ic->ic_softc;
1834
1835	RUM_LOCK(sc);
1836	if (!sc->sc_running) {
1837		RUM_UNLOCK(sc);
1838		return;
1839	}
1840	rum_setpromisc(sc);
1841	RUM_UNLOCK(sc);
1842}
1843
1844static void
1845rum_update_mcast(struct ieee80211com *ic)
1846{
1847	static int warning_printed;
1848
1849	if (warning_printed == 0) {
1850		ic_printf(ic, "need to implement %s\n", __func__);
1851		warning_printed = 1;
1852	}
1853}
1854
1855static const char *
1856rum_get_rf(int rev)
1857{
1858	switch (rev) {
1859	case RT2573_RF_2527:	return "RT2527 (MIMO XR)";
1860	case RT2573_RF_2528:	return "RT2528";
1861	case RT2573_RF_5225:	return "RT5225 (MIMO XR)";
1862	case RT2573_RF_5226:	return "RT5226";
1863	default:		return "unknown";
1864	}
1865}
1866
1867static void
1868rum_read_eeprom(struct rum_softc *sc)
1869{
1870	uint16_t val;
1871#ifdef RUM_DEBUG
1872	int i;
1873#endif
1874
1875	/* read MAC address */
1876	rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, sc->sc_ic.ic_macaddr, 6);
1877
1878	rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2);
1879	val = le16toh(val);
1880	sc->rf_rev =   (val >> 11) & 0x1f;
1881	sc->hw_radio = (val >> 10) & 0x1;
1882	sc->rx_ant =   (val >> 4)  & 0x3;
1883	sc->tx_ant =   (val >> 2)  & 0x3;
1884	sc->nb_ant =   val & 0x3;
1885
1886	DPRINTF("RF revision=%d\n", sc->rf_rev);
1887
1888	rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2);
1889	val = le16toh(val);
1890	sc->ext_5ghz_lna = (val >> 6) & 0x1;
1891	sc->ext_2ghz_lna = (val >> 4) & 0x1;
1892
1893	DPRINTF("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n",
1894	    sc->ext_2ghz_lna, sc->ext_5ghz_lna);
1895
1896	rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2);
1897	val = le16toh(val);
1898	if ((val & 0xff) != 0xff)
1899		sc->rssi_2ghz_corr = (int8_t)(val & 0xff);	/* signed */
1900
1901	/* Only [-10, 10] is valid */
1902	if (sc->rssi_2ghz_corr < -10 || sc->rssi_2ghz_corr > 10)
1903		sc->rssi_2ghz_corr = 0;
1904
1905	rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2);
1906	val = le16toh(val);
1907	if ((val & 0xff) != 0xff)
1908		sc->rssi_5ghz_corr = (int8_t)(val & 0xff);	/* signed */
1909
1910	/* Only [-10, 10] is valid */
1911	if (sc->rssi_5ghz_corr < -10 || sc->rssi_5ghz_corr > 10)
1912		sc->rssi_5ghz_corr = 0;
1913
1914	if (sc->ext_2ghz_lna)
1915		sc->rssi_2ghz_corr -= 14;
1916	if (sc->ext_5ghz_lna)
1917		sc->rssi_5ghz_corr -= 14;
1918
1919	DPRINTF("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n",
1920	    sc->rssi_2ghz_corr, sc->rssi_5ghz_corr);
1921
1922	rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2);
1923	val = le16toh(val);
1924	if ((val & 0xff) != 0xff)
1925		sc->rffreq = val & 0xff;
1926
1927	DPRINTF("RF freq=%d\n", sc->rffreq);
1928
1929	/* read Tx power for all a/b/g channels */
1930	rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14);
1931	/* XXX default Tx power for 802.11a channels */
1932	memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14);
1933#ifdef RUM_DEBUG
1934	for (i = 0; i < 14; i++)
1935		DPRINTF("Channel=%d Tx power=%d\n", i + 1,  sc->txpow[i]);
1936#endif
1937
1938	/* read default values for BBP registers */
1939	rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
1940#ifdef RUM_DEBUG
1941	for (i = 0; i < 14; i++) {
1942		if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
1943			continue;
1944		DPRINTF("BBP R%d=%02x\n", sc->bbp_prom[i].reg,
1945		    sc->bbp_prom[i].val);
1946	}
1947#endif
1948}
1949
1950static int
1951rum_bbp_init(struct rum_softc *sc)
1952{
1953	int i, ntries;
1954
1955	/* wait for BBP to be ready */
1956	for (ntries = 0; ntries < 100; ntries++) {
1957		const uint8_t val = rum_bbp_read(sc, 0);
1958		if (val != 0 && val != 0xff)
1959			break;
1960		if (rum_pause(sc, hz / 100))
1961			break;
1962	}
1963	if (ntries == 100) {
1964		device_printf(sc->sc_dev, "timeout waiting for BBP\n");
1965		return EIO;
1966	}
1967
1968	/* initialize BBP registers to default values */
1969	for (i = 0; i < N(rum_def_bbp); i++)
1970		rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val);
1971
1972	/* write vendor-specific BBP values (from EEPROM) */
1973	for (i = 0; i < 16; i++) {
1974		if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
1975			continue;
1976		rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
1977	}
1978
1979	return 0;
1980}
1981
1982static void
1983rum_init(struct rum_softc *sc)
1984{
1985	struct ieee80211com *ic = &sc->sc_ic;
1986	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1987	uint32_t tmp;
1988	usb_error_t error;
1989	int i, ntries;
1990
1991	RUM_LOCK_ASSERT(sc, MA_OWNED);
1992
1993	rum_stop(sc);
1994
1995	/* initialize MAC registers to default values */
1996	for (i = 0; i < N(rum_def_mac); i++)
1997		rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val);
1998
1999	/* set host ready */
2000	rum_write(sc, RT2573_MAC_CSR1, 3);
2001	rum_write(sc, RT2573_MAC_CSR1, 0);
2002
2003	/* wait for BBP/RF to wakeup */
2004	for (ntries = 0; ntries < 100; ntries++) {
2005		if (rum_read(sc, RT2573_MAC_CSR12) & 8)
2006			break;
2007		rum_write(sc, RT2573_MAC_CSR12, 4);	/* force wakeup */
2008		if (rum_pause(sc, hz / 100))
2009			break;
2010	}
2011	if (ntries == 100) {
2012		device_printf(sc->sc_dev,
2013		    "timeout waiting for BBP/RF to wakeup\n");
2014		goto fail;
2015	}
2016
2017	if ((error = rum_bbp_init(sc)) != 0)
2018		goto fail;
2019
2020	/* select default channel */
2021	rum_select_band(sc, ic->ic_curchan);
2022	rum_select_antenna(sc);
2023	rum_set_chan(sc, ic->ic_curchan);
2024
2025	/* clear STA registers */
2026	rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
2027
2028	rum_set_macaddr(sc, vap ? vap->iv_myaddr : ic->ic_macaddr);
2029
2030	/* initialize ASIC */
2031	rum_write(sc, RT2573_MAC_CSR1, 4);
2032
2033	/*
2034	 * Allocate Tx and Rx xfer queues.
2035	 */
2036	rum_setup_tx_list(sc);
2037
2038	/* update Rx filter */
2039	tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff;
2040
2041	tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR;
2042	if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2043		tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR |
2044		       RT2573_DROP_ACKCTS;
2045		if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2046			tmp |= RT2573_DROP_TODS;
2047		if (ic->ic_promisc == 0)
2048			tmp |= RT2573_DROP_NOT_TO_ME;
2049	}
2050	rum_write(sc, RT2573_TXRX_CSR0, tmp);
2051
2052	sc->sc_running = 1;
2053	usbd_xfer_set_stall(sc->sc_xfer[RUM_BULK_WR]);
2054	usbd_transfer_start(sc->sc_xfer[RUM_BULK_RD]);
2055	return;
2056
2057fail:	rum_stop(sc);
2058#undef N
2059}
2060
2061static void
2062rum_stop(struct rum_softc *sc)
2063{
2064
2065	RUM_LOCK_ASSERT(sc, MA_OWNED);
2066
2067	sc->sc_running = 0;
2068
2069	RUM_UNLOCK(sc);
2070
2071	/*
2072	 * Drain the USB transfers, if not already drained:
2073	 */
2074	usbd_transfer_drain(sc->sc_xfer[RUM_BULK_WR]);
2075	usbd_transfer_drain(sc->sc_xfer[RUM_BULK_RD]);
2076
2077	RUM_LOCK(sc);
2078
2079	rum_unsetup_tx_list(sc);
2080
2081	/* disable Rx */
2082	rum_setbits(sc, RT2573_TXRX_CSR0, RT2573_DISABLE_RX);
2083
2084	/* reset ASIC */
2085	rum_write(sc, RT2573_MAC_CSR1, 3);
2086	rum_write(sc, RT2573_MAC_CSR1, 0);
2087}
2088
2089static void
2090rum_load_microcode(struct rum_softc *sc, const uint8_t *ucode, size_t size)
2091{
2092	struct usb_device_request req;
2093	uint16_t reg = RT2573_MCU_CODE_BASE;
2094	usb_error_t err;
2095
2096	/* copy firmware image into NIC */
2097	for (; size >= 4; reg += 4, ucode += 4, size -= 4) {
2098		err = rum_write(sc, reg, UGETDW(ucode));
2099		if (err) {
2100			/* firmware already loaded ? */
2101			device_printf(sc->sc_dev, "Firmware load "
2102			    "failure! (ignored)\n");
2103			break;
2104		}
2105	}
2106
2107	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2108	req.bRequest = RT2573_MCU_CNTL;
2109	USETW(req.wValue, RT2573_MCU_RUN);
2110	USETW(req.wIndex, 0);
2111	USETW(req.wLength, 0);
2112
2113	err = rum_do_request(sc, &req, NULL);
2114	if (err != 0) {
2115		device_printf(sc->sc_dev, "could not run firmware: %s\n",
2116		    usbd_errstr(err));
2117	}
2118
2119	/* give the chip some time to boot */
2120	rum_pause(sc, hz / 8);
2121}
2122
2123static void
2124rum_prepare_beacon(struct rum_softc *sc, struct ieee80211vap *vap)
2125{
2126	struct ieee80211com *ic = vap->iv_ic;
2127	const struct ieee80211_txparam *tp;
2128	struct rum_tx_desc desc;
2129	struct mbuf *m0;
2130
2131	if (vap->iv_bss->ni_chan == IEEE80211_CHAN_ANYC)
2132		return;
2133	if (ic->ic_bsschan == IEEE80211_CHAN_ANYC)
2134		return;
2135
2136	m0 = ieee80211_beacon_alloc(vap->iv_bss, &RUM_VAP(vap)->bo);
2137	if (m0 == NULL)
2138		return;
2139
2140	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)];
2141	rum_setup_tx_desc(sc, &desc, RT2573_TX_TIMESTAMP, RT2573_TX_HWSEQ,
2142	    m0->m_pkthdr.len, tp->mgmtrate);
2143
2144	/* copy the first 24 bytes of Tx descriptor into NIC memory */
2145	rum_write_multi(sc, RT2573_HW_BEACON_BASE0, (uint8_t *)&desc, 24);
2146
2147	/* copy beacon header and payload into NIC memory */
2148	rum_write_multi(sc, RT2573_HW_BEACON_BASE0 + 24, mtod(m0, uint8_t *),
2149	    m0->m_pkthdr.len);
2150
2151	m_freem(m0);
2152}
2153
2154static int
2155rum_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2156    const struct ieee80211_bpf_params *params)
2157{
2158	struct rum_softc *sc = ni->ni_ic->ic_softc;
2159
2160	RUM_LOCK(sc);
2161	/* prevent management frames from being sent if we're not ready */
2162	if (!sc->sc_running) {
2163		RUM_UNLOCK(sc);
2164		m_freem(m);
2165		ieee80211_free_node(ni);
2166		return ENETDOWN;
2167	}
2168	if (sc->tx_nfree < RUM_TX_MINFREE) {
2169		RUM_UNLOCK(sc);
2170		m_freem(m);
2171		ieee80211_free_node(ni);
2172		return EIO;
2173	}
2174
2175	if (params == NULL) {
2176		/*
2177		 * Legacy path; interpret frame contents to decide
2178		 * precisely how to send the frame.
2179		 */
2180		if (rum_tx_mgt(sc, m, ni) != 0)
2181			goto bad;
2182	} else {
2183		/*
2184		 * Caller supplied explicit parameters to use in
2185		 * sending the frame.
2186		 */
2187		if (rum_tx_raw(sc, m, ni, params) != 0)
2188			goto bad;
2189	}
2190	RUM_UNLOCK(sc);
2191
2192	return 0;
2193bad:
2194	RUM_UNLOCK(sc);
2195	ieee80211_free_node(ni);
2196	return EIO;
2197}
2198
2199static void
2200rum_ratectl_start(struct rum_softc *sc, struct ieee80211_node *ni)
2201{
2202	struct ieee80211vap *vap = ni->ni_vap;
2203	struct rum_vap *rvp = RUM_VAP(vap);
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	usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp);
2209}
2210
2211static void
2212rum_ratectl_timeout(void *arg)
2213{
2214	struct rum_vap *rvp = arg;
2215	struct ieee80211vap *vap = &rvp->vap;
2216	struct ieee80211com *ic = vap->iv_ic;
2217
2218	ieee80211_runtask(ic, &rvp->ratectl_task);
2219}
2220
2221static void
2222rum_ratectl_task(void *arg, int pending)
2223{
2224	struct rum_vap *rvp = arg;
2225	struct ieee80211vap *vap = &rvp->vap;
2226	struct ieee80211com *ic = vap->iv_ic;
2227	struct rum_softc *sc = ic->ic_softc;
2228	struct ieee80211_node *ni;
2229	int ok, fail;
2230	int sum, retrycnt;
2231
2232	RUM_LOCK(sc);
2233	/* read and clear statistic registers (STA_CSR0 to STA_CSR10) */
2234	rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof(sc->sta));
2235
2236	ok = (le32toh(sc->sta[4]) >> 16) +	/* TX ok w/o retry */
2237	    (le32toh(sc->sta[5]) & 0xffff);	/* TX ok w/ retry */
2238	fail = (le32toh(sc->sta[5]) >> 16);	/* TX retry-fail count */
2239	sum = ok+fail;
2240	retrycnt = (le32toh(sc->sta[5]) & 0xffff) + fail;
2241
2242	ni = ieee80211_ref_node(vap->iv_bss);
2243	ieee80211_ratectl_tx_update(vap, ni, &sum, &ok, &retrycnt);
2244	(void) ieee80211_ratectl_rate(ni, NULL, 0);
2245	ieee80211_free_node(ni);
2246
2247	/* count TX retry-fail as Tx errors */
2248	if_inc_counter(ni->ni_vap->iv_ifp, IFCOUNTER_OERRORS, fail);
2249
2250	usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp);
2251	RUM_UNLOCK(sc);
2252}
2253
2254static void
2255rum_scan_start(struct ieee80211com *ic)
2256{
2257	struct rum_softc *sc = ic->ic_softc;
2258
2259	RUM_LOCK(sc);
2260	rum_abort_tsf_sync(sc);
2261	rum_set_bssid(sc, ieee80211broadcastaddr);
2262	RUM_UNLOCK(sc);
2263
2264}
2265
2266static void
2267rum_scan_end(struct ieee80211com *ic)
2268{
2269	struct rum_softc *sc = ic->ic_softc;
2270
2271	RUM_LOCK(sc);
2272	rum_enable_tsf_sync(sc);
2273	rum_set_bssid(sc, ic->ic_macaddr);
2274	RUM_UNLOCK(sc);
2275
2276}
2277
2278static void
2279rum_set_channel(struct ieee80211com *ic)
2280{
2281	struct rum_softc *sc = ic->ic_softc;
2282
2283	RUM_LOCK(sc);
2284	rum_set_chan(sc, ic->ic_curchan);
2285	RUM_UNLOCK(sc);
2286}
2287
2288static int
2289rum_get_rssi(struct rum_softc *sc, uint8_t raw)
2290{
2291	struct ieee80211com *ic = &sc->sc_ic;
2292	int lna, agc, rssi;
2293
2294	lna = (raw >> 5) & 0x3;
2295	agc = raw & 0x1f;
2296
2297	if (lna == 0) {
2298		/*
2299		 * No RSSI mapping
2300		 *
2301		 * NB: Since RSSI is relative to noise floor, -1 is
2302		 *     adequate for caller to know error happened.
2303		 */
2304		return -1;
2305	}
2306
2307	rssi = (2 * agc) - RT2573_NOISE_FLOOR;
2308
2309	if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) {
2310		rssi += sc->rssi_2ghz_corr;
2311
2312		if (lna == 1)
2313			rssi -= 64;
2314		else if (lna == 2)
2315			rssi -= 74;
2316		else if (lna == 3)
2317			rssi -= 90;
2318	} else {
2319		rssi += sc->rssi_5ghz_corr;
2320
2321		if (!sc->ext_5ghz_lna && lna != 1)
2322			rssi += 4;
2323
2324		if (lna == 1)
2325			rssi -= 64;
2326		else if (lna == 2)
2327			rssi -= 86;
2328		else if (lna == 3)
2329			rssi -= 100;
2330	}
2331	return rssi;
2332}
2333
2334static int
2335rum_pause(struct rum_softc *sc, int timeout)
2336{
2337
2338	usb_pause_mtx(&sc->sc_mtx, timeout);
2339	return (0);
2340}
2341
2342static device_method_t rum_methods[] = {
2343	/* Device interface */
2344	DEVMETHOD(device_probe,		rum_match),
2345	DEVMETHOD(device_attach,	rum_attach),
2346	DEVMETHOD(device_detach,	rum_detach),
2347	DEVMETHOD_END
2348};
2349
2350static driver_t rum_driver = {
2351	.name = "rum",
2352	.methods = rum_methods,
2353	.size = sizeof(struct rum_softc),
2354};
2355
2356static devclass_t rum_devclass;
2357
2358DRIVER_MODULE(rum, uhub, rum_driver, rum_devclass, NULL, 0);
2359MODULE_DEPEND(rum, wlan, 1, 1, 1);
2360MODULE_DEPEND(rum, usb, 1, 1, 1);
2361MODULE_VERSION(rum, 1);
2362