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 * Copyright (c) 2015 Andriy Voskoboinyk <avos@FreeBSD.org>
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #include <sys/cdefs.h>
23 __FBSDID("$FreeBSD$");
26 * Ralink Technology RT2501USB/RT2601USB chipset driver
27 * http://www.ralinktech.com.tw/
32 #include <sys/param.h>
33 #include <sys/sockio.h>
34 #include <sys/sysctl.h>
36 #include <sys/mutex.h>
38 #include <sys/kernel.h>
39 #include <sys/socket.h>
40 #include <sys/systm.h>
41 #include <sys/malloc.h>
42 #include <sys/module.h>
44 #include <sys/endian.h>
49 #include <net/if_var.h>
50 #include <net/if_arp.h>
51 #include <net/ethernet.h>
52 #include <net/if_dl.h>
53 #include <net/if_media.h>
54 #include <net/if_types.h>
57 #include <netinet/in.h>
58 #include <netinet/in_systm.h>
59 #include <netinet/in_var.h>
60 #include <netinet/if_ether.h>
61 #include <netinet/ip.h>
64 #include <net80211/ieee80211_var.h>
65 #include <net80211/ieee80211_regdomain.h>
66 #include <net80211/ieee80211_radiotap.h>
67 #include <net80211/ieee80211_ratectl.h>
69 #include <dev/usb/usb.h>
70 #include <dev/usb/usbdi.h>
73 #define USB_DEBUG_VAR rum_debug
74 #include <dev/usb/usb_debug.h>
76 #include <dev/usb/wlan/if_rumreg.h>
77 #include <dev/usb/wlan/if_rumvar.h>
78 #include <dev/usb/wlan/if_rumfw.h>
81 static int rum_debug = 0;
83 static SYSCTL_NODE(_hw_usb, OID_AUTO, rum, CTLFLAG_RW, 0, "USB rum");
84 SYSCTL_INT(_hw_usb_rum, OID_AUTO, debug, CTLFLAG_RWTUN, &rum_debug, 0,
88 static const STRUCT_USB_HOST_ID rum_devs[] = {
89 #define RUM_DEV(v,p) { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
90 RUM_DEV(ABOCOM, HWU54DM),
91 RUM_DEV(ABOCOM, RT2573_2),
92 RUM_DEV(ABOCOM, RT2573_3),
93 RUM_DEV(ABOCOM, RT2573_4),
94 RUM_DEV(ABOCOM, WUG2700),
95 RUM_DEV(AMIT, CGWLUSB2GO),
96 RUM_DEV(ASUS, RT2573_1),
97 RUM_DEV(ASUS, RT2573_2),
98 RUM_DEV(BELKIN, F5D7050A),
99 RUM_DEV(BELKIN, F5D9050V3),
100 RUM_DEV(CISCOLINKSYS, WUSB54GC),
101 RUM_DEV(CISCOLINKSYS, WUSB54GR),
102 RUM_DEV(CONCEPTRONIC2, C54RU2),
103 RUM_DEV(COREGA, CGWLUSB2GL),
104 RUM_DEV(COREGA, CGWLUSB2GPX),
105 RUM_DEV(DICKSMITH, CWD854F),
106 RUM_DEV(DICKSMITH, RT2573),
107 RUM_DEV(EDIMAX, EW7318USG),
108 RUM_DEV(DLINK2, DWLG122C1),
109 RUM_DEV(DLINK2, WUA1340),
110 RUM_DEV(DLINK2, DWA111),
111 RUM_DEV(DLINK2, DWA110),
112 RUM_DEV(GIGABYTE, GNWB01GS),
113 RUM_DEV(GIGABYTE, GNWI05GS),
114 RUM_DEV(GIGASET, RT2573),
115 RUM_DEV(GOODWAY, RT2573),
116 RUM_DEV(GUILLEMOT, HWGUSB254LB),
117 RUM_DEV(GUILLEMOT, HWGUSB254V2AP),
118 RUM_DEV(HUAWEI3COM, WUB320G),
119 RUM_DEV(MELCO, G54HP),
120 RUM_DEV(MELCO, SG54HP),
121 RUM_DEV(MELCO, SG54HG),
122 RUM_DEV(MELCO, WLIUCG),
123 RUM_DEV(MELCO, WLRUCG),
124 RUM_DEV(MELCO, WLRUCGAOSS),
125 RUM_DEV(MSI, RT2573_1),
126 RUM_DEV(MSI, RT2573_2),
127 RUM_DEV(MSI, RT2573_3),
128 RUM_DEV(MSI, RT2573_4),
129 RUM_DEV(NOVATECH, RT2573),
130 RUM_DEV(PLANEX2, GWUS54HP),
131 RUM_DEV(PLANEX2, GWUS54MINI2),
132 RUM_DEV(PLANEX2, GWUSMM),
133 RUM_DEV(QCOM, RT2573),
134 RUM_DEV(QCOM, RT2573_2),
135 RUM_DEV(QCOM, RT2573_3),
136 RUM_DEV(RALINK, RT2573),
137 RUM_DEV(RALINK, RT2573_2),
138 RUM_DEV(RALINK, RT2671),
139 RUM_DEV(SITECOMEU, WL113R2),
140 RUM_DEV(SITECOMEU, WL172),
141 RUM_DEV(SPARKLAN, RT2573),
142 RUM_DEV(SURECOM, RT2573),
146 static device_probe_t rum_match;
147 static device_attach_t rum_attach;
148 static device_detach_t rum_detach;
150 static usb_callback_t rum_bulk_read_callback;
151 static usb_callback_t rum_bulk_write_callback;
153 static usb_error_t rum_do_request(struct rum_softc *sc,
154 struct usb_device_request *req, void *data);
155 static usb_error_t rum_do_mcu_request(struct rum_softc *sc, int);
156 static 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]);
160 static void rum_vap_delete(struct ieee80211vap *);
161 static void rum_cmdq_cb(void *, int);
162 static int rum_cmd_sleepable(struct rum_softc *, const void *,
163 size_t, uint8_t, CMD_FUNC_PROTO);
164 static void rum_tx_free(struct rum_tx_data *, int);
165 static void rum_setup_tx_list(struct rum_softc *);
166 static void rum_reset_tx_list(struct rum_softc *,
167 struct ieee80211vap *);
168 static void rum_unsetup_tx_list(struct rum_softc *);
169 static void rum_beacon_miss(struct ieee80211vap *);
170 static void rum_sta_recv_mgmt(struct ieee80211_node *,
172 const struct ieee80211_rx_stats *, int, int);
173 static int rum_set_power_state(struct rum_softc *, int);
174 static int rum_newstate(struct ieee80211vap *,
175 enum ieee80211_state, int);
176 static uint8_t rum_crypto_mode(struct rum_softc *, u_int, int);
177 static void rum_setup_tx_desc(struct rum_softc *,
178 struct rum_tx_desc *, struct ieee80211_key *,
179 uint32_t, uint8_t, uint8_t, int, int, int);
180 static uint32_t rum_tx_crypto_flags(struct rum_softc *,
181 struct ieee80211_node *,
182 const struct ieee80211_key *);
183 static int rum_tx_mgt(struct rum_softc *, struct mbuf *,
184 struct ieee80211_node *);
185 static int rum_tx_raw(struct rum_softc *, struct mbuf *,
186 struct ieee80211_node *,
187 const struct ieee80211_bpf_params *);
188 static int rum_tx_data(struct rum_softc *, struct mbuf *,
189 struct ieee80211_node *);
190 static int rum_transmit(struct ieee80211com *, struct mbuf *);
191 static void rum_start(struct rum_softc *);
192 static void rum_parent(struct ieee80211com *);
193 static void rum_eeprom_read(struct rum_softc *, uint16_t, void *,
195 static uint32_t rum_read(struct rum_softc *, uint16_t);
196 static void rum_read_multi(struct rum_softc *, uint16_t, void *,
198 static usb_error_t rum_write(struct rum_softc *, uint16_t, uint32_t);
199 static usb_error_t rum_write_multi(struct rum_softc *, uint16_t, void *,
201 static usb_error_t rum_setbits(struct rum_softc *, uint16_t, uint32_t);
202 static usb_error_t rum_clrbits(struct rum_softc *, uint16_t, uint32_t);
203 static usb_error_t rum_modbits(struct rum_softc *, uint16_t, uint32_t,
205 static int rum_bbp_busy(struct rum_softc *);
206 static void rum_bbp_write(struct rum_softc *, uint8_t, uint8_t);
207 static uint8_t rum_bbp_read(struct rum_softc *, uint8_t);
208 static void rum_rf_write(struct rum_softc *, uint8_t, uint32_t);
209 static void rum_select_antenna(struct rum_softc *);
210 static void rum_enable_mrr(struct rum_softc *);
211 static void rum_set_txpreamble(struct rum_softc *);
212 static void rum_set_basicrates(struct rum_softc *);
213 static void rum_select_band(struct rum_softc *,
214 struct ieee80211_channel *);
215 static void rum_set_chan(struct rum_softc *,
216 struct ieee80211_channel *);
217 static void rum_set_maxretry(struct rum_softc *,
218 struct ieee80211vap *);
219 static int rum_enable_tsf_sync(struct rum_softc *);
220 static void rum_enable_tsf(struct rum_softc *);
221 static void rum_abort_tsf_sync(struct rum_softc *);
222 static void rum_get_tsf(struct rum_softc *, uint64_t *);
223 static void rum_update_slot_cb(struct rum_softc *,
224 union sec_param *, uint8_t);
225 static void rum_update_slot(struct ieee80211com *);
226 static int rum_wme_update(struct ieee80211com *);
227 static void rum_set_bssid(struct rum_softc *, const uint8_t *);
228 static void rum_set_macaddr(struct rum_softc *, const uint8_t *);
229 static void rum_update_mcast(struct ieee80211com *);
230 static void rum_update_promisc(struct ieee80211com *);
231 static void rum_setpromisc(struct rum_softc *);
232 static const char *rum_get_rf(int);
233 static void rum_read_eeprom(struct rum_softc *);
234 static int rum_bbp_wakeup(struct rum_softc *);
235 static int rum_bbp_init(struct rum_softc *);
236 static void rum_clr_shkey_regs(struct rum_softc *);
237 static int rum_init(struct rum_softc *);
238 static void rum_stop(struct rum_softc *);
239 static void rum_load_microcode(struct rum_softc *, const uint8_t *,
241 static int rum_set_sleep_time(struct rum_softc *, uint16_t);
242 static int rum_reset(struct ieee80211vap *, u_long);
243 static int rum_set_beacon(struct rum_softc *,
244 struct ieee80211vap *);
245 static int rum_alloc_beacon(struct rum_softc *,
246 struct ieee80211vap *);
247 static void rum_update_beacon_cb(struct rum_softc *,
248 union sec_param *, uint8_t);
249 static void rum_update_beacon(struct ieee80211vap *, int);
250 static int rum_common_key_set(struct rum_softc *,
251 struct ieee80211_key *, uint16_t);
252 static void rum_group_key_set_cb(struct rum_softc *,
253 union sec_param *, uint8_t);
254 static void rum_group_key_del_cb(struct rum_softc *,
255 union sec_param *, uint8_t);
256 static void rum_pair_key_set_cb(struct rum_softc *,
257 union sec_param *, uint8_t);
258 static void rum_pair_key_del_cb(struct rum_softc *,
259 union sec_param *, uint8_t);
260 static int rum_key_alloc(struct ieee80211vap *,
261 struct ieee80211_key *, ieee80211_keyix *,
263 static int rum_key_set(struct ieee80211vap *,
264 const struct ieee80211_key *);
265 static int rum_key_delete(struct ieee80211vap *,
266 const struct ieee80211_key *);
267 static int rum_raw_xmit(struct ieee80211_node *, struct mbuf *,
268 const struct ieee80211_bpf_params *);
269 static void rum_scan_start(struct ieee80211com *);
270 static void rum_scan_end(struct ieee80211com *);
271 static void rum_set_channel(struct ieee80211com *);
272 static void rum_getradiocaps(struct ieee80211com *, int, int *,
273 struct ieee80211_channel[]);
274 static int rum_get_rssi(struct rum_softc *, uint8_t);
275 static void rum_ratectl_start(struct rum_softc *,
276 struct ieee80211_node *);
277 static void rum_ratectl_timeout(void *);
278 static void rum_ratectl_task(void *, int);
279 static int rum_pause(struct rum_softc *, int);
281 static const struct {
285 { RT2573_TXRX_CSR0, 0x025fb032 },
286 { RT2573_TXRX_CSR1, 0x9eaa9eaf },
287 { RT2573_TXRX_CSR2, 0x8a8b8c8d },
288 { RT2573_TXRX_CSR3, 0x00858687 },
289 { RT2573_TXRX_CSR7, 0x2e31353b },
290 { RT2573_TXRX_CSR8, 0x2a2a2a2c },
291 { RT2573_TXRX_CSR15, 0x0000000f },
292 { RT2573_MAC_CSR6, 0x00000fff },
293 { RT2573_MAC_CSR8, 0x016c030a },
294 { RT2573_MAC_CSR10, 0x00000718 },
295 { RT2573_MAC_CSR12, 0x00000004 },
296 { RT2573_MAC_CSR13, 0x00007f00 },
297 { RT2573_SEC_CSR2, 0x00000000 },
298 { RT2573_SEC_CSR3, 0x00000000 },
299 { RT2573_SEC_CSR4, 0x00000000 },
300 { RT2573_PHY_CSR1, 0x000023b0 },
301 { RT2573_PHY_CSR5, 0x00040a06 },
302 { RT2573_PHY_CSR6, 0x00080606 },
303 { RT2573_PHY_CSR7, 0x00000408 },
304 { RT2573_AIFSN_CSR, 0x00002273 },
305 { RT2573_CWMIN_CSR, 0x00002344 },
306 { RT2573_CWMAX_CSR, 0x000034aa }
309 static const struct {
341 static const uint8_t rum_chan_5ghz[] =
342 { 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64,
343 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140,
344 149, 153, 157, 161, 165 };
346 static const struct rfprog {
348 uint32_t r1, r2, r3, r4;
350 { 1, 0x00b03, 0x001e1, 0x1a014, 0x30282 },
351 { 2, 0x00b03, 0x001e1, 0x1a014, 0x30287 },
352 { 3, 0x00b03, 0x001e2, 0x1a014, 0x30282 },
353 { 4, 0x00b03, 0x001e2, 0x1a014, 0x30287 },
354 { 5, 0x00b03, 0x001e3, 0x1a014, 0x30282 },
355 { 6, 0x00b03, 0x001e3, 0x1a014, 0x30287 },
356 { 7, 0x00b03, 0x001e4, 0x1a014, 0x30282 },
357 { 8, 0x00b03, 0x001e4, 0x1a014, 0x30287 },
358 { 9, 0x00b03, 0x001e5, 0x1a014, 0x30282 },
359 { 10, 0x00b03, 0x001e5, 0x1a014, 0x30287 },
360 { 11, 0x00b03, 0x001e6, 0x1a014, 0x30282 },
361 { 12, 0x00b03, 0x001e6, 0x1a014, 0x30287 },
362 { 13, 0x00b03, 0x001e7, 0x1a014, 0x30282 },
363 { 14, 0x00b03, 0x001e8, 0x1a014, 0x30284 },
365 { 34, 0x00b03, 0x20266, 0x36014, 0x30282 },
366 { 38, 0x00b03, 0x20267, 0x36014, 0x30284 },
367 { 42, 0x00b03, 0x20268, 0x36014, 0x30286 },
368 { 46, 0x00b03, 0x20269, 0x36014, 0x30288 },
370 { 36, 0x00b03, 0x00266, 0x26014, 0x30288 },
371 { 40, 0x00b03, 0x00268, 0x26014, 0x30280 },
372 { 44, 0x00b03, 0x00269, 0x26014, 0x30282 },
373 { 48, 0x00b03, 0x0026a, 0x26014, 0x30284 },
374 { 52, 0x00b03, 0x0026b, 0x26014, 0x30286 },
375 { 56, 0x00b03, 0x0026c, 0x26014, 0x30288 },
376 { 60, 0x00b03, 0x0026e, 0x26014, 0x30280 },
377 { 64, 0x00b03, 0x0026f, 0x26014, 0x30282 },
379 { 100, 0x00b03, 0x0028a, 0x2e014, 0x30280 },
380 { 104, 0x00b03, 0x0028b, 0x2e014, 0x30282 },
381 { 108, 0x00b03, 0x0028c, 0x2e014, 0x30284 },
382 { 112, 0x00b03, 0x0028d, 0x2e014, 0x30286 },
383 { 116, 0x00b03, 0x0028e, 0x2e014, 0x30288 },
384 { 120, 0x00b03, 0x002a0, 0x2e014, 0x30280 },
385 { 124, 0x00b03, 0x002a1, 0x2e014, 0x30282 },
386 { 128, 0x00b03, 0x002a2, 0x2e014, 0x30284 },
387 { 132, 0x00b03, 0x002a3, 0x2e014, 0x30286 },
388 { 136, 0x00b03, 0x002a4, 0x2e014, 0x30288 },
389 { 140, 0x00b03, 0x002a6, 0x2e014, 0x30280 },
391 { 149, 0x00b03, 0x002a8, 0x2e014, 0x30287 },
392 { 153, 0x00b03, 0x002a9, 0x2e014, 0x30289 },
393 { 157, 0x00b03, 0x002ab, 0x2e014, 0x30281 },
394 { 161, 0x00b03, 0x002ac, 0x2e014, 0x30283 },
395 { 165, 0x00b03, 0x002ad, 0x2e014, 0x30285 }
397 { 1, 0x00b33, 0x011e1, 0x1a014, 0x30282 },
398 { 2, 0x00b33, 0x011e1, 0x1a014, 0x30287 },
399 { 3, 0x00b33, 0x011e2, 0x1a014, 0x30282 },
400 { 4, 0x00b33, 0x011e2, 0x1a014, 0x30287 },
401 { 5, 0x00b33, 0x011e3, 0x1a014, 0x30282 },
402 { 6, 0x00b33, 0x011e3, 0x1a014, 0x30287 },
403 { 7, 0x00b33, 0x011e4, 0x1a014, 0x30282 },
404 { 8, 0x00b33, 0x011e4, 0x1a014, 0x30287 },
405 { 9, 0x00b33, 0x011e5, 0x1a014, 0x30282 },
406 { 10, 0x00b33, 0x011e5, 0x1a014, 0x30287 },
407 { 11, 0x00b33, 0x011e6, 0x1a014, 0x30282 },
408 { 12, 0x00b33, 0x011e6, 0x1a014, 0x30287 },
409 { 13, 0x00b33, 0x011e7, 0x1a014, 0x30282 },
410 { 14, 0x00b33, 0x011e8, 0x1a014, 0x30284 },
412 { 34, 0x00b33, 0x01266, 0x26014, 0x30282 },
413 { 38, 0x00b33, 0x01267, 0x26014, 0x30284 },
414 { 42, 0x00b33, 0x01268, 0x26014, 0x30286 },
415 { 46, 0x00b33, 0x01269, 0x26014, 0x30288 },
417 { 36, 0x00b33, 0x01266, 0x26014, 0x30288 },
418 { 40, 0x00b33, 0x01268, 0x26014, 0x30280 },
419 { 44, 0x00b33, 0x01269, 0x26014, 0x30282 },
420 { 48, 0x00b33, 0x0126a, 0x26014, 0x30284 },
421 { 52, 0x00b33, 0x0126b, 0x26014, 0x30286 },
422 { 56, 0x00b33, 0x0126c, 0x26014, 0x30288 },
423 { 60, 0x00b33, 0x0126e, 0x26014, 0x30280 },
424 { 64, 0x00b33, 0x0126f, 0x26014, 0x30282 },
426 { 100, 0x00b33, 0x0128a, 0x2e014, 0x30280 },
427 { 104, 0x00b33, 0x0128b, 0x2e014, 0x30282 },
428 { 108, 0x00b33, 0x0128c, 0x2e014, 0x30284 },
429 { 112, 0x00b33, 0x0128d, 0x2e014, 0x30286 },
430 { 116, 0x00b33, 0x0128e, 0x2e014, 0x30288 },
431 { 120, 0x00b33, 0x012a0, 0x2e014, 0x30280 },
432 { 124, 0x00b33, 0x012a1, 0x2e014, 0x30282 },
433 { 128, 0x00b33, 0x012a2, 0x2e014, 0x30284 },
434 { 132, 0x00b33, 0x012a3, 0x2e014, 0x30286 },
435 { 136, 0x00b33, 0x012a4, 0x2e014, 0x30288 },
436 { 140, 0x00b33, 0x012a6, 0x2e014, 0x30280 },
438 { 149, 0x00b33, 0x012a8, 0x2e014, 0x30287 },
439 { 153, 0x00b33, 0x012a9, 0x2e014, 0x30289 },
440 { 157, 0x00b33, 0x012ab, 0x2e014, 0x30281 },
441 { 161, 0x00b33, 0x012ac, 0x2e014, 0x30283 },
442 { 165, 0x00b33, 0x012ad, 0x2e014, 0x30285 }
445 static const struct usb_config rum_config[RUM_N_TRANSFER] = {
448 .endpoint = UE_ADDR_ANY,
449 .direction = UE_DIR_OUT,
450 .bufsize = (MCLBYTES + RT2573_TX_DESC_SIZE + 8),
451 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
452 .callback = rum_bulk_write_callback,
453 .timeout = 5000, /* ms */
457 .endpoint = UE_ADDR_ANY,
458 .direction = UE_DIR_IN,
459 .bufsize = (MCLBYTES + RT2573_RX_DESC_SIZE),
460 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
461 .callback = rum_bulk_read_callback,
466 rum_match(device_t self)
468 struct usb_attach_arg *uaa = device_get_ivars(self);
470 if (uaa->usb_mode != USB_MODE_HOST)
472 if (uaa->info.bConfigIndex != 0)
474 if (uaa->info.bIfaceIndex != RT2573_IFACE_INDEX)
477 return (usbd_lookup_id_by_uaa(rum_devs, sizeof(rum_devs), uaa));
481 rum_attach(device_t self)
483 struct usb_attach_arg *uaa = device_get_ivars(self);
484 struct rum_softc *sc = device_get_softc(self);
485 struct ieee80211com *ic = &sc->sc_ic;
490 device_set_usb_desc(self);
491 sc->sc_udev = uaa->device;
495 RUM_CMDQ_LOCK_INIT(sc);
496 mbufq_init(&sc->sc_snd, ifqmaxlen);
498 iface_index = RT2573_IFACE_INDEX;
499 error = usbd_transfer_setup(uaa->device, &iface_index,
500 sc->sc_xfer, rum_config, RUM_N_TRANSFER, sc, &sc->sc_mtx);
502 device_printf(self, "could not allocate USB transfers, "
503 "err=%s\n", usbd_errstr(error));
508 /* retrieve RT2573 rev. no */
509 for (ntries = 0; ntries < 100; ntries++) {
510 if ((tmp = rum_read(sc, RT2573_MAC_CSR0)) != 0)
512 if (rum_pause(sc, hz / 100))
516 device_printf(sc->sc_dev, "timeout waiting for chip to settle\n");
521 /* retrieve MAC address and various other things from EEPROM */
524 device_printf(sc->sc_dev, "MAC/BBP RT2573 (rev 0x%05x), RF %s\n",
525 tmp, rum_get_rf(sc->rf_rev));
527 rum_load_microcode(sc, rt2573_ucode, sizeof(rt2573_ucode));
531 ic->ic_name = device_get_nameunit(self);
532 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
534 /* set device capabilities */
536 IEEE80211_C_STA /* station mode supported */
537 | IEEE80211_C_IBSS /* IBSS mode supported */
538 | IEEE80211_C_MONITOR /* monitor mode supported */
539 | IEEE80211_C_HOSTAP /* HostAp mode supported */
540 | IEEE80211_C_AHDEMO /* adhoc demo mode */
541 | IEEE80211_C_TXPMGT /* tx power management */
542 | IEEE80211_C_SHPREAMBLE /* short preamble supported */
543 | IEEE80211_C_SHSLOT /* short slot time supported */
544 | IEEE80211_C_BGSCAN /* bg scanning supported */
545 | IEEE80211_C_WPA /* 802.11i */
546 | IEEE80211_C_WME /* 802.11e */
547 | IEEE80211_C_PMGT /* Station-side power mgmt */
548 | IEEE80211_C_SWSLEEP /* net80211 managed power mgmt */
552 IEEE80211_CRYPTO_WEP |
553 IEEE80211_CRYPTO_AES_CCM |
554 IEEE80211_CRYPTO_TKIPMIC |
555 IEEE80211_CRYPTO_TKIP;
557 rum_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
560 ieee80211_ifattach(ic);
561 ic->ic_update_promisc = rum_update_promisc;
562 ic->ic_raw_xmit = rum_raw_xmit;
563 ic->ic_scan_start = rum_scan_start;
564 ic->ic_scan_end = rum_scan_end;
565 ic->ic_set_channel = rum_set_channel;
566 ic->ic_getradiocaps = rum_getradiocaps;
567 ic->ic_transmit = rum_transmit;
568 ic->ic_parent = rum_parent;
569 ic->ic_vap_create = rum_vap_create;
570 ic->ic_vap_delete = rum_vap_delete;
571 ic->ic_updateslot = rum_update_slot;
572 ic->ic_wme.wme_update = rum_wme_update;
573 ic->ic_update_mcast = rum_update_mcast;
575 ieee80211_radiotap_attach(ic,
576 &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
577 RT2573_TX_RADIOTAP_PRESENT,
578 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
579 RT2573_RX_RADIOTAP_PRESENT);
581 TASK_INIT(&sc->cmdq_task, 0, rum_cmdq_cb, sc);
584 ieee80211_announce(ic);
590 return (ENXIO); /* failure */
594 rum_detach(device_t self)
596 struct rum_softc *sc = device_get_softc(self);
597 struct ieee80211com *ic = &sc->sc_ic;
599 /* Prevent further ioctls */
604 /* stop all USB transfers */
605 usbd_transfer_unsetup(sc->sc_xfer, RUM_N_TRANSFER);
607 /* free TX list, if any */
609 rum_unsetup_tx_list(sc);
612 if (ic->ic_softc == sc) {
613 ieee80211_draintask(ic, &sc->cmdq_task);
614 ieee80211_ifdetach(ic);
617 mbufq_drain(&sc->sc_snd);
618 RUM_CMDQ_LOCK_DESTROY(sc);
619 RUM_LOCK_DESTROY(sc);
625 rum_do_request(struct rum_softc *sc,
626 struct usb_device_request *req, void *data)
632 err = usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
633 req, data, 0, NULL, 250 /* ms */);
637 DPRINTFN(1, "Control request failed, %s (retrying)\n",
639 if (rum_pause(sc, hz / 100))
646 rum_do_mcu_request(struct rum_softc *sc, int request)
648 struct usb_device_request req;
650 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
651 req.bRequest = RT2573_MCU_CNTL;
652 USETW(req.wValue, request);
653 USETW(req.wIndex, 0);
654 USETW(req.wLength, 0);
656 return (rum_do_request(sc, &req, NULL));
659 static struct ieee80211vap *
660 rum_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
661 enum ieee80211_opmode opmode, int flags,
662 const uint8_t bssid[IEEE80211_ADDR_LEN],
663 const uint8_t mac[IEEE80211_ADDR_LEN])
665 struct rum_softc *sc = ic->ic_softc;
667 struct ieee80211vap *vap;
669 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */
671 rvp = malloc(sizeof(struct rum_vap), M_80211_VAP, M_WAITOK | M_ZERO);
673 /* enable s/w bmiss handling for sta mode */
675 if (ieee80211_vap_setup(ic, vap, name, unit, opmode,
676 flags | IEEE80211_CLONE_NOBEACONS, bssid) != 0) {
678 free(rvp, M_80211_VAP);
682 /* override state transition machine */
683 rvp->newstate = vap->iv_newstate;
684 vap->iv_newstate = rum_newstate;
685 vap->iv_key_alloc = rum_key_alloc;
686 vap->iv_key_set = rum_key_set;
687 vap->iv_key_delete = rum_key_delete;
688 vap->iv_update_beacon = rum_update_beacon;
689 vap->iv_reset = rum_reset;
690 vap->iv_max_aid = RT2573_ADDR_MAX;
692 if (opmode == IEEE80211_M_STA) {
694 * Move device to the sleep state when
695 * beacon is received and there is no data for us.
697 * Used only for IEEE80211_S_SLEEP state.
699 rvp->recv_mgmt = vap->iv_recv_mgmt;
700 vap->iv_recv_mgmt = rum_sta_recv_mgmt;
702 /* Ignored while sleeping. */
703 rvp->bmiss = vap->iv_bmiss;
704 vap->iv_bmiss = rum_beacon_miss;
707 usb_callout_init_mtx(&rvp->ratectl_ch, &sc->sc_mtx, 0);
708 TASK_INIT(&rvp->ratectl_task, 0, rum_ratectl_task, rvp);
709 ieee80211_ratectl_init(vap);
710 ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */);
712 ieee80211_vap_attach(vap, ieee80211_media_change,
713 ieee80211_media_status, mac);
714 ic->ic_opmode = opmode;
719 rum_vap_delete(struct ieee80211vap *vap)
721 struct rum_vap *rvp = RUM_VAP(vap);
722 struct ieee80211com *ic = vap->iv_ic;
723 struct rum_softc *sc = ic->ic_softc;
725 /* Put vap into INIT state. */
726 ieee80211_new_state(vap, IEEE80211_S_INIT, -1);
727 ieee80211_draintask(ic, &vap->iv_nstate_task);
730 /* Cancel any unfinished Tx. */
731 rum_reset_tx_list(sc, vap);
734 usb_callout_drain(&rvp->ratectl_ch);
735 ieee80211_draintask(ic, &rvp->ratectl_task);
736 ieee80211_ratectl_deinit(vap);
737 ieee80211_vap_detach(vap);
738 m_freem(rvp->bcn_mbuf);
739 free(rvp, M_80211_VAP);
743 rum_cmdq_cb(void *arg, int pending)
745 struct rum_softc *sc = arg;
749 while (sc->cmdq[sc->cmdq_first].func != NULL) {
750 rc = &sc->cmdq[sc->cmdq_first];
754 rc->func(sc, &rc->data, rc->rvp_id);
758 memset(rc, 0, sizeof (*rc));
759 sc->cmdq_first = (sc->cmdq_first + 1) % RUM_CMDQ_SIZE;
765 rum_cmd_sleepable(struct rum_softc *sc, const void *ptr, size_t len,
766 uint8_t rvp_id, CMD_FUNC_PROTO)
768 struct ieee80211com *ic = &sc->sc_ic;
770 KASSERT(len <= sizeof(union sec_param), ("buffer overflow"));
773 if (sc->cmdq[sc->cmdq_last].func != NULL) {
774 device_printf(sc->sc_dev, "%s: cmdq overflow\n", __func__);
781 memcpy(&sc->cmdq[sc->cmdq_last].data, ptr, len);
782 sc->cmdq[sc->cmdq_last].rvp_id = rvp_id;
783 sc->cmdq[sc->cmdq_last].func = func;
784 sc->cmdq_last = (sc->cmdq_last + 1) % RUM_CMDQ_SIZE;
787 ieee80211_runtask(ic, &sc->cmdq_task);
793 rum_tx_free(struct rum_tx_data *data, int txerr)
795 struct rum_softc *sc = data->sc;
797 if (data->m != NULL) {
798 ieee80211_tx_complete(data->ni, data->m, txerr);
802 STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
807 rum_setup_tx_list(struct rum_softc *sc)
809 struct rum_tx_data *data;
813 STAILQ_INIT(&sc->tx_q);
814 STAILQ_INIT(&sc->tx_free);
816 for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
817 data = &sc->tx_data[i];
820 STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
826 rum_reset_tx_list(struct rum_softc *sc, struct ieee80211vap *vap)
828 struct rum_tx_data *data, *tmp;
830 KASSERT(vap != NULL, ("%s: vap is NULL\n", __func__));
832 STAILQ_FOREACH_SAFE(data, &sc->tx_q, next, tmp) {
833 if (data->ni != NULL && data->ni->ni_vap == vap) {
834 ieee80211_free_node(data->ni);
837 KASSERT(data->m != NULL, ("%s: m is NULL\n",
842 STAILQ_REMOVE(&sc->tx_q, data, rum_tx_data, next);
843 STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
850 rum_unsetup_tx_list(struct rum_softc *sc)
852 struct rum_tx_data *data;
855 /* make sure any subsequent use of the queues will fail */
857 STAILQ_INIT(&sc->tx_q);
858 STAILQ_INIT(&sc->tx_free);
860 /* free up all node references and mbufs */
861 for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
862 data = &sc->tx_data[i];
864 if (data->m != NULL) {
868 if (data->ni != NULL) {
869 ieee80211_free_node(data->ni);
876 rum_beacon_miss(struct ieee80211vap *vap)
878 struct ieee80211com *ic = vap->iv_ic;
879 struct rum_softc *sc = ic->ic_softc;
880 struct rum_vap *rvp = RUM_VAP(vap);
884 if (sc->sc_sleeping && sc->sc_sleep_end < ticks) {
885 DPRINTFN(12, "dropping 'sleeping' bit, "
886 "device must be awake now\n");
891 sleep = sc->sc_sleeping;
898 DPRINTFN(13, "bmiss event is ignored whilst sleeping\n");
903 rum_sta_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m, int subtype,
904 const struct ieee80211_rx_stats *rxs,
907 struct ieee80211vap *vap = ni->ni_vap;
908 struct rum_softc *sc = vap->iv_ic->ic_softc;
909 struct rum_vap *rvp = RUM_VAP(vap);
911 if (vap->iv_state == IEEE80211_S_SLEEP &&
912 subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
914 DPRINTFN(12, "beacon, mybss %d (flags %02X)\n",
915 !!(sc->last_rx_flags & RT2573_RX_MYBSS),
918 if ((sc->last_rx_flags & (RT2573_RX_MYBSS | RT2573_RX_BC)) ==
919 (RT2573_RX_MYBSS | RT2573_RX_BC)) {
921 * Put it to sleep here; in case if there is a data
922 * for us, iv_recv_mgmt() will wakeup the device via
923 * SLEEP -> RUN state transition.
925 rum_set_power_state(sc, 1);
930 rvp->recv_mgmt(ni, m, subtype, rxs, rssi, nf);
934 rum_set_power_state(struct rum_softc *sc, int sleep)
940 DPRINTFN(12, "moving to %s state (sleep time %u)\n",
941 sleep ? "sleep" : "awake", sc->sc_sleep_time);
943 uerror = rum_do_mcu_request(sc,
944 sleep ? RT2573_MCU_SLEEP : RT2573_MCU_WAKEUP);
945 if (uerror != USB_ERR_NORMAL_COMPLETION) {
946 device_printf(sc->sc_dev,
947 "%s: could not change power state: %s\n",
948 __func__, usbd_errstr(uerror));
952 sc->sc_sleeping = !!sleep;
953 sc->sc_sleep_end = sleep ? ticks + sc->sc_sleep_time : 0;
959 rum_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
961 struct rum_vap *rvp = RUM_VAP(vap);
962 struct ieee80211com *ic = vap->iv_ic;
963 struct rum_softc *sc = ic->ic_softc;
964 const struct ieee80211_txparam *tp;
965 enum ieee80211_state ostate;
966 struct ieee80211_node *ni;
970 ostate = vap->iv_state;
971 DPRINTF("%s -> %s\n",
972 ieee80211_state_name[ostate],
973 ieee80211_state_name[nstate]);
975 IEEE80211_UNLOCK(ic);
977 usb_callout_stop(&rvp->ratectl_ch);
979 if (ostate == IEEE80211_S_SLEEP && vap->iv_opmode == IEEE80211_M_STA) {
980 rum_clrbits(sc, RT2573_TXRX_CSR4, RT2573_ACKCTS_PWRMGT);
981 rum_clrbits(sc, RT2573_MAC_CSR11, RT2573_AUTO_WAKEUP);
985 * any subsequent TX will wakeup it anyway
987 (void) rum_set_power_state(sc, 0);
991 case IEEE80211_S_INIT:
992 if (ostate == IEEE80211_S_RUN)
993 rum_abort_tsf_sync(sc);
997 case IEEE80211_S_RUN:
998 if (ostate == IEEE80211_S_SLEEP)
999 break; /* already handled */
1001 ni = ieee80211_ref_node(vap->iv_bss);
1003 if (vap->iv_opmode != IEEE80211_M_MONITOR) {
1004 if (ic->ic_bsschan == IEEE80211_CHAN_ANYC ||
1005 ni->ni_chan == IEEE80211_CHAN_ANYC) {
1009 rum_update_slot_cb(sc, NULL, 0);
1011 rum_set_txpreamble(sc);
1012 rum_set_basicrates(sc);
1013 rum_set_maxretry(sc, vap);
1014 IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid);
1015 rum_set_bssid(sc, sc->sc_bssid);
1018 if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
1019 vap->iv_opmode == IEEE80211_M_IBSS) {
1020 if ((ret = rum_alloc_beacon(sc, vap)) != 0)
1024 if (vap->iv_opmode != IEEE80211_M_MONITOR &&
1025 vap->iv_opmode != IEEE80211_M_AHDEMO) {
1026 if ((ret = rum_enable_tsf_sync(sc)) != 0)
1031 /* enable automatic rate adaptation */
1032 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1033 if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
1034 rum_ratectl_start(sc, ni);
1036 ieee80211_free_node(ni);
1038 case IEEE80211_S_SLEEP:
1039 /* Implemented for STA mode only. */
1040 if (vap->iv_opmode != IEEE80211_M_STA)
1043 uerror = rum_setbits(sc, RT2573_MAC_CSR11, RT2573_AUTO_WAKEUP);
1044 if (uerror != USB_ERR_NORMAL_COMPLETION) {
1049 uerror = rum_setbits(sc, RT2573_TXRX_CSR4, RT2573_ACKCTS_PWRMGT);
1050 if (uerror != USB_ERR_NORMAL_COMPLETION) {
1055 ret = rum_set_power_state(sc, 1);
1057 device_printf(sc->sc_dev,
1058 "%s: could not move to the SLEEP state: %s\n",
1059 __func__, usbd_errstr(uerror));
1067 return (ret == 0 ? rvp->newstate(vap, nstate, arg) : ret);
1071 rum_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
1073 struct rum_softc *sc = usbd_xfer_softc(xfer);
1074 struct ieee80211vap *vap;
1075 struct rum_tx_data *data;
1077 struct usb_page_cache *pc;
1081 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
1083 switch (USB_GET_STATE(xfer)) {
1084 case USB_ST_TRANSFERRED:
1085 DPRINTFN(11, "transfer complete, %d bytes\n", actlen);
1087 /* free resources */
1088 data = usbd_xfer_get_priv(xfer);
1089 rum_tx_free(data, 0);
1090 usbd_xfer_set_priv(xfer, NULL);
1095 data = STAILQ_FIRST(&sc->tx_q);
1097 STAILQ_REMOVE_HEAD(&sc->tx_q, next);
1100 if (m->m_pkthdr.len > (int)(MCLBYTES + RT2573_TX_DESC_SIZE)) {
1101 DPRINTFN(0, "data overflow, %u bytes\n",
1103 m->m_pkthdr.len = (MCLBYTES + RT2573_TX_DESC_SIZE);
1105 pc = usbd_xfer_get_frame(xfer, 0);
1106 usbd_copy_in(pc, 0, &data->desc, RT2573_TX_DESC_SIZE);
1107 usbd_m_copy_in(pc, RT2573_TX_DESC_SIZE, m, 0,
1110 vap = data->ni->ni_vap;
1111 if (ieee80211_radiotap_active_vap(vap)) {
1112 struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
1115 tap->wt_rate = data->rate;
1116 tap->wt_antenna = sc->tx_ant;
1118 ieee80211_radiotap_tx(vap, m);
1121 /* align end on a 4-bytes boundary */
1122 len = (RT2573_TX_DESC_SIZE + m->m_pkthdr.len + 3) & ~3;
1123 if ((len % 64) == 0)
1126 DPRINTFN(11, "sending frame len=%u xferlen=%u\n",
1127 m->m_pkthdr.len, len);
1129 usbd_xfer_set_frame_len(xfer, 0, len);
1130 usbd_xfer_set_priv(xfer, data);
1132 usbd_transfer_submit(xfer);
1137 default: /* Error */
1138 DPRINTFN(11, "transfer error, %s\n",
1139 usbd_errstr(error));
1141 counter_u64_add(sc->sc_ic.ic_oerrors, 1);
1142 data = usbd_xfer_get_priv(xfer);
1144 rum_tx_free(data, error);
1145 usbd_xfer_set_priv(xfer, NULL);
1148 if (error != USB_ERR_CANCELLED) {
1149 if (error == USB_ERR_TIMEOUT)
1150 device_printf(sc->sc_dev, "device timeout\n");
1153 * Try to clear stall first, also if other
1154 * errors occur, hence clearing stall
1155 * introduces a 50 ms delay:
1157 usbd_xfer_set_stall(xfer);
1165 rum_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
1167 struct rum_softc *sc = usbd_xfer_softc(xfer);
1168 struct ieee80211com *ic = &sc->sc_ic;
1169 struct ieee80211_frame_min *wh;
1170 struct ieee80211_node *ni;
1171 struct epoch_tracker et;
1172 struct mbuf *m = NULL;
1173 struct usb_page_cache *pc;
1178 usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
1180 switch (USB_GET_STATE(xfer)) {
1181 case USB_ST_TRANSFERRED:
1183 DPRINTFN(15, "rx done, actlen=%d\n", len);
1185 if (len < RT2573_RX_DESC_SIZE) {
1186 DPRINTF("%s: xfer too short %d\n",
1187 device_get_nameunit(sc->sc_dev), len);
1188 counter_u64_add(ic->ic_ierrors, 1);
1192 len -= RT2573_RX_DESC_SIZE;
1193 pc = usbd_xfer_get_frame(xfer, 0);
1194 usbd_copy_out(pc, 0, &sc->sc_rx_desc, RT2573_RX_DESC_SIZE);
1196 rssi = rum_get_rssi(sc, sc->sc_rx_desc.rssi);
1197 flags = le32toh(sc->sc_rx_desc.flags);
1198 sc->last_rx_flags = flags;
1199 if (len < ((flags >> 16) & 0xfff)) {
1200 DPRINTFN(5, "%s: frame is truncated from %d to %d "
1201 "bytes\n", device_get_nameunit(sc->sc_dev),
1202 (flags >> 16) & 0xfff, len);
1203 counter_u64_add(ic->ic_ierrors, 1);
1206 len = (flags >> 16) & 0xfff;
1207 if (len < sizeof(struct ieee80211_frame_ack)) {
1208 DPRINTFN(5, "%s: frame too short %d\n",
1209 device_get_nameunit(sc->sc_dev), len);
1210 counter_u64_add(ic->ic_ierrors, 1);
1213 if (flags & RT2573_RX_CRC_ERROR) {
1215 * This should not happen since we did not
1216 * request to receive those frames when we
1217 * filled RUM_TXRX_CSR2:
1219 DPRINTFN(5, "PHY or CRC error\n");
1220 counter_u64_add(ic->ic_ierrors, 1);
1223 if ((flags & RT2573_RX_DEC_MASK) != RT2573_RX_DEC_OK) {
1224 switch (flags & RT2573_RX_DEC_MASK) {
1225 case RT2573_RX_IV_ERROR:
1226 DPRINTFN(5, "IV/EIV error\n");
1228 case RT2573_RX_MIC_ERROR:
1229 DPRINTFN(5, "MIC error\n");
1231 case RT2573_RX_KEY_ERROR:
1232 DPRINTFN(5, "Key error\n");
1235 counter_u64_add(ic->ic_ierrors, 1);
1239 m = m_get2(len, M_NOWAIT, MT_DATA, M_PKTHDR);
1241 DPRINTF("could not allocate mbuf\n");
1242 counter_u64_add(ic->ic_ierrors, 1);
1245 usbd_copy_out(pc, RT2573_RX_DESC_SIZE,
1246 mtod(m, uint8_t *), len);
1248 wh = mtod(m, struct ieee80211_frame_min *);
1250 if ((wh->i_fc[1] & IEEE80211_FC1_PROTECTED) &&
1251 (flags & RT2573_RX_CIP_MASK) !=
1252 RT2573_RX_CIP_MODE(RT2573_MODE_NOSEC)) {
1253 wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
1254 m->m_flags |= M_WEP;
1258 m->m_pkthdr.len = m->m_len = len;
1260 if (ieee80211_radiotap_active(ic)) {
1261 struct rum_rx_radiotap_header *tap = &sc->sc_rxtap;
1264 tap->wr_rate = ieee80211_plcp2rate(sc->sc_rx_desc.rate,
1265 (flags & RT2573_RX_OFDM) ?
1266 IEEE80211_T_OFDM : IEEE80211_T_CCK);
1267 rum_get_tsf(sc, &tap->wr_tsf);
1268 tap->wr_antsignal = RT2573_NOISE_FLOOR + rssi;
1269 tap->wr_antnoise = RT2573_NOISE_FLOOR;
1270 tap->wr_antenna = sc->rx_ant;
1275 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1276 usbd_transfer_submit(xfer);
1279 * At the end of a USB callback it is always safe to unlock
1280 * the private mutex of a device! That is why we do the
1281 * "ieee80211_input" here, and not some lines up!
1285 if (m->m_len >= sizeof(struct ieee80211_frame_min))
1286 ni = ieee80211_find_rxnode(ic, wh);
1290 NET_EPOCH_ENTER(et);
1292 (void) ieee80211_input(ni, m, rssi,
1293 RT2573_NOISE_FLOOR);
1294 ieee80211_free_node(ni);
1296 (void) ieee80211_input_all(ic, m, rssi,
1297 RT2573_NOISE_FLOOR);
1304 default: /* Error */
1305 if (error != USB_ERR_CANCELLED) {
1306 /* try to clear stall first */
1307 usbd_xfer_set_stall(xfer);
1315 rum_plcp_signal(int rate)
1318 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1319 case 12: return 0xb;
1320 case 18: return 0xf;
1321 case 24: return 0xa;
1322 case 36: return 0xe;
1323 case 48: return 0x9;
1324 case 72: return 0xd;
1325 case 96: return 0x8;
1326 case 108: return 0xc;
1328 /* CCK rates (NB: not IEEE std, device-specific) */
1331 case 11: return 0x2;
1332 case 22: return 0x3;
1334 return 0xff; /* XXX unsupported/unknown rate */
1338 * Map net80211 cipher to RT2573 security mode.
1341 rum_crypto_mode(struct rum_softc *sc, u_int cipher, int keylen)
1344 case IEEE80211_CIPHER_WEP:
1345 return (keylen < 8 ? RT2573_MODE_WEP40 : RT2573_MODE_WEP104);
1346 case IEEE80211_CIPHER_TKIP:
1347 return RT2573_MODE_TKIP;
1348 case IEEE80211_CIPHER_AES_CCM:
1349 return RT2573_MODE_AES_CCMP;
1351 device_printf(sc->sc_dev, "unknown cipher %d\n", cipher);
1357 rum_setup_tx_desc(struct rum_softc *sc, struct rum_tx_desc *desc,
1358 struct ieee80211_key *k, uint32_t flags, uint8_t xflags, uint8_t qid,
1359 int hdrlen, int len, int rate)
1361 struct ieee80211com *ic = &sc->sc_ic;
1362 struct wmeParams *wmep = &sc->wme_params[qid];
1363 uint16_t plcp_length;
1366 flags |= RT2573_TX_VALID;
1369 if (k != NULL && !(k->wk_flags & IEEE80211_KEY_SWCRYPT)) {
1370 const struct ieee80211_cipher *cip = k->wk_cipher;
1372 len += cip->ic_header + cip->ic_trailer + cip->ic_miclen;
1374 desc->eiv = 0; /* for WEP */
1375 cip->ic_setiv(k, (uint8_t *)&desc->iv);
1378 /* setup PLCP fields */
1379 desc->plcp_signal = rum_plcp_signal(rate);
1380 desc->plcp_service = 4;
1382 len += IEEE80211_CRC_LEN;
1383 if (ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) {
1384 flags |= RT2573_TX_OFDM;
1386 plcp_length = len & 0xfff;
1387 desc->plcp_length_hi = plcp_length >> 6;
1388 desc->plcp_length_lo = plcp_length & 0x3f;
1391 rate = 2; /* avoid division by zero */
1392 plcp_length = howmany(16 * len, rate);
1394 remainder = (16 * len) % 22;
1395 if (remainder != 0 && remainder < 7)
1396 desc->plcp_service |= RT2573_PLCP_LENGEXT;
1398 desc->plcp_length_hi = plcp_length >> 8;
1399 desc->plcp_length_lo = plcp_length & 0xff;
1401 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1402 desc->plcp_signal |= 0x08;
1405 desc->flags = htole32(flags);
1406 desc->hdrlen = hdrlen;
1407 desc->xflags = xflags;
1409 desc->wme = htole16(RT2573_QID(qid) |
1410 RT2573_AIFSN(wmep->wmep_aifsn) |
1411 RT2573_LOGCWMIN(wmep->wmep_logcwmin) |
1412 RT2573_LOGCWMAX(wmep->wmep_logcwmax));
1416 rum_sendprot(struct rum_softc *sc,
1417 const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate)
1419 struct ieee80211com *ic = ni->ni_ic;
1420 struct rum_tx_data *data;
1422 int protrate, flags;
1424 RUM_LOCK_ASSERT(sc);
1426 mprot = ieee80211_alloc_prot(ni, m, rate, prot);
1427 if (mprot == NULL) {
1428 if_inc_counter(ni->ni_vap->iv_ifp, IFCOUNTER_OERRORS, 1);
1429 device_printf(sc->sc_dev,
1430 "could not allocate mbuf for protection mode %d\n", prot);
1434 protrate = ieee80211_ctl_rate(ic->ic_rt, rate);
1436 if (prot == IEEE80211_PROT_RTSCTS)
1437 flags |= RT2573_TX_NEED_ACK;
1439 data = STAILQ_FIRST(&sc->tx_free);
1440 STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1444 data->ni = ieee80211_ref_node(ni);
1445 data->rate = protrate;
1446 rum_setup_tx_desc(sc, &data->desc, NULL, flags, 0, 0, 0,
1447 mprot->m_pkthdr.len, protrate);
1449 STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1450 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1456 rum_tx_crypto_flags(struct rum_softc *sc, struct ieee80211_node *ni,
1457 const struct ieee80211_key *k)
1459 struct ieee80211vap *vap = ni->ni_vap;
1464 if (!(k->wk_flags & IEEE80211_KEY_SWCRYPT)) {
1465 cipher = k->wk_cipher->ic_cipher;
1467 mode = rum_crypto_mode(sc, cipher, k->wk_keylen);
1471 flags |= RT2573_TX_CIP_MODE(mode);
1473 /* Do not trust GROUP flag */
1474 if (!(k >= &vap->iv_nw_keys[0] &&
1475 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID]))
1476 flags |= RT2573_TX_KEY_PAIR;
1478 pos += 0 * RT2573_SKEY_MAX; /* vap id */
1480 flags |= RT2573_TX_KEY_ID(pos);
1482 if (cipher == IEEE80211_CIPHER_TKIP)
1483 flags |= RT2573_TX_TKIPMIC;
1490 rum_tx_mgt(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1492 const struct ieee80211_txparam *tp = ni->ni_txparms;
1493 struct ieee80211com *ic = &sc->sc_ic;
1494 struct rum_tx_data *data;
1495 struct ieee80211_frame *wh;
1496 struct ieee80211_key *k = NULL;
1499 uint8_t ac, type, xflags = 0;
1502 RUM_LOCK_ASSERT(sc);
1504 data = STAILQ_FIRST(&sc->tx_free);
1505 STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1508 wh = mtod(m0, struct ieee80211_frame *);
1509 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1510 hdrlen = ieee80211_anyhdrsize(wh);
1511 ac = M_WME_GETAC(m0);
1513 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1514 k = ieee80211_crypto_get_txkey(ni, m0);
1518 if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) &&
1519 !k->wk_cipher->ic_encap(k, m0))
1522 wh = mtod(m0, struct ieee80211_frame *);
1525 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1526 flags |= RT2573_TX_NEED_ACK;
1528 dur = ieee80211_ack_duration(ic->ic_rt, tp->mgmtrate,
1529 ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1530 USETW(wh->i_dur, dur);
1532 /* tell hardware to add timestamp for probe responses */
1533 if (type == IEEE80211_FC0_TYPE_MGT &&
1534 (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
1535 IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1536 flags |= RT2573_TX_TIMESTAMP;
1539 if (type != IEEE80211_FC0_TYPE_CTL && !IEEE80211_QOS_HAS_SEQ(wh))
1540 xflags |= RT2573_TX_HWSEQ;
1543 flags |= rum_tx_crypto_flags(sc, ni, k);
1547 data->rate = tp->mgmtrate;
1549 rum_setup_tx_desc(sc, &data->desc, k, flags, xflags, ac, hdrlen,
1550 m0->m_pkthdr.len, tp->mgmtrate);
1552 DPRINTFN(10, "sending mgt frame len=%d rate=%d\n",
1553 m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, tp->mgmtrate);
1555 STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1556 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1562 rum_tx_raw(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
1563 const struct ieee80211_bpf_params *params)
1565 struct ieee80211com *ic = ni->ni_ic;
1566 struct ieee80211_frame *wh;
1567 struct rum_tx_data *data;
1569 uint8_t ac, type, xflags = 0;
1572 RUM_LOCK_ASSERT(sc);
1574 wh = mtod(m0, struct ieee80211_frame *);
1575 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1577 ac = params->ibp_pri & 3;
1579 rate = params->ibp_rate0;
1580 if (!ieee80211_isratevalid(ic->ic_rt, rate))
1584 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
1585 flags |= RT2573_TX_NEED_ACK;
1586 if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) {
1587 error = rum_sendprot(sc, m0, ni,
1588 params->ibp_flags & IEEE80211_BPF_RTS ?
1589 IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY,
1591 if (error || sc->tx_nfree == 0)
1594 flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
1597 if (type != IEEE80211_FC0_TYPE_CTL && !IEEE80211_QOS_HAS_SEQ(wh))
1598 xflags |= RT2573_TX_HWSEQ;
1600 data = STAILQ_FIRST(&sc->tx_free);
1601 STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1608 /* XXX need to setup descriptor ourself */
1609 rum_setup_tx_desc(sc, &data->desc, NULL, flags, xflags, ac, 0,
1610 m0->m_pkthdr.len, rate);
1612 DPRINTFN(10, "sending raw frame len=%u rate=%u\n",
1613 m0->m_pkthdr.len, rate);
1615 STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1616 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1622 rum_tx_data(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1624 struct ieee80211vap *vap = ni->ni_vap;
1625 struct ieee80211com *ic = &sc->sc_ic;
1626 struct rum_tx_data *data;
1627 struct ieee80211_frame *wh;
1628 const struct ieee80211_txparam *tp = ni->ni_txparms;
1629 struct ieee80211_key *k = NULL;
1632 uint8_t ac, type, qos, xflags = 0;
1633 int error, hdrlen, rate;
1635 RUM_LOCK_ASSERT(sc);
1637 wh = mtod(m0, struct ieee80211_frame *);
1638 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1639 hdrlen = ieee80211_anyhdrsize(wh);
1641 if (IEEE80211_QOS_HAS_SEQ(wh))
1642 qos = ((const struct ieee80211_qosframe *)wh)->i_qos[0];
1645 ac = M_WME_GETAC(m0);
1647 if (m0->m_flags & M_EAPOL)
1648 rate = tp->mgmtrate;
1649 else if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1650 rate = tp->mcastrate;
1651 else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
1652 rate = tp->ucastrate;
1654 (void) ieee80211_ratectl_rate(ni, NULL, 0);
1655 rate = ni->ni_txrate;
1658 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1659 k = ieee80211_crypto_get_txkey(ni, m0);
1664 if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) &&
1665 !k->wk_cipher->ic_encap(k, m0)) {
1670 /* packet header may have moved, reset our local pointer */
1671 wh = mtod(m0, struct ieee80211_frame *);
1674 if (type != IEEE80211_FC0_TYPE_CTL && !IEEE80211_QOS_HAS_SEQ(wh))
1675 xflags |= RT2573_TX_HWSEQ;
1677 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1678 int prot = IEEE80211_PROT_NONE;
1679 if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold)
1680 prot = IEEE80211_PROT_RTSCTS;
1681 else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
1682 ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM)
1683 prot = ic->ic_protmode;
1684 if (prot != IEEE80211_PROT_NONE) {
1685 error = rum_sendprot(sc, m0, ni, prot, rate);
1686 if (error || sc->tx_nfree == 0) {
1690 flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
1695 flags |= rum_tx_crypto_flags(sc, ni, k);
1697 data = STAILQ_FIRST(&sc->tx_free);
1698 STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1705 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1706 /* Unicast frame, check if an ACK is expected. */
1707 if (!qos || (qos & IEEE80211_QOS_ACKPOLICY) !=
1708 IEEE80211_QOS_ACKPOLICY_NOACK)
1709 flags |= RT2573_TX_NEED_ACK;
1711 dur = ieee80211_ack_duration(ic->ic_rt, rate,
1712 ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1713 USETW(wh->i_dur, dur);
1716 rum_setup_tx_desc(sc, &data->desc, k, flags, xflags, ac, hdrlen,
1717 m0->m_pkthdr.len, rate);
1719 DPRINTFN(10, "sending frame len=%d rate=%d\n",
1720 m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, rate);
1722 STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1723 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1729 rum_transmit(struct ieee80211com *ic, struct mbuf *m)
1731 struct rum_softc *sc = ic->ic_softc;
1735 if (!sc->sc_running) {
1739 error = mbufq_enqueue(&sc->sc_snd, m);
1751 rum_start(struct rum_softc *sc)
1753 struct ieee80211_node *ni;
1756 RUM_LOCK_ASSERT(sc);
1758 if (!sc->sc_running)
1761 while (sc->tx_nfree >= RUM_TX_MINFREE &&
1762 (m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
1763 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1764 if (rum_tx_data(sc, m, ni) != 0) {
1765 if_inc_counter(ni->ni_vap->iv_ifp,
1766 IFCOUNTER_OERRORS, 1);
1767 ieee80211_free_node(ni);
1774 rum_parent(struct ieee80211com *ic)
1776 struct rum_softc *sc = ic->ic_softc;
1777 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1780 if (sc->sc_detached) {
1786 if (ic->ic_nrunning > 0) {
1787 if (rum_init(sc) == 0)
1788 ieee80211_start_all(ic);
1790 ieee80211_stop(vap);
1796 rum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len)
1798 struct usb_device_request req;
1801 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1802 req.bRequest = RT2573_READ_EEPROM;
1803 USETW(req.wValue, 0);
1804 USETW(req.wIndex, addr);
1805 USETW(req.wLength, len);
1807 error = rum_do_request(sc, &req, buf);
1809 device_printf(sc->sc_dev, "could not read EEPROM: %s\n",
1810 usbd_errstr(error));
1815 rum_read(struct rum_softc *sc, uint16_t reg)
1819 rum_read_multi(sc, reg, &val, sizeof val);
1821 return le32toh(val);
1825 rum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len)
1827 struct usb_device_request req;
1830 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1831 req.bRequest = RT2573_READ_MULTI_MAC;
1832 USETW(req.wValue, 0);
1833 USETW(req.wIndex, reg);
1834 USETW(req.wLength, len);
1836 error = rum_do_request(sc, &req, buf);
1838 device_printf(sc->sc_dev,
1839 "could not multi read MAC register: %s\n",
1840 usbd_errstr(error));
1845 rum_write(struct rum_softc *sc, uint16_t reg, uint32_t val)
1847 uint32_t tmp = htole32(val);
1849 return (rum_write_multi(sc, reg, &tmp, sizeof tmp));
1853 rum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len)
1855 struct usb_device_request req;
1859 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1860 req.bRequest = RT2573_WRITE_MULTI_MAC;
1861 USETW(req.wValue, 0);
1863 /* write at most 64 bytes at a time */
1864 for (offset = 0; offset < len; offset += 64) {
1865 USETW(req.wIndex, reg + offset);
1866 USETW(req.wLength, MIN(len - offset, 64));
1868 error = rum_do_request(sc, &req, (char *)buf + offset);
1870 device_printf(sc->sc_dev,
1871 "could not multi write MAC register: %s\n",
1872 usbd_errstr(error));
1877 return (USB_ERR_NORMAL_COMPLETION);
1881 rum_setbits(struct rum_softc *sc, uint16_t reg, uint32_t mask)
1883 return (rum_write(sc, reg, rum_read(sc, reg) | mask));
1887 rum_clrbits(struct rum_softc *sc, uint16_t reg, uint32_t mask)
1889 return (rum_write(sc, reg, rum_read(sc, reg) & ~mask));
1893 rum_modbits(struct rum_softc *sc, uint16_t reg, uint32_t set, uint32_t unset)
1895 return (rum_write(sc, reg, (rum_read(sc, reg) & ~unset) | set));
1899 rum_bbp_busy(struct rum_softc *sc)
1903 for (ntries = 0; ntries < 100; ntries++) {
1904 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1906 if (rum_pause(sc, hz / 100))
1916 rum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val)
1920 DPRINTFN(2, "reg=0x%08x\n", reg);
1922 if (rum_bbp_busy(sc) != 0) {
1923 device_printf(sc->sc_dev, "could not write to BBP\n");
1927 tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val;
1928 rum_write(sc, RT2573_PHY_CSR3, tmp);
1932 rum_bbp_read(struct rum_softc *sc, uint8_t reg)
1937 DPRINTFN(2, "reg=0x%08x\n", reg);
1939 if (rum_bbp_busy(sc) != 0) {
1940 device_printf(sc->sc_dev, "could not read BBP\n");
1944 val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8;
1945 rum_write(sc, RT2573_PHY_CSR3, val);
1947 for (ntries = 0; ntries < 100; ntries++) {
1948 val = rum_read(sc, RT2573_PHY_CSR3);
1949 if (!(val & RT2573_BBP_BUSY))
1951 if (rum_pause(sc, hz / 100))
1955 device_printf(sc->sc_dev, "could not read BBP\n");
1960 rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val)
1965 for (ntries = 0; ntries < 100; ntries++) {
1966 if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY))
1968 if (rum_pause(sc, hz / 100))
1971 if (ntries == 100) {
1972 device_printf(sc->sc_dev, "could not write to RF\n");
1976 tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 |
1978 rum_write(sc, RT2573_PHY_CSR4, tmp);
1980 /* remember last written value in sc */
1981 sc->rf_regs[reg] = val;
1983 DPRINTFN(15, "RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff);
1987 rum_select_antenna(struct rum_softc *sc)
1989 uint8_t bbp4, bbp77;
1992 bbp4 = rum_bbp_read(sc, 4);
1993 bbp77 = rum_bbp_read(sc, 77);
1997 /* make sure Rx is disabled before switching antenna */
1998 tmp = rum_read(sc, RT2573_TXRX_CSR0);
1999 rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
2001 rum_bbp_write(sc, 4, bbp4);
2002 rum_bbp_write(sc, 77, bbp77);
2004 rum_write(sc, RT2573_TXRX_CSR0, tmp);
2008 * Enable multi-rate retries for frames sent at OFDM rates.
2009 * In 802.11b/g mode, allow fallback to CCK rates.
2012 rum_enable_mrr(struct rum_softc *sc)
2014 struct ieee80211com *ic = &sc->sc_ic;
2016 if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) {
2017 rum_setbits(sc, RT2573_TXRX_CSR4,
2018 RT2573_MRR_ENABLED | RT2573_MRR_CCK_FALLBACK);
2020 rum_modbits(sc, RT2573_TXRX_CSR4,
2021 RT2573_MRR_ENABLED, RT2573_MRR_CCK_FALLBACK);
2026 rum_set_txpreamble(struct rum_softc *sc)
2028 struct ieee80211com *ic = &sc->sc_ic;
2030 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
2031 rum_setbits(sc, RT2573_TXRX_CSR4, RT2573_SHORT_PREAMBLE);
2033 rum_clrbits(sc, RT2573_TXRX_CSR4, RT2573_SHORT_PREAMBLE);
2037 rum_set_basicrates(struct rum_softc *sc)
2039 struct ieee80211com *ic = &sc->sc_ic;
2041 /* update basic rate set */
2042 if (ic->ic_curmode == IEEE80211_MODE_11B) {
2043 /* 11b basic rates: 1, 2Mbps */
2044 rum_write(sc, RT2573_TXRX_CSR5, 0x3);
2045 } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) {
2046 /* 11a basic rates: 6, 12, 24Mbps */
2047 rum_write(sc, RT2573_TXRX_CSR5, 0x150);
2049 /* 11b/g basic rates: 1, 2, 5.5, 11Mbps */
2050 rum_write(sc, RT2573_TXRX_CSR5, 0xf);
2055 * Reprogram MAC/BBP to switch to a new band. Values taken from the reference
2059 rum_select_band(struct rum_softc *sc, struct ieee80211_channel *c)
2061 uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104;
2063 /* update all BBP registers that depend on the band */
2064 bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c;
2065 bbp35 = 0x50; bbp97 = 0x48; bbp98 = 0x48;
2066 if (IEEE80211_IS_CHAN_5GHZ(c)) {
2067 bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c;
2068 bbp35 += 0x10; bbp97 += 0x10; bbp98 += 0x10;
2070 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
2071 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
2072 bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10;
2076 rum_bbp_write(sc, 17, bbp17);
2077 rum_bbp_write(sc, 96, bbp96);
2078 rum_bbp_write(sc, 104, bbp104);
2080 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
2081 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
2082 rum_bbp_write(sc, 75, 0x80);
2083 rum_bbp_write(sc, 86, 0x80);
2084 rum_bbp_write(sc, 88, 0x80);
2087 rum_bbp_write(sc, 35, bbp35);
2088 rum_bbp_write(sc, 97, bbp97);
2089 rum_bbp_write(sc, 98, bbp98);
2091 if (IEEE80211_IS_CHAN_2GHZ(c)) {
2092 rum_modbits(sc, RT2573_PHY_CSR0, RT2573_PA_PE_2GHZ,
2095 rum_modbits(sc, RT2573_PHY_CSR0, RT2573_PA_PE_5GHZ,
2101 rum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c)
2103 struct ieee80211com *ic = &sc->sc_ic;
2104 const struct rfprog *rfprog;
2105 uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT;
2109 chan = ieee80211_chan2ieee(ic, c);
2110 if (chan == 0 || chan == IEEE80211_CHAN_ANY)
2113 /* select the appropriate RF settings based on what EEPROM says */
2114 rfprog = (sc->rf_rev == RT2573_RF_5225 ||
2115 sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226;
2117 /* find the settings for this channel (we know it exists) */
2118 for (i = 0; rfprog[i].chan != chan; i++);
2120 power = sc->txpow[i];
2124 } else if (power > 31) {
2125 bbp94 += power - 31;
2130 * If we are switching from the 2GHz band to the 5GHz band or
2131 * vice-versa, BBP registers need to be reprogrammed.
2133 if (c->ic_flags != ic->ic_curchan->ic_flags) {
2134 rum_select_band(sc, c);
2135 rum_select_antenna(sc);
2139 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
2140 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
2141 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
2142 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
2144 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
2145 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
2146 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7 | 1);
2147 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
2149 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
2150 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
2151 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
2152 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
2154 rum_pause(sc, hz / 100);
2156 /* enable smart mode for MIMO-capable RFs */
2157 bbp3 = rum_bbp_read(sc, 3);
2159 bbp3 &= ~RT2573_SMART_MODE;
2160 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527)
2161 bbp3 |= RT2573_SMART_MODE;
2163 rum_bbp_write(sc, 3, bbp3);
2165 if (bbp94 != RT2573_BBPR94_DEFAULT)
2166 rum_bbp_write(sc, 94, bbp94);
2168 /* give the chip some extra time to do the switchover */
2169 rum_pause(sc, hz / 100);
2173 rum_set_maxretry(struct rum_softc *sc, struct ieee80211vap *vap)
2175 struct ieee80211_node *ni = vap->iv_bss;
2176 const struct ieee80211_txparam *tp = ni->ni_txparms;
2177 struct rum_vap *rvp = RUM_VAP(vap);
2179 rvp->maxretry = MIN(tp->maxretry, 0xf);
2181 rum_modbits(sc, RT2573_TXRX_CSR4, RT2573_SHORT_RETRY(rvp->maxretry) |
2182 RT2573_LONG_RETRY(rvp->maxretry),
2183 RT2573_SHORT_RETRY_MASK | RT2573_LONG_RETRY_MASK);
2187 * Enable TSF synchronization and tell h/w to start sending beacons for IBSS
2188 * and HostAP operating modes.
2191 rum_enable_tsf_sync(struct rum_softc *sc)
2193 struct ieee80211com *ic = &sc->sc_ic;
2194 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2198 if (vap->iv_opmode != IEEE80211_M_STA) {
2200 * Change default 16ms TBTT adjustment to 8ms.
2201 * Must be done before enabling beacon generation.
2203 if (rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8) != 0)
2207 tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000;
2209 /* set beacon interval (in 1/16ms unit) */
2210 bintval = vap->iv_bss->ni_intval;
2211 tmp |= bintval * 16;
2212 tmp |= RT2573_TSF_TIMER_EN | RT2573_TBTT_TIMER_EN;
2214 switch (vap->iv_opmode) {
2215 case IEEE80211_M_STA:
2217 * Local TSF is always updated with remote TSF on beacon
2220 tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_STA);
2222 case IEEE80211_M_IBSS:
2224 * Local TSF is updated with remote TSF on beacon reception
2225 * only if the remote TSF is greater than local TSF.
2227 tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_IBSS);
2228 tmp |= RT2573_BCN_TX_EN;
2230 case IEEE80211_M_HOSTAP:
2231 /* SYNC with nobody */
2232 tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_HOSTAP);
2233 tmp |= RT2573_BCN_TX_EN;
2236 device_printf(sc->sc_dev,
2237 "Enabling TSF failed. undefined opmode %d\n",
2242 if (rum_write(sc, RT2573_TXRX_CSR9, tmp) != 0)
2245 /* refresh current sleep time */
2246 return (rum_set_sleep_time(sc, bintval));
2250 rum_enable_tsf(struct rum_softc *sc)
2252 rum_modbits(sc, RT2573_TXRX_CSR9, RT2573_TSF_TIMER_EN |
2253 RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_DIS), 0x00ffffff);
2257 rum_abort_tsf_sync(struct rum_softc *sc)
2259 rum_clrbits(sc, RT2573_TXRX_CSR9, 0x00ffffff);
2263 rum_get_tsf(struct rum_softc *sc, uint64_t *buf)
2265 rum_read_multi(sc, RT2573_TXRX_CSR12, buf, sizeof (*buf));
2269 rum_update_slot_cb(struct rum_softc *sc, union sec_param *data, uint8_t rvp_id)
2271 struct ieee80211com *ic = &sc->sc_ic;
2274 slottime = IEEE80211_GET_SLOTTIME(ic);
2276 rum_modbits(sc, RT2573_MAC_CSR9, slottime, 0xff);
2278 DPRINTF("setting slot time to %uus\n", slottime);
2282 rum_update_slot(struct ieee80211com *ic)
2284 rum_cmd_sleepable(ic->ic_softc, NULL, 0, 0, rum_update_slot_cb);
2288 rum_wme_update(struct ieee80211com *ic)
2290 struct chanAccParams chp;
2291 const struct wmeParams *chanp;
2292 struct rum_softc *sc = ic->ic_softc;
2295 ieee80211_wme_ic_getparams(ic, &chp);
2296 chanp = chp.cap_wmeParams;
2299 error = rum_write(sc, RT2573_AIFSN_CSR,
2300 chanp[WME_AC_VO].wmep_aifsn << 12 |
2301 chanp[WME_AC_VI].wmep_aifsn << 8 |
2302 chanp[WME_AC_BK].wmep_aifsn << 4 |
2303 chanp[WME_AC_BE].wmep_aifsn);
2306 error = rum_write(sc, RT2573_CWMIN_CSR,
2307 chanp[WME_AC_VO].wmep_logcwmin << 12 |
2308 chanp[WME_AC_VI].wmep_logcwmin << 8 |
2309 chanp[WME_AC_BK].wmep_logcwmin << 4 |
2310 chanp[WME_AC_BE].wmep_logcwmin);
2313 error = rum_write(sc, RT2573_CWMAX_CSR,
2314 chanp[WME_AC_VO].wmep_logcwmax << 12 |
2315 chanp[WME_AC_VI].wmep_logcwmax << 8 |
2316 chanp[WME_AC_BK].wmep_logcwmax << 4 |
2317 chanp[WME_AC_BE].wmep_logcwmax);
2320 error = rum_write(sc, RT2573_TXOP01_CSR,
2321 chanp[WME_AC_BK].wmep_txopLimit << 16 |
2322 chanp[WME_AC_BE].wmep_txopLimit);
2325 error = rum_write(sc, RT2573_TXOP23_CSR,
2326 chanp[WME_AC_VO].wmep_txopLimit << 16 |
2327 chanp[WME_AC_VI].wmep_txopLimit);
2331 memcpy(sc->wme_params, chanp, sizeof(*chanp) * WME_NUM_AC);
2336 device_printf(sc->sc_dev, "%s: WME update failed, error %d\n",
2344 rum_set_bssid(struct rum_softc *sc, const uint8_t *bssid)
2347 rum_write(sc, RT2573_MAC_CSR4,
2348 bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24);
2349 rum_write(sc, RT2573_MAC_CSR5,
2350 bssid[4] | bssid[5] << 8 | RT2573_NUM_BSSID_MSK(1));
2354 rum_set_macaddr(struct rum_softc *sc, const uint8_t *addr)
2357 rum_write(sc, RT2573_MAC_CSR2,
2358 addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
2359 rum_write(sc, RT2573_MAC_CSR3,
2360 addr[4] | addr[5] << 8 | 0xff << 16);
2364 rum_setpromisc(struct rum_softc *sc)
2366 struct ieee80211com *ic = &sc->sc_ic;
2368 if (ic->ic_promisc == 0)
2369 rum_setbits(sc, RT2573_TXRX_CSR0, RT2573_DROP_NOT_TO_ME);
2371 rum_clrbits(sc, RT2573_TXRX_CSR0, RT2573_DROP_NOT_TO_ME);
2373 DPRINTF("%s promiscuous mode\n", ic->ic_promisc > 0 ?
2374 "entering" : "leaving");
2378 rum_update_promisc(struct ieee80211com *ic)
2380 struct rum_softc *sc = ic->ic_softc;
2389 rum_update_mcast(struct ieee80211com *ic)
2398 case RT2573_RF_2527: return "RT2527 (MIMO XR)";
2399 case RT2573_RF_2528: return "RT2528";
2400 case RT2573_RF_5225: return "RT5225 (MIMO XR)";
2401 case RT2573_RF_5226: return "RT5226";
2402 default: return "unknown";
2407 rum_read_eeprom(struct rum_softc *sc)
2414 /* read MAC address */
2415 rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, sc->sc_ic.ic_macaddr, 6);
2417 rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2);
2419 sc->rf_rev = (val >> 11) & 0x1f;
2420 sc->hw_radio = (val >> 10) & 0x1;
2421 sc->rx_ant = (val >> 4) & 0x3;
2422 sc->tx_ant = (val >> 2) & 0x3;
2423 sc->nb_ant = val & 0x3;
2425 DPRINTF("RF revision=%d\n", sc->rf_rev);
2427 rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2);
2429 sc->ext_5ghz_lna = (val >> 6) & 0x1;
2430 sc->ext_2ghz_lna = (val >> 4) & 0x1;
2432 DPRINTF("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n",
2433 sc->ext_2ghz_lna, sc->ext_5ghz_lna);
2435 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2);
2437 if ((val & 0xff) != 0xff)
2438 sc->rssi_2ghz_corr = (int8_t)(val & 0xff); /* signed */
2440 /* Only [-10, 10] is valid */
2441 if (sc->rssi_2ghz_corr < -10 || sc->rssi_2ghz_corr > 10)
2442 sc->rssi_2ghz_corr = 0;
2444 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2);
2446 if ((val & 0xff) != 0xff)
2447 sc->rssi_5ghz_corr = (int8_t)(val & 0xff); /* signed */
2449 /* Only [-10, 10] is valid */
2450 if (sc->rssi_5ghz_corr < -10 || sc->rssi_5ghz_corr > 10)
2451 sc->rssi_5ghz_corr = 0;
2453 if (sc->ext_2ghz_lna)
2454 sc->rssi_2ghz_corr -= 14;
2455 if (sc->ext_5ghz_lna)
2456 sc->rssi_5ghz_corr -= 14;
2458 DPRINTF("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n",
2459 sc->rssi_2ghz_corr, sc->rssi_5ghz_corr);
2461 rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2);
2463 if ((val & 0xff) != 0xff)
2464 sc->rffreq = val & 0xff;
2466 DPRINTF("RF freq=%d\n", sc->rffreq);
2468 /* read Tx power for all a/b/g channels */
2469 rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14);
2470 /* XXX default Tx power for 802.11a channels */
2471 memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14);
2473 for (i = 0; i < 14; i++)
2474 DPRINTF("Channel=%d Tx power=%d\n", i + 1, sc->txpow[i]);
2477 /* read default values for BBP registers */
2478 rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
2480 for (i = 0; i < 14; i++) {
2481 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
2483 DPRINTF("BBP R%d=%02x\n", sc->bbp_prom[i].reg,
2484 sc->bbp_prom[i].val);
2490 rum_bbp_wakeup(struct rum_softc *sc)
2492 unsigned int ntries;
2494 for (ntries = 0; ntries < 100; ntries++) {
2495 if (rum_read(sc, RT2573_MAC_CSR12) & 8)
2497 rum_write(sc, RT2573_MAC_CSR12, 4); /* force wakeup */
2498 if (rum_pause(sc, hz / 100))
2501 if (ntries == 100) {
2502 device_printf(sc->sc_dev,
2503 "timeout waiting for BBP/RF to wakeup\n");
2511 rum_bbp_init(struct rum_softc *sc)
2515 /* wait for BBP to be ready */
2516 for (ntries = 0; ntries < 100; ntries++) {
2517 const uint8_t val = rum_bbp_read(sc, 0);
2518 if (val != 0 && val != 0xff)
2520 if (rum_pause(sc, hz / 100))
2523 if (ntries == 100) {
2524 device_printf(sc->sc_dev, "timeout waiting for BBP\n");
2528 /* initialize BBP registers to default values */
2529 for (i = 0; i < nitems(rum_def_bbp); i++)
2530 rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val);
2532 /* write vendor-specific BBP values (from EEPROM) */
2533 for (i = 0; i < 16; i++) {
2534 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
2536 rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
2543 rum_clr_shkey_regs(struct rum_softc *sc)
2545 rum_write(sc, RT2573_SEC_CSR0, 0);
2546 rum_write(sc, RT2573_SEC_CSR1, 0);
2547 rum_write(sc, RT2573_SEC_CSR5, 0);
2551 rum_init(struct rum_softc *sc)
2553 struct ieee80211com *ic = &sc->sc_ic;
2554 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2559 if (sc->sc_running) {
2564 /* initialize MAC registers to default values */
2565 for (i = 0; i < nitems(rum_def_mac); i++)
2566 rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val);
2568 /* reset some WME parameters to default values */
2569 sc->wme_params[0].wmep_aifsn = 2;
2570 sc->wme_params[0].wmep_logcwmin = 4;
2571 sc->wme_params[0].wmep_logcwmax = 10;
2573 /* set host ready */
2574 rum_write(sc, RT2573_MAC_CSR1, RT2573_RESET_ASIC | RT2573_RESET_BBP);
2575 rum_write(sc, RT2573_MAC_CSR1, 0);
2577 /* wait for BBP/RF to wakeup */
2578 if ((ret = rum_bbp_wakeup(sc)) != 0)
2581 if ((ret = rum_bbp_init(sc)) != 0)
2584 /* select default channel */
2585 rum_select_band(sc, ic->ic_curchan);
2586 rum_select_antenna(sc);
2587 rum_set_chan(sc, ic->ic_curchan);
2589 /* clear STA registers */
2590 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
2592 /* clear security registers (if required) */
2593 if (sc->sc_clr_shkeys == 0) {
2594 rum_clr_shkey_regs(sc);
2595 sc->sc_clr_shkeys = 1;
2598 rum_set_macaddr(sc, vap ? vap->iv_myaddr : ic->ic_macaddr);
2600 /* initialize ASIC */
2601 rum_write(sc, RT2573_MAC_CSR1, RT2573_HOST_READY);
2604 * Allocate Tx and Rx xfer queues.
2606 rum_setup_tx_list(sc);
2608 /* update Rx filter */
2609 tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff;
2611 tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR;
2612 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2613 tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR |
2615 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2616 tmp |= RT2573_DROP_TODS;
2617 if (ic->ic_promisc == 0)
2618 tmp |= RT2573_DROP_NOT_TO_ME;
2620 rum_write(sc, RT2573_TXRX_CSR0, tmp);
2623 usbd_xfer_set_stall(sc->sc_xfer[RUM_BULK_WR]);
2624 usbd_transfer_start(sc->sc_xfer[RUM_BULK_RD]);
2626 end: RUM_UNLOCK(sc);
2635 rum_stop(struct rum_softc *sc)
2639 if (!sc->sc_running) {
2647 * Drain the USB transfers, if not already drained:
2649 usbd_transfer_drain(sc->sc_xfer[RUM_BULK_WR]);
2650 usbd_transfer_drain(sc->sc_xfer[RUM_BULK_RD]);
2653 rum_unsetup_tx_list(sc);
2656 rum_setbits(sc, RT2573_TXRX_CSR0, RT2573_DISABLE_RX);
2659 rum_write(sc, RT2573_MAC_CSR1, RT2573_RESET_ASIC | RT2573_RESET_BBP);
2660 rum_write(sc, RT2573_MAC_CSR1, 0);
2665 rum_load_microcode(struct rum_softc *sc, const uint8_t *ucode, size_t size)
2667 uint16_t reg = RT2573_MCU_CODE_BASE;
2670 /* copy firmware image into NIC */
2671 for (; size >= 4; reg += 4, ucode += 4, size -= 4) {
2672 err = rum_write(sc, reg, UGETDW(ucode));
2674 /* firmware already loaded ? */
2675 device_printf(sc->sc_dev, "Firmware load "
2676 "failure! (ignored)\n");
2681 err = rum_do_mcu_request(sc, RT2573_MCU_RUN);
2682 if (err != USB_ERR_NORMAL_COMPLETION) {
2683 device_printf(sc->sc_dev, "could not run firmware: %s\n",
2687 /* give the chip some time to boot */
2688 rum_pause(sc, hz / 8);
2692 rum_set_sleep_time(struct rum_softc *sc, uint16_t bintval)
2694 struct ieee80211com *ic = &sc->sc_ic;
2698 RUM_LOCK_ASSERT(sc);
2700 exp = ic->ic_lintval / bintval;
2701 delay = ic->ic_lintval % bintval;
2703 if (exp > RT2573_TBCN_EXP_MAX)
2704 exp = RT2573_TBCN_EXP_MAX;
2705 if (delay > RT2573_TBCN_DELAY_MAX)
2706 delay = RT2573_TBCN_DELAY_MAX;
2708 uerror = rum_modbits(sc, RT2573_MAC_CSR11,
2709 RT2573_TBCN_EXP(exp) |
2710 RT2573_TBCN_DELAY(delay),
2711 RT2573_TBCN_EXP(RT2573_TBCN_EXP_MAX) |
2712 RT2573_TBCN_DELAY(RT2573_TBCN_DELAY_MAX));
2714 if (uerror != USB_ERR_NORMAL_COMPLETION)
2717 sc->sc_sleep_time = IEEE80211_TU_TO_TICKS(exp * bintval + delay);
2723 rum_reset(struct ieee80211vap *vap, u_long cmd)
2725 struct ieee80211com *ic = vap->iv_ic;
2726 struct ieee80211_node *ni;
2727 struct rum_softc *sc = ic->ic_softc;
2731 case IEEE80211_IOC_POWERSAVE:
2732 case IEEE80211_IOC_PROTMODE:
2733 case IEEE80211_IOC_RTSTHRESHOLD:
2736 case IEEE80211_IOC_POWERSAVESLEEP:
2737 ni = ieee80211_ref_node(vap->iv_bss);
2740 error = rum_set_sleep_time(sc, ni->ni_intval);
2741 if (vap->iv_state == IEEE80211_S_SLEEP) {
2742 /* Use new values for wakeup timer. */
2743 rum_clrbits(sc, RT2573_MAC_CSR11, RT2573_AUTO_WAKEUP);
2744 rum_setbits(sc, RT2573_MAC_CSR11, RT2573_AUTO_WAKEUP);
2746 /* XXX send reassoc */
2749 ieee80211_free_node(ni);
2760 rum_set_beacon(struct rum_softc *sc, struct ieee80211vap *vap)
2762 struct ieee80211com *ic = vap->iv_ic;
2763 struct rum_vap *rvp = RUM_VAP(vap);
2764 struct mbuf *m = rvp->bcn_mbuf;
2765 const struct ieee80211_txparam *tp;
2766 struct rum_tx_desc desc;
2768 RUM_LOCK_ASSERT(sc);
2772 if (ic->ic_bsschan == IEEE80211_CHAN_ANYC)
2775 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)];
2776 rum_setup_tx_desc(sc, &desc, NULL, RT2573_TX_TIMESTAMP,
2777 RT2573_TX_HWSEQ, 0, 0, m->m_pkthdr.len, tp->mgmtrate);
2779 /* copy the Tx descriptor into NIC memory */
2780 if (rum_write_multi(sc, RT2573_HW_BCN_BASE(0), (uint8_t *)&desc,
2781 RT2573_TX_DESC_SIZE) != 0)
2784 /* copy beacon header and payload into NIC memory */
2785 if (rum_write_multi(sc, RT2573_HW_BCN_BASE(0) + RT2573_TX_DESC_SIZE,
2786 mtod(m, uint8_t *), m->m_pkthdr.len) != 0)
2793 rum_alloc_beacon(struct rum_softc *sc, struct ieee80211vap *vap)
2795 struct rum_vap *rvp = RUM_VAP(vap);
2796 struct ieee80211_node *ni = vap->iv_bss;
2799 if (ni->ni_chan == IEEE80211_CHAN_ANYC)
2802 m = ieee80211_beacon_alloc(ni);
2806 if (rvp->bcn_mbuf != NULL)
2807 m_freem(rvp->bcn_mbuf);
2811 return (rum_set_beacon(sc, vap));
2815 rum_update_beacon_cb(struct rum_softc *sc, union sec_param *data,
2818 struct ieee80211vap *vap = data->vap;
2820 rum_set_beacon(sc, vap);
2824 rum_update_beacon(struct ieee80211vap *vap, int item)
2826 struct ieee80211com *ic = vap->iv_ic;
2827 struct rum_softc *sc = ic->ic_softc;
2828 struct rum_vap *rvp = RUM_VAP(vap);
2829 struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
2830 struct ieee80211_node *ni = vap->iv_bss;
2831 struct mbuf *m = rvp->bcn_mbuf;
2836 m = ieee80211_beacon_alloc(ni);
2838 device_printf(sc->sc_dev,
2839 "%s: could not allocate beacon frame\n", __func__);
2847 case IEEE80211_BEACON_ERP:
2848 rum_update_slot(ic);
2850 case IEEE80211_BEACON_TIM:
2858 setbit(bo->bo_flags, item);
2859 ieee80211_beacon_update(ni, m, mcast);
2861 rum_cmd_sleepable(sc, &vap, sizeof(vap), 0, rum_update_beacon_cb);
2865 rum_common_key_set(struct rum_softc *sc, struct ieee80211_key *k,
2869 if (rum_write_multi(sc, base, k->wk_key, k->wk_keylen))
2872 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP) {
2873 if (rum_write_multi(sc, base + IEEE80211_KEYBUF_SIZE,
2876 if (rum_write_multi(sc, base + IEEE80211_KEYBUF_SIZE + 8,
2885 rum_group_key_set_cb(struct rum_softc *sc, union sec_param *data,
2888 struct ieee80211_key *k = &data->key;
2891 if (sc->sc_clr_shkeys == 0) {
2892 rum_clr_shkey_regs(sc);
2893 sc->sc_clr_shkeys = 1;
2896 mode = rum_crypto_mode(sc, k->wk_cipher->ic_cipher, k->wk_keylen);
2900 DPRINTFN(1, "setting group key %d for vap %d, mode %d "
2901 "(tx %s, rx %s)\n", k->wk_keyix, rvp_id, mode,
2902 (k->wk_flags & IEEE80211_KEY_XMIT) ? "on" : "off",
2903 (k->wk_flags & IEEE80211_KEY_RECV) ? "on" : "off");
2905 /* Install the key. */
2906 if (rum_common_key_set(sc, k, RT2573_SKEY(rvp_id, k->wk_keyix)) != 0)
2909 /* Set cipher mode. */
2910 if (rum_modbits(sc, rvp_id < 2 ? RT2573_SEC_CSR1 : RT2573_SEC_CSR5,
2911 mode << (rvp_id % 2 + k->wk_keyix) * RT2573_SKEY_MAX,
2912 RT2573_MODE_MASK << (rvp_id % 2 + k->wk_keyix) * RT2573_SKEY_MAX)
2916 /* Mark this key as valid. */
2917 if (rum_setbits(sc, RT2573_SEC_CSR0,
2918 1 << (rvp_id * RT2573_SKEY_MAX + k->wk_keyix)) != 0)
2924 device_printf(sc->sc_dev, "%s: cannot set group key %d for vap %d\n",
2925 __func__, k->wk_keyix, rvp_id);
2929 rum_group_key_del_cb(struct rum_softc *sc, union sec_param *data,
2932 struct ieee80211_key *k = &data->key;
2934 DPRINTF("%s: removing group key %d for vap %d\n", __func__,
2935 k->wk_keyix, rvp_id);
2937 rvp_id < 2 ? RT2573_SEC_CSR1 : RT2573_SEC_CSR5,
2938 RT2573_MODE_MASK << (rvp_id % 2 + k->wk_keyix) * RT2573_SKEY_MAX);
2939 rum_clrbits(sc, RT2573_SEC_CSR0,
2940 rvp_id * RT2573_SKEY_MAX + k->wk_keyix);
2944 rum_pair_key_set_cb(struct rum_softc *sc, union sec_param *data,
2947 struct ieee80211_key *k = &data->key;
2948 uint8_t buf[IEEE80211_ADDR_LEN + 1];
2951 mode = rum_crypto_mode(sc, k->wk_cipher->ic_cipher, k->wk_keylen);
2955 DPRINTFN(1, "setting pairwise key %d for vap %d, mode %d "
2956 "(tx %s, rx %s)\n", k->wk_keyix, rvp_id, mode,
2957 (k->wk_flags & IEEE80211_KEY_XMIT) ? "on" : "off",
2958 (k->wk_flags & IEEE80211_KEY_RECV) ? "on" : "off");
2960 /* Install the key. */
2961 if (rum_common_key_set(sc, k, RT2573_PKEY(k->wk_keyix)) != 0)
2964 IEEE80211_ADDR_COPY(buf, k->wk_macaddr);
2965 buf[IEEE80211_ADDR_LEN] = mode;
2967 /* Set transmitter address and cipher mode. */
2968 if (rum_write_multi(sc, RT2573_ADDR_ENTRY(k->wk_keyix),
2969 buf, sizeof buf) != 0)
2972 /* Enable key table lookup for this vap. */
2973 if (sc->vap_key_count[rvp_id]++ == 0)
2974 if (rum_setbits(sc, RT2573_SEC_CSR4, 1 << rvp_id) != 0)
2977 /* Mark this key as valid. */
2979 k->wk_keyix < 32 ? RT2573_SEC_CSR2 : RT2573_SEC_CSR3,
2980 1 << (k->wk_keyix % 32)) != 0)
2986 device_printf(sc->sc_dev,
2987 "%s: cannot set pairwise key %d, vap %d\n", __func__, k->wk_keyix,
2992 rum_pair_key_del_cb(struct rum_softc *sc, union sec_param *data,
2995 struct ieee80211_key *k = &data->key;
2997 DPRINTF("%s: removing key %d\n", __func__, k->wk_keyix);
2998 rum_clrbits(sc, (k->wk_keyix < 32) ? RT2573_SEC_CSR2 : RT2573_SEC_CSR3,
2999 1 << (k->wk_keyix % 32));
3000 sc->keys_bmap &= ~(1ULL << k->wk_keyix);
3001 if (--sc->vap_key_count[rvp_id] == 0)
3002 rum_clrbits(sc, RT2573_SEC_CSR4, 1 << rvp_id);
3006 rum_key_alloc(struct ieee80211vap *vap, struct ieee80211_key *k,
3007 ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
3009 struct rum_softc *sc = vap->iv_ic->ic_softc;
3012 if (!(&vap->iv_nw_keys[0] <= k &&
3013 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID])) {
3014 if (!(k->wk_flags & IEEE80211_KEY_SWCRYPT)) {
3016 for (i = 0; i < RT2573_ADDR_MAX; i++) {
3017 if ((sc->keys_bmap & (1ULL << i)) == 0) {
3018 sc->keys_bmap |= (1ULL << i);
3024 if (i == RT2573_ADDR_MAX) {
3025 device_printf(sc->sc_dev,
3026 "%s: no free space in the key table\n",
3033 *keyix = ieee80211_crypto_get_key_wepidx(vap, k);
3040 rum_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
3042 struct rum_softc *sc = vap->iv_ic->ic_softc;
3045 if (k->wk_flags & IEEE80211_KEY_SWCRYPT) {
3050 group = k >= &vap->iv_nw_keys[0] && k < &vap->iv_nw_keys[IEEE80211_WEP_NKID];
3052 return !rum_cmd_sleepable(sc, k, sizeof(*k), 0,
3053 group ? rum_group_key_set_cb : rum_pair_key_set_cb);
3057 rum_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
3059 struct rum_softc *sc = vap->iv_ic->ic_softc;
3062 if (k->wk_flags & IEEE80211_KEY_SWCRYPT) {
3067 group = k >= &vap->iv_nw_keys[0] && k < &vap->iv_nw_keys[IEEE80211_WEP_NKID];
3069 return !rum_cmd_sleepable(sc, k, sizeof(*k), 0,
3070 group ? rum_group_key_del_cb : rum_pair_key_del_cb);
3074 rum_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
3075 const struct ieee80211_bpf_params *params)
3077 struct rum_softc *sc = ni->ni_ic->ic_softc;
3081 /* prevent management frames from being sent if we're not ready */
3082 if (!sc->sc_running) {
3086 if (sc->tx_nfree < RUM_TX_MINFREE) {
3091 if (params == NULL) {
3093 * Legacy path; interpret frame contents to decide
3094 * precisely how to send the frame.
3096 if ((ret = rum_tx_mgt(sc, m, ni)) != 0)
3100 * Caller supplied explicit parameters to use in
3101 * sending the frame.
3103 if ((ret = rum_tx_raw(sc, m, ni, params)) != 0)
3116 rum_ratectl_start(struct rum_softc *sc, struct ieee80211_node *ni)
3118 struct ieee80211vap *vap = ni->ni_vap;
3119 struct rum_vap *rvp = RUM_VAP(vap);
3121 /* clear statistic registers (STA_CSR0 to STA_CSR5) */
3122 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
3124 usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp);
3128 rum_ratectl_timeout(void *arg)
3130 struct rum_vap *rvp = arg;
3131 struct ieee80211vap *vap = &rvp->vap;
3132 struct ieee80211com *ic = vap->iv_ic;
3134 ieee80211_runtask(ic, &rvp->ratectl_task);
3138 rum_ratectl_task(void *arg, int pending)
3140 struct rum_vap *rvp = arg;
3141 struct ieee80211vap *vap = &rvp->vap;
3142 struct rum_softc *sc = vap->iv_ic->ic_softc;
3143 struct ieee80211_ratectl_tx_stats *txs = &sc->sc_txs;
3147 /* read and clear statistic registers (STA_CSR0 to STA_CSR5) */
3148 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof(sc->sta));
3150 ok[0] = (le32toh(sc->sta[4]) & 0xffff); /* TX ok w/o retry */
3151 ok[1] = (le32toh(sc->sta[4]) >> 16); /* TX ok w/ one retry */
3152 ok[2] = (le32toh(sc->sta[5]) & 0xffff); /* TX ok w/ multiple retries */
3153 fail = (le32toh(sc->sta[5]) >> 16); /* TX retry-fail count */
3155 txs->flags = IEEE80211_RATECTL_TX_STATS_RETRIES;
3156 txs->nframes = ok[0] + ok[1] + ok[2] + fail;
3157 txs->nsuccess = txs->nframes - fail;
3159 txs->nretries = ok[1] + ok[2] * 2 + fail * (rvp->maxretry + 1);
3161 if (txs->nframes != 0)
3162 ieee80211_ratectl_tx_update(vap, txs);
3164 /* count TX retry-fail as Tx errors */
3165 if_inc_counter(vap->iv_ifp, IFCOUNTER_OERRORS, fail);
3167 usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp);
3172 rum_scan_start(struct ieee80211com *ic)
3174 struct rum_softc *sc = ic->ic_softc;
3177 rum_abort_tsf_sync(sc);
3178 rum_set_bssid(sc, ieee80211broadcastaddr);
3184 rum_scan_end(struct ieee80211com *ic)
3186 struct rum_softc *sc = ic->ic_softc;
3188 if (ic->ic_flags_ext & IEEE80211_FEXT_BGSCAN) {
3190 if (ic->ic_opmode != IEEE80211_M_AHDEMO)
3191 rum_enable_tsf_sync(sc);
3194 rum_set_bssid(sc, sc->sc_bssid);
3200 rum_set_channel(struct ieee80211com *ic)
3202 struct rum_softc *sc = ic->ic_softc;
3205 rum_set_chan(sc, ic->ic_curchan);
3210 rum_getradiocaps(struct ieee80211com *ic,
3211 int maxchans, int *nchans, struct ieee80211_channel chans[])
3213 struct rum_softc *sc = ic->ic_softc;
3214 uint8_t bands[IEEE80211_MODE_BYTES];
3216 memset(bands, 0, sizeof(bands));
3217 setbit(bands, IEEE80211_MODE_11B);
3218 setbit(bands, IEEE80211_MODE_11G);
3219 ieee80211_add_channels_default_2ghz(chans, maxchans, nchans, bands, 0);
3221 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_5226) {
3222 setbit(bands, IEEE80211_MODE_11A);
3223 ieee80211_add_channel_list_5ghz(chans, maxchans, nchans,
3224 rum_chan_5ghz, nitems(rum_chan_5ghz), bands, 0);
3229 rum_get_rssi(struct rum_softc *sc, uint8_t raw)
3231 struct ieee80211com *ic = &sc->sc_ic;
3234 lna = (raw >> 5) & 0x3;
3241 * NB: Since RSSI is relative to noise floor, -1 is
3242 * adequate for caller to know error happened.
3247 rssi = (2 * agc) - RT2573_NOISE_FLOOR;
3249 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) {
3250 rssi += sc->rssi_2ghz_corr;
3259 rssi += sc->rssi_5ghz_corr;
3261 if (!sc->ext_5ghz_lna && lna != 1)
3275 rum_pause(struct rum_softc *sc, int timeout)
3278 usb_pause_mtx(&sc->sc_mtx, timeout);
3282 static device_method_t rum_methods[] = {
3283 /* Device interface */
3284 DEVMETHOD(device_probe, rum_match),
3285 DEVMETHOD(device_attach, rum_attach),
3286 DEVMETHOD(device_detach, rum_detach),
3290 static driver_t rum_driver = {
3292 .methods = rum_methods,
3293 .size = sizeof(struct rum_softc),
3296 static devclass_t rum_devclass;
3298 DRIVER_MODULE(rum, uhub, rum_driver, rum_devclass, NULL, 0);
3299 MODULE_DEPEND(rum, wlan, 1, 1, 1);
3300 MODULE_DEPEND(rum, usb, 1, 1, 1);
3301 MODULE_VERSION(rum, 1);
3302 USB_PNP_HOST_INFO(rum_devs);