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