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