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