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