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