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