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