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