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