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