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