if_rum.c revision 288639
1/*	$FreeBSD: head/sys/dev/usb/wlan/if_rum.c 288639 2015-10-03 22:26:59Z 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 288639 2015-10-03 22:26:59Z 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_cmdq_cb(void *, int);
162static int		rum_cmd_sleepable(struct rum_softc *, const void *,
163			    size_t, uint8_t, CMD_FUNC_PROTO);
164static void		rum_tx_free(struct rum_tx_data *, int);
165static void		rum_setup_tx_list(struct rum_softc *);
166static void		rum_unsetup_tx_list(struct rum_softc *);
167static int		rum_newstate(struct ieee80211vap *,
168			    enum ieee80211_state, int);
169static uint8_t		rum_crypto_mode(struct rum_softc *, u_int, int);
170static void		rum_setup_tx_desc(struct rum_softc *,
171			    struct rum_tx_desc *, struct ieee80211_key *,
172			    uint32_t, uint8_t, int, int, int);
173static uint32_t		rum_tx_crypto_flags(struct rum_softc *,
174			    struct ieee80211_node *,
175			    const struct ieee80211_key *);
176static int		rum_tx_mgt(struct rum_softc *, struct mbuf *,
177			    struct ieee80211_node *);
178static int		rum_tx_raw(struct rum_softc *, struct mbuf *,
179			    struct ieee80211_node *,
180			    const struct ieee80211_bpf_params *);
181static int		rum_tx_data(struct rum_softc *, struct mbuf *,
182			    struct ieee80211_node *);
183static int		rum_transmit(struct ieee80211com *, struct mbuf *);
184static void		rum_start(struct rum_softc *);
185static void		rum_parent(struct ieee80211com *);
186static void		rum_eeprom_read(struct rum_softc *, uint16_t, void *,
187			    int);
188static uint32_t		rum_read(struct rum_softc *, uint16_t);
189static void		rum_read_multi(struct rum_softc *, uint16_t, void *,
190			    int);
191static usb_error_t	rum_write(struct rum_softc *, uint16_t, uint32_t);
192static usb_error_t	rum_write_multi(struct rum_softc *, uint16_t, void *,
193			    size_t);
194static usb_error_t	rum_setbits(struct rum_softc *, uint16_t, uint32_t);
195static usb_error_t	rum_clrbits(struct rum_softc *, uint16_t, uint32_t);
196static usb_error_t	rum_modbits(struct rum_softc *, uint16_t, uint32_t,
197			    uint32_t);
198static int		rum_bbp_busy(struct rum_softc *);
199static void		rum_bbp_write(struct rum_softc *, uint8_t, uint8_t);
200static uint8_t		rum_bbp_read(struct rum_softc *, uint8_t);
201static void		rum_rf_write(struct rum_softc *, uint8_t, uint32_t);
202static void		rum_select_antenna(struct rum_softc *);
203static void		rum_enable_mrr(struct rum_softc *);
204static void		rum_set_txpreamble(struct rum_softc *);
205static void		rum_set_basicrates(struct rum_softc *);
206static void		rum_select_band(struct rum_softc *,
207			    struct ieee80211_channel *);
208static void		rum_set_chan(struct rum_softc *,
209			    struct ieee80211_channel *);
210static void		rum_set_maxretry(struct rum_softc *,
211			    struct ieee80211vap *);
212static int		rum_enable_tsf_sync(struct rum_softc *);
213static void		rum_enable_tsf(struct rum_softc *);
214static void		rum_abort_tsf_sync(struct rum_softc *);
215static void		rum_get_tsf(struct rum_softc *, uint64_t *);
216static void		rum_update_slot_cb(struct rum_softc *,
217			    union sec_param *, uint8_t);
218static void		rum_update_slot(struct ieee80211com *);
219static void		rum_set_bssid(struct rum_softc *, const uint8_t *);
220static void		rum_set_macaddr(struct rum_softc *, const uint8_t *);
221static void		rum_update_mcast(struct ieee80211com *);
222static void		rum_update_promisc(struct ieee80211com *);
223static void		rum_setpromisc(struct rum_softc *);
224static const char	*rum_get_rf(int);
225static void		rum_read_eeprom(struct rum_softc *);
226static int		rum_bbp_wakeup(struct rum_softc *);
227static int		rum_bbp_init(struct rum_softc *);
228static void		rum_clr_shkey_regs(struct rum_softc *);
229static int		rum_init(struct rum_softc *);
230static void		rum_stop(struct rum_softc *);
231static void		rum_load_microcode(struct rum_softc *, const uint8_t *,
232			    size_t);
233static int		rum_set_beacon(struct rum_softc *,
234			    struct ieee80211vap *);
235static int		rum_alloc_beacon(struct rum_softc *,
236			    struct ieee80211vap *);
237static void		rum_update_beacon_cb(struct rum_softc *,
238			    union sec_param *, uint8_t);
239static void		rum_update_beacon(struct ieee80211vap *, int);
240static int		rum_common_key_set(struct rum_softc *,
241			    struct ieee80211_key *, uint16_t);
242static void		rum_group_key_set_cb(struct rum_softc *,
243			    union sec_param *, uint8_t);
244static void		rum_group_key_del_cb(struct rum_softc *,
245			    union sec_param *, uint8_t);
246static void		rum_pair_key_set_cb(struct rum_softc *,
247			    union sec_param *, uint8_t);
248static void		rum_pair_key_del_cb(struct rum_softc *,
249			    union sec_param *, uint8_t);
250static int		rum_key_alloc(struct ieee80211vap *,
251			    struct ieee80211_key *, ieee80211_keyix *,
252			    ieee80211_keyix *);
253static int		rum_key_set(struct ieee80211vap *,
254			    const struct ieee80211_key *);
255static int		rum_key_delete(struct ieee80211vap *,
256			    const struct ieee80211_key *);
257static int		rum_raw_xmit(struct ieee80211_node *, struct mbuf *,
258			    const struct ieee80211_bpf_params *);
259static void		rum_scan_start(struct ieee80211com *);
260static void		rum_scan_end(struct ieee80211com *);
261static void		rum_set_channel(struct ieee80211com *);
262static int		rum_get_rssi(struct rum_softc *, uint8_t);
263static void		rum_ratectl_start(struct rum_softc *,
264			    struct ieee80211_node *);
265static void		rum_ratectl_timeout(void *);
266static void		rum_ratectl_task(void *, int);
267static int		rum_pause(struct rum_softc *, int);
268
269static const struct {
270	uint32_t	reg;
271	uint32_t	val;
272} rum_def_mac[] = {
273	{ RT2573_TXRX_CSR0,  0x025fb032 },
274	{ RT2573_TXRX_CSR1,  0x9eaa9eaf },
275	{ RT2573_TXRX_CSR2,  0x8a8b8c8d },
276	{ RT2573_TXRX_CSR3,  0x00858687 },
277	{ RT2573_TXRX_CSR7,  0x2e31353b },
278	{ RT2573_TXRX_CSR8,  0x2a2a2a2c },
279	{ RT2573_TXRX_CSR15, 0x0000000f },
280	{ RT2573_MAC_CSR6,   0x00000fff },
281	{ RT2573_MAC_CSR8,   0x016c030a },
282	{ RT2573_MAC_CSR10,  0x00000718 },
283	{ RT2573_MAC_CSR12,  0x00000004 },
284	{ RT2573_MAC_CSR13,  0x00007f00 },
285	{ RT2573_SEC_CSR2,   0x00000000 },
286	{ RT2573_SEC_CSR3,   0x00000000 },
287	{ RT2573_SEC_CSR4,   0x00000000 },
288	{ RT2573_PHY_CSR1,   0x000023b0 },
289	{ RT2573_PHY_CSR5,   0x00040a06 },
290	{ RT2573_PHY_CSR6,   0x00080606 },
291	{ RT2573_PHY_CSR7,   0x00000408 },
292	{ RT2573_AIFSN_CSR,  0x00002273 },
293	{ RT2573_CWMIN_CSR,  0x00002344 },
294	{ RT2573_CWMAX_CSR,  0x000034aa }
295};
296
297static const struct {
298	uint8_t	reg;
299	uint8_t	val;
300} rum_def_bbp[] = {
301	{   3, 0x80 },
302	{  15, 0x30 },
303	{  17, 0x20 },
304	{  21, 0xc8 },
305	{  22, 0x38 },
306	{  23, 0x06 },
307	{  24, 0xfe },
308	{  25, 0x0a },
309	{  26, 0x0d },
310	{  32, 0x0b },
311	{  34, 0x12 },
312	{  37, 0x07 },
313	{  39, 0xf8 },
314	{  41, 0x60 },
315	{  53, 0x10 },
316	{  54, 0x18 },
317	{  60, 0x10 },
318	{  61, 0x04 },
319	{  62, 0x04 },
320	{  75, 0xfe },
321	{  86, 0xfe },
322	{  88, 0xfe },
323	{  90, 0x0f },
324	{  99, 0x00 },
325	{ 102, 0x16 },
326	{ 107, 0x04 }
327};
328
329static const struct rfprog {
330	uint8_t		chan;
331	uint32_t	r1, r2, r3, r4;
332}  rum_rf5226[] = {
333	{   1, 0x00b03, 0x001e1, 0x1a014, 0x30282 },
334	{   2, 0x00b03, 0x001e1, 0x1a014, 0x30287 },
335	{   3, 0x00b03, 0x001e2, 0x1a014, 0x30282 },
336	{   4, 0x00b03, 0x001e2, 0x1a014, 0x30287 },
337	{   5, 0x00b03, 0x001e3, 0x1a014, 0x30282 },
338	{   6, 0x00b03, 0x001e3, 0x1a014, 0x30287 },
339	{   7, 0x00b03, 0x001e4, 0x1a014, 0x30282 },
340	{   8, 0x00b03, 0x001e4, 0x1a014, 0x30287 },
341	{   9, 0x00b03, 0x001e5, 0x1a014, 0x30282 },
342	{  10, 0x00b03, 0x001e5, 0x1a014, 0x30287 },
343	{  11, 0x00b03, 0x001e6, 0x1a014, 0x30282 },
344	{  12, 0x00b03, 0x001e6, 0x1a014, 0x30287 },
345	{  13, 0x00b03, 0x001e7, 0x1a014, 0x30282 },
346	{  14, 0x00b03, 0x001e8, 0x1a014, 0x30284 },
347
348	{  34, 0x00b03, 0x20266, 0x36014, 0x30282 },
349	{  38, 0x00b03, 0x20267, 0x36014, 0x30284 },
350	{  42, 0x00b03, 0x20268, 0x36014, 0x30286 },
351	{  46, 0x00b03, 0x20269, 0x36014, 0x30288 },
352
353	{  36, 0x00b03, 0x00266, 0x26014, 0x30288 },
354	{  40, 0x00b03, 0x00268, 0x26014, 0x30280 },
355	{  44, 0x00b03, 0x00269, 0x26014, 0x30282 },
356	{  48, 0x00b03, 0x0026a, 0x26014, 0x30284 },
357	{  52, 0x00b03, 0x0026b, 0x26014, 0x30286 },
358	{  56, 0x00b03, 0x0026c, 0x26014, 0x30288 },
359	{  60, 0x00b03, 0x0026e, 0x26014, 0x30280 },
360	{  64, 0x00b03, 0x0026f, 0x26014, 0x30282 },
361
362	{ 100, 0x00b03, 0x0028a, 0x2e014, 0x30280 },
363	{ 104, 0x00b03, 0x0028b, 0x2e014, 0x30282 },
364	{ 108, 0x00b03, 0x0028c, 0x2e014, 0x30284 },
365	{ 112, 0x00b03, 0x0028d, 0x2e014, 0x30286 },
366	{ 116, 0x00b03, 0x0028e, 0x2e014, 0x30288 },
367	{ 120, 0x00b03, 0x002a0, 0x2e014, 0x30280 },
368	{ 124, 0x00b03, 0x002a1, 0x2e014, 0x30282 },
369	{ 128, 0x00b03, 0x002a2, 0x2e014, 0x30284 },
370	{ 132, 0x00b03, 0x002a3, 0x2e014, 0x30286 },
371	{ 136, 0x00b03, 0x002a4, 0x2e014, 0x30288 },
372	{ 140, 0x00b03, 0x002a6, 0x2e014, 0x30280 },
373
374	{ 149, 0x00b03, 0x002a8, 0x2e014, 0x30287 },
375	{ 153, 0x00b03, 0x002a9, 0x2e014, 0x30289 },
376	{ 157, 0x00b03, 0x002ab, 0x2e014, 0x30281 },
377	{ 161, 0x00b03, 0x002ac, 0x2e014, 0x30283 },
378	{ 165, 0x00b03, 0x002ad, 0x2e014, 0x30285 }
379}, rum_rf5225[] = {
380	{   1, 0x00b33, 0x011e1, 0x1a014, 0x30282 },
381	{   2, 0x00b33, 0x011e1, 0x1a014, 0x30287 },
382	{   3, 0x00b33, 0x011e2, 0x1a014, 0x30282 },
383	{   4, 0x00b33, 0x011e2, 0x1a014, 0x30287 },
384	{   5, 0x00b33, 0x011e3, 0x1a014, 0x30282 },
385	{   6, 0x00b33, 0x011e3, 0x1a014, 0x30287 },
386	{   7, 0x00b33, 0x011e4, 0x1a014, 0x30282 },
387	{   8, 0x00b33, 0x011e4, 0x1a014, 0x30287 },
388	{   9, 0x00b33, 0x011e5, 0x1a014, 0x30282 },
389	{  10, 0x00b33, 0x011e5, 0x1a014, 0x30287 },
390	{  11, 0x00b33, 0x011e6, 0x1a014, 0x30282 },
391	{  12, 0x00b33, 0x011e6, 0x1a014, 0x30287 },
392	{  13, 0x00b33, 0x011e7, 0x1a014, 0x30282 },
393	{  14, 0x00b33, 0x011e8, 0x1a014, 0x30284 },
394
395	{  34, 0x00b33, 0x01266, 0x26014, 0x30282 },
396	{  38, 0x00b33, 0x01267, 0x26014, 0x30284 },
397	{  42, 0x00b33, 0x01268, 0x26014, 0x30286 },
398	{  46, 0x00b33, 0x01269, 0x26014, 0x30288 },
399
400	{  36, 0x00b33, 0x01266, 0x26014, 0x30288 },
401	{  40, 0x00b33, 0x01268, 0x26014, 0x30280 },
402	{  44, 0x00b33, 0x01269, 0x26014, 0x30282 },
403	{  48, 0x00b33, 0x0126a, 0x26014, 0x30284 },
404	{  52, 0x00b33, 0x0126b, 0x26014, 0x30286 },
405	{  56, 0x00b33, 0x0126c, 0x26014, 0x30288 },
406	{  60, 0x00b33, 0x0126e, 0x26014, 0x30280 },
407	{  64, 0x00b33, 0x0126f, 0x26014, 0x30282 },
408
409	{ 100, 0x00b33, 0x0128a, 0x2e014, 0x30280 },
410	{ 104, 0x00b33, 0x0128b, 0x2e014, 0x30282 },
411	{ 108, 0x00b33, 0x0128c, 0x2e014, 0x30284 },
412	{ 112, 0x00b33, 0x0128d, 0x2e014, 0x30286 },
413	{ 116, 0x00b33, 0x0128e, 0x2e014, 0x30288 },
414	{ 120, 0x00b33, 0x012a0, 0x2e014, 0x30280 },
415	{ 124, 0x00b33, 0x012a1, 0x2e014, 0x30282 },
416	{ 128, 0x00b33, 0x012a2, 0x2e014, 0x30284 },
417	{ 132, 0x00b33, 0x012a3, 0x2e014, 0x30286 },
418	{ 136, 0x00b33, 0x012a4, 0x2e014, 0x30288 },
419	{ 140, 0x00b33, 0x012a6, 0x2e014, 0x30280 },
420
421	{ 149, 0x00b33, 0x012a8, 0x2e014, 0x30287 },
422	{ 153, 0x00b33, 0x012a9, 0x2e014, 0x30289 },
423	{ 157, 0x00b33, 0x012ab, 0x2e014, 0x30281 },
424	{ 161, 0x00b33, 0x012ac, 0x2e014, 0x30283 },
425	{ 165, 0x00b33, 0x012ad, 0x2e014, 0x30285 }
426};
427
428static const struct usb_config rum_config[RUM_N_TRANSFER] = {
429	[RUM_BULK_WR] = {
430		.type = UE_BULK,
431		.endpoint = UE_ADDR_ANY,
432		.direction = UE_DIR_OUT,
433		.bufsize = (MCLBYTES + RT2573_TX_DESC_SIZE + 8),
434		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
435		.callback = rum_bulk_write_callback,
436		.timeout = 5000,	/* ms */
437	},
438	[RUM_BULK_RD] = {
439		.type = UE_BULK,
440		.endpoint = UE_ADDR_ANY,
441		.direction = UE_DIR_IN,
442		.bufsize = (MCLBYTES + RT2573_RX_DESC_SIZE),
443		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
444		.callback = rum_bulk_read_callback,
445	},
446};
447
448static int
449rum_match(device_t self)
450{
451	struct usb_attach_arg *uaa = device_get_ivars(self);
452
453	if (uaa->usb_mode != USB_MODE_HOST)
454		return (ENXIO);
455	if (uaa->info.bConfigIndex != 0)
456		return (ENXIO);
457	if (uaa->info.bIfaceIndex != RT2573_IFACE_INDEX)
458		return (ENXIO);
459
460	return (usbd_lookup_id_by_uaa(rum_devs, sizeof(rum_devs), uaa));
461}
462
463static int
464rum_attach(device_t self)
465{
466	struct usb_attach_arg *uaa = device_get_ivars(self);
467	struct rum_softc *sc = device_get_softc(self);
468	struct ieee80211com *ic = &sc->sc_ic;
469	uint8_t iface_index, bands;
470	uint32_t tmp;
471	int error, ntries;
472
473	device_set_usb_desc(self);
474	sc->sc_udev = uaa->device;
475	sc->sc_dev = self;
476
477	RUM_LOCK_INIT(sc);
478	RUM_CMDQ_LOCK_INIT(sc);
479	mbufq_init(&sc->sc_snd, ifqmaxlen);
480
481	iface_index = RT2573_IFACE_INDEX;
482	error = usbd_transfer_setup(uaa->device, &iface_index,
483	    sc->sc_xfer, rum_config, RUM_N_TRANSFER, sc, &sc->sc_mtx);
484	if (error) {
485		device_printf(self, "could not allocate USB transfers, "
486		    "err=%s\n", usbd_errstr(error));
487		goto detach;
488	}
489
490	RUM_LOCK(sc);
491	/* retrieve RT2573 rev. no */
492	for (ntries = 0; ntries < 100; ntries++) {
493		if ((tmp = rum_read(sc, RT2573_MAC_CSR0)) != 0)
494			break;
495		if (rum_pause(sc, hz / 100))
496			break;
497	}
498	if (ntries == 100) {
499		device_printf(sc->sc_dev, "timeout waiting for chip to settle\n");
500		RUM_UNLOCK(sc);
501		goto detach;
502	}
503
504	/* retrieve MAC address and various other things from EEPROM */
505	rum_read_eeprom(sc);
506
507	device_printf(sc->sc_dev, "MAC/BBP RT2573 (rev 0x%05x), RF %s\n",
508	    tmp, rum_get_rf(sc->rf_rev));
509
510	rum_load_microcode(sc, rt2573_ucode, sizeof(rt2573_ucode));
511	RUM_UNLOCK(sc);
512
513	ic->ic_softc = sc;
514	ic->ic_name = device_get_nameunit(self);
515	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
516
517	/* set device capabilities */
518	ic->ic_caps =
519	      IEEE80211_C_STA		/* station mode supported */
520	    | IEEE80211_C_IBSS		/* IBSS mode supported */
521	    | IEEE80211_C_MONITOR	/* monitor mode supported */
522	    | IEEE80211_C_HOSTAP	/* HostAp mode supported */
523	    | IEEE80211_C_AHDEMO	/* adhoc demo mode */
524	    | IEEE80211_C_TXPMGT	/* tx power management */
525	    | IEEE80211_C_SHPREAMBLE	/* short preamble supported */
526	    | IEEE80211_C_SHSLOT	/* short slot time supported */
527	    | IEEE80211_C_BGSCAN	/* bg scanning supported */
528	    | IEEE80211_C_WPA		/* 802.11i */
529	    ;
530
531	ic->ic_cryptocaps =
532	    IEEE80211_CRYPTO_WEP |
533	    IEEE80211_CRYPTO_AES_CCM |
534	    IEEE80211_CRYPTO_TKIPMIC |
535	    IEEE80211_CRYPTO_TKIP;
536
537	bands = 0;
538	setbit(&bands, IEEE80211_MODE_11B);
539	setbit(&bands, IEEE80211_MODE_11G);
540	if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_5226)
541		setbit(&bands, IEEE80211_MODE_11A);
542	ieee80211_init_channels(ic, NULL, &bands);
543
544	ieee80211_ifattach(ic);
545	ic->ic_update_promisc = rum_update_promisc;
546	ic->ic_raw_xmit = rum_raw_xmit;
547	ic->ic_scan_start = rum_scan_start;
548	ic->ic_scan_end = rum_scan_end;
549	ic->ic_set_channel = rum_set_channel;
550	ic->ic_transmit = rum_transmit;
551	ic->ic_parent = rum_parent;
552	ic->ic_vap_create = rum_vap_create;
553	ic->ic_vap_delete = rum_vap_delete;
554	ic->ic_updateslot = rum_update_slot;
555	ic->ic_update_mcast = rum_update_mcast;
556
557	ieee80211_radiotap_attach(ic,
558	    &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
559		RT2573_TX_RADIOTAP_PRESENT,
560	    &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
561		RT2573_RX_RADIOTAP_PRESENT);
562
563	TASK_INIT(&sc->cmdq_task, 0, rum_cmdq_cb, sc);
564
565	if (bootverbose)
566		ieee80211_announce(ic);
567
568	return (0);
569
570detach:
571	rum_detach(self);
572	return (ENXIO);			/* failure */
573}
574
575static int
576rum_detach(device_t self)
577{
578	struct rum_softc *sc = device_get_softc(self);
579	struct ieee80211com *ic = &sc->sc_ic;
580
581	/* Prevent further ioctls */
582	RUM_LOCK(sc);
583	sc->sc_detached = 1;
584	RUM_UNLOCK(sc);
585
586	/* stop all USB transfers */
587	usbd_transfer_unsetup(sc->sc_xfer, RUM_N_TRANSFER);
588
589	/* free TX list, if any */
590	RUM_LOCK(sc);
591	rum_unsetup_tx_list(sc);
592	RUM_UNLOCK(sc);
593
594	if (ic->ic_softc == sc) {
595		ieee80211_draintask(ic, &sc->cmdq_task);
596		ieee80211_ifdetach(ic);
597	}
598
599	mbufq_drain(&sc->sc_snd);
600	RUM_CMDQ_LOCK_DESTROY(sc);
601	RUM_LOCK_DESTROY(sc);
602
603	return (0);
604}
605
606static usb_error_t
607rum_do_request(struct rum_softc *sc,
608    struct usb_device_request *req, void *data)
609{
610	usb_error_t err;
611	int ntries = 10;
612
613	while (ntries--) {
614		err = usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
615		    req, data, 0, NULL, 250 /* ms */);
616		if (err == 0)
617			break;
618
619		DPRINTFN(1, "Control request failed, %s (retrying)\n",
620		    usbd_errstr(err));
621		if (rum_pause(sc, hz / 100))
622			break;
623	}
624	return (err);
625}
626
627static struct ieee80211vap *
628rum_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
629    enum ieee80211_opmode opmode, int flags,
630    const uint8_t bssid[IEEE80211_ADDR_LEN],
631    const uint8_t mac[IEEE80211_ADDR_LEN])
632{
633	struct rum_softc *sc = ic->ic_softc;
634	struct rum_vap *rvp;
635	struct ieee80211vap *vap;
636
637	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
638		return NULL;
639	rvp = malloc(sizeof(struct rum_vap), M_80211_VAP, M_WAITOK | M_ZERO);
640	vap = &rvp->vap;
641	/* enable s/w bmiss handling for sta mode */
642
643	if (ieee80211_vap_setup(ic, vap, name, unit, opmode,
644	    flags | IEEE80211_CLONE_NOBEACONS, bssid) != 0) {
645		/* out of memory */
646		free(rvp, M_80211_VAP);
647		return (NULL);
648	}
649
650	/* override state transition machine */
651	rvp->newstate = vap->iv_newstate;
652	vap->iv_newstate = rum_newstate;
653	vap->iv_key_alloc = rum_key_alloc;
654	vap->iv_key_set = rum_key_set;
655	vap->iv_key_delete = rum_key_delete;
656	vap->iv_update_beacon = rum_update_beacon;
657	vap->iv_max_aid = RT2573_ADDR_MAX;
658
659	usb_callout_init_mtx(&rvp->ratectl_ch, &sc->sc_mtx, 0);
660	TASK_INIT(&rvp->ratectl_task, 0, rum_ratectl_task, rvp);
661	ieee80211_ratectl_init(vap);
662	ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */);
663	/* complete setup */
664	ieee80211_vap_attach(vap, ieee80211_media_change,
665	    ieee80211_media_status, mac);
666	ic->ic_opmode = opmode;
667	return vap;
668}
669
670static void
671rum_vap_delete(struct ieee80211vap *vap)
672{
673	struct rum_vap *rvp = RUM_VAP(vap);
674	struct ieee80211com *ic = vap->iv_ic;
675
676	m_freem(rvp->bcn_mbuf);
677	usb_callout_drain(&rvp->ratectl_ch);
678	ieee80211_draintask(ic, &rvp->ratectl_task);
679	ieee80211_ratectl_deinit(vap);
680	ieee80211_vap_detach(vap);
681	free(rvp, M_80211_VAP);
682}
683
684static void
685rum_cmdq_cb(void *arg, int pending)
686{
687	struct rum_softc *sc = arg;
688	struct rum_cmdq *rc;
689
690	RUM_CMDQ_LOCK(sc);
691	while (sc->cmdq[sc->cmdq_first].func != NULL) {
692		rc = &sc->cmdq[sc->cmdq_first];
693		RUM_CMDQ_UNLOCK(sc);
694
695		RUM_LOCK(sc);
696		rc->func(sc, &rc->data, rc->rvp_id);
697		RUM_UNLOCK(sc);
698
699		RUM_CMDQ_LOCK(sc);
700		memset(rc, 0, sizeof (*rc));
701		sc->cmdq_first = (sc->cmdq_first + 1) % RUM_CMDQ_SIZE;
702	}
703	RUM_CMDQ_UNLOCK(sc);
704}
705
706static int
707rum_cmd_sleepable(struct rum_softc *sc, const void *ptr, size_t len,
708    uint8_t rvp_id, CMD_FUNC_PROTO)
709{
710	struct ieee80211com *ic = &sc->sc_ic;
711
712	KASSERT(len <= sizeof(union sec_param), ("buffer overflow"));
713
714	RUM_CMDQ_LOCK(sc);
715	if (sc->cmdq[sc->cmdq_last].func != NULL) {
716		device_printf(sc->sc_dev, "%s: cmdq overflow\n", __func__);
717		RUM_CMDQ_UNLOCK(sc);
718
719		return EAGAIN;
720	}
721
722	if (ptr != NULL)
723		memcpy(&sc->cmdq[sc->cmdq_last].data, ptr, len);
724	sc->cmdq[sc->cmdq_last].rvp_id = rvp_id;
725	sc->cmdq[sc->cmdq_last].func = func;
726	sc->cmdq_last = (sc->cmdq_last + 1) % RUM_CMDQ_SIZE;
727	RUM_CMDQ_UNLOCK(sc);
728
729	ieee80211_runtask(ic, &sc->cmdq_task);
730
731	return 0;
732}
733
734static void
735rum_tx_free(struct rum_tx_data *data, int txerr)
736{
737	struct rum_softc *sc = data->sc;
738
739	if (data->m != NULL) {
740		ieee80211_tx_complete(data->ni, data->m, txerr);
741		data->m = NULL;
742		data->ni = NULL;
743	}
744	STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
745	sc->tx_nfree++;
746}
747
748static void
749rum_setup_tx_list(struct rum_softc *sc)
750{
751	struct rum_tx_data *data;
752	int i;
753
754	sc->tx_nfree = 0;
755	STAILQ_INIT(&sc->tx_q);
756	STAILQ_INIT(&sc->tx_free);
757
758	for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
759		data = &sc->tx_data[i];
760
761		data->sc = sc;
762		STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
763		sc->tx_nfree++;
764	}
765}
766
767static void
768rum_unsetup_tx_list(struct rum_softc *sc)
769{
770	struct rum_tx_data *data;
771	int i;
772
773	/* make sure any subsequent use of the queues will fail */
774	sc->tx_nfree = 0;
775	STAILQ_INIT(&sc->tx_q);
776	STAILQ_INIT(&sc->tx_free);
777
778	/* free up all node references and mbufs */
779	for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
780		data = &sc->tx_data[i];
781
782		if (data->m != NULL) {
783			m_freem(data->m);
784			data->m = NULL;
785		}
786		if (data->ni != NULL) {
787			ieee80211_free_node(data->ni);
788			data->ni = NULL;
789		}
790	}
791}
792
793static int
794rum_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
795{
796	struct rum_vap *rvp = RUM_VAP(vap);
797	struct ieee80211com *ic = vap->iv_ic;
798	struct rum_softc *sc = ic->ic_softc;
799	const struct ieee80211_txparam *tp;
800	enum ieee80211_state ostate;
801	struct ieee80211_node *ni;
802	int ret;
803
804	ostate = vap->iv_state;
805	DPRINTF("%s -> %s\n",
806		ieee80211_state_name[ostate],
807		ieee80211_state_name[nstate]);
808
809	IEEE80211_UNLOCK(ic);
810	RUM_LOCK(sc);
811	usb_callout_stop(&rvp->ratectl_ch);
812
813	switch (nstate) {
814	case IEEE80211_S_INIT:
815		if (ostate == IEEE80211_S_RUN)
816			rum_abort_tsf_sync(sc);
817
818		break;
819
820	case IEEE80211_S_RUN:
821		ni = ieee80211_ref_node(vap->iv_bss);
822
823		if (vap->iv_opmode != IEEE80211_M_MONITOR) {
824			if (ic->ic_bsschan == IEEE80211_CHAN_ANYC ||
825			    ni->ni_chan == IEEE80211_CHAN_ANYC) {
826				ret = EINVAL;
827				goto run_fail;
828			}
829			rum_update_slot_cb(sc, NULL, 0);
830			rum_enable_mrr(sc);
831			rum_set_txpreamble(sc);
832			rum_set_basicrates(sc);
833			rum_set_maxretry(sc, vap);
834			IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid);
835			rum_set_bssid(sc, sc->sc_bssid);
836		}
837
838		if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
839		    vap->iv_opmode == IEEE80211_M_IBSS) {
840			if ((ret = rum_alloc_beacon(sc, vap)) != 0)
841				goto run_fail;
842		}
843
844		if (vap->iv_opmode != IEEE80211_M_MONITOR &&
845		    vap->iv_opmode != IEEE80211_M_AHDEMO) {
846			if ((ret = rum_enable_tsf_sync(sc)) != 0)
847				goto run_fail;
848		} else
849			rum_enable_tsf(sc);
850
851		/* enable automatic rate adaptation */
852		tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
853		if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
854			rum_ratectl_start(sc, ni);
855		ieee80211_free_node(ni);
856		break;
857	default:
858		break;
859	}
860	RUM_UNLOCK(sc);
861	IEEE80211_LOCK(ic);
862	return (rvp->newstate(vap, nstate, arg));
863
864run_fail:
865	RUM_UNLOCK(sc);
866	IEEE80211_LOCK(ic);
867	ieee80211_free_node(ni);
868	return ret;
869}
870
871static void
872rum_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
873{
874	struct rum_softc *sc = usbd_xfer_softc(xfer);
875	struct ieee80211vap *vap;
876	struct rum_tx_data *data;
877	struct mbuf *m;
878	struct usb_page_cache *pc;
879	unsigned int len;
880	int actlen, sumlen;
881
882	usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
883
884	switch (USB_GET_STATE(xfer)) {
885	case USB_ST_TRANSFERRED:
886		DPRINTFN(11, "transfer complete, %d bytes\n", actlen);
887
888		/* free resources */
889		data = usbd_xfer_get_priv(xfer);
890		rum_tx_free(data, 0);
891		usbd_xfer_set_priv(xfer, NULL);
892
893		/* FALLTHROUGH */
894	case USB_ST_SETUP:
895tr_setup:
896		data = STAILQ_FIRST(&sc->tx_q);
897		if (data) {
898			STAILQ_REMOVE_HEAD(&sc->tx_q, next);
899			m = data->m;
900
901			if (m->m_pkthdr.len > (int)(MCLBYTES + RT2573_TX_DESC_SIZE)) {
902				DPRINTFN(0, "data overflow, %u bytes\n",
903				    m->m_pkthdr.len);
904				m->m_pkthdr.len = (MCLBYTES + RT2573_TX_DESC_SIZE);
905			}
906			pc = usbd_xfer_get_frame(xfer, 0);
907			usbd_copy_in(pc, 0, &data->desc, RT2573_TX_DESC_SIZE);
908			usbd_m_copy_in(pc, RT2573_TX_DESC_SIZE, m, 0,
909			    m->m_pkthdr.len);
910
911			vap = data->ni->ni_vap;
912			if (ieee80211_radiotap_active_vap(vap)) {
913				struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
914
915				tap->wt_flags = 0;
916				tap->wt_rate = data->rate;
917				rum_get_tsf(sc, &tap->wt_tsf);
918				tap->wt_antenna = sc->tx_ant;
919
920				ieee80211_radiotap_tx(vap, m);
921			}
922
923			/* align end on a 4-bytes boundary */
924			len = (RT2573_TX_DESC_SIZE + m->m_pkthdr.len + 3) & ~3;
925			if ((len % 64) == 0)
926				len += 4;
927
928			DPRINTFN(11, "sending frame len=%u xferlen=%u\n",
929			    m->m_pkthdr.len, len);
930
931			usbd_xfer_set_frame_len(xfer, 0, len);
932			usbd_xfer_set_priv(xfer, data);
933
934			usbd_transfer_submit(xfer);
935		}
936		rum_start(sc);
937		break;
938
939	default:			/* Error */
940		DPRINTFN(11, "transfer error, %s\n",
941		    usbd_errstr(error));
942
943		counter_u64_add(sc->sc_ic.ic_oerrors, 1);
944		data = usbd_xfer_get_priv(xfer);
945		if (data != NULL) {
946			rum_tx_free(data, error);
947			usbd_xfer_set_priv(xfer, NULL);
948		}
949
950		if (error != USB_ERR_CANCELLED) {
951			if (error == USB_ERR_TIMEOUT)
952				device_printf(sc->sc_dev, "device timeout\n");
953
954			/*
955			 * Try to clear stall first, also if other
956			 * errors occur, hence clearing stall
957			 * introduces a 50 ms delay:
958			 */
959			usbd_xfer_set_stall(xfer);
960			goto tr_setup;
961		}
962		break;
963	}
964}
965
966static void
967rum_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
968{
969	struct rum_softc *sc = usbd_xfer_softc(xfer);
970	struct ieee80211com *ic = &sc->sc_ic;
971	struct ieee80211_frame_min *wh;
972	struct ieee80211_node *ni;
973	struct mbuf *m = NULL;
974	struct usb_page_cache *pc;
975	uint32_t flags;
976	uint8_t rssi = 0;
977	int len;
978
979	usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
980
981	switch (USB_GET_STATE(xfer)) {
982	case USB_ST_TRANSFERRED:
983
984		DPRINTFN(15, "rx done, actlen=%d\n", len);
985
986		if (len < (int)(RT2573_RX_DESC_SIZE + IEEE80211_MIN_LEN)) {
987			DPRINTF("%s: xfer too short %d\n",
988			    device_get_nameunit(sc->sc_dev), len);
989			counter_u64_add(ic->ic_ierrors, 1);
990			goto tr_setup;
991		}
992
993		len -= RT2573_RX_DESC_SIZE;
994		pc = usbd_xfer_get_frame(xfer, 0);
995		usbd_copy_out(pc, 0, &sc->sc_rx_desc, RT2573_RX_DESC_SIZE);
996
997		rssi = rum_get_rssi(sc, sc->sc_rx_desc.rssi);
998		flags = le32toh(sc->sc_rx_desc.flags);
999		if (flags & RT2573_RX_CRC_ERROR) {
1000			/*
1001		         * This should not happen since we did not
1002		         * request to receive those frames when we
1003		         * filled RUM_TXRX_CSR2:
1004		         */
1005			DPRINTFN(5, "PHY or CRC error\n");
1006			counter_u64_add(ic->ic_ierrors, 1);
1007			goto tr_setup;
1008		}
1009		if ((flags & RT2573_RX_DEC_MASK) != RT2573_RX_DEC_OK) {
1010			switch (flags & RT2573_RX_DEC_MASK) {
1011			case RT2573_RX_IV_ERROR:
1012				DPRINTFN(5, "IV/EIV error\n");
1013				break;
1014			case RT2573_RX_MIC_ERROR:
1015				DPRINTFN(5, "MIC error\n");
1016				break;
1017			case RT2573_RX_KEY_ERROR:
1018				DPRINTFN(5, "Key error\n");
1019				break;
1020			}
1021			counter_u64_add(ic->ic_ierrors, 1);
1022			goto tr_setup;
1023		}
1024
1025		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1026		if (m == NULL) {
1027			DPRINTF("could not allocate mbuf\n");
1028			counter_u64_add(ic->ic_ierrors, 1);
1029			goto tr_setup;
1030		}
1031		usbd_copy_out(pc, RT2573_RX_DESC_SIZE,
1032		    mtod(m, uint8_t *), len);
1033
1034		wh = mtod(m, struct ieee80211_frame_min *);
1035
1036		if ((wh->i_fc[1] & IEEE80211_FC1_PROTECTED) &&
1037		    (flags & RT2573_RX_CIP_MASK) !=
1038		     RT2573_RX_CIP_MODE(RT2573_MODE_NOSEC)) {
1039			wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
1040			m->m_flags |= M_WEP;
1041		}
1042
1043		/* finalize mbuf */
1044		m->m_pkthdr.len = m->m_len = (flags >> 16) & 0xfff;
1045
1046		if (ieee80211_radiotap_active(ic)) {
1047			struct rum_rx_radiotap_header *tap = &sc->sc_rxtap;
1048
1049			tap->wr_flags = 0;
1050			tap->wr_rate = ieee80211_plcp2rate(sc->sc_rx_desc.rate,
1051			    (flags & RT2573_RX_OFDM) ?
1052			    IEEE80211_T_OFDM : IEEE80211_T_CCK);
1053			rum_get_tsf(sc, &tap->wr_tsf);
1054			tap->wr_antsignal = RT2573_NOISE_FLOOR + rssi;
1055			tap->wr_antnoise = RT2573_NOISE_FLOOR;
1056			tap->wr_antenna = sc->rx_ant;
1057		}
1058		/* FALLTHROUGH */
1059	case USB_ST_SETUP:
1060tr_setup:
1061		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1062		usbd_transfer_submit(xfer);
1063
1064		/*
1065		 * At the end of a USB callback it is always safe to unlock
1066		 * the private mutex of a device! That is why we do the
1067		 * "ieee80211_input" here, and not some lines up!
1068		 */
1069		RUM_UNLOCK(sc);
1070		if (m) {
1071			if (m->m_len >= sizeof(struct ieee80211_frame_min))
1072				ni = ieee80211_find_rxnode(ic, wh);
1073			else
1074				ni = NULL;
1075
1076			if (ni != NULL) {
1077				(void) ieee80211_input(ni, m, rssi,
1078				    RT2573_NOISE_FLOOR);
1079				ieee80211_free_node(ni);
1080			} else
1081				(void) ieee80211_input_all(ic, m, rssi,
1082				    RT2573_NOISE_FLOOR);
1083		}
1084		RUM_LOCK(sc);
1085		rum_start(sc);
1086		return;
1087
1088	default:			/* Error */
1089		if (error != USB_ERR_CANCELLED) {
1090			/* try to clear stall first */
1091			usbd_xfer_set_stall(xfer);
1092			goto tr_setup;
1093		}
1094		return;
1095	}
1096}
1097
1098static uint8_t
1099rum_plcp_signal(int rate)
1100{
1101	switch (rate) {
1102	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1103	case 12:	return 0xb;
1104	case 18:	return 0xf;
1105	case 24:	return 0xa;
1106	case 36:	return 0xe;
1107	case 48:	return 0x9;
1108	case 72:	return 0xd;
1109	case 96:	return 0x8;
1110	case 108:	return 0xc;
1111
1112	/* CCK rates (NB: not IEEE std, device-specific) */
1113	case 2:		return 0x0;
1114	case 4:		return 0x1;
1115	case 11:	return 0x2;
1116	case 22:	return 0x3;
1117	}
1118	return 0xff;		/* XXX unsupported/unknown rate */
1119}
1120
1121/*
1122 * Map net80211 cipher to RT2573 security mode.
1123 */
1124static uint8_t
1125rum_crypto_mode(struct rum_softc *sc, u_int cipher, int keylen)
1126{
1127	switch (cipher) {
1128	case IEEE80211_CIPHER_WEP:
1129		return (keylen < 8 ? RT2573_MODE_WEP40 : RT2573_MODE_WEP104);
1130	case IEEE80211_CIPHER_TKIP:
1131		return RT2573_MODE_TKIP;
1132	case IEEE80211_CIPHER_AES_CCM:
1133		return RT2573_MODE_AES_CCMP;
1134	default:
1135		device_printf(sc->sc_dev, "unknown cipher %d\n", cipher);
1136		return 0;
1137	}
1138}
1139
1140static void
1141rum_setup_tx_desc(struct rum_softc *sc, struct rum_tx_desc *desc,
1142    struct ieee80211_key *k, uint32_t flags, uint8_t xflags, int hdrlen,
1143    int len, int rate)
1144{
1145	struct ieee80211com *ic = &sc->sc_ic;
1146	uint16_t plcp_length;
1147	int remainder;
1148
1149	flags |= RT2573_TX_VALID;
1150	flags |= len << 16;
1151
1152	if (k != NULL && !(k->wk_flags & IEEE80211_KEY_SWCRYPT)) {
1153		const struct ieee80211_cipher *cip = k->wk_cipher;
1154
1155		len += cip->ic_header + cip->ic_trailer + cip->ic_miclen;
1156
1157		desc->eiv = 0;		/* for WEP */
1158		cip->ic_setiv(k, (uint8_t *)&desc->iv);
1159	}
1160
1161	/* setup PLCP fields */
1162	desc->plcp_signal  = rum_plcp_signal(rate);
1163	desc->plcp_service = 4;
1164
1165	len += IEEE80211_CRC_LEN;
1166	if (ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) {
1167		flags |= RT2573_TX_OFDM;
1168
1169		plcp_length = len & 0xfff;
1170		desc->plcp_length_hi = plcp_length >> 6;
1171		desc->plcp_length_lo = plcp_length & 0x3f;
1172	} else {
1173		if (rate == 0)
1174			rate = 2;	/* avoid division by zero */
1175		plcp_length = (16 * len + rate - 1) / rate;
1176		if (rate == 22) {
1177			remainder = (16 * len) % 22;
1178			if (remainder != 0 && remainder < 7)
1179				desc->plcp_service |= RT2573_PLCP_LENGEXT;
1180		}
1181		desc->plcp_length_hi = plcp_length >> 8;
1182		desc->plcp_length_lo = plcp_length & 0xff;
1183
1184		if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1185			desc->plcp_signal |= 0x08;
1186	}
1187
1188	desc->flags = htole32(flags);
1189	desc->hdrlen = hdrlen;
1190	desc->xflags = xflags;
1191
1192	desc->wme = htole16(RT2573_QID(0) | RT2573_AIFSN(2) |
1193	    RT2573_LOGCWMIN(4) | RT2573_LOGCWMAX(10));
1194}
1195
1196static int
1197rum_sendprot(struct rum_softc *sc,
1198    const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate)
1199{
1200	struct ieee80211com *ic = ni->ni_ic;
1201	const struct ieee80211_frame *wh;
1202	struct rum_tx_data *data;
1203	struct mbuf *mprot;
1204	int protrate, pktlen, flags, isshort;
1205	uint16_t dur;
1206
1207	RUM_LOCK_ASSERT(sc);
1208	KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY,
1209	    ("protection %d", prot));
1210
1211	wh = mtod(m, const struct ieee80211_frame *);
1212	pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
1213
1214	protrate = ieee80211_ctl_rate(ic->ic_rt, rate);
1215
1216	isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0;
1217	dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort)
1218	    + ieee80211_ack_duration(ic->ic_rt, rate, isshort);
1219	flags = 0;
1220	if (prot == IEEE80211_PROT_RTSCTS) {
1221		/* NB: CTS is the same size as an ACK */
1222		dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort);
1223		flags |= RT2573_TX_NEED_ACK;
1224		mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur);
1225	} else {
1226		mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur);
1227	}
1228	if (mprot == NULL) {
1229		/* XXX stat + msg */
1230		return (ENOBUFS);
1231	}
1232	data = STAILQ_FIRST(&sc->tx_free);
1233	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1234	sc->tx_nfree--;
1235
1236	data->m = mprot;
1237	data->ni = ieee80211_ref_node(ni);
1238	data->rate = protrate;
1239	rum_setup_tx_desc(sc, &data->desc, NULL, flags, 0, 0,
1240	    mprot->m_pkthdr.len, protrate);
1241
1242	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1243	usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1244
1245	return 0;
1246}
1247
1248static uint32_t
1249rum_tx_crypto_flags(struct rum_softc *sc, struct ieee80211_node *ni,
1250    const struct ieee80211_key *k)
1251{
1252	struct ieee80211vap *vap = ni->ni_vap;
1253	u_int cipher;
1254	uint32_t flags = 0;
1255	uint8_t mode, pos;
1256
1257	if (!(k->wk_flags & IEEE80211_KEY_SWCRYPT)) {
1258		cipher = k->wk_cipher->ic_cipher;
1259		pos = k->wk_keyix;
1260		mode = rum_crypto_mode(sc, cipher, k->wk_keylen);
1261		if (mode == 0)
1262			return 0;
1263
1264		flags |= RT2573_TX_CIP_MODE(mode);
1265
1266		/* Do not trust GROUP flag */
1267		if (!(k >= &vap->iv_nw_keys[0] &&
1268		      k < &vap->iv_nw_keys[IEEE80211_WEP_NKID]))
1269			flags |= RT2573_TX_KEY_PAIR;
1270		else
1271			pos += 0 * RT2573_SKEY_MAX;	/* vap id */
1272
1273		flags |= RT2573_TX_KEY_ID(pos);
1274
1275		if (cipher == IEEE80211_CIPHER_TKIP)
1276			flags |= RT2573_TX_TKIPMIC;
1277	}
1278
1279	return flags;
1280}
1281
1282static int
1283rum_tx_mgt(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1284{
1285	struct ieee80211vap *vap = ni->ni_vap;
1286	struct ieee80211com *ic = &sc->sc_ic;
1287	struct rum_tx_data *data;
1288	struct ieee80211_frame *wh;
1289	const struct ieee80211_txparam *tp;
1290	struct ieee80211_key *k = NULL;
1291	uint32_t flags = 0;
1292	uint16_t dur;
1293	uint8_t type, xflags = 0;
1294	int hdrlen;
1295
1296	RUM_LOCK_ASSERT(sc);
1297
1298	data = STAILQ_FIRST(&sc->tx_free);
1299	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1300	sc->tx_nfree--;
1301
1302	wh = mtod(m0, struct ieee80211_frame *);
1303	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1304	hdrlen = ieee80211_anyhdrsize(wh);
1305
1306	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1307		k = ieee80211_crypto_get_txkey(ni, m0);
1308		if (k == NULL)
1309			return (ENOENT);
1310
1311		if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) &&
1312		    !k->wk_cipher->ic_encap(k, m0))
1313			return (ENOBUFS);
1314
1315		wh = mtod(m0, struct ieee80211_frame *);
1316	}
1317
1318	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1319
1320	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1321		flags |= RT2573_TX_NEED_ACK;
1322
1323		dur = ieee80211_ack_duration(ic->ic_rt, tp->mgmtrate,
1324		    ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1325		USETW(wh->i_dur, dur);
1326
1327		/* tell hardware to add timestamp for probe responses */
1328		if (type == IEEE80211_FC0_TYPE_MGT &&
1329		    (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
1330		    IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1331			flags |= RT2573_TX_TIMESTAMP;
1332	}
1333
1334	if (type != IEEE80211_FC0_TYPE_CTL && !IEEE80211_QOS_HAS_SEQ(wh))
1335		xflags |= RT2573_TX_HWSEQ;
1336
1337	if (k != NULL)
1338		flags |= rum_tx_crypto_flags(sc, ni, k);
1339
1340	data->m = m0;
1341	data->ni = ni;
1342	data->rate = tp->mgmtrate;
1343
1344	rum_setup_tx_desc(sc, &data->desc, k, flags, xflags, hdrlen,
1345	    m0->m_pkthdr.len, tp->mgmtrate);
1346
1347	DPRINTFN(10, "sending mgt frame len=%d rate=%d\n",
1348	    m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, tp->mgmtrate);
1349
1350	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1351	usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1352
1353	return (0);
1354}
1355
1356static int
1357rum_tx_raw(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
1358    const struct ieee80211_bpf_params *params)
1359{
1360	struct ieee80211com *ic = ni->ni_ic;
1361	struct ieee80211_frame *wh;
1362	struct rum_tx_data *data;
1363	uint32_t flags;
1364	uint8_t type, xflags = 0;
1365	int rate, error;
1366
1367	RUM_LOCK_ASSERT(sc);
1368
1369	wh = mtod(m0, struct ieee80211_frame *);
1370	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1371
1372	rate = params->ibp_rate0;
1373	if (!ieee80211_isratevalid(ic->ic_rt, rate))
1374		return (EINVAL);
1375
1376	flags = 0;
1377	if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
1378		flags |= RT2573_TX_NEED_ACK;
1379	if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) {
1380		error = rum_sendprot(sc, m0, ni,
1381		    params->ibp_flags & IEEE80211_BPF_RTS ?
1382			 IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY,
1383		    rate);
1384		if (error || sc->tx_nfree == 0)
1385			return (ENOBUFS);
1386
1387		flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
1388	}
1389
1390	if (type != IEEE80211_FC0_TYPE_CTL && !IEEE80211_QOS_HAS_SEQ(wh))
1391		xflags |= RT2573_TX_HWSEQ;
1392
1393	data = STAILQ_FIRST(&sc->tx_free);
1394	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1395	sc->tx_nfree--;
1396
1397	data->m = m0;
1398	data->ni = ni;
1399	data->rate = rate;
1400
1401	/* XXX need to setup descriptor ourself */
1402	rum_setup_tx_desc(sc, &data->desc, NULL, flags, xflags, 0,
1403	    m0->m_pkthdr.len, rate);
1404
1405	DPRINTFN(10, "sending raw frame len=%u rate=%u\n",
1406	    m0->m_pkthdr.len, rate);
1407
1408	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1409	usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1410
1411	return 0;
1412}
1413
1414static int
1415rum_tx_data(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1416{
1417	struct ieee80211vap *vap = ni->ni_vap;
1418	struct ieee80211com *ic = &sc->sc_ic;
1419	struct rum_tx_data *data;
1420	struct ieee80211_frame *wh;
1421	const struct ieee80211_txparam *tp;
1422	struct ieee80211_key *k = NULL;
1423	uint32_t flags = 0;
1424	uint16_t dur;
1425	uint8_t type, xflags = 0;
1426	int error, hdrlen, rate;
1427
1428	RUM_LOCK_ASSERT(sc);
1429
1430	wh = mtod(m0, struct ieee80211_frame *);
1431	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1432	hdrlen = ieee80211_anyhdrsize(wh);
1433
1434	tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
1435	if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1436		rate = tp->mcastrate;
1437	else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
1438		rate = tp->ucastrate;
1439	else
1440		rate = ni->ni_txrate;
1441
1442	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1443		k = ieee80211_crypto_get_txkey(ni, m0);
1444		if (k == NULL) {
1445			m_freem(m0);
1446			return (ENOENT);
1447		}
1448		if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) &&
1449		    !k->wk_cipher->ic_encap(k, m0)) {
1450			m_freem(m0);
1451			return (ENOBUFS);
1452		}
1453
1454		/* packet header may have moved, reset our local pointer */
1455		wh = mtod(m0, struct ieee80211_frame *);
1456	}
1457
1458	if (type != IEEE80211_FC0_TYPE_CTL && !IEEE80211_QOS_HAS_SEQ(wh))
1459		xflags |= RT2573_TX_HWSEQ;
1460
1461	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1462		int prot = IEEE80211_PROT_NONE;
1463		if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold)
1464			prot = IEEE80211_PROT_RTSCTS;
1465		else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
1466		    ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM)
1467			prot = ic->ic_protmode;
1468		if (prot != IEEE80211_PROT_NONE) {
1469			error = rum_sendprot(sc, m0, ni, prot, rate);
1470			if (error || sc->tx_nfree == 0) {
1471				m_freem(m0);
1472				return ENOBUFS;
1473			}
1474			flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
1475		}
1476	}
1477
1478	if (k != NULL)
1479		flags |= rum_tx_crypto_flags(sc, ni, k);
1480
1481	data = STAILQ_FIRST(&sc->tx_free);
1482	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1483	sc->tx_nfree--;
1484
1485	data->m = m0;
1486	data->ni = ni;
1487	data->rate = rate;
1488
1489	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1490		flags |= RT2573_TX_NEED_ACK;
1491
1492		dur = ieee80211_ack_duration(ic->ic_rt, rate,
1493		    ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1494		USETW(wh->i_dur, dur);
1495	}
1496
1497	rum_setup_tx_desc(sc, &data->desc, k, flags, xflags, hdrlen,
1498	    m0->m_pkthdr.len, rate);
1499
1500	DPRINTFN(10, "sending frame len=%d rate=%d\n",
1501	    m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, rate);
1502
1503	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1504	usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1505
1506	return 0;
1507}
1508
1509static int
1510rum_transmit(struct ieee80211com *ic, struct mbuf *m)
1511{
1512	struct rum_softc *sc = ic->ic_softc;
1513	int error;
1514
1515	RUM_LOCK(sc);
1516	if (!sc->sc_running) {
1517		RUM_UNLOCK(sc);
1518		return (ENXIO);
1519	}
1520	error = mbufq_enqueue(&sc->sc_snd, m);
1521	if (error) {
1522		RUM_UNLOCK(sc);
1523		return (error);
1524	}
1525	rum_start(sc);
1526	RUM_UNLOCK(sc);
1527
1528	return (0);
1529}
1530
1531static void
1532rum_start(struct rum_softc *sc)
1533{
1534	struct ieee80211_node *ni;
1535	struct mbuf *m;
1536
1537	RUM_LOCK_ASSERT(sc);
1538
1539	if (!sc->sc_running)
1540		return;
1541
1542	while (sc->tx_nfree >= RUM_TX_MINFREE &&
1543	    (m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
1544		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1545		if (rum_tx_data(sc, m, ni) != 0) {
1546			if_inc_counter(ni->ni_vap->iv_ifp,
1547			    IFCOUNTER_OERRORS, 1);
1548			ieee80211_free_node(ni);
1549			break;
1550		}
1551	}
1552}
1553
1554static void
1555rum_parent(struct ieee80211com *ic)
1556{
1557	struct rum_softc *sc = ic->ic_softc;
1558	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1559
1560	RUM_LOCK(sc);
1561	if (sc->sc_detached) {
1562		RUM_UNLOCK(sc);
1563		return;
1564	}
1565	RUM_UNLOCK(sc);
1566
1567	if (ic->ic_nrunning > 0) {
1568		if (rum_init(sc) == 0)
1569			ieee80211_start_all(ic);
1570		else
1571			ieee80211_stop(vap);
1572	} else
1573		rum_stop(sc);
1574}
1575
1576static void
1577rum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len)
1578{
1579	struct usb_device_request req;
1580	usb_error_t error;
1581
1582	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1583	req.bRequest = RT2573_READ_EEPROM;
1584	USETW(req.wValue, 0);
1585	USETW(req.wIndex, addr);
1586	USETW(req.wLength, len);
1587
1588	error = rum_do_request(sc, &req, buf);
1589	if (error != 0) {
1590		device_printf(sc->sc_dev, "could not read EEPROM: %s\n",
1591		    usbd_errstr(error));
1592	}
1593}
1594
1595static uint32_t
1596rum_read(struct rum_softc *sc, uint16_t reg)
1597{
1598	uint32_t val;
1599
1600	rum_read_multi(sc, reg, &val, sizeof val);
1601
1602	return le32toh(val);
1603}
1604
1605static void
1606rum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len)
1607{
1608	struct usb_device_request req;
1609	usb_error_t error;
1610
1611	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1612	req.bRequest = RT2573_READ_MULTI_MAC;
1613	USETW(req.wValue, 0);
1614	USETW(req.wIndex, reg);
1615	USETW(req.wLength, len);
1616
1617	error = rum_do_request(sc, &req, buf);
1618	if (error != 0) {
1619		device_printf(sc->sc_dev,
1620		    "could not multi read MAC register: %s\n",
1621		    usbd_errstr(error));
1622	}
1623}
1624
1625static usb_error_t
1626rum_write(struct rum_softc *sc, uint16_t reg, uint32_t val)
1627{
1628	uint32_t tmp = htole32(val);
1629
1630	return (rum_write_multi(sc, reg, &tmp, sizeof tmp));
1631}
1632
1633static usb_error_t
1634rum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len)
1635{
1636	struct usb_device_request req;
1637	usb_error_t error;
1638	size_t offset;
1639
1640	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1641	req.bRequest = RT2573_WRITE_MULTI_MAC;
1642	USETW(req.wValue, 0);
1643
1644	/* write at most 64 bytes at a time */
1645	for (offset = 0; offset < len; offset += 64) {
1646		USETW(req.wIndex, reg + offset);
1647		USETW(req.wLength, MIN(len - offset, 64));
1648
1649		error = rum_do_request(sc, &req, (char *)buf + offset);
1650		if (error != 0) {
1651			device_printf(sc->sc_dev,
1652			    "could not multi write MAC register: %s\n",
1653			    usbd_errstr(error));
1654			return (error);
1655		}
1656	}
1657
1658	return (USB_ERR_NORMAL_COMPLETION);
1659}
1660
1661static usb_error_t
1662rum_setbits(struct rum_softc *sc, uint16_t reg, uint32_t mask)
1663{
1664	return (rum_write(sc, reg, rum_read(sc, reg) | mask));
1665}
1666
1667static usb_error_t
1668rum_clrbits(struct rum_softc *sc, uint16_t reg, uint32_t mask)
1669{
1670	return (rum_write(sc, reg, rum_read(sc, reg) & ~mask));
1671}
1672
1673static usb_error_t
1674rum_modbits(struct rum_softc *sc, uint16_t reg, uint32_t set, uint32_t unset)
1675{
1676	return (rum_write(sc, reg, (rum_read(sc, reg) & ~unset) | set));
1677}
1678
1679static int
1680rum_bbp_busy(struct rum_softc *sc)
1681{
1682	int ntries;
1683
1684	for (ntries = 0; ntries < 100; ntries++) {
1685		if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1686			break;
1687		if (rum_pause(sc, hz / 100))
1688			break;
1689	}
1690	if (ntries == 100)
1691		return (ETIMEDOUT);
1692
1693	return (0);
1694}
1695
1696static void
1697rum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val)
1698{
1699	uint32_t tmp;
1700
1701	DPRINTFN(2, "reg=0x%08x\n", reg);
1702
1703	if (rum_bbp_busy(sc) != 0) {
1704		device_printf(sc->sc_dev, "could not write to BBP\n");
1705		return;
1706	}
1707
1708	tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val;
1709	rum_write(sc, RT2573_PHY_CSR3, tmp);
1710}
1711
1712static uint8_t
1713rum_bbp_read(struct rum_softc *sc, uint8_t reg)
1714{
1715	uint32_t val;
1716	int ntries;
1717
1718	DPRINTFN(2, "reg=0x%08x\n", reg);
1719
1720	if (rum_bbp_busy(sc) != 0) {
1721		device_printf(sc->sc_dev, "could not read BBP\n");
1722		return 0;
1723	}
1724
1725	val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8;
1726	rum_write(sc, RT2573_PHY_CSR3, val);
1727
1728	for (ntries = 0; ntries < 100; ntries++) {
1729		val = rum_read(sc, RT2573_PHY_CSR3);
1730		if (!(val & RT2573_BBP_BUSY))
1731			return val & 0xff;
1732		if (rum_pause(sc, hz / 100))
1733			break;
1734	}
1735
1736	device_printf(sc->sc_dev, "could not read BBP\n");
1737	return 0;
1738}
1739
1740static void
1741rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val)
1742{
1743	uint32_t tmp;
1744	int ntries;
1745
1746	for (ntries = 0; ntries < 100; ntries++) {
1747		if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY))
1748			break;
1749		if (rum_pause(sc, hz / 100))
1750			break;
1751	}
1752	if (ntries == 100) {
1753		device_printf(sc->sc_dev, "could not write to RF\n");
1754		return;
1755	}
1756
1757	tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 |
1758	    (reg & 3);
1759	rum_write(sc, RT2573_PHY_CSR4, tmp);
1760
1761	/* remember last written value in sc */
1762	sc->rf_regs[reg] = val;
1763
1764	DPRINTFN(15, "RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff);
1765}
1766
1767static void
1768rum_select_antenna(struct rum_softc *sc)
1769{
1770	uint8_t bbp4, bbp77;
1771	uint32_t tmp;
1772
1773	bbp4  = rum_bbp_read(sc, 4);
1774	bbp77 = rum_bbp_read(sc, 77);
1775
1776	/* TBD */
1777
1778	/* make sure Rx is disabled before switching antenna */
1779	tmp = rum_read(sc, RT2573_TXRX_CSR0);
1780	rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
1781
1782	rum_bbp_write(sc,  4, bbp4);
1783	rum_bbp_write(sc, 77, bbp77);
1784
1785	rum_write(sc, RT2573_TXRX_CSR0, tmp);
1786}
1787
1788/*
1789 * Enable multi-rate retries for frames sent at OFDM rates.
1790 * In 802.11b/g mode, allow fallback to CCK rates.
1791 */
1792static void
1793rum_enable_mrr(struct rum_softc *sc)
1794{
1795	struct ieee80211com *ic = &sc->sc_ic;
1796
1797	if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) {
1798		rum_setbits(sc, RT2573_TXRX_CSR4,
1799		    RT2573_MRR_ENABLED | RT2573_MRR_CCK_FALLBACK);
1800	} else {
1801		rum_modbits(sc, RT2573_TXRX_CSR4,
1802		    RT2573_MRR_ENABLED, RT2573_MRR_CCK_FALLBACK);
1803	}
1804}
1805
1806static void
1807rum_set_txpreamble(struct rum_softc *sc)
1808{
1809	struct ieee80211com *ic = &sc->sc_ic;
1810
1811	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
1812		rum_setbits(sc, RT2573_TXRX_CSR4, RT2573_SHORT_PREAMBLE);
1813	else
1814		rum_clrbits(sc, RT2573_TXRX_CSR4, RT2573_SHORT_PREAMBLE);
1815}
1816
1817static void
1818rum_set_basicrates(struct rum_softc *sc)
1819{
1820	struct ieee80211com *ic = &sc->sc_ic;
1821
1822	/* update basic rate set */
1823	if (ic->ic_curmode == IEEE80211_MODE_11B) {
1824		/* 11b basic rates: 1, 2Mbps */
1825		rum_write(sc, RT2573_TXRX_CSR5, 0x3);
1826	} else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) {
1827		/* 11a basic rates: 6, 12, 24Mbps */
1828		rum_write(sc, RT2573_TXRX_CSR5, 0x150);
1829	} else {
1830		/* 11b/g basic rates: 1, 2, 5.5, 11Mbps */
1831		rum_write(sc, RT2573_TXRX_CSR5, 0xf);
1832	}
1833}
1834
1835/*
1836 * Reprogram MAC/BBP to switch to a new band.  Values taken from the reference
1837 * driver.
1838 */
1839static void
1840rum_select_band(struct rum_softc *sc, struct ieee80211_channel *c)
1841{
1842	uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104;
1843
1844	/* update all BBP registers that depend on the band */
1845	bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c;
1846	bbp35 = 0x50; bbp97 = 0x48; bbp98  = 0x48;
1847	if (IEEE80211_IS_CHAN_5GHZ(c)) {
1848		bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c;
1849		bbp35 += 0x10; bbp97 += 0x10; bbp98  += 0x10;
1850	}
1851	if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1852	    (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1853		bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10;
1854	}
1855
1856	sc->bbp17 = bbp17;
1857	rum_bbp_write(sc,  17, bbp17);
1858	rum_bbp_write(sc,  96, bbp96);
1859	rum_bbp_write(sc, 104, bbp104);
1860
1861	if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1862	    (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1863		rum_bbp_write(sc, 75, 0x80);
1864		rum_bbp_write(sc, 86, 0x80);
1865		rum_bbp_write(sc, 88, 0x80);
1866	}
1867
1868	rum_bbp_write(sc, 35, bbp35);
1869	rum_bbp_write(sc, 97, bbp97);
1870	rum_bbp_write(sc, 98, bbp98);
1871
1872	if (IEEE80211_IS_CHAN_2GHZ(c)) {
1873		rum_modbits(sc, RT2573_PHY_CSR0, RT2573_PA_PE_2GHZ,
1874		    RT2573_PA_PE_5GHZ);
1875	} else {
1876		rum_modbits(sc, RT2573_PHY_CSR0, RT2573_PA_PE_5GHZ,
1877		    RT2573_PA_PE_2GHZ);
1878	}
1879}
1880
1881static void
1882rum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c)
1883{
1884	struct ieee80211com *ic = &sc->sc_ic;
1885	const struct rfprog *rfprog;
1886	uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT;
1887	int8_t power;
1888	int i, chan;
1889
1890	chan = ieee80211_chan2ieee(ic, c);
1891	if (chan == 0 || chan == IEEE80211_CHAN_ANY)
1892		return;
1893
1894	/* select the appropriate RF settings based on what EEPROM says */
1895	rfprog = (sc->rf_rev == RT2573_RF_5225 ||
1896		  sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226;
1897
1898	/* find the settings for this channel (we know it exists) */
1899	for (i = 0; rfprog[i].chan != chan; i++);
1900
1901	power = sc->txpow[i];
1902	if (power < 0) {
1903		bbp94 += power;
1904		power = 0;
1905	} else if (power > 31) {
1906		bbp94 += power - 31;
1907		power = 31;
1908	}
1909
1910	/*
1911	 * If we are switching from the 2GHz band to the 5GHz band or
1912	 * vice-versa, BBP registers need to be reprogrammed.
1913	 */
1914	if (c->ic_flags != ic->ic_curchan->ic_flags) {
1915		rum_select_band(sc, c);
1916		rum_select_antenna(sc);
1917	}
1918	ic->ic_curchan = c;
1919
1920	rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1921	rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1922	rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1923	rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1924
1925	rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1926	rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1927	rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7 | 1);
1928	rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1929
1930	rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1931	rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1932	rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1933	rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1934
1935	rum_pause(sc, hz / 100);
1936
1937	/* enable smart mode for MIMO-capable RFs */
1938	bbp3 = rum_bbp_read(sc, 3);
1939
1940	bbp3 &= ~RT2573_SMART_MODE;
1941	if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527)
1942		bbp3 |= RT2573_SMART_MODE;
1943
1944	rum_bbp_write(sc, 3, bbp3);
1945
1946	if (bbp94 != RT2573_BBPR94_DEFAULT)
1947		rum_bbp_write(sc, 94, bbp94);
1948
1949	/* give the chip some extra time to do the switchover */
1950	rum_pause(sc, hz / 100);
1951}
1952
1953static void
1954rum_set_maxretry(struct rum_softc *sc, struct ieee80211vap *vap)
1955{
1956	const struct ieee80211_txparam *tp;
1957	struct ieee80211_node *ni = vap->iv_bss;
1958	struct rum_vap *rvp = RUM_VAP(vap);
1959
1960	tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
1961	rvp->maxretry = tp->maxretry < 0xf ? tp->maxretry : 0xf;
1962
1963	rum_modbits(sc, RT2573_TXRX_CSR4, RT2573_SHORT_RETRY(rvp->maxretry) |
1964	    RT2573_LONG_RETRY(rvp->maxretry),
1965	    RT2573_SHORT_RETRY_MASK | RT2573_LONG_RETRY_MASK);
1966}
1967
1968/*
1969 * Enable TSF synchronization and tell h/w to start sending beacons for IBSS
1970 * and HostAP operating modes.
1971 */
1972static int
1973rum_enable_tsf_sync(struct rum_softc *sc)
1974{
1975	struct ieee80211com *ic = &sc->sc_ic;
1976	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1977	uint32_t tmp;
1978
1979	if (vap->iv_opmode != IEEE80211_M_STA) {
1980		/*
1981		 * Change default 16ms TBTT adjustment to 8ms.
1982		 * Must be done before enabling beacon generation.
1983		 */
1984		if (rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8) != 0)
1985			return EIO;
1986	}
1987
1988	tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000;
1989
1990	/* set beacon interval (in 1/16ms unit) */
1991	tmp |= vap->iv_bss->ni_intval * 16;
1992	tmp |= RT2573_TSF_TIMER_EN | RT2573_TBTT_TIMER_EN;
1993
1994	switch (vap->iv_opmode) {
1995	case IEEE80211_M_STA:
1996		/*
1997		 * Local TSF is always updated with remote TSF on beacon
1998		 * reception.
1999		 */
2000		tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_STA);
2001		break;
2002	case IEEE80211_M_IBSS:
2003		/*
2004		 * Local TSF is updated with remote TSF on beacon reception
2005		 * only if the remote TSF is greater than local TSF.
2006		 */
2007		tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_IBSS);
2008		tmp |= RT2573_BCN_TX_EN;
2009		break;
2010	case IEEE80211_M_HOSTAP:
2011		/* SYNC with nobody */
2012		tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_HOSTAP);
2013		tmp |= RT2573_BCN_TX_EN;
2014		break;
2015	default:
2016		device_printf(sc->sc_dev,
2017		    "Enabling TSF failed. undefined opmode %d\n",
2018		    vap->iv_opmode);
2019		return EINVAL;
2020	}
2021
2022	if (rum_write(sc, RT2573_TXRX_CSR9, tmp) != 0)
2023		return EIO;
2024
2025	return 0;
2026}
2027
2028static void
2029rum_enable_tsf(struct rum_softc *sc)
2030{
2031	rum_modbits(sc, RT2573_TXRX_CSR9, RT2573_TSF_TIMER_EN |
2032	    RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_DIS), 0x00ffffff);
2033}
2034
2035static void
2036rum_abort_tsf_sync(struct rum_softc *sc)
2037{
2038	rum_clrbits(sc, RT2573_TXRX_CSR9, 0x00ffffff);
2039}
2040
2041static void
2042rum_get_tsf(struct rum_softc *sc, uint64_t *buf)
2043{
2044	rum_read_multi(sc, RT2573_TXRX_CSR12, buf, sizeof (*buf));
2045}
2046
2047static void
2048rum_update_slot_cb(struct rum_softc *sc, union sec_param *data, uint8_t rvp_id)
2049{
2050	struct ieee80211com *ic = &sc->sc_ic;
2051	uint8_t slottime;
2052
2053	slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
2054
2055	rum_modbits(sc, RT2573_MAC_CSR9, slottime, 0xff);
2056
2057	DPRINTF("setting slot time to %uus\n", slottime);
2058}
2059
2060static void
2061rum_update_slot(struct ieee80211com *ic)
2062{
2063	rum_cmd_sleepable(ic->ic_softc, NULL, 0, 0, rum_update_slot_cb);
2064}
2065
2066static void
2067rum_set_bssid(struct rum_softc *sc, const uint8_t *bssid)
2068{
2069
2070	rum_write(sc, RT2573_MAC_CSR4,
2071	    bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24);
2072	rum_write(sc, RT2573_MAC_CSR5,
2073	    bssid[4] | bssid[5] << 8 | RT2573_NUM_BSSID_MSK(1));
2074}
2075
2076static void
2077rum_set_macaddr(struct rum_softc *sc, const uint8_t *addr)
2078{
2079
2080	rum_write(sc, RT2573_MAC_CSR2,
2081	    addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
2082	rum_write(sc, RT2573_MAC_CSR3,
2083	    addr[4] | addr[5] << 8 | 0xff << 16);
2084}
2085
2086static void
2087rum_setpromisc(struct rum_softc *sc)
2088{
2089	struct ieee80211com *ic = &sc->sc_ic;
2090
2091	if (ic->ic_promisc == 0)
2092		rum_setbits(sc, RT2573_TXRX_CSR0, RT2573_DROP_NOT_TO_ME);
2093	else
2094		rum_clrbits(sc, RT2573_TXRX_CSR0, RT2573_DROP_NOT_TO_ME);
2095
2096	DPRINTF("%s promiscuous mode\n", ic->ic_promisc > 0 ?
2097	    "entering" : "leaving");
2098}
2099
2100static void
2101rum_update_promisc(struct ieee80211com *ic)
2102{
2103	struct rum_softc *sc = ic->ic_softc;
2104
2105	RUM_LOCK(sc);
2106	if (sc->sc_running)
2107		rum_setpromisc(sc);
2108	RUM_UNLOCK(sc);
2109}
2110
2111static void
2112rum_update_mcast(struct ieee80211com *ic)
2113{
2114	/* Ignore. */
2115}
2116
2117static const char *
2118rum_get_rf(int rev)
2119{
2120	switch (rev) {
2121	case RT2573_RF_2527:	return "RT2527 (MIMO XR)";
2122	case RT2573_RF_2528:	return "RT2528";
2123	case RT2573_RF_5225:	return "RT5225 (MIMO XR)";
2124	case RT2573_RF_5226:	return "RT5226";
2125	default:		return "unknown";
2126	}
2127}
2128
2129static void
2130rum_read_eeprom(struct rum_softc *sc)
2131{
2132	uint16_t val;
2133#ifdef RUM_DEBUG
2134	int i;
2135#endif
2136
2137	/* read MAC address */
2138	rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, sc->sc_ic.ic_macaddr, 6);
2139
2140	rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2);
2141	val = le16toh(val);
2142	sc->rf_rev =   (val >> 11) & 0x1f;
2143	sc->hw_radio = (val >> 10) & 0x1;
2144	sc->rx_ant =   (val >> 4)  & 0x3;
2145	sc->tx_ant =   (val >> 2)  & 0x3;
2146	sc->nb_ant =   val & 0x3;
2147
2148	DPRINTF("RF revision=%d\n", sc->rf_rev);
2149
2150	rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2);
2151	val = le16toh(val);
2152	sc->ext_5ghz_lna = (val >> 6) & 0x1;
2153	sc->ext_2ghz_lna = (val >> 4) & 0x1;
2154
2155	DPRINTF("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n",
2156	    sc->ext_2ghz_lna, sc->ext_5ghz_lna);
2157
2158	rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2);
2159	val = le16toh(val);
2160	if ((val & 0xff) != 0xff)
2161		sc->rssi_2ghz_corr = (int8_t)(val & 0xff);	/* signed */
2162
2163	/* Only [-10, 10] is valid */
2164	if (sc->rssi_2ghz_corr < -10 || sc->rssi_2ghz_corr > 10)
2165		sc->rssi_2ghz_corr = 0;
2166
2167	rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2);
2168	val = le16toh(val);
2169	if ((val & 0xff) != 0xff)
2170		sc->rssi_5ghz_corr = (int8_t)(val & 0xff);	/* signed */
2171
2172	/* Only [-10, 10] is valid */
2173	if (sc->rssi_5ghz_corr < -10 || sc->rssi_5ghz_corr > 10)
2174		sc->rssi_5ghz_corr = 0;
2175
2176	if (sc->ext_2ghz_lna)
2177		sc->rssi_2ghz_corr -= 14;
2178	if (sc->ext_5ghz_lna)
2179		sc->rssi_5ghz_corr -= 14;
2180
2181	DPRINTF("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n",
2182	    sc->rssi_2ghz_corr, sc->rssi_5ghz_corr);
2183
2184	rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2);
2185	val = le16toh(val);
2186	if ((val & 0xff) != 0xff)
2187		sc->rffreq = val & 0xff;
2188
2189	DPRINTF("RF freq=%d\n", sc->rffreq);
2190
2191	/* read Tx power for all a/b/g channels */
2192	rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14);
2193	/* XXX default Tx power for 802.11a channels */
2194	memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14);
2195#ifdef RUM_DEBUG
2196	for (i = 0; i < 14; i++)
2197		DPRINTF("Channel=%d Tx power=%d\n", i + 1,  sc->txpow[i]);
2198#endif
2199
2200	/* read default values for BBP registers */
2201	rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
2202#ifdef RUM_DEBUG
2203	for (i = 0; i < 14; i++) {
2204		if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
2205			continue;
2206		DPRINTF("BBP R%d=%02x\n", sc->bbp_prom[i].reg,
2207		    sc->bbp_prom[i].val);
2208	}
2209#endif
2210}
2211
2212static int
2213rum_bbp_wakeup(struct rum_softc *sc)
2214{
2215	unsigned int ntries;
2216
2217	for (ntries = 0; ntries < 100; ntries++) {
2218		if (rum_read(sc, RT2573_MAC_CSR12) & 8)
2219			break;
2220		rum_write(sc, RT2573_MAC_CSR12, 4);	/* force wakeup */
2221		if (rum_pause(sc, hz / 100))
2222			break;
2223	}
2224	if (ntries == 100) {
2225		device_printf(sc->sc_dev,
2226		    "timeout waiting for BBP/RF to wakeup\n");
2227		return (ETIMEDOUT);
2228	}
2229
2230	return (0);
2231}
2232
2233static int
2234rum_bbp_init(struct rum_softc *sc)
2235{
2236	int i, ntries;
2237
2238	/* wait for BBP to be ready */
2239	for (ntries = 0; ntries < 100; ntries++) {
2240		const uint8_t val = rum_bbp_read(sc, 0);
2241		if (val != 0 && val != 0xff)
2242			break;
2243		if (rum_pause(sc, hz / 100))
2244			break;
2245	}
2246	if (ntries == 100) {
2247		device_printf(sc->sc_dev, "timeout waiting for BBP\n");
2248		return EIO;
2249	}
2250
2251	/* initialize BBP registers to default values */
2252	for (i = 0; i < nitems(rum_def_bbp); i++)
2253		rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val);
2254
2255	/* write vendor-specific BBP values (from EEPROM) */
2256	for (i = 0; i < 16; i++) {
2257		if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
2258			continue;
2259		rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
2260	}
2261
2262	return 0;
2263}
2264
2265static void
2266rum_clr_shkey_regs(struct rum_softc *sc)
2267{
2268	rum_write(sc, RT2573_SEC_CSR0, 0);
2269	rum_write(sc, RT2573_SEC_CSR1, 0);
2270	rum_write(sc, RT2573_SEC_CSR5, 0);
2271}
2272
2273static int
2274rum_init(struct rum_softc *sc)
2275{
2276	struct ieee80211com *ic = &sc->sc_ic;
2277	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2278	uint32_t tmp;
2279	int i, ret;
2280
2281	RUM_LOCK(sc);
2282	if (sc->sc_running) {
2283		ret = 0;
2284		goto end;
2285	}
2286
2287	/* initialize MAC registers to default values */
2288	for (i = 0; i < nitems(rum_def_mac); i++)
2289		rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val);
2290
2291	/* set host ready */
2292	rum_write(sc, RT2573_MAC_CSR1, RT2573_RESET_ASIC | RT2573_RESET_BBP);
2293	rum_write(sc, RT2573_MAC_CSR1, 0);
2294
2295	/* wait for BBP/RF to wakeup */
2296	if ((ret = rum_bbp_wakeup(sc)) != 0)
2297		goto end;
2298
2299	if ((ret = rum_bbp_init(sc)) != 0)
2300		goto end;
2301
2302	/* select default channel */
2303	rum_select_band(sc, ic->ic_curchan);
2304	rum_select_antenna(sc);
2305	rum_set_chan(sc, ic->ic_curchan);
2306
2307	/* clear STA registers */
2308	rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
2309
2310	/* clear security registers (if required) */
2311	if (sc->sc_clr_shkeys == 0) {
2312		rum_clr_shkey_regs(sc);
2313		sc->sc_clr_shkeys = 1;
2314	}
2315
2316	rum_set_macaddr(sc, vap ? vap->iv_myaddr : ic->ic_macaddr);
2317
2318	/* initialize ASIC */
2319	rum_write(sc, RT2573_MAC_CSR1, RT2573_HOST_READY);
2320
2321	/*
2322	 * Allocate Tx and Rx xfer queues.
2323	 */
2324	rum_setup_tx_list(sc);
2325
2326	/* update Rx filter */
2327	tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff;
2328
2329	tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR;
2330	if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2331		tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR |
2332		       RT2573_DROP_ACKCTS;
2333		if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2334			tmp |= RT2573_DROP_TODS;
2335		if (ic->ic_promisc == 0)
2336			tmp |= RT2573_DROP_NOT_TO_ME;
2337	}
2338	rum_write(sc, RT2573_TXRX_CSR0, tmp);
2339
2340	sc->sc_running = 1;
2341	usbd_xfer_set_stall(sc->sc_xfer[RUM_BULK_WR]);
2342	usbd_transfer_start(sc->sc_xfer[RUM_BULK_RD]);
2343
2344end:	RUM_UNLOCK(sc);
2345
2346	if (ret != 0)
2347		rum_stop(sc);
2348
2349	return ret;
2350}
2351
2352static void
2353rum_stop(struct rum_softc *sc)
2354{
2355
2356	RUM_LOCK(sc);
2357	if (!sc->sc_running) {
2358		RUM_UNLOCK(sc);
2359		return;
2360	}
2361	sc->sc_running = 0;
2362	RUM_UNLOCK(sc);
2363
2364	/*
2365	 * Drain the USB transfers, if not already drained:
2366	 */
2367	usbd_transfer_drain(sc->sc_xfer[RUM_BULK_WR]);
2368	usbd_transfer_drain(sc->sc_xfer[RUM_BULK_RD]);
2369
2370	RUM_LOCK(sc);
2371	rum_unsetup_tx_list(sc);
2372
2373	/* disable Rx */
2374	rum_setbits(sc, RT2573_TXRX_CSR0, RT2573_DISABLE_RX);
2375
2376	/* reset ASIC */
2377	rum_write(sc, RT2573_MAC_CSR1, RT2573_RESET_ASIC | RT2573_RESET_BBP);
2378	rum_write(sc, RT2573_MAC_CSR1, 0);
2379	RUM_UNLOCK(sc);
2380}
2381
2382static void
2383rum_load_microcode(struct rum_softc *sc, const uint8_t *ucode, size_t size)
2384{
2385	struct usb_device_request req;
2386	uint16_t reg = RT2573_MCU_CODE_BASE;
2387	usb_error_t err;
2388
2389	/* copy firmware image into NIC */
2390	for (; size >= 4; reg += 4, ucode += 4, size -= 4) {
2391		err = rum_write(sc, reg, UGETDW(ucode));
2392		if (err) {
2393			/* firmware already loaded ? */
2394			device_printf(sc->sc_dev, "Firmware load "
2395			    "failure! (ignored)\n");
2396			break;
2397		}
2398	}
2399
2400	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2401	req.bRequest = RT2573_MCU_CNTL;
2402	USETW(req.wValue, RT2573_MCU_RUN);
2403	USETW(req.wIndex, 0);
2404	USETW(req.wLength, 0);
2405
2406	err = rum_do_request(sc, &req, NULL);
2407	if (err != 0) {
2408		device_printf(sc->sc_dev, "could not run firmware: %s\n",
2409		    usbd_errstr(err));
2410	}
2411
2412	/* give the chip some time to boot */
2413	rum_pause(sc, hz / 8);
2414}
2415
2416static int
2417rum_set_beacon(struct rum_softc *sc, struct ieee80211vap *vap)
2418{
2419	struct ieee80211com *ic = vap->iv_ic;
2420	struct rum_vap *rvp = RUM_VAP(vap);
2421	struct mbuf *m = rvp->bcn_mbuf;
2422	const struct ieee80211_txparam *tp;
2423	struct rum_tx_desc desc;
2424
2425	RUM_LOCK_ASSERT(sc);
2426
2427	if (m == NULL)
2428		return EINVAL;
2429	if (ic->ic_bsschan == IEEE80211_CHAN_ANYC)
2430		return EINVAL;
2431
2432	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)];
2433	rum_setup_tx_desc(sc, &desc, NULL, RT2573_TX_TIMESTAMP,
2434	    RT2573_TX_HWSEQ, 0, m->m_pkthdr.len, tp->mgmtrate);
2435
2436	/* copy the Tx descriptor into NIC memory */
2437	if (rum_write_multi(sc, RT2573_HW_BCN_BASE(0), (uint8_t *)&desc,
2438	    RT2573_TX_DESC_SIZE) != 0)
2439		return EIO;
2440
2441	/* copy beacon header and payload into NIC memory */
2442	if (rum_write_multi(sc, RT2573_HW_BCN_BASE(0) + RT2573_TX_DESC_SIZE,
2443	    mtod(m, uint8_t *), m->m_pkthdr.len) != 0)
2444		return EIO;
2445
2446	return 0;
2447}
2448
2449static int
2450rum_alloc_beacon(struct rum_softc *sc, struct ieee80211vap *vap)
2451{
2452	struct rum_vap *rvp = RUM_VAP(vap);
2453	struct ieee80211_node *ni = vap->iv_bss;
2454	struct mbuf *m;
2455
2456	if (ni->ni_chan == IEEE80211_CHAN_ANYC)
2457		return EINVAL;
2458
2459	m = ieee80211_beacon_alloc(ni);
2460	if (m == NULL)
2461		return ENOMEM;
2462
2463	if (rvp->bcn_mbuf != NULL)
2464		m_freem(rvp->bcn_mbuf);
2465
2466	rvp->bcn_mbuf = m;
2467
2468	return (rum_set_beacon(sc, vap));
2469}
2470
2471static void
2472rum_update_beacon_cb(struct rum_softc *sc, union sec_param *data,
2473    uint8_t rvp_id)
2474{
2475	struct ieee80211vap *vap = data->vap;
2476
2477	rum_set_beacon(sc, vap);
2478}
2479
2480static void
2481rum_update_beacon(struct ieee80211vap *vap, int item)
2482{
2483	struct ieee80211com *ic = vap->iv_ic;
2484	struct rum_softc *sc = ic->ic_softc;
2485	struct rum_vap *rvp = RUM_VAP(vap);
2486	struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
2487	struct ieee80211_node *ni = vap->iv_bss;
2488	struct mbuf *m = rvp->bcn_mbuf;
2489	int mcast = 0;
2490
2491	RUM_LOCK(sc);
2492	if (m == NULL) {
2493		m = ieee80211_beacon_alloc(ni);
2494		if (m == NULL) {
2495			device_printf(sc->sc_dev,
2496			    "%s: could not allocate beacon frame\n", __func__);
2497			RUM_UNLOCK(sc);
2498			return;
2499		}
2500		rvp->bcn_mbuf = m;
2501	}
2502
2503	switch (item) {
2504	case IEEE80211_BEACON_ERP:
2505		rum_update_slot(ic);
2506		break;
2507	case IEEE80211_BEACON_TIM:
2508		mcast = 1;	/*TODO*/
2509		break;
2510	default:
2511		break;
2512	}
2513	RUM_UNLOCK(sc);
2514
2515	setbit(bo->bo_flags, item);
2516	ieee80211_beacon_update(ni, m, mcast);
2517
2518	rum_cmd_sleepable(sc, &vap, sizeof(vap), 0, rum_update_beacon_cb);
2519}
2520
2521static int
2522rum_common_key_set(struct rum_softc *sc, struct ieee80211_key *k,
2523    uint16_t base)
2524{
2525
2526	if (rum_write_multi(sc, base, k->wk_key, k->wk_keylen))
2527		return EIO;
2528
2529	if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP) {
2530		if (rum_write_multi(sc, base + IEEE80211_KEYBUF_SIZE,
2531		    k->wk_txmic, 8))
2532			return EIO;
2533		if (rum_write_multi(sc, base + IEEE80211_KEYBUF_SIZE + 8,
2534		    k->wk_rxmic, 8))
2535			return EIO;
2536	}
2537
2538	return 0;
2539}
2540
2541static void
2542rum_group_key_set_cb(struct rum_softc *sc, union sec_param *data,
2543    uint8_t rvp_id)
2544{
2545	struct ieee80211_key *k = &data->key;
2546	uint8_t mode;
2547
2548	if (sc->sc_clr_shkeys == 0) {
2549		rum_clr_shkey_regs(sc);
2550		sc->sc_clr_shkeys = 1;
2551	}
2552
2553	mode = rum_crypto_mode(sc, k->wk_cipher->ic_cipher, k->wk_keylen);
2554	if (mode == 0)
2555		goto print_err;
2556
2557	DPRINTFN(1, "setting group key %d for vap %d, mode %d "
2558	    "(tx %s, rx %s)\n", k->wk_keyix, rvp_id, mode,
2559	    (k->wk_flags & IEEE80211_KEY_XMIT) ? "on" : "off",
2560	    (k->wk_flags & IEEE80211_KEY_RECV) ? "on" : "off");
2561
2562	/* Install the key. */
2563	if (rum_common_key_set(sc, k, RT2573_SKEY(rvp_id, k->wk_keyix)) != 0)
2564		goto print_err;
2565
2566	/* Set cipher mode. */
2567	if (rum_modbits(sc, rvp_id < 2 ? RT2573_SEC_CSR1 : RT2573_SEC_CSR5,
2568	      mode << (rvp_id % 2 + k->wk_keyix) * RT2573_SKEY_MAX,
2569	      RT2573_MODE_MASK << (rvp_id % 2 + k->wk_keyix) * RT2573_SKEY_MAX)
2570	    != 0)
2571		goto print_err;
2572
2573	/* Mark this key as valid. */
2574	if (rum_setbits(sc, RT2573_SEC_CSR0,
2575	      1 << (rvp_id * RT2573_SKEY_MAX + k->wk_keyix)) != 0)
2576		goto print_err;
2577
2578	return;
2579
2580print_err:
2581	device_printf(sc->sc_dev, "%s: cannot set group key %d for vap %d\n",
2582	    __func__, k->wk_keyix, rvp_id);
2583}
2584
2585static void
2586rum_group_key_del_cb(struct rum_softc *sc, union sec_param *data,
2587    uint8_t rvp_id)
2588{
2589	struct ieee80211_key *k = &data->key;
2590
2591	DPRINTF("%s: removing group key %d for vap %d\n", __func__,
2592	    k->wk_keyix, rvp_id);
2593	rum_clrbits(sc,
2594	    rvp_id < 2 ? RT2573_SEC_CSR1 : RT2573_SEC_CSR5,
2595	    RT2573_MODE_MASK << (rvp_id % 2 + k->wk_keyix) * RT2573_SKEY_MAX);
2596	rum_clrbits(sc, RT2573_SEC_CSR0,
2597	    rvp_id * RT2573_SKEY_MAX + k->wk_keyix);
2598}
2599
2600static void
2601rum_pair_key_set_cb(struct rum_softc *sc, union sec_param *data,
2602    uint8_t rvp_id)
2603{
2604	struct ieee80211_key *k = &data->key;
2605	uint8_t buf[IEEE80211_ADDR_LEN + 1];
2606	uint8_t mode;
2607
2608	mode = rum_crypto_mode(sc, k->wk_cipher->ic_cipher, k->wk_keylen);
2609	if (mode == 0)
2610		goto print_err;
2611
2612	DPRINTFN(1, "setting pairwise key %d for vap %d, mode %d "
2613	    "(tx %s, rx %s)\n", k->wk_keyix, rvp_id, mode,
2614	    (k->wk_flags & IEEE80211_KEY_XMIT) ? "on" : "off",
2615	    (k->wk_flags & IEEE80211_KEY_RECV) ? "on" : "off");
2616
2617	/* Install the key. */
2618	if (rum_common_key_set(sc, k, RT2573_PKEY(k->wk_keyix)) != 0)
2619		goto print_err;
2620
2621	IEEE80211_ADDR_COPY(buf, k->wk_macaddr);
2622	buf[IEEE80211_ADDR_LEN] = mode;
2623
2624	/* Set transmitter address and cipher mode. */
2625	if (rum_write_multi(sc, RT2573_ADDR_ENTRY(k->wk_keyix),
2626	      buf, sizeof buf) != 0)
2627		goto print_err;
2628
2629	/* Enable key table lookup for this vap. */
2630	if (sc->vap_key_count[rvp_id]++ == 0)
2631		if (rum_setbits(sc, RT2573_SEC_CSR4, 1 << rvp_id) != 0)
2632			goto print_err;
2633
2634	/* Mark this key as valid. */
2635	if (rum_setbits(sc,
2636	      k->wk_keyix < 32 ? RT2573_SEC_CSR2 : RT2573_SEC_CSR3,
2637	      1 << (k->wk_keyix % 32)) != 0)
2638		goto print_err;
2639
2640	return;
2641
2642print_err:
2643	device_printf(sc->sc_dev,
2644	    "%s: cannot set pairwise key %d, vap %d\n", __func__, k->wk_keyix,
2645	    rvp_id);
2646}
2647
2648static void
2649rum_pair_key_del_cb(struct rum_softc *sc, union sec_param *data,
2650    uint8_t rvp_id)
2651{
2652	struct ieee80211_key *k = &data->key;
2653
2654	DPRINTF("%s: removing key %d\n", __func__, k->wk_keyix);
2655	rum_clrbits(sc, (k->wk_keyix < 32) ? RT2573_SEC_CSR2 : RT2573_SEC_CSR3,
2656	    1 << (k->wk_keyix % 32));
2657	sc->keys_bmap &= ~(1 << k->wk_keyix);
2658	if (--sc->vap_key_count[rvp_id] == 0)
2659		rum_clrbits(sc, RT2573_SEC_CSR4, 1 << rvp_id);
2660}
2661
2662static int
2663rum_key_alloc(struct ieee80211vap *vap, struct ieee80211_key *k,
2664    ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
2665{
2666	struct rum_softc *sc = vap->iv_ic->ic_softc;
2667	uint8_t i;
2668
2669	if (!(&vap->iv_nw_keys[0] <= k &&
2670	     k < &vap->iv_nw_keys[IEEE80211_WEP_NKID])) {
2671		if (!(k->wk_flags & IEEE80211_KEY_SWCRYPT)) {
2672			RUM_LOCK(sc);
2673			for (i = 0; i < RT2573_ADDR_MAX; i++) {
2674				if ((sc->keys_bmap & (1 << i)) == 0) {
2675					sc->keys_bmap |= 1 << i;
2676					*keyix = i;
2677					break;
2678				}
2679			}
2680			RUM_UNLOCK(sc);
2681			if (i == RT2573_ADDR_MAX) {
2682				device_printf(sc->sc_dev,
2683				    "%s: no free space in the key table\n",
2684				    __func__);
2685				return 0;
2686			}
2687		} else
2688			*keyix = 0;
2689	} else {
2690		*keyix = k - vap->iv_nw_keys;
2691	}
2692	*rxkeyix = *keyix;
2693	return 1;
2694}
2695
2696static int
2697rum_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
2698{
2699	struct rum_softc *sc = vap->iv_ic->ic_softc;
2700	int group;
2701
2702	if (k->wk_flags & IEEE80211_KEY_SWCRYPT) {
2703		/* Not for us. */
2704		return 1;
2705	}
2706
2707	group = k >= &vap->iv_nw_keys[0] && k < &vap->iv_nw_keys[IEEE80211_WEP_NKID];
2708
2709	return !rum_cmd_sleepable(sc, k, sizeof(*k), 0,
2710		   group ? rum_group_key_set_cb : rum_pair_key_set_cb);
2711}
2712
2713static int
2714rum_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
2715{
2716	struct rum_softc *sc = vap->iv_ic->ic_softc;
2717	int group;
2718
2719	if (k->wk_flags & IEEE80211_KEY_SWCRYPT) {
2720		/* Not for us. */
2721		return 1;
2722	}
2723
2724	group = k >= &vap->iv_nw_keys[0] && k < &vap->iv_nw_keys[IEEE80211_WEP_NKID];
2725
2726	return !rum_cmd_sleepable(sc, k, sizeof(*k), 0,
2727		   group ? rum_group_key_del_cb : rum_pair_key_del_cb);
2728}
2729
2730static int
2731rum_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2732    const struct ieee80211_bpf_params *params)
2733{
2734	struct rum_softc *sc = ni->ni_ic->ic_softc;
2735	int ret;
2736
2737	RUM_LOCK(sc);
2738	/* prevent management frames from being sent if we're not ready */
2739	if (!sc->sc_running) {
2740		ret = ENETDOWN;
2741		goto bad;
2742	}
2743	if (sc->tx_nfree < RUM_TX_MINFREE) {
2744		ret = EIO;
2745		goto bad;
2746	}
2747
2748	if (params == NULL) {
2749		/*
2750		 * Legacy path; interpret frame contents to decide
2751		 * precisely how to send the frame.
2752		 */
2753		if ((ret = rum_tx_mgt(sc, m, ni)) != 0)
2754			goto bad;
2755	} else {
2756		/*
2757		 * Caller supplied explicit parameters to use in
2758		 * sending the frame.
2759		 */
2760		if ((ret = rum_tx_raw(sc, m, ni, params)) != 0)
2761			goto bad;
2762	}
2763	RUM_UNLOCK(sc);
2764
2765	return 0;
2766bad:
2767	RUM_UNLOCK(sc);
2768	m_freem(m);
2769	ieee80211_free_node(ni);
2770	return ret;
2771}
2772
2773static void
2774rum_ratectl_start(struct rum_softc *sc, struct ieee80211_node *ni)
2775{
2776	struct ieee80211vap *vap = ni->ni_vap;
2777	struct rum_vap *rvp = RUM_VAP(vap);
2778
2779	/* clear statistic registers (STA_CSR0 to STA_CSR5) */
2780	rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
2781
2782	usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp);
2783}
2784
2785static void
2786rum_ratectl_timeout(void *arg)
2787{
2788	struct rum_vap *rvp = arg;
2789	struct ieee80211vap *vap = &rvp->vap;
2790	struct ieee80211com *ic = vap->iv_ic;
2791
2792	ieee80211_runtask(ic, &rvp->ratectl_task);
2793}
2794
2795static void
2796rum_ratectl_task(void *arg, int pending)
2797{
2798	struct rum_vap *rvp = arg;
2799	struct ieee80211vap *vap = &rvp->vap;
2800	struct rum_softc *sc = vap->iv_ic->ic_softc;
2801	struct ieee80211_node *ni;
2802	int ok[3], fail;
2803	int sum, success, retrycnt;
2804
2805	RUM_LOCK(sc);
2806	/* read and clear statistic registers (STA_CSR0 to STA_CSR5) */
2807	rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof(sc->sta));
2808
2809	ok[0] = (le32toh(sc->sta[4]) & 0xffff);	/* TX ok w/o retry */
2810	ok[1] = (le32toh(sc->sta[4]) >> 16);	/* TX ok w/ one retry */
2811	ok[2] = (le32toh(sc->sta[5]) & 0xffff);	/* TX ok w/ multiple retries */
2812	fail =  (le32toh(sc->sta[5]) >> 16);	/* TX retry-fail count */
2813
2814	success = ok[0] + ok[1] + ok[2];
2815	sum = success + fail;
2816	/* XXX at least */
2817	retrycnt = ok[1] + ok[2] * 2 + fail * (rvp->maxretry + 1);
2818
2819	if (sum != 0) {
2820		ni = ieee80211_ref_node(vap->iv_bss);
2821		ieee80211_ratectl_tx_update(vap, ni, &sum, &ok, &retrycnt);
2822		(void) ieee80211_ratectl_rate(ni, NULL, 0);
2823		ieee80211_free_node(ni);
2824	}
2825
2826	/* count TX retry-fail as Tx errors */
2827	if_inc_counter(vap->iv_ifp, IFCOUNTER_OERRORS, fail);
2828
2829	usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp);
2830	RUM_UNLOCK(sc);
2831}
2832
2833static void
2834rum_scan_start(struct ieee80211com *ic)
2835{
2836	struct rum_softc *sc = ic->ic_softc;
2837
2838	RUM_LOCK(sc);
2839	rum_abort_tsf_sync(sc);
2840	rum_set_bssid(sc, ieee80211broadcastaddr);
2841	RUM_UNLOCK(sc);
2842
2843}
2844
2845static void
2846rum_scan_end(struct ieee80211com *ic)
2847{
2848	struct rum_softc *sc = ic->ic_softc;
2849
2850	RUM_LOCK(sc);
2851	if (ic->ic_opmode != IEEE80211_M_AHDEMO)
2852		rum_enable_tsf_sync(sc);
2853	else
2854		rum_enable_tsf(sc);
2855	rum_set_bssid(sc, sc->sc_bssid);
2856	RUM_UNLOCK(sc);
2857
2858}
2859
2860static void
2861rum_set_channel(struct ieee80211com *ic)
2862{
2863	struct rum_softc *sc = ic->ic_softc;
2864
2865	RUM_LOCK(sc);
2866	rum_set_chan(sc, ic->ic_curchan);
2867	RUM_UNLOCK(sc);
2868}
2869
2870static int
2871rum_get_rssi(struct rum_softc *sc, uint8_t raw)
2872{
2873	struct ieee80211com *ic = &sc->sc_ic;
2874	int lna, agc, rssi;
2875
2876	lna = (raw >> 5) & 0x3;
2877	agc = raw & 0x1f;
2878
2879	if (lna == 0) {
2880		/*
2881		 * No RSSI mapping
2882		 *
2883		 * NB: Since RSSI is relative to noise floor, -1 is
2884		 *     adequate for caller to know error happened.
2885		 */
2886		return -1;
2887	}
2888
2889	rssi = (2 * agc) - RT2573_NOISE_FLOOR;
2890
2891	if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) {
2892		rssi += sc->rssi_2ghz_corr;
2893
2894		if (lna == 1)
2895			rssi -= 64;
2896		else if (lna == 2)
2897			rssi -= 74;
2898		else if (lna == 3)
2899			rssi -= 90;
2900	} else {
2901		rssi += sc->rssi_5ghz_corr;
2902
2903		if (!sc->ext_5ghz_lna && lna != 1)
2904			rssi += 4;
2905
2906		if (lna == 1)
2907			rssi -= 64;
2908		else if (lna == 2)
2909			rssi -= 86;
2910		else if (lna == 3)
2911			rssi -= 100;
2912	}
2913	return rssi;
2914}
2915
2916static int
2917rum_pause(struct rum_softc *sc, int timeout)
2918{
2919
2920	usb_pause_mtx(&sc->sc_mtx, timeout);
2921	return (0);
2922}
2923
2924static device_method_t rum_methods[] = {
2925	/* Device interface */
2926	DEVMETHOD(device_probe,		rum_match),
2927	DEVMETHOD(device_attach,	rum_attach),
2928	DEVMETHOD(device_detach,	rum_detach),
2929	DEVMETHOD_END
2930};
2931
2932static driver_t rum_driver = {
2933	.name = "rum",
2934	.methods = rum_methods,
2935	.size = sizeof(struct rum_softc),
2936};
2937
2938static devclass_t rum_devclass;
2939
2940DRIVER_MODULE(rum, uhub, rum_driver, rum_devclass, NULL, 0);
2941MODULE_DEPEND(rum, wlan, 1, 1, 1);
2942MODULE_DEPEND(rum, usb, 1, 1, 1);
2943MODULE_VERSION(rum, 1);
2944