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