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