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