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