1188417Sthompsa/*	$FreeBSD$	*/
2188417Sthompsa
3184610Salfred/*-
4184610Salfred * Copyright (c) 2005-2007 Damien Bergamini <damien.bergamini@free.fr>
5184610Salfred * Copyright (c) 2006 Niall O'Higgins <niallo@openbsd.org>
6189002Sed * Copyright (c) 2007-2008 Hans Petter Selasky <hselasky@FreeBSD.org>
7184610Salfred *
8184610Salfred * Permission to use, copy, modify, and distribute this software for any
9184610Salfred * purpose with or without fee is hereby granted, provided that the above
10184610Salfred * copyright notice and this permission notice appear in all copies.
11184610Salfred *
12184610Salfred * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13184610Salfred * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14184610Salfred * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15184610Salfred * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16184610Salfred * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17184610Salfred * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18184610Salfred * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19184610Salfred */
20184610Salfred
21184610Salfred#include <sys/cdefs.h>
22184610Salfred__FBSDID("$FreeBSD$");
23184610Salfred
24184610Salfred/*-
25184610Salfred * Ralink Technology RT2501USB/RT2601USB chipset driver
26184610Salfred * http://www.ralinktech.com.tw/
27184610Salfred */
28184610Salfred
29191746Sthompsa#include <sys/param.h>
30191746Sthompsa#include <sys/sockio.h>
31191746Sthompsa#include <sys/sysctl.h>
32191746Sthompsa#include <sys/lock.h>
33191746Sthompsa#include <sys/mutex.h>
34191746Sthompsa#include <sys/mbuf.h>
35191746Sthompsa#include <sys/kernel.h>
36191746Sthompsa#include <sys/socket.h>
37191746Sthompsa#include <sys/systm.h>
38191746Sthompsa#include <sys/malloc.h>
39191746Sthompsa#include <sys/module.h>
40191746Sthompsa#include <sys/bus.h>
41191746Sthompsa#include <sys/endian.h>
42191746Sthompsa#include <sys/kdb.h>
43184610Salfred
44191746Sthompsa#include <machine/bus.h>
45196970Sphk#include <machine/resource.h>
46196970Sphk#include <sys/rman.h>
47191746Sthompsa
48191746Sthompsa#include <net/bpf.h>
49191746Sthompsa#include <net/if.h>
50196970Sphk#include <net/if_arp.h>
51191746Sthompsa#include <net/ethernet.h>
52191746Sthompsa#include <net/if_dl.h>
53191746Sthompsa#include <net/if_media.h>
54191746Sthompsa#include <net/if_types.h>
55191746Sthompsa
56191746Sthompsa#ifdef INET
57191746Sthompsa#include <netinet/in.h>
58191746Sthompsa#include <netinet/in_systm.h>
59191746Sthompsa#include <netinet/in_var.h>
60191746Sthompsa#include <netinet/if_ether.h>
61191746Sthompsa#include <netinet/ip.h>
62191746Sthompsa#endif
63191746Sthompsa
64191746Sthompsa#include <net80211/ieee80211_var.h>
65191746Sthompsa#include <net80211/ieee80211_regdomain.h>
66191746Sthompsa#include <net80211/ieee80211_radiotap.h>
67206358Srpaulo#include <net80211/ieee80211_ratectl.h>
68191746Sthompsa
69191746Sthompsa#include <dev/usb/usb.h>
70194677Sthompsa#include <dev/usb/usbdi.h>
71191746Sthompsa#include "usbdevs.h"
72184610Salfred
73194677Sthompsa#define	USB_DEBUG_VAR rum_debug
74194677Sthompsa#include <dev/usb/usb_debug.h>
75194677Sthompsa
76188942Sthompsa#include <dev/usb/wlan/if_rumreg.h>
77188942Sthompsa#include <dev/usb/wlan/if_rumvar.h>
78188942Sthompsa#include <dev/usb/wlan/if_rumfw.h>
79184610Salfred
80207077Sthompsa#ifdef USB_DEBUG
81184610Salfredstatic int rum_debug = 0;
82184610Salfred
83248085Smariusstatic SYSCTL_NODE(_hw_usb, OID_AUTO, rum, CTLFLAG_RW, 0, "USB rum");
84192502SthompsaSYSCTL_INT(_hw_usb_rum, OID_AUTO, debug, CTLFLAG_RW, &rum_debug, 0,
85184610Salfred    "Debug level");
86184610Salfred#endif
87184610Salfred
88235000Shselasky#define N(a)	((int)(sizeof (a) / sizeof ((a)[0])))
89235000Shselasky
90223486Shselaskystatic const STRUCT_USB_HOST_ID rum_devs[] = {
91201028Sthompsa#define	RUM_DEV(v,p)  { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
92201028Sthompsa    RUM_DEV(ABOCOM, HWU54DM),
93201028Sthompsa    RUM_DEV(ABOCOM, RT2573_2),
94201028Sthompsa    RUM_DEV(ABOCOM, RT2573_3),
95201028Sthompsa    RUM_DEV(ABOCOM, RT2573_4),
96201028Sthompsa    RUM_DEV(ABOCOM, WUG2700),
97201028Sthompsa    RUM_DEV(AMIT, CGWLUSB2GO),
98201028Sthompsa    RUM_DEV(ASUS, RT2573_1),
99201028Sthompsa    RUM_DEV(ASUS, RT2573_2),
100201028Sthompsa    RUM_DEV(BELKIN, F5D7050A),
101201028Sthompsa    RUM_DEV(BELKIN, F5D9050V3),
102201028Sthompsa    RUM_DEV(CISCOLINKSYS, WUSB54GC),
103201028Sthompsa    RUM_DEV(CISCOLINKSYS, WUSB54GR),
104201028Sthompsa    RUM_DEV(CONCEPTRONIC2, C54RU2),
105201028Sthompsa    RUM_DEV(COREGA, CGWLUSB2GL),
106201028Sthompsa    RUM_DEV(COREGA, CGWLUSB2GPX),
107201028Sthompsa    RUM_DEV(DICKSMITH, CWD854F),
108201028Sthompsa    RUM_DEV(DICKSMITH, RT2573),
109203139Sthompsa    RUM_DEV(EDIMAX, EW7318USG),
110201028Sthompsa    RUM_DEV(DLINK2, DWLG122C1),
111201028Sthompsa    RUM_DEV(DLINK2, WUA1340),
112201028Sthompsa    RUM_DEV(DLINK2, DWA111),
113201028Sthompsa    RUM_DEV(DLINK2, DWA110),
114201028Sthompsa    RUM_DEV(GIGABYTE, GNWB01GS),
115201028Sthompsa    RUM_DEV(GIGABYTE, GNWI05GS),
116201028Sthompsa    RUM_DEV(GIGASET, RT2573),
117201028Sthompsa    RUM_DEV(GOODWAY, RT2573),
118201028Sthompsa    RUM_DEV(GUILLEMOT, HWGUSB254LB),
119201028Sthompsa    RUM_DEV(GUILLEMOT, HWGUSB254V2AP),
120201028Sthompsa    RUM_DEV(HUAWEI3COM, WUB320G),
121201028Sthompsa    RUM_DEV(MELCO, G54HP),
122201028Sthompsa    RUM_DEV(MELCO, SG54HP),
123250842Shselasky    RUM_DEV(MELCO, SG54HG),
124216057Ssanpei    RUM_DEV(MELCO, WLIUCG),
125213880Shselasky    RUM_DEV(MELCO, WLRUCG),
126213880Shselasky    RUM_DEV(MELCO, WLRUCGAOSS),
127201028Sthompsa    RUM_DEV(MSI, RT2573_1),
128201028Sthompsa    RUM_DEV(MSI, RT2573_2),
129201028Sthompsa    RUM_DEV(MSI, RT2573_3),
130201028Sthompsa    RUM_DEV(MSI, RT2573_4),
131201028Sthompsa    RUM_DEV(NOVATECH, RT2573),
132201028Sthompsa    RUM_DEV(PLANEX2, GWUS54HP),
133201028Sthompsa    RUM_DEV(PLANEX2, GWUS54MINI2),
134201028Sthompsa    RUM_DEV(PLANEX2, GWUSMM),
135201028Sthompsa    RUM_DEV(QCOM, RT2573),
136201028Sthompsa    RUM_DEV(QCOM, RT2573_2),
137201028Sthompsa    RUM_DEV(QCOM, RT2573_3),
138201028Sthompsa    RUM_DEV(RALINK, RT2573),
139201028Sthompsa    RUM_DEV(RALINK, RT2573_2),
140201028Sthompsa    RUM_DEV(RALINK, RT2671),
141201028Sthompsa    RUM_DEV(SITECOMEU, WL113R2),
142201028Sthompsa    RUM_DEV(SITECOMEU, WL172),
143201028Sthompsa    RUM_DEV(SPARKLAN, RT2573),
144201028Sthompsa    RUM_DEV(SURECOM, RT2573),
145201028Sthompsa#undef RUM_DEV
146188417Sthompsa};
147188417Sthompsa
148188417Sthompsastatic device_probe_t rum_match;
149184610Salfredstatic device_attach_t rum_attach;
150184610Salfredstatic device_detach_t rum_detach;
151184610Salfred
152193045Sthompsastatic usb_callback_t rum_bulk_read_callback;
153193045Sthompsastatic usb_callback_t rum_bulk_write_callback;
154184610Salfred
155193045Sthompsastatic usb_error_t	rum_do_request(struct rum_softc *sc,
156192984Sthompsa			    struct usb_device_request *req, void *data);
157185948Sthompsastatic struct ieee80211vap *rum_vap_create(struct ieee80211com *,
158234753Sdim			    const char [IFNAMSIZ], int, enum ieee80211_opmode,
159234753Sdim			    int, const uint8_t [IEEE80211_ADDR_LEN],
160234753Sdim			    const uint8_t [IEEE80211_ADDR_LEN]);
161188417Sthompsastatic void		rum_vap_delete(struct ieee80211vap *);
162188417Sthompsastatic void		rum_tx_free(struct rum_tx_data *, int);
163188419Sthompsastatic void		rum_setup_tx_list(struct rum_softc *);
164188419Sthompsastatic void		rum_unsetup_tx_list(struct rum_softc *);
165188417Sthompsastatic int		rum_newstate(struct ieee80211vap *,
166188417Sthompsa			    enum ieee80211_state, int);
167188417Sthompsastatic void		rum_setup_tx_desc(struct rum_softc *,
168188417Sthompsa			    struct rum_tx_desc *, uint32_t, uint16_t, int,
169188417Sthompsa			    int);
170188417Sthompsastatic int		rum_tx_mgt(struct rum_softc *, struct mbuf *,
171188417Sthompsa			    struct ieee80211_node *);
172188417Sthompsastatic int		rum_tx_raw(struct rum_softc *, struct mbuf *,
173188417Sthompsa			    struct ieee80211_node *,
174188417Sthompsa			    const struct ieee80211_bpf_params *);
175188417Sthompsastatic int		rum_tx_data(struct rum_softc *, struct mbuf *,
176188417Sthompsa			    struct ieee80211_node *);
177188417Sthompsastatic void		rum_start(struct ifnet *);
178188417Sthompsastatic int		rum_ioctl(struct ifnet *, u_long, caddr_t);
179188417Sthompsastatic void		rum_eeprom_read(struct rum_softc *, uint16_t, void *,
180188417Sthompsa			    int);
181188417Sthompsastatic uint32_t		rum_read(struct rum_softc *, uint16_t);
182188417Sthompsastatic void		rum_read_multi(struct rum_softc *, uint16_t, void *,
183188417Sthompsa			    int);
184193045Sthompsastatic usb_error_t	rum_write(struct rum_softc *, uint16_t, uint32_t);
185193045Sthompsastatic usb_error_t	rum_write_multi(struct rum_softc *, uint16_t, void *,
186188417Sthompsa			    size_t);
187188417Sthompsastatic void		rum_bbp_write(struct rum_softc *, uint8_t, uint8_t);
188188417Sthompsastatic uint8_t		rum_bbp_read(struct rum_softc *, uint8_t);
189188417Sthompsastatic void		rum_rf_write(struct rum_softc *, uint8_t, uint32_t);
190188417Sthompsastatic void		rum_select_antenna(struct rum_softc *);
191188417Sthompsastatic void		rum_enable_mrr(struct rum_softc *);
192188417Sthompsastatic void		rum_set_txpreamble(struct rum_softc *);
193188417Sthompsastatic void		rum_set_basicrates(struct rum_softc *);
194188417Sthompsastatic void		rum_select_band(struct rum_softc *,
195188417Sthompsa			    struct ieee80211_channel *);
196188417Sthompsastatic void		rum_set_chan(struct rum_softc *,
197188417Sthompsa			    struct ieee80211_channel *);
198188417Sthompsastatic void		rum_enable_tsf_sync(struct rum_softc *);
199192468Ssamstatic void		rum_enable_tsf(struct rum_softc *);
200188417Sthompsastatic void		rum_update_slot(struct ifnet *);
201188417Sthompsastatic void		rum_set_bssid(struct rum_softc *, const uint8_t *);
202188417Sthompsastatic void		rum_set_macaddr(struct rum_softc *, const uint8_t *);
203207768Semastestatic void		rum_update_mcast(struct ifnet *);
204189123Sthompsastatic void		rum_update_promisc(struct ifnet *);
205191746Sthompsastatic void		rum_setpromisc(struct rum_softc *);
206188417Sthompsastatic const char	*rum_get_rf(int);
207188417Sthompsastatic void		rum_read_eeprom(struct rum_softc *);
208188417Sthompsastatic int		rum_bbp_init(struct rum_softc *);
209191746Sthompsastatic void		rum_init_locked(struct rum_softc *);
210188417Sthompsastatic void		rum_init(void *);
211191746Sthompsastatic void		rum_stop(struct rum_softc *);
212189123Sthompsastatic void		rum_load_microcode(struct rum_softc *, const uint8_t *,
213188417Sthompsa			    size_t);
214221199Skevlostatic void		rum_prepare_beacon(struct rum_softc *,
215188417Sthompsa			    struct ieee80211vap *);
216188417Sthompsastatic int		rum_raw_xmit(struct ieee80211_node *, struct mbuf *,
217188417Sthompsa			    const struct ieee80211_bpf_params *);
218188417Sthompsastatic void		rum_scan_start(struct ieee80211com *);
219188417Sthompsastatic void		rum_scan_end(struct ieee80211com *);
220188417Sthompsastatic void		rum_set_channel(struct ieee80211com *);
221188417Sthompsastatic int		rum_get_rssi(struct rum_softc *, uint8_t);
222206358Srpaulostatic void		rum_ratectl_start(struct rum_softc *,
223188417Sthompsa			    struct ieee80211_node *);
224206358Srpaulostatic void		rum_ratectl_timeout(void *);
225206358Srpaulostatic void		rum_ratectl_task(void *, int);
226188619Sthompsastatic int		rum_pause(struct rum_softc *, int);
227184610Salfred
228188417Sthompsastatic const struct {
229188417Sthompsa	uint32_t	reg;
230188417Sthompsa	uint32_t	val;
231188417Sthompsa} rum_def_mac[] = {
232188417Sthompsa	{ RT2573_TXRX_CSR0,  0x025fb032 },
233188417Sthompsa	{ RT2573_TXRX_CSR1,  0x9eaa9eaf },
234188417Sthompsa	{ RT2573_TXRX_CSR2,  0x8a8b8c8d },
235188417Sthompsa	{ RT2573_TXRX_CSR3,  0x00858687 },
236188417Sthompsa	{ RT2573_TXRX_CSR7,  0x2e31353b },
237188417Sthompsa	{ RT2573_TXRX_CSR8,  0x2a2a2a2c },
238188417Sthompsa	{ RT2573_TXRX_CSR15, 0x0000000f },
239188417Sthompsa	{ RT2573_MAC_CSR6,   0x00000fff },
240188417Sthompsa	{ RT2573_MAC_CSR8,   0x016c030a },
241188417Sthompsa	{ RT2573_MAC_CSR10,  0x00000718 },
242188417Sthompsa	{ RT2573_MAC_CSR12,  0x00000004 },
243188417Sthompsa	{ RT2573_MAC_CSR13,  0x00007f00 },
244188417Sthompsa	{ RT2573_SEC_CSR0,   0x00000000 },
245188417Sthompsa	{ RT2573_SEC_CSR1,   0x00000000 },
246188417Sthompsa	{ RT2573_SEC_CSR5,   0x00000000 },
247188417Sthompsa	{ RT2573_PHY_CSR1,   0x000023b0 },
248188417Sthompsa	{ RT2573_PHY_CSR5,   0x00040a06 },
249188417Sthompsa	{ RT2573_PHY_CSR6,   0x00080606 },
250188417Sthompsa	{ RT2573_PHY_CSR7,   0x00000408 },
251188417Sthompsa	{ RT2573_AIFSN_CSR,  0x00002273 },
252188417Sthompsa	{ RT2573_CWMIN_CSR,  0x00002344 },
253188417Sthompsa	{ RT2573_CWMAX_CSR,  0x000034aa }
254184610Salfred};
255184610Salfred
256188417Sthompsastatic const struct {
257184610Salfred	uint8_t	reg;
258184610Salfred	uint8_t	val;
259188417Sthompsa} rum_def_bbp[] = {
260188417Sthompsa	{   3, 0x80 },
261188417Sthompsa	{  15, 0x30 },
262188417Sthompsa	{  17, 0x20 },
263188417Sthompsa	{  21, 0xc8 },
264188417Sthompsa	{  22, 0x38 },
265188417Sthompsa	{  23, 0x06 },
266188417Sthompsa	{  24, 0xfe },
267188417Sthompsa	{  25, 0x0a },
268188417Sthompsa	{  26, 0x0d },
269188417Sthompsa	{  32, 0x0b },
270188417Sthompsa	{  34, 0x12 },
271188417Sthompsa	{  37, 0x07 },
272188417Sthompsa	{  39, 0xf8 },
273188417Sthompsa	{  41, 0x60 },
274188417Sthompsa	{  53, 0x10 },
275188417Sthompsa	{  54, 0x18 },
276188417Sthompsa	{  60, 0x10 },
277188417Sthompsa	{  61, 0x04 },
278188417Sthompsa	{  62, 0x04 },
279188417Sthompsa	{  75, 0xfe },
280188417Sthompsa	{  86, 0xfe },
281188417Sthompsa	{  88, 0xfe },
282188417Sthompsa	{  90, 0x0f },
283188417Sthompsa	{  99, 0x00 },
284188417Sthompsa	{ 102, 0x16 },
285188417Sthompsa	{ 107, 0x04 }
286184610Salfred};
287184610Salfred
288188417Sthompsastatic const struct rfprog {
289188417Sthompsa	uint8_t		chan;
290188417Sthompsa	uint32_t	r1, r2, r3, r4;
291188417Sthompsa}  rum_rf5226[] = {
292188417Sthompsa	{   1, 0x00b03, 0x001e1, 0x1a014, 0x30282 },
293188417Sthompsa	{   2, 0x00b03, 0x001e1, 0x1a014, 0x30287 },
294188417Sthompsa	{   3, 0x00b03, 0x001e2, 0x1a014, 0x30282 },
295188417Sthompsa	{   4, 0x00b03, 0x001e2, 0x1a014, 0x30287 },
296188417Sthompsa	{   5, 0x00b03, 0x001e3, 0x1a014, 0x30282 },
297188417Sthompsa	{   6, 0x00b03, 0x001e3, 0x1a014, 0x30287 },
298188417Sthompsa	{   7, 0x00b03, 0x001e4, 0x1a014, 0x30282 },
299188417Sthompsa	{   8, 0x00b03, 0x001e4, 0x1a014, 0x30287 },
300188417Sthompsa	{   9, 0x00b03, 0x001e5, 0x1a014, 0x30282 },
301188417Sthompsa	{  10, 0x00b03, 0x001e5, 0x1a014, 0x30287 },
302188417Sthompsa	{  11, 0x00b03, 0x001e6, 0x1a014, 0x30282 },
303188417Sthompsa	{  12, 0x00b03, 0x001e6, 0x1a014, 0x30287 },
304188417Sthompsa	{  13, 0x00b03, 0x001e7, 0x1a014, 0x30282 },
305188417Sthompsa	{  14, 0x00b03, 0x001e8, 0x1a014, 0x30284 },
306184610Salfred
307188417Sthompsa	{  34, 0x00b03, 0x20266, 0x36014, 0x30282 },
308188417Sthompsa	{  38, 0x00b03, 0x20267, 0x36014, 0x30284 },
309188417Sthompsa	{  42, 0x00b03, 0x20268, 0x36014, 0x30286 },
310188417Sthompsa	{  46, 0x00b03, 0x20269, 0x36014, 0x30288 },
311184610Salfred
312188417Sthompsa	{  36, 0x00b03, 0x00266, 0x26014, 0x30288 },
313188417Sthompsa	{  40, 0x00b03, 0x00268, 0x26014, 0x30280 },
314188417Sthompsa	{  44, 0x00b03, 0x00269, 0x26014, 0x30282 },
315188417Sthompsa	{  48, 0x00b03, 0x0026a, 0x26014, 0x30284 },
316188417Sthompsa	{  52, 0x00b03, 0x0026b, 0x26014, 0x30286 },
317188417Sthompsa	{  56, 0x00b03, 0x0026c, 0x26014, 0x30288 },
318188417Sthompsa	{  60, 0x00b03, 0x0026e, 0x26014, 0x30280 },
319188417Sthompsa	{  64, 0x00b03, 0x0026f, 0x26014, 0x30282 },
320184610Salfred
321188417Sthompsa	{ 100, 0x00b03, 0x0028a, 0x2e014, 0x30280 },
322188417Sthompsa	{ 104, 0x00b03, 0x0028b, 0x2e014, 0x30282 },
323188417Sthompsa	{ 108, 0x00b03, 0x0028c, 0x2e014, 0x30284 },
324188417Sthompsa	{ 112, 0x00b03, 0x0028d, 0x2e014, 0x30286 },
325188417Sthompsa	{ 116, 0x00b03, 0x0028e, 0x2e014, 0x30288 },
326188417Sthompsa	{ 120, 0x00b03, 0x002a0, 0x2e014, 0x30280 },
327188417Sthompsa	{ 124, 0x00b03, 0x002a1, 0x2e014, 0x30282 },
328188417Sthompsa	{ 128, 0x00b03, 0x002a2, 0x2e014, 0x30284 },
329188417Sthompsa	{ 132, 0x00b03, 0x002a3, 0x2e014, 0x30286 },
330188417Sthompsa	{ 136, 0x00b03, 0x002a4, 0x2e014, 0x30288 },
331188417Sthompsa	{ 140, 0x00b03, 0x002a6, 0x2e014, 0x30280 },
332184610Salfred
333188417Sthompsa	{ 149, 0x00b03, 0x002a8, 0x2e014, 0x30287 },
334188417Sthompsa	{ 153, 0x00b03, 0x002a9, 0x2e014, 0x30289 },
335188417Sthompsa	{ 157, 0x00b03, 0x002ab, 0x2e014, 0x30281 },
336188417Sthompsa	{ 161, 0x00b03, 0x002ac, 0x2e014, 0x30283 },
337188417Sthompsa	{ 165, 0x00b03, 0x002ad, 0x2e014, 0x30285 }
338188417Sthompsa}, rum_rf5225[] = {
339188417Sthompsa	{   1, 0x00b33, 0x011e1, 0x1a014, 0x30282 },
340188417Sthompsa	{   2, 0x00b33, 0x011e1, 0x1a014, 0x30287 },
341188417Sthompsa	{   3, 0x00b33, 0x011e2, 0x1a014, 0x30282 },
342188417Sthompsa	{   4, 0x00b33, 0x011e2, 0x1a014, 0x30287 },
343188417Sthompsa	{   5, 0x00b33, 0x011e3, 0x1a014, 0x30282 },
344188417Sthompsa	{   6, 0x00b33, 0x011e3, 0x1a014, 0x30287 },
345188417Sthompsa	{   7, 0x00b33, 0x011e4, 0x1a014, 0x30282 },
346188417Sthompsa	{   8, 0x00b33, 0x011e4, 0x1a014, 0x30287 },
347188417Sthompsa	{   9, 0x00b33, 0x011e5, 0x1a014, 0x30282 },
348188417Sthompsa	{  10, 0x00b33, 0x011e5, 0x1a014, 0x30287 },
349188417Sthompsa	{  11, 0x00b33, 0x011e6, 0x1a014, 0x30282 },
350188417Sthompsa	{  12, 0x00b33, 0x011e6, 0x1a014, 0x30287 },
351188417Sthompsa	{  13, 0x00b33, 0x011e7, 0x1a014, 0x30282 },
352188417Sthompsa	{  14, 0x00b33, 0x011e8, 0x1a014, 0x30284 },
353184610Salfred
354188417Sthompsa	{  34, 0x00b33, 0x01266, 0x26014, 0x30282 },
355188417Sthompsa	{  38, 0x00b33, 0x01267, 0x26014, 0x30284 },
356188417Sthompsa	{  42, 0x00b33, 0x01268, 0x26014, 0x30286 },
357188417Sthompsa	{  46, 0x00b33, 0x01269, 0x26014, 0x30288 },
358184610Salfred
359188417Sthompsa	{  36, 0x00b33, 0x01266, 0x26014, 0x30288 },
360188417Sthompsa	{  40, 0x00b33, 0x01268, 0x26014, 0x30280 },
361188417Sthompsa	{  44, 0x00b33, 0x01269, 0x26014, 0x30282 },
362188417Sthompsa	{  48, 0x00b33, 0x0126a, 0x26014, 0x30284 },
363188417Sthompsa	{  52, 0x00b33, 0x0126b, 0x26014, 0x30286 },
364188417Sthompsa	{  56, 0x00b33, 0x0126c, 0x26014, 0x30288 },
365188417Sthompsa	{  60, 0x00b33, 0x0126e, 0x26014, 0x30280 },
366188417Sthompsa	{  64, 0x00b33, 0x0126f, 0x26014, 0x30282 },
367184610Salfred
368188417Sthompsa	{ 100, 0x00b33, 0x0128a, 0x2e014, 0x30280 },
369188417Sthompsa	{ 104, 0x00b33, 0x0128b, 0x2e014, 0x30282 },
370188417Sthompsa	{ 108, 0x00b33, 0x0128c, 0x2e014, 0x30284 },
371188417Sthompsa	{ 112, 0x00b33, 0x0128d, 0x2e014, 0x30286 },
372188417Sthompsa	{ 116, 0x00b33, 0x0128e, 0x2e014, 0x30288 },
373188417Sthompsa	{ 120, 0x00b33, 0x012a0, 0x2e014, 0x30280 },
374188417Sthompsa	{ 124, 0x00b33, 0x012a1, 0x2e014, 0x30282 },
375188417Sthompsa	{ 128, 0x00b33, 0x012a2, 0x2e014, 0x30284 },
376188417Sthompsa	{ 132, 0x00b33, 0x012a3, 0x2e014, 0x30286 },
377188417Sthompsa	{ 136, 0x00b33, 0x012a4, 0x2e014, 0x30288 },
378188417Sthompsa	{ 140, 0x00b33, 0x012a6, 0x2e014, 0x30280 },
379184610Salfred
380188417Sthompsa	{ 149, 0x00b33, 0x012a8, 0x2e014, 0x30287 },
381188417Sthompsa	{ 153, 0x00b33, 0x012a9, 0x2e014, 0x30289 },
382188417Sthompsa	{ 157, 0x00b33, 0x012ab, 0x2e014, 0x30281 },
383188417Sthompsa	{ 161, 0x00b33, 0x012ac, 0x2e014, 0x30283 },
384188417Sthompsa	{ 165, 0x00b33, 0x012ad, 0x2e014, 0x30285 }
385184610Salfred};
386184610Salfred
387192984Sthompsastatic const struct usb_config rum_config[RUM_N_TRANSFER] = {
388188417Sthompsa	[RUM_BULK_WR] = {
389184610Salfred		.type = UE_BULK,
390184610Salfred		.endpoint = UE_ADDR_ANY,
391184610Salfred		.direction = UE_DIR_OUT,
392190734Sthompsa		.bufsize = (MCLBYTES + RT2573_TX_DESC_SIZE + 8),
393190734Sthompsa		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
394190734Sthompsa		.callback = rum_bulk_write_callback,
395190734Sthompsa		.timeout = 5000,	/* ms */
396184610Salfred	},
397188417Sthompsa	[RUM_BULK_RD] = {
398184610Salfred		.type = UE_BULK,
399184610Salfred		.endpoint = UE_ADDR_ANY,
400184610Salfred		.direction = UE_DIR_IN,
401190734Sthompsa		.bufsize = (MCLBYTES + RT2573_RX_DESC_SIZE),
402190734Sthompsa		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
403190734Sthompsa		.callback = rum_bulk_read_callback,
404184610Salfred	},
405184610Salfred};
406184610Salfred
407184610Salfredstatic int
408188417Sthompsarum_match(device_t self)
409184610Salfred{
410192984Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(self);
411184610Salfred
412192499Sthompsa	if (uaa->usb_mode != USB_MODE_HOST)
413184610Salfred		return (ENXIO);
414188417Sthompsa	if (uaa->info.bConfigIndex != 0)
415184610Salfred		return (ENXIO);
416188417Sthompsa	if (uaa->info.bIfaceIndex != RT2573_IFACE_INDEX)
417184610Salfred		return (ENXIO);
418188417Sthompsa
419194228Sthompsa	return (usbd_lookup_id_by_uaa(rum_devs, sizeof(rum_devs), uaa));
420184610Salfred}
421184610Salfred
422184610Salfredstatic int
423188417Sthompsarum_attach(device_t self)
424184610Salfred{
425192984Sthompsa	struct usb_attach_arg *uaa = device_get_ivars(self);
426188417Sthompsa	struct rum_softc *sc = device_get_softc(self);
427191746Sthompsa	struct ieee80211com *ic;
428191746Sthompsa	struct ifnet *ifp;
429191746Sthompsa	uint8_t iface_index, bands;
430191746Sthompsa	uint32_t tmp;
431191746Sthompsa	int error, ntries;
432184610Salfred
433194228Sthompsa	device_set_usb_desc(self);
434184610Salfred	sc->sc_udev = uaa->device;
435188417Sthompsa	sc->sc_dev = self;
436184610Salfred
437188417Sthompsa	mtx_init(&sc->sc_mtx, device_get_nameunit(self),
438188417Sthompsa	    MTX_NETWORK_LOCK, MTX_DEF);
439184610Salfred
440184610Salfred	iface_index = RT2573_IFACE_INDEX;
441194228Sthompsa	error = usbd_transfer_setup(uaa->device, &iface_index,
442184610Salfred	    sc->sc_xfer, rum_config, RUM_N_TRANSFER, sc, &sc->sc_mtx);
443184610Salfred	if (error) {
444188417Sthompsa		device_printf(self, "could not allocate USB transfers, "
445194228Sthompsa		    "err=%s\n", usbd_errstr(error));
446184610Salfred		goto detach;
447184610Salfred	}
448184610Salfred
449188419Sthompsa	RUM_LOCK(sc);
450188417Sthompsa	/* retrieve RT2573 rev. no */
451188619Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
452188417Sthompsa		if ((tmp = rum_read(sc, RT2573_MAC_CSR0)) != 0)
453188417Sthompsa			break;
454188619Sthompsa		if (rum_pause(sc, hz / 100))
455188619Sthompsa			break;
456188417Sthompsa	}
457188619Sthompsa	if (ntries == 100) {
458188419Sthompsa		device_printf(sc->sc_dev, "timeout waiting for chip to settle\n");
459191746Sthompsa		RUM_UNLOCK(sc);
460191746Sthompsa		goto detach;
461188417Sthompsa	}
462184610Salfred
463188417Sthompsa	/* retrieve MAC address and various other things from EEPROM */
464188417Sthompsa	rum_read_eeprom(sc);
465184610Salfred
466188419Sthompsa	device_printf(sc->sc_dev, "MAC/BBP RT2573 (rev 0x%05x), RF %s\n",
467188417Sthompsa	    tmp, rum_get_rf(sc->rf_rev));
468184610Salfred
469189123Sthompsa	rum_load_microcode(sc, rt2573_ucode, sizeof(rt2573_ucode));
470188417Sthompsa	RUM_UNLOCK(sc);
471184610Salfred
472188419Sthompsa	ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
473188419Sthompsa	if (ifp == NULL) {
474188419Sthompsa		device_printf(sc->sc_dev, "can not if_alloc()\n");
475191746Sthompsa		goto detach;
476188419Sthompsa	}
477188419Sthompsa	ic = ifp->if_l2com;
478188419Sthompsa
479188417Sthompsa	ifp->if_softc = sc;
480188417Sthompsa	if_initname(ifp, "rum", device_get_unit(sc->sc_dev));
481188417Sthompsa	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
482188417Sthompsa	ifp->if_init = rum_init;
483188417Sthompsa	ifp->if_ioctl = rum_ioctl;
484188417Sthompsa	ifp->if_start = rum_start;
485207554Ssobomax	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
486207554Ssobomax	ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
487188417Sthompsa	IFQ_SET_READY(&ifp->if_snd);
488184610Salfred
489188417Sthompsa	ic->ic_ifp = ifp;
490188417Sthompsa	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
491184610Salfred
492188417Sthompsa	/* set device capabilities */
493188417Sthompsa	ic->ic_caps =
494188417Sthompsa	      IEEE80211_C_STA		/* station mode supported */
495188417Sthompsa	    | IEEE80211_C_IBSS		/* IBSS mode supported */
496188417Sthompsa	    | IEEE80211_C_MONITOR	/* monitor mode supported */
497188417Sthompsa	    | IEEE80211_C_HOSTAP	/* HostAp mode supported */
498188417Sthompsa	    | IEEE80211_C_TXPMGT	/* tx power management */
499188417Sthompsa	    | IEEE80211_C_SHPREAMBLE	/* short preamble supported */
500188417Sthompsa	    | IEEE80211_C_SHSLOT	/* short slot time supported */
501188417Sthompsa	    | IEEE80211_C_BGSCAN	/* bg scanning supported */
502188417Sthompsa	    | IEEE80211_C_WPA		/* 802.11i */
503188417Sthompsa	    ;
504184610Salfred
505188417Sthompsa	bands = 0;
506188417Sthompsa	setbit(&bands, IEEE80211_MODE_11B);
507188417Sthompsa	setbit(&bands, IEEE80211_MODE_11G);
508188417Sthompsa	if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_5226)
509188417Sthompsa		setbit(&bands, IEEE80211_MODE_11A);
510188417Sthompsa	ieee80211_init_channels(ic, NULL, &bands);
511184610Salfred
512190526Ssam	ieee80211_ifattach(ic, sc->sc_bssid);
513189123Sthompsa	ic->ic_update_promisc = rum_update_promisc;
514188417Sthompsa	ic->ic_raw_xmit = rum_raw_xmit;
515188417Sthompsa	ic->ic_scan_start = rum_scan_start;
516188417Sthompsa	ic->ic_scan_end = rum_scan_end;
517188417Sthompsa	ic->ic_set_channel = rum_set_channel;
518184610Salfred
519188417Sthompsa	ic->ic_vap_create = rum_vap_create;
520188417Sthompsa	ic->ic_vap_delete = rum_vap_delete;
521207768Semaste	ic->ic_update_mcast = rum_update_mcast;
522184610Salfred
523192468Ssam	ieee80211_radiotap_attach(ic,
524192468Ssam	    &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
525192468Ssam		RT2573_TX_RADIOTAP_PRESENT,
526192468Ssam	    &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
527192468Ssam		RT2573_RX_RADIOTAP_PRESENT);
528184610Salfred
529188417Sthompsa	if (bootverbose)
530188417Sthompsa		ieee80211_announce(ic);
531184610Salfred
532191746Sthompsa	return (0);
533191746Sthompsa
534191746Sthompsadetach:
535191746Sthompsa	rum_detach(self);
536191746Sthompsa	return (ENXIO);			/* failure */
537184610Salfred}
538184610Salfred
539188417Sthompsastatic int
540188417Sthompsarum_detach(device_t self)
541184610Salfred{
542188417Sthompsa	struct rum_softc *sc = device_get_softc(self);
543188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
544188619Sthompsa	struct ieee80211com *ic;
545184610Salfred
546259456Shselasky	/* Prevent further ioctls */
547259456Shselasky	RUM_LOCK(sc);
548259456Shselasky	sc->sc_detached = 1;
549259456Shselasky	RUM_UNLOCK(sc);
550259456Shselasky
551188419Sthompsa	/* stop all USB transfers */
552194228Sthompsa	usbd_transfer_unsetup(sc->sc_xfer, RUM_N_TRANSFER);
553184610Salfred
554188419Sthompsa	/* free TX list, if any */
555188419Sthompsa	RUM_LOCK(sc);
556188419Sthompsa	rum_unsetup_tx_list(sc);
557188419Sthompsa	RUM_UNLOCK(sc);
558188419Sthompsa
559188417Sthompsa	if (ifp) {
560188619Sthompsa		ic = ifp->if_l2com;
561188417Sthompsa		ieee80211_ifdetach(ic);
562188417Sthompsa		if_free(ifp);
563188417Sthompsa	}
564188417Sthompsa	mtx_destroy(&sc->sc_mtx);
565188417Sthompsa	return (0);
566184610Salfred}
567184610Salfred
568193045Sthompsastatic usb_error_t
569189123Sthompsarum_do_request(struct rum_softc *sc,
570192984Sthompsa    struct usb_device_request *req, void *data)
571189123Sthompsa{
572193045Sthompsa	usb_error_t err;
573189123Sthompsa	int ntries = 10;
574189123Sthompsa
575189123Sthompsa	while (ntries--) {
576194228Sthompsa		err = usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
577189123Sthompsa		    req, data, 0, NULL, 250 /* ms */);
578189123Sthompsa		if (err == 0)
579189123Sthompsa			break;
580189123Sthompsa
581189123Sthompsa		DPRINTFN(1, "Control request failed, %s (retrying)\n",
582194228Sthompsa		    usbd_errstr(err));
583189123Sthompsa		if (rum_pause(sc, hz / 100))
584189123Sthompsa			break;
585189123Sthompsa	}
586189123Sthompsa	return (err);
587189123Sthompsa}
588189123Sthompsa
589188417Sthompsastatic struct ieee80211vap *
590234753Sdimrum_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
591234753Sdim    enum ieee80211_opmode opmode, int flags,
592234753Sdim    const uint8_t bssid[IEEE80211_ADDR_LEN],
593234753Sdim    const uint8_t mac[IEEE80211_ADDR_LEN])
594184610Salfred{
595188417Sthompsa	struct rum_softc *sc = ic->ic_ifp->if_softc;
596188417Sthompsa	struct rum_vap *rvp;
597188417Sthompsa	struct ieee80211vap *vap;
598184610Salfred
599188417Sthompsa	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
600188417Sthompsa		return NULL;
601188417Sthompsa	rvp = (struct rum_vap *) malloc(sizeof(struct rum_vap),
602188417Sthompsa	    M_80211_VAP, M_NOWAIT | M_ZERO);
603188417Sthompsa	if (rvp == NULL)
604188417Sthompsa		return NULL;
605188417Sthompsa	vap = &rvp->vap;
606188417Sthompsa	/* enable s/w bmiss handling for sta mode */
607184610Salfred
608259457Shselasky	if (ieee80211_vap_setup(ic, vap, name, unit, opmode,
609259457Shselasky	    flags | IEEE80211_CLONE_NOBEACONS, bssid, mac) != 0) {
610259457Shselasky		/* out of memory */
611259457Shselasky		free(rvp, M_80211_VAP);
612259457Shselasky		return (NULL);
613259457Shselasky	}
614259457Shselasky
615188417Sthompsa	/* override state transition machine */
616188417Sthompsa	rvp->newstate = vap->iv_newstate;
617188417Sthompsa	vap->iv_newstate = rum_newstate;
618184610Salfred
619206358Srpaulo	usb_callout_init_mtx(&rvp->ratectl_ch, &sc->sc_mtx, 0);
620206358Srpaulo	TASK_INIT(&rvp->ratectl_task, 0, rum_ratectl_task, rvp);
621206358Srpaulo	ieee80211_ratectl_init(vap);
622206358Srpaulo	ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */);
623188417Sthompsa	/* complete setup */
624188417Sthompsa	ieee80211_vap_attach(vap, ieee80211_media_change, ieee80211_media_status);
625188417Sthompsa	ic->ic_opmode = opmode;
626188417Sthompsa	return vap;
627184610Salfred}
628184610Salfred
629184610Salfredstatic void
630188417Sthompsarum_vap_delete(struct ieee80211vap *vap)
631184610Salfred{
632188417Sthompsa	struct rum_vap *rvp = RUM_VAP(vap);
633191746Sthompsa	struct ieee80211com *ic = vap->iv_ic;
634184610Salfred
635206358Srpaulo	usb_callout_drain(&rvp->ratectl_ch);
636206358Srpaulo	ieee80211_draintask(ic, &rvp->ratectl_task);
637206358Srpaulo	ieee80211_ratectl_deinit(vap);
638188417Sthompsa	ieee80211_vap_detach(vap);
639188417Sthompsa	free(rvp, M_80211_VAP);
640184610Salfred}
641184610Salfred
642188417Sthompsastatic void
643188417Sthompsarum_tx_free(struct rum_tx_data *data, int txerr)
644184610Salfred{
645188417Sthompsa	struct rum_softc *sc = data->sc;
646184610Salfred
647188417Sthompsa	if (data->m != NULL) {
648188417Sthompsa		if (data->m->m_flags & M_TXCB)
649188417Sthompsa			ieee80211_process_callback(data->ni, data->m,
650188417Sthompsa			    txerr ? ETIMEDOUT : 0);
651188417Sthompsa		m_freem(data->m);
652188417Sthompsa		data->m = NULL;
653184610Salfred
654188417Sthompsa		ieee80211_free_node(data->ni);
655188417Sthompsa		data->ni = NULL;
656184610Salfred	}
657188417Sthompsa	STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
658188417Sthompsa	sc->tx_nfree++;
659184610Salfred}
660184610Salfred
661188419Sthompsastatic void
662188419Sthompsarum_setup_tx_list(struct rum_softc *sc)
663184610Salfred{
664188417Sthompsa	struct rum_tx_data *data;
665188417Sthompsa	int i;
666184610Salfred
667188417Sthompsa	sc->tx_nfree = 0;
668188417Sthompsa	STAILQ_INIT(&sc->tx_q);
669188417Sthompsa	STAILQ_INIT(&sc->tx_free);
670184610Salfred
671188417Sthompsa	for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
672188417Sthompsa		data = &sc->tx_data[i];
673184610Salfred
674188417Sthompsa		data->sc = sc;
675188417Sthompsa		STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
676188417Sthompsa		sc->tx_nfree++;
677184610Salfred	}
678184610Salfred}
679184610Salfred
680184610Salfredstatic void
681188419Sthompsarum_unsetup_tx_list(struct rum_softc *sc)
682184610Salfred{
683188417Sthompsa	struct rum_tx_data *data;
684188417Sthompsa	int i;
685184610Salfred
686188419Sthompsa	/* make sure any subsequent use of the queues will fail */
687188419Sthompsa	sc->tx_nfree = 0;
688188419Sthompsa	STAILQ_INIT(&sc->tx_q);
689188419Sthompsa	STAILQ_INIT(&sc->tx_free);
690184610Salfred
691188419Sthompsa	/* free up all node references and mbufs */
692188417Sthompsa	for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
693188417Sthompsa		data = &sc->tx_data[i];
694188417Sthompsa
695188417Sthompsa		if (data->m != NULL) {
696188417Sthompsa			m_freem(data->m);
697188417Sthompsa			data->m = NULL;
698184610Salfred		}
699188417Sthompsa		if (data->ni != NULL) {
700188417Sthompsa			ieee80211_free_node(data->ni);
701188417Sthompsa			data->ni = NULL;
702188417Sthompsa		}
703184610Salfred	}
704184610Salfred}
705184610Salfred
706191746Sthompsastatic int
707191746Sthompsarum_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
708184610Salfred{
709188417Sthompsa	struct rum_vap *rvp = RUM_VAP(vap);
710191746Sthompsa	struct ieee80211com *ic = vap->iv_ic;
711191746Sthompsa	struct rum_softc *sc = ic->ic_ifp->if_softc;
712188417Sthompsa	const struct ieee80211_txparam *tp;
713188417Sthompsa	enum ieee80211_state ostate;
714188417Sthompsa	struct ieee80211_node *ni;
715184610Salfred	uint32_t tmp;
716184610Salfred
717188417Sthompsa	ostate = vap->iv_state;
718191746Sthompsa	DPRINTF("%s -> %s\n",
719191746Sthompsa		ieee80211_state_name[ostate],
720191746Sthompsa		ieee80211_state_name[nstate]);
721184610Salfred
722191746Sthompsa	IEEE80211_UNLOCK(ic);
723191746Sthompsa	RUM_LOCK(sc);
724206358Srpaulo	usb_callout_stop(&rvp->ratectl_ch);
725191746Sthompsa
726191746Sthompsa	switch (nstate) {
727188417Sthompsa	case IEEE80211_S_INIT:
728188417Sthompsa		if (ostate == IEEE80211_S_RUN) {
729188417Sthompsa			/* abort TSF synchronization */
730188417Sthompsa			tmp = rum_read(sc, RT2573_TXRX_CSR9);
731188417Sthompsa			rum_write(sc, RT2573_TXRX_CSR9, tmp & ~0x00ffffff);
732188417Sthompsa		}
733188417Sthompsa		break;
734184610Salfred
735188417Sthompsa	case IEEE80211_S_RUN:
736212127Sthompsa		ni = ieee80211_ref_node(vap->iv_bss);
737184610Salfred
738188417Sthompsa		if (vap->iv_opmode != IEEE80211_M_MONITOR) {
739236897Shselasky			if (ic->ic_bsschan == IEEE80211_CHAN_ANYC) {
740236897Shselasky				RUM_UNLOCK(sc);
741236897Shselasky				IEEE80211_LOCK(ic);
742236897Shselasky				ieee80211_free_node(ni);
743236897Shselasky				return (-1);
744236897Shselasky			}
745188417Sthompsa			rum_update_slot(ic->ic_ifp);
746188417Sthompsa			rum_enable_mrr(sc);
747188417Sthompsa			rum_set_txpreamble(sc);
748188417Sthompsa			rum_set_basicrates(sc);
749188419Sthompsa			IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid);
750188419Sthompsa			rum_set_bssid(sc, sc->sc_bssid);
751184610Salfred		}
752184610Salfred
753188417Sthompsa		if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
754188417Sthompsa		    vap->iv_opmode == IEEE80211_M_IBSS)
755188417Sthompsa			rum_prepare_beacon(sc, vap);
756184610Salfred
757188417Sthompsa		if (vap->iv_opmode != IEEE80211_M_MONITOR)
758188417Sthompsa			rum_enable_tsf_sync(sc);
759192468Ssam		else
760192468Ssam			rum_enable_tsf(sc);
761184610Salfred
762188417Sthompsa		/* enable automatic rate adaptation */
763192468Ssam		tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
764188417Sthompsa		if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
765206358Srpaulo			rum_ratectl_start(sc, ni);
766212127Sthompsa		ieee80211_free_node(ni);
767188417Sthompsa		break;
768188417Sthompsa	default:
769188417Sthompsa		break;
770184610Salfred	}
771188417Sthompsa	RUM_UNLOCK(sc);
772188417Sthompsa	IEEE80211_LOCK(ic);
773191746Sthompsa	return (rvp->newstate(vap, nstate, arg));
774188417Sthompsa}
775184610Salfred
776188417Sthompsastatic void
777194677Sthompsarum_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
778188417Sthompsa{
779194677Sthompsa	struct rum_softc *sc = usbd_xfer_softc(xfer);
780188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
781192468Ssam	struct ieee80211vap *vap;
782188417Sthompsa	struct rum_tx_data *data;
783188417Sthompsa	struct mbuf *m;
784194677Sthompsa	struct usb_page_cache *pc;
785188419Sthompsa	unsigned int len;
786194677Sthompsa	int actlen, sumlen;
787184610Salfred
788194677Sthompsa	usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
789194677Sthompsa
790188417Sthompsa	switch (USB_GET_STATE(xfer)) {
791188417Sthompsa	case USB_ST_TRANSFERRED:
792194677Sthompsa		DPRINTFN(11, "transfer complete, %d bytes\n", actlen);
793184610Salfred
794188417Sthompsa		/* free resources */
795194677Sthompsa		data = usbd_xfer_get_priv(xfer);
796188417Sthompsa		rum_tx_free(data, 0);
797194677Sthompsa		usbd_xfer_set_priv(xfer, NULL);
798184610Salfred
799188417Sthompsa		ifp->if_opackets++;
800188417Sthompsa		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
801184610Salfred
802188417Sthompsa		/* FALLTHROUGH */
803188417Sthompsa	case USB_ST_SETUP:
804188417Sthompsatr_setup:
805188417Sthompsa		data = STAILQ_FIRST(&sc->tx_q);
806188417Sthompsa		if (data) {
807188417Sthompsa			STAILQ_REMOVE_HEAD(&sc->tx_q, next);
808188417Sthompsa			m = data->m;
809184610Salfred
810235000Shselasky			if (m->m_pkthdr.len > (int)(MCLBYTES + RT2573_TX_DESC_SIZE)) {
811188417Sthompsa				DPRINTFN(0, "data overflow, %u bytes\n",
812188417Sthompsa				    m->m_pkthdr.len);
813188417Sthompsa				m->m_pkthdr.len = (MCLBYTES + RT2573_TX_DESC_SIZE);
814188417Sthompsa			}
815194677Sthompsa			pc = usbd_xfer_get_frame(xfer, 0);
816194677Sthompsa			usbd_copy_in(pc, 0, &data->desc, RT2573_TX_DESC_SIZE);
817194677Sthompsa			usbd_m_copy_in(pc, RT2573_TX_DESC_SIZE, m, 0,
818194677Sthompsa			    m->m_pkthdr.len);
819184610Salfred
820192468Ssam			vap = data->ni->ni_vap;
821192468Ssam			if (ieee80211_radiotap_active_vap(vap)) {
822188417Sthompsa				struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
823184610Salfred
824188417Sthompsa				tap->wt_flags = 0;
825188417Sthompsa				tap->wt_rate = data->rate;
826188417Sthompsa				tap->wt_antenna = sc->tx_ant;
827184610Salfred
828192468Ssam				ieee80211_radiotap_tx(vap, m);
829188417Sthompsa			}
830184610Salfred
831188417Sthompsa			/* align end on a 4-bytes boundary */
832188417Sthompsa			len = (RT2573_TX_DESC_SIZE + m->m_pkthdr.len + 3) & ~3;
833188417Sthompsa			if ((len % 64) == 0)
834188417Sthompsa				len += 4;
835184610Salfred
836188417Sthompsa			DPRINTFN(11, "sending frame len=%u xferlen=%u\n",
837188417Sthompsa			    m->m_pkthdr.len, len);
838184610Salfred
839194677Sthompsa			usbd_xfer_set_frame_len(xfer, 0, len);
840194677Sthompsa			usbd_xfer_set_priv(xfer, data);
841184610Salfred
842194228Sthompsa			usbd_transfer_submit(xfer);
843188417Sthompsa		}
844198098Sweongyo		RUM_UNLOCK(sc);
845198098Sweongyo		rum_start(ifp);
846198098Sweongyo		RUM_LOCK(sc);
847188417Sthompsa		break;
848184610Salfred
849188417Sthompsa	default:			/* Error */
850188417Sthompsa		DPRINTFN(11, "transfer error, %s\n",
851194677Sthompsa		    usbd_errstr(error));
852184610Salfred
853188419Sthompsa		ifp->if_oerrors++;
854194677Sthompsa		data = usbd_xfer_get_priv(xfer);
855188419Sthompsa		if (data != NULL) {
856194677Sthompsa			rum_tx_free(data, error);
857194677Sthompsa			usbd_xfer_set_priv(xfer, NULL);
858188419Sthompsa		}
859188419Sthompsa
860203141Sthompsa		if (error != USB_ERR_CANCELLED) {
861203141Sthompsa			if (error == USB_ERR_TIMEOUT)
862203141Sthompsa				device_printf(sc->sc_dev, "device timeout\n");
863203141Sthompsa
864203141Sthompsa			/*
865203141Sthompsa			 * Try to clear stall first, also if other
866203141Sthompsa			 * errors occur, hence clearing stall
867203141Sthompsa			 * introduces a 50 ms delay:
868203141Sthompsa			 */
869194677Sthompsa			usbd_xfer_set_stall(xfer);
870188417Sthompsa			goto tr_setup;
871188417Sthompsa		}
872188417Sthompsa		break;
873184610Salfred	}
874184610Salfred}
875184610Salfred
876184610Salfredstatic void
877194677Sthompsarum_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
878184610Salfred{
879194677Sthompsa	struct rum_softc *sc = usbd_xfer_softc(xfer);
880184610Salfred	struct ifnet *ifp = sc->sc_ifp;
881184610Salfred	struct ieee80211com *ic = ifp->if_l2com;
882184610Salfred	struct ieee80211_node *ni;
883184610Salfred	struct mbuf *m = NULL;
884194677Sthompsa	struct usb_page_cache *pc;
885184610Salfred	uint32_t flags;
886184610Salfred	uint8_t rssi = 0;
887194677Sthompsa	int len;
888184610Salfred
889194677Sthompsa	usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
890194677Sthompsa
891184610Salfred	switch (USB_GET_STATE(xfer)) {
892184610Salfred	case USB_ST_TRANSFERRED:
893184610Salfred
894194677Sthompsa		DPRINTFN(15, "rx done, actlen=%d\n", len);
895184610Salfred
896235000Shselasky		if (len < (int)(RT2573_RX_DESC_SIZE + IEEE80211_MIN_LEN)) {
897188417Sthompsa			DPRINTF("%s: xfer too short %d\n",
898188417Sthompsa			    device_get_nameunit(sc->sc_dev), len);
899184610Salfred			ifp->if_ierrors++;
900184610Salfred			goto tr_setup;
901184610Salfred		}
902184610Salfred
903188417Sthompsa		len -= RT2573_RX_DESC_SIZE;
904194677Sthompsa		pc = usbd_xfer_get_frame(xfer, 0);
905194677Sthompsa		usbd_copy_out(pc, 0, &sc->sc_rx_desc, RT2573_RX_DESC_SIZE);
906188417Sthompsa
907188417Sthompsa		rssi = rum_get_rssi(sc, sc->sc_rx_desc.rssi);
908184610Salfred		flags = le32toh(sc->sc_rx_desc.flags);
909184610Salfred		if (flags & RT2573_RX_CRC_ERROR) {
910184610Salfred			/*
911184610Salfred		         * This should not happen since we did not
912184610Salfred		         * request to receive those frames when we
913188417Sthompsa		         * filled RUM_TXRX_CSR2:
914184610Salfred		         */
915188417Sthompsa			DPRINTFN(5, "PHY or CRC error\n");
916184610Salfred			ifp->if_ierrors++;
917184610Salfred			goto tr_setup;
918184610Salfred		}
919188417Sthompsa
920248078Smarius		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
921184610Salfred		if (m == NULL) {
922184610Salfred			DPRINTF("could not allocate mbuf\n");
923184610Salfred			ifp->if_ierrors++;
924184610Salfred			goto tr_setup;
925184610Salfred		}
926194677Sthompsa		usbd_copy_out(pc, RT2573_RX_DESC_SIZE,
927188417Sthompsa		    mtod(m, uint8_t *), len);
928184610Salfred
929184610Salfred		/* finalize mbuf */
930184610Salfred		m->m_pkthdr.rcvif = ifp;
931184610Salfred		m->m_pkthdr.len = m->m_len = (flags >> 16) & 0xfff;
932184610Salfred
933192468Ssam		if (ieee80211_radiotap_active(ic)) {
934184610Salfred			struct rum_rx_radiotap_header *tap = &sc->sc_rxtap;
935184610Salfred
936192468Ssam			/* XXX read tsf */
937192468Ssam			tap->wr_flags = 0;
938184610Salfred			tap->wr_rate = ieee80211_plcp2rate(sc->sc_rx_desc.rate,
939188417Sthompsa			    (flags & RT2573_RX_OFDM) ?
940184610Salfred			    IEEE80211_T_OFDM : IEEE80211_T_CCK);
941192468Ssam			tap->wr_antsignal = RT2573_NOISE_FLOOR + rssi;
942192468Ssam			tap->wr_antnoise = RT2573_NOISE_FLOOR;
943188417Sthompsa			tap->wr_antenna = sc->rx_ant;
944184610Salfred		}
945188417Sthompsa		/* FALLTHROUGH */
946184610Salfred	case USB_ST_SETUP:
947184610Salfredtr_setup:
948194677Sthompsa		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
949194228Sthompsa		usbd_transfer_submit(xfer);
950184610Salfred
951184610Salfred		/*
952184610Salfred		 * At the end of a USB callback it is always safe to unlock
953184610Salfred		 * the private mutex of a device! That is why we do the
954184610Salfred		 * "ieee80211_input" here, and not some lines up!
955184610Salfred		 */
956198098Sweongyo		RUM_UNLOCK(sc);
957184610Salfred		if (m) {
958188417Sthompsa			ni = ieee80211_find_rxnode(ic,
959188417Sthompsa			    mtod(m, struct ieee80211_frame_min *));
960184610Salfred			if (ni != NULL) {
961188417Sthompsa				(void) ieee80211_input(ni, m, rssi,
962192468Ssam				    RT2573_NOISE_FLOOR);
963184610Salfred				ieee80211_free_node(ni);
964188417Sthompsa			} else
965188417Sthompsa				(void) ieee80211_input_all(ic, m, rssi,
966192468Ssam				    RT2573_NOISE_FLOOR);
967184610Salfred		}
968198098Sweongyo		if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0 &&
969198098Sweongyo		    !IFQ_IS_EMPTY(&ifp->if_snd))
970198098Sweongyo			rum_start(ifp);
971198098Sweongyo		RUM_LOCK(sc);
972184610Salfred		return;
973184610Salfred
974184610Salfred	default:			/* Error */
975194677Sthompsa		if (error != USB_ERR_CANCELLED) {
976184610Salfred			/* try to clear stall first */
977194677Sthompsa			usbd_xfer_set_stall(xfer);
978188417Sthompsa			goto tr_setup;
979184610Salfred		}
980184610Salfred		return;
981184610Salfred	}
982184610Salfred}
983184610Salfred
984184610Salfredstatic uint8_t
985188417Sthompsarum_plcp_signal(int rate)
986184610Salfred{
987184610Salfred	switch (rate) {
988188417Sthompsa	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
989188417Sthompsa	case 12:	return 0xb;
990188417Sthompsa	case 18:	return 0xf;
991188417Sthompsa	case 24:	return 0xa;
992188417Sthompsa	case 36:	return 0xe;
993188417Sthompsa	case 48:	return 0x9;
994188417Sthompsa	case 72:	return 0xd;
995188417Sthompsa	case 96:	return 0x8;
996188417Sthompsa	case 108:	return 0xc;
997184610Salfred
998188417Sthompsa	/* CCK rates (NB: not IEEE std, device-specific) */
999188417Sthompsa	case 2:		return 0x0;
1000188417Sthompsa	case 4:		return 0x1;
1001188417Sthompsa	case 11:	return 0x2;
1002188417Sthompsa	case 22:	return 0x3;
1003184610Salfred	}
1004188417Sthompsa	return 0xff;		/* XXX unsupported/unknown rate */
1005184610Salfred}
1006184610Salfred
1007184610Salfredstatic void
1008188417Sthompsarum_setup_tx_desc(struct rum_softc *sc, struct rum_tx_desc *desc,
1009188417Sthompsa    uint32_t flags, uint16_t xflags, int len, int rate)
1010184610Salfred{
1011184610Salfred	struct ifnet *ifp = sc->sc_ifp;
1012184610Salfred	struct ieee80211com *ic = ifp->if_l2com;
1013184610Salfred	uint16_t plcp_length;
1014188417Sthompsa	int remainder;
1015184610Salfred
1016188417Sthompsa	desc->flags = htole32(flags);
1017188417Sthompsa	desc->flags |= htole32(RT2573_TX_VALID);
1018188417Sthompsa	desc->flags |= htole32(len << 16);
1019184610Salfred
1020188417Sthompsa	desc->xflags = htole16(xflags);
1021184610Salfred
1022188417Sthompsa	desc->wme = htole16(RT2573_QID(0) | RT2573_AIFSN(2) |
1023184610Salfred	    RT2573_LOGCWMIN(4) | RT2573_LOGCWMAX(10));
1024184610Salfred
1025184610Salfred	/* setup PLCP fields */
1026188417Sthompsa	desc->plcp_signal  = rum_plcp_signal(rate);
1027188417Sthompsa	desc->plcp_service = 4;
1028184610Salfred
1029184610Salfred	len += IEEE80211_CRC_LEN;
1030190532Ssam	if (ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) {
1031188417Sthompsa		desc->flags |= htole32(RT2573_TX_OFDM);
1032184610Salfred
1033188417Sthompsa		plcp_length = len & 0xfff;
1034188417Sthompsa		desc->plcp_length_hi = plcp_length >> 6;
1035188417Sthompsa		desc->plcp_length_lo = plcp_length & 0x3f;
1036184610Salfred	} else {
1037188417Sthompsa		plcp_length = (16 * len + rate - 1) / rate;
1038184610Salfred		if (rate == 22) {
1039184610Salfred			remainder = (16 * len) % 22;
1040188417Sthompsa			if (remainder != 0 && remainder < 7)
1041188417Sthompsa				desc->plcp_service |= RT2573_PLCP_LENGEXT;
1042184610Salfred		}
1043188417Sthompsa		desc->plcp_length_hi = plcp_length >> 8;
1044188417Sthompsa		desc->plcp_length_lo = plcp_length & 0xff;
1045184610Salfred
1046188417Sthompsa		if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1047188417Sthompsa			desc->plcp_signal |= 0x08;
1048184610Salfred	}
1049188417Sthompsa}
1050184610Salfred
1051188417Sthompsastatic int
1052188417Sthompsarum_sendprot(struct rum_softc *sc,
1053188417Sthompsa    const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate)
1054188417Sthompsa{
1055188417Sthompsa	struct ieee80211com *ic = ni->ni_ic;
1056188417Sthompsa	const struct ieee80211_frame *wh;
1057188417Sthompsa	struct rum_tx_data *data;
1058188417Sthompsa	struct mbuf *mprot;
1059188417Sthompsa	int protrate, ackrate, pktlen, flags, isshort;
1060188417Sthompsa	uint16_t dur;
1061188417Sthompsa
1062188417Sthompsa	RUM_LOCK_ASSERT(sc, MA_OWNED);
1063188417Sthompsa	KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY,
1064188417Sthompsa	    ("protection %d", prot));
1065188417Sthompsa
1066188417Sthompsa	wh = mtod(m, const struct ieee80211_frame *);
1067188417Sthompsa	pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
1068188417Sthompsa
1069190532Ssam	protrate = ieee80211_ctl_rate(ic->ic_rt, rate);
1070190532Ssam	ackrate = ieee80211_ack_rate(ic->ic_rt, rate);
1071188417Sthompsa
1072188417Sthompsa	isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0;
1073209189Sjkim	dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort)
1074190532Ssam	    + ieee80211_ack_duration(ic->ic_rt, rate, isshort);
1075188417Sthompsa	flags = RT2573_TX_MORE_FRAG;
1076188417Sthompsa	if (prot == IEEE80211_PROT_RTSCTS) {
1077188417Sthompsa		/* NB: CTS is the same size as an ACK */
1078190532Ssam		dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort);
1079188417Sthompsa		flags |= RT2573_TX_NEED_ACK;
1080188417Sthompsa		mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur);
1081188417Sthompsa	} else {
1082188417Sthompsa		mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur);
1083184610Salfred	}
1084188417Sthompsa	if (mprot == NULL) {
1085188417Sthompsa		/* XXX stat + msg */
1086189123Sthompsa		return (ENOBUFS);
1087184610Salfred	}
1088188417Sthompsa	data = STAILQ_FIRST(&sc->tx_free);
1089188417Sthompsa	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1090188417Sthompsa	sc->tx_nfree--;
1091184610Salfred
1092188417Sthompsa	data->m = mprot;
1093188417Sthompsa	data->ni = ieee80211_ref_node(ni);
1094188417Sthompsa	data->rate = protrate;
1095188417Sthompsa	rum_setup_tx_desc(sc, &data->desc, flags, 0, mprot->m_pkthdr.len, protrate);
1096184610Salfred
1097188417Sthompsa	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1098194228Sthompsa	usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1099188417Sthompsa
1100188417Sthompsa	return 0;
1101188417Sthompsa}
1102188417Sthompsa
1103188417Sthompsastatic int
1104188417Sthompsarum_tx_mgt(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1105188417Sthompsa{
1106188417Sthompsa	struct ieee80211vap *vap = ni->ni_vap;
1107188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
1108188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
1109188417Sthompsa	struct rum_tx_data *data;
1110188417Sthompsa	struct ieee80211_frame *wh;
1111188417Sthompsa	const struct ieee80211_txparam *tp;
1112188417Sthompsa	struct ieee80211_key *k;
1113188417Sthompsa	uint32_t flags = 0;
1114188417Sthompsa	uint16_t dur;
1115188417Sthompsa
1116188417Sthompsa	RUM_LOCK_ASSERT(sc, MA_OWNED);
1117188417Sthompsa
1118188417Sthompsa	data = STAILQ_FIRST(&sc->tx_free);
1119188417Sthompsa	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1120188417Sthompsa	sc->tx_nfree--;
1121188417Sthompsa
1122188417Sthompsa	wh = mtod(m0, struct ieee80211_frame *);
1123188417Sthompsa	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1124188417Sthompsa		k = ieee80211_crypto_encap(ni, m0);
1125188417Sthompsa		if (k == NULL) {
1126188417Sthompsa			m_freem(m0);
1127188417Sthompsa			return ENOBUFS;
1128184610Salfred		}
1129188417Sthompsa		wh = mtod(m0, struct ieee80211_frame *);
1130188417Sthompsa	}
1131184610Salfred
1132188417Sthompsa	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1133188417Sthompsa
1134188417Sthompsa	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1135188417Sthompsa		flags |= RT2573_TX_NEED_ACK;
1136188417Sthompsa
1137190532Ssam		dur = ieee80211_ack_duration(ic->ic_rt, tp->mgmtrate,
1138188417Sthompsa		    ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1139259457Shselasky		USETW(wh->i_dur, dur);
1140188417Sthompsa
1141188417Sthompsa		/* tell hardware to add timestamp for probe responses */
1142188417Sthompsa		if ((wh->i_fc[0] &
1143188417Sthompsa		    (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
1144188417Sthompsa		    (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
1145188417Sthompsa			flags |= RT2573_TX_TIMESTAMP;
1146184610Salfred	}
1147184610Salfred
1148188417Sthompsa	data->m = m0;
1149188417Sthompsa	data->ni = ni;
1150188417Sthompsa	data->rate = tp->mgmtrate;
1151188417Sthompsa
1152188417Sthompsa	rum_setup_tx_desc(sc, &data->desc, flags, 0, m0->m_pkthdr.len, tp->mgmtrate);
1153188417Sthompsa
1154188417Sthompsa	DPRINTFN(10, "sending mgt frame len=%d rate=%d\n",
1155188417Sthompsa	    m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, tp->mgmtrate);
1156188417Sthompsa
1157188417Sthompsa	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1158194228Sthompsa	usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1159188417Sthompsa
1160189123Sthompsa	return (0);
1161184610Salfred}
1162184610Salfred
1163188417Sthompsastatic int
1164188417Sthompsarum_tx_raw(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
1165188417Sthompsa    const struct ieee80211_bpf_params *params)
1166184610Salfred{
1167193073Ssam	struct ieee80211com *ic = ni->ni_ic;
1168188417Sthompsa	struct rum_tx_data *data;
1169188417Sthompsa	uint32_t flags;
1170188417Sthompsa	int rate, error;
1171184610Salfred
1172188417Sthompsa	RUM_LOCK_ASSERT(sc, MA_OWNED);
1173188417Sthompsa	KASSERT(params != NULL, ("no raw xmit params"));
1174184610Salfred
1175193073Ssam	rate = params->ibp_rate0;
1176193073Ssam	if (!ieee80211_isratevalid(ic->ic_rt, rate)) {
1177188417Sthompsa		m_freem(m0);
1178188417Sthompsa		return EINVAL;
1179188417Sthompsa	}
1180188417Sthompsa	flags = 0;
1181188417Sthompsa	if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
1182188417Sthompsa		flags |= RT2573_TX_NEED_ACK;
1183188417Sthompsa	if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) {
1184188417Sthompsa		error = rum_sendprot(sc, m0, ni,
1185188417Sthompsa		    params->ibp_flags & IEEE80211_BPF_RTS ?
1186188417Sthompsa			 IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY,
1187188417Sthompsa		    rate);
1188188969Sthompsa		if (error || sc->tx_nfree == 0) {
1189188417Sthompsa			m_freem(m0);
1190188969Sthompsa			return ENOBUFS;
1191184610Salfred		}
1192188417Sthompsa		flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
1193188417Sthompsa	}
1194184610Salfred
1195188969Sthompsa	data = STAILQ_FIRST(&sc->tx_free);
1196188969Sthompsa	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1197188969Sthompsa	sc->tx_nfree--;
1198188969Sthompsa
1199188417Sthompsa	data->m = m0;
1200188417Sthompsa	data->ni = ni;
1201188417Sthompsa	data->rate = rate;
1202184610Salfred
1203188417Sthompsa	/* XXX need to setup descriptor ourself */
1204188417Sthompsa	rum_setup_tx_desc(sc, &data->desc, flags, 0, m0->m_pkthdr.len, rate);
1205184610Salfred
1206188417Sthompsa	DPRINTFN(10, "sending raw frame len=%u rate=%u\n",
1207188417Sthompsa	    m0->m_pkthdr.len, rate);
1208184610Salfred
1209188417Sthompsa	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1210194228Sthompsa	usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1211184610Salfred
1212188417Sthompsa	return 0;
1213188417Sthompsa}
1214184610Salfred
1215188417Sthompsastatic int
1216188417Sthompsarum_tx_data(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1217188417Sthompsa{
1218188417Sthompsa	struct ieee80211vap *vap = ni->ni_vap;
1219188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
1220188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
1221188417Sthompsa	struct rum_tx_data *data;
1222188417Sthompsa	struct ieee80211_frame *wh;
1223188417Sthompsa	const struct ieee80211_txparam *tp;
1224188417Sthompsa	struct ieee80211_key *k;
1225188417Sthompsa	uint32_t flags = 0;
1226188417Sthompsa	uint16_t dur;
1227188417Sthompsa	int error, rate;
1228184610Salfred
1229188417Sthompsa	RUM_LOCK_ASSERT(sc, MA_OWNED);
1230184610Salfred
1231188417Sthompsa	wh = mtod(m0, struct ieee80211_frame *);
1232184610Salfred
1233188417Sthompsa	tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
1234188417Sthompsa	if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1235188417Sthompsa		rate = tp->mcastrate;
1236188417Sthompsa	else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
1237188417Sthompsa		rate = tp->ucastrate;
1238188417Sthompsa	else
1239188417Sthompsa		rate = ni->ni_txrate;
1240188417Sthompsa
1241188417Sthompsa	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1242188417Sthompsa		k = ieee80211_crypto_encap(ni, m0);
1243188417Sthompsa		if (k == NULL) {
1244188417Sthompsa			m_freem(m0);
1245188417Sthompsa			return ENOBUFS;
1246184610Salfred		}
1247184610Salfred
1248188417Sthompsa		/* packet header may have moved, reset our local pointer */
1249188417Sthompsa		wh = mtod(m0, struct ieee80211_frame *);
1250188417Sthompsa	}
1251184610Salfred
1252188417Sthompsa	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1253188417Sthompsa		int prot = IEEE80211_PROT_NONE;
1254188417Sthompsa		if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold)
1255188417Sthompsa			prot = IEEE80211_PROT_RTSCTS;
1256188417Sthompsa		else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
1257190532Ssam		    ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM)
1258188417Sthompsa			prot = ic->ic_protmode;
1259188417Sthompsa		if (prot != IEEE80211_PROT_NONE) {
1260188417Sthompsa			error = rum_sendprot(sc, m0, ni, prot, rate);
1261188969Sthompsa			if (error || sc->tx_nfree == 0) {
1262188417Sthompsa				m_freem(m0);
1263188969Sthompsa				return ENOBUFS;
1264188417Sthompsa			}
1265188417Sthompsa			flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
1266184610Salfred		}
1267184610Salfred	}
1268184610Salfred
1269188417Sthompsa	data = STAILQ_FIRST(&sc->tx_free);
1270188417Sthompsa	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1271188417Sthompsa	sc->tx_nfree--;
1272184610Salfred
1273188417Sthompsa	data->m = m0;
1274188417Sthompsa	data->ni = ni;
1275188417Sthompsa	data->rate = rate;
1276188417Sthompsa
1277188417Sthompsa	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1278188417Sthompsa		flags |= RT2573_TX_NEED_ACK;
1279188417Sthompsa		flags |= RT2573_TX_MORE_FRAG;
1280188417Sthompsa
1281190532Ssam		dur = ieee80211_ack_duration(ic->ic_rt, rate,
1282188417Sthompsa		    ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1283259457Shselasky		USETW(wh->i_dur, dur);
1284184610Salfred	}
1285184610Salfred
1286188417Sthompsa	rum_setup_tx_desc(sc, &data->desc, flags, 0, m0->m_pkthdr.len, rate);
1287184610Salfred
1288188417Sthompsa	DPRINTFN(10, "sending frame len=%d rate=%d\n",
1289188417Sthompsa	    m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, rate);
1290184610Salfred
1291188417Sthompsa	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1292194228Sthompsa	usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1293188417Sthompsa
1294188417Sthompsa	return 0;
1295184610Salfred}
1296184610Salfred
1297184610Salfredstatic void
1298188417Sthompsarum_start(struct ifnet *ifp)
1299184610Salfred{
1300188417Sthompsa	struct rum_softc *sc = ifp->if_softc;
1301188417Sthompsa	struct ieee80211_node *ni;
1302188417Sthompsa	struct mbuf *m;
1303184610Salfred
1304188417Sthompsa	RUM_LOCK(sc);
1305188417Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1306188417Sthompsa		RUM_UNLOCK(sc);
1307188417Sthompsa		return;
1308188417Sthompsa	}
1309188417Sthompsa	for (;;) {
1310188417Sthompsa		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1311188417Sthompsa		if (m == NULL)
1312188417Sthompsa			break;
1313188969Sthompsa		if (sc->tx_nfree < RUM_TX_MINFREE) {
1314188417Sthompsa			IFQ_DRV_PREPEND(&ifp->if_snd, m);
1315188417Sthompsa			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1316188417Sthompsa			break;
1317188417Sthompsa		}
1318188417Sthompsa		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1319188417Sthompsa		if (rum_tx_data(sc, m, ni) != 0) {
1320188417Sthompsa			ieee80211_free_node(ni);
1321188417Sthompsa			ifp->if_oerrors++;
1322188417Sthompsa			break;
1323188417Sthompsa		}
1324188417Sthompsa	}
1325188417Sthompsa	RUM_UNLOCK(sc);
1326184610Salfred}
1327184610Salfred
1328184610Salfredstatic int
1329188417Sthompsarum_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1330184610Salfred{
1331184610Salfred	struct rum_softc *sc = ifp->if_softc;
1332184610Salfred	struct ieee80211com *ic = ifp->if_l2com;
1333188417Sthompsa	struct ifreq *ifr = (struct ifreq *) data;
1334259456Shselasky	int error;
1335259456Shselasky	int startall = 0;
1336184610Salfred
1337259456Shselasky	RUM_LOCK(sc);
1338259456Shselasky	error = sc->sc_detached ? ENXIO : 0;
1339259456Shselasky	RUM_UNLOCK(sc);
1340259456Shselasky	if (error)
1341259456Shselasky		return (error);
1342259456Shselasky
1343184610Salfred	switch (cmd) {
1344184610Salfred	case SIOCSIFFLAGS:
1345188417Sthompsa		RUM_LOCK(sc);
1346184610Salfred		if (ifp->if_flags & IFF_UP) {
1347188417Sthompsa			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1348191746Sthompsa				rum_init_locked(sc);
1349188417Sthompsa				startall = 1;
1350188417Sthompsa			} else
1351191746Sthompsa				rum_setpromisc(sc);
1352184610Salfred		} else {
1353191746Sthompsa			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1354191746Sthompsa				rum_stop(sc);
1355184610Salfred		}
1356188417Sthompsa		RUM_UNLOCK(sc);
1357188417Sthompsa		if (startall)
1358188417Sthompsa			ieee80211_start_all(ic);
1359184610Salfred		break;
1360184610Salfred	case SIOCGIFMEDIA:
1361188417Sthompsa		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
1362184610Salfred		break;
1363188417Sthompsa	case SIOCGIFADDR:
1364188417Sthompsa		error = ether_ioctl(ifp, cmd, data);
1365188417Sthompsa		break;
1366184610Salfred	default:
1367188417Sthompsa		error = EINVAL;
1368188417Sthompsa		break;
1369184610Salfred	}
1370188417Sthompsa	return error;
1371184610Salfred}
1372184610Salfred
1373184610Salfredstatic void
1374188417Sthompsarum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len)
1375184610Salfred{
1376192984Sthompsa	struct usb_device_request req;
1377193045Sthompsa	usb_error_t error;
1378184610Salfred
1379188417Sthompsa	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1380188417Sthompsa	req.bRequest = RT2573_READ_EEPROM;
1381188417Sthompsa	USETW(req.wValue, 0);
1382188417Sthompsa	USETW(req.wIndex, addr);
1383188417Sthompsa	USETW(req.wLength, len);
1384188417Sthompsa
1385188419Sthompsa	error = rum_do_request(sc, &req, buf);
1386188417Sthompsa	if (error != 0) {
1387188417Sthompsa		device_printf(sc->sc_dev, "could not read EEPROM: %s\n",
1388194228Sthompsa		    usbd_errstr(error));
1389188417Sthompsa	}
1390184610Salfred}
1391184610Salfred
1392188417Sthompsastatic uint32_t
1393188417Sthompsarum_read(struct rum_softc *sc, uint16_t reg)
1394188417Sthompsa{
1395188417Sthompsa	uint32_t val;
1396188417Sthompsa
1397188417Sthompsa	rum_read_multi(sc, reg, &val, sizeof val);
1398188417Sthompsa
1399188417Sthompsa	return le32toh(val);
1400188417Sthompsa}
1401188417Sthompsa
1402184610Salfredstatic void
1403188417Sthompsarum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len)
1404184610Salfred{
1405192984Sthompsa	struct usb_device_request req;
1406193045Sthompsa	usb_error_t error;
1407184610Salfred
1408188417Sthompsa	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1409188417Sthompsa	req.bRequest = RT2573_READ_MULTI_MAC;
1410188417Sthompsa	USETW(req.wValue, 0);
1411188417Sthompsa	USETW(req.wIndex, reg);
1412188417Sthompsa	USETW(req.wLength, len);
1413184610Salfred
1414188419Sthompsa	error = rum_do_request(sc, &req, buf);
1415188417Sthompsa	if (error != 0) {
1416188417Sthompsa		device_printf(sc->sc_dev,
1417188417Sthompsa		    "could not multi read MAC register: %s\n",
1418194228Sthompsa		    usbd_errstr(error));
1419184610Salfred	}
1420188417Sthompsa}
1421184610Salfred
1422193045Sthompsastatic usb_error_t
1423188417Sthompsarum_write(struct rum_softc *sc, uint16_t reg, uint32_t val)
1424188417Sthompsa{
1425188417Sthompsa	uint32_t tmp = htole32(val);
1426184610Salfred
1427189123Sthompsa	return (rum_write_multi(sc, reg, &tmp, sizeof tmp));
1428188417Sthompsa}
1429188417Sthompsa
1430193045Sthompsastatic usb_error_t
1431188417Sthompsarum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len)
1432188417Sthompsa{
1433192984Sthompsa	struct usb_device_request req;
1434193045Sthompsa	usb_error_t error;
1435235000Shselasky	size_t offset;
1436188417Sthompsa
1437188417Sthompsa	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1438188417Sthompsa	req.bRequest = RT2573_WRITE_MULTI_MAC;
1439188417Sthompsa	USETW(req.wValue, 0);
1440188417Sthompsa
1441229111Shselasky	/* write at most 64 bytes at a time */
1442229111Shselasky	for (offset = 0; offset < len; offset += 64) {
1443229111Shselasky		USETW(req.wIndex, reg + offset);
1444229111Shselasky		USETW(req.wLength, MIN(len - offset, 64));
1445229111Shselasky
1446229111Shselasky		error = rum_do_request(sc, &req, (char *)buf + offset);
1447229111Shselasky		if (error != 0) {
1448229111Shselasky			device_printf(sc->sc_dev,
1449229111Shselasky			    "could not multi write MAC register: %s\n",
1450229111Shselasky			    usbd_errstr(error));
1451229111Shselasky			return (error);
1452229111Shselasky		}
1453184610Salfred	}
1454229111Shselasky
1455229111Shselasky	return (USB_ERR_NORMAL_COMPLETION);
1456188417Sthompsa}
1457184610Salfred
1458188417Sthompsastatic void
1459188417Sthompsarum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val)
1460188417Sthompsa{
1461188417Sthompsa	uint32_t tmp;
1462188417Sthompsa	int ntries;
1463188417Sthompsa
1464189123Sthompsa	DPRINTFN(2, "reg=0x%08x\n", reg);
1465189123Sthompsa
1466188619Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
1467188417Sthompsa		if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1468188417Sthompsa			break;
1469188619Sthompsa		if (rum_pause(sc, hz / 100))
1470188619Sthompsa			break;
1471188417Sthompsa	}
1472188619Sthompsa	if (ntries == 100) {
1473188417Sthompsa		device_printf(sc->sc_dev, "could not write to BBP\n");
1474188417Sthompsa		return;
1475188417Sthompsa	}
1476188417Sthompsa
1477188417Sthompsa	tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val;
1478188417Sthompsa	rum_write(sc, RT2573_PHY_CSR3, tmp);
1479184610Salfred}
1480184610Salfred
1481188417Sthompsastatic uint8_t
1482188417Sthompsarum_bbp_read(struct rum_softc *sc, uint8_t reg)
1483184610Salfred{
1484188417Sthompsa	uint32_t val;
1485188417Sthompsa	int ntries;
1486184610Salfred
1487189123Sthompsa	DPRINTFN(2, "reg=0x%08x\n", reg);
1488189123Sthompsa
1489188619Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
1490188417Sthompsa		if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1491188417Sthompsa			break;
1492188619Sthompsa		if (rum_pause(sc, hz / 100))
1493188619Sthompsa			break;
1494184610Salfred	}
1495188619Sthompsa	if (ntries == 100) {
1496188417Sthompsa		device_printf(sc->sc_dev, "could not read BBP\n");
1497188417Sthompsa		return 0;
1498184610Salfred	}
1499184610Salfred
1500188417Sthompsa	val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8;
1501188417Sthompsa	rum_write(sc, RT2573_PHY_CSR3, val);
1502184610Salfred
1503188417Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
1504188417Sthompsa		val = rum_read(sc, RT2573_PHY_CSR3);
1505188417Sthompsa		if (!(val & RT2573_BBP_BUSY))
1506188417Sthompsa			return val & 0xff;
1507188619Sthompsa		if (rum_pause(sc, hz / 100))
1508188619Sthompsa			break;
1509188417Sthompsa	}
1510184610Salfred
1511188417Sthompsa	device_printf(sc->sc_dev, "could not read BBP\n");
1512188417Sthompsa	return 0;
1513184610Salfred}
1514184610Salfred
1515184610Salfredstatic void
1516188417Sthompsarum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val)
1517184610Salfred{
1518188417Sthompsa	uint32_t tmp;
1519188417Sthompsa	int ntries;
1520184610Salfred
1521188619Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
1522188417Sthompsa		if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY))
1523188417Sthompsa			break;
1524188619Sthompsa		if (rum_pause(sc, hz / 100))
1525188619Sthompsa			break;
1526188417Sthompsa	}
1527188619Sthompsa	if (ntries == 100) {
1528188417Sthompsa		device_printf(sc->sc_dev, "could not write to RF\n");
1529188417Sthompsa		return;
1530188417Sthompsa	}
1531184610Salfred
1532196970Sphk	tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 |
1533188417Sthompsa	    (reg & 3);
1534188417Sthompsa	rum_write(sc, RT2573_PHY_CSR4, tmp);
1535184610Salfred
1536188417Sthompsa	/* remember last written value in sc */
1537188417Sthompsa	sc->rf_regs[reg] = val;
1538184610Salfred
1539188417Sthompsa	DPRINTFN(15, "RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff);
1540184610Salfred}
1541184610Salfred
1542184610Salfredstatic void
1543188417Sthompsarum_select_antenna(struct rum_softc *sc)
1544184610Salfred{
1545188417Sthompsa	uint8_t bbp4, bbp77;
1546188417Sthompsa	uint32_t tmp;
1547188417Sthompsa
1548188417Sthompsa	bbp4  = rum_bbp_read(sc, 4);
1549188417Sthompsa	bbp77 = rum_bbp_read(sc, 77);
1550188417Sthompsa
1551188417Sthompsa	/* TBD */
1552188417Sthompsa
1553188417Sthompsa	/* make sure Rx is disabled before switching antenna */
1554188417Sthompsa	tmp = rum_read(sc, RT2573_TXRX_CSR0);
1555188417Sthompsa	rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
1556188417Sthompsa
1557188417Sthompsa	rum_bbp_write(sc,  4, bbp4);
1558188417Sthompsa	rum_bbp_write(sc, 77, bbp77);
1559188417Sthompsa
1560188417Sthompsa	rum_write(sc, RT2573_TXRX_CSR0, tmp);
1561184610Salfred}
1562184610Salfred
1563188417Sthompsa/*
1564188417Sthompsa * Enable multi-rate retries for frames sent at OFDM rates.
1565188417Sthompsa * In 802.11b/g mode, allow fallback to CCK rates.
1566188417Sthompsa */
1567184610Salfredstatic void
1568188417Sthompsarum_enable_mrr(struct rum_softc *sc)
1569184610Salfred{
1570188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
1571188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
1572188417Sthompsa	uint32_t tmp;
1573188417Sthompsa
1574188417Sthompsa	tmp = rum_read(sc, RT2573_TXRX_CSR4);
1575188417Sthompsa
1576188417Sthompsa	tmp &= ~RT2573_MRR_CCK_FALLBACK;
1577188417Sthompsa	if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan))
1578188417Sthompsa		tmp |= RT2573_MRR_CCK_FALLBACK;
1579188417Sthompsa	tmp |= RT2573_MRR_ENABLED;
1580188417Sthompsa
1581188417Sthompsa	rum_write(sc, RT2573_TXRX_CSR4, tmp);
1582184610Salfred}
1583184610Salfred
1584184610Salfredstatic void
1585188417Sthompsarum_set_txpreamble(struct rum_softc *sc)
1586184610Salfred{
1587188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
1588188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
1589188417Sthompsa	uint32_t tmp;
1590188417Sthompsa
1591188417Sthompsa	tmp = rum_read(sc, RT2573_TXRX_CSR4);
1592188417Sthompsa
1593188417Sthompsa	tmp &= ~RT2573_SHORT_PREAMBLE;
1594188417Sthompsa	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
1595188417Sthompsa		tmp |= RT2573_SHORT_PREAMBLE;
1596188417Sthompsa
1597188417Sthompsa	rum_write(sc, RT2573_TXRX_CSR4, tmp);
1598184610Salfred}
1599184610Salfred
1600184610Salfredstatic void
1601188417Sthompsarum_set_basicrates(struct rum_softc *sc)
1602184610Salfred{
1603188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
1604188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
1605184610Salfred
1606188417Sthompsa	/* update basic rate set */
1607188417Sthompsa	if (ic->ic_curmode == IEEE80211_MODE_11B) {
1608188417Sthompsa		/* 11b basic rates: 1, 2Mbps */
1609188417Sthompsa		rum_write(sc, RT2573_TXRX_CSR5, 0x3);
1610188417Sthompsa	} else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) {
1611188417Sthompsa		/* 11a basic rates: 6, 12, 24Mbps */
1612188417Sthompsa		rum_write(sc, RT2573_TXRX_CSR5, 0x150);
1613188417Sthompsa	} else {
1614188417Sthompsa		/* 11b/g basic rates: 1, 2, 5.5, 11Mbps */
1615188417Sthompsa		rum_write(sc, RT2573_TXRX_CSR5, 0xf);
1616188417Sthompsa	}
1617184610Salfred}
1618184610Salfred
1619184610Salfred/*
1620188417Sthompsa * Reprogram MAC/BBP to switch to a new band.  Values taken from the reference
1621184610Salfred * driver.
1622184610Salfred */
1623184610Salfredstatic void
1624188417Sthompsarum_select_band(struct rum_softc *sc, struct ieee80211_channel *c)
1625184610Salfred{
1626188417Sthompsa	uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104;
1627184610Salfred	uint32_t tmp;
1628184610Salfred
1629184610Salfred	/* update all BBP registers that depend on the band */
1630188417Sthompsa	bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c;
1631188417Sthompsa	bbp35 = 0x50; bbp97 = 0x48; bbp98  = 0x48;
1632188417Sthompsa	if (IEEE80211_IS_CHAN_5GHZ(c)) {
1633188417Sthompsa		bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c;
1634188417Sthompsa		bbp35 += 0x10; bbp97 += 0x10; bbp98  += 0x10;
1635184610Salfred	}
1636188417Sthompsa	if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1637188417Sthompsa	    (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1638188417Sthompsa		bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10;
1639184610Salfred	}
1640184610Salfred
1641188417Sthompsa	sc->bbp17 = bbp17;
1642188417Sthompsa	rum_bbp_write(sc,  17, bbp17);
1643188417Sthompsa	rum_bbp_write(sc,  96, bbp96);
1644188417Sthompsa	rum_bbp_write(sc, 104, bbp104);
1645188417Sthompsa
1646188417Sthompsa	if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1647188417Sthompsa	    (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1648188417Sthompsa		rum_bbp_write(sc, 75, 0x80);
1649188417Sthompsa		rum_bbp_write(sc, 86, 0x80);
1650188417Sthompsa		rum_bbp_write(sc, 88, 0x80);
1651184610Salfred	}
1652184610Salfred
1653188417Sthompsa	rum_bbp_write(sc, 35, bbp35);
1654188417Sthompsa	rum_bbp_write(sc, 97, bbp97);
1655188417Sthompsa	rum_bbp_write(sc, 98, bbp98);
1656188417Sthompsa
1657188417Sthompsa	tmp = rum_read(sc, RT2573_PHY_CSR0);
1658184610Salfred	tmp &= ~(RT2573_PA_PE_2GHZ | RT2573_PA_PE_5GHZ);
1659188417Sthompsa	if (IEEE80211_IS_CHAN_2GHZ(c))
1660184610Salfred		tmp |= RT2573_PA_PE_2GHZ;
1661184610Salfred	else
1662184610Salfred		tmp |= RT2573_PA_PE_5GHZ;
1663188417Sthompsa	rum_write(sc, RT2573_PHY_CSR0, tmp);
1664184610Salfred}
1665184610Salfred
1666184610Salfredstatic void
1667188417Sthompsarum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c)
1668184610Salfred{
1669188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
1670188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
1671184610Salfred	const struct rfprog *rfprog;
1672188417Sthompsa	uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT;
1673184610Salfred	int8_t power;
1674188619Sthompsa	int i, chan;
1675184610Salfred
1676188417Sthompsa	chan = ieee80211_chan2ieee(ic, c);
1677188417Sthompsa	if (chan == 0 || chan == IEEE80211_CHAN_ANY)
1678184610Salfred		return;
1679184610Salfred
1680184610Salfred	/* select the appropriate RF settings based on what EEPROM says */
1681188417Sthompsa	rfprog = (sc->rf_rev == RT2573_RF_5225 ||
1682188417Sthompsa		  sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226;
1683184610Salfred
1684188417Sthompsa	/* find the settings for this channel (we know it exists) */
1685188417Sthompsa	for (i = 0; rfprog[i].chan != chan; i++);
1686184610Salfred
1687188417Sthompsa	power = sc->txpow[i];
1688184610Salfred	if (power < 0) {
1689184610Salfred		bbp94 += power;
1690184610Salfred		power = 0;
1691184610Salfred	} else if (power > 31) {
1692184610Salfred		bbp94 += power - 31;
1693184610Salfred		power = 31;
1694184610Salfred	}
1695188417Sthompsa
1696184610Salfred	/*
1697184610Salfred	 * If we are switching from the 2GHz band to the 5GHz band or
1698184610Salfred	 * vice-versa, BBP registers need to be reprogrammed.
1699184610Salfred	 */
1700188417Sthompsa	if (c->ic_flags != ic->ic_curchan->ic_flags) {
1701188417Sthompsa		rum_select_band(sc, c);
1702188417Sthompsa		rum_select_antenna(sc);
1703188417Sthompsa	}
1704188417Sthompsa	ic->ic_curchan = c;
1705184610Salfred
1706188417Sthompsa	rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1707188417Sthompsa	rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1708188417Sthompsa	rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1709188417Sthompsa	rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1710184610Salfred
1711188417Sthompsa	rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1712188417Sthompsa	rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1713188417Sthompsa	rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7 | 1);
1714188417Sthompsa	rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1715184610Salfred
1716188417Sthompsa	rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1717188417Sthompsa	rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1718188417Sthompsa	rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1719188417Sthompsa	rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1720184610Salfred
1721188619Sthompsa	rum_pause(sc, hz / 100);
1722188417Sthompsa
1723184610Salfred	/* enable smart mode for MIMO-capable RFs */
1724188417Sthompsa	bbp3 = rum_bbp_read(sc, 3);
1725184610Salfred
1726188417Sthompsa	bbp3 &= ~RT2573_SMART_MODE;
1727188417Sthompsa	if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527)
1728184610Salfred		bbp3 |= RT2573_SMART_MODE;
1729184610Salfred
1730188417Sthompsa	rum_bbp_write(sc, 3, bbp3);
1731184610Salfred
1732188417Sthompsa	if (bbp94 != RT2573_BBPR94_DEFAULT)
1733188417Sthompsa		rum_bbp_write(sc, 94, bbp94);
1734189123Sthompsa
1735189123Sthompsa	/* give the chip some extra time to do the switchover */
1736189123Sthompsa	rum_pause(sc, hz / 100);
1737184610Salfred}
1738184610Salfred
1739188417Sthompsa/*
1740188417Sthompsa * Enable TSF synchronization and tell h/w to start sending beacons for IBSS
1741188417Sthompsa * and HostAP operating modes.
1742188417Sthompsa */
1743184610Salfredstatic void
1744188417Sthompsarum_enable_tsf_sync(struct rum_softc *sc)
1745184610Salfred{
1746188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
1747188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
1748188417Sthompsa	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1749184610Salfred	uint32_t tmp;
1750184610Salfred
1751188417Sthompsa	if (vap->iv_opmode != IEEE80211_M_STA) {
1752184610Salfred		/*
1753184610Salfred		 * Change default 16ms TBTT adjustment to 8ms.
1754184610Salfred		 * Must be done before enabling beacon generation.
1755184610Salfred		 */
1756188417Sthompsa		rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8);
1757184610Salfred	}
1758184610Salfred
1759188417Sthompsa	tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000;
1760188417Sthompsa
1761184610Salfred	/* set beacon interval (in 1/16ms unit) */
1762188417Sthompsa	tmp |= vap->iv_bss->ni_intval * 16;
1763184610Salfred
1764184610Salfred	tmp |= RT2573_TSF_TICKING | RT2573_ENABLE_TBTT;
1765188417Sthompsa	if (vap->iv_opmode == IEEE80211_M_STA)
1766184610Salfred		tmp |= RT2573_TSF_MODE(1);
1767184610Salfred	else
1768184610Salfred		tmp |= RT2573_TSF_MODE(2) | RT2573_GENERATE_BEACON;
1769184610Salfred
1770188417Sthompsa	rum_write(sc, RT2573_TXRX_CSR9, tmp);
1771184610Salfred}
1772184610Salfred
1773184610Salfredstatic void
1774192468Ssamrum_enable_tsf(struct rum_softc *sc)
1775192468Ssam{
1776192468Ssam	rum_write(sc, RT2573_TXRX_CSR9,
1777192468Ssam	    (rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000) |
1778192468Ssam	    RT2573_TSF_TICKING | RT2573_TSF_MODE(2));
1779192468Ssam}
1780192468Ssam
1781192468Ssamstatic void
1782188417Sthompsarum_update_slot(struct ifnet *ifp)
1783184610Salfred{
1784188417Sthompsa	struct rum_softc *sc = ifp->if_softc;
1785188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
1786188417Sthompsa	uint8_t slottime;
1787184610Salfred	uint32_t tmp;
1788184610Salfred
1789188417Sthompsa	slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
1790184610Salfred
1791188417Sthompsa	tmp = rum_read(sc, RT2573_MAC_CSR9);
1792184610Salfred	tmp = (tmp & ~0xff) | slottime;
1793188417Sthompsa	rum_write(sc, RT2573_MAC_CSR9, tmp);
1794184610Salfred
1795188417Sthompsa	DPRINTF("setting slot time to %uus\n", slottime);
1796184610Salfred}
1797184610Salfred
1798184610Salfredstatic void
1799188417Sthompsarum_set_bssid(struct rum_softc *sc, const uint8_t *bssid)
1800184610Salfred{
1801184610Salfred	uint32_t tmp;
1802184610Salfred
1803188417Sthompsa	tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24;
1804188417Sthompsa	rum_write(sc, RT2573_MAC_CSR4, tmp);
1805184610Salfred
1806188417Sthompsa	tmp = bssid[4] | bssid[5] << 8 | RT2573_ONE_BSSID << 16;
1807188417Sthompsa	rum_write(sc, RT2573_MAC_CSR5, tmp);
1808184610Salfred}
1809184610Salfred
1810184610Salfredstatic void
1811188417Sthompsarum_set_macaddr(struct rum_softc *sc, const uint8_t *addr)
1812184610Salfred{
1813184610Salfred	uint32_t tmp;
1814184610Salfred
1815188417Sthompsa	tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24;
1816188417Sthompsa	rum_write(sc, RT2573_MAC_CSR2, tmp);
1817184610Salfred
1818188417Sthompsa	tmp = addr[4] | addr[5] << 8 | 0xff << 16;
1819188417Sthompsa	rum_write(sc, RT2573_MAC_CSR3, tmp);
1820184610Salfred}
1821184610Salfred
1822184610Salfredstatic void
1823191746Sthompsarum_setpromisc(struct rum_softc *sc)
1824184610Salfred{
1825188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
1826184610Salfred	uint32_t tmp;
1827184610Salfred
1828188417Sthompsa	tmp = rum_read(sc, RT2573_TXRX_CSR0);
1829184610Salfred
1830188417Sthompsa	tmp &= ~RT2573_DROP_NOT_TO_ME;
1831188417Sthompsa	if (!(ifp->if_flags & IFF_PROMISC))
1832184610Salfred		tmp |= RT2573_DROP_NOT_TO_ME;
1833184610Salfred
1834188417Sthompsa	rum_write(sc, RT2573_TXRX_CSR0, tmp);
1835184610Salfred
1836188417Sthompsa	DPRINTF("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
1837184610Salfred	    "entering" : "leaving");
1838184610Salfred}
1839184610Salfred
1840189123Sthompsastatic void
1841189123Sthompsarum_update_promisc(struct ifnet *ifp)
1842189123Sthompsa{
1843189123Sthompsa	struct rum_softc *sc = ifp->if_softc;
1844189123Sthompsa
1845189123Sthompsa	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1846189123Sthompsa		return;
1847189123Sthompsa
1848189123Sthompsa	RUM_LOCK(sc);
1849191746Sthompsa	rum_setpromisc(sc);
1850189123Sthompsa	RUM_UNLOCK(sc);
1851189123Sthompsa}
1852189123Sthompsa
1853207768Semastestatic void
1854207768Semasterum_update_mcast(struct ifnet *ifp)
1855207768Semaste{
1856213537Semaste	static int warning_printed;
1857207768Semaste
1858213537Semaste	if (warning_printed == 0) {
1859213537Semaste		if_printf(ifp, "need to implement %s\n", __func__);
1860213537Semaste		warning_printed = 1;
1861213537Semaste	}
1862207768Semaste}
1863207768Semaste
1864188417Sthompsastatic const char *
1865188417Sthompsarum_get_rf(int rev)
1866184610Salfred{
1867188417Sthompsa	switch (rev) {
1868188417Sthompsa	case RT2573_RF_2527:	return "RT2527 (MIMO XR)";
1869188417Sthompsa	case RT2573_RF_2528:	return "RT2528";
1870188417Sthompsa	case RT2573_RF_5225:	return "RT5225 (MIMO XR)";
1871188417Sthompsa	case RT2573_RF_5226:	return "RT5226";
1872188417Sthompsa	default:		return "unknown";
1873184610Salfred	}
1874184610Salfred}
1875184610Salfred
1876184610Salfredstatic void
1877188417Sthompsarum_read_eeprom(struct rum_softc *sc)
1878184610Salfred{
1879184610Salfred	uint16_t val;
1880188417Sthompsa#ifdef RUM_DEBUG
1881188417Sthompsa	int i;
1882188417Sthompsa#endif
1883184610Salfred
1884184610Salfred	/* read MAC address */
1885188419Sthompsa	rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, sc->sc_bssid, 6);
1886184610Salfred
1887188417Sthompsa	rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2);
1888188417Sthompsa	val = le16toh(val);
1889188417Sthompsa	sc->rf_rev =   (val >> 11) & 0x1f;
1890188417Sthompsa	sc->hw_radio = (val >> 10) & 0x1;
1891188417Sthompsa	sc->rx_ant =   (val >> 4)  & 0x3;
1892188417Sthompsa	sc->tx_ant =   (val >> 2)  & 0x3;
1893188417Sthompsa	sc->nb_ant =   val & 0x3;
1894184610Salfred
1895188417Sthompsa	DPRINTF("RF revision=%d\n", sc->rf_rev);
1896184610Salfred
1897188417Sthompsa	rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2);
1898188417Sthompsa	val = le16toh(val);
1899188417Sthompsa	sc->ext_5ghz_lna = (val >> 6) & 0x1;
1900188417Sthompsa	sc->ext_2ghz_lna = (val >> 4) & 0x1;
1901184610Salfred
1902188417Sthompsa	DPRINTF("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n",
1903188417Sthompsa	    sc->ext_2ghz_lna, sc->ext_5ghz_lna);
1904184610Salfred
1905188417Sthompsa	rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2);
1906188417Sthompsa	val = le16toh(val);
1907184610Salfred	if ((val & 0xff) != 0xff)
1908188417Sthompsa		sc->rssi_2ghz_corr = (int8_t)(val & 0xff);	/* signed */
1909184610Salfred
1910188417Sthompsa	/* Only [-10, 10] is valid */
1911188417Sthompsa	if (sc->rssi_2ghz_corr < -10 || sc->rssi_2ghz_corr > 10)
1912188417Sthompsa		sc->rssi_2ghz_corr = 0;
1913188417Sthompsa
1914188417Sthompsa	rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2);
1915188417Sthompsa	val = le16toh(val);
1916184610Salfred	if ((val & 0xff) != 0xff)
1917188417Sthompsa		sc->rssi_5ghz_corr = (int8_t)(val & 0xff);	/* signed */
1918184610Salfred
1919188417Sthompsa	/* Only [-10, 10] is valid */
1920188417Sthompsa	if (sc->rssi_5ghz_corr < -10 || sc->rssi_5ghz_corr > 10)
1921188417Sthompsa		sc->rssi_5ghz_corr = 0;
1922184610Salfred
1923188417Sthompsa	if (sc->ext_2ghz_lna)
1924188417Sthompsa		sc->rssi_2ghz_corr -= 14;
1925188417Sthompsa	if (sc->ext_5ghz_lna)
1926188417Sthompsa		sc->rssi_5ghz_corr -= 14;
1927188417Sthompsa
1928188417Sthompsa	DPRINTF("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n",
1929188417Sthompsa	    sc->rssi_2ghz_corr, sc->rssi_5ghz_corr);
1930188417Sthompsa
1931188417Sthompsa	rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2);
1932188417Sthompsa	val = le16toh(val);
1933184610Salfred	if ((val & 0xff) != 0xff)
1934188417Sthompsa		sc->rffreq = val & 0xff;
1935184610Salfred
1936188417Sthompsa	DPRINTF("RF freq=%d\n", sc->rffreq);
1937184610Salfred
1938184610Salfred	/* read Tx power for all a/b/g channels */
1939188417Sthompsa	rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14);
1940184610Salfred	/* XXX default Tx power for 802.11a channels */
1941188417Sthompsa	memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14);
1942188417Sthompsa#ifdef RUM_DEBUG
1943188417Sthompsa	for (i = 0; i < 14; i++)
1944188417Sthompsa		DPRINTF("Channel=%d Tx power=%d\n", i + 1,  sc->txpow[i]);
1945188417Sthompsa#endif
1946184610Salfred
1947184610Salfred	/* read default values for BBP registers */
1948188417Sthompsa	rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
1949188417Sthompsa#ifdef RUM_DEBUG
1950188417Sthompsa	for (i = 0; i < 14; i++) {
1951188417Sthompsa		if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
1952188417Sthompsa			continue;
1953188417Sthompsa		DPRINTF("BBP R%d=%02x\n", sc->bbp_prom[i].reg,
1954188417Sthompsa		    sc->bbp_prom[i].val);
1955188417Sthompsa	}
1956188417Sthompsa#endif
1957184610Salfred}
1958184610Salfred
1959188417Sthompsastatic int
1960188417Sthompsarum_bbp_init(struct rum_softc *sc)
1961184610Salfred{
1962188417Sthompsa	int i, ntries;
1963184610Salfred
1964188417Sthompsa	/* wait for BBP to be ready */
1965188417Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
1966188417Sthompsa		const uint8_t val = rum_bbp_read(sc, 0);
1967188417Sthompsa		if (val != 0 && val != 0xff)
1968188417Sthompsa			break;
1969188619Sthompsa		if (rum_pause(sc, hz / 100))
1970188619Sthompsa			break;
1971184610Salfred	}
1972188417Sthompsa	if (ntries == 100) {
1973188417Sthompsa		device_printf(sc->sc_dev, "timeout waiting for BBP\n");
1974188417Sthompsa		return EIO;
1975188417Sthompsa	}
1976184610Salfred
1977184610Salfred	/* initialize BBP registers to default values */
1978188417Sthompsa	for (i = 0; i < N(rum_def_bbp); i++)
1979188417Sthompsa		rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val);
1980184610Salfred
1981184610Salfred	/* write vendor-specific BBP values (from EEPROM) */
1982184610Salfred	for (i = 0; i < 16; i++) {
1983188417Sthompsa		if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
1984184610Salfred			continue;
1985188417Sthompsa		rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
1986184610Salfred	}
1987188417Sthompsa
1988188417Sthompsa	return 0;
1989184610Salfred}
1990184610Salfred
1991184610Salfredstatic void
1992191746Sthompsarum_init_locked(struct rum_softc *sc)
1993184610Salfred{
1994184610Salfred	struct ifnet *ifp = sc->sc_ifp;
1995184610Salfred	struct ieee80211com *ic = ifp->if_l2com;
1996184610Salfred	uint32_t tmp;
1997193045Sthompsa	usb_error_t error;
1998188417Sthompsa	int i, ntries;
1999184610Salfred
2000188417Sthompsa	RUM_LOCK_ASSERT(sc, MA_OWNED);
2001184610Salfred
2002191746Sthompsa	rum_stop(sc);
2003184610Salfred
2004184610Salfred	/* initialize MAC registers to default values */
2005188417Sthompsa	for (i = 0; i < N(rum_def_mac); i++)
2006188417Sthompsa		rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val);
2007184610Salfred
2008184610Salfred	/* set host ready */
2009188417Sthompsa	rum_write(sc, RT2573_MAC_CSR1, 3);
2010188417Sthompsa	rum_write(sc, RT2573_MAC_CSR1, 0);
2011184610Salfred
2012184610Salfred	/* wait for BBP/RF to wakeup */
2013188619Sthompsa	for (ntries = 0; ntries < 100; ntries++) {
2014188417Sthompsa		if (rum_read(sc, RT2573_MAC_CSR12) & 8)
2015188417Sthompsa			break;
2016188417Sthompsa		rum_write(sc, RT2573_MAC_CSR12, 4);	/* force wakeup */
2017188619Sthompsa		if (rum_pause(sc, hz / 100))
2018188619Sthompsa			break;
2019184610Salfred	}
2020188619Sthompsa	if (ntries == 100) {
2021188417Sthompsa		device_printf(sc->sc_dev,
2022188417Sthompsa		    "timeout waiting for BBP/RF to wakeup\n");
2023184610Salfred		goto fail;
2024184610Salfred	}
2025184610Salfred
2026188417Sthompsa	if ((error = rum_bbp_init(sc)) != 0)
2027188417Sthompsa		goto fail;
2028184610Salfred
2029188417Sthompsa	/* select default channel */
2030188417Sthompsa	rum_select_band(sc, ic->ic_curchan);
2031188417Sthompsa	rum_select_antenna(sc);
2032188417Sthompsa	rum_set_chan(sc, ic->ic_curchan);
2033184610Salfred
2034184610Salfred	/* clear STA registers */
2035188417Sthompsa	rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
2036184610Salfred
2037190526Ssam	rum_set_macaddr(sc, IF_LLADDR(ifp));
2038188417Sthompsa
2039184610Salfred	/* initialize ASIC */
2040188417Sthompsa	rum_write(sc, RT2573_MAC_CSR1, 4);
2041184610Salfred
2042184610Salfred	/*
2043188417Sthompsa	 * Allocate Tx and Rx xfer queues.
2044184610Salfred	 */
2045188419Sthompsa	rum_setup_tx_list(sc);
2046184610Salfred
2047184610Salfred	/* update Rx filter */
2048188417Sthompsa	tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff;
2049184610Salfred
2050184610Salfred	tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR;
2051188417Sthompsa	if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2052184610Salfred		tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR |
2053188417Sthompsa		       RT2573_DROP_ACKCTS;
2054188417Sthompsa		if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2055184610Salfred			tmp |= RT2573_DROP_TODS;
2056188417Sthompsa		if (!(ifp->if_flags & IFF_PROMISC))
2057184610Salfred			tmp |= RT2573_DROP_NOT_TO_ME;
2058184610Salfred	}
2059188417Sthompsa	rum_write(sc, RT2573_TXRX_CSR0, tmp);
2060184610Salfred
2061188417Sthompsa	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2062188417Sthompsa	ifp->if_drv_flags |= IFF_DRV_RUNNING;
2063194677Sthompsa	usbd_xfer_set_stall(sc->sc_xfer[RUM_BULK_WR]);
2064194228Sthompsa	usbd_transfer_start(sc->sc_xfer[RUM_BULK_RD]);
2065184610Salfred	return;
2066184610Salfred
2067191746Sthompsafail:	rum_stop(sc);
2068188417Sthompsa#undef N
2069184610Salfred}
2070184610Salfred
2071184610Salfredstatic void
2072188417Sthompsarum_init(void *priv)
2073184610Salfred{
2074188417Sthompsa	struct rum_softc *sc = priv;
2075184610Salfred	struct ifnet *ifp = sc->sc_ifp;
2076188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
2077184610Salfred
2078188417Sthompsa	RUM_LOCK(sc);
2079191746Sthompsa	rum_init_locked(sc);
2080188417Sthompsa	RUM_UNLOCK(sc);
2081184610Salfred
2082188417Sthompsa	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2083188417Sthompsa		ieee80211_start_all(ic);		/* start all vap's */
2084184610Salfred}
2085184610Salfred
2086184610Salfredstatic void
2087191746Sthompsarum_stop(struct rum_softc *sc)
2088184610Salfred{
2089188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2090184610Salfred	uint32_t tmp;
2091184610Salfred
2092188417Sthompsa	RUM_LOCK_ASSERT(sc, MA_OWNED);
2093184610Salfred
2094188417Sthompsa	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2095184610Salfred
2096188419Sthompsa	RUM_UNLOCK(sc);
2097188419Sthompsa
2098188417Sthompsa	/*
2099188419Sthompsa	 * Drain the USB transfers, if not already drained:
2100188417Sthompsa	 */
2101194228Sthompsa	usbd_transfer_drain(sc->sc_xfer[RUM_BULK_WR]);
2102194228Sthompsa	usbd_transfer_drain(sc->sc_xfer[RUM_BULK_RD]);
2103184610Salfred
2104188419Sthompsa	RUM_LOCK(sc);
2105184610Salfred
2106188419Sthompsa	rum_unsetup_tx_list(sc);
2107184610Salfred
2108188417Sthompsa	/* disable Rx */
2109188417Sthompsa	tmp = rum_read(sc, RT2573_TXRX_CSR0);
2110188417Sthompsa	rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
2111184610Salfred
2112188417Sthompsa	/* reset ASIC */
2113188417Sthompsa	rum_write(sc, RT2573_MAC_CSR1, 3);
2114188417Sthompsa	rum_write(sc, RT2573_MAC_CSR1, 0);
2115184610Salfred}
2116184610Salfred
2117189123Sthompsastatic void
2118189123Sthompsarum_load_microcode(struct rum_softc *sc, const uint8_t *ucode, size_t size)
2119184610Salfred{
2120192984Sthompsa	struct usb_device_request req;
2121184610Salfred	uint16_t reg = RT2573_MCU_CODE_BASE;
2122193045Sthompsa	usb_error_t err;
2123184610Salfred
2124184610Salfred	/* copy firmware image into NIC */
2125189123Sthompsa	for (; size >= 4; reg += 4, ucode += 4, size -= 4) {
2126189123Sthompsa		err = rum_write(sc, reg, UGETDW(ucode));
2127189123Sthompsa		if (err) {
2128189123Sthompsa			/* firmware already loaded ? */
2129189123Sthompsa			device_printf(sc->sc_dev, "Firmware load "
2130189123Sthompsa			    "failure! (ignored)\n");
2131189123Sthompsa			break;
2132189123Sthompsa		}
2133189123Sthompsa	}
2134184610Salfred
2135184610Salfred	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2136184610Salfred	req.bRequest = RT2573_MCU_CNTL;
2137184610Salfred	USETW(req.wValue, RT2573_MCU_RUN);
2138184610Salfred	USETW(req.wIndex, 0);
2139184610Salfred	USETW(req.wLength, 0);
2140184610Salfred
2141189123Sthompsa	err = rum_do_request(sc, &req, NULL);
2142189123Sthompsa	if (err != 0) {
2143188417Sthompsa		device_printf(sc->sc_dev, "could not run firmware: %s\n",
2144194228Sthompsa		    usbd_errstr(err));
2145188417Sthompsa	}
2146189123Sthompsa
2147189123Sthompsa	/* give the chip some time to boot */
2148189123Sthompsa	rum_pause(sc, hz / 8);
2149184610Salfred}
2150184610Salfred
2151221199Skevlostatic void
2152188417Sthompsarum_prepare_beacon(struct rum_softc *sc, struct ieee80211vap *vap)
2153184610Salfred{
2154188417Sthompsa	struct ieee80211com *ic = vap->iv_ic;
2155184610Salfred	const struct ieee80211_txparam *tp;
2156188417Sthompsa	struct rum_tx_desc desc;
2157188417Sthompsa	struct mbuf *m0;
2158184610Salfred
2159221199Skevlo	if (vap->iv_bss->ni_chan == IEEE80211_CHAN_ANYC)
2160221199Skevlo		return;
2161236897Shselasky	if (ic->ic_bsschan == IEEE80211_CHAN_ANYC)
2162236897Shselasky		return;
2163221199Skevlo
2164188417Sthompsa	m0 = ieee80211_beacon_alloc(vap->iv_bss, &RUM_VAP(vap)->bo);
2165236897Shselasky	if (m0 == NULL)
2166221199Skevlo		return;
2167184610Salfred
2168184610Salfred	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)];
2169188417Sthompsa	rum_setup_tx_desc(sc, &desc, RT2573_TX_TIMESTAMP, RT2573_TX_HWSEQ,
2170188417Sthompsa	    m0->m_pkthdr.len, tp->mgmtrate);
2171184610Salfred
2172188417Sthompsa	/* copy the first 24 bytes of Tx descriptor into NIC memory */
2173188417Sthompsa	rum_write_multi(sc, RT2573_HW_BEACON_BASE0, (uint8_t *)&desc, 24);
2174188417Sthompsa
2175188417Sthompsa	/* copy beacon header and payload into NIC memory */
2176188417Sthompsa	rum_write_multi(sc, RT2573_HW_BEACON_BASE0 + 24, mtod(m0, uint8_t *),
2177188417Sthompsa	    m0->m_pkthdr.len);
2178188417Sthompsa
2179188417Sthompsa	m_freem(m0);
2180184610Salfred}
2181184610Salfred
2182188417Sthompsastatic int
2183188417Sthompsarum_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2184188417Sthompsa    const struct ieee80211_bpf_params *params)
2185184610Salfred{
2186188417Sthompsa	struct ifnet *ifp = ni->ni_ic->ic_ifp;
2187188417Sthompsa	struct rum_softc *sc = ifp->if_softc;
2188184610Salfred
2189188417Sthompsa	RUM_LOCK(sc);
2190188417Sthompsa	/* prevent management frames from being sent if we're not ready */
2191188417Sthompsa	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2192188417Sthompsa		RUM_UNLOCK(sc);
2193188417Sthompsa		m_freem(m);
2194188417Sthompsa		ieee80211_free_node(ni);
2195188417Sthompsa		return ENETDOWN;
2196184610Salfred	}
2197188969Sthompsa	if (sc->tx_nfree < RUM_TX_MINFREE) {
2198188417Sthompsa		ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2199188417Sthompsa		RUM_UNLOCK(sc);
2200188417Sthompsa		m_freem(m);
2201188417Sthompsa		ieee80211_free_node(ni);
2202188417Sthompsa		return EIO;
2203188417Sthompsa	}
2204184610Salfred
2205188417Sthompsa	ifp->if_opackets++;
2206184610Salfred
2207188417Sthompsa	if (params == NULL) {
2208188417Sthompsa		/*
2209188417Sthompsa		 * Legacy path; interpret frame contents to decide
2210188417Sthompsa		 * precisely how to send the frame.
2211188417Sthompsa		 */
2212188417Sthompsa		if (rum_tx_mgt(sc, m, ni) != 0)
2213188417Sthompsa			goto bad;
2214184610Salfred	} else {
2215188417Sthompsa		/*
2216188417Sthompsa		 * Caller supplied explicit parameters to use in
2217188417Sthompsa		 * sending the frame.
2218188417Sthompsa		 */
2219188417Sthompsa		if (rum_tx_raw(sc, m, ni, params) != 0)
2220188417Sthompsa			goto bad;
2221184610Salfred	}
2222188417Sthompsa	RUM_UNLOCK(sc);
2223184610Salfred
2224188417Sthompsa	return 0;
2225188417Sthompsabad:
2226188417Sthompsa	ifp->if_oerrors++;
2227188417Sthompsa	RUM_UNLOCK(sc);
2228188417Sthompsa	ieee80211_free_node(ni);
2229188417Sthompsa	return EIO;
2230184610Salfred}
2231184610Salfred
2232188417Sthompsastatic void
2233206358Srpaulorum_ratectl_start(struct rum_softc *sc, struct ieee80211_node *ni)
2234184610Salfred{
2235188417Sthompsa	struct ieee80211vap *vap = ni->ni_vap;
2236188417Sthompsa	struct rum_vap *rvp = RUM_VAP(vap);
2237184610Salfred
2238188417Sthompsa	/* clear statistic registers (STA_CSR0 to STA_CSR5) */
2239188417Sthompsa	rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
2240184610Salfred
2241206358Srpaulo	usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp);
2242188417Sthompsa}
2243184610Salfred
2244188417Sthompsastatic void
2245206358Srpaulorum_ratectl_timeout(void *arg)
2246188417Sthompsa{
2247188417Sthompsa	struct rum_vap *rvp = arg;
2248191746Sthompsa	struct ieee80211vap *vap = &rvp->vap;
2249191746Sthompsa	struct ieee80211com *ic = vap->iv_ic;
2250184610Salfred
2251206358Srpaulo	ieee80211_runtask(ic, &rvp->ratectl_task);
2252184610Salfred}
2253184610Salfred
2254184610Salfredstatic void
2255206358Srpaulorum_ratectl_task(void *arg, int pending)
2256184610Salfred{
2257191746Sthompsa	struct rum_vap *rvp = arg;
2258191746Sthompsa	struct ieee80211vap *vap = &rvp->vap;
2259191746Sthompsa	struct ieee80211com *ic = vap->iv_ic;
2260191746Sthompsa	struct ifnet *ifp = ic->ic_ifp;
2261191746Sthompsa	struct rum_softc *sc = ifp->if_softc;
2262212127Sthompsa	struct ieee80211_node *ni;
2263188417Sthompsa	int ok, fail;
2264206358Srpaulo	int sum, retrycnt;
2265184610Salfred
2266191746Sthompsa	RUM_LOCK(sc);
2267188417Sthompsa	/* read and clear statistic registers (STA_CSR0 to STA_CSR10) */
2268188417Sthompsa	rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof(sc->sta));
2269184610Salfred
2270188417Sthompsa	ok = (le32toh(sc->sta[4]) >> 16) +	/* TX ok w/o retry */
2271188417Sthompsa	    (le32toh(sc->sta[5]) & 0xffff);	/* TX ok w/ retry */
2272188417Sthompsa	fail = (le32toh(sc->sta[5]) >> 16);	/* TX retry-fail count */
2273206358Srpaulo	sum = ok+fail;
2274206358Srpaulo	retrycnt = (le32toh(sc->sta[5]) & 0xffff) + fail;
2275184610Salfred
2276212127Sthompsa	ni = ieee80211_ref_node(vap->iv_bss);
2277206358Srpaulo	ieee80211_ratectl_tx_update(vap, ni, &sum, &ok, &retrycnt);
2278206358Srpaulo	(void) ieee80211_ratectl_rate(ni, NULL, 0);
2279212127Sthompsa	ieee80211_free_node(ni);
2280188417Sthompsa
2281188417Sthompsa	ifp->if_oerrors += fail;	/* count TX retry-fail as Tx errors */
2282188417Sthompsa
2283206358Srpaulo	usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp);
2284191746Sthompsa	RUM_UNLOCK(sc);
2285184610Salfred}
2286184610Salfred
2287184610Salfredstatic void
2288188417Sthompsarum_scan_start(struct ieee80211com *ic)
2289184610Salfred{
2290191746Sthompsa	struct ifnet *ifp = ic->ic_ifp;
2291191746Sthompsa	struct rum_softc *sc = ifp->if_softc;
2292191746Sthompsa	uint32_t tmp;
2293184610Salfred
2294188417Sthompsa	RUM_LOCK(sc);
2295191746Sthompsa	/* abort TSF synchronization */
2296191746Sthompsa	tmp = rum_read(sc, RT2573_TXRX_CSR9);
2297191746Sthompsa	rum_write(sc, RT2573_TXRX_CSR9, tmp & ~0x00ffffff);
2298191746Sthompsa	rum_set_bssid(sc, ifp->if_broadcastaddr);
2299188417Sthompsa	RUM_UNLOCK(sc);
2300184610Salfred
2301184610Salfred}
2302184610Salfred
2303184610Salfredstatic void
2304188417Sthompsarum_scan_end(struct ieee80211com *ic)
2305184610Salfred{
2306188417Sthompsa	struct rum_softc *sc = ic->ic_ifp->if_softc;
2307184610Salfred
2308188417Sthompsa	RUM_LOCK(sc);
2309191746Sthompsa	rum_enable_tsf_sync(sc);
2310191746Sthompsa	rum_set_bssid(sc, sc->sc_bssid);
2311188417Sthompsa	RUM_UNLOCK(sc);
2312184610Salfred
2313184610Salfred}
2314184610Salfred
2315184610Salfredstatic void
2316188417Sthompsarum_set_channel(struct ieee80211com *ic)
2317184610Salfred{
2318188417Sthompsa	struct rum_softc *sc = ic->ic_ifp->if_softc;
2319184610Salfred
2320188417Sthompsa	RUM_LOCK(sc);
2321191746Sthompsa	rum_set_chan(sc, ic->ic_curchan);
2322188417Sthompsa	RUM_UNLOCK(sc);
2323184610Salfred}
2324184610Salfred
2325188417Sthompsastatic int
2326188417Sthompsarum_get_rssi(struct rum_softc *sc, uint8_t raw)
2327184610Salfred{
2328188417Sthompsa	struct ifnet *ifp = sc->sc_ifp;
2329188417Sthompsa	struct ieee80211com *ic = ifp->if_l2com;
2330188417Sthompsa	int lna, agc, rssi;
2331184610Salfred
2332188417Sthompsa	lna = (raw >> 5) & 0x3;
2333188417Sthompsa	agc = raw & 0x1f;
2334188417Sthompsa
2335188417Sthompsa	if (lna == 0) {
2336188417Sthompsa		/*
2337188417Sthompsa		 * No RSSI mapping
2338188417Sthompsa		 *
2339188417Sthompsa		 * NB: Since RSSI is relative to noise floor, -1 is
2340188417Sthompsa		 *     adequate for caller to know error happened.
2341188417Sthompsa		 */
2342188417Sthompsa		return -1;
2343184610Salfred	}
2344184610Salfred
2345188417Sthompsa	rssi = (2 * agc) - RT2573_NOISE_FLOOR;
2346184610Salfred
2347188417Sthompsa	if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) {
2348188417Sthompsa		rssi += sc->rssi_2ghz_corr;
2349184610Salfred
2350188417Sthompsa		if (lna == 1)
2351188417Sthompsa			rssi -= 64;
2352188417Sthompsa		else if (lna == 2)
2353188417Sthompsa			rssi -= 74;
2354188417Sthompsa		else if (lna == 3)
2355188417Sthompsa			rssi -= 90;
2356188417Sthompsa	} else {
2357188417Sthompsa		rssi += sc->rssi_5ghz_corr;
2358184610Salfred
2359188417Sthompsa		if (!sc->ext_5ghz_lna && lna != 1)
2360188417Sthompsa			rssi += 4;
2361184610Salfred
2362188417Sthompsa		if (lna == 1)
2363188417Sthompsa			rssi -= 64;
2364188417Sthompsa		else if (lna == 2)
2365188417Sthompsa			rssi -= 86;
2366188417Sthompsa		else if (lna == 3)
2367188417Sthompsa			rssi -= 100;
2368184610Salfred	}
2369188417Sthompsa	return rssi;
2370184610Salfred}
2371184610Salfred
2372188619Sthompsastatic int
2373188619Sthompsarum_pause(struct rum_softc *sc, int timeout)
2374188619Sthompsa{
2375188619Sthompsa
2376194228Sthompsa	usb_pause_mtx(&sc->sc_mtx, timeout);
2377188619Sthompsa	return (0);
2378188619Sthompsa}
2379188619Sthompsa
2380188417Sthompsastatic device_method_t rum_methods[] = {
2381188417Sthompsa	/* Device interface */
2382188417Sthompsa	DEVMETHOD(device_probe,		rum_match),
2383188417Sthompsa	DEVMETHOD(device_attach,	rum_attach),
2384188417Sthompsa	DEVMETHOD(device_detach,	rum_detach),
2385259456Shselasky	DEVMETHOD_END
2386188417Sthompsa};
2387184610Salfred
2388188417Sthompsastatic driver_t rum_driver = {
2389188417Sthompsa	.name = "rum",
2390188417Sthompsa	.methods = rum_methods,
2391188417Sthompsa	.size = sizeof(struct rum_softc),
2392188417Sthompsa};
2393184610Salfred
2394188417Sthompsastatic devclass_t rum_devclass;
2395184610Salfred
2396189275SthompsaDRIVER_MODULE(rum, uhub, rum_driver, rum_devclass, NULL, 0);
2397212122SthompsaMODULE_DEPEND(rum, wlan, 1, 1, 1);
2398212122SthompsaMODULE_DEPEND(rum, usb, 1, 1, 1);
2399212122SthompsaMODULE_VERSION(rum, 1);
2400