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/
30 #include <sys/param.h>
31 #include <sys/sockio.h>
32 #include <sys/sysctl.h>
34 #include <sys/mutex.h>
36 #include <sys/kernel.h>
37 #include <sys/socket.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
42 #include <sys/endian.h>
47 #include <net/if_var.h>
48 #include <net/if_arp.h>
49 #include <net/ethernet.h>
50 #include <net/if_dl.h>
51 #include <net/if_media.h>
52 #include <net/if_types.h>
55 #include <netinet/in.h>
56 #include <netinet/in_systm.h>
57 #include <netinet/in_var.h>
58 #include <netinet/if_ether.h>
59 #include <netinet/ip.h>
62 #include <net80211/ieee80211_var.h>
63 #include <net80211/ieee80211_regdomain.h>
64 #include <net80211/ieee80211_radiotap.h>
65 #include <net80211/ieee80211_ratectl.h>
67 #include <dev/usb/usb.h>
68 #include <dev/usb/usbdi.h>
71 #define USB_DEBUG_VAR rum_debug
72 #include <dev/usb/usb_debug.h>
74 #include <dev/usb/wlan/if_rumreg.h>
75 #include <dev/usb/wlan/if_rumvar.h>
76 #include <dev/usb/wlan/if_rumfw.h>
79 static int rum_debug = 0;
81 static SYSCTL_NODE(_hw_usb, OID_AUTO, rum, CTLFLAG_RW, 0, "USB rum");
82 SYSCTL_INT(_hw_usb_rum, OID_AUTO, debug, CTLFLAG_RWTUN, &rum_debug, 0,
86 static const STRUCT_USB_HOST_ID rum_devs[] = {
87 #define RUM_DEV(v,p) { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
88 RUM_DEV(ABOCOM, HWU54DM),
89 RUM_DEV(ABOCOM, RT2573_2),
90 RUM_DEV(ABOCOM, RT2573_3),
91 RUM_DEV(ABOCOM, RT2573_4),
92 RUM_DEV(ABOCOM, WUG2700),
93 RUM_DEV(AMIT, CGWLUSB2GO),
94 RUM_DEV(ASUS, RT2573_1),
95 RUM_DEV(ASUS, RT2573_2),
96 RUM_DEV(BELKIN, F5D7050A),
97 RUM_DEV(BELKIN, F5D9050V3),
98 RUM_DEV(CISCOLINKSYS, WUSB54GC),
99 RUM_DEV(CISCOLINKSYS, WUSB54GR),
100 RUM_DEV(CONCEPTRONIC2, C54RU2),
101 RUM_DEV(COREGA, CGWLUSB2GL),
102 RUM_DEV(COREGA, CGWLUSB2GPX),
103 RUM_DEV(DICKSMITH, CWD854F),
104 RUM_DEV(DICKSMITH, RT2573),
105 RUM_DEV(EDIMAX, EW7318USG),
106 RUM_DEV(DLINK2, DWLG122C1),
107 RUM_DEV(DLINK2, WUA1340),
108 RUM_DEV(DLINK2, DWA111),
109 RUM_DEV(DLINK2, DWA110),
110 RUM_DEV(GIGABYTE, GNWB01GS),
111 RUM_DEV(GIGABYTE, GNWI05GS),
112 RUM_DEV(GIGASET, RT2573),
113 RUM_DEV(GOODWAY, RT2573),
114 RUM_DEV(GUILLEMOT, HWGUSB254LB),
115 RUM_DEV(GUILLEMOT, HWGUSB254V2AP),
116 RUM_DEV(HUAWEI3COM, WUB320G),
117 RUM_DEV(MELCO, G54HP),
118 RUM_DEV(MELCO, SG54HP),
119 RUM_DEV(MELCO, SG54HG),
120 RUM_DEV(MELCO, WLIUCG),
121 RUM_DEV(MELCO, WLRUCG),
122 RUM_DEV(MELCO, WLRUCGAOSS),
123 RUM_DEV(MSI, RT2573_1),
124 RUM_DEV(MSI, RT2573_2),
125 RUM_DEV(MSI, RT2573_3),
126 RUM_DEV(MSI, RT2573_4),
127 RUM_DEV(NOVATECH, RT2573),
128 RUM_DEV(PLANEX2, GWUS54HP),
129 RUM_DEV(PLANEX2, GWUS54MINI2),
130 RUM_DEV(PLANEX2, GWUSMM),
131 RUM_DEV(QCOM, RT2573),
132 RUM_DEV(QCOM, RT2573_2),
133 RUM_DEV(QCOM, RT2573_3),
134 RUM_DEV(RALINK, RT2573),
135 RUM_DEV(RALINK, RT2573_2),
136 RUM_DEV(RALINK, RT2671),
137 RUM_DEV(SITECOMEU, WL113R2),
138 RUM_DEV(SITECOMEU, WL172),
139 RUM_DEV(SPARKLAN, RT2573),
140 RUM_DEV(SURECOM, RT2573),
144 static device_probe_t rum_match;
145 static device_attach_t rum_attach;
146 static device_detach_t rum_detach;
148 static usb_callback_t rum_bulk_read_callback;
149 static usb_callback_t rum_bulk_write_callback;
151 static usb_error_t rum_do_request(struct rum_softc *sc,
152 struct usb_device_request *req, void *data);
153 static usb_error_t rum_do_mcu_request(struct rum_softc *sc, int);
154 static struct ieee80211vap *rum_vap_create(struct ieee80211com *,
155 const char [IFNAMSIZ], int, enum ieee80211_opmode,
156 int, const uint8_t [IEEE80211_ADDR_LEN],
157 const uint8_t [IEEE80211_ADDR_LEN]);
158 static void rum_vap_delete(struct ieee80211vap *);
159 static void rum_cmdq_cb(void *, int);
160 static int rum_cmd_sleepable(struct rum_softc *, const void *,
161 size_t, uint8_t, CMD_FUNC_PROTO);
162 static void rum_tx_free(struct rum_tx_data *, int);
163 static void rum_setup_tx_list(struct rum_softc *);
164 static void rum_unsetup_tx_list(struct rum_softc *);
165 static void rum_beacon_miss(struct ieee80211vap *);
166 static void rum_sta_recv_mgmt(struct ieee80211_node *,
168 const struct ieee80211_rx_stats *, int, int);
169 static int rum_set_power_state(struct rum_softc *, int);
170 static int rum_newstate(struct ieee80211vap *,
171 enum ieee80211_state, int);
172 static uint8_t rum_crypto_mode(struct rum_softc *, u_int, int);
173 static void rum_setup_tx_desc(struct rum_softc *,
174 struct rum_tx_desc *, struct ieee80211_key *,
175 uint32_t, uint8_t, uint8_t, int, int, int);
176 static uint32_t rum_tx_crypto_flags(struct rum_softc *,
177 struct ieee80211_node *,
178 const struct ieee80211_key *);
179 static int rum_tx_mgt(struct rum_softc *, struct mbuf *,
180 struct ieee80211_node *);
181 static int rum_tx_raw(struct rum_softc *, struct mbuf *,
182 struct ieee80211_node *,
183 const struct ieee80211_bpf_params *);
184 static int rum_tx_data(struct rum_softc *, struct mbuf *,
185 struct ieee80211_node *);
186 static int rum_transmit(struct ieee80211com *, struct mbuf *);
187 static void rum_start(struct rum_softc *);
188 static void rum_parent(struct ieee80211com *);
189 static void rum_eeprom_read(struct rum_softc *, uint16_t, void *,
191 static uint32_t rum_read(struct rum_softc *, uint16_t);
192 static void rum_read_multi(struct rum_softc *, uint16_t, void *,
194 static usb_error_t rum_write(struct rum_softc *, uint16_t, uint32_t);
195 static usb_error_t rum_write_multi(struct rum_softc *, uint16_t, void *,
197 static usb_error_t rum_setbits(struct rum_softc *, uint16_t, uint32_t);
198 static usb_error_t rum_clrbits(struct rum_softc *, uint16_t, uint32_t);
199 static usb_error_t rum_modbits(struct rum_softc *, uint16_t, uint32_t,
201 static int rum_bbp_busy(struct rum_softc *);
202 static void rum_bbp_write(struct rum_softc *, uint8_t, uint8_t);
203 static uint8_t rum_bbp_read(struct rum_softc *, uint8_t);
204 static void rum_rf_write(struct rum_softc *, uint8_t, uint32_t);
205 static void rum_select_antenna(struct rum_softc *);
206 static void rum_enable_mrr(struct rum_softc *);
207 static void rum_set_txpreamble(struct rum_softc *);
208 static void rum_set_basicrates(struct rum_softc *);
209 static void rum_select_band(struct rum_softc *,
210 struct ieee80211_channel *);
211 static void rum_set_chan(struct rum_softc *,
212 struct ieee80211_channel *);
213 static void rum_set_maxretry(struct rum_softc *,
214 struct ieee80211vap *);
215 static int rum_enable_tsf_sync(struct rum_softc *);
216 static void rum_enable_tsf(struct rum_softc *);
217 static void rum_abort_tsf_sync(struct rum_softc *);
218 static void rum_get_tsf(struct rum_softc *, uint64_t *);
219 static void rum_update_slot_cb(struct rum_softc *,
220 union sec_param *, uint8_t);
221 static void rum_update_slot(struct ieee80211com *);
222 static int rum_wme_update(struct ieee80211com *);
223 static void rum_set_bssid(struct rum_softc *, const uint8_t *);
224 static void rum_set_macaddr(struct rum_softc *, const uint8_t *);
225 static void rum_update_mcast(struct ieee80211com *);
226 static void rum_update_promisc(struct ieee80211com *);
227 static void rum_setpromisc(struct rum_softc *);
228 static const char *rum_get_rf(int);
229 static void rum_read_eeprom(struct rum_softc *);
230 static int rum_bbp_wakeup(struct rum_softc *);
231 static int rum_bbp_init(struct rum_softc *);
232 static void rum_clr_shkey_regs(struct rum_softc *);
233 static int rum_init(struct rum_softc *);
234 static void rum_stop(struct rum_softc *);
235 static void rum_load_microcode(struct rum_softc *, const uint8_t *,
237 static int rum_set_sleep_time(struct rum_softc *, uint16_t);
238 static int rum_reset(struct ieee80211vap *, u_long);
239 static int rum_set_beacon(struct rum_softc *,
240 struct ieee80211vap *);
241 static int rum_alloc_beacon(struct rum_softc *,
242 struct ieee80211vap *);
243 static void rum_update_beacon_cb(struct rum_softc *,
244 union sec_param *, uint8_t);
245 static void rum_update_beacon(struct ieee80211vap *, int);
246 static int rum_common_key_set(struct rum_softc *,
247 struct ieee80211_key *, uint16_t);
248 static void rum_group_key_set_cb(struct rum_softc *,
249 union sec_param *, uint8_t);
250 static void rum_group_key_del_cb(struct rum_softc *,
251 union sec_param *, uint8_t);
252 static void rum_pair_key_set_cb(struct rum_softc *,
253 union sec_param *, uint8_t);
254 static void rum_pair_key_del_cb(struct rum_softc *,
255 union sec_param *, uint8_t);
256 static int rum_key_alloc(struct ieee80211vap *,
257 struct ieee80211_key *, ieee80211_keyix *,
259 static int rum_key_set(struct ieee80211vap *,
260 const struct ieee80211_key *);
261 static int rum_key_delete(struct ieee80211vap *,
262 const struct ieee80211_key *);
263 static int rum_raw_xmit(struct ieee80211_node *, struct mbuf *,
264 const struct ieee80211_bpf_params *);
265 static void rum_scan_start(struct ieee80211com *);
266 static void rum_scan_end(struct ieee80211com *);
267 static void rum_set_channel(struct ieee80211com *);
268 static void rum_getradiocaps(struct ieee80211com *, int, int *,
269 struct ieee80211_channel[]);
270 static int rum_get_rssi(struct rum_softc *, uint8_t);
271 static void rum_ratectl_start(struct rum_softc *,
272 struct ieee80211_node *);
273 static void rum_ratectl_timeout(void *);
274 static void rum_ratectl_task(void *, int);
275 static int rum_pause(struct rum_softc *, int);
277 static const struct {
281 { RT2573_TXRX_CSR0, 0x025fb032 },
282 { RT2573_TXRX_CSR1, 0x9eaa9eaf },
283 { RT2573_TXRX_CSR2, 0x8a8b8c8d },
284 { RT2573_TXRX_CSR3, 0x00858687 },
285 { RT2573_TXRX_CSR7, 0x2e31353b },
286 { RT2573_TXRX_CSR8, 0x2a2a2a2c },
287 { RT2573_TXRX_CSR15, 0x0000000f },
288 { RT2573_MAC_CSR6, 0x00000fff },
289 { RT2573_MAC_CSR8, 0x016c030a },
290 { RT2573_MAC_CSR10, 0x00000718 },
291 { RT2573_MAC_CSR12, 0x00000004 },
292 { RT2573_MAC_CSR13, 0x00007f00 },
293 { RT2573_SEC_CSR2, 0x00000000 },
294 { RT2573_SEC_CSR3, 0x00000000 },
295 { RT2573_SEC_CSR4, 0x00000000 },
296 { RT2573_PHY_CSR1, 0x000023b0 },
297 { RT2573_PHY_CSR5, 0x00040a06 },
298 { RT2573_PHY_CSR6, 0x00080606 },
299 { RT2573_PHY_CSR7, 0x00000408 },
300 { RT2573_AIFSN_CSR, 0x00002273 },
301 { RT2573_CWMIN_CSR, 0x00002344 },
302 { RT2573_CWMAX_CSR, 0x000034aa }
305 static const struct {
337 static const uint8_t rum_chan_5ghz[] =
338 { 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64,
339 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140,
340 149, 153, 157, 161, 165 };
342 static const struct rfprog {
344 uint32_t r1, r2, r3, r4;
346 { 1, 0x00b03, 0x001e1, 0x1a014, 0x30282 },
347 { 2, 0x00b03, 0x001e1, 0x1a014, 0x30287 },
348 { 3, 0x00b03, 0x001e2, 0x1a014, 0x30282 },
349 { 4, 0x00b03, 0x001e2, 0x1a014, 0x30287 },
350 { 5, 0x00b03, 0x001e3, 0x1a014, 0x30282 },
351 { 6, 0x00b03, 0x001e3, 0x1a014, 0x30287 },
352 { 7, 0x00b03, 0x001e4, 0x1a014, 0x30282 },
353 { 8, 0x00b03, 0x001e4, 0x1a014, 0x30287 },
354 { 9, 0x00b03, 0x001e5, 0x1a014, 0x30282 },
355 { 10, 0x00b03, 0x001e5, 0x1a014, 0x30287 },
356 { 11, 0x00b03, 0x001e6, 0x1a014, 0x30282 },
357 { 12, 0x00b03, 0x001e6, 0x1a014, 0x30287 },
358 { 13, 0x00b03, 0x001e7, 0x1a014, 0x30282 },
359 { 14, 0x00b03, 0x001e8, 0x1a014, 0x30284 },
361 { 34, 0x00b03, 0x20266, 0x36014, 0x30282 },
362 { 38, 0x00b03, 0x20267, 0x36014, 0x30284 },
363 { 42, 0x00b03, 0x20268, 0x36014, 0x30286 },
364 { 46, 0x00b03, 0x20269, 0x36014, 0x30288 },
366 { 36, 0x00b03, 0x00266, 0x26014, 0x30288 },
367 { 40, 0x00b03, 0x00268, 0x26014, 0x30280 },
368 { 44, 0x00b03, 0x00269, 0x26014, 0x30282 },
369 { 48, 0x00b03, 0x0026a, 0x26014, 0x30284 },
370 { 52, 0x00b03, 0x0026b, 0x26014, 0x30286 },
371 { 56, 0x00b03, 0x0026c, 0x26014, 0x30288 },
372 { 60, 0x00b03, 0x0026e, 0x26014, 0x30280 },
373 { 64, 0x00b03, 0x0026f, 0x26014, 0x30282 },
375 { 100, 0x00b03, 0x0028a, 0x2e014, 0x30280 },
376 { 104, 0x00b03, 0x0028b, 0x2e014, 0x30282 },
377 { 108, 0x00b03, 0x0028c, 0x2e014, 0x30284 },
378 { 112, 0x00b03, 0x0028d, 0x2e014, 0x30286 },
379 { 116, 0x00b03, 0x0028e, 0x2e014, 0x30288 },
380 { 120, 0x00b03, 0x002a0, 0x2e014, 0x30280 },
381 { 124, 0x00b03, 0x002a1, 0x2e014, 0x30282 },
382 { 128, 0x00b03, 0x002a2, 0x2e014, 0x30284 },
383 { 132, 0x00b03, 0x002a3, 0x2e014, 0x30286 },
384 { 136, 0x00b03, 0x002a4, 0x2e014, 0x30288 },
385 { 140, 0x00b03, 0x002a6, 0x2e014, 0x30280 },
387 { 149, 0x00b03, 0x002a8, 0x2e014, 0x30287 },
388 { 153, 0x00b03, 0x002a9, 0x2e014, 0x30289 },
389 { 157, 0x00b03, 0x002ab, 0x2e014, 0x30281 },
390 { 161, 0x00b03, 0x002ac, 0x2e014, 0x30283 },
391 { 165, 0x00b03, 0x002ad, 0x2e014, 0x30285 }
393 { 1, 0x00b33, 0x011e1, 0x1a014, 0x30282 },
394 { 2, 0x00b33, 0x011e1, 0x1a014, 0x30287 },
395 { 3, 0x00b33, 0x011e2, 0x1a014, 0x30282 },
396 { 4, 0x00b33, 0x011e2, 0x1a014, 0x30287 },
397 { 5, 0x00b33, 0x011e3, 0x1a014, 0x30282 },
398 { 6, 0x00b33, 0x011e3, 0x1a014, 0x30287 },
399 { 7, 0x00b33, 0x011e4, 0x1a014, 0x30282 },
400 { 8, 0x00b33, 0x011e4, 0x1a014, 0x30287 },
401 { 9, 0x00b33, 0x011e5, 0x1a014, 0x30282 },
402 { 10, 0x00b33, 0x011e5, 0x1a014, 0x30287 },
403 { 11, 0x00b33, 0x011e6, 0x1a014, 0x30282 },
404 { 12, 0x00b33, 0x011e6, 0x1a014, 0x30287 },
405 { 13, 0x00b33, 0x011e7, 0x1a014, 0x30282 },
406 { 14, 0x00b33, 0x011e8, 0x1a014, 0x30284 },
408 { 34, 0x00b33, 0x01266, 0x26014, 0x30282 },
409 { 38, 0x00b33, 0x01267, 0x26014, 0x30284 },
410 { 42, 0x00b33, 0x01268, 0x26014, 0x30286 },
411 { 46, 0x00b33, 0x01269, 0x26014, 0x30288 },
413 { 36, 0x00b33, 0x01266, 0x26014, 0x30288 },
414 { 40, 0x00b33, 0x01268, 0x26014, 0x30280 },
415 { 44, 0x00b33, 0x01269, 0x26014, 0x30282 },
416 { 48, 0x00b33, 0x0126a, 0x26014, 0x30284 },
417 { 52, 0x00b33, 0x0126b, 0x26014, 0x30286 },
418 { 56, 0x00b33, 0x0126c, 0x26014, 0x30288 },
419 { 60, 0x00b33, 0x0126e, 0x26014, 0x30280 },
420 { 64, 0x00b33, 0x0126f, 0x26014, 0x30282 },
422 { 100, 0x00b33, 0x0128a, 0x2e014, 0x30280 },
423 { 104, 0x00b33, 0x0128b, 0x2e014, 0x30282 },
424 { 108, 0x00b33, 0x0128c, 0x2e014, 0x30284 },
425 { 112, 0x00b33, 0x0128d, 0x2e014, 0x30286 },
426 { 116, 0x00b33, 0x0128e, 0x2e014, 0x30288 },
427 { 120, 0x00b33, 0x012a0, 0x2e014, 0x30280 },
428 { 124, 0x00b33, 0x012a1, 0x2e014, 0x30282 },
429 { 128, 0x00b33, 0x012a2, 0x2e014, 0x30284 },
430 { 132, 0x00b33, 0x012a3, 0x2e014, 0x30286 },
431 { 136, 0x00b33, 0x012a4, 0x2e014, 0x30288 },
432 { 140, 0x00b33, 0x012a6, 0x2e014, 0x30280 },
434 { 149, 0x00b33, 0x012a8, 0x2e014, 0x30287 },
435 { 153, 0x00b33, 0x012a9, 0x2e014, 0x30289 },
436 { 157, 0x00b33, 0x012ab, 0x2e014, 0x30281 },
437 { 161, 0x00b33, 0x012ac, 0x2e014, 0x30283 },
438 { 165, 0x00b33, 0x012ad, 0x2e014, 0x30285 }
441 static const struct usb_config rum_config[RUM_N_TRANSFER] = {
444 .endpoint = UE_ADDR_ANY,
445 .direction = UE_DIR_OUT,
446 .bufsize = (MCLBYTES + RT2573_TX_DESC_SIZE + 8),
447 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
448 .callback = rum_bulk_write_callback,
449 .timeout = 5000, /* ms */
453 .endpoint = UE_ADDR_ANY,
454 .direction = UE_DIR_IN,
455 .bufsize = (MCLBYTES + RT2573_RX_DESC_SIZE),
456 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
457 .callback = rum_bulk_read_callback,
462 rum_match(device_t self)
464 struct usb_attach_arg *uaa = device_get_ivars(self);
466 if (uaa->usb_mode != USB_MODE_HOST)
468 if (uaa->info.bConfigIndex != 0)
470 if (uaa->info.bIfaceIndex != RT2573_IFACE_INDEX)
473 return (usbd_lookup_id_by_uaa(rum_devs, sizeof(rum_devs), uaa));
477 rum_attach(device_t self)
479 struct usb_attach_arg *uaa = device_get_ivars(self);
480 struct rum_softc *sc = device_get_softc(self);
481 struct ieee80211com *ic = &sc->sc_ic;
486 device_set_usb_desc(self);
487 sc->sc_udev = uaa->device;
491 RUM_CMDQ_LOCK_INIT(sc);
492 mbufq_init(&sc->sc_snd, ifqmaxlen);
494 iface_index = RT2573_IFACE_INDEX;
495 error = usbd_transfer_setup(uaa->device, &iface_index,
496 sc->sc_xfer, rum_config, RUM_N_TRANSFER, sc, &sc->sc_mtx);
498 device_printf(self, "could not allocate USB transfers, "
499 "err=%s\n", usbd_errstr(error));
504 /* retrieve RT2573 rev. no */
505 for (ntries = 0; ntries < 100; ntries++) {
506 if ((tmp = rum_read(sc, RT2573_MAC_CSR0)) != 0)
508 if (rum_pause(sc, hz / 100))
512 device_printf(sc->sc_dev, "timeout waiting for chip to settle\n");
517 /* retrieve MAC address and various other things from EEPROM */
520 device_printf(sc->sc_dev, "MAC/BBP RT2573 (rev 0x%05x), RF %s\n",
521 tmp, rum_get_rf(sc->rf_rev));
523 rum_load_microcode(sc, rt2573_ucode, sizeof(rt2573_ucode));
527 ic->ic_name = device_get_nameunit(self);
528 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
530 /* set device capabilities */
532 IEEE80211_C_STA /* station mode supported */
533 | IEEE80211_C_IBSS /* IBSS mode supported */
534 | IEEE80211_C_MONITOR /* monitor mode supported */
535 | IEEE80211_C_HOSTAP /* HostAp mode supported */
536 | IEEE80211_C_AHDEMO /* adhoc demo mode */
537 | IEEE80211_C_TXPMGT /* tx power management */
538 | IEEE80211_C_SHPREAMBLE /* short preamble supported */
539 | IEEE80211_C_SHSLOT /* short slot time supported */
540 | IEEE80211_C_BGSCAN /* bg scanning supported */
541 | IEEE80211_C_WPA /* 802.11i */
542 | IEEE80211_C_WME /* 802.11e */
543 | IEEE80211_C_PMGT /* Station-side power mgmt */
544 | IEEE80211_C_SWSLEEP /* net80211 managed power mgmt */
548 IEEE80211_CRYPTO_WEP |
549 IEEE80211_CRYPTO_AES_CCM |
550 IEEE80211_CRYPTO_TKIPMIC |
551 IEEE80211_CRYPTO_TKIP;
553 rum_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
556 ieee80211_ifattach(ic);
557 ic->ic_update_promisc = rum_update_promisc;
558 ic->ic_raw_xmit = rum_raw_xmit;
559 ic->ic_scan_start = rum_scan_start;
560 ic->ic_scan_end = rum_scan_end;
561 ic->ic_set_channel = rum_set_channel;
562 ic->ic_getradiocaps = rum_getradiocaps;
563 ic->ic_transmit = rum_transmit;
564 ic->ic_parent = rum_parent;
565 ic->ic_vap_create = rum_vap_create;
566 ic->ic_vap_delete = rum_vap_delete;
567 ic->ic_updateslot = rum_update_slot;
568 ic->ic_wme.wme_update = rum_wme_update;
569 ic->ic_update_mcast = rum_update_mcast;
571 ieee80211_radiotap_attach(ic,
572 &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
573 RT2573_TX_RADIOTAP_PRESENT,
574 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
575 RT2573_RX_RADIOTAP_PRESENT);
577 TASK_INIT(&sc->cmdq_task, 0, rum_cmdq_cb, sc);
580 ieee80211_announce(ic);
586 return (ENXIO); /* failure */
590 rum_detach(device_t self)
592 struct rum_softc *sc = device_get_softc(self);
593 struct ieee80211com *ic = &sc->sc_ic;
595 /* Prevent further ioctls */
600 /* stop all USB transfers */
601 usbd_transfer_unsetup(sc->sc_xfer, RUM_N_TRANSFER);
603 /* free TX list, if any */
605 rum_unsetup_tx_list(sc);
608 if (ic->ic_softc == sc) {
609 ieee80211_draintask(ic, &sc->cmdq_task);
610 ieee80211_ifdetach(ic);
613 mbufq_drain(&sc->sc_snd);
614 RUM_CMDQ_LOCK_DESTROY(sc);
615 RUM_LOCK_DESTROY(sc);
621 rum_do_request(struct rum_softc *sc,
622 struct usb_device_request *req, void *data)
628 err = usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
629 req, data, 0, NULL, 250 /* ms */);
633 DPRINTFN(1, "Control request failed, %s (retrying)\n",
635 if (rum_pause(sc, hz / 100))
642 rum_do_mcu_request(struct rum_softc *sc, int request)
644 struct usb_device_request req;
646 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
647 req.bRequest = RT2573_MCU_CNTL;
648 USETW(req.wValue, request);
649 USETW(req.wIndex, 0);
650 USETW(req.wLength, 0);
652 return (rum_do_request(sc, &req, NULL));
655 static struct ieee80211vap *
656 rum_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
657 enum ieee80211_opmode opmode, int flags,
658 const uint8_t bssid[IEEE80211_ADDR_LEN],
659 const uint8_t mac[IEEE80211_ADDR_LEN])
661 struct rum_softc *sc = ic->ic_softc;
663 struct ieee80211vap *vap;
665 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */
667 rvp = malloc(sizeof(struct rum_vap), M_80211_VAP, M_WAITOK | M_ZERO);
669 /* enable s/w bmiss handling for sta mode */
671 if (ieee80211_vap_setup(ic, vap, name, unit, opmode,
672 flags | IEEE80211_CLONE_NOBEACONS, bssid) != 0) {
674 free(rvp, M_80211_VAP);
678 /* override state transition machine */
679 rvp->newstate = vap->iv_newstate;
680 vap->iv_newstate = rum_newstate;
681 vap->iv_key_alloc = rum_key_alloc;
682 vap->iv_key_set = rum_key_set;
683 vap->iv_key_delete = rum_key_delete;
684 vap->iv_update_beacon = rum_update_beacon;
685 vap->iv_reset = rum_reset;
686 vap->iv_max_aid = RT2573_ADDR_MAX;
688 if (opmode == IEEE80211_M_STA) {
690 * Move device to the sleep state when
691 * beacon is received and there is no data for us.
693 * Used only for IEEE80211_S_SLEEP state.
695 rvp->recv_mgmt = vap->iv_recv_mgmt;
696 vap->iv_recv_mgmt = rum_sta_recv_mgmt;
698 /* Ignored while sleeping. */
699 rvp->bmiss = vap->iv_bmiss;
700 vap->iv_bmiss = rum_beacon_miss;
703 usb_callout_init_mtx(&rvp->ratectl_ch, &sc->sc_mtx, 0);
704 TASK_INIT(&rvp->ratectl_task, 0, rum_ratectl_task, rvp);
705 ieee80211_ratectl_init(vap);
706 ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */);
708 ieee80211_vap_attach(vap, ieee80211_media_change,
709 ieee80211_media_status, mac);
710 ic->ic_opmode = opmode;
715 rum_vap_delete(struct ieee80211vap *vap)
717 struct rum_vap *rvp = RUM_VAP(vap);
718 struct ieee80211com *ic = vap->iv_ic;
720 m_freem(rvp->bcn_mbuf);
721 usb_callout_drain(&rvp->ratectl_ch);
722 ieee80211_draintask(ic, &rvp->ratectl_task);
723 ieee80211_ratectl_deinit(vap);
724 ieee80211_vap_detach(vap);
725 free(rvp, M_80211_VAP);
729 rum_cmdq_cb(void *arg, int pending)
731 struct rum_softc *sc = arg;
735 while (sc->cmdq[sc->cmdq_first].func != NULL) {
736 rc = &sc->cmdq[sc->cmdq_first];
740 rc->func(sc, &rc->data, rc->rvp_id);
744 memset(rc, 0, sizeof (*rc));
745 sc->cmdq_first = (sc->cmdq_first + 1) % RUM_CMDQ_SIZE;
751 rum_cmd_sleepable(struct rum_softc *sc, const void *ptr, size_t len,
752 uint8_t rvp_id, CMD_FUNC_PROTO)
754 struct ieee80211com *ic = &sc->sc_ic;
756 KASSERT(len <= sizeof(union sec_param), ("buffer overflow"));
759 if (sc->cmdq[sc->cmdq_last].func != NULL) {
760 device_printf(sc->sc_dev, "%s: cmdq overflow\n", __func__);
767 memcpy(&sc->cmdq[sc->cmdq_last].data, ptr, len);
768 sc->cmdq[sc->cmdq_last].rvp_id = rvp_id;
769 sc->cmdq[sc->cmdq_last].func = func;
770 sc->cmdq_last = (sc->cmdq_last + 1) % RUM_CMDQ_SIZE;
773 ieee80211_runtask(ic, &sc->cmdq_task);
779 rum_tx_free(struct rum_tx_data *data, int txerr)
781 struct rum_softc *sc = data->sc;
783 if (data->m != NULL) {
784 ieee80211_tx_complete(data->ni, data->m, txerr);
788 STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
793 rum_setup_tx_list(struct rum_softc *sc)
795 struct rum_tx_data *data;
799 STAILQ_INIT(&sc->tx_q);
800 STAILQ_INIT(&sc->tx_free);
802 for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
803 data = &sc->tx_data[i];
806 STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
812 rum_unsetup_tx_list(struct rum_softc *sc)
814 struct rum_tx_data *data;
817 /* make sure any subsequent use of the queues will fail */
819 STAILQ_INIT(&sc->tx_q);
820 STAILQ_INIT(&sc->tx_free);
822 /* free up all node references and mbufs */
823 for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
824 data = &sc->tx_data[i];
826 if (data->m != NULL) {
830 if (data->ni != NULL) {
831 ieee80211_free_node(data->ni);
838 rum_beacon_miss(struct ieee80211vap *vap)
840 struct ieee80211com *ic = vap->iv_ic;
841 struct rum_softc *sc = ic->ic_softc;
842 struct rum_vap *rvp = RUM_VAP(vap);
846 if (sc->sc_sleeping && sc->sc_sleep_end < ticks) {
847 DPRINTFN(12, "dropping 'sleeping' bit, "
848 "device must be awake now\n");
853 sleep = sc->sc_sleeping;
860 DPRINTFN(13, "bmiss event is ignored whilst sleeping\n");
865 rum_sta_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m, int subtype,
866 const struct ieee80211_rx_stats *rxs,
869 struct ieee80211vap *vap = ni->ni_vap;
870 struct rum_softc *sc = vap->iv_ic->ic_softc;
871 struct rum_vap *rvp = RUM_VAP(vap);
873 if (vap->iv_state == IEEE80211_S_SLEEP &&
874 subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
876 DPRINTFN(12, "beacon, mybss %d (flags %02X)\n",
877 !!(sc->last_rx_flags & RT2573_RX_MYBSS),
880 if ((sc->last_rx_flags & (RT2573_RX_MYBSS | RT2573_RX_BC)) ==
881 (RT2573_RX_MYBSS | RT2573_RX_BC)) {
883 * Put it to sleep here; in case if there is a data
884 * for us, iv_recv_mgmt() will wakeup the device via
885 * SLEEP -> RUN state transition.
887 rum_set_power_state(sc, 1);
892 rvp->recv_mgmt(ni, m, subtype, rxs, rssi, nf);
896 rum_set_power_state(struct rum_softc *sc, int sleep)
902 DPRINTFN(12, "moving to %s state (sleep time %u)\n",
903 sleep ? "sleep" : "awake", sc->sc_sleep_time);
905 uerror = rum_do_mcu_request(sc,
906 sleep ? RT2573_MCU_SLEEP : RT2573_MCU_WAKEUP);
907 if (uerror != USB_ERR_NORMAL_COMPLETION) {
908 device_printf(sc->sc_dev,
909 "%s: could not change power state: %s\n",
910 __func__, usbd_errstr(uerror));
914 sc->sc_sleeping = !!sleep;
915 sc->sc_sleep_end = sleep ? ticks + sc->sc_sleep_time : 0;
921 rum_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
923 struct rum_vap *rvp = RUM_VAP(vap);
924 struct ieee80211com *ic = vap->iv_ic;
925 struct rum_softc *sc = ic->ic_softc;
926 const struct ieee80211_txparam *tp;
927 enum ieee80211_state ostate;
928 struct ieee80211_node *ni;
932 ostate = vap->iv_state;
933 DPRINTF("%s -> %s\n",
934 ieee80211_state_name[ostate],
935 ieee80211_state_name[nstate]);
937 IEEE80211_UNLOCK(ic);
939 usb_callout_stop(&rvp->ratectl_ch);
941 if (ostate == IEEE80211_S_SLEEP && vap->iv_opmode == IEEE80211_M_STA) {
942 rum_clrbits(sc, RT2573_TXRX_CSR4, RT2573_ACKCTS_PWRMGT);
943 rum_clrbits(sc, RT2573_MAC_CSR11, RT2573_AUTO_WAKEUP);
947 * any subsequent TX will wakeup it anyway
949 (void) rum_set_power_state(sc, 0);
953 case IEEE80211_S_INIT:
954 if (ostate == IEEE80211_S_RUN)
955 rum_abort_tsf_sync(sc);
959 case IEEE80211_S_RUN:
960 if (ostate == IEEE80211_S_SLEEP)
961 break; /* already handled */
963 ni = ieee80211_ref_node(vap->iv_bss);
965 if (vap->iv_opmode != IEEE80211_M_MONITOR) {
966 if (ic->ic_bsschan == IEEE80211_CHAN_ANYC ||
967 ni->ni_chan == IEEE80211_CHAN_ANYC) {
971 rum_update_slot_cb(sc, NULL, 0);
973 rum_set_txpreamble(sc);
974 rum_set_basicrates(sc);
975 rum_set_maxretry(sc, vap);
976 IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid);
977 rum_set_bssid(sc, sc->sc_bssid);
980 if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
981 vap->iv_opmode == IEEE80211_M_IBSS) {
982 if ((ret = rum_alloc_beacon(sc, vap)) != 0)
986 if (vap->iv_opmode != IEEE80211_M_MONITOR &&
987 vap->iv_opmode != IEEE80211_M_AHDEMO) {
988 if ((ret = rum_enable_tsf_sync(sc)) != 0)
993 /* enable automatic rate adaptation */
994 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
995 if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
996 rum_ratectl_start(sc, ni);
998 ieee80211_free_node(ni);
1000 case IEEE80211_S_SLEEP:
1001 /* Implemented for STA mode only. */
1002 if (vap->iv_opmode != IEEE80211_M_STA)
1005 uerror = rum_setbits(sc, RT2573_MAC_CSR11, RT2573_AUTO_WAKEUP);
1006 if (uerror != USB_ERR_NORMAL_COMPLETION) {
1011 uerror = rum_setbits(sc, RT2573_TXRX_CSR4, RT2573_ACKCTS_PWRMGT);
1012 if (uerror != USB_ERR_NORMAL_COMPLETION) {
1017 ret = rum_set_power_state(sc, 1);
1019 device_printf(sc->sc_dev,
1020 "%s: could not move to the SLEEP state: %s\n",
1021 __func__, usbd_errstr(uerror));
1029 return (ret == 0 ? rvp->newstate(vap, nstate, arg) : ret);
1033 rum_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
1035 struct rum_softc *sc = usbd_xfer_softc(xfer);
1036 struct ieee80211vap *vap;
1037 struct rum_tx_data *data;
1039 struct usb_page_cache *pc;
1043 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
1045 switch (USB_GET_STATE(xfer)) {
1046 case USB_ST_TRANSFERRED:
1047 DPRINTFN(11, "transfer complete, %d bytes\n", actlen);
1049 /* free resources */
1050 data = usbd_xfer_get_priv(xfer);
1051 rum_tx_free(data, 0);
1052 usbd_xfer_set_priv(xfer, NULL);
1057 data = STAILQ_FIRST(&sc->tx_q);
1059 STAILQ_REMOVE_HEAD(&sc->tx_q, next);
1062 if (m->m_pkthdr.len > (int)(MCLBYTES + RT2573_TX_DESC_SIZE)) {
1063 DPRINTFN(0, "data overflow, %u bytes\n",
1065 m->m_pkthdr.len = (MCLBYTES + RT2573_TX_DESC_SIZE);
1067 pc = usbd_xfer_get_frame(xfer, 0);
1068 usbd_copy_in(pc, 0, &data->desc, RT2573_TX_DESC_SIZE);
1069 usbd_m_copy_in(pc, RT2573_TX_DESC_SIZE, m, 0,
1072 vap = data->ni->ni_vap;
1073 if (ieee80211_radiotap_active_vap(vap)) {
1074 struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
1077 tap->wt_rate = data->rate;
1078 tap->wt_antenna = sc->tx_ant;
1080 ieee80211_radiotap_tx(vap, m);
1083 /* align end on a 4-bytes boundary */
1084 len = (RT2573_TX_DESC_SIZE + m->m_pkthdr.len + 3) & ~3;
1085 if ((len % 64) == 0)
1088 DPRINTFN(11, "sending frame len=%u xferlen=%u\n",
1089 m->m_pkthdr.len, len);
1091 usbd_xfer_set_frame_len(xfer, 0, len);
1092 usbd_xfer_set_priv(xfer, data);
1094 usbd_transfer_submit(xfer);
1099 default: /* Error */
1100 DPRINTFN(11, "transfer error, %s\n",
1101 usbd_errstr(error));
1103 counter_u64_add(sc->sc_ic.ic_oerrors, 1);
1104 data = usbd_xfer_get_priv(xfer);
1106 rum_tx_free(data, error);
1107 usbd_xfer_set_priv(xfer, NULL);
1110 if (error != USB_ERR_CANCELLED) {
1111 if (error == USB_ERR_TIMEOUT)
1112 device_printf(sc->sc_dev, "device timeout\n");
1115 * Try to clear stall first, also if other
1116 * errors occur, hence clearing stall
1117 * introduces a 50 ms delay:
1119 usbd_xfer_set_stall(xfer);
1127 rum_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
1129 struct rum_softc *sc = usbd_xfer_softc(xfer);
1130 struct ieee80211com *ic = &sc->sc_ic;
1131 struct ieee80211_frame_min *wh;
1132 struct ieee80211_node *ni;
1133 struct mbuf *m = NULL;
1134 struct usb_page_cache *pc;
1139 usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
1141 switch (USB_GET_STATE(xfer)) {
1142 case USB_ST_TRANSFERRED:
1144 DPRINTFN(15, "rx done, actlen=%d\n", len);
1146 if (len < (int)(RT2573_RX_DESC_SIZE + IEEE80211_MIN_LEN)) {
1147 DPRINTF("%s: xfer too short %d\n",
1148 device_get_nameunit(sc->sc_dev), len);
1149 counter_u64_add(ic->ic_ierrors, 1);
1153 len -= RT2573_RX_DESC_SIZE;
1154 pc = usbd_xfer_get_frame(xfer, 0);
1155 usbd_copy_out(pc, 0, &sc->sc_rx_desc, RT2573_RX_DESC_SIZE);
1157 rssi = rum_get_rssi(sc, sc->sc_rx_desc.rssi);
1158 flags = le32toh(sc->sc_rx_desc.flags);
1159 sc->last_rx_flags = flags;
1160 if (flags & RT2573_RX_CRC_ERROR) {
1162 * This should not happen since we did not
1163 * request to receive those frames when we
1164 * filled RUM_TXRX_CSR2:
1166 DPRINTFN(5, "PHY or CRC error\n");
1167 counter_u64_add(ic->ic_ierrors, 1);
1170 if ((flags & RT2573_RX_DEC_MASK) != RT2573_RX_DEC_OK) {
1171 switch (flags & RT2573_RX_DEC_MASK) {
1172 case RT2573_RX_IV_ERROR:
1173 DPRINTFN(5, "IV/EIV error\n");
1175 case RT2573_RX_MIC_ERROR:
1176 DPRINTFN(5, "MIC error\n");
1178 case RT2573_RX_KEY_ERROR:
1179 DPRINTFN(5, "Key error\n");
1182 counter_u64_add(ic->ic_ierrors, 1);
1186 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1188 DPRINTF("could not allocate mbuf\n");
1189 counter_u64_add(ic->ic_ierrors, 1);
1192 usbd_copy_out(pc, RT2573_RX_DESC_SIZE,
1193 mtod(m, uint8_t *), len);
1195 wh = mtod(m, struct ieee80211_frame_min *);
1197 if ((wh->i_fc[1] & IEEE80211_FC1_PROTECTED) &&
1198 (flags & RT2573_RX_CIP_MASK) !=
1199 RT2573_RX_CIP_MODE(RT2573_MODE_NOSEC)) {
1200 wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
1201 m->m_flags |= M_WEP;
1205 m->m_pkthdr.len = m->m_len = (flags >> 16) & 0xfff;
1207 if (ieee80211_radiotap_active(ic)) {
1208 struct rum_rx_radiotap_header *tap = &sc->sc_rxtap;
1211 tap->wr_rate = ieee80211_plcp2rate(sc->sc_rx_desc.rate,
1212 (flags & RT2573_RX_OFDM) ?
1213 IEEE80211_T_OFDM : IEEE80211_T_CCK);
1214 rum_get_tsf(sc, &tap->wr_tsf);
1215 tap->wr_antsignal = RT2573_NOISE_FLOOR + rssi;
1216 tap->wr_antnoise = RT2573_NOISE_FLOOR;
1217 tap->wr_antenna = sc->rx_ant;
1222 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1223 usbd_transfer_submit(xfer);
1226 * At the end of a USB callback it is always safe to unlock
1227 * the private mutex of a device! That is why we do the
1228 * "ieee80211_input" here, and not some lines up!
1232 if (m->m_len >= sizeof(struct ieee80211_frame_min))
1233 ni = ieee80211_find_rxnode(ic, wh);
1238 (void) ieee80211_input(ni, m, rssi,
1239 RT2573_NOISE_FLOOR);
1240 ieee80211_free_node(ni);
1242 (void) ieee80211_input_all(ic, m, rssi,
1243 RT2573_NOISE_FLOOR);
1249 default: /* Error */
1250 if (error != USB_ERR_CANCELLED) {
1251 /* try to clear stall first */
1252 usbd_xfer_set_stall(xfer);
1260 rum_plcp_signal(int rate)
1263 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1264 case 12: return 0xb;
1265 case 18: return 0xf;
1266 case 24: return 0xa;
1267 case 36: return 0xe;
1268 case 48: return 0x9;
1269 case 72: return 0xd;
1270 case 96: return 0x8;
1271 case 108: return 0xc;
1273 /* CCK rates (NB: not IEEE std, device-specific) */
1276 case 11: return 0x2;
1277 case 22: return 0x3;
1279 return 0xff; /* XXX unsupported/unknown rate */
1283 * Map net80211 cipher to RT2573 security mode.
1286 rum_crypto_mode(struct rum_softc *sc, u_int cipher, int keylen)
1289 case IEEE80211_CIPHER_WEP:
1290 return (keylen < 8 ? RT2573_MODE_WEP40 : RT2573_MODE_WEP104);
1291 case IEEE80211_CIPHER_TKIP:
1292 return RT2573_MODE_TKIP;
1293 case IEEE80211_CIPHER_AES_CCM:
1294 return RT2573_MODE_AES_CCMP;
1296 device_printf(sc->sc_dev, "unknown cipher %d\n", cipher);
1302 rum_setup_tx_desc(struct rum_softc *sc, struct rum_tx_desc *desc,
1303 struct ieee80211_key *k, uint32_t flags, uint8_t xflags, uint8_t qid,
1304 int hdrlen, int len, int rate)
1306 struct ieee80211com *ic = &sc->sc_ic;
1307 struct wmeParams *wmep = &sc->wme_params[qid];
1308 uint16_t plcp_length;
1311 flags |= RT2573_TX_VALID;
1314 if (k != NULL && !(k->wk_flags & IEEE80211_KEY_SWCRYPT)) {
1315 const struct ieee80211_cipher *cip = k->wk_cipher;
1317 len += cip->ic_header + cip->ic_trailer + cip->ic_miclen;
1319 desc->eiv = 0; /* for WEP */
1320 cip->ic_setiv(k, (uint8_t *)&desc->iv);
1323 /* setup PLCP fields */
1324 desc->plcp_signal = rum_plcp_signal(rate);
1325 desc->plcp_service = 4;
1327 len += IEEE80211_CRC_LEN;
1328 if (ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) {
1329 flags |= RT2573_TX_OFDM;
1331 plcp_length = len & 0xfff;
1332 desc->plcp_length_hi = plcp_length >> 6;
1333 desc->plcp_length_lo = plcp_length & 0x3f;
1336 rate = 2; /* avoid division by zero */
1337 plcp_length = howmany(16 * len, rate);
1339 remainder = (16 * len) % 22;
1340 if (remainder != 0 && remainder < 7)
1341 desc->plcp_service |= RT2573_PLCP_LENGEXT;
1343 desc->plcp_length_hi = plcp_length >> 8;
1344 desc->plcp_length_lo = plcp_length & 0xff;
1346 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1347 desc->plcp_signal |= 0x08;
1350 desc->flags = htole32(flags);
1351 desc->hdrlen = hdrlen;
1352 desc->xflags = xflags;
1354 desc->wme = htole16(RT2573_QID(qid) |
1355 RT2573_AIFSN(wmep->wmep_aifsn) |
1356 RT2573_LOGCWMIN(wmep->wmep_logcwmin) |
1357 RT2573_LOGCWMAX(wmep->wmep_logcwmax));
1361 rum_sendprot(struct rum_softc *sc,
1362 const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate)
1364 struct ieee80211com *ic = ni->ni_ic;
1365 const struct ieee80211_frame *wh;
1366 struct rum_tx_data *data;
1368 int protrate, pktlen, flags, isshort;
1371 RUM_LOCK_ASSERT(sc);
1372 KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY,
1373 ("protection %d", prot));
1375 wh = mtod(m, const struct ieee80211_frame *);
1376 pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
1378 protrate = ieee80211_ctl_rate(ic->ic_rt, rate);
1380 isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0;
1381 dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort)
1382 + ieee80211_ack_duration(ic->ic_rt, rate, isshort);
1384 if (prot == IEEE80211_PROT_RTSCTS) {
1385 /* NB: CTS is the same size as an ACK */
1386 dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort);
1387 flags |= RT2573_TX_NEED_ACK;
1388 mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur);
1390 mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur);
1392 if (mprot == NULL) {
1393 /* XXX stat + msg */
1396 data = STAILQ_FIRST(&sc->tx_free);
1397 STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1401 data->ni = ieee80211_ref_node(ni);
1402 data->rate = protrate;
1403 rum_setup_tx_desc(sc, &data->desc, NULL, flags, 0, 0, 0,
1404 mprot->m_pkthdr.len, protrate);
1406 STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1407 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1413 rum_tx_crypto_flags(struct rum_softc *sc, struct ieee80211_node *ni,
1414 const struct ieee80211_key *k)
1416 struct ieee80211vap *vap = ni->ni_vap;
1421 if (!(k->wk_flags & IEEE80211_KEY_SWCRYPT)) {
1422 cipher = k->wk_cipher->ic_cipher;
1424 mode = rum_crypto_mode(sc, cipher, k->wk_keylen);
1428 flags |= RT2573_TX_CIP_MODE(mode);
1430 /* Do not trust GROUP flag */
1431 if (!(k >= &vap->iv_nw_keys[0] &&
1432 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID]))
1433 flags |= RT2573_TX_KEY_PAIR;
1435 pos += 0 * RT2573_SKEY_MAX; /* vap id */
1437 flags |= RT2573_TX_KEY_ID(pos);
1439 if (cipher == IEEE80211_CIPHER_TKIP)
1440 flags |= RT2573_TX_TKIPMIC;
1447 rum_tx_mgt(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1449 struct ieee80211vap *vap = ni->ni_vap;
1450 struct ieee80211com *ic = &sc->sc_ic;
1451 struct rum_tx_data *data;
1452 struct ieee80211_frame *wh;
1453 const struct ieee80211_txparam *tp;
1454 struct ieee80211_key *k = NULL;
1457 uint8_t ac, type, xflags = 0;
1460 RUM_LOCK_ASSERT(sc);
1462 data = STAILQ_FIRST(&sc->tx_free);
1463 STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1466 wh = mtod(m0, struct ieee80211_frame *);
1467 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1468 hdrlen = ieee80211_anyhdrsize(wh);
1469 ac = M_WME_GETAC(m0);
1471 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1472 k = ieee80211_crypto_get_txkey(ni, m0);
1476 if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) &&
1477 !k->wk_cipher->ic_encap(k, m0))
1480 wh = mtod(m0, struct ieee80211_frame *);
1483 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1485 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1486 flags |= RT2573_TX_NEED_ACK;
1488 dur = ieee80211_ack_duration(ic->ic_rt, tp->mgmtrate,
1489 ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1490 USETW(wh->i_dur, dur);
1492 /* tell hardware to add timestamp for probe responses */
1493 if (type == IEEE80211_FC0_TYPE_MGT &&
1494 (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
1495 IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1496 flags |= RT2573_TX_TIMESTAMP;
1499 if (type != IEEE80211_FC0_TYPE_CTL && !IEEE80211_QOS_HAS_SEQ(wh))
1500 xflags |= RT2573_TX_HWSEQ;
1503 flags |= rum_tx_crypto_flags(sc, ni, k);
1507 data->rate = tp->mgmtrate;
1509 rum_setup_tx_desc(sc, &data->desc, k, flags, xflags, ac, hdrlen,
1510 m0->m_pkthdr.len, tp->mgmtrate);
1512 DPRINTFN(10, "sending mgt frame len=%d rate=%d\n",
1513 m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, tp->mgmtrate);
1515 STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1516 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1522 rum_tx_raw(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
1523 const struct ieee80211_bpf_params *params)
1525 struct ieee80211com *ic = ni->ni_ic;
1526 struct ieee80211_frame *wh;
1527 struct rum_tx_data *data;
1529 uint8_t ac, type, xflags = 0;
1532 RUM_LOCK_ASSERT(sc);
1534 wh = mtod(m0, struct ieee80211_frame *);
1535 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1537 ac = params->ibp_pri & 3;
1539 rate = params->ibp_rate0;
1540 if (!ieee80211_isratevalid(ic->ic_rt, rate))
1544 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
1545 flags |= RT2573_TX_NEED_ACK;
1546 if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) {
1547 error = rum_sendprot(sc, m0, ni,
1548 params->ibp_flags & IEEE80211_BPF_RTS ?
1549 IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY,
1551 if (error || sc->tx_nfree == 0)
1554 flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
1557 if (type != IEEE80211_FC0_TYPE_CTL && !IEEE80211_QOS_HAS_SEQ(wh))
1558 xflags |= RT2573_TX_HWSEQ;
1560 data = STAILQ_FIRST(&sc->tx_free);
1561 STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1568 /* XXX need to setup descriptor ourself */
1569 rum_setup_tx_desc(sc, &data->desc, NULL, flags, xflags, ac, 0,
1570 m0->m_pkthdr.len, rate);
1572 DPRINTFN(10, "sending raw frame len=%u rate=%u\n",
1573 m0->m_pkthdr.len, rate);
1575 STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1576 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1582 rum_tx_data(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1584 struct ieee80211vap *vap = ni->ni_vap;
1585 struct ieee80211com *ic = &sc->sc_ic;
1586 struct rum_tx_data *data;
1587 struct ieee80211_frame *wh;
1588 const struct ieee80211_txparam *tp;
1589 struct ieee80211_key *k = NULL;
1592 uint8_t ac, type, qos, xflags = 0;
1593 int error, hdrlen, rate;
1595 RUM_LOCK_ASSERT(sc);
1597 wh = mtod(m0, struct ieee80211_frame *);
1598 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1599 hdrlen = ieee80211_anyhdrsize(wh);
1601 if (IEEE80211_QOS_HAS_SEQ(wh))
1602 qos = ((const struct ieee80211_qosframe *)wh)->i_qos[0];
1605 ac = M_WME_GETAC(m0);
1607 tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
1608 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1609 rate = tp->mcastrate;
1610 else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
1611 rate = tp->ucastrate;
1613 rate = ni->ni_txrate;
1615 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1616 k = ieee80211_crypto_get_txkey(ni, m0);
1621 if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) &&
1622 !k->wk_cipher->ic_encap(k, m0)) {
1627 /* packet header may have moved, reset our local pointer */
1628 wh = mtod(m0, struct ieee80211_frame *);
1631 if (type != IEEE80211_FC0_TYPE_CTL && !IEEE80211_QOS_HAS_SEQ(wh))
1632 xflags |= RT2573_TX_HWSEQ;
1634 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1635 int prot = IEEE80211_PROT_NONE;
1636 if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold)
1637 prot = IEEE80211_PROT_RTSCTS;
1638 else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
1639 ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM)
1640 prot = ic->ic_protmode;
1641 if (prot != IEEE80211_PROT_NONE) {
1642 error = rum_sendprot(sc, m0, ni, prot, rate);
1643 if (error || sc->tx_nfree == 0) {
1647 flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
1652 flags |= rum_tx_crypto_flags(sc, ni, k);
1654 data = STAILQ_FIRST(&sc->tx_free);
1655 STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1662 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1663 /* Unicast frame, check if an ACK is expected. */
1664 if (!qos || (qos & IEEE80211_QOS_ACKPOLICY) !=
1665 IEEE80211_QOS_ACKPOLICY_NOACK)
1666 flags |= RT2573_TX_NEED_ACK;
1668 dur = ieee80211_ack_duration(ic->ic_rt, rate,
1669 ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1670 USETW(wh->i_dur, dur);
1673 rum_setup_tx_desc(sc, &data->desc, k, flags, xflags, ac, hdrlen,
1674 m0->m_pkthdr.len, rate);
1676 DPRINTFN(10, "sending frame len=%d rate=%d\n",
1677 m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, rate);
1679 STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1680 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1686 rum_transmit(struct ieee80211com *ic, struct mbuf *m)
1688 struct rum_softc *sc = ic->ic_softc;
1692 if (!sc->sc_running) {
1696 error = mbufq_enqueue(&sc->sc_snd, m);
1708 rum_start(struct rum_softc *sc)
1710 struct ieee80211_node *ni;
1713 RUM_LOCK_ASSERT(sc);
1715 if (!sc->sc_running)
1718 while (sc->tx_nfree >= RUM_TX_MINFREE &&
1719 (m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
1720 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1721 if (rum_tx_data(sc, m, ni) != 0) {
1722 if_inc_counter(ni->ni_vap->iv_ifp,
1723 IFCOUNTER_OERRORS, 1);
1724 ieee80211_free_node(ni);
1731 rum_parent(struct ieee80211com *ic)
1733 struct rum_softc *sc = ic->ic_softc;
1734 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1737 if (sc->sc_detached) {
1743 if (ic->ic_nrunning > 0) {
1744 if (rum_init(sc) == 0)
1745 ieee80211_start_all(ic);
1747 ieee80211_stop(vap);
1753 rum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len)
1755 struct usb_device_request req;
1758 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1759 req.bRequest = RT2573_READ_EEPROM;
1760 USETW(req.wValue, 0);
1761 USETW(req.wIndex, addr);
1762 USETW(req.wLength, len);
1764 error = rum_do_request(sc, &req, buf);
1766 device_printf(sc->sc_dev, "could not read EEPROM: %s\n",
1767 usbd_errstr(error));
1772 rum_read(struct rum_softc *sc, uint16_t reg)
1776 rum_read_multi(sc, reg, &val, sizeof val);
1778 return le32toh(val);
1782 rum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len)
1784 struct usb_device_request req;
1787 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1788 req.bRequest = RT2573_READ_MULTI_MAC;
1789 USETW(req.wValue, 0);
1790 USETW(req.wIndex, reg);
1791 USETW(req.wLength, len);
1793 error = rum_do_request(sc, &req, buf);
1795 device_printf(sc->sc_dev,
1796 "could not multi read MAC register: %s\n",
1797 usbd_errstr(error));
1802 rum_write(struct rum_softc *sc, uint16_t reg, uint32_t val)
1804 uint32_t tmp = htole32(val);
1806 return (rum_write_multi(sc, reg, &tmp, sizeof tmp));
1810 rum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len)
1812 struct usb_device_request req;
1816 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1817 req.bRequest = RT2573_WRITE_MULTI_MAC;
1818 USETW(req.wValue, 0);
1820 /* write at most 64 bytes at a time */
1821 for (offset = 0; offset < len; offset += 64) {
1822 USETW(req.wIndex, reg + offset);
1823 USETW(req.wLength, MIN(len - offset, 64));
1825 error = rum_do_request(sc, &req, (char *)buf + offset);
1827 device_printf(sc->sc_dev,
1828 "could not multi write MAC register: %s\n",
1829 usbd_errstr(error));
1834 return (USB_ERR_NORMAL_COMPLETION);
1838 rum_setbits(struct rum_softc *sc, uint16_t reg, uint32_t mask)
1840 return (rum_write(sc, reg, rum_read(sc, reg) | mask));
1844 rum_clrbits(struct rum_softc *sc, uint16_t reg, uint32_t mask)
1846 return (rum_write(sc, reg, rum_read(sc, reg) & ~mask));
1850 rum_modbits(struct rum_softc *sc, uint16_t reg, uint32_t set, uint32_t unset)
1852 return (rum_write(sc, reg, (rum_read(sc, reg) & ~unset) | set));
1856 rum_bbp_busy(struct rum_softc *sc)
1860 for (ntries = 0; ntries < 100; ntries++) {
1861 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1863 if (rum_pause(sc, hz / 100))
1873 rum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val)
1877 DPRINTFN(2, "reg=0x%08x\n", reg);
1879 if (rum_bbp_busy(sc) != 0) {
1880 device_printf(sc->sc_dev, "could not write to BBP\n");
1884 tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val;
1885 rum_write(sc, RT2573_PHY_CSR3, tmp);
1889 rum_bbp_read(struct rum_softc *sc, uint8_t reg)
1894 DPRINTFN(2, "reg=0x%08x\n", reg);
1896 if (rum_bbp_busy(sc) != 0) {
1897 device_printf(sc->sc_dev, "could not read BBP\n");
1901 val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8;
1902 rum_write(sc, RT2573_PHY_CSR3, val);
1904 for (ntries = 0; ntries < 100; ntries++) {
1905 val = rum_read(sc, RT2573_PHY_CSR3);
1906 if (!(val & RT2573_BBP_BUSY))
1908 if (rum_pause(sc, hz / 100))
1912 device_printf(sc->sc_dev, "could not read BBP\n");
1917 rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val)
1922 for (ntries = 0; ntries < 100; ntries++) {
1923 if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY))
1925 if (rum_pause(sc, hz / 100))
1928 if (ntries == 100) {
1929 device_printf(sc->sc_dev, "could not write to RF\n");
1933 tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 |
1935 rum_write(sc, RT2573_PHY_CSR4, tmp);
1937 /* remember last written value in sc */
1938 sc->rf_regs[reg] = val;
1940 DPRINTFN(15, "RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff);
1944 rum_select_antenna(struct rum_softc *sc)
1946 uint8_t bbp4, bbp77;
1949 bbp4 = rum_bbp_read(sc, 4);
1950 bbp77 = rum_bbp_read(sc, 77);
1954 /* make sure Rx is disabled before switching antenna */
1955 tmp = rum_read(sc, RT2573_TXRX_CSR0);
1956 rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
1958 rum_bbp_write(sc, 4, bbp4);
1959 rum_bbp_write(sc, 77, bbp77);
1961 rum_write(sc, RT2573_TXRX_CSR0, tmp);
1965 * Enable multi-rate retries for frames sent at OFDM rates.
1966 * In 802.11b/g mode, allow fallback to CCK rates.
1969 rum_enable_mrr(struct rum_softc *sc)
1971 struct ieee80211com *ic = &sc->sc_ic;
1973 if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) {
1974 rum_setbits(sc, RT2573_TXRX_CSR4,
1975 RT2573_MRR_ENABLED | RT2573_MRR_CCK_FALLBACK);
1977 rum_modbits(sc, RT2573_TXRX_CSR4,
1978 RT2573_MRR_ENABLED, RT2573_MRR_CCK_FALLBACK);
1983 rum_set_txpreamble(struct rum_softc *sc)
1985 struct ieee80211com *ic = &sc->sc_ic;
1987 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
1988 rum_setbits(sc, RT2573_TXRX_CSR4, RT2573_SHORT_PREAMBLE);
1990 rum_clrbits(sc, RT2573_TXRX_CSR4, RT2573_SHORT_PREAMBLE);
1994 rum_set_basicrates(struct rum_softc *sc)
1996 struct ieee80211com *ic = &sc->sc_ic;
1998 /* update basic rate set */
1999 if (ic->ic_curmode == IEEE80211_MODE_11B) {
2000 /* 11b basic rates: 1, 2Mbps */
2001 rum_write(sc, RT2573_TXRX_CSR5, 0x3);
2002 } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) {
2003 /* 11a basic rates: 6, 12, 24Mbps */
2004 rum_write(sc, RT2573_TXRX_CSR5, 0x150);
2006 /* 11b/g basic rates: 1, 2, 5.5, 11Mbps */
2007 rum_write(sc, RT2573_TXRX_CSR5, 0xf);
2012 * Reprogram MAC/BBP to switch to a new band. Values taken from the reference
2016 rum_select_band(struct rum_softc *sc, struct ieee80211_channel *c)
2018 uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104;
2020 /* update all BBP registers that depend on the band */
2021 bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c;
2022 bbp35 = 0x50; bbp97 = 0x48; bbp98 = 0x48;
2023 if (IEEE80211_IS_CHAN_5GHZ(c)) {
2024 bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c;
2025 bbp35 += 0x10; bbp97 += 0x10; bbp98 += 0x10;
2027 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
2028 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
2029 bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10;
2033 rum_bbp_write(sc, 17, bbp17);
2034 rum_bbp_write(sc, 96, bbp96);
2035 rum_bbp_write(sc, 104, bbp104);
2037 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
2038 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
2039 rum_bbp_write(sc, 75, 0x80);
2040 rum_bbp_write(sc, 86, 0x80);
2041 rum_bbp_write(sc, 88, 0x80);
2044 rum_bbp_write(sc, 35, bbp35);
2045 rum_bbp_write(sc, 97, bbp97);
2046 rum_bbp_write(sc, 98, bbp98);
2048 if (IEEE80211_IS_CHAN_2GHZ(c)) {
2049 rum_modbits(sc, RT2573_PHY_CSR0, RT2573_PA_PE_2GHZ,
2052 rum_modbits(sc, RT2573_PHY_CSR0, RT2573_PA_PE_5GHZ,
2058 rum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c)
2060 struct ieee80211com *ic = &sc->sc_ic;
2061 const struct rfprog *rfprog;
2062 uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT;
2066 chan = ieee80211_chan2ieee(ic, c);
2067 if (chan == 0 || chan == IEEE80211_CHAN_ANY)
2070 /* select the appropriate RF settings based on what EEPROM says */
2071 rfprog = (sc->rf_rev == RT2573_RF_5225 ||
2072 sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226;
2074 /* find the settings for this channel (we know it exists) */
2075 for (i = 0; rfprog[i].chan != chan; i++);
2077 power = sc->txpow[i];
2081 } else if (power > 31) {
2082 bbp94 += power - 31;
2087 * If we are switching from the 2GHz band to the 5GHz band or
2088 * vice-versa, BBP registers need to be reprogrammed.
2090 if (c->ic_flags != ic->ic_curchan->ic_flags) {
2091 rum_select_band(sc, c);
2092 rum_select_antenna(sc);
2096 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
2097 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
2098 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
2099 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
2101 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
2102 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
2103 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7 | 1);
2104 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
2106 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
2107 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
2108 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
2109 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
2111 rum_pause(sc, hz / 100);
2113 /* enable smart mode for MIMO-capable RFs */
2114 bbp3 = rum_bbp_read(sc, 3);
2116 bbp3 &= ~RT2573_SMART_MODE;
2117 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527)
2118 bbp3 |= RT2573_SMART_MODE;
2120 rum_bbp_write(sc, 3, bbp3);
2122 if (bbp94 != RT2573_BBPR94_DEFAULT)
2123 rum_bbp_write(sc, 94, bbp94);
2125 /* give the chip some extra time to do the switchover */
2126 rum_pause(sc, hz / 100);
2130 rum_set_maxretry(struct rum_softc *sc, struct ieee80211vap *vap)
2132 const struct ieee80211_txparam *tp;
2133 struct ieee80211_node *ni = vap->iv_bss;
2134 struct rum_vap *rvp = RUM_VAP(vap);
2136 tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
2137 rvp->maxretry = tp->maxretry < 0xf ? tp->maxretry : 0xf;
2139 rum_modbits(sc, RT2573_TXRX_CSR4, RT2573_SHORT_RETRY(rvp->maxretry) |
2140 RT2573_LONG_RETRY(rvp->maxretry),
2141 RT2573_SHORT_RETRY_MASK | RT2573_LONG_RETRY_MASK);
2145 * Enable TSF synchronization and tell h/w to start sending beacons for IBSS
2146 * and HostAP operating modes.
2149 rum_enable_tsf_sync(struct rum_softc *sc)
2151 struct ieee80211com *ic = &sc->sc_ic;
2152 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2156 if (vap->iv_opmode != IEEE80211_M_STA) {
2158 * Change default 16ms TBTT adjustment to 8ms.
2159 * Must be done before enabling beacon generation.
2161 if (rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8) != 0)
2165 tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000;
2167 /* set beacon interval (in 1/16ms unit) */
2168 bintval = vap->iv_bss->ni_intval;
2169 tmp |= bintval * 16;
2170 tmp |= RT2573_TSF_TIMER_EN | RT2573_TBTT_TIMER_EN;
2172 switch (vap->iv_opmode) {
2173 case IEEE80211_M_STA:
2175 * Local TSF is always updated with remote TSF on beacon
2178 tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_STA);
2180 case IEEE80211_M_IBSS:
2182 * Local TSF is updated with remote TSF on beacon reception
2183 * only if the remote TSF is greater than local TSF.
2185 tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_IBSS);
2186 tmp |= RT2573_BCN_TX_EN;
2188 case IEEE80211_M_HOSTAP:
2189 /* SYNC with nobody */
2190 tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_HOSTAP);
2191 tmp |= RT2573_BCN_TX_EN;
2194 device_printf(sc->sc_dev,
2195 "Enabling TSF failed. undefined opmode %d\n",
2200 if (rum_write(sc, RT2573_TXRX_CSR9, tmp) != 0)
2203 /* refresh current sleep time */
2204 return (rum_set_sleep_time(sc, bintval));
2208 rum_enable_tsf(struct rum_softc *sc)
2210 rum_modbits(sc, RT2573_TXRX_CSR9, RT2573_TSF_TIMER_EN |
2211 RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_DIS), 0x00ffffff);
2215 rum_abort_tsf_sync(struct rum_softc *sc)
2217 rum_clrbits(sc, RT2573_TXRX_CSR9, 0x00ffffff);
2221 rum_get_tsf(struct rum_softc *sc, uint64_t *buf)
2223 rum_read_multi(sc, RT2573_TXRX_CSR12, buf, sizeof (*buf));
2227 rum_update_slot_cb(struct rum_softc *sc, union sec_param *data, uint8_t rvp_id)
2229 struct ieee80211com *ic = &sc->sc_ic;
2232 slottime = IEEE80211_GET_SLOTTIME(ic);
2234 rum_modbits(sc, RT2573_MAC_CSR9, slottime, 0xff);
2236 DPRINTF("setting slot time to %uus\n", slottime);
2240 rum_update_slot(struct ieee80211com *ic)
2242 rum_cmd_sleepable(ic->ic_softc, NULL, 0, 0, rum_update_slot_cb);
2246 rum_wme_update(struct ieee80211com *ic)
2248 const struct wmeParams *chanp =
2249 ic->ic_wme.wme_chanParams.cap_wmeParams;
2250 struct rum_softc *sc = ic->ic_softc;
2254 error = rum_write(sc, RT2573_AIFSN_CSR,
2255 chanp[WME_AC_VO].wmep_aifsn << 12 |
2256 chanp[WME_AC_VI].wmep_aifsn << 8 |
2257 chanp[WME_AC_BK].wmep_aifsn << 4 |
2258 chanp[WME_AC_BE].wmep_aifsn);
2261 error = rum_write(sc, RT2573_CWMIN_CSR,
2262 chanp[WME_AC_VO].wmep_logcwmin << 12 |
2263 chanp[WME_AC_VI].wmep_logcwmin << 8 |
2264 chanp[WME_AC_BK].wmep_logcwmin << 4 |
2265 chanp[WME_AC_BE].wmep_logcwmin);
2268 error = rum_write(sc, RT2573_CWMAX_CSR,
2269 chanp[WME_AC_VO].wmep_logcwmax << 12 |
2270 chanp[WME_AC_VI].wmep_logcwmax << 8 |
2271 chanp[WME_AC_BK].wmep_logcwmax << 4 |
2272 chanp[WME_AC_BE].wmep_logcwmax);
2275 error = rum_write(sc, RT2573_TXOP01_CSR,
2276 chanp[WME_AC_BK].wmep_txopLimit << 16 |
2277 chanp[WME_AC_BE].wmep_txopLimit);
2280 error = rum_write(sc, RT2573_TXOP23_CSR,
2281 chanp[WME_AC_VO].wmep_txopLimit << 16 |
2282 chanp[WME_AC_VI].wmep_txopLimit);
2286 memcpy(sc->wme_params, chanp, sizeof(*chanp) * WME_NUM_AC);
2291 device_printf(sc->sc_dev, "%s: WME update failed, error %d\n",
2299 rum_set_bssid(struct rum_softc *sc, const uint8_t *bssid)
2302 rum_write(sc, RT2573_MAC_CSR4,
2303 bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24);
2304 rum_write(sc, RT2573_MAC_CSR5,
2305 bssid[4] | bssid[5] << 8 | RT2573_NUM_BSSID_MSK(1));
2309 rum_set_macaddr(struct rum_softc *sc, const uint8_t *addr)
2312 rum_write(sc, RT2573_MAC_CSR2,
2313 addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
2314 rum_write(sc, RT2573_MAC_CSR3,
2315 addr[4] | addr[5] << 8 | 0xff << 16);
2319 rum_setpromisc(struct rum_softc *sc)
2321 struct ieee80211com *ic = &sc->sc_ic;
2323 if (ic->ic_promisc == 0)
2324 rum_setbits(sc, RT2573_TXRX_CSR0, RT2573_DROP_NOT_TO_ME);
2326 rum_clrbits(sc, RT2573_TXRX_CSR0, RT2573_DROP_NOT_TO_ME);
2328 DPRINTF("%s promiscuous mode\n", ic->ic_promisc > 0 ?
2329 "entering" : "leaving");
2333 rum_update_promisc(struct ieee80211com *ic)
2335 struct rum_softc *sc = ic->ic_softc;
2344 rum_update_mcast(struct ieee80211com *ic)
2353 case RT2573_RF_2527: return "RT2527 (MIMO XR)";
2354 case RT2573_RF_2528: return "RT2528";
2355 case RT2573_RF_5225: return "RT5225 (MIMO XR)";
2356 case RT2573_RF_5226: return "RT5226";
2357 default: return "unknown";
2362 rum_read_eeprom(struct rum_softc *sc)
2369 /* read MAC address */
2370 rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, sc->sc_ic.ic_macaddr, 6);
2372 rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2);
2374 sc->rf_rev = (val >> 11) & 0x1f;
2375 sc->hw_radio = (val >> 10) & 0x1;
2376 sc->rx_ant = (val >> 4) & 0x3;
2377 sc->tx_ant = (val >> 2) & 0x3;
2378 sc->nb_ant = val & 0x3;
2380 DPRINTF("RF revision=%d\n", sc->rf_rev);
2382 rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2);
2384 sc->ext_5ghz_lna = (val >> 6) & 0x1;
2385 sc->ext_2ghz_lna = (val >> 4) & 0x1;
2387 DPRINTF("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n",
2388 sc->ext_2ghz_lna, sc->ext_5ghz_lna);
2390 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2);
2392 if ((val & 0xff) != 0xff)
2393 sc->rssi_2ghz_corr = (int8_t)(val & 0xff); /* signed */
2395 /* Only [-10, 10] is valid */
2396 if (sc->rssi_2ghz_corr < -10 || sc->rssi_2ghz_corr > 10)
2397 sc->rssi_2ghz_corr = 0;
2399 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2);
2401 if ((val & 0xff) != 0xff)
2402 sc->rssi_5ghz_corr = (int8_t)(val & 0xff); /* signed */
2404 /* Only [-10, 10] is valid */
2405 if (sc->rssi_5ghz_corr < -10 || sc->rssi_5ghz_corr > 10)
2406 sc->rssi_5ghz_corr = 0;
2408 if (sc->ext_2ghz_lna)
2409 sc->rssi_2ghz_corr -= 14;
2410 if (sc->ext_5ghz_lna)
2411 sc->rssi_5ghz_corr -= 14;
2413 DPRINTF("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n",
2414 sc->rssi_2ghz_corr, sc->rssi_5ghz_corr);
2416 rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2);
2418 if ((val & 0xff) != 0xff)
2419 sc->rffreq = val & 0xff;
2421 DPRINTF("RF freq=%d\n", sc->rffreq);
2423 /* read Tx power for all a/b/g channels */
2424 rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14);
2425 /* XXX default Tx power for 802.11a channels */
2426 memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14);
2428 for (i = 0; i < 14; i++)
2429 DPRINTF("Channel=%d Tx power=%d\n", i + 1, sc->txpow[i]);
2432 /* read default values for BBP registers */
2433 rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
2435 for (i = 0; i < 14; i++) {
2436 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
2438 DPRINTF("BBP R%d=%02x\n", sc->bbp_prom[i].reg,
2439 sc->bbp_prom[i].val);
2445 rum_bbp_wakeup(struct rum_softc *sc)
2447 unsigned int ntries;
2449 for (ntries = 0; ntries < 100; ntries++) {
2450 if (rum_read(sc, RT2573_MAC_CSR12) & 8)
2452 rum_write(sc, RT2573_MAC_CSR12, 4); /* force wakeup */
2453 if (rum_pause(sc, hz / 100))
2456 if (ntries == 100) {
2457 device_printf(sc->sc_dev,
2458 "timeout waiting for BBP/RF to wakeup\n");
2466 rum_bbp_init(struct rum_softc *sc)
2470 /* wait for BBP to be ready */
2471 for (ntries = 0; ntries < 100; ntries++) {
2472 const uint8_t val = rum_bbp_read(sc, 0);
2473 if (val != 0 && val != 0xff)
2475 if (rum_pause(sc, hz / 100))
2478 if (ntries == 100) {
2479 device_printf(sc->sc_dev, "timeout waiting for BBP\n");
2483 /* initialize BBP registers to default values */
2484 for (i = 0; i < nitems(rum_def_bbp); i++)
2485 rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val);
2487 /* write vendor-specific BBP values (from EEPROM) */
2488 for (i = 0; i < 16; i++) {
2489 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
2491 rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
2498 rum_clr_shkey_regs(struct rum_softc *sc)
2500 rum_write(sc, RT2573_SEC_CSR0, 0);
2501 rum_write(sc, RT2573_SEC_CSR1, 0);
2502 rum_write(sc, RT2573_SEC_CSR5, 0);
2506 rum_init(struct rum_softc *sc)
2508 struct ieee80211com *ic = &sc->sc_ic;
2509 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2514 if (sc->sc_running) {
2519 /* initialize MAC registers to default values */
2520 for (i = 0; i < nitems(rum_def_mac); i++)
2521 rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val);
2523 /* reset some WME parameters to default values */
2524 sc->wme_params[0].wmep_aifsn = 2;
2525 sc->wme_params[0].wmep_logcwmin = 4;
2526 sc->wme_params[0].wmep_logcwmax = 10;
2528 /* set host ready */
2529 rum_write(sc, RT2573_MAC_CSR1, RT2573_RESET_ASIC | RT2573_RESET_BBP);
2530 rum_write(sc, RT2573_MAC_CSR1, 0);
2532 /* wait for BBP/RF to wakeup */
2533 if ((ret = rum_bbp_wakeup(sc)) != 0)
2536 if ((ret = rum_bbp_init(sc)) != 0)
2539 /* select default channel */
2540 rum_select_band(sc, ic->ic_curchan);
2541 rum_select_antenna(sc);
2542 rum_set_chan(sc, ic->ic_curchan);
2544 /* clear STA registers */
2545 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
2547 /* clear security registers (if required) */
2548 if (sc->sc_clr_shkeys == 0) {
2549 rum_clr_shkey_regs(sc);
2550 sc->sc_clr_shkeys = 1;
2553 rum_set_macaddr(sc, vap ? vap->iv_myaddr : ic->ic_macaddr);
2555 /* initialize ASIC */
2556 rum_write(sc, RT2573_MAC_CSR1, RT2573_HOST_READY);
2559 * Allocate Tx and Rx xfer queues.
2561 rum_setup_tx_list(sc);
2563 /* update Rx filter */
2564 tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff;
2566 tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR;
2567 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2568 tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR |
2570 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2571 tmp |= RT2573_DROP_TODS;
2572 if (ic->ic_promisc == 0)
2573 tmp |= RT2573_DROP_NOT_TO_ME;
2575 rum_write(sc, RT2573_TXRX_CSR0, tmp);
2578 usbd_xfer_set_stall(sc->sc_xfer[RUM_BULK_WR]);
2579 usbd_transfer_start(sc->sc_xfer[RUM_BULK_RD]);
2581 end: RUM_UNLOCK(sc);
2590 rum_stop(struct rum_softc *sc)
2594 if (!sc->sc_running) {
2602 * Drain the USB transfers, if not already drained:
2604 usbd_transfer_drain(sc->sc_xfer[RUM_BULK_WR]);
2605 usbd_transfer_drain(sc->sc_xfer[RUM_BULK_RD]);
2608 rum_unsetup_tx_list(sc);
2611 rum_setbits(sc, RT2573_TXRX_CSR0, RT2573_DISABLE_RX);
2614 rum_write(sc, RT2573_MAC_CSR1, RT2573_RESET_ASIC | RT2573_RESET_BBP);
2615 rum_write(sc, RT2573_MAC_CSR1, 0);
2620 rum_load_microcode(struct rum_softc *sc, const uint8_t *ucode, size_t size)
2622 uint16_t reg = RT2573_MCU_CODE_BASE;
2625 /* copy firmware image into NIC */
2626 for (; size >= 4; reg += 4, ucode += 4, size -= 4) {
2627 err = rum_write(sc, reg, UGETDW(ucode));
2629 /* firmware already loaded ? */
2630 device_printf(sc->sc_dev, "Firmware load "
2631 "failure! (ignored)\n");
2636 err = rum_do_mcu_request(sc, RT2573_MCU_RUN);
2637 if (err != USB_ERR_NORMAL_COMPLETION) {
2638 device_printf(sc->sc_dev, "could not run firmware: %s\n",
2642 /* give the chip some time to boot */
2643 rum_pause(sc, hz / 8);
2647 rum_set_sleep_time(struct rum_softc *sc, uint16_t bintval)
2649 struct ieee80211com *ic = &sc->sc_ic;
2653 RUM_LOCK_ASSERT(sc);
2655 exp = ic->ic_lintval / bintval;
2656 delay = ic->ic_lintval % bintval;
2658 if (exp > RT2573_TBCN_EXP_MAX)
2659 exp = RT2573_TBCN_EXP_MAX;
2660 if (delay > RT2573_TBCN_DELAY_MAX)
2661 delay = RT2573_TBCN_DELAY_MAX;
2663 uerror = rum_modbits(sc, RT2573_MAC_CSR11,
2664 RT2573_TBCN_EXP(exp) |
2665 RT2573_TBCN_DELAY(delay),
2666 RT2573_TBCN_EXP(RT2573_TBCN_EXP_MAX) |
2667 RT2573_TBCN_DELAY(RT2573_TBCN_DELAY_MAX));
2669 if (uerror != USB_ERR_NORMAL_COMPLETION)
2672 sc->sc_sleep_time = IEEE80211_TU_TO_TICKS(exp * bintval + delay);
2678 rum_reset(struct ieee80211vap *vap, u_long cmd)
2680 struct ieee80211com *ic = vap->iv_ic;
2681 struct ieee80211_node *ni;
2682 struct rum_softc *sc = ic->ic_softc;
2686 case IEEE80211_IOC_POWERSAVE:
2689 case IEEE80211_IOC_POWERSAVESLEEP:
2690 ni = ieee80211_ref_node(vap->iv_bss);
2693 error = rum_set_sleep_time(sc, ni->ni_intval);
2694 if (vap->iv_state == IEEE80211_S_SLEEP) {
2695 /* Use new values for wakeup timer. */
2696 rum_clrbits(sc, RT2573_MAC_CSR11, RT2573_AUTO_WAKEUP);
2697 rum_setbits(sc, RT2573_MAC_CSR11, RT2573_AUTO_WAKEUP);
2699 /* XXX send reassoc */
2702 ieee80211_free_node(ni);
2713 rum_set_beacon(struct rum_softc *sc, struct ieee80211vap *vap)
2715 struct ieee80211com *ic = vap->iv_ic;
2716 struct rum_vap *rvp = RUM_VAP(vap);
2717 struct mbuf *m = rvp->bcn_mbuf;
2718 const struct ieee80211_txparam *tp;
2719 struct rum_tx_desc desc;
2721 RUM_LOCK_ASSERT(sc);
2725 if (ic->ic_bsschan == IEEE80211_CHAN_ANYC)
2728 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)];
2729 rum_setup_tx_desc(sc, &desc, NULL, RT2573_TX_TIMESTAMP,
2730 RT2573_TX_HWSEQ, 0, 0, m->m_pkthdr.len, tp->mgmtrate);
2732 /* copy the Tx descriptor into NIC memory */
2733 if (rum_write_multi(sc, RT2573_HW_BCN_BASE(0), (uint8_t *)&desc,
2734 RT2573_TX_DESC_SIZE) != 0)
2737 /* copy beacon header and payload into NIC memory */
2738 if (rum_write_multi(sc, RT2573_HW_BCN_BASE(0) + RT2573_TX_DESC_SIZE,
2739 mtod(m, uint8_t *), m->m_pkthdr.len) != 0)
2746 rum_alloc_beacon(struct rum_softc *sc, struct ieee80211vap *vap)
2748 struct rum_vap *rvp = RUM_VAP(vap);
2749 struct ieee80211_node *ni = vap->iv_bss;
2752 if (ni->ni_chan == IEEE80211_CHAN_ANYC)
2755 m = ieee80211_beacon_alloc(ni);
2759 if (rvp->bcn_mbuf != NULL)
2760 m_freem(rvp->bcn_mbuf);
2764 return (rum_set_beacon(sc, vap));
2768 rum_update_beacon_cb(struct rum_softc *sc, union sec_param *data,
2771 struct ieee80211vap *vap = data->vap;
2773 rum_set_beacon(sc, vap);
2777 rum_update_beacon(struct ieee80211vap *vap, int item)
2779 struct ieee80211com *ic = vap->iv_ic;
2780 struct rum_softc *sc = ic->ic_softc;
2781 struct rum_vap *rvp = RUM_VAP(vap);
2782 struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
2783 struct ieee80211_node *ni = vap->iv_bss;
2784 struct mbuf *m = rvp->bcn_mbuf;
2789 m = ieee80211_beacon_alloc(ni);
2791 device_printf(sc->sc_dev,
2792 "%s: could not allocate beacon frame\n", __func__);
2800 case IEEE80211_BEACON_ERP:
2801 rum_update_slot(ic);
2803 case IEEE80211_BEACON_TIM:
2811 setbit(bo->bo_flags, item);
2812 ieee80211_beacon_update(ni, m, mcast);
2814 rum_cmd_sleepable(sc, &vap, sizeof(vap), 0, rum_update_beacon_cb);
2818 rum_common_key_set(struct rum_softc *sc, struct ieee80211_key *k,
2822 if (rum_write_multi(sc, base, k->wk_key, k->wk_keylen))
2825 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP) {
2826 if (rum_write_multi(sc, base + IEEE80211_KEYBUF_SIZE,
2829 if (rum_write_multi(sc, base + IEEE80211_KEYBUF_SIZE + 8,
2838 rum_group_key_set_cb(struct rum_softc *sc, union sec_param *data,
2841 struct ieee80211_key *k = &data->key;
2844 if (sc->sc_clr_shkeys == 0) {
2845 rum_clr_shkey_regs(sc);
2846 sc->sc_clr_shkeys = 1;
2849 mode = rum_crypto_mode(sc, k->wk_cipher->ic_cipher, k->wk_keylen);
2853 DPRINTFN(1, "setting group key %d for vap %d, mode %d "
2854 "(tx %s, rx %s)\n", k->wk_keyix, rvp_id, mode,
2855 (k->wk_flags & IEEE80211_KEY_XMIT) ? "on" : "off",
2856 (k->wk_flags & IEEE80211_KEY_RECV) ? "on" : "off");
2858 /* Install the key. */
2859 if (rum_common_key_set(sc, k, RT2573_SKEY(rvp_id, k->wk_keyix)) != 0)
2862 /* Set cipher mode. */
2863 if (rum_modbits(sc, rvp_id < 2 ? RT2573_SEC_CSR1 : RT2573_SEC_CSR5,
2864 mode << (rvp_id % 2 + k->wk_keyix) * RT2573_SKEY_MAX,
2865 RT2573_MODE_MASK << (rvp_id % 2 + k->wk_keyix) * RT2573_SKEY_MAX)
2869 /* Mark this key as valid. */
2870 if (rum_setbits(sc, RT2573_SEC_CSR0,
2871 1 << (rvp_id * RT2573_SKEY_MAX + k->wk_keyix)) != 0)
2877 device_printf(sc->sc_dev, "%s: cannot set group key %d for vap %d\n",
2878 __func__, k->wk_keyix, rvp_id);
2882 rum_group_key_del_cb(struct rum_softc *sc, union sec_param *data,
2885 struct ieee80211_key *k = &data->key;
2887 DPRINTF("%s: removing group key %d for vap %d\n", __func__,
2888 k->wk_keyix, rvp_id);
2890 rvp_id < 2 ? RT2573_SEC_CSR1 : RT2573_SEC_CSR5,
2891 RT2573_MODE_MASK << (rvp_id % 2 + k->wk_keyix) * RT2573_SKEY_MAX);
2892 rum_clrbits(sc, RT2573_SEC_CSR0,
2893 rvp_id * RT2573_SKEY_MAX + k->wk_keyix);
2897 rum_pair_key_set_cb(struct rum_softc *sc, union sec_param *data,
2900 struct ieee80211_key *k = &data->key;
2901 uint8_t buf[IEEE80211_ADDR_LEN + 1];
2904 mode = rum_crypto_mode(sc, k->wk_cipher->ic_cipher, k->wk_keylen);
2908 DPRINTFN(1, "setting pairwise key %d for vap %d, mode %d "
2909 "(tx %s, rx %s)\n", k->wk_keyix, rvp_id, mode,
2910 (k->wk_flags & IEEE80211_KEY_XMIT) ? "on" : "off",
2911 (k->wk_flags & IEEE80211_KEY_RECV) ? "on" : "off");
2913 /* Install the key. */
2914 if (rum_common_key_set(sc, k, RT2573_PKEY(k->wk_keyix)) != 0)
2917 IEEE80211_ADDR_COPY(buf, k->wk_macaddr);
2918 buf[IEEE80211_ADDR_LEN] = mode;
2920 /* Set transmitter address and cipher mode. */
2921 if (rum_write_multi(sc, RT2573_ADDR_ENTRY(k->wk_keyix),
2922 buf, sizeof buf) != 0)
2925 /* Enable key table lookup for this vap. */
2926 if (sc->vap_key_count[rvp_id]++ == 0)
2927 if (rum_setbits(sc, RT2573_SEC_CSR4, 1 << rvp_id) != 0)
2930 /* Mark this key as valid. */
2932 k->wk_keyix < 32 ? RT2573_SEC_CSR2 : RT2573_SEC_CSR3,
2933 1 << (k->wk_keyix % 32)) != 0)
2939 device_printf(sc->sc_dev,
2940 "%s: cannot set pairwise key %d, vap %d\n", __func__, k->wk_keyix,
2945 rum_pair_key_del_cb(struct rum_softc *sc, union sec_param *data,
2948 struct ieee80211_key *k = &data->key;
2950 DPRINTF("%s: removing key %d\n", __func__, k->wk_keyix);
2951 rum_clrbits(sc, (k->wk_keyix < 32) ? RT2573_SEC_CSR2 : RT2573_SEC_CSR3,
2952 1 << (k->wk_keyix % 32));
2953 sc->keys_bmap &= ~(1ULL << k->wk_keyix);
2954 if (--sc->vap_key_count[rvp_id] == 0)
2955 rum_clrbits(sc, RT2573_SEC_CSR4, 1 << rvp_id);
2959 rum_key_alloc(struct ieee80211vap *vap, struct ieee80211_key *k,
2960 ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
2962 struct rum_softc *sc = vap->iv_ic->ic_softc;
2965 if (!(&vap->iv_nw_keys[0] <= k &&
2966 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID])) {
2967 if (!(k->wk_flags & IEEE80211_KEY_SWCRYPT)) {
2969 for (i = 0; i < RT2573_ADDR_MAX; i++) {
2970 if ((sc->keys_bmap & (1ULL << i)) == 0) {
2971 sc->keys_bmap |= (1ULL << i);
2977 if (i == RT2573_ADDR_MAX) {
2978 device_printf(sc->sc_dev,
2979 "%s: no free space in the key table\n",
2986 *keyix = k - vap->iv_nw_keys;
2993 rum_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
2995 struct rum_softc *sc = vap->iv_ic->ic_softc;
2998 if (k->wk_flags & IEEE80211_KEY_SWCRYPT) {
3003 group = k >= &vap->iv_nw_keys[0] && k < &vap->iv_nw_keys[IEEE80211_WEP_NKID];
3005 return !rum_cmd_sleepable(sc, k, sizeof(*k), 0,
3006 group ? rum_group_key_set_cb : rum_pair_key_set_cb);
3010 rum_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
3012 struct rum_softc *sc = vap->iv_ic->ic_softc;
3015 if (k->wk_flags & IEEE80211_KEY_SWCRYPT) {
3020 group = k >= &vap->iv_nw_keys[0] && k < &vap->iv_nw_keys[IEEE80211_WEP_NKID];
3022 return !rum_cmd_sleepable(sc, k, sizeof(*k), 0,
3023 group ? rum_group_key_del_cb : rum_pair_key_del_cb);
3027 rum_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
3028 const struct ieee80211_bpf_params *params)
3030 struct rum_softc *sc = ni->ni_ic->ic_softc;
3034 /* prevent management frames from being sent if we're not ready */
3035 if (!sc->sc_running) {
3039 if (sc->tx_nfree < RUM_TX_MINFREE) {
3044 if (params == NULL) {
3046 * Legacy path; interpret frame contents to decide
3047 * precisely how to send the frame.
3049 if ((ret = rum_tx_mgt(sc, m, ni)) != 0)
3053 * Caller supplied explicit parameters to use in
3054 * sending the frame.
3056 if ((ret = rum_tx_raw(sc, m, ni, params)) != 0)
3069 rum_ratectl_start(struct rum_softc *sc, struct ieee80211_node *ni)
3071 struct ieee80211vap *vap = ni->ni_vap;
3072 struct rum_vap *rvp = RUM_VAP(vap);
3074 /* clear statistic registers (STA_CSR0 to STA_CSR5) */
3075 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
3077 usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp);
3081 rum_ratectl_timeout(void *arg)
3083 struct rum_vap *rvp = arg;
3084 struct ieee80211vap *vap = &rvp->vap;
3085 struct ieee80211com *ic = vap->iv_ic;
3087 ieee80211_runtask(ic, &rvp->ratectl_task);
3091 rum_ratectl_task(void *arg, int pending)
3093 struct rum_vap *rvp = arg;
3094 struct ieee80211vap *vap = &rvp->vap;
3095 struct rum_softc *sc = vap->iv_ic->ic_softc;
3096 struct ieee80211_node *ni;
3098 int sum, success, retrycnt;
3101 /* read and clear statistic registers (STA_CSR0 to STA_CSR5) */
3102 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof(sc->sta));
3104 ok[0] = (le32toh(sc->sta[4]) & 0xffff); /* TX ok w/o retry */
3105 ok[1] = (le32toh(sc->sta[4]) >> 16); /* TX ok w/ one retry */
3106 ok[2] = (le32toh(sc->sta[5]) & 0xffff); /* TX ok w/ multiple retries */
3107 fail = (le32toh(sc->sta[5]) >> 16); /* TX retry-fail count */
3109 success = ok[0] + ok[1] + ok[2];
3110 sum = success + fail;
3112 retrycnt = ok[1] + ok[2] * 2 + fail * (rvp->maxretry + 1);
3115 ni = ieee80211_ref_node(vap->iv_bss);
3116 ieee80211_ratectl_tx_update(vap, ni, &sum, &ok, &retrycnt);
3117 (void) ieee80211_ratectl_rate(ni, NULL, 0);
3118 ieee80211_free_node(ni);
3121 /* count TX retry-fail as Tx errors */
3122 if_inc_counter(vap->iv_ifp, IFCOUNTER_OERRORS, fail);
3124 usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp);
3129 rum_scan_start(struct ieee80211com *ic)
3131 struct rum_softc *sc = ic->ic_softc;
3134 rum_abort_tsf_sync(sc);
3135 rum_set_bssid(sc, ieee80211broadcastaddr);
3141 rum_scan_end(struct ieee80211com *ic)
3143 struct rum_softc *sc = ic->ic_softc;
3145 if (ic->ic_flags_ext & IEEE80211_FEXT_BGSCAN) {
3147 if (ic->ic_opmode != IEEE80211_M_AHDEMO)
3148 rum_enable_tsf_sync(sc);
3151 rum_set_bssid(sc, sc->sc_bssid);
3157 rum_set_channel(struct ieee80211com *ic)
3159 struct rum_softc *sc = ic->ic_softc;
3162 rum_set_chan(sc, ic->ic_curchan);
3167 rum_getradiocaps(struct ieee80211com *ic,
3168 int maxchans, int *nchans, struct ieee80211_channel chans[])
3170 struct rum_softc *sc = ic->ic_softc;
3171 uint8_t bands[IEEE80211_MODE_BYTES];
3173 memset(bands, 0, sizeof(bands));
3174 setbit(bands, IEEE80211_MODE_11B);
3175 setbit(bands, IEEE80211_MODE_11G);
3176 ieee80211_add_channels_default_2ghz(chans, maxchans, nchans, bands, 0);
3178 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_5226) {
3179 setbit(bands, IEEE80211_MODE_11A);
3180 ieee80211_add_channel_list_5ghz(chans, maxchans, nchans,
3181 rum_chan_5ghz, nitems(rum_chan_5ghz), bands, 0);
3186 rum_get_rssi(struct rum_softc *sc, uint8_t raw)
3188 struct ieee80211com *ic = &sc->sc_ic;
3191 lna = (raw >> 5) & 0x3;
3198 * NB: Since RSSI is relative to noise floor, -1 is
3199 * adequate for caller to know error happened.
3204 rssi = (2 * agc) - RT2573_NOISE_FLOOR;
3206 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) {
3207 rssi += sc->rssi_2ghz_corr;
3216 rssi += sc->rssi_5ghz_corr;
3218 if (!sc->ext_5ghz_lna && lna != 1)
3232 rum_pause(struct rum_softc *sc, int timeout)
3235 usb_pause_mtx(&sc->sc_mtx, timeout);
3239 static device_method_t rum_methods[] = {
3240 /* Device interface */
3241 DEVMETHOD(device_probe, rum_match),
3242 DEVMETHOD(device_attach, rum_attach),
3243 DEVMETHOD(device_detach, rum_detach),
3247 static driver_t rum_driver = {
3249 .methods = rum_methods,
3250 .size = sizeof(struct rum_softc),
3253 static devclass_t rum_devclass;
3255 DRIVER_MODULE(rum, uhub, rum_driver, rum_devclass, NULL, 0);
3256 MODULE_DEPEND(rum, wlan, 1, 1, 1);
3257 MODULE_DEPEND(rum, usb, 1, 1, 1);
3258 MODULE_VERSION(rum, 1);
3259 USB_PNP_HOST_INFO(rum_devs);