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