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