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>
47 #include <machine/bus.h>
48 #include <machine/resource.h>
53 #include <net/if_var.h>
54 #include <net/if_arp.h>
55 #include <net/ethernet.h>
56 #include <net/if_dl.h>
57 #include <net/if_media.h>
58 #include <net/if_types.h>
61 #include <netinet/in.h>
62 #include <netinet/in_systm.h>
63 #include <netinet/in_var.h>
64 #include <netinet/if_ether.h>
65 #include <netinet/ip.h>
68 #include <net80211/ieee80211_var.h>
69 #include <net80211/ieee80211_regdomain.h>
70 #include <net80211/ieee80211_radiotap.h>
71 #include <net80211/ieee80211_ratectl.h>
73 #include <dev/usb/usb.h>
74 #include <dev/usb/usbdi.h>
77 #define USB_DEBUG_VAR rum_debug
78 #include <dev/usb/usb_debug.h>
80 #include <dev/usb/wlan/if_rumreg.h>
81 #include <dev/usb/wlan/if_rumvar.h>
82 #include <dev/usb/wlan/if_rumfw.h>
85 static int rum_debug = 0;
87 static SYSCTL_NODE(_hw_usb, OID_AUTO, rum, CTLFLAG_RW, 0, "USB rum");
88 SYSCTL_INT(_hw_usb_rum, OID_AUTO, debug, CTLFLAG_RWTUN, &rum_debug, 0,
92 static const STRUCT_USB_HOST_ID rum_devs[] = {
93 #define RUM_DEV(v,p) { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
94 RUM_DEV(ABOCOM, HWU54DM),
95 RUM_DEV(ABOCOM, RT2573_2),
96 RUM_DEV(ABOCOM, RT2573_3),
97 RUM_DEV(ABOCOM, RT2573_4),
98 RUM_DEV(ABOCOM, WUG2700),
99 RUM_DEV(AMIT, CGWLUSB2GO),
100 RUM_DEV(ASUS, RT2573_1),
101 RUM_DEV(ASUS, RT2573_2),
102 RUM_DEV(BELKIN, F5D7050A),
103 RUM_DEV(BELKIN, F5D9050V3),
104 RUM_DEV(CISCOLINKSYS, WUSB54GC),
105 RUM_DEV(CISCOLINKSYS, WUSB54GR),
106 RUM_DEV(CONCEPTRONIC2, C54RU2),
107 RUM_DEV(COREGA, CGWLUSB2GL),
108 RUM_DEV(COREGA, CGWLUSB2GPX),
109 RUM_DEV(DICKSMITH, CWD854F),
110 RUM_DEV(DICKSMITH, RT2573),
111 RUM_DEV(EDIMAX, EW7318USG),
112 RUM_DEV(DLINK2, DWLG122C1),
113 RUM_DEV(DLINK2, WUA1340),
114 RUM_DEV(DLINK2, DWA111),
115 RUM_DEV(DLINK2, DWA110),
116 RUM_DEV(GIGABYTE, GNWB01GS),
117 RUM_DEV(GIGABYTE, GNWI05GS),
118 RUM_DEV(GIGASET, RT2573),
119 RUM_DEV(GOODWAY, RT2573),
120 RUM_DEV(GUILLEMOT, HWGUSB254LB),
121 RUM_DEV(GUILLEMOT, HWGUSB254V2AP),
122 RUM_DEV(HUAWEI3COM, WUB320G),
123 RUM_DEV(MELCO, G54HP),
124 RUM_DEV(MELCO, SG54HP),
125 RUM_DEV(MELCO, SG54HG),
126 RUM_DEV(MELCO, WLIUCG),
127 RUM_DEV(MELCO, WLRUCG),
128 RUM_DEV(MELCO, WLRUCGAOSS),
129 RUM_DEV(MSI, RT2573_1),
130 RUM_DEV(MSI, RT2573_2),
131 RUM_DEV(MSI, RT2573_3),
132 RUM_DEV(MSI, RT2573_4),
133 RUM_DEV(NOVATECH, RT2573),
134 RUM_DEV(PLANEX2, GWUS54HP),
135 RUM_DEV(PLANEX2, GWUS54MINI2),
136 RUM_DEV(PLANEX2, GWUSMM),
137 RUM_DEV(QCOM, RT2573),
138 RUM_DEV(QCOM, RT2573_2),
139 RUM_DEV(QCOM, RT2573_3),
140 RUM_DEV(RALINK, RT2573),
141 RUM_DEV(RALINK, RT2573_2),
142 RUM_DEV(RALINK, RT2671),
143 RUM_DEV(SITECOMEU, WL113R2),
144 RUM_DEV(SITECOMEU, WL172),
145 RUM_DEV(SPARKLAN, RT2573),
146 RUM_DEV(SURECOM, RT2573),
150 static device_probe_t rum_match;
151 static device_attach_t rum_attach;
152 static device_detach_t rum_detach;
154 static usb_callback_t rum_bulk_read_callback;
155 static usb_callback_t rum_bulk_write_callback;
157 static usb_error_t rum_do_request(struct rum_softc *sc,
158 struct usb_device_request *req, void *data);
159 static usb_error_t rum_do_mcu_request(struct rum_softc *sc, int);
160 static struct ieee80211vap *rum_vap_create(struct ieee80211com *,
161 const char [IFNAMSIZ], int, enum ieee80211_opmode,
162 int, const uint8_t [IEEE80211_ADDR_LEN],
163 const uint8_t [IEEE80211_ADDR_LEN]);
164 static void rum_vap_delete(struct ieee80211vap *);
165 static void rum_cmdq_cb(void *, int);
166 static int rum_cmd_sleepable(struct rum_softc *, const void *,
167 size_t, uint8_t, CMD_FUNC_PROTO);
168 static void rum_tx_free(struct rum_tx_data *, int);
169 static void rum_setup_tx_list(struct rum_softc *);
170 static void rum_reset_tx_list(struct rum_softc *,
171 struct ieee80211vap *);
172 static void rum_unsetup_tx_list(struct rum_softc *);
173 static void rum_beacon_miss(struct ieee80211vap *);
174 static void rum_sta_recv_mgmt(struct ieee80211_node *,
176 const struct ieee80211_rx_stats *, int, int);
177 static int rum_set_power_state(struct rum_softc *, int);
178 static int rum_newstate(struct ieee80211vap *,
179 enum ieee80211_state, int);
180 static uint8_t rum_crypto_mode(struct rum_softc *, u_int, int);
181 static void rum_setup_tx_desc(struct rum_softc *,
182 struct rum_tx_desc *, struct ieee80211_key *,
183 uint32_t, uint8_t, uint8_t, int, int, int);
184 static uint32_t rum_tx_crypto_flags(struct rum_softc *,
185 struct ieee80211_node *,
186 const struct ieee80211_key *);
187 static int rum_tx_mgt(struct rum_softc *, struct mbuf *,
188 struct ieee80211_node *);
189 static int rum_tx_raw(struct rum_softc *, struct mbuf *,
190 struct ieee80211_node *,
191 const struct ieee80211_bpf_params *);
192 static int rum_tx_data(struct rum_softc *, struct mbuf *,
193 struct ieee80211_node *);
194 static int rum_transmit(struct ieee80211com *, struct mbuf *);
195 static void rum_start(struct rum_softc *);
196 static void rum_parent(struct ieee80211com *);
197 static void rum_eeprom_read(struct rum_softc *, uint16_t, void *,
199 static uint32_t rum_read(struct rum_softc *, uint16_t);
200 static void rum_read_multi(struct rum_softc *, uint16_t, void *,
202 static usb_error_t rum_write(struct rum_softc *, uint16_t, uint32_t);
203 static usb_error_t rum_write_multi(struct rum_softc *, uint16_t, void *,
205 static usb_error_t rum_setbits(struct rum_softc *, uint16_t, uint32_t);
206 static usb_error_t rum_clrbits(struct rum_softc *, uint16_t, uint32_t);
207 static usb_error_t rum_modbits(struct rum_softc *, uint16_t, uint32_t,
209 static int rum_bbp_busy(struct rum_softc *);
210 static void rum_bbp_write(struct rum_softc *, uint8_t, uint8_t);
211 static uint8_t rum_bbp_read(struct rum_softc *, uint8_t);
212 static void rum_rf_write(struct rum_softc *, uint8_t, uint32_t);
213 static void rum_select_antenna(struct rum_softc *);
214 static void rum_enable_mrr(struct rum_softc *);
215 static void rum_set_txpreamble(struct rum_softc *);
216 static void rum_set_basicrates(struct rum_softc *);
217 static void rum_select_band(struct rum_softc *,
218 struct ieee80211_channel *);
219 static void rum_set_chan(struct rum_softc *,
220 struct ieee80211_channel *);
221 static void rum_set_maxretry(struct rum_softc *,
222 struct ieee80211vap *);
223 static int rum_enable_tsf_sync(struct rum_softc *);
224 static void rum_enable_tsf(struct rum_softc *);
225 static void rum_abort_tsf_sync(struct rum_softc *);
226 static void rum_get_tsf(struct rum_softc *, uint64_t *);
227 static void rum_update_slot_cb(struct rum_softc *,
228 union sec_param *, uint8_t);
229 static void rum_update_slot(struct ieee80211com *);
230 static int rum_wme_update(struct ieee80211com *);
231 static void rum_set_bssid(struct rum_softc *, const uint8_t *);
232 static void rum_set_macaddr(struct rum_softc *, const uint8_t *);
233 static void rum_update_mcast(struct ieee80211com *);
234 static void rum_update_promisc(struct ieee80211com *);
235 static void rum_setpromisc(struct rum_softc *);
236 static const char *rum_get_rf(int);
237 static void rum_read_eeprom(struct rum_softc *);
238 static int rum_bbp_wakeup(struct rum_softc *);
239 static int rum_bbp_init(struct rum_softc *);
240 static void rum_clr_shkey_regs(struct rum_softc *);
241 static int rum_init(struct rum_softc *);
242 static void rum_stop(struct rum_softc *);
243 static void rum_load_microcode(struct rum_softc *, const uint8_t *,
245 static int rum_set_sleep_time(struct rum_softc *, uint16_t);
246 static int rum_reset(struct ieee80211vap *, u_long);
247 static int rum_set_beacon(struct rum_softc *,
248 struct ieee80211vap *);
249 static int rum_alloc_beacon(struct rum_softc *,
250 struct ieee80211vap *);
251 static void rum_update_beacon_cb(struct rum_softc *,
252 union sec_param *, uint8_t);
253 static void rum_update_beacon(struct ieee80211vap *, int);
254 static int rum_common_key_set(struct rum_softc *,
255 struct ieee80211_key *, uint16_t);
256 static void rum_group_key_set_cb(struct rum_softc *,
257 union sec_param *, uint8_t);
258 static void rum_group_key_del_cb(struct rum_softc *,
259 union sec_param *, uint8_t);
260 static void rum_pair_key_set_cb(struct rum_softc *,
261 union sec_param *, uint8_t);
262 static void rum_pair_key_del_cb(struct rum_softc *,
263 union sec_param *, uint8_t);
264 static int rum_key_alloc(struct ieee80211vap *,
265 struct ieee80211_key *, ieee80211_keyix *,
267 static int rum_key_set(struct ieee80211vap *,
268 const struct ieee80211_key *);
269 static int rum_key_delete(struct ieee80211vap *,
270 const struct ieee80211_key *);
271 static int rum_raw_xmit(struct ieee80211_node *, struct mbuf *,
272 const struct ieee80211_bpf_params *);
273 static void rum_scan_start(struct ieee80211com *);
274 static void rum_scan_end(struct ieee80211com *);
275 static void rum_set_channel(struct ieee80211com *);
276 static void rum_getradiocaps(struct ieee80211com *, int, int *,
277 struct ieee80211_channel[]);
278 static int rum_get_rssi(struct rum_softc *, uint8_t);
279 static void rum_ratectl_start(struct rum_softc *,
280 struct ieee80211_node *);
281 static void rum_ratectl_timeout(void *);
282 static void rum_ratectl_task(void *, int);
283 static int rum_pause(struct rum_softc *, int);
285 static const struct {
289 { RT2573_TXRX_CSR0, 0x025fb032 },
290 { RT2573_TXRX_CSR1, 0x9eaa9eaf },
291 { RT2573_TXRX_CSR2, 0x8a8b8c8d },
292 { RT2573_TXRX_CSR3, 0x00858687 },
293 { RT2573_TXRX_CSR7, 0x2e31353b },
294 { RT2573_TXRX_CSR8, 0x2a2a2a2c },
295 { RT2573_TXRX_CSR15, 0x0000000f },
296 { RT2573_MAC_CSR6, 0x00000fff },
297 { RT2573_MAC_CSR8, 0x016c030a },
298 { RT2573_MAC_CSR10, 0x00000718 },
299 { RT2573_MAC_CSR12, 0x00000004 },
300 { RT2573_MAC_CSR13, 0x00007f00 },
301 { RT2573_SEC_CSR2, 0x00000000 },
302 { RT2573_SEC_CSR3, 0x00000000 },
303 { RT2573_SEC_CSR4, 0x00000000 },
304 { RT2573_PHY_CSR1, 0x000023b0 },
305 { RT2573_PHY_CSR5, 0x00040a06 },
306 { RT2573_PHY_CSR6, 0x00080606 },
307 { RT2573_PHY_CSR7, 0x00000408 },
308 { RT2573_AIFSN_CSR, 0x00002273 },
309 { RT2573_CWMIN_CSR, 0x00002344 },
310 { RT2573_CWMAX_CSR, 0x000034aa }
313 static const struct {
345 static const uint8_t rum_chan_2ghz[] =
346 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };
348 static const uint8_t rum_chan_5ghz[] =
349 { 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64,
350 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140,
351 149, 153, 157, 161, 165 };
353 static const struct rfprog {
355 uint32_t r1, r2, r3, r4;
357 { 1, 0x00b03, 0x001e1, 0x1a014, 0x30282 },
358 { 2, 0x00b03, 0x001e1, 0x1a014, 0x30287 },
359 { 3, 0x00b03, 0x001e2, 0x1a014, 0x30282 },
360 { 4, 0x00b03, 0x001e2, 0x1a014, 0x30287 },
361 { 5, 0x00b03, 0x001e3, 0x1a014, 0x30282 },
362 { 6, 0x00b03, 0x001e3, 0x1a014, 0x30287 },
363 { 7, 0x00b03, 0x001e4, 0x1a014, 0x30282 },
364 { 8, 0x00b03, 0x001e4, 0x1a014, 0x30287 },
365 { 9, 0x00b03, 0x001e5, 0x1a014, 0x30282 },
366 { 10, 0x00b03, 0x001e5, 0x1a014, 0x30287 },
367 { 11, 0x00b03, 0x001e6, 0x1a014, 0x30282 },
368 { 12, 0x00b03, 0x001e6, 0x1a014, 0x30287 },
369 { 13, 0x00b03, 0x001e7, 0x1a014, 0x30282 },
370 { 14, 0x00b03, 0x001e8, 0x1a014, 0x30284 },
372 { 34, 0x00b03, 0x20266, 0x36014, 0x30282 },
373 { 38, 0x00b03, 0x20267, 0x36014, 0x30284 },
374 { 42, 0x00b03, 0x20268, 0x36014, 0x30286 },
375 { 46, 0x00b03, 0x20269, 0x36014, 0x30288 },
377 { 36, 0x00b03, 0x00266, 0x26014, 0x30288 },
378 { 40, 0x00b03, 0x00268, 0x26014, 0x30280 },
379 { 44, 0x00b03, 0x00269, 0x26014, 0x30282 },
380 { 48, 0x00b03, 0x0026a, 0x26014, 0x30284 },
381 { 52, 0x00b03, 0x0026b, 0x26014, 0x30286 },
382 { 56, 0x00b03, 0x0026c, 0x26014, 0x30288 },
383 { 60, 0x00b03, 0x0026e, 0x26014, 0x30280 },
384 { 64, 0x00b03, 0x0026f, 0x26014, 0x30282 },
386 { 100, 0x00b03, 0x0028a, 0x2e014, 0x30280 },
387 { 104, 0x00b03, 0x0028b, 0x2e014, 0x30282 },
388 { 108, 0x00b03, 0x0028c, 0x2e014, 0x30284 },
389 { 112, 0x00b03, 0x0028d, 0x2e014, 0x30286 },
390 { 116, 0x00b03, 0x0028e, 0x2e014, 0x30288 },
391 { 120, 0x00b03, 0x002a0, 0x2e014, 0x30280 },
392 { 124, 0x00b03, 0x002a1, 0x2e014, 0x30282 },
393 { 128, 0x00b03, 0x002a2, 0x2e014, 0x30284 },
394 { 132, 0x00b03, 0x002a3, 0x2e014, 0x30286 },
395 { 136, 0x00b03, 0x002a4, 0x2e014, 0x30288 },
396 { 140, 0x00b03, 0x002a6, 0x2e014, 0x30280 },
398 { 149, 0x00b03, 0x002a8, 0x2e014, 0x30287 },
399 { 153, 0x00b03, 0x002a9, 0x2e014, 0x30289 },
400 { 157, 0x00b03, 0x002ab, 0x2e014, 0x30281 },
401 { 161, 0x00b03, 0x002ac, 0x2e014, 0x30283 },
402 { 165, 0x00b03, 0x002ad, 0x2e014, 0x30285 }
404 { 1, 0x00b33, 0x011e1, 0x1a014, 0x30282 },
405 { 2, 0x00b33, 0x011e1, 0x1a014, 0x30287 },
406 { 3, 0x00b33, 0x011e2, 0x1a014, 0x30282 },
407 { 4, 0x00b33, 0x011e2, 0x1a014, 0x30287 },
408 { 5, 0x00b33, 0x011e3, 0x1a014, 0x30282 },
409 { 6, 0x00b33, 0x011e3, 0x1a014, 0x30287 },
410 { 7, 0x00b33, 0x011e4, 0x1a014, 0x30282 },
411 { 8, 0x00b33, 0x011e4, 0x1a014, 0x30287 },
412 { 9, 0x00b33, 0x011e5, 0x1a014, 0x30282 },
413 { 10, 0x00b33, 0x011e5, 0x1a014, 0x30287 },
414 { 11, 0x00b33, 0x011e6, 0x1a014, 0x30282 },
415 { 12, 0x00b33, 0x011e6, 0x1a014, 0x30287 },
416 { 13, 0x00b33, 0x011e7, 0x1a014, 0x30282 },
417 { 14, 0x00b33, 0x011e8, 0x1a014, 0x30284 },
419 { 34, 0x00b33, 0x01266, 0x26014, 0x30282 },
420 { 38, 0x00b33, 0x01267, 0x26014, 0x30284 },
421 { 42, 0x00b33, 0x01268, 0x26014, 0x30286 },
422 { 46, 0x00b33, 0x01269, 0x26014, 0x30288 },
424 { 36, 0x00b33, 0x01266, 0x26014, 0x30288 },
425 { 40, 0x00b33, 0x01268, 0x26014, 0x30280 },
426 { 44, 0x00b33, 0x01269, 0x26014, 0x30282 },
427 { 48, 0x00b33, 0x0126a, 0x26014, 0x30284 },
428 { 52, 0x00b33, 0x0126b, 0x26014, 0x30286 },
429 { 56, 0x00b33, 0x0126c, 0x26014, 0x30288 },
430 { 60, 0x00b33, 0x0126e, 0x26014, 0x30280 },
431 { 64, 0x00b33, 0x0126f, 0x26014, 0x30282 },
433 { 100, 0x00b33, 0x0128a, 0x2e014, 0x30280 },
434 { 104, 0x00b33, 0x0128b, 0x2e014, 0x30282 },
435 { 108, 0x00b33, 0x0128c, 0x2e014, 0x30284 },
436 { 112, 0x00b33, 0x0128d, 0x2e014, 0x30286 },
437 { 116, 0x00b33, 0x0128e, 0x2e014, 0x30288 },
438 { 120, 0x00b33, 0x012a0, 0x2e014, 0x30280 },
439 { 124, 0x00b33, 0x012a1, 0x2e014, 0x30282 },
440 { 128, 0x00b33, 0x012a2, 0x2e014, 0x30284 },
441 { 132, 0x00b33, 0x012a3, 0x2e014, 0x30286 },
442 { 136, 0x00b33, 0x012a4, 0x2e014, 0x30288 },
443 { 140, 0x00b33, 0x012a6, 0x2e014, 0x30280 },
445 { 149, 0x00b33, 0x012a8, 0x2e014, 0x30287 },
446 { 153, 0x00b33, 0x012a9, 0x2e014, 0x30289 },
447 { 157, 0x00b33, 0x012ab, 0x2e014, 0x30281 },
448 { 161, 0x00b33, 0x012ac, 0x2e014, 0x30283 },
449 { 165, 0x00b33, 0x012ad, 0x2e014, 0x30285 }
452 static const struct usb_config rum_config[RUM_N_TRANSFER] = {
455 .endpoint = UE_ADDR_ANY,
456 .direction = UE_DIR_OUT,
457 .bufsize = (MCLBYTES + RT2573_TX_DESC_SIZE + 8),
458 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
459 .callback = rum_bulk_write_callback,
460 .timeout = 5000, /* ms */
464 .endpoint = UE_ADDR_ANY,
465 .direction = UE_DIR_IN,
466 .bufsize = (MCLBYTES + RT2573_RX_DESC_SIZE),
467 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
468 .callback = rum_bulk_read_callback,
473 rum_match(device_t self)
475 struct usb_attach_arg *uaa = device_get_ivars(self);
477 if (uaa->usb_mode != USB_MODE_HOST)
479 if (uaa->info.bConfigIndex != 0)
481 if (uaa->info.bIfaceIndex != RT2573_IFACE_INDEX)
484 return (usbd_lookup_id_by_uaa(rum_devs, sizeof(rum_devs), uaa));
488 rum_attach(device_t self)
490 struct usb_attach_arg *uaa = device_get_ivars(self);
491 struct rum_softc *sc = device_get_softc(self);
492 struct ieee80211com *ic = &sc->sc_ic;
497 device_set_usb_desc(self);
498 sc->sc_udev = uaa->device;
502 RUM_CMDQ_LOCK_INIT(sc);
503 mbufq_init(&sc->sc_snd, ifqmaxlen);
505 iface_index = RT2573_IFACE_INDEX;
506 error = usbd_transfer_setup(uaa->device, &iface_index,
507 sc->sc_xfer, rum_config, RUM_N_TRANSFER, sc, &sc->sc_mtx);
509 device_printf(self, "could not allocate USB transfers, "
510 "err=%s\n", usbd_errstr(error));
515 /* retrieve RT2573 rev. no */
516 for (ntries = 0; ntries < 100; ntries++) {
517 if ((tmp = rum_read(sc, RT2573_MAC_CSR0)) != 0)
519 if (rum_pause(sc, hz / 100))
523 device_printf(sc->sc_dev, "timeout waiting for chip to settle\n");
528 /* retrieve MAC address and various other things from EEPROM */
531 device_printf(sc->sc_dev, "MAC/BBP RT2573 (rev 0x%05x), RF %s\n",
532 tmp, rum_get_rf(sc->rf_rev));
534 rum_load_microcode(sc, rt2573_ucode, sizeof(rt2573_ucode));
538 ic->ic_name = device_get_nameunit(self);
539 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
541 /* set device capabilities */
543 IEEE80211_C_STA /* station mode supported */
544 | IEEE80211_C_IBSS /* IBSS mode supported */
545 | IEEE80211_C_MONITOR /* monitor mode supported */
546 | IEEE80211_C_HOSTAP /* HostAp mode supported */
547 | IEEE80211_C_AHDEMO /* adhoc demo mode */
548 | IEEE80211_C_TXPMGT /* tx power management */
549 | IEEE80211_C_SHPREAMBLE /* short preamble supported */
550 | IEEE80211_C_SHSLOT /* short slot time supported */
551 | IEEE80211_C_BGSCAN /* bg scanning supported */
552 | IEEE80211_C_WPA /* 802.11i */
553 | IEEE80211_C_WME /* 802.11e */
554 | IEEE80211_C_PMGT /* Station-side power mgmt */
555 | IEEE80211_C_SWSLEEP /* net80211 managed power mgmt */
559 IEEE80211_CRYPTO_WEP |
560 IEEE80211_CRYPTO_AES_CCM |
561 IEEE80211_CRYPTO_TKIPMIC |
562 IEEE80211_CRYPTO_TKIP;
564 rum_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
567 ieee80211_ifattach(ic);
568 ic->ic_update_promisc = rum_update_promisc;
569 ic->ic_raw_xmit = rum_raw_xmit;
570 ic->ic_scan_start = rum_scan_start;
571 ic->ic_scan_end = rum_scan_end;
572 ic->ic_set_channel = rum_set_channel;
573 ic->ic_getradiocaps = rum_getradiocaps;
574 ic->ic_transmit = rum_transmit;
575 ic->ic_parent = rum_parent;
576 ic->ic_vap_create = rum_vap_create;
577 ic->ic_vap_delete = rum_vap_delete;
578 ic->ic_updateslot = rum_update_slot;
579 ic->ic_wme.wme_update = rum_wme_update;
580 ic->ic_update_mcast = rum_update_mcast;
582 ieee80211_radiotap_attach(ic,
583 &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
584 RT2573_TX_RADIOTAP_PRESENT,
585 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
586 RT2573_RX_RADIOTAP_PRESENT);
588 TASK_INIT(&sc->cmdq_task, 0, rum_cmdq_cb, sc);
591 ieee80211_announce(ic);
597 return (ENXIO); /* failure */
601 rum_detach(device_t self)
603 struct rum_softc *sc = device_get_softc(self);
604 struct ieee80211com *ic = &sc->sc_ic;
606 /* Prevent further ioctls */
611 /* stop all USB transfers */
612 usbd_transfer_unsetup(sc->sc_xfer, RUM_N_TRANSFER);
614 /* free TX list, if any */
616 rum_unsetup_tx_list(sc);
619 if (ic->ic_softc == sc) {
620 ieee80211_draintask(ic, &sc->cmdq_task);
621 ieee80211_ifdetach(ic);
624 mbufq_drain(&sc->sc_snd);
625 RUM_CMDQ_LOCK_DESTROY(sc);
626 RUM_LOCK_DESTROY(sc);
632 rum_do_request(struct rum_softc *sc,
633 struct usb_device_request *req, void *data)
639 err = usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
640 req, data, 0, NULL, 250 /* ms */);
644 DPRINTFN(1, "Control request failed, %s (retrying)\n",
646 if (rum_pause(sc, hz / 100))
653 rum_do_mcu_request(struct rum_softc *sc, int request)
655 struct usb_device_request req;
657 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
658 req.bRequest = RT2573_MCU_CNTL;
659 USETW(req.wValue, request);
660 USETW(req.wIndex, 0);
661 USETW(req.wLength, 0);
663 return (rum_do_request(sc, &req, NULL));
666 static struct ieee80211vap *
667 rum_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
668 enum ieee80211_opmode opmode, int flags,
669 const uint8_t bssid[IEEE80211_ADDR_LEN],
670 const uint8_t mac[IEEE80211_ADDR_LEN])
672 struct rum_softc *sc = ic->ic_softc;
674 struct ieee80211vap *vap;
676 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */
678 rvp = malloc(sizeof(struct rum_vap), M_80211_VAP, M_WAITOK | M_ZERO);
680 /* enable s/w bmiss handling for sta mode */
682 if (ieee80211_vap_setup(ic, vap, name, unit, opmode,
683 flags | IEEE80211_CLONE_NOBEACONS, bssid) != 0) {
685 free(rvp, M_80211_VAP);
689 /* override state transition machine */
690 rvp->newstate = vap->iv_newstate;
691 vap->iv_newstate = rum_newstate;
692 vap->iv_key_alloc = rum_key_alloc;
693 vap->iv_key_set = rum_key_set;
694 vap->iv_key_delete = rum_key_delete;
695 vap->iv_update_beacon = rum_update_beacon;
696 vap->iv_reset = rum_reset;
697 vap->iv_max_aid = RT2573_ADDR_MAX;
699 if (opmode == IEEE80211_M_STA) {
701 * Move device to the sleep state when
702 * beacon is received and there is no data for us.
704 * Used only for IEEE80211_S_SLEEP state.
706 rvp->recv_mgmt = vap->iv_recv_mgmt;
707 vap->iv_recv_mgmt = rum_sta_recv_mgmt;
709 /* Ignored while sleeping. */
710 rvp->bmiss = vap->iv_bmiss;
711 vap->iv_bmiss = rum_beacon_miss;
714 usb_callout_init_mtx(&rvp->ratectl_ch, &sc->sc_mtx, 0);
715 TASK_INIT(&rvp->ratectl_task, 0, rum_ratectl_task, rvp);
716 ieee80211_ratectl_init(vap);
717 ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */);
719 ieee80211_vap_attach(vap, ieee80211_media_change,
720 ieee80211_media_status, mac);
721 ic->ic_opmode = opmode;
726 rum_vap_delete(struct ieee80211vap *vap)
728 struct rum_vap *rvp = RUM_VAP(vap);
729 struct ieee80211com *ic = vap->iv_ic;
730 struct rum_softc *sc = ic->ic_softc;
732 /* Put vap into INIT state. */
733 ieee80211_new_state(vap, IEEE80211_S_INIT, -1);
734 ieee80211_draintask(ic, &vap->iv_nstate_task);
737 /* Cancel any unfinished Tx. */
738 rum_reset_tx_list(sc, vap);
741 usb_callout_drain(&rvp->ratectl_ch);
742 ieee80211_draintask(ic, &rvp->ratectl_task);
743 ieee80211_ratectl_deinit(vap);
744 ieee80211_vap_detach(vap);
745 m_freem(rvp->bcn_mbuf);
746 free(rvp, M_80211_VAP);
750 rum_cmdq_cb(void *arg, int pending)
752 struct rum_softc *sc = arg;
756 while (sc->cmdq[sc->cmdq_first].func != NULL) {
757 rc = &sc->cmdq[sc->cmdq_first];
761 rc->func(sc, &rc->data, rc->rvp_id);
765 memset(rc, 0, sizeof (*rc));
766 sc->cmdq_first = (sc->cmdq_first + 1) % RUM_CMDQ_SIZE;
772 rum_cmd_sleepable(struct rum_softc *sc, const void *ptr, size_t len,
773 uint8_t rvp_id, CMD_FUNC_PROTO)
775 struct ieee80211com *ic = &sc->sc_ic;
777 KASSERT(len <= sizeof(union sec_param), ("buffer overflow"));
780 if (sc->cmdq[sc->cmdq_last].func != NULL) {
781 device_printf(sc->sc_dev, "%s: cmdq overflow\n", __func__);
788 memcpy(&sc->cmdq[sc->cmdq_last].data, ptr, len);
789 sc->cmdq[sc->cmdq_last].rvp_id = rvp_id;
790 sc->cmdq[sc->cmdq_last].func = func;
791 sc->cmdq_last = (sc->cmdq_last + 1) % RUM_CMDQ_SIZE;
794 ieee80211_runtask(ic, &sc->cmdq_task);
800 rum_tx_free(struct rum_tx_data *data, int txerr)
802 struct rum_softc *sc = data->sc;
804 if (data->m != NULL) {
805 ieee80211_tx_complete(data->ni, data->m, txerr);
809 STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
814 rum_setup_tx_list(struct rum_softc *sc)
816 struct rum_tx_data *data;
820 STAILQ_INIT(&sc->tx_q);
821 STAILQ_INIT(&sc->tx_free);
823 for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
824 data = &sc->tx_data[i];
827 STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
833 rum_reset_tx_list(struct rum_softc *sc, struct ieee80211vap *vap)
835 struct rum_tx_data *data, *tmp;
837 KASSERT(vap != NULL, ("%s: vap is NULL\n", __func__));
839 STAILQ_FOREACH_SAFE(data, &sc->tx_q, next, tmp) {
840 if (data->ni != NULL && data->ni->ni_vap == vap) {
841 ieee80211_free_node(data->ni);
844 KASSERT(data->m != NULL, ("%s: m is NULL\n",
849 STAILQ_REMOVE(&sc->tx_q, data, rum_tx_data, next);
850 STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
857 rum_unsetup_tx_list(struct rum_softc *sc)
859 struct rum_tx_data *data;
862 /* make sure any subsequent use of the queues will fail */
864 STAILQ_INIT(&sc->tx_q);
865 STAILQ_INIT(&sc->tx_free);
867 /* free up all node references and mbufs */
868 for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
869 data = &sc->tx_data[i];
871 if (data->m != NULL) {
875 if (data->ni != NULL) {
876 ieee80211_free_node(data->ni);
883 rum_beacon_miss(struct ieee80211vap *vap)
885 struct ieee80211com *ic = vap->iv_ic;
886 struct rum_softc *sc = ic->ic_softc;
887 struct rum_vap *rvp = RUM_VAP(vap);
891 if (sc->sc_sleeping && sc->sc_sleep_end < ticks) {
892 DPRINTFN(12, "dropping 'sleeping' bit, "
893 "device must be awake now\n");
898 sleep = sc->sc_sleeping;
905 DPRINTFN(13, "bmiss event is ignored whilst sleeping\n");
910 rum_sta_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m, int subtype,
911 const struct ieee80211_rx_stats *rxs,
914 struct ieee80211vap *vap = ni->ni_vap;
915 struct rum_softc *sc = vap->iv_ic->ic_softc;
916 struct rum_vap *rvp = RUM_VAP(vap);
918 if (vap->iv_state == IEEE80211_S_SLEEP &&
919 subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
921 DPRINTFN(12, "beacon, mybss %d (flags %02X)\n",
922 !!(sc->last_rx_flags & RT2573_RX_MYBSS),
925 if ((sc->last_rx_flags & (RT2573_RX_MYBSS | RT2573_RX_BC)) ==
926 (RT2573_RX_MYBSS | RT2573_RX_BC)) {
928 * Put it to sleep here; in case if there is a data
929 * for us, iv_recv_mgmt() will wakeup the device via
930 * SLEEP -> RUN state transition.
932 rum_set_power_state(sc, 1);
937 rvp->recv_mgmt(ni, m, subtype, rxs, rssi, nf);
941 rum_set_power_state(struct rum_softc *sc, int sleep)
947 DPRINTFN(12, "moving to %s state (sleep time %u)\n",
948 sleep ? "sleep" : "awake", sc->sc_sleep_time);
950 uerror = rum_do_mcu_request(sc,
951 sleep ? RT2573_MCU_SLEEP : RT2573_MCU_WAKEUP);
952 if (uerror != USB_ERR_NORMAL_COMPLETION) {
953 device_printf(sc->sc_dev,
954 "%s: could not change power state: %s\n",
955 __func__, usbd_errstr(uerror));
959 sc->sc_sleeping = !!sleep;
960 sc->sc_sleep_end = sleep ? ticks + sc->sc_sleep_time : 0;
966 rum_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
968 struct rum_vap *rvp = RUM_VAP(vap);
969 struct ieee80211com *ic = vap->iv_ic;
970 struct rum_softc *sc = ic->ic_softc;
971 const struct ieee80211_txparam *tp;
972 enum ieee80211_state ostate;
973 struct ieee80211_node *ni;
977 ostate = vap->iv_state;
978 DPRINTF("%s -> %s\n",
979 ieee80211_state_name[ostate],
980 ieee80211_state_name[nstate]);
982 IEEE80211_UNLOCK(ic);
984 usb_callout_stop(&rvp->ratectl_ch);
986 if (ostate == IEEE80211_S_SLEEP && vap->iv_opmode == IEEE80211_M_STA) {
987 rum_clrbits(sc, RT2573_TXRX_CSR4, RT2573_ACKCTS_PWRMGT);
988 rum_clrbits(sc, RT2573_MAC_CSR11, RT2573_AUTO_WAKEUP);
992 * any subsequent TX will wakeup it anyway
994 (void) rum_set_power_state(sc, 0);
998 case IEEE80211_S_INIT:
999 if (ostate == IEEE80211_S_RUN)
1000 rum_abort_tsf_sync(sc);
1004 case IEEE80211_S_RUN:
1005 if (ostate == IEEE80211_S_SLEEP)
1006 break; /* already handled */
1008 ni = ieee80211_ref_node(vap->iv_bss);
1010 if (vap->iv_opmode != IEEE80211_M_MONITOR) {
1011 if (ic->ic_bsschan == IEEE80211_CHAN_ANYC ||
1012 ni->ni_chan == IEEE80211_CHAN_ANYC) {
1016 rum_update_slot_cb(sc, NULL, 0);
1018 rum_set_txpreamble(sc);
1019 rum_set_basicrates(sc);
1020 rum_set_maxretry(sc, vap);
1021 IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid);
1022 rum_set_bssid(sc, sc->sc_bssid);
1025 if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
1026 vap->iv_opmode == IEEE80211_M_IBSS) {
1027 if ((ret = rum_alloc_beacon(sc, vap)) != 0)
1031 if (vap->iv_opmode != IEEE80211_M_MONITOR &&
1032 vap->iv_opmode != IEEE80211_M_AHDEMO) {
1033 if ((ret = rum_enable_tsf_sync(sc)) != 0)
1038 /* enable automatic rate adaptation */
1039 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1040 if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
1041 rum_ratectl_start(sc, ni);
1043 ieee80211_free_node(ni);
1045 case IEEE80211_S_SLEEP:
1046 /* Implemented for STA mode only. */
1047 if (vap->iv_opmode != IEEE80211_M_STA)
1050 uerror = rum_setbits(sc, RT2573_MAC_CSR11, RT2573_AUTO_WAKEUP);
1051 if (uerror != USB_ERR_NORMAL_COMPLETION) {
1056 uerror = rum_setbits(sc, RT2573_TXRX_CSR4, RT2573_ACKCTS_PWRMGT);
1057 if (uerror != USB_ERR_NORMAL_COMPLETION) {
1062 ret = rum_set_power_state(sc, 1);
1064 device_printf(sc->sc_dev,
1065 "%s: could not move to the SLEEP state: %s\n",
1066 __func__, usbd_errstr(uerror));
1074 return (ret == 0 ? rvp->newstate(vap, nstate, arg) : ret);
1078 rum_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
1080 struct rum_softc *sc = usbd_xfer_softc(xfer);
1081 struct ieee80211vap *vap;
1082 struct rum_tx_data *data;
1084 struct usb_page_cache *pc;
1088 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
1090 switch (USB_GET_STATE(xfer)) {
1091 case USB_ST_TRANSFERRED:
1092 DPRINTFN(11, "transfer complete, %d bytes\n", actlen);
1094 /* free resources */
1095 data = usbd_xfer_get_priv(xfer);
1096 rum_tx_free(data, 0);
1097 usbd_xfer_set_priv(xfer, NULL);
1102 data = STAILQ_FIRST(&sc->tx_q);
1104 STAILQ_REMOVE_HEAD(&sc->tx_q, next);
1107 if (m->m_pkthdr.len > (int)(MCLBYTES + RT2573_TX_DESC_SIZE)) {
1108 DPRINTFN(0, "data overflow, %u bytes\n",
1110 m->m_pkthdr.len = (MCLBYTES + RT2573_TX_DESC_SIZE);
1112 pc = usbd_xfer_get_frame(xfer, 0);
1113 usbd_copy_in(pc, 0, &data->desc, RT2573_TX_DESC_SIZE);
1114 usbd_m_copy_in(pc, RT2573_TX_DESC_SIZE, m, 0,
1117 vap = data->ni->ni_vap;
1118 if (ieee80211_radiotap_active_vap(vap)) {
1119 struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
1122 tap->wt_rate = data->rate;
1123 tap->wt_antenna = sc->tx_ant;
1125 ieee80211_radiotap_tx(vap, m);
1128 /* align end on a 4-bytes boundary */
1129 len = (RT2573_TX_DESC_SIZE + m->m_pkthdr.len + 3) & ~3;
1130 if ((len % 64) == 0)
1133 DPRINTFN(11, "sending frame len=%u xferlen=%u\n",
1134 m->m_pkthdr.len, len);
1136 usbd_xfer_set_frame_len(xfer, 0, len);
1137 usbd_xfer_set_priv(xfer, data);
1139 usbd_transfer_submit(xfer);
1144 default: /* Error */
1145 DPRINTFN(11, "transfer error, %s\n",
1146 usbd_errstr(error));
1148 counter_u64_add(sc->sc_ic.ic_oerrors, 1);
1149 data = usbd_xfer_get_priv(xfer);
1151 rum_tx_free(data, error);
1152 usbd_xfer_set_priv(xfer, NULL);
1155 if (error != USB_ERR_CANCELLED) {
1156 if (error == USB_ERR_TIMEOUT)
1157 device_printf(sc->sc_dev, "device timeout\n");
1160 * Try to clear stall first, also if other
1161 * errors occur, hence clearing stall
1162 * introduces a 50 ms delay:
1164 usbd_xfer_set_stall(xfer);
1172 rum_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
1174 struct rum_softc *sc = usbd_xfer_softc(xfer);
1175 struct ieee80211com *ic = &sc->sc_ic;
1176 struct ieee80211_frame_min *wh;
1177 struct ieee80211_node *ni;
1178 struct mbuf *m = NULL;
1179 struct usb_page_cache *pc;
1184 usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
1186 switch (USB_GET_STATE(xfer)) {
1187 case USB_ST_TRANSFERRED:
1189 DPRINTFN(15, "rx done, actlen=%d\n", len);
1191 if (len < RT2573_RX_DESC_SIZE) {
1192 DPRINTF("%s: xfer too short %d\n",
1193 device_get_nameunit(sc->sc_dev), len);
1194 counter_u64_add(ic->ic_ierrors, 1);
1198 len -= RT2573_RX_DESC_SIZE;
1199 pc = usbd_xfer_get_frame(xfer, 0);
1200 usbd_copy_out(pc, 0, &sc->sc_rx_desc, RT2573_RX_DESC_SIZE);
1202 rssi = rum_get_rssi(sc, sc->sc_rx_desc.rssi);
1203 flags = le32toh(sc->sc_rx_desc.flags);
1204 sc->last_rx_flags = flags;
1205 if (len < ((flags >> 16) & 0xfff)) {
1206 DPRINTFN(5, "%s: frame is truncated from %d to %d "
1207 "bytes\n", device_get_nameunit(sc->sc_dev),
1208 (flags >> 16) & 0xfff, len);
1209 counter_u64_add(ic->ic_ierrors, 1);
1212 len = (flags >> 16) & 0xfff;
1213 if (len < sizeof(struct ieee80211_frame_ack)) {
1214 DPRINTFN(5, "%s: frame too short %d\n",
1215 device_get_nameunit(sc->sc_dev), len);
1216 counter_u64_add(ic->ic_ierrors, 1);
1219 if (flags & RT2573_RX_CRC_ERROR) {
1221 * This should not happen since we did not
1222 * request to receive those frames when we
1223 * filled RUM_TXRX_CSR2:
1225 DPRINTFN(5, "PHY or CRC error\n");
1226 counter_u64_add(ic->ic_ierrors, 1);
1229 if ((flags & RT2573_RX_DEC_MASK) != RT2573_RX_DEC_OK) {
1230 switch (flags & RT2573_RX_DEC_MASK) {
1231 case RT2573_RX_IV_ERROR:
1232 DPRINTFN(5, "IV/EIV error\n");
1234 case RT2573_RX_MIC_ERROR:
1235 DPRINTFN(5, "MIC error\n");
1237 case RT2573_RX_KEY_ERROR:
1238 DPRINTFN(5, "Key error\n");
1241 counter_u64_add(ic->ic_ierrors, 1);
1245 m = m_get2(len, M_NOWAIT, MT_DATA, M_PKTHDR);
1247 DPRINTF("could not allocate mbuf\n");
1248 counter_u64_add(ic->ic_ierrors, 1);
1251 usbd_copy_out(pc, RT2573_RX_DESC_SIZE,
1252 mtod(m, uint8_t *), len);
1254 wh = mtod(m, struct ieee80211_frame_min *);
1256 if ((wh->i_fc[1] & IEEE80211_FC1_PROTECTED) &&
1257 (flags & RT2573_RX_CIP_MASK) !=
1258 RT2573_RX_CIP_MODE(RT2573_MODE_NOSEC)) {
1259 wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
1260 m->m_flags |= M_WEP;
1264 m->m_pkthdr.len = m->m_len = len;
1266 if (ieee80211_radiotap_active(ic)) {
1267 struct rum_rx_radiotap_header *tap = &sc->sc_rxtap;
1270 tap->wr_rate = ieee80211_plcp2rate(sc->sc_rx_desc.rate,
1271 (flags & RT2573_RX_OFDM) ?
1272 IEEE80211_T_OFDM : IEEE80211_T_CCK);
1273 rum_get_tsf(sc, &tap->wr_tsf);
1274 tap->wr_antsignal = RT2573_NOISE_FLOOR + rssi;
1275 tap->wr_antnoise = RT2573_NOISE_FLOOR;
1276 tap->wr_antenna = sc->rx_ant;
1281 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1282 usbd_transfer_submit(xfer);
1285 * At the end of a USB callback it is always safe to unlock
1286 * the private mutex of a device! That is why we do the
1287 * "ieee80211_input" here, and not some lines up!
1291 if (m->m_len >= sizeof(struct ieee80211_frame_min))
1292 ni = ieee80211_find_rxnode(ic, wh);
1297 (void) ieee80211_input(ni, m, rssi,
1298 RT2573_NOISE_FLOOR);
1299 ieee80211_free_node(ni);
1301 (void) ieee80211_input_all(ic, m, rssi,
1302 RT2573_NOISE_FLOOR);
1308 default: /* Error */
1309 if (error != USB_ERR_CANCELLED) {
1310 /* try to clear stall first */
1311 usbd_xfer_set_stall(xfer);
1319 rum_plcp_signal(int rate)
1322 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1323 case 12: return 0xb;
1324 case 18: return 0xf;
1325 case 24: return 0xa;
1326 case 36: return 0xe;
1327 case 48: return 0x9;
1328 case 72: return 0xd;
1329 case 96: return 0x8;
1330 case 108: return 0xc;
1332 /* CCK rates (NB: not IEEE std, device-specific) */
1335 case 11: return 0x2;
1336 case 22: return 0x3;
1338 return 0xff; /* XXX unsupported/unknown rate */
1342 * Map net80211 cipher to RT2573 security mode.
1345 rum_crypto_mode(struct rum_softc *sc, u_int cipher, int keylen)
1348 case IEEE80211_CIPHER_WEP:
1349 return (keylen < 8 ? RT2573_MODE_WEP40 : RT2573_MODE_WEP104);
1350 case IEEE80211_CIPHER_TKIP:
1351 return RT2573_MODE_TKIP;
1352 case IEEE80211_CIPHER_AES_CCM:
1353 return RT2573_MODE_AES_CCMP;
1355 device_printf(sc->sc_dev, "unknown cipher %d\n", cipher);
1361 rum_setup_tx_desc(struct rum_softc *sc, struct rum_tx_desc *desc,
1362 struct ieee80211_key *k, uint32_t flags, uint8_t xflags, uint8_t qid,
1363 int hdrlen, int len, int rate)
1365 struct ieee80211com *ic = &sc->sc_ic;
1366 struct wmeParams *wmep = &sc->wme_params[qid];
1367 uint16_t plcp_length;
1370 flags |= RT2573_TX_VALID;
1373 if (k != NULL && !(k->wk_flags & IEEE80211_KEY_SWCRYPT)) {
1374 const struct ieee80211_cipher *cip = k->wk_cipher;
1376 len += cip->ic_header + cip->ic_trailer + cip->ic_miclen;
1378 desc->eiv = 0; /* for WEP */
1379 cip->ic_setiv(k, (uint8_t *)&desc->iv);
1382 /* setup PLCP fields */
1383 desc->plcp_signal = rum_plcp_signal(rate);
1384 desc->plcp_service = 4;
1386 len += IEEE80211_CRC_LEN;
1387 if (ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) {
1388 flags |= RT2573_TX_OFDM;
1390 plcp_length = len & 0xfff;
1391 desc->plcp_length_hi = plcp_length >> 6;
1392 desc->plcp_length_lo = plcp_length & 0x3f;
1395 rate = 2; /* avoid division by zero */
1396 plcp_length = howmany(16 * len, rate);
1398 remainder = (16 * len) % 22;
1399 if (remainder != 0 && remainder < 7)
1400 desc->plcp_service |= RT2573_PLCP_LENGEXT;
1402 desc->plcp_length_hi = plcp_length >> 8;
1403 desc->plcp_length_lo = plcp_length & 0xff;
1405 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1406 desc->plcp_signal |= 0x08;
1409 desc->flags = htole32(flags);
1410 desc->hdrlen = hdrlen;
1411 desc->xflags = xflags;
1413 desc->wme = htole16(RT2573_QID(qid) |
1414 RT2573_AIFSN(wmep->wmep_aifsn) |
1415 RT2573_LOGCWMIN(wmep->wmep_logcwmin) |
1416 RT2573_LOGCWMAX(wmep->wmep_logcwmax));
1420 rum_sendprot(struct rum_softc *sc,
1421 const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate)
1423 struct ieee80211com *ic = ni->ni_ic;
1424 struct rum_tx_data *data;
1426 int protrate, flags;
1428 RUM_LOCK_ASSERT(sc);
1430 mprot = ieee80211_alloc_prot(ni, m, rate, prot);
1431 if (mprot == NULL) {
1432 if_inc_counter(ni->ni_vap->iv_ifp, IFCOUNTER_OERRORS, 1);
1433 device_printf(sc->sc_dev,
1434 "could not allocate mbuf for protection mode %d\n", prot);
1438 protrate = ieee80211_ctl_rate(ic->ic_rt, rate);
1440 if (prot == IEEE80211_PROT_RTSCTS)
1441 flags |= RT2573_TX_NEED_ACK;
1443 data = STAILQ_FIRST(&sc->tx_free);
1444 STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1448 data->ni = ieee80211_ref_node(ni);
1449 data->rate = protrate;
1450 rum_setup_tx_desc(sc, &data->desc, NULL, flags, 0, 0, 0,
1451 mprot->m_pkthdr.len, protrate);
1453 STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1454 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1460 rum_tx_crypto_flags(struct rum_softc *sc, struct ieee80211_node *ni,
1461 const struct ieee80211_key *k)
1463 struct ieee80211vap *vap = ni->ni_vap;
1468 if (!(k->wk_flags & IEEE80211_KEY_SWCRYPT)) {
1469 cipher = k->wk_cipher->ic_cipher;
1471 mode = rum_crypto_mode(sc, cipher, k->wk_keylen);
1475 flags |= RT2573_TX_CIP_MODE(mode);
1477 /* Do not trust GROUP flag */
1478 if (!(k >= &vap->iv_nw_keys[0] &&
1479 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID]))
1480 flags |= RT2573_TX_KEY_PAIR;
1482 pos += 0 * RT2573_SKEY_MAX; /* vap id */
1484 flags |= RT2573_TX_KEY_ID(pos);
1486 if (cipher == IEEE80211_CIPHER_TKIP)
1487 flags |= RT2573_TX_TKIPMIC;
1494 rum_tx_mgt(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1496 const struct ieee80211_txparam *tp = ni->ni_txparms;
1497 struct ieee80211com *ic = &sc->sc_ic;
1498 struct rum_tx_data *data;
1499 struct ieee80211_frame *wh;
1500 struct ieee80211_key *k = NULL;
1503 uint8_t ac, type, xflags = 0;
1506 RUM_LOCK_ASSERT(sc);
1508 data = STAILQ_FIRST(&sc->tx_free);
1509 STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1512 wh = mtod(m0, struct ieee80211_frame *);
1513 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1514 hdrlen = ieee80211_anyhdrsize(wh);
1515 ac = M_WME_GETAC(m0);
1517 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1518 k = ieee80211_crypto_get_txkey(ni, m0);
1522 if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) &&
1523 !k->wk_cipher->ic_encap(k, m0))
1526 wh = mtod(m0, struct ieee80211_frame *);
1529 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1530 flags |= RT2573_TX_NEED_ACK;
1532 dur = ieee80211_ack_duration(ic->ic_rt, tp->mgmtrate,
1533 ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1534 USETW(wh->i_dur, dur);
1536 /* tell hardware to add timestamp for probe responses */
1537 if (type == IEEE80211_FC0_TYPE_MGT &&
1538 (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
1539 IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1540 flags |= RT2573_TX_TIMESTAMP;
1543 if (type != IEEE80211_FC0_TYPE_CTL && !IEEE80211_QOS_HAS_SEQ(wh))
1544 xflags |= RT2573_TX_HWSEQ;
1547 flags |= rum_tx_crypto_flags(sc, ni, k);
1551 data->rate = tp->mgmtrate;
1553 rum_setup_tx_desc(sc, &data->desc, k, flags, xflags, ac, hdrlen,
1554 m0->m_pkthdr.len, tp->mgmtrate);
1556 DPRINTFN(10, "sending mgt frame len=%d rate=%d\n",
1557 m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, tp->mgmtrate);
1559 STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1560 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1566 rum_tx_raw(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
1567 const struct ieee80211_bpf_params *params)
1569 struct ieee80211com *ic = ni->ni_ic;
1570 struct ieee80211_frame *wh;
1571 struct rum_tx_data *data;
1573 uint8_t ac, type, xflags = 0;
1576 RUM_LOCK_ASSERT(sc);
1578 wh = mtod(m0, struct ieee80211_frame *);
1579 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1581 ac = params->ibp_pri & 3;
1583 rate = params->ibp_rate0;
1584 if (!ieee80211_isratevalid(ic->ic_rt, rate))
1588 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
1589 flags |= RT2573_TX_NEED_ACK;
1590 if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) {
1591 error = rum_sendprot(sc, m0, ni,
1592 params->ibp_flags & IEEE80211_BPF_RTS ?
1593 IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY,
1595 if (error || sc->tx_nfree == 0)
1598 flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
1601 if (type != IEEE80211_FC0_TYPE_CTL && !IEEE80211_QOS_HAS_SEQ(wh))
1602 xflags |= RT2573_TX_HWSEQ;
1604 data = STAILQ_FIRST(&sc->tx_free);
1605 STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1612 /* XXX need to setup descriptor ourself */
1613 rum_setup_tx_desc(sc, &data->desc, NULL, flags, xflags, ac, 0,
1614 m0->m_pkthdr.len, rate);
1616 DPRINTFN(10, "sending raw frame len=%u rate=%u\n",
1617 m0->m_pkthdr.len, rate);
1619 STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1620 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1626 rum_tx_data(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1628 struct ieee80211vap *vap = ni->ni_vap;
1629 struct ieee80211com *ic = &sc->sc_ic;
1630 struct rum_tx_data *data;
1631 struct ieee80211_frame *wh;
1632 const struct ieee80211_txparam *tp = ni->ni_txparms;
1633 struct ieee80211_key *k = NULL;
1636 uint8_t ac, type, qos, xflags = 0;
1637 int error, hdrlen, rate;
1639 RUM_LOCK_ASSERT(sc);
1641 wh = mtod(m0, struct ieee80211_frame *);
1642 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1643 hdrlen = ieee80211_anyhdrsize(wh);
1645 if (IEEE80211_QOS_HAS_SEQ(wh))
1646 qos = ((const struct ieee80211_qosframe *)wh)->i_qos[0];
1649 ac = M_WME_GETAC(m0);
1651 if (m0->m_flags & M_EAPOL)
1652 rate = tp->mgmtrate;
1653 else if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1654 rate = tp->mcastrate;
1655 else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
1656 rate = tp->ucastrate;
1658 (void) ieee80211_ratectl_rate(ni, NULL, 0);
1659 rate = ni->ni_txrate;
1662 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1663 k = ieee80211_crypto_get_txkey(ni, m0);
1668 if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) &&
1669 !k->wk_cipher->ic_encap(k, m0)) {
1674 /* packet header may have moved, reset our local pointer */
1675 wh = mtod(m0, struct ieee80211_frame *);
1678 if (type != IEEE80211_FC0_TYPE_CTL && !IEEE80211_QOS_HAS_SEQ(wh))
1679 xflags |= RT2573_TX_HWSEQ;
1681 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1682 int prot = IEEE80211_PROT_NONE;
1683 if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold)
1684 prot = IEEE80211_PROT_RTSCTS;
1685 else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
1686 ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM)
1687 prot = ic->ic_protmode;
1688 if (prot != IEEE80211_PROT_NONE) {
1689 error = rum_sendprot(sc, m0, ni, prot, rate);
1690 if (error || sc->tx_nfree == 0) {
1694 flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
1699 flags |= rum_tx_crypto_flags(sc, ni, k);
1701 data = STAILQ_FIRST(&sc->tx_free);
1702 STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1709 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1710 /* Unicast frame, check if an ACK is expected. */
1711 if (!qos || (qos & IEEE80211_QOS_ACKPOLICY) !=
1712 IEEE80211_QOS_ACKPOLICY_NOACK)
1713 flags |= RT2573_TX_NEED_ACK;
1715 dur = ieee80211_ack_duration(ic->ic_rt, rate,
1716 ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1717 USETW(wh->i_dur, dur);
1720 rum_setup_tx_desc(sc, &data->desc, k, flags, xflags, ac, hdrlen,
1721 m0->m_pkthdr.len, rate);
1723 DPRINTFN(10, "sending frame len=%d rate=%d\n",
1724 m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, rate);
1726 STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1727 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1733 rum_transmit(struct ieee80211com *ic, struct mbuf *m)
1735 struct rum_softc *sc = ic->ic_softc;
1739 if (!sc->sc_running) {
1743 error = mbufq_enqueue(&sc->sc_snd, m);
1755 rum_start(struct rum_softc *sc)
1757 struct ieee80211_node *ni;
1760 RUM_LOCK_ASSERT(sc);
1762 if (!sc->sc_running)
1765 while (sc->tx_nfree >= RUM_TX_MINFREE &&
1766 (m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
1767 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1768 if (rum_tx_data(sc, m, ni) != 0) {
1769 if_inc_counter(ni->ni_vap->iv_ifp,
1770 IFCOUNTER_OERRORS, 1);
1771 ieee80211_free_node(ni);
1778 rum_parent(struct ieee80211com *ic)
1780 struct rum_softc *sc = ic->ic_softc;
1781 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1784 if (sc->sc_detached) {
1790 if (ic->ic_nrunning > 0) {
1791 if (rum_init(sc) == 0)
1792 ieee80211_start_all(ic);
1794 ieee80211_stop(vap);
1800 rum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len)
1802 struct usb_device_request req;
1805 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1806 req.bRequest = RT2573_READ_EEPROM;
1807 USETW(req.wValue, 0);
1808 USETW(req.wIndex, addr);
1809 USETW(req.wLength, len);
1811 error = rum_do_request(sc, &req, buf);
1813 device_printf(sc->sc_dev, "could not read EEPROM: %s\n",
1814 usbd_errstr(error));
1819 rum_read(struct rum_softc *sc, uint16_t reg)
1823 rum_read_multi(sc, reg, &val, sizeof val);
1825 return le32toh(val);
1829 rum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len)
1831 struct usb_device_request req;
1834 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1835 req.bRequest = RT2573_READ_MULTI_MAC;
1836 USETW(req.wValue, 0);
1837 USETW(req.wIndex, reg);
1838 USETW(req.wLength, len);
1840 error = rum_do_request(sc, &req, buf);
1842 device_printf(sc->sc_dev,
1843 "could not multi read MAC register: %s\n",
1844 usbd_errstr(error));
1849 rum_write(struct rum_softc *sc, uint16_t reg, uint32_t val)
1851 uint32_t tmp = htole32(val);
1853 return (rum_write_multi(sc, reg, &tmp, sizeof tmp));
1857 rum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len)
1859 struct usb_device_request req;
1863 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1864 req.bRequest = RT2573_WRITE_MULTI_MAC;
1865 USETW(req.wValue, 0);
1867 /* write at most 64 bytes at a time */
1868 for (offset = 0; offset < len; offset += 64) {
1869 USETW(req.wIndex, reg + offset);
1870 USETW(req.wLength, MIN(len - offset, 64));
1872 error = rum_do_request(sc, &req, (char *)buf + offset);
1874 device_printf(sc->sc_dev,
1875 "could not multi write MAC register: %s\n",
1876 usbd_errstr(error));
1881 return (USB_ERR_NORMAL_COMPLETION);
1885 rum_setbits(struct rum_softc *sc, uint16_t reg, uint32_t mask)
1887 return (rum_write(sc, reg, rum_read(sc, reg) | mask));
1891 rum_clrbits(struct rum_softc *sc, uint16_t reg, uint32_t mask)
1893 return (rum_write(sc, reg, rum_read(sc, reg) & ~mask));
1897 rum_modbits(struct rum_softc *sc, uint16_t reg, uint32_t set, uint32_t unset)
1899 return (rum_write(sc, reg, (rum_read(sc, reg) & ~unset) | set));
1903 rum_bbp_busy(struct rum_softc *sc)
1907 for (ntries = 0; ntries < 100; ntries++) {
1908 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1910 if (rum_pause(sc, hz / 100))
1920 rum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val)
1924 DPRINTFN(2, "reg=0x%08x\n", reg);
1926 if (rum_bbp_busy(sc) != 0) {
1927 device_printf(sc->sc_dev, "could not write to BBP\n");
1931 tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val;
1932 rum_write(sc, RT2573_PHY_CSR3, tmp);
1936 rum_bbp_read(struct rum_softc *sc, uint8_t reg)
1941 DPRINTFN(2, "reg=0x%08x\n", reg);
1943 if (rum_bbp_busy(sc) != 0) {
1944 device_printf(sc->sc_dev, "could not read BBP\n");
1948 val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8;
1949 rum_write(sc, RT2573_PHY_CSR3, val);
1951 for (ntries = 0; ntries < 100; ntries++) {
1952 val = rum_read(sc, RT2573_PHY_CSR3);
1953 if (!(val & RT2573_BBP_BUSY))
1955 if (rum_pause(sc, hz / 100))
1959 device_printf(sc->sc_dev, "could not read BBP\n");
1964 rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val)
1969 for (ntries = 0; ntries < 100; ntries++) {
1970 if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY))
1972 if (rum_pause(sc, hz / 100))
1975 if (ntries == 100) {
1976 device_printf(sc->sc_dev, "could not write to RF\n");
1980 tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 |
1982 rum_write(sc, RT2573_PHY_CSR4, tmp);
1984 /* remember last written value in sc */
1985 sc->rf_regs[reg] = val;
1987 DPRINTFN(15, "RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff);
1991 rum_select_antenna(struct rum_softc *sc)
1993 uint8_t bbp4, bbp77;
1996 bbp4 = rum_bbp_read(sc, 4);
1997 bbp77 = rum_bbp_read(sc, 77);
2001 /* make sure Rx is disabled before switching antenna */
2002 tmp = rum_read(sc, RT2573_TXRX_CSR0);
2003 rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
2005 rum_bbp_write(sc, 4, bbp4);
2006 rum_bbp_write(sc, 77, bbp77);
2008 rum_write(sc, RT2573_TXRX_CSR0, tmp);
2012 * Enable multi-rate retries for frames sent at OFDM rates.
2013 * In 802.11b/g mode, allow fallback to CCK rates.
2016 rum_enable_mrr(struct rum_softc *sc)
2018 struct ieee80211com *ic = &sc->sc_ic;
2020 if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) {
2021 rum_setbits(sc, RT2573_TXRX_CSR4,
2022 RT2573_MRR_ENABLED | RT2573_MRR_CCK_FALLBACK);
2024 rum_modbits(sc, RT2573_TXRX_CSR4,
2025 RT2573_MRR_ENABLED, RT2573_MRR_CCK_FALLBACK);
2030 rum_set_txpreamble(struct rum_softc *sc)
2032 struct ieee80211com *ic = &sc->sc_ic;
2034 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
2035 rum_setbits(sc, RT2573_TXRX_CSR4, RT2573_SHORT_PREAMBLE);
2037 rum_clrbits(sc, RT2573_TXRX_CSR4, RT2573_SHORT_PREAMBLE);
2041 rum_set_basicrates(struct rum_softc *sc)
2043 struct ieee80211com *ic = &sc->sc_ic;
2045 /* update basic rate set */
2046 if (ic->ic_curmode == IEEE80211_MODE_11B) {
2047 /* 11b basic rates: 1, 2Mbps */
2048 rum_write(sc, RT2573_TXRX_CSR5, 0x3);
2049 } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) {
2050 /* 11a basic rates: 6, 12, 24Mbps */
2051 rum_write(sc, RT2573_TXRX_CSR5, 0x150);
2053 /* 11b/g basic rates: 1, 2, 5.5, 11Mbps */
2054 rum_write(sc, RT2573_TXRX_CSR5, 0xf);
2059 * Reprogram MAC/BBP to switch to a new band. Values taken from the reference
2063 rum_select_band(struct rum_softc *sc, struct ieee80211_channel *c)
2065 uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104;
2067 /* update all BBP registers that depend on the band */
2068 bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c;
2069 bbp35 = 0x50; bbp97 = 0x48; bbp98 = 0x48;
2070 if (IEEE80211_IS_CHAN_5GHZ(c)) {
2071 bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c;
2072 bbp35 += 0x10; bbp97 += 0x10; bbp98 += 0x10;
2074 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
2075 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
2076 bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10;
2080 rum_bbp_write(sc, 17, bbp17);
2081 rum_bbp_write(sc, 96, bbp96);
2082 rum_bbp_write(sc, 104, bbp104);
2084 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
2085 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
2086 rum_bbp_write(sc, 75, 0x80);
2087 rum_bbp_write(sc, 86, 0x80);
2088 rum_bbp_write(sc, 88, 0x80);
2091 rum_bbp_write(sc, 35, bbp35);
2092 rum_bbp_write(sc, 97, bbp97);
2093 rum_bbp_write(sc, 98, bbp98);
2095 if (IEEE80211_IS_CHAN_2GHZ(c)) {
2096 rum_modbits(sc, RT2573_PHY_CSR0, RT2573_PA_PE_2GHZ,
2099 rum_modbits(sc, RT2573_PHY_CSR0, RT2573_PA_PE_5GHZ,
2105 rum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c)
2107 struct ieee80211com *ic = &sc->sc_ic;
2108 const struct rfprog *rfprog;
2109 uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT;
2113 chan = ieee80211_chan2ieee(ic, c);
2114 if (chan == 0 || chan == IEEE80211_CHAN_ANY)
2117 /* select the appropriate RF settings based on what EEPROM says */
2118 rfprog = (sc->rf_rev == RT2573_RF_5225 ||
2119 sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226;
2121 /* find the settings for this channel (we know it exists) */
2122 for (i = 0; rfprog[i].chan != chan; i++);
2124 power = sc->txpow[i];
2128 } else if (power > 31) {
2129 bbp94 += power - 31;
2134 * If we are switching from the 2GHz band to the 5GHz band or
2135 * vice-versa, BBP registers need to be reprogrammed.
2137 if (c->ic_flags != ic->ic_curchan->ic_flags) {
2138 rum_select_band(sc, c);
2139 rum_select_antenna(sc);
2143 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
2144 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
2145 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
2146 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
2148 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
2149 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
2150 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7 | 1);
2151 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
2153 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
2154 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
2155 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
2156 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
2158 rum_pause(sc, hz / 100);
2160 /* enable smart mode for MIMO-capable RFs */
2161 bbp3 = rum_bbp_read(sc, 3);
2163 bbp3 &= ~RT2573_SMART_MODE;
2164 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527)
2165 bbp3 |= RT2573_SMART_MODE;
2167 rum_bbp_write(sc, 3, bbp3);
2169 if (bbp94 != RT2573_BBPR94_DEFAULT)
2170 rum_bbp_write(sc, 94, bbp94);
2172 /* give the chip some extra time to do the switchover */
2173 rum_pause(sc, hz / 100);
2177 rum_set_maxretry(struct rum_softc *sc, struct ieee80211vap *vap)
2179 struct ieee80211_node *ni = vap->iv_bss;
2180 const struct ieee80211_txparam *tp = ni->ni_txparms;
2181 struct rum_vap *rvp = RUM_VAP(vap);
2183 rvp->maxretry = MIN(tp->maxretry, 0xf);
2185 rum_modbits(sc, RT2573_TXRX_CSR4, RT2573_SHORT_RETRY(rvp->maxretry) |
2186 RT2573_LONG_RETRY(rvp->maxretry),
2187 RT2573_SHORT_RETRY_MASK | RT2573_LONG_RETRY_MASK);
2191 * Enable TSF synchronization and tell h/w to start sending beacons for IBSS
2192 * and HostAP operating modes.
2195 rum_enable_tsf_sync(struct rum_softc *sc)
2197 struct ieee80211com *ic = &sc->sc_ic;
2198 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2202 if (vap->iv_opmode != IEEE80211_M_STA) {
2204 * Change default 16ms TBTT adjustment to 8ms.
2205 * Must be done before enabling beacon generation.
2207 if (rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8) != 0)
2211 tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000;
2213 /* set beacon interval (in 1/16ms unit) */
2214 bintval = vap->iv_bss->ni_intval;
2215 tmp |= bintval * 16;
2216 tmp |= RT2573_TSF_TIMER_EN | RT2573_TBTT_TIMER_EN;
2218 switch (vap->iv_opmode) {
2219 case IEEE80211_M_STA:
2221 * Local TSF is always updated with remote TSF on beacon
2224 tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_STA);
2226 case IEEE80211_M_IBSS:
2228 * Local TSF is updated with remote TSF on beacon reception
2229 * only if the remote TSF is greater than local TSF.
2231 tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_IBSS);
2232 tmp |= RT2573_BCN_TX_EN;
2234 case IEEE80211_M_HOSTAP:
2235 /* SYNC with nobody */
2236 tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_HOSTAP);
2237 tmp |= RT2573_BCN_TX_EN;
2240 device_printf(sc->sc_dev,
2241 "Enabling TSF failed. undefined opmode %d\n",
2246 if (rum_write(sc, RT2573_TXRX_CSR9, tmp) != 0)
2249 /* refresh current sleep time */
2250 return (rum_set_sleep_time(sc, bintval));
2254 rum_enable_tsf(struct rum_softc *sc)
2256 rum_modbits(sc, RT2573_TXRX_CSR9, RT2573_TSF_TIMER_EN |
2257 RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_DIS), 0x00ffffff);
2261 rum_abort_tsf_sync(struct rum_softc *sc)
2263 rum_clrbits(sc, RT2573_TXRX_CSR9, 0x00ffffff);
2267 rum_get_tsf(struct rum_softc *sc, uint64_t *buf)
2269 rum_read_multi(sc, RT2573_TXRX_CSR12, buf, sizeof (*buf));
2273 rum_update_slot_cb(struct rum_softc *sc, union sec_param *data, uint8_t rvp_id)
2275 struct ieee80211com *ic = &sc->sc_ic;
2278 slottime = IEEE80211_GET_SLOTTIME(ic);
2280 rum_modbits(sc, RT2573_MAC_CSR9, slottime, 0xff);
2282 DPRINTF("setting slot time to %uus\n", slottime);
2286 rum_update_slot(struct ieee80211com *ic)
2288 rum_cmd_sleepable(ic->ic_softc, NULL, 0, 0, rum_update_slot_cb);
2292 rum_wme_update(struct ieee80211com *ic)
2294 struct chanAccParams chp;
2295 const struct wmeParams *chanp;
2296 struct rum_softc *sc = ic->ic_softc;
2299 ieee80211_wme_ic_getparams(ic, &chp);
2300 chanp = chp.cap_wmeParams;
2303 error = rum_write(sc, RT2573_AIFSN_CSR,
2304 chanp[WME_AC_VO].wmep_aifsn << 12 |
2305 chanp[WME_AC_VI].wmep_aifsn << 8 |
2306 chanp[WME_AC_BK].wmep_aifsn << 4 |
2307 chanp[WME_AC_BE].wmep_aifsn);
2310 error = rum_write(sc, RT2573_CWMIN_CSR,
2311 chanp[WME_AC_VO].wmep_logcwmin << 12 |
2312 chanp[WME_AC_VI].wmep_logcwmin << 8 |
2313 chanp[WME_AC_BK].wmep_logcwmin << 4 |
2314 chanp[WME_AC_BE].wmep_logcwmin);
2317 error = rum_write(sc, RT2573_CWMAX_CSR,
2318 chanp[WME_AC_VO].wmep_logcwmax << 12 |
2319 chanp[WME_AC_VI].wmep_logcwmax << 8 |
2320 chanp[WME_AC_BK].wmep_logcwmax << 4 |
2321 chanp[WME_AC_BE].wmep_logcwmax);
2324 error = rum_write(sc, RT2573_TXOP01_CSR,
2325 chanp[WME_AC_BK].wmep_txopLimit << 16 |
2326 chanp[WME_AC_BE].wmep_txopLimit);
2329 error = rum_write(sc, RT2573_TXOP23_CSR,
2330 chanp[WME_AC_VO].wmep_txopLimit << 16 |
2331 chanp[WME_AC_VI].wmep_txopLimit);
2335 memcpy(sc->wme_params, chanp, sizeof(*chanp) * WME_NUM_AC);
2340 device_printf(sc->sc_dev, "%s: WME update failed, error %d\n",
2348 rum_set_bssid(struct rum_softc *sc, const uint8_t *bssid)
2351 rum_write(sc, RT2573_MAC_CSR4,
2352 bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24);
2353 rum_write(sc, RT2573_MAC_CSR5,
2354 bssid[4] | bssid[5] << 8 | RT2573_NUM_BSSID_MSK(1));
2358 rum_set_macaddr(struct rum_softc *sc, const uint8_t *addr)
2361 rum_write(sc, RT2573_MAC_CSR2,
2362 addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
2363 rum_write(sc, RT2573_MAC_CSR3,
2364 addr[4] | addr[5] << 8 | 0xff << 16);
2368 rum_setpromisc(struct rum_softc *sc)
2370 struct ieee80211com *ic = &sc->sc_ic;
2372 if (ic->ic_promisc == 0)
2373 rum_setbits(sc, RT2573_TXRX_CSR0, RT2573_DROP_NOT_TO_ME);
2375 rum_clrbits(sc, RT2573_TXRX_CSR0, RT2573_DROP_NOT_TO_ME);
2377 DPRINTF("%s promiscuous mode\n", ic->ic_promisc > 0 ?
2378 "entering" : "leaving");
2382 rum_update_promisc(struct ieee80211com *ic)
2384 struct rum_softc *sc = ic->ic_softc;
2393 rum_update_mcast(struct ieee80211com *ic)
2402 case RT2573_RF_2527: return "RT2527 (MIMO XR)";
2403 case RT2573_RF_2528: return "RT2528";
2404 case RT2573_RF_5225: return "RT5225 (MIMO XR)";
2405 case RT2573_RF_5226: return "RT5226";
2406 default: return "unknown";
2411 rum_read_eeprom(struct rum_softc *sc)
2418 /* read MAC address */
2419 rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, sc->sc_ic.ic_macaddr, 6);
2421 rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2);
2423 sc->rf_rev = (val >> 11) & 0x1f;
2424 sc->hw_radio = (val >> 10) & 0x1;
2425 sc->rx_ant = (val >> 4) & 0x3;
2426 sc->tx_ant = (val >> 2) & 0x3;
2427 sc->nb_ant = val & 0x3;
2429 DPRINTF("RF revision=%d\n", sc->rf_rev);
2431 rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2);
2433 sc->ext_5ghz_lna = (val >> 6) & 0x1;
2434 sc->ext_2ghz_lna = (val >> 4) & 0x1;
2436 DPRINTF("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n",
2437 sc->ext_2ghz_lna, sc->ext_5ghz_lna);
2439 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2);
2441 if ((val & 0xff) != 0xff)
2442 sc->rssi_2ghz_corr = (int8_t)(val & 0xff); /* signed */
2444 /* Only [-10, 10] is valid */
2445 if (sc->rssi_2ghz_corr < -10 || sc->rssi_2ghz_corr > 10)
2446 sc->rssi_2ghz_corr = 0;
2448 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2);
2450 if ((val & 0xff) != 0xff)
2451 sc->rssi_5ghz_corr = (int8_t)(val & 0xff); /* signed */
2453 /* Only [-10, 10] is valid */
2454 if (sc->rssi_5ghz_corr < -10 || sc->rssi_5ghz_corr > 10)
2455 sc->rssi_5ghz_corr = 0;
2457 if (sc->ext_2ghz_lna)
2458 sc->rssi_2ghz_corr -= 14;
2459 if (sc->ext_5ghz_lna)
2460 sc->rssi_5ghz_corr -= 14;
2462 DPRINTF("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n",
2463 sc->rssi_2ghz_corr, sc->rssi_5ghz_corr);
2465 rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2);
2467 if ((val & 0xff) != 0xff)
2468 sc->rffreq = val & 0xff;
2470 DPRINTF("RF freq=%d\n", sc->rffreq);
2472 /* read Tx power for all a/b/g channels */
2473 rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14);
2474 /* XXX default Tx power for 802.11a channels */
2475 memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14);
2477 for (i = 0; i < 14; i++)
2478 DPRINTF("Channel=%d Tx power=%d\n", i + 1, sc->txpow[i]);
2481 /* read default values for BBP registers */
2482 rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
2484 for (i = 0; i < 14; i++) {
2485 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
2487 DPRINTF("BBP R%d=%02x\n", sc->bbp_prom[i].reg,
2488 sc->bbp_prom[i].val);
2494 rum_bbp_wakeup(struct rum_softc *sc)
2496 unsigned int ntries;
2498 for (ntries = 0; ntries < 100; ntries++) {
2499 if (rum_read(sc, RT2573_MAC_CSR12) & 8)
2501 rum_write(sc, RT2573_MAC_CSR12, 4); /* force wakeup */
2502 if (rum_pause(sc, hz / 100))
2505 if (ntries == 100) {
2506 device_printf(sc->sc_dev,
2507 "timeout waiting for BBP/RF to wakeup\n");
2515 rum_bbp_init(struct rum_softc *sc)
2519 /* wait for BBP to be ready */
2520 for (ntries = 0; ntries < 100; ntries++) {
2521 const uint8_t val = rum_bbp_read(sc, 0);
2522 if (val != 0 && val != 0xff)
2524 if (rum_pause(sc, hz / 100))
2527 if (ntries == 100) {
2528 device_printf(sc->sc_dev, "timeout waiting for BBP\n");
2532 /* initialize BBP registers to default values */
2533 for (i = 0; i < nitems(rum_def_bbp); i++)
2534 rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val);
2536 /* write vendor-specific BBP values (from EEPROM) */
2537 for (i = 0; i < 16; i++) {
2538 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
2540 rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
2547 rum_clr_shkey_regs(struct rum_softc *sc)
2549 rum_write(sc, RT2573_SEC_CSR0, 0);
2550 rum_write(sc, RT2573_SEC_CSR1, 0);
2551 rum_write(sc, RT2573_SEC_CSR5, 0);
2555 rum_init(struct rum_softc *sc)
2557 struct ieee80211com *ic = &sc->sc_ic;
2558 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2563 if (sc->sc_running) {
2568 /* initialize MAC registers to default values */
2569 for (i = 0; i < nitems(rum_def_mac); i++)
2570 rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val);
2572 /* reset some WME parameters to default values */
2573 sc->wme_params[0].wmep_aifsn = 2;
2574 sc->wme_params[0].wmep_logcwmin = 4;
2575 sc->wme_params[0].wmep_logcwmax = 10;
2577 /* set host ready */
2578 rum_write(sc, RT2573_MAC_CSR1, RT2573_RESET_ASIC | RT2573_RESET_BBP);
2579 rum_write(sc, RT2573_MAC_CSR1, 0);
2581 /* wait for BBP/RF to wakeup */
2582 if ((ret = rum_bbp_wakeup(sc)) != 0)
2585 if ((ret = rum_bbp_init(sc)) != 0)
2588 /* select default channel */
2589 rum_select_band(sc, ic->ic_curchan);
2590 rum_select_antenna(sc);
2591 rum_set_chan(sc, ic->ic_curchan);
2593 /* clear STA registers */
2594 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
2596 /* clear security registers (if required) */
2597 if (sc->sc_clr_shkeys == 0) {
2598 rum_clr_shkey_regs(sc);
2599 sc->sc_clr_shkeys = 1;
2602 rum_set_macaddr(sc, vap ? vap->iv_myaddr : ic->ic_macaddr);
2604 /* initialize ASIC */
2605 rum_write(sc, RT2573_MAC_CSR1, RT2573_HOST_READY);
2608 * Allocate Tx and Rx xfer queues.
2610 rum_setup_tx_list(sc);
2612 /* update Rx filter */
2613 tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff;
2615 tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR;
2616 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2617 tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR |
2619 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2620 tmp |= RT2573_DROP_TODS;
2621 if (ic->ic_promisc == 0)
2622 tmp |= RT2573_DROP_NOT_TO_ME;
2624 rum_write(sc, RT2573_TXRX_CSR0, tmp);
2627 usbd_xfer_set_stall(sc->sc_xfer[RUM_BULK_WR]);
2628 usbd_transfer_start(sc->sc_xfer[RUM_BULK_RD]);
2630 end: RUM_UNLOCK(sc);
2639 rum_stop(struct rum_softc *sc)
2643 if (!sc->sc_running) {
2651 * Drain the USB transfers, if not already drained:
2653 usbd_transfer_drain(sc->sc_xfer[RUM_BULK_WR]);
2654 usbd_transfer_drain(sc->sc_xfer[RUM_BULK_RD]);
2657 rum_unsetup_tx_list(sc);
2660 rum_setbits(sc, RT2573_TXRX_CSR0, RT2573_DISABLE_RX);
2663 rum_write(sc, RT2573_MAC_CSR1, RT2573_RESET_ASIC | RT2573_RESET_BBP);
2664 rum_write(sc, RT2573_MAC_CSR1, 0);
2669 rum_load_microcode(struct rum_softc *sc, const uint8_t *ucode, size_t size)
2671 uint16_t reg = RT2573_MCU_CODE_BASE;
2674 /* copy firmware image into NIC */
2675 for (; size >= 4; reg += 4, ucode += 4, size -= 4) {
2676 err = rum_write(sc, reg, UGETDW(ucode));
2678 /* firmware already loaded ? */
2679 device_printf(sc->sc_dev, "Firmware load "
2680 "failure! (ignored)\n");
2685 err = rum_do_mcu_request(sc, RT2573_MCU_RUN);
2686 if (err != USB_ERR_NORMAL_COMPLETION) {
2687 device_printf(sc->sc_dev, "could not run firmware: %s\n",
2691 /* give the chip some time to boot */
2692 rum_pause(sc, hz / 8);
2696 rum_set_sleep_time(struct rum_softc *sc, uint16_t bintval)
2698 struct ieee80211com *ic = &sc->sc_ic;
2702 RUM_LOCK_ASSERT(sc);
2704 exp = ic->ic_lintval / bintval;
2705 delay = ic->ic_lintval % bintval;
2707 if (exp > RT2573_TBCN_EXP_MAX)
2708 exp = RT2573_TBCN_EXP_MAX;
2709 if (delay > RT2573_TBCN_DELAY_MAX)
2710 delay = RT2573_TBCN_DELAY_MAX;
2712 uerror = rum_modbits(sc, RT2573_MAC_CSR11,
2713 RT2573_TBCN_EXP(exp) |
2714 RT2573_TBCN_DELAY(delay),
2715 RT2573_TBCN_EXP(RT2573_TBCN_EXP_MAX) |
2716 RT2573_TBCN_DELAY(RT2573_TBCN_DELAY_MAX));
2718 if (uerror != USB_ERR_NORMAL_COMPLETION)
2721 sc->sc_sleep_time = IEEE80211_TU_TO_TICKS(exp * bintval + delay);
2727 rum_reset(struct ieee80211vap *vap, u_long cmd)
2729 struct ieee80211com *ic = vap->iv_ic;
2730 struct ieee80211_node *ni;
2731 struct rum_softc *sc = ic->ic_softc;
2735 case IEEE80211_IOC_POWERSAVE:
2736 case IEEE80211_IOC_PROTMODE:
2737 case IEEE80211_IOC_RTSTHRESHOLD:
2740 case IEEE80211_IOC_POWERSAVESLEEP:
2741 ni = ieee80211_ref_node(vap->iv_bss);
2744 error = rum_set_sleep_time(sc, ni->ni_intval);
2745 if (vap->iv_state == IEEE80211_S_SLEEP) {
2746 /* Use new values for wakeup timer. */
2747 rum_clrbits(sc, RT2573_MAC_CSR11, RT2573_AUTO_WAKEUP);
2748 rum_setbits(sc, RT2573_MAC_CSR11, RT2573_AUTO_WAKEUP);
2750 /* XXX send reassoc */
2753 ieee80211_free_node(ni);
2764 rum_set_beacon(struct rum_softc *sc, struct ieee80211vap *vap)
2766 struct ieee80211com *ic = vap->iv_ic;
2767 struct rum_vap *rvp = RUM_VAP(vap);
2768 struct mbuf *m = rvp->bcn_mbuf;
2769 const struct ieee80211_txparam *tp;
2770 struct rum_tx_desc desc;
2772 RUM_LOCK_ASSERT(sc);
2776 if (ic->ic_bsschan == IEEE80211_CHAN_ANYC)
2779 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)];
2780 rum_setup_tx_desc(sc, &desc, NULL, RT2573_TX_TIMESTAMP,
2781 RT2573_TX_HWSEQ, 0, 0, m->m_pkthdr.len, tp->mgmtrate);
2783 /* copy the Tx descriptor into NIC memory */
2784 if (rum_write_multi(sc, RT2573_HW_BCN_BASE(0), (uint8_t *)&desc,
2785 RT2573_TX_DESC_SIZE) != 0)
2788 /* copy beacon header and payload into NIC memory */
2789 if (rum_write_multi(sc, RT2573_HW_BCN_BASE(0) + RT2573_TX_DESC_SIZE,
2790 mtod(m, uint8_t *), m->m_pkthdr.len) != 0)
2797 rum_alloc_beacon(struct rum_softc *sc, struct ieee80211vap *vap)
2799 struct rum_vap *rvp = RUM_VAP(vap);
2800 struct ieee80211_node *ni = vap->iv_bss;
2803 if (ni->ni_chan == IEEE80211_CHAN_ANYC)
2806 m = ieee80211_beacon_alloc(ni);
2810 if (rvp->bcn_mbuf != NULL)
2811 m_freem(rvp->bcn_mbuf);
2815 return (rum_set_beacon(sc, vap));
2819 rum_update_beacon_cb(struct rum_softc *sc, union sec_param *data,
2822 struct ieee80211vap *vap = data->vap;
2824 rum_set_beacon(sc, vap);
2828 rum_update_beacon(struct ieee80211vap *vap, int item)
2830 struct ieee80211com *ic = vap->iv_ic;
2831 struct rum_softc *sc = ic->ic_softc;
2832 struct rum_vap *rvp = RUM_VAP(vap);
2833 struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
2834 struct ieee80211_node *ni = vap->iv_bss;
2835 struct mbuf *m = rvp->bcn_mbuf;
2840 m = ieee80211_beacon_alloc(ni);
2842 device_printf(sc->sc_dev,
2843 "%s: could not allocate beacon frame\n", __func__);
2851 case IEEE80211_BEACON_ERP:
2852 rum_update_slot(ic);
2854 case IEEE80211_BEACON_TIM:
2862 setbit(bo->bo_flags, item);
2863 ieee80211_beacon_update(ni, m, mcast);
2865 rum_cmd_sleepable(sc, &vap, sizeof(vap), 0, rum_update_beacon_cb);
2869 rum_common_key_set(struct rum_softc *sc, struct ieee80211_key *k,
2873 if (rum_write_multi(sc, base, k->wk_key, k->wk_keylen))
2876 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP) {
2877 if (rum_write_multi(sc, base + IEEE80211_KEYBUF_SIZE,
2880 if (rum_write_multi(sc, base + IEEE80211_KEYBUF_SIZE + 8,
2889 rum_group_key_set_cb(struct rum_softc *sc, union sec_param *data,
2892 struct ieee80211_key *k = &data->key;
2895 if (sc->sc_clr_shkeys == 0) {
2896 rum_clr_shkey_regs(sc);
2897 sc->sc_clr_shkeys = 1;
2900 mode = rum_crypto_mode(sc, k->wk_cipher->ic_cipher, k->wk_keylen);
2904 DPRINTFN(1, "setting group key %d for vap %d, mode %d "
2905 "(tx %s, rx %s)\n", k->wk_keyix, rvp_id, mode,
2906 (k->wk_flags & IEEE80211_KEY_XMIT) ? "on" : "off",
2907 (k->wk_flags & IEEE80211_KEY_RECV) ? "on" : "off");
2909 /* Install the key. */
2910 if (rum_common_key_set(sc, k, RT2573_SKEY(rvp_id, k->wk_keyix)) != 0)
2913 /* Set cipher mode. */
2914 if (rum_modbits(sc, rvp_id < 2 ? RT2573_SEC_CSR1 : RT2573_SEC_CSR5,
2915 mode << (rvp_id % 2 + k->wk_keyix) * RT2573_SKEY_MAX,
2916 RT2573_MODE_MASK << (rvp_id % 2 + k->wk_keyix) * RT2573_SKEY_MAX)
2920 /* Mark this key as valid. */
2921 if (rum_setbits(sc, RT2573_SEC_CSR0,
2922 1 << (rvp_id * RT2573_SKEY_MAX + k->wk_keyix)) != 0)
2928 device_printf(sc->sc_dev, "%s: cannot set group key %d for vap %d\n",
2929 __func__, k->wk_keyix, rvp_id);
2933 rum_group_key_del_cb(struct rum_softc *sc, union sec_param *data,
2936 struct ieee80211_key *k = &data->key;
2938 DPRINTF("%s: removing group key %d for vap %d\n", __func__,
2939 k->wk_keyix, rvp_id);
2941 rvp_id < 2 ? RT2573_SEC_CSR1 : RT2573_SEC_CSR5,
2942 RT2573_MODE_MASK << (rvp_id % 2 + k->wk_keyix) * RT2573_SKEY_MAX);
2943 rum_clrbits(sc, RT2573_SEC_CSR0,
2944 rvp_id * RT2573_SKEY_MAX + k->wk_keyix);
2948 rum_pair_key_set_cb(struct rum_softc *sc, union sec_param *data,
2951 struct ieee80211_key *k = &data->key;
2952 uint8_t buf[IEEE80211_ADDR_LEN + 1];
2955 mode = rum_crypto_mode(sc, k->wk_cipher->ic_cipher, k->wk_keylen);
2959 DPRINTFN(1, "setting pairwise key %d for vap %d, mode %d "
2960 "(tx %s, rx %s)\n", k->wk_keyix, rvp_id, mode,
2961 (k->wk_flags & IEEE80211_KEY_XMIT) ? "on" : "off",
2962 (k->wk_flags & IEEE80211_KEY_RECV) ? "on" : "off");
2964 /* Install the key. */
2965 if (rum_common_key_set(sc, k, RT2573_PKEY(k->wk_keyix)) != 0)
2968 IEEE80211_ADDR_COPY(buf, k->wk_macaddr);
2969 buf[IEEE80211_ADDR_LEN] = mode;
2971 /* Set transmitter address and cipher mode. */
2972 if (rum_write_multi(sc, RT2573_ADDR_ENTRY(k->wk_keyix),
2973 buf, sizeof buf) != 0)
2976 /* Enable key table lookup for this vap. */
2977 if (sc->vap_key_count[rvp_id]++ == 0)
2978 if (rum_setbits(sc, RT2573_SEC_CSR4, 1 << rvp_id) != 0)
2981 /* Mark this key as valid. */
2983 k->wk_keyix < 32 ? RT2573_SEC_CSR2 : RT2573_SEC_CSR3,
2984 1 << (k->wk_keyix % 32)) != 0)
2990 device_printf(sc->sc_dev,
2991 "%s: cannot set pairwise key %d, vap %d\n", __func__, k->wk_keyix,
2996 rum_pair_key_del_cb(struct rum_softc *sc, union sec_param *data,
2999 struct ieee80211_key *k = &data->key;
3001 DPRINTF("%s: removing key %d\n", __func__, k->wk_keyix);
3002 rum_clrbits(sc, (k->wk_keyix < 32) ? RT2573_SEC_CSR2 : RT2573_SEC_CSR3,
3003 1 << (k->wk_keyix % 32));
3004 sc->keys_bmap &= ~(1ULL << k->wk_keyix);
3005 if (--sc->vap_key_count[rvp_id] == 0)
3006 rum_clrbits(sc, RT2573_SEC_CSR4, 1 << rvp_id);
3010 rum_key_alloc(struct ieee80211vap *vap, struct ieee80211_key *k,
3011 ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
3013 struct rum_softc *sc = vap->iv_ic->ic_softc;
3016 if (!(&vap->iv_nw_keys[0] <= k &&
3017 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID])) {
3018 if (!(k->wk_flags & IEEE80211_KEY_SWCRYPT)) {
3020 for (i = 0; i < RT2573_ADDR_MAX; i++) {
3021 if ((sc->keys_bmap & (1ULL << i)) == 0) {
3022 sc->keys_bmap |= (1ULL << i);
3028 if (i == RT2573_ADDR_MAX) {
3029 device_printf(sc->sc_dev,
3030 "%s: no free space in the key table\n",
3037 *keyix = ieee80211_crypto_get_key_wepidx(vap, k);
3044 rum_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
3046 struct rum_softc *sc = vap->iv_ic->ic_softc;
3049 if (k->wk_flags & IEEE80211_KEY_SWCRYPT) {
3054 group = k >= &vap->iv_nw_keys[0] && k < &vap->iv_nw_keys[IEEE80211_WEP_NKID];
3056 return !rum_cmd_sleepable(sc, k, sizeof(*k), 0,
3057 group ? rum_group_key_set_cb : rum_pair_key_set_cb);
3061 rum_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
3063 struct rum_softc *sc = vap->iv_ic->ic_softc;
3066 if (k->wk_flags & IEEE80211_KEY_SWCRYPT) {
3071 group = k >= &vap->iv_nw_keys[0] && k < &vap->iv_nw_keys[IEEE80211_WEP_NKID];
3073 return !rum_cmd_sleepable(sc, k, sizeof(*k), 0,
3074 group ? rum_group_key_del_cb : rum_pair_key_del_cb);
3078 rum_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
3079 const struct ieee80211_bpf_params *params)
3081 struct rum_softc *sc = ni->ni_ic->ic_softc;
3085 /* prevent management frames from being sent if we're not ready */
3086 if (!sc->sc_running) {
3090 if (sc->tx_nfree < RUM_TX_MINFREE) {
3095 if (params == NULL) {
3097 * Legacy path; interpret frame contents to decide
3098 * precisely how to send the frame.
3100 if ((ret = rum_tx_mgt(sc, m, ni)) != 0)
3104 * Caller supplied explicit parameters to use in
3105 * sending the frame.
3107 if ((ret = rum_tx_raw(sc, m, ni, params)) != 0)
3120 rum_ratectl_start(struct rum_softc *sc, struct ieee80211_node *ni)
3122 struct ieee80211vap *vap = ni->ni_vap;
3123 struct rum_vap *rvp = RUM_VAP(vap);
3125 /* clear statistic registers (STA_CSR0 to STA_CSR5) */
3126 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
3128 usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp);
3132 rum_ratectl_timeout(void *arg)
3134 struct rum_vap *rvp = arg;
3135 struct ieee80211vap *vap = &rvp->vap;
3136 struct ieee80211com *ic = vap->iv_ic;
3138 ieee80211_runtask(ic, &rvp->ratectl_task);
3142 rum_ratectl_task(void *arg, int pending)
3144 struct rum_vap *rvp = arg;
3145 struct ieee80211vap *vap = &rvp->vap;
3146 struct rum_softc *sc = vap->iv_ic->ic_softc;
3147 struct ieee80211_ratectl_tx_stats *txs = &sc->sc_txs;
3151 /* read and clear statistic registers (STA_CSR0 to STA_CSR5) */
3152 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof(sc->sta));
3154 ok[0] = (le32toh(sc->sta[4]) & 0xffff); /* TX ok w/o retry */
3155 ok[1] = (le32toh(sc->sta[4]) >> 16); /* TX ok w/ one retry */
3156 ok[2] = (le32toh(sc->sta[5]) & 0xffff); /* TX ok w/ multiple retries */
3157 fail = (le32toh(sc->sta[5]) >> 16); /* TX retry-fail count */
3159 txs->flags = IEEE80211_RATECTL_TX_STATS_RETRIES;
3160 txs->nframes = ok[0] + ok[1] + ok[2] + fail;
3161 txs->nsuccess = txs->nframes - fail;
3163 txs->nretries = ok[1] + ok[2] * 2 + fail * (rvp->maxretry + 1);
3165 if (txs->nframes != 0)
3166 ieee80211_ratectl_tx_update(vap, txs);
3168 /* count TX retry-fail as Tx errors */
3169 if_inc_counter(vap->iv_ifp, IFCOUNTER_OERRORS, fail);
3171 usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp);
3176 rum_scan_start(struct ieee80211com *ic)
3178 struct rum_softc *sc = ic->ic_softc;
3181 rum_abort_tsf_sync(sc);
3182 rum_set_bssid(sc, ieee80211broadcastaddr);
3188 rum_scan_end(struct ieee80211com *ic)
3190 struct rum_softc *sc = ic->ic_softc;
3192 if (ic->ic_flags_ext & IEEE80211_FEXT_BGSCAN) {
3194 if (ic->ic_opmode != IEEE80211_M_AHDEMO)
3195 rum_enable_tsf_sync(sc);
3198 rum_set_bssid(sc, sc->sc_bssid);
3204 rum_set_channel(struct ieee80211com *ic)
3206 struct rum_softc *sc = ic->ic_softc;
3209 rum_set_chan(sc, ic->ic_curchan);
3214 rum_getradiocaps(struct ieee80211com *ic,
3215 int maxchans, int *nchans, struct ieee80211_channel chans[])
3217 struct rum_softc *sc = ic->ic_softc;
3218 uint8_t bands[IEEE80211_MODE_BYTES];
3220 memset(bands, 0, sizeof(bands));
3221 setbit(bands, IEEE80211_MODE_11B);
3222 setbit(bands, IEEE80211_MODE_11G);
3223 ieee80211_add_channel_list_2ghz(chans, maxchans, nchans,
3224 rum_chan_2ghz, nitems(rum_chan_2ghz), bands, 0);
3226 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_5226) {
3227 setbit(bands, IEEE80211_MODE_11A);
3228 ieee80211_add_channel_list_5ghz(chans, maxchans, nchans,
3229 rum_chan_5ghz, nitems(rum_chan_5ghz), bands, 0);
3234 rum_get_rssi(struct rum_softc *sc, uint8_t raw)
3236 struct ieee80211com *ic = &sc->sc_ic;
3239 lna = (raw >> 5) & 0x3;
3246 * NB: Since RSSI is relative to noise floor, -1 is
3247 * adequate for caller to know error happened.
3252 rssi = (2 * agc) - RT2573_NOISE_FLOOR;
3254 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) {
3255 rssi += sc->rssi_2ghz_corr;
3264 rssi += sc->rssi_5ghz_corr;
3266 if (!sc->ext_5ghz_lna && lna != 1)
3280 rum_pause(struct rum_softc *sc, int timeout)
3283 usb_pause_mtx(&sc->sc_mtx, timeout);
3287 static device_method_t rum_methods[] = {
3288 /* Device interface */
3289 DEVMETHOD(device_probe, rum_match),
3290 DEVMETHOD(device_attach, rum_attach),
3291 DEVMETHOD(device_detach, rum_detach),
3295 static driver_t rum_driver = {
3297 .methods = rum_methods,
3298 .size = sizeof(struct rum_softc),
3301 static devclass_t rum_devclass;
3303 DRIVER_MODULE(rum, uhub, rum_driver, rum_devclass, NULL, 0);
3304 MODULE_DEPEND(rum, wlan, 1, 1, 1);
3305 MODULE_DEPEND(rum, usb, 1, 1, 1);
3306 MODULE_VERSION(rum, 1);
3307 USB_PNP_HOST_INFO(rum_devs);