]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/usb/if_rum.c
Use device_foo_t to declare kobj methods
[FreeBSD/FreeBSD.git] / sys / dev / usb / 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  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19
20 #include <sys/cdefs.h>
21 __FBSDID("$FreeBSD$");
22
23 /*-
24  * Ralink Technology RT2501USB/RT2601USB chipset driver
25  * http://www.ralinktech.com.tw/
26  */
27
28 #include <sys/param.h>
29 #include <sys/sysctl.h>
30 #include <sys/sockio.h>
31 #include <sys/mbuf.h>
32 #include <sys/kernel.h>
33 #include <sys/socket.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/bus.h>
38 #include <sys/endian.h>
39
40 #include <machine/bus.h>
41 #include <machine/resource.h>
42 #include <sys/rman.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 #include <net80211/ieee80211_var.h>
53 #include <net80211/ieee80211_amrr.h>
54 #include <net80211/ieee80211_radiotap.h>
55 #include <net80211/ieee80211_regdomain.h>
56
57 #include <dev/usb/usb.h>
58 #include <dev/usb/usbdi.h>
59 #include <dev/usb/usbdi_util.h>
60 #include "usbdevs.h"
61
62 #include <dev/usb/if_rumreg.h>
63 #include <dev/usb/if_rumvar.h>
64 #include <dev/usb/rt2573_ucode.h>
65
66 #ifdef USB_DEBUG
67 #define DPRINTF(x)      do { if (rumdebug > 0) logprintf x; } while (0)
68 #define DPRINTFN(n, x)  do { if (rumdebug >= (n)) logprintf x; } while (0)
69 int rumdebug = 0;
70 SYSCTL_NODE(_hw_usb, OID_AUTO, rum, CTLFLAG_RW, 0, "USB rum");
71 SYSCTL_INT(_hw_usb_rum, OID_AUTO, debug, CTLFLAG_RW, &rumdebug, 0,
72     "rum debug level");
73 #else
74 #define DPRINTF(x)
75 #define DPRINTFN(n, x)
76 #endif
77
78 /* various supported device vendors/products */
79 static const struct usb_devno rum_devs[] = {
80         { USB_VENDOR_ABOCOM,            USB_PRODUCT_ABOCOM_HWU54DM },
81         { USB_VENDOR_ABOCOM,            USB_PRODUCT_ABOCOM_RT2573_2 },
82         { USB_VENDOR_ABOCOM,            USB_PRODUCT_ABOCOM_RT2573_3 },
83         { USB_VENDOR_ABOCOM,            USB_PRODUCT_ABOCOM_RT2573_4 },
84         { USB_VENDOR_ABOCOM,            USB_PRODUCT_ABOCOM_WUG2700 },
85         { USB_VENDOR_AMIT,              USB_PRODUCT_AMIT_CGWLUSB2GO },
86         { USB_VENDOR_ASUS,              USB_PRODUCT_ASUS_RT2573_1 },
87         { USB_VENDOR_ASUS,              USB_PRODUCT_ASUS_RT2573_2 },
88         { USB_VENDOR_BELKIN,            USB_PRODUCT_BELKIN_F5D7050A },
89         { USB_VENDOR_BELKIN,            USB_PRODUCT_BELKIN_F5D9050V3 },
90         { USB_VENDOR_CISCOLINKSYS,      USB_PRODUCT_CISCOLINKSYS_WUSB54GC },
91         { USB_VENDOR_CISCOLINKSYS,      USB_PRODUCT_CISCOLINKSYS_WUSB54GR },
92         { USB_VENDOR_CONCEPTRONIC2,     USB_PRODUCT_CONCEPTRONIC2_C54RU2 },
93         { USB_VENDOR_DICKSMITH,         USB_PRODUCT_DICKSMITH_CWD854F },
94         { USB_VENDOR_DICKSMITH,         USB_PRODUCT_DICKSMITH_RT2573 },
95         { USB_VENDOR_DLINK2,            USB_PRODUCT_DLINK2_DWLG122C1 },
96         { USB_VENDOR_DLINK2,            USB_PRODUCT_DLINK2_WUA1340 },
97         { USB_VENDOR_GIGABYTE,          USB_PRODUCT_GIGABYTE_GNWB01GS },
98         { USB_VENDOR_GIGABYTE,          USB_PRODUCT_GIGABYTE_GNWI05GS },
99         { USB_VENDOR_GIGASET,           USB_PRODUCT_GIGASET_RT2573 },
100         { USB_VENDOR_GOODWAY,           USB_PRODUCT_GOODWAY_RT2573 },
101         { USB_VENDOR_GUILLEMOT,         USB_PRODUCT_GUILLEMOT_HWGUSB254LB },
102         { USB_VENDOR_GUILLEMOT,         USB_PRODUCT_GUILLEMOT_HWGUSB254V2AP },
103         { USB_VENDOR_HUAWEI3COM,        USB_PRODUCT_HUAWEI3COM_WUB320G },
104         { USB_VENDOR_MELCO,             USB_PRODUCT_MELCO_G54HP },
105         { USB_VENDOR_MELCO,             USB_PRODUCT_MELCO_SG54HP },
106         { USB_VENDOR_MSI,               USB_PRODUCT_MSI_RT2573_1 },
107         { USB_VENDOR_MSI,               USB_PRODUCT_MSI_RT2573_2 },
108         { USB_VENDOR_MSI,               USB_PRODUCT_MSI_RT2573_3 },
109         { USB_VENDOR_MSI,               USB_PRODUCT_MSI_RT2573_4 },
110         { USB_VENDOR_NOVATECH,          USB_PRODUCT_NOVATECH_RT2573 },
111         { USB_VENDOR_PLANEX2,           USB_PRODUCT_PLANEX2_GWUS54HP },
112         { USB_VENDOR_PLANEX2,           USB_PRODUCT_PLANEX2_GWUS54MINI2 },
113         { USB_VENDOR_PLANEX2,           USB_PRODUCT_PLANEX2_GWUSMM },
114         { USB_VENDOR_QCOM,              USB_PRODUCT_QCOM_RT2573 },
115         { USB_VENDOR_QCOM,              USB_PRODUCT_QCOM_RT2573_2 },
116         { USB_VENDOR_RALINK,            USB_PRODUCT_RALINK_RT2573 },
117         { USB_VENDOR_RALINK,            USB_PRODUCT_RALINK_RT2573_2 },
118         { USB_VENDOR_RALINK,            USB_PRODUCT_RALINK_RT2671 },
119         { USB_VENDOR_SITECOMEU,         USB_PRODUCT_SITECOMEU_WL113R2 },
120         { USB_VENDOR_SITECOMEU,         USB_PRODUCT_SITECOMEU_WL172 },
121         { USB_VENDOR_SURECOM,           USB_PRODUCT_SURECOM_RT2573 }
122 };
123
124 MODULE_DEPEND(rum, wlan, 1, 1, 1);
125 MODULE_DEPEND(rum, wlan_amrr, 1, 1, 1);
126
127 static int              rum_alloc_tx_list(struct rum_softc *);
128 static void             rum_free_tx_list(struct rum_softc *);
129 static int              rum_alloc_rx_list(struct rum_softc *);
130 static void             rum_free_rx_list(struct rum_softc *);
131 static int              rum_media_change(struct ifnet *);
132 static void             rum_task(void *);
133 static void             rum_scantask(void *);
134 static int              rum_newstate(struct ieee80211com *,
135                             enum ieee80211_state, int);
136 static void             rum_txeof(usbd_xfer_handle, usbd_private_handle,
137                             usbd_status);
138 static void             rum_rxeof(usbd_xfer_handle, usbd_private_handle,
139                             usbd_status);
140 static int              rum_rxrate(struct rum_rx_desc *);
141 static int              rum_ack_rate(struct ieee80211com *, int);
142 static uint16_t         rum_txtime(int, int, uint32_t);
143 static uint8_t          rum_plcp_signal(int);
144 static void             rum_setup_tx_desc(struct rum_softc *,
145                             struct rum_tx_desc *, uint32_t, uint16_t, int,
146                             int);
147 static int              rum_tx_mgt(struct rum_softc *, struct mbuf *,
148                             struct ieee80211_node *);
149 static int              rum_tx_raw(struct rum_softc *, struct mbuf *,
150                             struct ieee80211_node *, 
151                             const struct ieee80211_bpf_params *);
152 static int              rum_tx_data(struct rum_softc *, struct mbuf *,
153                             struct ieee80211_node *);
154 static void             rum_start(struct ifnet *);
155 static void             rum_watchdog(void *);
156 static int              rum_ioctl(struct ifnet *, u_long, caddr_t);
157 static void             rum_eeprom_read(struct rum_softc *, uint16_t, void *,
158                             int);
159 static uint32_t         rum_read(struct rum_softc *, uint16_t);
160 static void             rum_read_multi(struct rum_softc *, uint16_t, void *,
161                             int);
162 static void             rum_write(struct rum_softc *, uint16_t, uint32_t);
163 static void             rum_write_multi(struct rum_softc *, uint16_t, void *,
164                             size_t);
165 static void             rum_bbp_write(struct rum_softc *, uint8_t, uint8_t);
166 static uint8_t          rum_bbp_read(struct rum_softc *, uint8_t);
167 static void             rum_rf_write(struct rum_softc *, uint8_t, uint32_t);
168 static void             rum_select_antenna(struct rum_softc *);
169 static void             rum_enable_mrr(struct rum_softc *);
170 static void             rum_set_txpreamble(struct rum_softc *);
171 static void             rum_set_basicrates(struct rum_softc *);
172 static void             rum_select_band(struct rum_softc *,
173                             struct ieee80211_channel *);
174 static void             rum_set_chan(struct rum_softc *,
175                             struct ieee80211_channel *);
176 static void             rum_enable_tsf_sync(struct rum_softc *);
177 static void             rum_update_slot(struct ifnet *);
178 static void             rum_set_bssid(struct rum_softc *, const uint8_t *);
179 static void             rum_set_macaddr(struct rum_softc *, const uint8_t *);
180 static void             rum_update_promisc(struct rum_softc *);
181 static const char       *rum_get_rf(int);
182 static void             rum_read_eeprom(struct rum_softc *);
183 static int              rum_bbp_init(struct rum_softc *);
184 static void             rum_init(void *);
185 static void             rum_stop(void *);
186 static int              rum_load_microcode(struct rum_softc *, const u_char *,
187                             size_t);
188 static int              rum_prepare_beacon(struct rum_softc *);
189 static int              rum_raw_xmit(struct ieee80211_node *, struct mbuf *,
190                             const struct ieee80211_bpf_params *);
191 static void             rum_scan_start(struct ieee80211com *);
192 static void             rum_scan_end(struct ieee80211com *);
193 static void             rum_set_channel(struct ieee80211com *);
194 static int              rum_get_rssi(struct rum_softc *, uint8_t);
195 static void             rum_amrr_start(struct rum_softc *,
196                             struct ieee80211_node *);
197 static void             rum_amrr_timeout(void *);
198 static void             rum_amrr_update(usbd_xfer_handle, usbd_private_handle,
199                             usbd_status);
200
201 static const struct {
202         uint32_t        reg;
203         uint32_t        val;
204 } rum_def_mac[] = {
205         { RT2573_TXRX_CSR0,  0x025fb032 },
206         { RT2573_TXRX_CSR1,  0x9eaa9eaf },
207         { RT2573_TXRX_CSR2,  0x8a8b8c8d }, 
208         { RT2573_TXRX_CSR3,  0x00858687 },
209         { RT2573_TXRX_CSR7,  0x2e31353b },
210         { RT2573_TXRX_CSR8,  0x2a2a2a2c },
211         { RT2573_TXRX_CSR15, 0x0000000f },
212         { RT2573_MAC_CSR6,   0x00000fff },
213         { RT2573_MAC_CSR8,   0x016c030a },
214         { RT2573_MAC_CSR10,  0x00000718 },
215         { RT2573_MAC_CSR12,  0x00000004 },
216         { RT2573_MAC_CSR13,  0x00007f00 },
217         { RT2573_SEC_CSR0,   0x00000000 },
218         { RT2573_SEC_CSR1,   0x00000000 },
219         { RT2573_SEC_CSR5,   0x00000000 },
220         { RT2573_PHY_CSR1,   0x000023b0 },
221         { RT2573_PHY_CSR5,   0x00040a06 },
222         { RT2573_PHY_CSR6,   0x00080606 },
223         { RT2573_PHY_CSR7,   0x00000408 },
224         { RT2573_AIFSN_CSR,  0x00002273 },
225         { RT2573_CWMIN_CSR,  0x00002344 },
226         { RT2573_CWMAX_CSR,  0x000034aa }
227 };
228
229 static const struct {
230         uint8_t reg;
231         uint8_t val;
232 } rum_def_bbp[] = {
233         {   3, 0x80 },
234         {  15, 0x30 },
235         {  17, 0x20 },
236         {  21, 0xc8 },
237         {  22, 0x38 },
238         {  23, 0x06 },
239         {  24, 0xfe },
240         {  25, 0x0a },
241         {  26, 0x0d },
242         {  32, 0x0b },
243         {  34, 0x12 },
244         {  37, 0x07 },
245         {  39, 0xf8 },
246         {  41, 0x60 },
247         {  53, 0x10 },
248         {  54, 0x18 },
249         {  60, 0x10 },
250         {  61, 0x04 },
251         {  62, 0x04 },
252         {  75, 0xfe },
253         {  86, 0xfe },
254         {  88, 0xfe },
255         {  90, 0x0f },
256         {  99, 0x00 },
257         { 102, 0x16 },
258         { 107, 0x04 }
259 };
260
261 static const struct rfprog {
262         uint8_t         chan;
263         uint32_t        r1, r2, r3, r4;
264 }  rum_rf5226[] = {
265         {   1, 0x00b03, 0x001e1, 0x1a014, 0x30282 },
266         {   2, 0x00b03, 0x001e1, 0x1a014, 0x30287 },
267         {   3, 0x00b03, 0x001e2, 0x1a014, 0x30282 },
268         {   4, 0x00b03, 0x001e2, 0x1a014, 0x30287 },
269         {   5, 0x00b03, 0x001e3, 0x1a014, 0x30282 },
270         {   6, 0x00b03, 0x001e3, 0x1a014, 0x30287 },
271         {   7, 0x00b03, 0x001e4, 0x1a014, 0x30282 },
272         {   8, 0x00b03, 0x001e4, 0x1a014, 0x30287 },
273         {   9, 0x00b03, 0x001e5, 0x1a014, 0x30282 },
274         {  10, 0x00b03, 0x001e5, 0x1a014, 0x30287 },
275         {  11, 0x00b03, 0x001e6, 0x1a014, 0x30282 },
276         {  12, 0x00b03, 0x001e6, 0x1a014, 0x30287 },
277         {  13, 0x00b03, 0x001e7, 0x1a014, 0x30282 },
278         {  14, 0x00b03, 0x001e8, 0x1a014, 0x30284 },
279
280         {  34, 0x00b03, 0x20266, 0x36014, 0x30282 },
281         {  38, 0x00b03, 0x20267, 0x36014, 0x30284 },
282         {  42, 0x00b03, 0x20268, 0x36014, 0x30286 },
283         {  46, 0x00b03, 0x20269, 0x36014, 0x30288 },
284
285         {  36, 0x00b03, 0x00266, 0x26014, 0x30288 },
286         {  40, 0x00b03, 0x00268, 0x26014, 0x30280 },
287         {  44, 0x00b03, 0x00269, 0x26014, 0x30282 },
288         {  48, 0x00b03, 0x0026a, 0x26014, 0x30284 },
289         {  52, 0x00b03, 0x0026b, 0x26014, 0x30286 },
290         {  56, 0x00b03, 0x0026c, 0x26014, 0x30288 },
291         {  60, 0x00b03, 0x0026e, 0x26014, 0x30280 },
292         {  64, 0x00b03, 0x0026f, 0x26014, 0x30282 },
293
294         { 100, 0x00b03, 0x0028a, 0x2e014, 0x30280 },
295         { 104, 0x00b03, 0x0028b, 0x2e014, 0x30282 },
296         { 108, 0x00b03, 0x0028c, 0x2e014, 0x30284 },
297         { 112, 0x00b03, 0x0028d, 0x2e014, 0x30286 },
298         { 116, 0x00b03, 0x0028e, 0x2e014, 0x30288 },
299         { 120, 0x00b03, 0x002a0, 0x2e014, 0x30280 },
300         { 124, 0x00b03, 0x002a1, 0x2e014, 0x30282 },
301         { 128, 0x00b03, 0x002a2, 0x2e014, 0x30284 },
302         { 132, 0x00b03, 0x002a3, 0x2e014, 0x30286 },
303         { 136, 0x00b03, 0x002a4, 0x2e014, 0x30288 },
304         { 140, 0x00b03, 0x002a6, 0x2e014, 0x30280 },
305
306         { 149, 0x00b03, 0x002a8, 0x2e014, 0x30287 },
307         { 153, 0x00b03, 0x002a9, 0x2e014, 0x30289 },
308         { 157, 0x00b03, 0x002ab, 0x2e014, 0x30281 },
309         { 161, 0x00b03, 0x002ac, 0x2e014, 0x30283 },
310         { 165, 0x00b03, 0x002ad, 0x2e014, 0x30285 }
311 }, rum_rf5225[] = {
312         {   1, 0x00b33, 0x011e1, 0x1a014, 0x30282 },
313         {   2, 0x00b33, 0x011e1, 0x1a014, 0x30287 },
314         {   3, 0x00b33, 0x011e2, 0x1a014, 0x30282 },
315         {   4, 0x00b33, 0x011e2, 0x1a014, 0x30287 },
316         {   5, 0x00b33, 0x011e3, 0x1a014, 0x30282 },
317         {   6, 0x00b33, 0x011e3, 0x1a014, 0x30287 },
318         {   7, 0x00b33, 0x011e4, 0x1a014, 0x30282 },
319         {   8, 0x00b33, 0x011e4, 0x1a014, 0x30287 },
320         {   9, 0x00b33, 0x011e5, 0x1a014, 0x30282 },
321         {  10, 0x00b33, 0x011e5, 0x1a014, 0x30287 },
322         {  11, 0x00b33, 0x011e6, 0x1a014, 0x30282 },
323         {  12, 0x00b33, 0x011e6, 0x1a014, 0x30287 },
324         {  13, 0x00b33, 0x011e7, 0x1a014, 0x30282 },
325         {  14, 0x00b33, 0x011e8, 0x1a014, 0x30284 },
326
327         {  34, 0x00b33, 0x01266, 0x26014, 0x30282 },
328         {  38, 0x00b33, 0x01267, 0x26014, 0x30284 },
329         {  42, 0x00b33, 0x01268, 0x26014, 0x30286 },
330         {  46, 0x00b33, 0x01269, 0x26014, 0x30288 },
331
332         {  36, 0x00b33, 0x01266, 0x26014, 0x30288 },
333         {  40, 0x00b33, 0x01268, 0x26014, 0x30280 },
334         {  44, 0x00b33, 0x01269, 0x26014, 0x30282 },
335         {  48, 0x00b33, 0x0126a, 0x26014, 0x30284 },
336         {  52, 0x00b33, 0x0126b, 0x26014, 0x30286 },
337         {  56, 0x00b33, 0x0126c, 0x26014, 0x30288 },
338         {  60, 0x00b33, 0x0126e, 0x26014, 0x30280 },
339         {  64, 0x00b33, 0x0126f, 0x26014, 0x30282 },
340
341         { 100, 0x00b33, 0x0128a, 0x2e014, 0x30280 },
342         { 104, 0x00b33, 0x0128b, 0x2e014, 0x30282 },
343         { 108, 0x00b33, 0x0128c, 0x2e014, 0x30284 },
344         { 112, 0x00b33, 0x0128d, 0x2e014, 0x30286 },
345         { 116, 0x00b33, 0x0128e, 0x2e014, 0x30288 },
346         { 120, 0x00b33, 0x012a0, 0x2e014, 0x30280 },
347         { 124, 0x00b33, 0x012a1, 0x2e014, 0x30282 },
348         { 128, 0x00b33, 0x012a2, 0x2e014, 0x30284 },
349         { 132, 0x00b33, 0x012a3, 0x2e014, 0x30286 },
350         { 136, 0x00b33, 0x012a4, 0x2e014, 0x30288 },
351         { 140, 0x00b33, 0x012a6, 0x2e014, 0x30280 },
352
353         { 149, 0x00b33, 0x012a8, 0x2e014, 0x30287 },
354         { 153, 0x00b33, 0x012a9, 0x2e014, 0x30289 },
355         { 157, 0x00b33, 0x012ab, 0x2e014, 0x30281 },
356         { 161, 0x00b33, 0x012ac, 0x2e014, 0x30283 },
357         { 165, 0x00b33, 0x012ad, 0x2e014, 0x30285 }
358 };
359
360 static int
361 rum_match(device_t self)
362 {
363         struct usb_attach_arg *uaa = device_get_ivars(self);
364
365         if (uaa->iface != NULL)
366                 return UMATCH_NONE;
367
368         return (usb_lookup(rum_devs, uaa->vendor, uaa->product) != NULL) ?
369             UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
370 }
371
372 static int
373 rum_attach(device_t self)
374 {
375         struct rum_softc *sc = device_get_softc(self);
376         struct usb_attach_arg *uaa = device_get_ivars(self);
377         struct ieee80211com *ic = &sc->sc_ic;
378         struct ifnet *ifp;
379         const uint8_t *ucode = NULL;
380         usb_interface_descriptor_t *id;
381         usb_endpoint_descriptor_t *ed;
382         usbd_status error;
383         int i, ntries, size, bands;
384         uint32_t tmp;
385
386         sc->sc_udev = uaa->device;
387         sc->sc_dev = self;
388
389         if (usbd_set_config_no(sc->sc_udev, RT2573_CONFIG_NO, 0) != 0) {
390                 printf("%s: could not set configuration no\n",
391                     device_get_nameunit(sc->sc_dev));
392                 return ENXIO;
393         }
394
395         /* get the first interface handle */
396         error = usbd_device2interface_handle(sc->sc_udev, RT2573_IFACE_INDEX,
397             &sc->sc_iface);
398         if (error != 0) {
399                 printf("%s: could not get interface handle\n",
400                     device_get_nameunit(sc->sc_dev));
401                 return ENXIO;
402         }
403
404         /*
405          * Find endpoints.
406          */
407         id = usbd_get_interface_descriptor(sc->sc_iface);
408
409         sc->sc_rx_no = sc->sc_tx_no = -1;
410         for (i = 0; i < id->bNumEndpoints; i++) {
411                 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
412                 if (ed == NULL) {
413                         printf("%s: no endpoint descriptor for iface %d\n",
414                             device_get_nameunit(sc->sc_dev), i);
415                         return ENXIO;
416                 }
417
418                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
419                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
420                         sc->sc_rx_no = ed->bEndpointAddress;
421                 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
422                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
423                         sc->sc_tx_no = ed->bEndpointAddress;
424         }
425         if (sc->sc_rx_no == -1 || sc->sc_tx_no == -1) {
426                 printf("%s: missing endpoint\n", 
427                     device_get_nameunit(sc->sc_dev));
428                 return ENXIO;
429         }
430
431         mtx_init(&sc->sc_mtx, device_get_nameunit(sc->sc_dev), MTX_NETWORK_LOCK,
432             MTX_DEF | MTX_RECURSE);
433
434         usb_init_task(&sc->sc_task, rum_task, sc);
435         usb_init_task(&sc->sc_scantask, rum_scantask, sc);
436         callout_init(&sc->watchdog_ch, 0);
437         callout_init(&sc->amrr_ch, 0);
438
439         /* retrieve RT2573 rev. no */
440         for (ntries = 0; ntries < 1000; ntries++) {
441                 if ((tmp = rum_read(sc, RT2573_MAC_CSR0)) != 0)
442                         break;
443                 DELAY(1000);
444         }
445         if (ntries == 1000) {
446                 printf("%s: timeout waiting for chip to settle\n",
447                     device_get_nameunit(sc->sc_dev));
448                 return ENXIO;
449         }
450
451         /* retrieve MAC address and various other things from EEPROM */
452         rum_read_eeprom(sc);
453
454         printf("%s: MAC/BBP RT2573 (rev 0x%05x), RF %s\n",
455             device_get_nameunit(sc->sc_dev), tmp, rum_get_rf(sc->rf_rev));
456
457         ucode = rt2573_ucode;
458         size = sizeof rt2573_ucode;
459         error = rum_load_microcode(sc, ucode, size);
460         if (error != 0) {
461                 device_printf(sc->sc_dev, "could not load 8051 microcode\n");
462                 mtx_destroy(&sc->sc_mtx);
463                 return ENXIO;
464         }
465
466         ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
467         if (ifp == NULL) {
468                 printf("%s: can not if_alloc()\n", 
469                     device_get_nameunit(sc->sc_dev));
470                 mtx_destroy(&sc->sc_mtx);
471                 return ENXIO;
472         }
473
474         ifp->if_softc = sc;
475         if_initname(ifp, "rum", device_get_unit(sc->sc_dev));
476         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST |
477             IFF_NEEDSGIANT; /* USB stack is still under Giant lock */
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, IFQ_MAXLEN);
482         ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
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         ic->ic_opmode = IEEE80211_M_STA;        /* default to BSS mode */
488         ic->ic_state = IEEE80211_S_INIT;
489
490         /* set device capabilities */
491         ic->ic_caps =
492             IEEE80211_C_IBSS |          /* IBSS mode supported */
493             IEEE80211_C_MONITOR |       /* monitor mode supported */
494             IEEE80211_C_HOSTAP |        /* HostAp mode supported */
495             IEEE80211_C_TXPMGT |        /* tx power management */
496             IEEE80211_C_SHPREAMBLE |    /* short preamble supported */
497             IEEE80211_C_SHSLOT |        /* short slot time supported */
498             IEEE80211_C_BGSCAN |        /* bg scanning supported */
499             IEEE80211_C_WPA;            /* 802.11i */
500
501         bands = 0;
502         setbit(&bands, IEEE80211_MODE_11B);
503         setbit(&bands, IEEE80211_MODE_11G);
504         ieee80211_init_channels(ic, 0, CTRY_DEFAULT, bands, 0, 1);
505
506         if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_5226) {
507                 struct ieee80211_channel *c;
508
509                 /* set supported .11a channels */
510                 for (i = 34; i <= 46; i += 4) {
511                         c = &ic->ic_channels[ic->ic_nchans++];
512                         c->ic_freq = ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
513                         c->ic_flags = IEEE80211_CHAN_A;
514                         c->ic_ieee = i;
515                 }
516                 for (i = 36; i <= 64; i += 4) {
517                         c = &ic->ic_channels[ic->ic_nchans++];
518                         c->ic_freq = ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
519                         c->ic_flags = IEEE80211_CHAN_A;
520                         c->ic_ieee = i;
521                 }
522                 for (i = 100; i <= 140; i += 4) {
523                         c = &ic->ic_channels[ic->ic_nchans++];
524                         c->ic_freq = ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
525                         c->ic_flags = IEEE80211_CHAN_A;
526                         c->ic_ieee = i;
527                 }
528                 for (i = 149; i <= 165; i += 4) {
529                         c = &ic->ic_channels[ic->ic_nchans++];
530                         c->ic_freq = ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
531                         c->ic_flags = IEEE80211_CHAN_A;
532                         c->ic_ieee = i;
533                 }
534         }
535
536         ieee80211_ifattach(ic);
537         ic->ic_scan_start = rum_scan_start;
538         ic->ic_scan_end = rum_scan_end;
539         ic->ic_set_channel = rum_set_channel;
540
541         /* enable s/w bmiss handling in sta mode */
542         ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS;
543
544         /* override state transition machine */
545         sc->sc_newstate = ic->ic_newstate;
546         ic->ic_newstate = rum_newstate;
547         ic->ic_raw_xmit = rum_raw_xmit;
548         ieee80211_media_init(ic, rum_media_change, ieee80211_media_status);
549
550         ieee80211_amrr_init(&sc->amrr, ic,
551             IEEE80211_AMRR_MIN_SUCCESS_THRESHOLD,
552             IEEE80211_AMRR_MAX_SUCCESS_THRESHOLD);
553
554         bpfattach2(ifp, DLT_IEEE802_11_RADIO,
555             sizeof (struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN, 
556             &sc->sc_drvbpf);
557
558         sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
559         sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
560         sc->sc_rxtap.wr_ihdr.it_present = htole32(RT2573_RX_RADIOTAP_PRESENT);
561
562         sc->sc_txtap_len = sizeof sc->sc_txtapu;
563         sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
564         sc->sc_txtap.wt_ihdr.it_present = htole32(RT2573_TX_RADIOTAP_PRESENT);
565
566         if (bootverbose)
567                 ieee80211_announce(ic);
568
569         return 0;
570 }
571
572 static int
573 rum_detach(device_t self)
574 {
575         struct rum_softc *sc = device_get_softc(self);
576         struct ieee80211com *ic = &sc->sc_ic;
577         struct ifnet *ifp = ic->ic_ifp;
578
579         rum_stop(sc);
580         usb_rem_task(sc->sc_udev, &sc->sc_task);
581         usb_rem_task(sc->sc_udev, &sc->sc_scantask);
582         callout_stop(&sc->watchdog_ch);
583         callout_stop(&sc->amrr_ch);
584
585         if (sc->amrr_xfer != NULL) {
586                 usbd_free_xfer(sc->amrr_xfer);
587                 sc->amrr_xfer = NULL;
588         }
589
590         if (sc->sc_rx_pipeh != NULL) {
591                 usbd_abort_pipe(sc->sc_rx_pipeh);
592                 usbd_close_pipe(sc->sc_rx_pipeh);
593         }
594         if (sc->sc_tx_pipeh != NULL) {
595                 usbd_abort_pipe(sc->sc_tx_pipeh);
596                 usbd_close_pipe(sc->sc_tx_pipeh);
597         }
598         
599         rum_free_rx_list(sc);
600         rum_free_tx_list(sc);
601
602         bpfdetach(ifp);
603         ieee80211_ifdetach(ic);
604         if_free(ifp);
605
606         mtx_destroy(&sc->sc_mtx);
607
608         return 0;
609 }
610
611 static int
612 rum_alloc_tx_list(struct rum_softc *sc)
613 {
614         struct rum_tx_data *data;
615         int i, error;
616
617         sc->tx_queued = 0;
618
619         for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
620                 data = &sc->tx_data[i];
621
622                 data->sc = sc;
623
624                 data->xfer = usbd_alloc_xfer(sc->sc_udev);
625                 if (data->xfer == NULL) {
626                         printf("%s: could not allocate tx xfer\n",
627                             device_get_nameunit(sc->sc_dev));
628                         error = ENOMEM;
629                         goto fail;
630                 }
631                 data->buf = usbd_alloc_buffer(data->xfer,
632                     RT2573_TX_DESC_SIZE + MCLBYTES);
633                 if (data->buf == NULL) {
634                         printf("%s: could not allocate tx buffer\n",
635                             device_get_nameunit(sc->sc_dev));
636                         error = ENOMEM;
637                         goto fail;
638                 }
639                 /* clean Tx descriptor */
640                 bzero(data->buf, RT2573_TX_DESC_SIZE);
641         }
642
643         return 0;
644
645 fail:   rum_free_tx_list(sc);
646         return error;
647 }
648
649 static void
650 rum_free_tx_list(struct rum_softc *sc)
651 {
652         struct rum_tx_data *data;
653         int i;
654
655         for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
656                 data = &sc->tx_data[i];
657
658                 if (data->xfer != NULL) {
659                         usbd_free_xfer(data->xfer);
660                         data->xfer = NULL;
661                 }
662
663                 if (data->ni != NULL) {
664                         ieee80211_free_node(data->ni);
665                         data->ni = NULL;
666                 }
667         }
668 }
669
670 static int
671 rum_alloc_rx_list(struct rum_softc *sc)
672 {
673         struct rum_rx_data *data;
674         int i, error;
675
676         for (i = 0; i < RUM_RX_LIST_COUNT; i++) {
677                 data = &sc->rx_data[i];
678
679                 data->sc = sc;
680
681                 data->xfer = usbd_alloc_xfer(sc->sc_udev);
682                 if (data->xfer == NULL) {
683                         printf("%s: could not allocate rx xfer\n",
684                             device_get_nameunit(sc->sc_dev));
685                         error = ENOMEM;
686                         goto fail;
687                 }
688                 if (usbd_alloc_buffer(data->xfer, MCLBYTES) == NULL) {
689                         printf("%s: could not allocate rx buffer\n",
690                             device_get_nameunit(sc->sc_dev));
691                         error = ENOMEM;
692                         goto fail;
693                 }
694
695                 data->m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
696                 if (data->m == NULL) {
697                         printf("%s: could not allocate rx mbuf\n",
698                             device_get_nameunit(sc->sc_dev));
699                         error = ENOMEM;
700                         goto fail;
701                 }
702
703                 data->buf = mtod(data->m, uint8_t *);
704         }
705
706         return 0;
707
708 fail:   rum_free_tx_list(sc);
709         return error;
710 }
711
712 static void
713 rum_free_rx_list(struct rum_softc *sc)
714 {
715         struct rum_rx_data *data;
716         int i;
717
718         for (i = 0; i < RUM_RX_LIST_COUNT; i++) {
719                 data = &sc->rx_data[i];
720
721                 if (data->xfer != NULL) {
722                         usbd_free_xfer(data->xfer);
723                         data->xfer = NULL;
724                 }
725                 if (data->m != NULL) {
726                         m_freem(data->m);
727                         data->m = NULL;
728                 }
729         }
730 }
731
732 static int
733 rum_media_change(struct ifnet *ifp)
734 {
735         struct rum_softc *sc = ifp->if_softc;
736         int error;
737
738         RUM_LOCK(sc);
739
740         error = ieee80211_media_change(ifp);
741         if (error != ENETRESET) {
742                 RUM_UNLOCK(sc);
743                 return error;
744         }
745
746         if ((ifp->if_flags & IFF_UP) &&
747             (ifp->if_drv_flags & IFF_DRV_RUNNING))
748                 rum_init(sc);
749
750         RUM_UNLOCK(sc);
751
752         return 0;
753 }
754
755 static void
756 rum_task(void *arg)
757 {
758         struct rum_softc *sc = arg;
759         struct ieee80211com *ic = &sc->sc_ic;
760         enum ieee80211_state ostate;
761         struct ieee80211_node *ni;
762         uint32_t tmp;
763
764         ostate = ic->ic_state;
765
766         RUM_LOCK(sc);
767
768         switch (sc->sc_state) {
769         case IEEE80211_S_INIT:
770                 if (ostate == IEEE80211_S_RUN) {
771                         /* abort TSF synchronization */
772                         tmp = rum_read(sc, RT2573_TXRX_CSR9);
773                         rum_write(sc, RT2573_TXRX_CSR9, tmp & ~0x00ffffff);
774                 }
775                 break;
776
777         case IEEE80211_S_RUN:
778                 ni = ic->ic_bss;
779
780                 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
781                         rum_update_slot(ic->ic_ifp);
782                         rum_enable_mrr(sc);
783                         rum_set_txpreamble(sc);
784                         rum_set_basicrates(sc);
785                         rum_set_bssid(sc, ni->ni_bssid);
786                 }
787
788                 if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
789                     ic->ic_opmode == IEEE80211_M_IBSS)
790                         rum_prepare_beacon(sc);
791
792                 if (ic->ic_opmode != IEEE80211_M_MONITOR)
793                         rum_enable_tsf_sync(sc);
794
795                 /* enable automatic rate adaptation in STA mode */
796                 if (ic->ic_opmode == IEEE80211_M_STA &&
797                     ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE)
798                         rum_amrr_start(sc, ni);
799                 break;
800         default:
801                 break;
802         }
803
804         RUM_UNLOCK(sc);
805
806         sc->sc_newstate(ic, sc->sc_state, sc->sc_arg);
807 }
808
809 static int
810 rum_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
811 {
812         struct rum_softc *sc = ic->ic_ifp->if_softc;
813
814         callout_stop(&sc->amrr_ch);
815
816         /* do it in a process context */
817         sc->sc_state = nstate;
818         sc->sc_arg = arg;
819
820         usb_rem_task(sc->sc_udev, &sc->sc_task);
821         if (nstate == IEEE80211_S_INIT)
822                 sc->sc_newstate(ic, nstate, arg);
823         else
824                 usb_add_task(sc->sc_udev, &sc->sc_task, USB_TASKQ_DRIVER);
825         return 0;
826 }
827
828 /* quickly determine if a given rate is CCK or OFDM */
829 #define RUM_RATE_IS_OFDM(rate)  ((rate) >= 12 && (rate) != 22)
830
831 #define RUM_ACK_SIZE    14      /* 10 + 4(FCS) */
832 #define RUM_CTS_SIZE    14      /* 10 + 4(FCS) */
833
834 static void
835 rum_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
836 {
837         struct rum_tx_data *data = priv;
838         struct rum_softc *sc = data->sc;
839         struct ifnet *ifp = sc->sc_ic.ic_ifp;
840
841         if (data->m->m_flags & M_TXCB)
842                 ieee80211_process_callback(data->ni, data->m,
843                         status == USBD_NORMAL_COMPLETION ? 0 : ETIMEDOUT);
844
845         if (status != USBD_NORMAL_COMPLETION) {
846                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
847                         return;
848
849                 printf("%s: could not transmit buffer: %s\n",
850                     device_get_nameunit(sc->sc_dev), usbd_errstr(status));
851
852                 if (status == USBD_STALLED)
853                         usbd_clear_endpoint_stall_async(sc->sc_tx_pipeh);
854
855                 ifp->if_oerrors++;
856                 return;
857         }
858
859         m_freem(data->m);
860         data->m = NULL;
861         ieee80211_free_node(data->ni);
862         data->ni = NULL;
863
864         sc->tx_queued--;
865         ifp->if_opackets++;
866
867         DPRINTFN(10, ("tx done\n"));
868
869         sc->sc_tx_timer = 0;
870         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
871         rum_start(ifp);
872 }
873
874 static void
875 rum_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
876 {
877         struct rum_rx_data *data = priv;
878         struct rum_softc *sc = data->sc;
879         struct ieee80211com *ic = &sc->sc_ic;
880         struct ifnet *ifp = ic->ic_ifp;
881         struct rum_rx_desc *desc;
882         struct ieee80211_frame *wh;
883         struct ieee80211_node *ni;
884         struct mbuf *mnew, *m;
885         int len, rssi;
886
887         if (status != USBD_NORMAL_COMPLETION) {
888                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
889                         return;
890
891                 if (status == USBD_STALLED)
892                         usbd_clear_endpoint_stall_async(sc->sc_rx_pipeh);
893                 goto skip;
894         }
895
896         usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
897
898         if (len < RT2573_RX_DESC_SIZE + sizeof (struct ieee80211_frame_min)) {
899                 DPRINTF(("%s: xfer too short %d\n", 
900                     device_get_nameunit(sc->sc_dev), len));
901                 ifp->if_ierrors++;
902                 goto skip;
903         }
904
905         desc = (struct rum_rx_desc *)data->buf;
906
907         if (le32toh(desc->flags) & RT2573_RX_CRC_ERROR) {
908                 /*
909                  * This should not happen since we did not request to receive
910                  * those frames when we filled RT2573_TXRX_CSR0.
911                  */
912                 DPRINTFN(5, ("CRC error\n"));
913                 ifp->if_ierrors++;
914                 goto skip;
915         }
916
917         mnew = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
918         if (mnew == NULL) {
919                 ifp->if_ierrors++;
920                 goto skip;
921         }
922
923         m = data->m;
924         data->m = mnew;
925         data->buf = mtod(data->m, uint8_t *);
926
927         /* finalize mbuf */
928         m->m_pkthdr.rcvif = ifp;
929         m->m_data = (caddr_t)(desc + 1); 
930         m->m_pkthdr.len = m->m_len = (le32toh(desc->flags) >> 16) & 0xfff;
931
932         rssi = rum_get_rssi(sc, desc->rssi);
933
934         wh = mtod(m, struct ieee80211_frame *);
935         ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
936
937         /* Error happened during RSSI conversion. */
938         if (rssi < 0)
939                 rssi = ni->ni_rssi;
940
941         if (bpf_peers_present(sc->sc_drvbpf)) {
942                 struct rum_rx_radiotap_header *tap = &sc->sc_rxtap;
943
944                 tap->wr_flags = IEEE80211_RADIOTAP_F_FCS;
945                 tap->wr_rate = rum_rxrate(desc);
946                 tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
947                 tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
948                 tap->wr_antenna = sc->rx_ant;
949                 tap->wr_antsignal = rssi;
950
951                 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m);
952         }
953
954         /* send the frame to the 802.11 layer */
955         ieee80211_input(ic, m, ni, rssi, RT2573_NOISE_FLOOR, 0);
956
957         /* node is no longer needed */
958         ieee80211_free_node(ni);
959
960         DPRINTFN(15, ("rx done\n"));
961
962 skip:   /* setup a new transfer */
963         usbd_setup_xfer(xfer, sc->sc_rx_pipeh, data, data->buf, MCLBYTES,
964             USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rum_rxeof);
965         usbd_transfer(xfer);
966 }
967
968 /*
969  * This function is only used by the Rx radiotap code. 
970  */
971 static int
972 rum_rxrate(struct rum_rx_desc *desc)
973 {
974         if (le32toh(desc->flags) & RT2573_RX_OFDM) {
975                 /* reverse function of rum_plcp_signal */
976                 switch (desc->rate) {
977                 case 0xb:       return 12;
978                 case 0xf:       return 18;
979                 case 0xa:       return 24;
980                 case 0xe:       return 36;
981                 case 0x9:       return 48;
982                 case 0xd:       return 72;
983                 case 0x8:       return 96;
984                 case 0xc:       return 108;
985                 }
986         } else {
987                 if (desc->rate == 10)
988                         return 2;
989                 if (desc->rate == 20)
990                         return 4;
991                 if (desc->rate == 55)
992                         return 11;
993                 if (desc->rate == 110)
994                         return 22;
995         }
996         return 2;       /* should not get there */
997 }
998
999 /*
1000  * Return the expected ack rate for a frame transmitted at rate `rate'.
1001  */
1002 static int
1003 rum_ack_rate(struct ieee80211com *ic, int rate)
1004 {
1005         switch (rate) {
1006         /* CCK rates */
1007         case 2:
1008                 return 2;
1009         case 4:
1010         case 11:
1011         case 22:
1012                 return (ic->ic_curmode == IEEE80211_MODE_11B) ? 4 : rate;
1013
1014         /* OFDM rates */
1015         case 12:
1016         case 18:
1017                 return 12;
1018         case 24:
1019         case 36:
1020                 return 24;
1021         case 48:
1022         case 72:
1023         case 96:
1024         case 108:
1025                 return 48;
1026         }
1027
1028         /* default to 1Mbps */
1029         return 2;
1030 }
1031
1032 /*
1033  * Compute the duration (in us) needed to transmit `len' bytes at rate `rate'.
1034  * The function automatically determines the operating mode depending on the
1035  * given rate. `flags' indicates whether short preamble is in use or not.
1036  */
1037 static uint16_t
1038 rum_txtime(int len, int rate, uint32_t flags)
1039 {
1040         uint16_t txtime;
1041
1042         if (RUM_RATE_IS_OFDM(rate)) {
1043                 /* IEEE Std 802.11a-1999, pp. 37 */
1044                 txtime = (8 + 4 * len + 3 + rate - 1) / rate;
1045                 txtime = 16 + 4 + 4 * txtime + 6;
1046         } else {
1047                 /* IEEE Std 802.11b-1999, pp. 28 */
1048                 txtime = (16 * len + rate - 1) / rate;
1049                 if (rate != 2 && (flags & IEEE80211_F_SHPREAMBLE))
1050                         txtime +=  72 + 24;
1051                 else
1052                         txtime += 144 + 48;
1053         }
1054         return txtime;
1055 }
1056
1057 static uint8_t
1058 rum_plcp_signal(int rate)
1059 {
1060         switch (rate) {
1061         /* CCK rates (returned values are device-dependent) */
1062         case 2:         return 0x0;
1063         case 4:         return 0x1;
1064         case 11:        return 0x2;
1065         case 22:        return 0x3;
1066
1067         /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1068         case 12:        return 0xb;
1069         case 18:        return 0xf;
1070         case 24:        return 0xa;
1071         case 36:        return 0xe;
1072         case 48:        return 0x9;
1073         case 72:        return 0xd;
1074         case 96:        return 0x8;
1075         case 108:       return 0xc;
1076
1077         /* unsupported rates (should not get there) */
1078         default:        return 0xff;
1079         }
1080 }
1081
1082 static void
1083 rum_setup_tx_desc(struct rum_softc *sc, struct rum_tx_desc *desc,
1084     uint32_t flags, uint16_t xflags, int len, int rate)
1085 {
1086         struct ieee80211com *ic = &sc->sc_ic;
1087         uint16_t plcp_length;
1088         int remainder;
1089
1090         desc->flags = htole32(flags);
1091         desc->flags |= htole32(RT2573_TX_VALID);
1092         desc->flags |= htole32(len << 16);
1093
1094         desc->xflags = htole16(xflags);
1095
1096         desc->wme = htole16(RT2573_QID(0) | RT2573_AIFSN(2) | 
1097             RT2573_LOGCWMIN(4) | RT2573_LOGCWMAX(10));
1098
1099         /* setup PLCP fields */
1100         desc->plcp_signal  = rum_plcp_signal(rate);
1101         desc->plcp_service = 4;
1102
1103         len += IEEE80211_CRC_LEN;
1104         if (RUM_RATE_IS_OFDM(rate)) {
1105                 desc->flags |= htole32(RT2573_TX_OFDM);
1106
1107                 plcp_length = len & 0xfff;
1108                 desc->plcp_length_hi = plcp_length >> 6;
1109                 desc->plcp_length_lo = plcp_length & 0x3f;
1110         } else {
1111                 plcp_length = (16 * len + rate - 1) / rate;
1112                 if (rate == 22) {
1113                         remainder = (16 * len) % 22;
1114                         if (remainder != 0 && remainder < 7)
1115                                 desc->plcp_service |= RT2573_PLCP_LENGEXT;
1116                 }
1117                 desc->plcp_length_hi = plcp_length >> 8;
1118                 desc->plcp_length_lo = plcp_length & 0xff;
1119
1120                 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1121                         desc->plcp_signal |= 0x08;
1122         }
1123 }
1124
1125 #define RUM_TX_TIMEOUT  5000
1126
1127 static int
1128 rum_tx_mgt(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1129 {
1130         struct ieee80211com *ic = &sc->sc_ic;
1131         struct rum_tx_desc *desc;
1132         struct rum_tx_data *data;
1133         struct ieee80211_frame *wh;
1134         uint32_t flags = 0;
1135         uint16_t dur;
1136         usbd_status error;
1137         int xferlen, rate;
1138
1139         data = &sc->tx_data[0];
1140         desc = (struct rum_tx_desc *)data->buf;
1141
1142         rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
1143
1144         data->m = m0;
1145         data->ni = ni;
1146
1147         wh = mtod(m0, struct ieee80211_frame *);
1148
1149         if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1150                 flags |= RT2573_TX_NEED_ACK;
1151
1152                 dur = rum_txtime(RUM_ACK_SIZE, rum_ack_rate(ic, rate), 
1153                     ic->ic_flags) + sc->sifs;
1154                 *(uint16_t *)wh->i_dur = htole16(dur);
1155
1156                 /* tell hardware to add timestamp for probe responses */
1157                 if ((wh->i_fc[0] &
1158                     (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
1159                     (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
1160                         flags |= RT2573_TX_TIMESTAMP;
1161         }
1162
1163         if (bpf_peers_present(sc->sc_drvbpf)) {
1164                 struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
1165
1166                 tap->wt_flags = 0;
1167                 tap->wt_rate = rate;
1168                 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1169                 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1170                 tap->wt_antenna = sc->tx_ant;
1171
1172                 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
1173         }
1174
1175         m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RT2573_TX_DESC_SIZE);
1176         rum_setup_tx_desc(sc, desc, flags, 0, m0->m_pkthdr.len, rate);
1177
1178         /* align end on a 4-bytes boundary */
1179         xferlen = (RT2573_TX_DESC_SIZE + m0->m_pkthdr.len + 3) & ~3;
1180
1181         /*
1182          * No space left in the last URB to store the extra 4 bytes, force
1183          * sending of another URB.
1184          */
1185         if ((xferlen % 64) == 0)
1186                 xferlen += 4;
1187
1188         DPRINTFN(10, ("sending mgt frame len=%d rate=%d xfer len=%d\n",
1189             m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, rate, xferlen));
1190         
1191         usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf, xferlen,
1192             USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RUM_TX_TIMEOUT, rum_txeof);
1193
1194         error = usbd_transfer(data->xfer);
1195         if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS) {
1196                 m_freem(m0);
1197                 data->m = NULL;
1198                 data->ni = NULL;
1199                 return error;
1200         }
1201
1202         sc->tx_queued++;
1203
1204         return 0;
1205 }
1206
1207 static int
1208 rum_tx_raw(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
1209     const struct ieee80211_bpf_params *params)
1210 {
1211         struct ieee80211com *ic = &sc->sc_ic;
1212         struct rum_tx_desc *desc;
1213         struct rum_tx_data *data;
1214         uint32_t flags;
1215         usbd_status error;
1216         int xferlen, rate;
1217
1218         data = &sc->tx_data[0];
1219         desc = (struct rum_tx_desc *)data->buf;
1220
1221         rate = params->ibp_rate0 & IEEE80211_RATE_VAL;
1222         /* XXX validate */
1223         if (rate == 0) {
1224                 m_freem(m0);
1225                 return EINVAL;
1226         }
1227
1228         if (bpf_peers_present(sc->sc_drvbpf)) {
1229                 struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
1230
1231                 tap->wt_flags = 0;
1232                 tap->wt_rate = rate;
1233                 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1234                 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1235                 tap->wt_antenna = sc->tx_ant;
1236
1237                 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
1238         }
1239
1240         data->m = m0;
1241         data->ni = ni;
1242
1243         flags = 0;
1244         if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
1245                 flags |= RT2573_TX_NEED_ACK;
1246
1247         m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RT2573_TX_DESC_SIZE);
1248         /* XXX need to setup descriptor ourself */
1249         rum_setup_tx_desc(sc, desc, flags, 0, m0->m_pkthdr.len, rate);
1250
1251         /* align end on a 4-bytes boundary */
1252         xferlen = (RT2573_TX_DESC_SIZE + m0->m_pkthdr.len + 3) & ~3;
1253
1254         /*
1255          * No space left in the last URB to store the extra 4 bytes, force
1256          * sending of another URB.
1257          */
1258         if ((xferlen % 64) == 0)
1259                 xferlen += 4;
1260
1261         DPRINTFN(10, ("sending raw frame len=%u rate=%u xfer len=%u\n",
1262             m0->m_pkthdr.len, rate, xferlen));
1263
1264         usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf,
1265             xferlen, USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RUM_TX_TIMEOUT,
1266             rum_txeof);
1267
1268         error = usbd_transfer(data->xfer);
1269         if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS)
1270                 return error;
1271
1272         sc->tx_queued++;
1273
1274         return 0;
1275 }
1276
1277 static int
1278 rum_tx_data(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1279 {
1280         struct ieee80211com *ic = &sc->sc_ic;
1281         struct rum_tx_desc *desc;
1282         struct rum_tx_data *data;
1283         struct ieee80211_frame *wh;
1284         struct ieee80211_key *k;
1285         uint32_t flags = 0;
1286         uint16_t dur;
1287         usbd_status error;
1288         int rate, xferlen;
1289
1290         wh = mtod(m0, struct ieee80211_frame *);
1291
1292         if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE)
1293                 rate = ic->ic_fixed_rate;
1294         else
1295                 rate = ni->ni_rates.rs_rates[ni->ni_txrate];
1296
1297         rate &= IEEE80211_RATE_VAL;
1298
1299         if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1300                 k = ieee80211_crypto_encap(ic, ni, m0);
1301                 if (k == NULL) {
1302                         m_freem(m0);
1303                         return ENOBUFS;
1304                 }
1305
1306                 /* packet header may have moved, reset our local pointer */
1307                 wh = mtod(m0, struct ieee80211_frame *);
1308         }
1309
1310         data = &sc->tx_data[0];
1311         desc = (struct rum_tx_desc *)data->buf;
1312
1313         data->m = m0;
1314         data->ni = ni;
1315
1316         if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1317                 flags |= RT2573_TX_NEED_ACK;
1318                 flags |= RT2573_TX_MORE_FRAG;
1319
1320                 dur = rum_txtime(RUM_ACK_SIZE, rum_ack_rate(ic, rate),
1321                     ic->ic_flags) + sc->sifs;
1322                 *(uint16_t *)wh->i_dur = htole16(dur);
1323         }
1324
1325         if (bpf_peers_present(sc->sc_drvbpf)) {
1326                 struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
1327
1328                 tap->wt_flags = 0;
1329                 tap->wt_rate = rate;
1330                 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1331                 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1332                 tap->wt_antenna = sc->tx_ant;
1333
1334                 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
1335         }
1336
1337         m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RT2573_TX_DESC_SIZE);
1338         rum_setup_tx_desc(sc, desc, flags, 0, m0->m_pkthdr.len, rate);
1339
1340         /* align end on a 4-bytes boundary */
1341         xferlen = (RT2573_TX_DESC_SIZE + m0->m_pkthdr.len + 3) & ~3;
1342
1343         /*
1344          * No space left in the last URB to store the extra 4 bytes, force
1345          * sending of another URB.
1346          */
1347         if ((xferlen % 64) == 0)
1348                 xferlen += 4;
1349
1350         DPRINTFN(10, ("sending frame len=%d rate=%d xfer len=%d\n",
1351             m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, rate, xferlen));
1352
1353         usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf, xferlen,
1354             USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RUM_TX_TIMEOUT, rum_txeof);
1355
1356         error = usbd_transfer(data->xfer);
1357         if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS) {
1358                 m_freem(m0);
1359                 data->m = NULL;
1360                 data->ni = NULL;
1361                 return error;
1362         }
1363
1364         sc->tx_queued++;
1365
1366         return 0;
1367 }
1368
1369 static void
1370 rum_start(struct ifnet *ifp)
1371 {
1372         struct rum_softc *sc = ifp->if_softc;
1373         struct ieee80211com *ic = &sc->sc_ic;
1374         struct ieee80211_node *ni;
1375         struct mbuf *m0;
1376         struct ether_header *eh;
1377
1378         for (;;) {
1379                 IF_POLL(&ic->ic_mgtq, m0);
1380                 if (m0 != NULL) {
1381                         if (sc->tx_queued >= RUM_TX_LIST_COUNT) {
1382                                 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1383                                 break;
1384                         }
1385                         IF_DEQUEUE(&ic->ic_mgtq, m0);
1386
1387                         ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
1388                         m0->m_pkthdr.rcvif = NULL;
1389
1390                         if (bpf_peers_present(ic->ic_rawbpf))
1391                                 bpf_mtap(ic->ic_rawbpf, m0);
1392
1393                         if (rum_tx_mgt(sc, m0, ni) != 0) {
1394                                 ieee80211_free_node(ni);
1395                                 break;
1396                         }
1397                 } else {
1398                         if (ic->ic_state != IEEE80211_S_RUN)
1399                                 break;
1400                         IFQ_DRV_DEQUEUE(&ifp->if_snd, m0);
1401                         if (m0 == NULL)
1402                                 break;
1403                         if (sc->tx_queued >= RUM_TX_LIST_COUNT) {
1404                                 IFQ_DRV_PREPEND(&ifp->if_snd, m0);
1405                                 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1406                                 break;
1407                         }
1408
1409                         if (m0->m_len < sizeof (struct ether_header) &&
1410                             !(m0 = m_pullup(m0, sizeof (struct ether_header))))
1411                                 continue;
1412
1413                         eh = mtod(m0, struct ether_header *);
1414                         ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1415                         if (ni == NULL) {
1416                                 m_freem(m0);
1417                                 continue;
1418                         }
1419                         BPF_MTAP(ifp, m0);
1420
1421                         m0 = ieee80211_encap(ic, m0, ni);
1422                         if (m0 == NULL) {
1423                                 ieee80211_free_node(ni);
1424                                 continue;
1425                         }
1426
1427                         if (bpf_peers_present(ic->ic_rawbpf))
1428                                 bpf_mtap(ic->ic_rawbpf, m0);
1429
1430                         if (rum_tx_data(sc, m0, ni) != 0) {
1431                                 ieee80211_free_node(ni);
1432                                 ifp->if_oerrors++;
1433                                 break;
1434                         }
1435                 }
1436
1437                 sc->sc_tx_timer = 5;
1438                 callout_reset(&sc->watchdog_ch, hz, rum_watchdog, sc);
1439         }
1440 }
1441
1442 static void
1443 rum_watchdog(void *arg)
1444 {
1445         struct rum_softc *sc = arg;
1446
1447         RUM_LOCK(sc);
1448
1449         if (sc->sc_tx_timer > 0) {
1450                 if (--sc->sc_tx_timer == 0) {
1451                         device_printf(sc->sc_dev, "device timeout\n");
1452                         /*rum_init(ifp); XXX needs a process context! */
1453                         sc->sc_ifp->if_oerrors++;
1454                         RUM_UNLOCK(sc);
1455                         return;
1456                 }
1457                 callout_reset(&sc->watchdog_ch, hz, rum_watchdog, sc);
1458         }
1459
1460         RUM_UNLOCK(sc);
1461 }
1462
1463 static int
1464 rum_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1465 {
1466         struct rum_softc *sc = ifp->if_softc;
1467         struct ieee80211com *ic = &sc->sc_ic;
1468         int error = 0;
1469
1470         RUM_LOCK(sc);
1471
1472         switch (cmd) {
1473         case SIOCSIFFLAGS:
1474                 if (ifp->if_flags & IFF_UP) {
1475                         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1476                                 rum_update_promisc(sc);
1477                         else
1478                                 rum_init(sc);
1479                 } else {
1480                         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1481                                 rum_stop(sc);
1482                 }
1483                 break;
1484         default:
1485                 error = ieee80211_ioctl(ic, cmd, data);
1486         }
1487
1488         if (error == ENETRESET) {
1489                 if ((ifp->if_flags & IFF_UP) &&
1490                     (ifp->if_drv_flags & IFF_DRV_RUNNING) &&
1491                     (ic->ic_roaming != IEEE80211_ROAMING_MANUAL))
1492                         rum_init(sc);
1493                 error = 0;
1494         }
1495
1496         RUM_UNLOCK(sc);
1497
1498         return error;
1499 }
1500
1501 static void
1502 rum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len)
1503 {
1504         usb_device_request_t req;
1505         usbd_status error;
1506
1507         req.bmRequestType = UT_READ_VENDOR_DEVICE;
1508         req.bRequest = RT2573_READ_EEPROM;
1509         USETW(req.wValue, 0);
1510         USETW(req.wIndex, addr);
1511         USETW(req.wLength, len);
1512
1513         error = usbd_do_request(sc->sc_udev, &req, buf);
1514         if (error != 0) {
1515                 printf("%s: could not read EEPROM: %s\n",
1516                     device_get_nameunit(sc->sc_dev), usbd_errstr(error));
1517         }
1518 }
1519
1520 static uint32_t
1521 rum_read(struct rum_softc *sc, uint16_t reg)
1522 {
1523         uint32_t val;
1524
1525         rum_read_multi(sc, reg, &val, sizeof val);
1526
1527         return le32toh(val);
1528 }
1529
1530 static void
1531 rum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len)
1532 {
1533         usb_device_request_t req;
1534         usbd_status error;
1535
1536         req.bmRequestType = UT_READ_VENDOR_DEVICE;
1537         req.bRequest = RT2573_READ_MULTI_MAC;
1538         USETW(req.wValue, 0);
1539         USETW(req.wIndex, reg);
1540         USETW(req.wLength, len);
1541
1542         error = usbd_do_request(sc->sc_udev, &req, buf);
1543         if (error != 0) {
1544                 printf("%s: could not multi read MAC register: %s\n",
1545                     device_get_nameunit(sc->sc_dev), usbd_errstr(error));
1546         }
1547 }
1548
1549 static void
1550 rum_write(struct rum_softc *sc, uint16_t reg, uint32_t val)
1551 {
1552         uint32_t tmp = htole32(val);
1553
1554         rum_write_multi(sc, reg, &tmp, sizeof tmp);
1555 }
1556
1557 static void
1558 rum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len)
1559 {
1560         usb_device_request_t req;
1561         usbd_status error;
1562
1563         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1564         req.bRequest = RT2573_WRITE_MULTI_MAC;
1565         USETW(req.wValue, 0);
1566         USETW(req.wIndex, reg);
1567         USETW(req.wLength, len);
1568
1569         error = usbd_do_request(sc->sc_udev, &req, buf);
1570         if (error != 0) {
1571                 printf("%s: could not multi write MAC register: %s\n",
1572                     device_get_nameunit(sc->sc_dev), usbd_errstr(error));
1573         }
1574 }
1575
1576 static void
1577 rum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val)
1578 {
1579         uint32_t tmp;
1580         int ntries;
1581
1582         for (ntries = 0; ntries < 5; ntries++) {
1583                 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1584                         break;
1585         }
1586         if (ntries == 5) {
1587                 printf("%s: could not write to BBP\n", 
1588                     device_get_nameunit(sc->sc_dev));
1589                 return;
1590         }
1591
1592         tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val;
1593         rum_write(sc, RT2573_PHY_CSR3, tmp);
1594 }
1595
1596 static uint8_t
1597 rum_bbp_read(struct rum_softc *sc, uint8_t reg)
1598 {
1599         uint32_t val;
1600         int ntries;
1601
1602         for (ntries = 0; ntries < 5; ntries++) {
1603                 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1604                         break;
1605         }
1606         if (ntries == 5) {
1607                 printf("%s: could not read BBP\n", 
1608                     device_get_nameunit(sc->sc_dev));
1609                 return 0;
1610         }
1611
1612         val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8;
1613         rum_write(sc, RT2573_PHY_CSR3, val);
1614
1615         for (ntries = 0; ntries < 100; ntries++) {
1616                 val = rum_read(sc, RT2573_PHY_CSR3);
1617                 if (!(val & RT2573_BBP_BUSY))
1618                         return val & 0xff;
1619                 DELAY(1);
1620         }
1621
1622         printf("%s: could not read BBP\n", device_get_nameunit(sc->sc_dev));
1623         return 0;
1624 }
1625
1626 static void
1627 rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val)
1628 {
1629         uint32_t tmp;
1630         int ntries;
1631
1632         for (ntries = 0; ntries < 5; ntries++) {
1633                 if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY))
1634                         break;
1635         }
1636         if (ntries == 5) {
1637                 printf("%s: could not write to RF\n", 
1638                     device_get_nameunit(sc->sc_dev));
1639                 return;
1640         }
1641
1642         tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 |
1643             (reg & 3);
1644         rum_write(sc, RT2573_PHY_CSR4, tmp);
1645
1646         /* remember last written value in sc */
1647         sc->rf_regs[reg] = val;
1648
1649         DPRINTFN(15, ("RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff));
1650 }
1651
1652 static void
1653 rum_select_antenna(struct rum_softc *sc)
1654 {
1655         uint8_t bbp4, bbp77;
1656         uint32_t tmp;
1657
1658         bbp4  = rum_bbp_read(sc, 4);
1659         bbp77 = rum_bbp_read(sc, 77);
1660
1661         /* TBD */
1662
1663         /* make sure Rx is disabled before switching antenna */
1664         tmp = rum_read(sc, RT2573_TXRX_CSR0);
1665         rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
1666
1667         rum_bbp_write(sc,  4, bbp4);
1668         rum_bbp_write(sc, 77, bbp77);
1669
1670         rum_write(sc, RT2573_TXRX_CSR0, tmp);
1671 }
1672
1673 /*
1674  * Enable multi-rate retries for frames sent at OFDM rates.
1675  * In 802.11b/g mode, allow fallback to CCK rates.
1676  */
1677 static void
1678 rum_enable_mrr(struct rum_softc *sc)
1679 {
1680         struct ieee80211com *ic = &sc->sc_ic;
1681         uint32_t tmp;
1682
1683         tmp = rum_read(sc, RT2573_TXRX_CSR4);
1684
1685         tmp &= ~RT2573_MRR_CCK_FALLBACK;
1686         if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan))
1687                 tmp |= RT2573_MRR_CCK_FALLBACK;
1688         tmp |= RT2573_MRR_ENABLED;
1689
1690         rum_write(sc, RT2573_TXRX_CSR4, tmp);
1691 }
1692
1693 static void
1694 rum_set_txpreamble(struct rum_softc *sc)
1695 {
1696         uint32_t tmp;
1697
1698         tmp = rum_read(sc, RT2573_TXRX_CSR4);
1699
1700         tmp &= ~RT2573_SHORT_PREAMBLE;
1701         if (sc->sc_ic.ic_flags & IEEE80211_F_SHPREAMBLE)
1702                 tmp |= RT2573_SHORT_PREAMBLE;
1703
1704         rum_write(sc, RT2573_TXRX_CSR4, tmp);
1705 }
1706
1707 static void
1708 rum_set_basicrates(struct rum_softc *sc)
1709 {
1710         struct ieee80211com *ic = &sc->sc_ic;
1711
1712         /* update basic rate set */
1713         if (ic->ic_curmode == IEEE80211_MODE_11B) {
1714                 /* 11b basic rates: 1, 2Mbps */
1715                 rum_write(sc, RT2573_TXRX_CSR5, 0x3);
1716         } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bss->ni_chan)) {
1717                 /* 11a basic rates: 6, 12, 24Mbps */
1718                 rum_write(sc, RT2573_TXRX_CSR5, 0x150);
1719         } else {
1720                 /* 11b/g basic rates: 1, 2, 5.5, 11Mbps */
1721                 rum_write(sc, RT2573_TXRX_CSR5, 0xf);
1722         }
1723 }
1724
1725 /*
1726  * Reprogram MAC/BBP to switch to a new band.  Values taken from the reference
1727  * driver.
1728  */
1729 static void
1730 rum_select_band(struct rum_softc *sc, struct ieee80211_channel *c)
1731 {
1732         uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104;
1733         uint32_t tmp;
1734
1735         /* update all BBP registers that depend on the band */
1736         bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c;
1737         bbp35 = 0x50; bbp97 = 0x48; bbp98  = 0x48;
1738         if (IEEE80211_IS_CHAN_5GHZ(c)) {
1739                 bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c;
1740                 bbp35 += 0x10; bbp97 += 0x10; bbp98  += 0x10;
1741         }
1742         if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1743             (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1744                 bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10;
1745         }
1746
1747         sc->bbp17 = bbp17;
1748         rum_bbp_write(sc,  17, bbp17);
1749         rum_bbp_write(sc,  96, bbp96);
1750         rum_bbp_write(sc, 104, bbp104);
1751
1752         if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1753             (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1754                 rum_bbp_write(sc, 75, 0x80);
1755                 rum_bbp_write(sc, 86, 0x80);
1756                 rum_bbp_write(sc, 88, 0x80);
1757         }
1758
1759         rum_bbp_write(sc, 35, bbp35);
1760         rum_bbp_write(sc, 97, bbp97);
1761         rum_bbp_write(sc, 98, bbp98);
1762
1763         tmp = rum_read(sc, RT2573_PHY_CSR0);
1764         tmp &= ~(RT2573_PA_PE_2GHZ | RT2573_PA_PE_5GHZ);
1765         if (IEEE80211_IS_CHAN_2GHZ(c))
1766                 tmp |= RT2573_PA_PE_2GHZ;
1767         else
1768                 tmp |= RT2573_PA_PE_5GHZ;
1769         rum_write(sc, RT2573_PHY_CSR0, tmp);
1770
1771         /* 802.11a uses a 16 microseconds short interframe space */
1772         sc->sifs = IEEE80211_IS_CHAN_5GHZ(c) ? 16 : 10;
1773 }
1774
1775 static void
1776 rum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c)
1777 {
1778         struct ieee80211com *ic = &sc->sc_ic;
1779         const struct rfprog *rfprog;
1780         uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT;
1781         int8_t power;
1782         u_int i, chan;
1783
1784         chan = ieee80211_chan2ieee(ic, c);
1785         if (chan == 0 || chan == IEEE80211_CHAN_ANY)
1786                 return;
1787
1788         /* select the appropriate RF settings based on what EEPROM says */
1789         rfprog = (sc->rf_rev == RT2573_RF_5225 ||
1790                   sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226;
1791
1792         /* find the settings for this channel (we know it exists) */
1793         for (i = 0; rfprog[i].chan != chan; i++);
1794
1795         power = sc->txpow[i];
1796         if (power < 0) {
1797                 bbp94 += power;
1798                 power = 0;
1799         } else if (power > 31) {
1800                 bbp94 += power - 31;
1801                 power = 31;
1802         }
1803
1804         /*
1805          * If we are switching from the 2GHz band to the 5GHz band or
1806          * vice-versa, BBP registers need to be reprogrammed.
1807          */
1808         if (c->ic_flags != ic->ic_curchan->ic_flags) {
1809                 rum_select_band(sc, c);
1810                 rum_select_antenna(sc);
1811         }
1812         ic->ic_curchan = c;
1813
1814         rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1815         rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1816         rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1817         rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1818
1819         rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1820         rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1821         rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7 | 1);
1822         rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1823
1824         rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1825         rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1826         rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1827         rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1828
1829         DELAY(10);
1830
1831         /* enable smart mode for MIMO-capable RFs */
1832         bbp3 = rum_bbp_read(sc, 3);
1833
1834         bbp3 &= ~RT2573_SMART_MODE;
1835         if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527)
1836                 bbp3 |= RT2573_SMART_MODE;
1837
1838         rum_bbp_write(sc, 3, bbp3);
1839
1840         if (bbp94 != RT2573_BBPR94_DEFAULT)
1841                 rum_bbp_write(sc, 94, bbp94);
1842 }
1843
1844 /*
1845  * Enable TSF synchronization and tell h/w to start sending beacons for IBSS
1846  * and HostAP operating modes.
1847  */
1848 static void
1849 rum_enable_tsf_sync(struct rum_softc *sc)
1850 {
1851         struct ieee80211com *ic = &sc->sc_ic;
1852         uint32_t tmp;
1853
1854         if (ic->ic_opmode != IEEE80211_M_STA) {
1855                 /*
1856                  * Change default 16ms TBTT adjustment to 8ms.
1857                  * Must be done before enabling beacon generation.
1858                  */
1859                 rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8);
1860         }
1861
1862         tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000;
1863
1864         /* set beacon interval (in 1/16ms unit) */
1865         tmp |= ic->ic_bss->ni_intval * 16;
1866
1867         tmp |= RT2573_TSF_TICKING | RT2573_ENABLE_TBTT;
1868         if (ic->ic_opmode == IEEE80211_M_STA)
1869                 tmp |= RT2573_TSF_MODE(1);
1870         else
1871                 tmp |= RT2573_TSF_MODE(2) | RT2573_GENERATE_BEACON;
1872
1873         rum_write(sc, RT2573_TXRX_CSR9, tmp);
1874 }
1875
1876 static void
1877 rum_update_slot(struct ifnet *ifp)
1878 {
1879         struct rum_softc *sc = ifp->if_softc;
1880         struct ieee80211com *ic = &sc->sc_ic;
1881         uint8_t slottime;
1882         uint32_t tmp;
1883
1884         slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
1885
1886         tmp = rum_read(sc, RT2573_MAC_CSR9);
1887         tmp = (tmp & ~0xff) | slottime;
1888         rum_write(sc, RT2573_MAC_CSR9, tmp);
1889
1890         DPRINTF(("setting slot time to %uus\n", slottime));
1891 }
1892
1893 static void
1894 rum_set_bssid(struct rum_softc *sc, const uint8_t *bssid)
1895 {
1896         uint32_t tmp;
1897
1898         tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24;
1899         rum_write(sc, RT2573_MAC_CSR4, tmp);
1900
1901         tmp = bssid[4] | bssid[5] << 8 | RT2573_ONE_BSSID << 16;
1902         rum_write(sc, RT2573_MAC_CSR5, tmp);
1903 }
1904
1905 static void
1906 rum_set_macaddr(struct rum_softc *sc, const uint8_t *addr)
1907 {
1908         uint32_t tmp;
1909
1910         tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24;
1911         rum_write(sc, RT2573_MAC_CSR2, tmp);
1912
1913         tmp = addr[4] | addr[5] << 8 | 0xff << 16;
1914         rum_write(sc, RT2573_MAC_CSR3, tmp);
1915 }
1916
1917 static void
1918 rum_update_promisc(struct rum_softc *sc)
1919 {
1920         struct ifnet *ifp = sc->sc_ic.ic_ifp;
1921         uint32_t tmp;
1922
1923         tmp = rum_read(sc, RT2573_TXRX_CSR0);
1924
1925         tmp &= ~RT2573_DROP_NOT_TO_ME;
1926         if (!(ifp->if_flags & IFF_PROMISC))
1927                 tmp |= RT2573_DROP_NOT_TO_ME;
1928
1929         rum_write(sc, RT2573_TXRX_CSR0, tmp);
1930
1931         DPRINTF(("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
1932             "entering" : "leaving"));
1933 }
1934
1935 static const char *
1936 rum_get_rf(int rev)
1937 {
1938         switch (rev) {
1939         case RT2573_RF_2527:    return "RT2527 (MIMO XR)";
1940         case RT2573_RF_2528:    return "RT2528";
1941         case RT2573_RF_5225:    return "RT5225 (MIMO XR)";
1942         case RT2573_RF_5226:    return "RT5226";
1943         default:                return "unknown";
1944         }
1945 }
1946
1947 static void
1948 rum_read_eeprom(struct rum_softc *sc)
1949 {
1950         struct ieee80211com *ic = &sc->sc_ic;
1951         uint16_t val;
1952 #ifdef RUM_DEBUG
1953         int i;
1954 #endif
1955
1956         /* read MAC address */
1957         rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, ic->ic_myaddr, 6);
1958
1959         rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2);
1960         val = le16toh(val);
1961         sc->rf_rev =   (val >> 11) & 0x1f;
1962         sc->hw_radio = (val >> 10) & 0x1;
1963         sc->rx_ant =   (val >> 4)  & 0x3;
1964         sc->tx_ant =   (val >> 2)  & 0x3;
1965         sc->nb_ant =   val & 0x3;
1966
1967         DPRINTF(("RF revision=%d\n", sc->rf_rev));
1968
1969         rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2);
1970         val = le16toh(val);
1971         sc->ext_5ghz_lna = (val >> 6) & 0x1;
1972         sc->ext_2ghz_lna = (val >> 4) & 0x1;
1973
1974         DPRINTF(("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n",
1975             sc->ext_2ghz_lna, sc->ext_5ghz_lna));
1976
1977         rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2);
1978         val = le16toh(val);
1979         if ((val & 0xff) != 0xff)
1980                 sc->rssi_2ghz_corr = (int8_t)(val & 0xff);      /* signed */
1981
1982         /* Only [-10, 10] is valid */
1983         if (sc->rssi_2ghz_corr < -10 || sc->rssi_2ghz_corr > 10)
1984                 sc->rssi_2ghz_corr = 0;
1985
1986         rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2);
1987         val = le16toh(val);
1988         if ((val & 0xff) != 0xff)
1989                 sc->rssi_5ghz_corr = (int8_t)(val & 0xff);      /* signed */
1990
1991         /* Only [-10, 10] is valid */
1992         if (sc->rssi_5ghz_corr < -10 || sc->rssi_5ghz_corr > 10)
1993                 sc->rssi_5ghz_corr = 0;
1994
1995         if (sc->ext_2ghz_lna)
1996                 sc->rssi_2ghz_corr -= 14;
1997         if (sc->ext_5ghz_lna)
1998                 sc->rssi_5ghz_corr -= 14;
1999
2000         DPRINTF(("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n",
2001             sc->rssi_2ghz_corr, sc->rssi_5ghz_corr));
2002
2003         rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2);
2004         val = le16toh(val);
2005         if ((val & 0xff) != 0xff)
2006                 sc->rffreq = val & 0xff;
2007
2008         DPRINTF(("RF freq=%d\n", sc->rffreq));
2009
2010         /* read Tx power for all a/b/g channels */
2011         rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14);
2012         /* XXX default Tx power for 802.11a channels */
2013         memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14);
2014 #ifdef RUM_DEBUG
2015         for (i = 0; i < 14; i++)
2016                 DPRINTF(("Channel=%d Tx power=%d\n", i + 1,  sc->txpow[i]));
2017 #endif
2018
2019         /* read default values for BBP registers */
2020         rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
2021 #ifdef RUM_DEBUG
2022         for (i = 0; i < 14; i++) {
2023                 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
2024                         continue;
2025                 DPRINTF(("BBP R%d=%02x\n", sc->bbp_prom[i].reg,
2026                     sc->bbp_prom[i].val));
2027         }
2028 #endif
2029 }
2030
2031 static int
2032 rum_bbp_init(struct rum_softc *sc)
2033 {
2034 #define N(a)    (sizeof (a) / sizeof ((a)[0]))
2035         int i, ntries;
2036
2037         /* wait for BBP to be ready */
2038         for (ntries = 0; ntries < 100; ntries++) {
2039                 const uint8_t val = rum_bbp_read(sc, 0);
2040                 if (val != 0 && val != 0xff)
2041                         break;
2042                 DELAY(1000);
2043         }
2044         if (ntries == 100) {
2045                 device_printf(sc->sc_dev, "timeout waiting for BBP\n");
2046                 return EIO;
2047         }
2048
2049         /* initialize BBP registers to default values */
2050         for (i = 0; i < N(rum_def_bbp); i++)
2051                 rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val);
2052
2053         /* write vendor-specific BBP values (from EEPROM) */
2054         for (i = 0; i < 16; i++) {
2055                 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
2056                         continue;
2057                 rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
2058         }
2059
2060         return 0;
2061 #undef N
2062 }
2063
2064 static void
2065 rum_init(void *priv)
2066 {
2067 #define N(a)    (sizeof (a) / sizeof ((a)[0]))
2068         struct rum_softc *sc = priv;
2069         struct ieee80211com *ic = &sc->sc_ic;
2070         struct ifnet *ifp = ic->ic_ifp;
2071         struct rum_rx_data *data;
2072         uint32_t tmp;
2073         usbd_status error;
2074         int i, ntries;
2075
2076         rum_stop(sc);
2077
2078         /* initialize MAC registers to default values */
2079         for (i = 0; i < N(rum_def_mac); i++)
2080                 rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val);
2081
2082         /* set host ready */
2083         rum_write(sc, RT2573_MAC_CSR1, 3);
2084         rum_write(sc, RT2573_MAC_CSR1, 0);
2085
2086         /* wait for BBP/RF to wakeup */
2087         for (ntries = 0; ntries < 1000; ntries++) {
2088                 if (rum_read(sc, RT2573_MAC_CSR12) & 8)
2089                         break;
2090                 rum_write(sc, RT2573_MAC_CSR12, 4);     /* force wakeup */
2091                 DELAY(1000);
2092         }
2093         if (ntries == 1000) {
2094                 printf("%s: timeout waiting for BBP/RF to wakeup\n",
2095                     device_get_nameunit(sc->sc_dev));
2096                 goto fail;
2097         }
2098
2099         if ((error = rum_bbp_init(sc)) != 0)
2100                 goto fail;
2101
2102         /* select default channel */
2103         rum_select_band(sc, ic->ic_curchan);
2104         rum_select_antenna(sc);
2105         rum_set_chan(sc, ic->ic_curchan);
2106
2107         /* clear STA registers */
2108         rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
2109
2110         IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
2111         rum_set_macaddr(sc, ic->ic_myaddr);
2112
2113         /* initialize ASIC */
2114         rum_write(sc, RT2573_MAC_CSR1, 4);
2115
2116         /*
2117          * Allocate xfer for AMRR statistics requests.
2118          */
2119         sc->amrr_xfer = usbd_alloc_xfer(sc->sc_udev);
2120         if (sc->amrr_xfer == NULL) {
2121                 printf("%s: could not allocate AMRR xfer\n",
2122                     device_get_nameunit(sc->sc_dev));
2123                 goto fail;
2124         }
2125
2126         /*
2127          * Open Tx and Rx USB bulk pipes.
2128          */
2129         error = usbd_open_pipe(sc->sc_iface, sc->sc_tx_no, USBD_EXCLUSIVE_USE,
2130             &sc->sc_tx_pipeh);
2131         if (error != 0) {
2132                 printf("%s: could not open Tx pipe: %s\n",
2133                     device_get_nameunit(sc->sc_dev), usbd_errstr(error));
2134                 goto fail;
2135         }
2136         error = usbd_open_pipe(sc->sc_iface, sc->sc_rx_no, USBD_EXCLUSIVE_USE,
2137             &sc->sc_rx_pipeh);
2138         if (error != 0) {
2139                 printf("%s: could not open Rx pipe: %s\n",
2140                     device_get_nameunit(sc->sc_dev), usbd_errstr(error));
2141                 goto fail;
2142         }
2143
2144         /*
2145          * Allocate Tx and Rx xfer queues.
2146          */
2147         error = rum_alloc_tx_list(sc);
2148         if (error != 0) {
2149                 printf("%s: could not allocate Tx list\n",
2150                     device_get_nameunit(sc->sc_dev));
2151                 goto fail;
2152         }
2153         error = rum_alloc_rx_list(sc);
2154         if (error != 0) {
2155                 printf("%s: could not allocate Rx list\n",
2156                     device_get_nameunit(sc->sc_dev));
2157                 goto fail;
2158         }
2159
2160         /*
2161          * Start up the receive pipe.
2162          */
2163         for (i = 0; i < RUM_RX_LIST_COUNT; i++) {
2164                 data = &sc->rx_data[i];
2165
2166                 usbd_setup_xfer(data->xfer, sc->sc_rx_pipeh, data, data->buf,
2167                     MCLBYTES, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rum_rxeof);
2168                 usbd_transfer(data->xfer);
2169         }
2170
2171         /* update Rx filter */
2172         tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff;
2173
2174         tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR;
2175         if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2176                 tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR |
2177                        RT2573_DROP_ACKCTS;
2178                 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2179                         tmp |= RT2573_DROP_TODS;
2180                 if (!(ifp->if_flags & IFF_PROMISC))
2181                         tmp |= RT2573_DROP_NOT_TO_ME;
2182         }
2183         rum_write(sc, RT2573_TXRX_CSR0, tmp);
2184
2185         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2186         ifp->if_drv_flags |= IFF_DRV_RUNNING;
2187
2188         if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2189                 if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
2190                         ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2191         } else
2192                 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2193
2194         return;
2195
2196 fail:   rum_stop(sc);
2197 #undef N
2198 }
2199
2200 static void
2201 rum_stop(void *priv)
2202 {
2203         struct rum_softc *sc = priv;
2204         struct ieee80211com *ic = &sc->sc_ic;
2205         struct ifnet *ifp = ic->ic_ifp;
2206         uint32_t tmp;
2207
2208         sc->sc_tx_timer = 0;
2209         ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2210
2211         ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2212
2213         /* disable Rx */
2214         tmp = rum_read(sc, RT2573_TXRX_CSR0);
2215         rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
2216
2217         /* reset ASIC */
2218         rum_write(sc, RT2573_MAC_CSR1, 3);
2219         rum_write(sc, RT2573_MAC_CSR1, 0);
2220
2221         if (sc->amrr_xfer != NULL) {
2222                 usbd_free_xfer(sc->amrr_xfer);
2223                 sc->amrr_xfer = NULL;
2224         }
2225
2226         if (sc->sc_rx_pipeh != NULL) {
2227                 usbd_abort_pipe(sc->sc_rx_pipeh);
2228                 usbd_close_pipe(sc->sc_rx_pipeh);
2229                 sc->sc_rx_pipeh = NULL;
2230         }
2231         if (sc->sc_tx_pipeh != NULL) {
2232                 usbd_abort_pipe(sc->sc_tx_pipeh);
2233                 usbd_close_pipe(sc->sc_tx_pipeh);
2234                 sc->sc_tx_pipeh = NULL;
2235         }
2236
2237         rum_free_rx_list(sc);
2238         rum_free_tx_list(sc);
2239 }
2240
2241 static int
2242 rum_load_microcode(struct rum_softc *sc, const u_char *ucode, size_t size)
2243 {
2244         usb_device_request_t req;
2245         uint16_t reg = RT2573_MCU_CODE_BASE;
2246         usbd_status error;
2247
2248         /* copy firmware image into NIC */
2249         for (; size >= 4; reg += 4, ucode += 4, size -= 4)
2250                 rum_write(sc, reg, UGETDW(ucode));
2251
2252         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2253         req.bRequest = RT2573_MCU_CNTL;
2254         USETW(req.wValue, RT2573_MCU_RUN);
2255         USETW(req.wIndex, 0);
2256         USETW(req.wLength, 0);
2257
2258         error = usbd_do_request(sc->sc_udev, &req, NULL);
2259         if (error != 0) {
2260                 printf("%s: could not run firmware: %s\n",
2261                     device_get_nameunit(sc->sc_dev), usbd_errstr(error));
2262         }
2263         return error;
2264 }
2265
2266 static int
2267 rum_prepare_beacon(struct rum_softc *sc)
2268 {
2269         struct ieee80211com *ic = &sc->sc_ic;
2270         struct rum_tx_desc desc;
2271         struct mbuf *m0;
2272         int rate;
2273
2274         m0 = ieee80211_beacon_alloc(ic, ic->ic_bss, &sc->sc_bo);
2275         if (m0 == NULL) {
2276                 return ENOBUFS;
2277         }
2278
2279         /* send beacons at the lowest available rate */
2280         rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
2281
2282         rum_setup_tx_desc(sc, &desc, RT2573_TX_TIMESTAMP, RT2573_TX_HWSEQ,
2283             m0->m_pkthdr.len, rate);
2284
2285         /* copy the first 24 bytes of Tx descriptor into NIC memory */
2286         rum_write_multi(sc, RT2573_HW_BEACON_BASE0, (uint8_t *)&desc, 24);
2287
2288         /* copy beacon header and payload into NIC memory */
2289         rum_write_multi(sc, RT2573_HW_BEACON_BASE0 + 24, mtod(m0, uint8_t *),
2290             m0->m_pkthdr.len);
2291
2292         m_freem(m0);
2293
2294         return 0;
2295 }
2296
2297 static int
2298 rum_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2299     const struct ieee80211_bpf_params *params)
2300 {
2301         struct ieee80211com *ic = ni->ni_ic;
2302         struct ifnet *ifp = ic->ic_ifp;
2303         struct rum_softc *sc = ifp->if_softc;
2304
2305         /* prevent management frames from being sent if we're not ready */
2306         if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2307                 m_freem(m);
2308                 ieee80211_free_node(ni);
2309                 return ENETDOWN;
2310         }
2311         if (sc->tx_queued >= RUM_TX_LIST_COUNT) {
2312                 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2313                 m_freem(m);
2314                 ieee80211_free_node(ni);
2315                 return EIO;
2316         }
2317
2318         if (bpf_peers_present(ic->ic_rawbpf))
2319                 bpf_mtap(ic->ic_rawbpf, m);
2320
2321         ifp->if_opackets++;
2322
2323         if (params == NULL) {
2324                 /*
2325                  * Legacy path; interpret frame contents to decide
2326                  * precisely how to send the frame.
2327                  */
2328                 if (rum_tx_mgt(sc, m, ni) != 0)
2329                         goto bad;
2330         } else {
2331                 /*
2332                  * Caller supplied explicit parameters to use in
2333                  * sending the frame.
2334                  */
2335                 if (rum_tx_raw(sc, m, ni, params) != 0)
2336                         goto bad;
2337         }
2338         sc->sc_tx_timer = 5;
2339         callout_reset(&sc->watchdog_ch, hz, rum_watchdog, sc);
2340
2341         return 0;
2342 bad:
2343         ifp->if_oerrors++;
2344         ieee80211_free_node(ni);
2345         return EIO;
2346 }
2347
2348 static void
2349 rum_amrr_start(struct rum_softc *sc, struct ieee80211_node *ni)
2350 {
2351         int i;
2352
2353         /* clear statistic registers (STA_CSR0 to STA_CSR5) */
2354         rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
2355
2356         ieee80211_amrr_node_init(&sc->amrr, &sc->amn);
2357
2358         /* set rate to some reasonable initial value */
2359         for (i = ni->ni_rates.rs_nrates - 1;
2360              i > 0 && (ni->ni_rates.rs_rates[i] & IEEE80211_RATE_VAL) > 72;
2361              i--);
2362         ni->ni_txrate = i;
2363
2364         callout_reset(&sc->amrr_ch, hz, rum_amrr_timeout, sc);
2365 }
2366
2367 static void
2368 rum_amrr_timeout(void *arg)
2369 {
2370         struct rum_softc *sc = (struct rum_softc *)arg;
2371         usb_device_request_t req;
2372
2373         /*
2374          * Asynchronously read statistic registers (cleared by read).
2375          */
2376         req.bmRequestType = UT_READ_VENDOR_DEVICE;
2377         req.bRequest = RT2573_READ_MULTI_MAC;
2378         USETW(req.wValue, 0);
2379         USETW(req.wIndex, RT2573_STA_CSR0);
2380         USETW(req.wLength, sizeof sc->sta);
2381
2382         usbd_setup_default_xfer(sc->amrr_xfer, sc->sc_udev, sc,
2383             USBD_DEFAULT_TIMEOUT, &req, sc->sta, sizeof sc->sta, 0,
2384             rum_amrr_update);
2385         (void)usbd_transfer(sc->amrr_xfer);
2386 }
2387
2388 static void
2389 rum_amrr_update(usbd_xfer_handle xfer, usbd_private_handle priv,
2390     usbd_status status)
2391 {
2392         struct rum_softc *sc = (struct rum_softc *)priv;
2393         struct ifnet *ifp = sc->sc_ic.ic_ifp;
2394
2395         if (status != USBD_NORMAL_COMPLETION) {
2396                 device_printf(sc->sc_dev, "could not retrieve Tx statistics - "
2397                     "cancelling automatic rate control\n");
2398                 return;
2399         }
2400
2401         /* count TX retry-fail as Tx errors */
2402         ifp->if_oerrors += le32toh(sc->sta[5]) >> 16;
2403
2404         sc->amn.amn_retrycnt =
2405             (le32toh(sc->sta[4]) >> 16) +       /* TX one-retry ok count */
2406             (le32toh(sc->sta[5]) & 0xffff) +    /* TX more-retry ok count */
2407             (le32toh(sc->sta[5]) >> 16);        /* TX retry-fail count */
2408
2409         sc->amn.amn_txcnt =
2410             sc->amn.amn_retrycnt +
2411             (le32toh(sc->sta[4]) & 0xffff);     /* TX no-retry ok count */
2412
2413         ieee80211_amrr_choose(&sc->amrr, sc->sc_ic.ic_bss, &sc->amn);
2414
2415         callout_reset(&sc->amrr_ch, hz, rum_amrr_timeout, sc);
2416 }
2417
2418 static void
2419 rum_scan_start(struct ieee80211com *ic)
2420 {
2421         struct rum_softc *sc = ic->ic_ifp->if_softc;
2422
2423         usb_rem_task(sc->sc_udev, &sc->sc_scantask);
2424
2425         /* do it in a process context */
2426         sc->sc_scan_action = RUM_SCAN_START;
2427         usb_add_task(sc->sc_udev, &sc->sc_scantask, USB_TASKQ_DRIVER);
2428 }
2429
2430 static void
2431 rum_scan_end(struct ieee80211com *ic)
2432 {
2433         struct rum_softc *sc = ic->ic_ifp->if_softc;
2434
2435         usb_rem_task(sc->sc_udev, &sc->sc_scantask);
2436
2437         /* do it in a process context */
2438         sc->sc_scan_action = RUM_SCAN_END;
2439         usb_add_task(sc->sc_udev, &sc->sc_scantask, USB_TASKQ_DRIVER);
2440 }
2441
2442 static void
2443 rum_set_channel(struct ieee80211com *ic)
2444 {
2445         struct rum_softc *sc = ic->ic_ifp->if_softc;
2446
2447         usb_rem_task(sc->sc_udev, &sc->sc_scantask);
2448
2449         /* do it in a process context */
2450         sc->sc_scan_action = RUM_SET_CHANNEL;
2451         usb_add_task(sc->sc_udev, &sc->sc_scantask, USB_TASKQ_DRIVER);
2452 }
2453
2454 static void
2455 rum_scantask(void *arg)
2456 {
2457         struct rum_softc *sc = arg;
2458         struct ieee80211com *ic = &sc->sc_ic;
2459         struct ifnet *ifp = ic->ic_ifp;
2460         uint32_t tmp;
2461
2462         RUM_LOCK(sc);
2463
2464         switch (sc->sc_scan_action) {
2465         case RUM_SCAN_START:
2466                 /* abort TSF synchronization */
2467                 tmp = rum_read(sc, RT2573_TXRX_CSR9);
2468                 rum_write(sc, RT2573_TXRX_CSR9, tmp & ~0x00ffffff);
2469                 rum_set_bssid(sc, ifp->if_broadcastaddr);
2470                 break;
2471
2472         case RUM_SCAN_END:
2473                 rum_enable_tsf_sync(sc);
2474                 /* XXX keep local copy */
2475                 rum_set_bssid(sc, ic->ic_bss->ni_bssid);
2476                 break;
2477
2478         case RUM_SET_CHANNEL:
2479                 mtx_lock(&Giant);
2480                 rum_set_chan(sc, ic->ic_curchan);
2481                 mtx_unlock(&Giant);
2482                 break;
2483
2484         default:
2485                 panic("unknown scan action %d\n", sc->sc_scan_action);
2486                 /* NEVER REACHED */
2487                 break;
2488         }
2489
2490         RUM_UNLOCK(sc);
2491 }
2492
2493 static int
2494 rum_get_rssi(struct rum_softc *sc, uint8_t raw)
2495 {
2496         int lna, agc, rssi;
2497
2498         lna = (raw >> 5) & 0x3;
2499         agc = raw & 0x1f;
2500
2501         if (lna == 0) {
2502                 /*
2503                  * No RSSI mapping
2504                  *
2505                  * NB: Since RSSI is relative to noise floor, -1 is
2506                  *     adequate for caller to know error happened.
2507                  */
2508                 return -1;
2509         }
2510
2511         rssi = (2 * agc) - RT2573_NOISE_FLOOR;
2512
2513         if (IEEE80211_IS_CHAN_2GHZ(sc->sc_ic.ic_curchan)) {
2514                 rssi += sc->rssi_2ghz_corr;
2515
2516                 if (lna == 1)
2517                         rssi -= 64;
2518                 else if (lna == 2)
2519                         rssi -= 74;
2520                 else if (lna == 3)
2521                         rssi -= 90;
2522         } else {
2523                 rssi += sc->rssi_5ghz_corr;
2524
2525                 if (!sc->ext_5ghz_lna && lna != 1)
2526                         rssi += 4;
2527
2528                 if (lna == 1)
2529                         rssi -= 64;
2530                 else if (lna == 2)
2531                         rssi -= 86;
2532                 else if (lna == 3)
2533                         rssi -= 100;
2534         }
2535         return rssi;
2536 }
2537
2538 static device_method_t rum_methods[] = {
2539         /* Device interface */
2540         DEVMETHOD(device_probe,         rum_match),
2541         DEVMETHOD(device_attach,        rum_attach),
2542         DEVMETHOD(device_detach,        rum_detach),
2543
2544         { 0, 0 }
2545 };
2546
2547 static driver_t rum_driver = {
2548         "rum",
2549         rum_methods,
2550         sizeof(struct rum_softc)
2551 };
2552
2553 static devclass_t rum_devclass;
2554
2555 DRIVER_MODULE(rum, uhub, rum_driver, rum_devclass, usbd_driver_load, 0);