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>
45 #include <machine/bus.h>
46 #include <machine/resource.h>
51 #include <net/if_var.h>
52 #include <net/if_arp.h>
53 #include <net/ethernet.h>
54 #include <net/if_dl.h>
55 #include <net/if_media.h>
56 #include <net/if_types.h>
59 #include <netinet/in.h>
60 #include <netinet/in_systm.h>
61 #include <netinet/in_var.h>
62 #include <netinet/if_ether.h>
63 #include <netinet/ip.h>
66 #include <net80211/ieee80211_var.h>
67 #include <net80211/ieee80211_regdomain.h>
68 #include <net80211/ieee80211_radiotap.h>
69 #include <net80211/ieee80211_ratectl.h>
71 #include <dev/usb/usb.h>
72 #include <dev/usb/usbdi.h>
75 #define USB_DEBUG_VAR rum_debug
76 #include <dev/usb/usb_debug.h>
78 #include <dev/usb/wlan/if_rumreg.h>
79 #include <dev/usb/wlan/if_rumvar.h>
80 #include <dev/usb/wlan/if_rumfw.h>
83 static int rum_debug = 0;
85 static SYSCTL_NODE(_hw_usb, OID_AUTO, rum, CTLFLAG_RW, 0, "USB rum");
86 SYSCTL_INT(_hw_usb_rum, OID_AUTO, debug, CTLFLAG_RWTUN, &rum_debug, 0,
90 static const STRUCT_USB_HOST_ID rum_devs[] = {
91 #define RUM_DEV(v,p) { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
92 RUM_DEV(ABOCOM, HWU54DM),
93 RUM_DEV(ABOCOM, RT2573_2),
94 RUM_DEV(ABOCOM, RT2573_3),
95 RUM_DEV(ABOCOM, RT2573_4),
96 RUM_DEV(ABOCOM, WUG2700),
97 RUM_DEV(AMIT, CGWLUSB2GO),
98 RUM_DEV(ASUS, RT2573_1),
99 RUM_DEV(ASUS, RT2573_2),
100 RUM_DEV(BELKIN, F5D7050A),
101 RUM_DEV(BELKIN, F5D9050V3),
102 RUM_DEV(CISCOLINKSYS, WUSB54GC),
103 RUM_DEV(CISCOLINKSYS, WUSB54GR),
104 RUM_DEV(CONCEPTRONIC2, C54RU2),
105 RUM_DEV(COREGA, CGWLUSB2GL),
106 RUM_DEV(COREGA, CGWLUSB2GPX),
107 RUM_DEV(DICKSMITH, CWD854F),
108 RUM_DEV(DICKSMITH, RT2573),
109 RUM_DEV(EDIMAX, EW7318USG),
110 RUM_DEV(DLINK2, DWLG122C1),
111 RUM_DEV(DLINK2, WUA1340),
112 RUM_DEV(DLINK2, DWA111),
113 RUM_DEV(DLINK2, DWA110),
114 RUM_DEV(GIGABYTE, GNWB01GS),
115 RUM_DEV(GIGABYTE, GNWI05GS),
116 RUM_DEV(GIGASET, RT2573),
117 RUM_DEV(GOODWAY, RT2573),
118 RUM_DEV(GUILLEMOT, HWGUSB254LB),
119 RUM_DEV(GUILLEMOT, HWGUSB254V2AP),
120 RUM_DEV(HUAWEI3COM, WUB320G),
121 RUM_DEV(MELCO, G54HP),
122 RUM_DEV(MELCO, SG54HP),
123 RUM_DEV(MELCO, SG54HG),
124 RUM_DEV(MELCO, WLIUCG),
125 RUM_DEV(MELCO, WLRUCG),
126 RUM_DEV(MELCO, WLRUCGAOSS),
127 RUM_DEV(MSI, RT2573_1),
128 RUM_DEV(MSI, RT2573_2),
129 RUM_DEV(MSI, RT2573_3),
130 RUM_DEV(MSI, RT2573_4),
131 RUM_DEV(NOVATECH, RT2573),
132 RUM_DEV(PLANEX2, GWUS54HP),
133 RUM_DEV(PLANEX2, GWUS54MINI2),
134 RUM_DEV(PLANEX2, GWUSMM),
135 RUM_DEV(QCOM, RT2573),
136 RUM_DEV(QCOM, RT2573_2),
137 RUM_DEV(QCOM, RT2573_3),
138 RUM_DEV(RALINK, RT2573),
139 RUM_DEV(RALINK, RT2573_2),
140 RUM_DEV(RALINK, RT2671),
141 RUM_DEV(SITECOMEU, WL113R2),
142 RUM_DEV(SITECOMEU, WL172),
143 RUM_DEV(SPARKLAN, RT2573),
144 RUM_DEV(SURECOM, RT2573),
148 static device_probe_t rum_match;
149 static device_attach_t rum_attach;
150 static device_detach_t rum_detach;
152 static usb_callback_t rum_bulk_read_callback;
153 static usb_callback_t rum_bulk_write_callback;
155 static usb_error_t rum_do_request(struct rum_softc *sc,
156 struct usb_device_request *req, void *data);
157 static usb_error_t rum_do_mcu_request(struct rum_softc *sc, int);
158 static struct ieee80211vap *rum_vap_create(struct ieee80211com *,
159 const char [IFNAMSIZ], int, enum ieee80211_opmode,
160 int, const uint8_t [IEEE80211_ADDR_LEN],
161 const uint8_t [IEEE80211_ADDR_LEN]);
162 static void rum_vap_delete(struct ieee80211vap *);
163 static void rum_cmdq_cb(void *, int);
164 static int rum_cmd_sleepable(struct rum_softc *, const void *,
165 size_t, uint8_t, CMD_FUNC_PROTO);
166 static void rum_tx_free(struct rum_tx_data *, int);
167 static void rum_setup_tx_list(struct rum_softc *);
168 static void rum_reset_tx_list(struct rum_softc *,
169 struct ieee80211vap *);
170 static void rum_unsetup_tx_list(struct rum_softc *);
171 static void rum_beacon_miss(struct ieee80211vap *);
172 static void rum_sta_recv_mgmt(struct ieee80211_node *,
174 const struct ieee80211_rx_stats *, int, int);
175 static int rum_set_power_state(struct rum_softc *, int);
176 static int rum_newstate(struct ieee80211vap *,
177 enum ieee80211_state, int);
178 static uint8_t rum_crypto_mode(struct rum_softc *, u_int, int);
179 static void rum_setup_tx_desc(struct rum_softc *,
180 struct rum_tx_desc *, struct ieee80211_key *,
181 uint32_t, uint8_t, uint8_t, int, int, int);
182 static uint32_t rum_tx_crypto_flags(struct rum_softc *,
183 struct ieee80211_node *,
184 const struct ieee80211_key *);
185 static int rum_tx_mgt(struct rum_softc *, struct mbuf *,
186 struct ieee80211_node *);
187 static int rum_tx_raw(struct rum_softc *, struct mbuf *,
188 struct ieee80211_node *,
189 const struct ieee80211_bpf_params *);
190 static int rum_tx_data(struct rum_softc *, struct mbuf *,
191 struct ieee80211_node *);
192 static int rum_transmit(struct ieee80211com *, struct mbuf *);
193 static void rum_start(struct rum_softc *);
194 static void rum_parent(struct ieee80211com *);
195 static void rum_eeprom_read(struct rum_softc *, uint16_t, void *,
197 static uint32_t rum_read(struct rum_softc *, uint16_t);
198 static void rum_read_multi(struct rum_softc *, uint16_t, void *,
200 static usb_error_t rum_write(struct rum_softc *, uint16_t, uint32_t);
201 static usb_error_t rum_write_multi(struct rum_softc *, uint16_t, void *,
203 static usb_error_t rum_setbits(struct rum_softc *, uint16_t, uint32_t);
204 static usb_error_t rum_clrbits(struct rum_softc *, uint16_t, uint32_t);
205 static usb_error_t rum_modbits(struct rum_softc *, uint16_t, uint32_t,
207 static int rum_bbp_busy(struct rum_softc *);
208 static void rum_bbp_write(struct rum_softc *, uint8_t, uint8_t);
209 static uint8_t rum_bbp_read(struct rum_softc *, uint8_t);
210 static void rum_rf_write(struct rum_softc *, uint8_t, uint32_t);
211 static void rum_select_antenna(struct rum_softc *);
212 static void rum_enable_mrr(struct rum_softc *);
213 static void rum_set_txpreamble(struct rum_softc *);
214 static void rum_set_basicrates(struct rum_softc *);
215 static void rum_select_band(struct rum_softc *,
216 struct ieee80211_channel *);
217 static void rum_set_chan(struct rum_softc *,
218 struct ieee80211_channel *);
219 static void rum_set_maxretry(struct rum_softc *,
220 struct ieee80211vap *);
221 static int rum_enable_tsf_sync(struct rum_softc *);
222 static void rum_enable_tsf(struct rum_softc *);
223 static void rum_abort_tsf_sync(struct rum_softc *);
224 static void rum_get_tsf(struct rum_softc *, uint64_t *);
225 static void rum_update_slot_cb(struct rum_softc *,
226 union sec_param *, uint8_t);
227 static void rum_update_slot(struct ieee80211com *);
228 static int rum_wme_update(struct ieee80211com *);
229 static void rum_set_bssid(struct rum_softc *, const uint8_t *);
230 static void rum_set_macaddr(struct rum_softc *, const uint8_t *);
231 static void rum_update_mcast(struct ieee80211com *);
232 static void rum_update_promisc(struct ieee80211com *);
233 static void rum_setpromisc(struct rum_softc *);
234 static const char *rum_get_rf(int);
235 static void rum_read_eeprom(struct rum_softc *);
236 static int rum_bbp_wakeup(struct rum_softc *);
237 static int rum_bbp_init(struct rum_softc *);
238 static void rum_clr_shkey_regs(struct rum_softc *);
239 static int rum_init(struct rum_softc *);
240 static void rum_stop(struct rum_softc *);
241 static void rum_load_microcode(struct rum_softc *, const uint8_t *,
243 static int rum_set_sleep_time(struct rum_softc *, uint16_t);
244 static int rum_reset(struct ieee80211vap *, u_long);
245 static int rum_set_beacon(struct rum_softc *,
246 struct ieee80211vap *);
247 static int rum_alloc_beacon(struct rum_softc *,
248 struct ieee80211vap *);
249 static void rum_update_beacon_cb(struct rum_softc *,
250 union sec_param *, uint8_t);
251 static void rum_update_beacon(struct ieee80211vap *, int);
252 static int rum_common_key_set(struct rum_softc *,
253 struct ieee80211_key *, uint16_t);
254 static void rum_group_key_set_cb(struct rum_softc *,
255 union sec_param *, uint8_t);
256 static void rum_group_key_del_cb(struct rum_softc *,
257 union sec_param *, uint8_t);
258 static void rum_pair_key_set_cb(struct rum_softc *,
259 union sec_param *, uint8_t);
260 static void rum_pair_key_del_cb(struct rum_softc *,
261 union sec_param *, uint8_t);
262 static int rum_key_alloc(struct ieee80211vap *,
263 struct ieee80211_key *, ieee80211_keyix *,
265 static int rum_key_set(struct ieee80211vap *,
266 const struct ieee80211_key *);
267 static int rum_key_delete(struct ieee80211vap *,
268 const struct ieee80211_key *);
269 static int rum_raw_xmit(struct ieee80211_node *, struct mbuf *,
270 const struct ieee80211_bpf_params *);
271 static void rum_scan_start(struct ieee80211com *);
272 static void rum_scan_end(struct ieee80211com *);
273 static void rum_set_channel(struct ieee80211com *);
274 static void rum_getradiocaps(struct ieee80211com *, int, int *,
275 struct ieee80211_channel[]);
276 static int rum_get_rssi(struct rum_softc *, uint8_t);
277 static void rum_ratectl_start(struct rum_softc *,
278 struct ieee80211_node *);
279 static void rum_ratectl_timeout(void *);
280 static void rum_ratectl_task(void *, int);
281 static int rum_pause(struct rum_softc *, int);
283 static const struct {
287 { RT2573_TXRX_CSR0, 0x025fb032 },
288 { RT2573_TXRX_CSR1, 0x9eaa9eaf },
289 { RT2573_TXRX_CSR2, 0x8a8b8c8d },
290 { RT2573_TXRX_CSR3, 0x00858687 },
291 { RT2573_TXRX_CSR7, 0x2e31353b },
292 { RT2573_TXRX_CSR8, 0x2a2a2a2c },
293 { RT2573_TXRX_CSR15, 0x0000000f },
294 { RT2573_MAC_CSR6, 0x00000fff },
295 { RT2573_MAC_CSR8, 0x016c030a },
296 { RT2573_MAC_CSR10, 0x00000718 },
297 { RT2573_MAC_CSR12, 0x00000004 },
298 { RT2573_MAC_CSR13, 0x00007f00 },
299 { RT2573_SEC_CSR2, 0x00000000 },
300 { RT2573_SEC_CSR3, 0x00000000 },
301 { RT2573_SEC_CSR4, 0x00000000 },
302 { RT2573_PHY_CSR1, 0x000023b0 },
303 { RT2573_PHY_CSR5, 0x00040a06 },
304 { RT2573_PHY_CSR6, 0x00080606 },
305 { RT2573_PHY_CSR7, 0x00000408 },
306 { RT2573_AIFSN_CSR, 0x00002273 },
307 { RT2573_CWMIN_CSR, 0x00002344 },
308 { RT2573_CWMAX_CSR, 0x000034aa }
311 static const struct {
343 static const uint8_t rum_chan_2ghz[] =
344 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };
346 static const uint8_t rum_chan_5ghz[] =
347 { 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64,
348 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140,
349 149, 153, 157, 161, 165 };
351 static const struct rfprog {
353 uint32_t r1, r2, r3, r4;
355 { 1, 0x00b03, 0x001e1, 0x1a014, 0x30282 },
356 { 2, 0x00b03, 0x001e1, 0x1a014, 0x30287 },
357 { 3, 0x00b03, 0x001e2, 0x1a014, 0x30282 },
358 { 4, 0x00b03, 0x001e2, 0x1a014, 0x30287 },
359 { 5, 0x00b03, 0x001e3, 0x1a014, 0x30282 },
360 { 6, 0x00b03, 0x001e3, 0x1a014, 0x30287 },
361 { 7, 0x00b03, 0x001e4, 0x1a014, 0x30282 },
362 { 8, 0x00b03, 0x001e4, 0x1a014, 0x30287 },
363 { 9, 0x00b03, 0x001e5, 0x1a014, 0x30282 },
364 { 10, 0x00b03, 0x001e5, 0x1a014, 0x30287 },
365 { 11, 0x00b03, 0x001e6, 0x1a014, 0x30282 },
366 { 12, 0x00b03, 0x001e6, 0x1a014, 0x30287 },
367 { 13, 0x00b03, 0x001e7, 0x1a014, 0x30282 },
368 { 14, 0x00b03, 0x001e8, 0x1a014, 0x30284 },
370 { 34, 0x00b03, 0x20266, 0x36014, 0x30282 },
371 { 38, 0x00b03, 0x20267, 0x36014, 0x30284 },
372 { 42, 0x00b03, 0x20268, 0x36014, 0x30286 },
373 { 46, 0x00b03, 0x20269, 0x36014, 0x30288 },
375 { 36, 0x00b03, 0x00266, 0x26014, 0x30288 },
376 { 40, 0x00b03, 0x00268, 0x26014, 0x30280 },
377 { 44, 0x00b03, 0x00269, 0x26014, 0x30282 },
378 { 48, 0x00b03, 0x0026a, 0x26014, 0x30284 },
379 { 52, 0x00b03, 0x0026b, 0x26014, 0x30286 },
380 { 56, 0x00b03, 0x0026c, 0x26014, 0x30288 },
381 { 60, 0x00b03, 0x0026e, 0x26014, 0x30280 },
382 { 64, 0x00b03, 0x0026f, 0x26014, 0x30282 },
384 { 100, 0x00b03, 0x0028a, 0x2e014, 0x30280 },
385 { 104, 0x00b03, 0x0028b, 0x2e014, 0x30282 },
386 { 108, 0x00b03, 0x0028c, 0x2e014, 0x30284 },
387 { 112, 0x00b03, 0x0028d, 0x2e014, 0x30286 },
388 { 116, 0x00b03, 0x0028e, 0x2e014, 0x30288 },
389 { 120, 0x00b03, 0x002a0, 0x2e014, 0x30280 },
390 { 124, 0x00b03, 0x002a1, 0x2e014, 0x30282 },
391 { 128, 0x00b03, 0x002a2, 0x2e014, 0x30284 },
392 { 132, 0x00b03, 0x002a3, 0x2e014, 0x30286 },
393 { 136, 0x00b03, 0x002a4, 0x2e014, 0x30288 },
394 { 140, 0x00b03, 0x002a6, 0x2e014, 0x30280 },
396 { 149, 0x00b03, 0x002a8, 0x2e014, 0x30287 },
397 { 153, 0x00b03, 0x002a9, 0x2e014, 0x30289 },
398 { 157, 0x00b03, 0x002ab, 0x2e014, 0x30281 },
399 { 161, 0x00b03, 0x002ac, 0x2e014, 0x30283 },
400 { 165, 0x00b03, 0x002ad, 0x2e014, 0x30285 }
402 { 1, 0x00b33, 0x011e1, 0x1a014, 0x30282 },
403 { 2, 0x00b33, 0x011e1, 0x1a014, 0x30287 },
404 { 3, 0x00b33, 0x011e2, 0x1a014, 0x30282 },
405 { 4, 0x00b33, 0x011e2, 0x1a014, 0x30287 },
406 { 5, 0x00b33, 0x011e3, 0x1a014, 0x30282 },
407 { 6, 0x00b33, 0x011e3, 0x1a014, 0x30287 },
408 { 7, 0x00b33, 0x011e4, 0x1a014, 0x30282 },
409 { 8, 0x00b33, 0x011e4, 0x1a014, 0x30287 },
410 { 9, 0x00b33, 0x011e5, 0x1a014, 0x30282 },
411 { 10, 0x00b33, 0x011e5, 0x1a014, 0x30287 },
412 { 11, 0x00b33, 0x011e6, 0x1a014, 0x30282 },
413 { 12, 0x00b33, 0x011e6, 0x1a014, 0x30287 },
414 { 13, 0x00b33, 0x011e7, 0x1a014, 0x30282 },
415 { 14, 0x00b33, 0x011e8, 0x1a014, 0x30284 },
417 { 34, 0x00b33, 0x01266, 0x26014, 0x30282 },
418 { 38, 0x00b33, 0x01267, 0x26014, 0x30284 },
419 { 42, 0x00b33, 0x01268, 0x26014, 0x30286 },
420 { 46, 0x00b33, 0x01269, 0x26014, 0x30288 },
422 { 36, 0x00b33, 0x01266, 0x26014, 0x30288 },
423 { 40, 0x00b33, 0x01268, 0x26014, 0x30280 },
424 { 44, 0x00b33, 0x01269, 0x26014, 0x30282 },
425 { 48, 0x00b33, 0x0126a, 0x26014, 0x30284 },
426 { 52, 0x00b33, 0x0126b, 0x26014, 0x30286 },
427 { 56, 0x00b33, 0x0126c, 0x26014, 0x30288 },
428 { 60, 0x00b33, 0x0126e, 0x26014, 0x30280 },
429 { 64, 0x00b33, 0x0126f, 0x26014, 0x30282 },
431 { 100, 0x00b33, 0x0128a, 0x2e014, 0x30280 },
432 { 104, 0x00b33, 0x0128b, 0x2e014, 0x30282 },
433 { 108, 0x00b33, 0x0128c, 0x2e014, 0x30284 },
434 { 112, 0x00b33, 0x0128d, 0x2e014, 0x30286 },
435 { 116, 0x00b33, 0x0128e, 0x2e014, 0x30288 },
436 { 120, 0x00b33, 0x012a0, 0x2e014, 0x30280 },
437 { 124, 0x00b33, 0x012a1, 0x2e014, 0x30282 },
438 { 128, 0x00b33, 0x012a2, 0x2e014, 0x30284 },
439 { 132, 0x00b33, 0x012a3, 0x2e014, 0x30286 },
440 { 136, 0x00b33, 0x012a4, 0x2e014, 0x30288 },
441 { 140, 0x00b33, 0x012a6, 0x2e014, 0x30280 },
443 { 149, 0x00b33, 0x012a8, 0x2e014, 0x30287 },
444 { 153, 0x00b33, 0x012a9, 0x2e014, 0x30289 },
445 { 157, 0x00b33, 0x012ab, 0x2e014, 0x30281 },
446 { 161, 0x00b33, 0x012ac, 0x2e014, 0x30283 },
447 { 165, 0x00b33, 0x012ad, 0x2e014, 0x30285 }
450 static const struct usb_config rum_config[RUM_N_TRANSFER] = {
453 .endpoint = UE_ADDR_ANY,
454 .direction = UE_DIR_OUT,
455 .bufsize = (MCLBYTES + RT2573_TX_DESC_SIZE + 8),
456 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
457 .callback = rum_bulk_write_callback,
458 .timeout = 5000, /* ms */
462 .endpoint = UE_ADDR_ANY,
463 .direction = UE_DIR_IN,
464 .bufsize = (MCLBYTES + RT2573_RX_DESC_SIZE),
465 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
466 .callback = rum_bulk_read_callback,
471 rum_match(device_t self)
473 struct usb_attach_arg *uaa = device_get_ivars(self);
475 if (uaa->usb_mode != USB_MODE_HOST)
477 if (uaa->info.bConfigIndex != 0)
479 if (uaa->info.bIfaceIndex != RT2573_IFACE_INDEX)
482 return (usbd_lookup_id_by_uaa(rum_devs, sizeof(rum_devs), uaa));
486 rum_attach(device_t self)
488 struct usb_attach_arg *uaa = device_get_ivars(self);
489 struct rum_softc *sc = device_get_softc(self);
490 struct ieee80211com *ic = &sc->sc_ic;
495 device_set_usb_desc(self);
496 sc->sc_udev = uaa->device;
500 RUM_CMDQ_LOCK_INIT(sc);
501 mbufq_init(&sc->sc_snd, ifqmaxlen);
503 iface_index = RT2573_IFACE_INDEX;
504 error = usbd_transfer_setup(uaa->device, &iface_index,
505 sc->sc_xfer, rum_config, RUM_N_TRANSFER, sc, &sc->sc_mtx);
507 device_printf(self, "could not allocate USB transfers, "
508 "err=%s\n", usbd_errstr(error));
513 /* retrieve RT2573 rev. no */
514 for (ntries = 0; ntries < 100; ntries++) {
515 if ((tmp = rum_read(sc, RT2573_MAC_CSR0)) != 0)
517 if (rum_pause(sc, hz / 100))
521 device_printf(sc->sc_dev, "timeout waiting for chip to settle\n");
526 /* retrieve MAC address and various other things from EEPROM */
529 device_printf(sc->sc_dev, "MAC/BBP RT2573 (rev 0x%05x), RF %s\n",
530 tmp, rum_get_rf(sc->rf_rev));
532 rum_load_microcode(sc, rt2573_ucode, sizeof(rt2573_ucode));
536 ic->ic_name = device_get_nameunit(self);
537 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
539 /* set device capabilities */
541 IEEE80211_C_STA /* station mode supported */
542 | IEEE80211_C_IBSS /* IBSS mode supported */
543 | IEEE80211_C_MONITOR /* monitor mode supported */
544 | IEEE80211_C_HOSTAP /* HostAp mode supported */
545 | IEEE80211_C_AHDEMO /* adhoc demo mode */
546 | IEEE80211_C_TXPMGT /* tx power management */
547 | IEEE80211_C_SHPREAMBLE /* short preamble supported */
548 | IEEE80211_C_SHSLOT /* short slot time supported */
549 | IEEE80211_C_BGSCAN /* bg scanning supported */
550 | IEEE80211_C_WPA /* 802.11i */
551 | IEEE80211_C_WME /* 802.11e */
552 | IEEE80211_C_PMGT /* Station-side power mgmt */
553 | IEEE80211_C_SWSLEEP /* net80211 managed power mgmt */
557 IEEE80211_CRYPTO_WEP |
558 IEEE80211_CRYPTO_AES_CCM |
559 IEEE80211_CRYPTO_TKIPMIC |
560 IEEE80211_CRYPTO_TKIP;
562 rum_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
565 ieee80211_ifattach(ic);
566 ic->ic_update_promisc = rum_update_promisc;
567 ic->ic_raw_xmit = rum_raw_xmit;
568 ic->ic_scan_start = rum_scan_start;
569 ic->ic_scan_end = rum_scan_end;
570 ic->ic_set_channel = rum_set_channel;
571 ic->ic_getradiocaps = rum_getradiocaps;
572 ic->ic_transmit = rum_transmit;
573 ic->ic_parent = rum_parent;
574 ic->ic_vap_create = rum_vap_create;
575 ic->ic_vap_delete = rum_vap_delete;
576 ic->ic_updateslot = rum_update_slot;
577 ic->ic_wme.wme_update = rum_wme_update;
578 ic->ic_update_mcast = rum_update_mcast;
580 ieee80211_radiotap_attach(ic,
581 &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
582 RT2573_TX_RADIOTAP_PRESENT,
583 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
584 RT2573_RX_RADIOTAP_PRESENT);
586 TASK_INIT(&sc->cmdq_task, 0, rum_cmdq_cb, sc);
589 ieee80211_announce(ic);
595 return (ENXIO); /* failure */
599 rum_detach(device_t self)
601 struct rum_softc *sc = device_get_softc(self);
602 struct ieee80211com *ic = &sc->sc_ic;
604 /* Prevent further ioctls */
609 /* stop all USB transfers */
610 usbd_transfer_unsetup(sc->sc_xfer, RUM_N_TRANSFER);
612 /* free TX list, if any */
614 rum_unsetup_tx_list(sc);
617 if (ic->ic_softc == sc) {
618 ieee80211_draintask(ic, &sc->cmdq_task);
619 ieee80211_ifdetach(ic);
622 mbufq_drain(&sc->sc_snd);
623 RUM_CMDQ_LOCK_DESTROY(sc);
624 RUM_LOCK_DESTROY(sc);
630 rum_do_request(struct rum_softc *sc,
631 struct usb_device_request *req, void *data)
637 err = usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
638 req, data, 0, NULL, 250 /* ms */);
642 DPRINTFN(1, "Control request failed, %s (retrying)\n",
644 if (rum_pause(sc, hz / 100))
651 rum_do_mcu_request(struct rum_softc *sc, int request)
653 struct usb_device_request req;
655 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
656 req.bRequest = RT2573_MCU_CNTL;
657 USETW(req.wValue, request);
658 USETW(req.wIndex, 0);
659 USETW(req.wLength, 0);
661 return (rum_do_request(sc, &req, NULL));
664 static struct ieee80211vap *
665 rum_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
666 enum ieee80211_opmode opmode, int flags,
667 const uint8_t bssid[IEEE80211_ADDR_LEN],
668 const uint8_t mac[IEEE80211_ADDR_LEN])
670 struct rum_softc *sc = ic->ic_softc;
672 struct ieee80211vap *vap;
674 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */
676 rvp = malloc(sizeof(struct rum_vap), M_80211_VAP, M_WAITOK | M_ZERO);
678 /* enable s/w bmiss handling for sta mode */
680 if (ieee80211_vap_setup(ic, vap, name, unit, opmode,
681 flags | IEEE80211_CLONE_NOBEACONS, bssid) != 0) {
683 free(rvp, M_80211_VAP);
687 /* override state transition machine */
688 rvp->newstate = vap->iv_newstate;
689 vap->iv_newstate = rum_newstate;
690 vap->iv_key_alloc = rum_key_alloc;
691 vap->iv_key_set = rum_key_set;
692 vap->iv_key_delete = rum_key_delete;
693 vap->iv_update_beacon = rum_update_beacon;
694 vap->iv_reset = rum_reset;
695 vap->iv_max_aid = RT2573_ADDR_MAX;
697 if (opmode == IEEE80211_M_STA) {
699 * Move device to the sleep state when
700 * beacon is received and there is no data for us.
702 * Used only for IEEE80211_S_SLEEP state.
704 rvp->recv_mgmt = vap->iv_recv_mgmt;
705 vap->iv_recv_mgmt = rum_sta_recv_mgmt;
707 /* Ignored while sleeping. */
708 rvp->bmiss = vap->iv_bmiss;
709 vap->iv_bmiss = rum_beacon_miss;
712 usb_callout_init_mtx(&rvp->ratectl_ch, &sc->sc_mtx, 0);
713 TASK_INIT(&rvp->ratectl_task, 0, rum_ratectl_task, rvp);
714 ieee80211_ratectl_init(vap);
715 ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */);
717 ieee80211_vap_attach(vap, ieee80211_media_change,
718 ieee80211_media_status, mac);
719 ic->ic_opmode = opmode;
724 rum_vap_delete(struct ieee80211vap *vap)
726 struct rum_vap *rvp = RUM_VAP(vap);
727 struct ieee80211com *ic = vap->iv_ic;
728 struct rum_softc *sc = ic->ic_softc;
730 /* Put vap into INIT state. */
731 ieee80211_new_state(vap, IEEE80211_S_INIT, -1);
732 ieee80211_draintask(ic, &vap->iv_nstate_task);
735 /* Cancel any unfinished Tx. */
736 rum_reset_tx_list(sc, vap);
739 usb_callout_drain(&rvp->ratectl_ch);
740 ieee80211_draintask(ic, &rvp->ratectl_task);
741 ieee80211_ratectl_deinit(vap);
742 ieee80211_vap_detach(vap);
743 m_freem(rvp->bcn_mbuf);
744 free(rvp, M_80211_VAP);
748 rum_cmdq_cb(void *arg, int pending)
750 struct rum_softc *sc = arg;
754 while (sc->cmdq[sc->cmdq_first].func != NULL) {
755 rc = &sc->cmdq[sc->cmdq_first];
759 rc->func(sc, &rc->data, rc->rvp_id);
763 memset(rc, 0, sizeof (*rc));
764 sc->cmdq_first = (sc->cmdq_first + 1) % RUM_CMDQ_SIZE;
770 rum_cmd_sleepable(struct rum_softc *sc, const void *ptr, size_t len,
771 uint8_t rvp_id, CMD_FUNC_PROTO)
773 struct ieee80211com *ic = &sc->sc_ic;
775 KASSERT(len <= sizeof(union sec_param), ("buffer overflow"));
778 if (sc->cmdq[sc->cmdq_last].func != NULL) {
779 device_printf(sc->sc_dev, "%s: cmdq overflow\n", __func__);
786 memcpy(&sc->cmdq[sc->cmdq_last].data, ptr, len);
787 sc->cmdq[sc->cmdq_last].rvp_id = rvp_id;
788 sc->cmdq[sc->cmdq_last].func = func;
789 sc->cmdq_last = (sc->cmdq_last + 1) % RUM_CMDQ_SIZE;
792 ieee80211_runtask(ic, &sc->cmdq_task);
798 rum_tx_free(struct rum_tx_data *data, int txerr)
800 struct rum_softc *sc = data->sc;
802 if (data->m != NULL) {
803 ieee80211_tx_complete(data->ni, data->m, txerr);
807 STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
812 rum_setup_tx_list(struct rum_softc *sc)
814 struct rum_tx_data *data;
818 STAILQ_INIT(&sc->tx_q);
819 STAILQ_INIT(&sc->tx_free);
821 for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
822 data = &sc->tx_data[i];
825 STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
831 rum_reset_tx_list(struct rum_softc *sc, struct ieee80211vap *vap)
833 struct rum_tx_data *data, *tmp;
835 KASSERT(vap != NULL, ("%s: vap is NULL\n", __func__));
837 STAILQ_FOREACH_SAFE(data, &sc->tx_q, next, tmp) {
838 if (data->ni != NULL && data->ni->ni_vap == vap) {
839 ieee80211_free_node(data->ni);
842 KASSERT(data->m != NULL, ("%s: m is NULL\n",
847 STAILQ_REMOVE(&sc->tx_q, data, rum_tx_data, next);
848 STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
855 rum_unsetup_tx_list(struct rum_softc *sc)
857 struct rum_tx_data *data;
860 /* make sure any subsequent use of the queues will fail */
862 STAILQ_INIT(&sc->tx_q);
863 STAILQ_INIT(&sc->tx_free);
865 /* free up all node references and mbufs */
866 for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
867 data = &sc->tx_data[i];
869 if (data->m != NULL) {
873 if (data->ni != NULL) {
874 ieee80211_free_node(data->ni);
881 rum_beacon_miss(struct ieee80211vap *vap)
883 struct ieee80211com *ic = vap->iv_ic;
884 struct rum_softc *sc = ic->ic_softc;
885 struct rum_vap *rvp = RUM_VAP(vap);
889 if (sc->sc_sleeping && sc->sc_sleep_end < ticks) {
890 DPRINTFN(12, "dropping 'sleeping' bit, "
891 "device must be awake now\n");
896 sleep = sc->sc_sleeping;
903 DPRINTFN(13, "bmiss event is ignored whilst sleeping\n");
908 rum_sta_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m, int subtype,
909 const struct ieee80211_rx_stats *rxs,
912 struct ieee80211vap *vap = ni->ni_vap;
913 struct rum_softc *sc = vap->iv_ic->ic_softc;
914 struct rum_vap *rvp = RUM_VAP(vap);
916 if (vap->iv_state == IEEE80211_S_SLEEP &&
917 subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
919 DPRINTFN(12, "beacon, mybss %d (flags %02X)\n",
920 !!(sc->last_rx_flags & RT2573_RX_MYBSS),
923 if ((sc->last_rx_flags & (RT2573_RX_MYBSS | RT2573_RX_BC)) ==
924 (RT2573_RX_MYBSS | RT2573_RX_BC)) {
926 * Put it to sleep here; in case if there is a data
927 * for us, iv_recv_mgmt() will wakeup the device via
928 * SLEEP -> RUN state transition.
930 rum_set_power_state(sc, 1);
935 rvp->recv_mgmt(ni, m, subtype, rxs, rssi, nf);
939 rum_set_power_state(struct rum_softc *sc, int sleep)
945 DPRINTFN(12, "moving to %s state (sleep time %u)\n",
946 sleep ? "sleep" : "awake", sc->sc_sleep_time);
948 uerror = rum_do_mcu_request(sc,
949 sleep ? RT2573_MCU_SLEEP : RT2573_MCU_WAKEUP);
950 if (uerror != USB_ERR_NORMAL_COMPLETION) {
951 device_printf(sc->sc_dev,
952 "%s: could not change power state: %s\n",
953 __func__, usbd_errstr(uerror));
957 sc->sc_sleeping = !!sleep;
958 sc->sc_sleep_end = sleep ? ticks + sc->sc_sleep_time : 0;
964 rum_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
966 struct rum_vap *rvp = RUM_VAP(vap);
967 struct ieee80211com *ic = vap->iv_ic;
968 struct rum_softc *sc = ic->ic_softc;
969 const struct ieee80211_txparam *tp;
970 enum ieee80211_state ostate;
971 struct ieee80211_node *ni;
975 ostate = vap->iv_state;
976 DPRINTF("%s -> %s\n",
977 ieee80211_state_name[ostate],
978 ieee80211_state_name[nstate]);
980 IEEE80211_UNLOCK(ic);
982 usb_callout_stop(&rvp->ratectl_ch);
984 if (ostate == IEEE80211_S_SLEEP && vap->iv_opmode == IEEE80211_M_STA) {
985 rum_clrbits(sc, RT2573_TXRX_CSR4, RT2573_ACKCTS_PWRMGT);
986 rum_clrbits(sc, RT2573_MAC_CSR11, RT2573_AUTO_WAKEUP);
990 * any subsequent TX will wakeup it anyway
992 (void) rum_set_power_state(sc, 0);
996 case IEEE80211_S_INIT:
997 if (ostate == IEEE80211_S_RUN)
998 rum_abort_tsf_sync(sc);
1002 case IEEE80211_S_RUN:
1003 if (ostate == IEEE80211_S_SLEEP)
1004 break; /* already handled */
1006 ni = ieee80211_ref_node(vap->iv_bss);
1008 if (vap->iv_opmode != IEEE80211_M_MONITOR) {
1009 if (ic->ic_bsschan == IEEE80211_CHAN_ANYC ||
1010 ni->ni_chan == IEEE80211_CHAN_ANYC) {
1014 rum_update_slot_cb(sc, NULL, 0);
1016 rum_set_txpreamble(sc);
1017 rum_set_basicrates(sc);
1018 rum_set_maxretry(sc, vap);
1019 IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid);
1020 rum_set_bssid(sc, sc->sc_bssid);
1023 if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
1024 vap->iv_opmode == IEEE80211_M_IBSS) {
1025 if ((ret = rum_alloc_beacon(sc, vap)) != 0)
1029 if (vap->iv_opmode != IEEE80211_M_MONITOR &&
1030 vap->iv_opmode != IEEE80211_M_AHDEMO) {
1031 if ((ret = rum_enable_tsf_sync(sc)) != 0)
1036 /* enable automatic rate adaptation */
1037 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1038 if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
1039 rum_ratectl_start(sc, ni);
1041 ieee80211_free_node(ni);
1043 case IEEE80211_S_SLEEP:
1044 /* Implemented for STA mode only. */
1045 if (vap->iv_opmode != IEEE80211_M_STA)
1048 uerror = rum_setbits(sc, RT2573_MAC_CSR11, RT2573_AUTO_WAKEUP);
1049 if (uerror != USB_ERR_NORMAL_COMPLETION) {
1054 uerror = rum_setbits(sc, RT2573_TXRX_CSR4, RT2573_ACKCTS_PWRMGT);
1055 if (uerror != USB_ERR_NORMAL_COMPLETION) {
1060 ret = rum_set_power_state(sc, 1);
1062 device_printf(sc->sc_dev,
1063 "%s: could not move to the SLEEP state: %s\n",
1064 __func__, usbd_errstr(uerror));
1072 return (ret == 0 ? rvp->newstate(vap, nstate, arg) : ret);
1076 rum_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
1078 struct rum_softc *sc = usbd_xfer_softc(xfer);
1079 struct ieee80211vap *vap;
1080 struct rum_tx_data *data;
1082 struct usb_page_cache *pc;
1086 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
1088 switch (USB_GET_STATE(xfer)) {
1089 case USB_ST_TRANSFERRED:
1090 DPRINTFN(11, "transfer complete, %d bytes\n", actlen);
1092 /* free resources */
1093 data = usbd_xfer_get_priv(xfer);
1094 rum_tx_free(data, 0);
1095 usbd_xfer_set_priv(xfer, NULL);
1100 data = STAILQ_FIRST(&sc->tx_q);
1102 STAILQ_REMOVE_HEAD(&sc->tx_q, next);
1105 if (m->m_pkthdr.len > (int)(MCLBYTES + RT2573_TX_DESC_SIZE)) {
1106 DPRINTFN(0, "data overflow, %u bytes\n",
1108 m->m_pkthdr.len = (MCLBYTES + RT2573_TX_DESC_SIZE);
1110 pc = usbd_xfer_get_frame(xfer, 0);
1111 usbd_copy_in(pc, 0, &data->desc, RT2573_TX_DESC_SIZE);
1112 usbd_m_copy_in(pc, RT2573_TX_DESC_SIZE, m, 0,
1115 vap = data->ni->ni_vap;
1116 if (ieee80211_radiotap_active_vap(vap)) {
1117 struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
1120 tap->wt_rate = data->rate;
1121 tap->wt_antenna = sc->tx_ant;
1123 ieee80211_radiotap_tx(vap, m);
1126 /* align end on a 4-bytes boundary */
1127 len = (RT2573_TX_DESC_SIZE + m->m_pkthdr.len + 3) & ~3;
1128 if ((len % 64) == 0)
1131 DPRINTFN(11, "sending frame len=%u xferlen=%u\n",
1132 m->m_pkthdr.len, len);
1134 usbd_xfer_set_frame_len(xfer, 0, len);
1135 usbd_xfer_set_priv(xfer, data);
1137 usbd_transfer_submit(xfer);
1142 default: /* Error */
1143 DPRINTFN(11, "transfer error, %s\n",
1144 usbd_errstr(error));
1146 counter_u64_add(sc->sc_ic.ic_oerrors, 1);
1147 data = usbd_xfer_get_priv(xfer);
1149 rum_tx_free(data, error);
1150 usbd_xfer_set_priv(xfer, NULL);
1153 if (error != USB_ERR_CANCELLED) {
1154 if (error == USB_ERR_TIMEOUT)
1155 device_printf(sc->sc_dev, "device timeout\n");
1158 * Try to clear stall first, also if other
1159 * errors occur, hence clearing stall
1160 * introduces a 50 ms delay:
1162 usbd_xfer_set_stall(xfer);
1170 rum_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
1172 struct rum_softc *sc = usbd_xfer_softc(xfer);
1173 struct ieee80211com *ic = &sc->sc_ic;
1174 struct ieee80211_frame_min *wh;
1175 struct ieee80211_node *ni;
1176 struct mbuf *m = NULL;
1177 struct usb_page_cache *pc;
1182 usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
1184 switch (USB_GET_STATE(xfer)) {
1185 case USB_ST_TRANSFERRED:
1187 DPRINTFN(15, "rx done, actlen=%d\n", len);
1189 if (len < RT2573_RX_DESC_SIZE) {
1190 DPRINTF("%s: xfer too short %d\n",
1191 device_get_nameunit(sc->sc_dev), len);
1192 counter_u64_add(ic->ic_ierrors, 1);
1196 len -= RT2573_RX_DESC_SIZE;
1197 pc = usbd_xfer_get_frame(xfer, 0);
1198 usbd_copy_out(pc, 0, &sc->sc_rx_desc, RT2573_RX_DESC_SIZE);
1200 rssi = rum_get_rssi(sc, sc->sc_rx_desc.rssi);
1201 flags = le32toh(sc->sc_rx_desc.flags);
1202 sc->last_rx_flags = flags;
1203 if (len < ((flags >> 16) & 0xfff)) {
1204 DPRINTFN(5, "%s: frame is truncated from %d to %d "
1205 "bytes\n", device_get_nameunit(sc->sc_dev),
1206 (flags >> 16) & 0xfff, len);
1207 counter_u64_add(ic->ic_ierrors, 1);
1210 len = (flags >> 16) & 0xfff;
1211 if (len < sizeof(struct ieee80211_frame_ack)) {
1212 DPRINTFN(5, "%s: frame too short %d\n",
1213 device_get_nameunit(sc->sc_dev), len);
1214 counter_u64_add(ic->ic_ierrors, 1);
1217 if (flags & RT2573_RX_CRC_ERROR) {
1219 * This should not happen since we did not
1220 * request to receive those frames when we
1221 * filled RUM_TXRX_CSR2:
1223 DPRINTFN(5, "PHY or CRC error\n");
1224 counter_u64_add(ic->ic_ierrors, 1);
1227 if ((flags & RT2573_RX_DEC_MASK) != RT2573_RX_DEC_OK) {
1228 switch (flags & RT2573_RX_DEC_MASK) {
1229 case RT2573_RX_IV_ERROR:
1230 DPRINTFN(5, "IV/EIV error\n");
1232 case RT2573_RX_MIC_ERROR:
1233 DPRINTFN(5, "MIC error\n");
1235 case RT2573_RX_KEY_ERROR:
1236 DPRINTFN(5, "Key error\n");
1239 counter_u64_add(ic->ic_ierrors, 1);
1243 m = m_get2(len, M_NOWAIT, MT_DATA, M_PKTHDR);
1245 DPRINTF("could not allocate mbuf\n");
1246 counter_u64_add(ic->ic_ierrors, 1);
1249 usbd_copy_out(pc, RT2573_RX_DESC_SIZE,
1250 mtod(m, uint8_t *), len);
1252 wh = mtod(m, struct ieee80211_frame_min *);
1254 if ((wh->i_fc[1] & IEEE80211_FC1_PROTECTED) &&
1255 (flags & RT2573_RX_CIP_MASK) !=
1256 RT2573_RX_CIP_MODE(RT2573_MODE_NOSEC)) {
1257 wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
1258 m->m_flags |= M_WEP;
1262 m->m_pkthdr.len = m->m_len = len;
1264 if (ieee80211_radiotap_active(ic)) {
1265 struct rum_rx_radiotap_header *tap = &sc->sc_rxtap;
1268 tap->wr_rate = ieee80211_plcp2rate(sc->sc_rx_desc.rate,
1269 (flags & RT2573_RX_OFDM) ?
1270 IEEE80211_T_OFDM : IEEE80211_T_CCK);
1271 rum_get_tsf(sc, &tap->wr_tsf);
1272 tap->wr_antsignal = RT2573_NOISE_FLOOR + rssi;
1273 tap->wr_antnoise = RT2573_NOISE_FLOOR;
1274 tap->wr_antenna = sc->rx_ant;
1279 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1280 usbd_transfer_submit(xfer);
1283 * At the end of a USB callback it is always safe to unlock
1284 * the private mutex of a device! That is why we do the
1285 * "ieee80211_input" here, and not some lines up!
1289 if (m->m_len >= sizeof(struct ieee80211_frame_min))
1290 ni = ieee80211_find_rxnode(ic, wh);
1295 (void) ieee80211_input(ni, m, rssi,
1296 RT2573_NOISE_FLOOR);
1297 ieee80211_free_node(ni);
1299 (void) ieee80211_input_all(ic, m, rssi,
1300 RT2573_NOISE_FLOOR);
1306 default: /* Error */
1307 if (error != USB_ERR_CANCELLED) {
1308 /* try to clear stall first */
1309 usbd_xfer_set_stall(xfer);
1317 rum_plcp_signal(int rate)
1320 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1321 case 12: return 0xb;
1322 case 18: return 0xf;
1323 case 24: return 0xa;
1324 case 36: return 0xe;
1325 case 48: return 0x9;
1326 case 72: return 0xd;
1327 case 96: return 0x8;
1328 case 108: return 0xc;
1330 /* CCK rates (NB: not IEEE std, device-specific) */
1333 case 11: return 0x2;
1334 case 22: return 0x3;
1336 return 0xff; /* XXX unsupported/unknown rate */
1340 * Map net80211 cipher to RT2573 security mode.
1343 rum_crypto_mode(struct rum_softc *sc, u_int cipher, int keylen)
1346 case IEEE80211_CIPHER_WEP:
1347 return (keylen < 8 ? RT2573_MODE_WEP40 : RT2573_MODE_WEP104);
1348 case IEEE80211_CIPHER_TKIP:
1349 return RT2573_MODE_TKIP;
1350 case IEEE80211_CIPHER_AES_CCM:
1351 return RT2573_MODE_AES_CCMP;
1353 device_printf(sc->sc_dev, "unknown cipher %d\n", cipher);
1359 rum_setup_tx_desc(struct rum_softc *sc, struct rum_tx_desc *desc,
1360 struct ieee80211_key *k, uint32_t flags, uint8_t xflags, uint8_t qid,
1361 int hdrlen, int len, int rate)
1363 struct ieee80211com *ic = &sc->sc_ic;
1364 struct wmeParams *wmep = &sc->wme_params[qid];
1365 uint16_t plcp_length;
1368 flags |= RT2573_TX_VALID;
1371 if (k != NULL && !(k->wk_flags & IEEE80211_KEY_SWCRYPT)) {
1372 const struct ieee80211_cipher *cip = k->wk_cipher;
1374 len += cip->ic_header + cip->ic_trailer + cip->ic_miclen;
1376 desc->eiv = 0; /* for WEP */
1377 cip->ic_setiv(k, (uint8_t *)&desc->iv);
1380 /* setup PLCP fields */
1381 desc->plcp_signal = rum_plcp_signal(rate);
1382 desc->plcp_service = 4;
1384 len += IEEE80211_CRC_LEN;
1385 if (ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) {
1386 flags |= RT2573_TX_OFDM;
1388 plcp_length = len & 0xfff;
1389 desc->plcp_length_hi = plcp_length >> 6;
1390 desc->plcp_length_lo = plcp_length & 0x3f;
1393 rate = 2; /* avoid division by zero */
1394 plcp_length = howmany(16 * len, rate);
1396 remainder = (16 * len) % 22;
1397 if (remainder != 0 && remainder < 7)
1398 desc->plcp_service |= RT2573_PLCP_LENGEXT;
1400 desc->plcp_length_hi = plcp_length >> 8;
1401 desc->plcp_length_lo = plcp_length & 0xff;
1403 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1404 desc->plcp_signal |= 0x08;
1407 desc->flags = htole32(flags);
1408 desc->hdrlen = hdrlen;
1409 desc->xflags = xflags;
1411 desc->wme = htole16(RT2573_QID(qid) |
1412 RT2573_AIFSN(wmep->wmep_aifsn) |
1413 RT2573_LOGCWMIN(wmep->wmep_logcwmin) |
1414 RT2573_LOGCWMAX(wmep->wmep_logcwmax));
1418 rum_sendprot(struct rum_softc *sc,
1419 const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate)
1421 struct ieee80211com *ic = ni->ni_ic;
1422 const struct ieee80211_frame *wh;
1423 struct rum_tx_data *data;
1425 int protrate, pktlen, flags, isshort;
1428 RUM_LOCK_ASSERT(sc);
1429 KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY,
1430 ("protection %d", prot));
1432 wh = mtod(m, const struct ieee80211_frame *);
1433 pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
1435 protrate = ieee80211_ctl_rate(ic->ic_rt, rate);
1437 isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0;
1438 dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort)
1439 + ieee80211_ack_duration(ic->ic_rt, rate, isshort);
1441 if (prot == IEEE80211_PROT_RTSCTS) {
1442 /* NB: CTS is the same size as an ACK */
1443 dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort);
1444 flags |= RT2573_TX_NEED_ACK;
1445 mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur);
1447 mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur);
1449 if (mprot == NULL) {
1450 /* XXX stat + msg */
1453 data = STAILQ_FIRST(&sc->tx_free);
1454 STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1458 data->ni = ieee80211_ref_node(ni);
1459 data->rate = protrate;
1460 rum_setup_tx_desc(sc, &data->desc, NULL, flags, 0, 0, 0,
1461 mprot->m_pkthdr.len, protrate);
1463 STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1464 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1470 rum_tx_crypto_flags(struct rum_softc *sc, struct ieee80211_node *ni,
1471 const struct ieee80211_key *k)
1473 struct ieee80211vap *vap = ni->ni_vap;
1478 if (!(k->wk_flags & IEEE80211_KEY_SWCRYPT)) {
1479 cipher = k->wk_cipher->ic_cipher;
1481 mode = rum_crypto_mode(sc, cipher, k->wk_keylen);
1485 flags |= RT2573_TX_CIP_MODE(mode);
1487 /* Do not trust GROUP flag */
1488 if (!(k >= &vap->iv_nw_keys[0] &&
1489 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID]))
1490 flags |= RT2573_TX_KEY_PAIR;
1492 pos += 0 * RT2573_SKEY_MAX; /* vap id */
1494 flags |= RT2573_TX_KEY_ID(pos);
1496 if (cipher == IEEE80211_CIPHER_TKIP)
1497 flags |= RT2573_TX_TKIPMIC;
1504 rum_tx_mgt(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1506 const struct ieee80211_txparam *tp = ni->ni_txparms;
1507 struct ieee80211com *ic = &sc->sc_ic;
1508 struct rum_tx_data *data;
1509 struct ieee80211_frame *wh;
1510 struct ieee80211_key *k = NULL;
1513 uint8_t ac, type, xflags = 0;
1516 RUM_LOCK_ASSERT(sc);
1518 data = STAILQ_FIRST(&sc->tx_free);
1519 STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1522 wh = mtod(m0, struct ieee80211_frame *);
1523 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1524 hdrlen = ieee80211_anyhdrsize(wh);
1525 ac = M_WME_GETAC(m0);
1527 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1528 k = ieee80211_crypto_get_txkey(ni, m0);
1532 if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) &&
1533 !k->wk_cipher->ic_encap(k, m0))
1536 wh = mtod(m0, struct ieee80211_frame *);
1539 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1540 flags |= RT2573_TX_NEED_ACK;
1542 dur = ieee80211_ack_duration(ic->ic_rt, tp->mgmtrate,
1543 ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1544 USETW(wh->i_dur, dur);
1546 /* tell hardware to add timestamp for probe responses */
1547 if (type == IEEE80211_FC0_TYPE_MGT &&
1548 (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
1549 IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1550 flags |= RT2573_TX_TIMESTAMP;
1553 if (type != IEEE80211_FC0_TYPE_CTL && !IEEE80211_QOS_HAS_SEQ(wh))
1554 xflags |= RT2573_TX_HWSEQ;
1557 flags |= rum_tx_crypto_flags(sc, ni, k);
1561 data->rate = tp->mgmtrate;
1563 rum_setup_tx_desc(sc, &data->desc, k, flags, xflags, ac, hdrlen,
1564 m0->m_pkthdr.len, tp->mgmtrate);
1566 DPRINTFN(10, "sending mgt frame len=%d rate=%d\n",
1567 m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, tp->mgmtrate);
1569 STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1570 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1576 rum_tx_raw(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
1577 const struct ieee80211_bpf_params *params)
1579 struct ieee80211com *ic = ni->ni_ic;
1580 struct ieee80211_frame *wh;
1581 struct rum_tx_data *data;
1583 uint8_t ac, type, xflags = 0;
1586 RUM_LOCK_ASSERT(sc);
1588 wh = mtod(m0, struct ieee80211_frame *);
1589 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1591 ac = params->ibp_pri & 3;
1593 rate = params->ibp_rate0;
1594 if (!ieee80211_isratevalid(ic->ic_rt, rate))
1598 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
1599 flags |= RT2573_TX_NEED_ACK;
1600 if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) {
1601 error = rum_sendprot(sc, m0, ni,
1602 params->ibp_flags & IEEE80211_BPF_RTS ?
1603 IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY,
1605 if (error || sc->tx_nfree == 0)
1608 flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
1611 if (type != IEEE80211_FC0_TYPE_CTL && !IEEE80211_QOS_HAS_SEQ(wh))
1612 xflags |= RT2573_TX_HWSEQ;
1614 data = STAILQ_FIRST(&sc->tx_free);
1615 STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1622 /* XXX need to setup descriptor ourself */
1623 rum_setup_tx_desc(sc, &data->desc, NULL, flags, xflags, ac, 0,
1624 m0->m_pkthdr.len, rate);
1626 DPRINTFN(10, "sending raw frame len=%u rate=%u\n",
1627 m0->m_pkthdr.len, rate);
1629 STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1630 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1636 rum_tx_data(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1638 struct ieee80211vap *vap = ni->ni_vap;
1639 struct ieee80211com *ic = &sc->sc_ic;
1640 struct rum_tx_data *data;
1641 struct ieee80211_frame *wh;
1642 const struct ieee80211_txparam *tp = ni->ni_txparms;
1643 struct ieee80211_key *k = NULL;
1646 uint8_t ac, type, qos, xflags = 0;
1647 int error, hdrlen, rate;
1649 RUM_LOCK_ASSERT(sc);
1651 wh = mtod(m0, struct ieee80211_frame *);
1652 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1653 hdrlen = ieee80211_anyhdrsize(wh);
1655 if (IEEE80211_QOS_HAS_SEQ(wh))
1656 qos = ((const struct ieee80211_qosframe *)wh)->i_qos[0];
1659 ac = M_WME_GETAC(m0);
1661 if (m0->m_flags & M_EAPOL)
1662 rate = tp->mgmtrate;
1663 else if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1664 rate = tp->mcastrate;
1665 else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
1666 rate = tp->ucastrate;
1668 (void) ieee80211_ratectl_rate(ni, NULL, 0);
1669 rate = ni->ni_txrate;
1672 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1673 k = ieee80211_crypto_get_txkey(ni, m0);
1678 if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) &&
1679 !k->wk_cipher->ic_encap(k, m0)) {
1684 /* packet header may have moved, reset our local pointer */
1685 wh = mtod(m0, struct ieee80211_frame *);
1688 if (type != IEEE80211_FC0_TYPE_CTL && !IEEE80211_QOS_HAS_SEQ(wh))
1689 xflags |= RT2573_TX_HWSEQ;
1691 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1692 int prot = IEEE80211_PROT_NONE;
1693 if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold)
1694 prot = IEEE80211_PROT_RTSCTS;
1695 else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
1696 ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM)
1697 prot = ic->ic_protmode;
1698 if (prot != IEEE80211_PROT_NONE) {
1699 error = rum_sendprot(sc, m0, ni, prot, rate);
1700 if (error || sc->tx_nfree == 0) {
1704 flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
1709 flags |= rum_tx_crypto_flags(sc, ni, k);
1711 data = STAILQ_FIRST(&sc->tx_free);
1712 STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1719 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1720 /* Unicast frame, check if an ACK is expected. */
1721 if (!qos || (qos & IEEE80211_QOS_ACKPOLICY) !=
1722 IEEE80211_QOS_ACKPOLICY_NOACK)
1723 flags |= RT2573_TX_NEED_ACK;
1725 dur = ieee80211_ack_duration(ic->ic_rt, rate,
1726 ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1727 USETW(wh->i_dur, dur);
1730 rum_setup_tx_desc(sc, &data->desc, k, flags, xflags, ac, hdrlen,
1731 m0->m_pkthdr.len, rate);
1733 DPRINTFN(10, "sending frame len=%d rate=%d\n",
1734 m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, rate);
1736 STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1737 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1743 rum_transmit(struct ieee80211com *ic, struct mbuf *m)
1745 struct rum_softc *sc = ic->ic_softc;
1749 if (!sc->sc_running) {
1753 error = mbufq_enqueue(&sc->sc_snd, m);
1765 rum_start(struct rum_softc *sc)
1767 struct ieee80211_node *ni;
1770 RUM_LOCK_ASSERT(sc);
1772 if (!sc->sc_running)
1775 while (sc->tx_nfree >= RUM_TX_MINFREE &&
1776 (m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
1777 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1778 if (rum_tx_data(sc, m, ni) != 0) {
1779 if_inc_counter(ni->ni_vap->iv_ifp,
1780 IFCOUNTER_OERRORS, 1);
1781 ieee80211_free_node(ni);
1788 rum_parent(struct ieee80211com *ic)
1790 struct rum_softc *sc = ic->ic_softc;
1791 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1794 if (sc->sc_detached) {
1800 if (ic->ic_nrunning > 0) {
1801 if (rum_init(sc) == 0)
1802 ieee80211_start_all(ic);
1804 ieee80211_stop(vap);
1810 rum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len)
1812 struct usb_device_request req;
1815 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1816 req.bRequest = RT2573_READ_EEPROM;
1817 USETW(req.wValue, 0);
1818 USETW(req.wIndex, addr);
1819 USETW(req.wLength, len);
1821 error = rum_do_request(sc, &req, buf);
1823 device_printf(sc->sc_dev, "could not read EEPROM: %s\n",
1824 usbd_errstr(error));
1829 rum_read(struct rum_softc *sc, uint16_t reg)
1833 rum_read_multi(sc, reg, &val, sizeof val);
1835 return le32toh(val);
1839 rum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len)
1841 struct usb_device_request req;
1844 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1845 req.bRequest = RT2573_READ_MULTI_MAC;
1846 USETW(req.wValue, 0);
1847 USETW(req.wIndex, reg);
1848 USETW(req.wLength, len);
1850 error = rum_do_request(sc, &req, buf);
1852 device_printf(sc->sc_dev,
1853 "could not multi read MAC register: %s\n",
1854 usbd_errstr(error));
1859 rum_write(struct rum_softc *sc, uint16_t reg, uint32_t val)
1861 uint32_t tmp = htole32(val);
1863 return (rum_write_multi(sc, reg, &tmp, sizeof tmp));
1867 rum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len)
1869 struct usb_device_request req;
1873 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1874 req.bRequest = RT2573_WRITE_MULTI_MAC;
1875 USETW(req.wValue, 0);
1877 /* write at most 64 bytes at a time */
1878 for (offset = 0; offset < len; offset += 64) {
1879 USETW(req.wIndex, reg + offset);
1880 USETW(req.wLength, MIN(len - offset, 64));
1882 error = rum_do_request(sc, &req, (char *)buf + offset);
1884 device_printf(sc->sc_dev,
1885 "could not multi write MAC register: %s\n",
1886 usbd_errstr(error));
1891 return (USB_ERR_NORMAL_COMPLETION);
1895 rum_setbits(struct rum_softc *sc, uint16_t reg, uint32_t mask)
1897 return (rum_write(sc, reg, rum_read(sc, reg) | mask));
1901 rum_clrbits(struct rum_softc *sc, uint16_t reg, uint32_t mask)
1903 return (rum_write(sc, reg, rum_read(sc, reg) & ~mask));
1907 rum_modbits(struct rum_softc *sc, uint16_t reg, uint32_t set, uint32_t unset)
1909 return (rum_write(sc, reg, (rum_read(sc, reg) & ~unset) | set));
1913 rum_bbp_busy(struct rum_softc *sc)
1917 for (ntries = 0; ntries < 100; ntries++) {
1918 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1920 if (rum_pause(sc, hz / 100))
1930 rum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val)
1934 DPRINTFN(2, "reg=0x%08x\n", reg);
1936 if (rum_bbp_busy(sc) != 0) {
1937 device_printf(sc->sc_dev, "could not write to BBP\n");
1941 tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val;
1942 rum_write(sc, RT2573_PHY_CSR3, tmp);
1946 rum_bbp_read(struct rum_softc *sc, uint8_t reg)
1951 DPRINTFN(2, "reg=0x%08x\n", reg);
1953 if (rum_bbp_busy(sc) != 0) {
1954 device_printf(sc->sc_dev, "could not read BBP\n");
1958 val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8;
1959 rum_write(sc, RT2573_PHY_CSR3, val);
1961 for (ntries = 0; ntries < 100; ntries++) {
1962 val = rum_read(sc, RT2573_PHY_CSR3);
1963 if (!(val & RT2573_BBP_BUSY))
1965 if (rum_pause(sc, hz / 100))
1969 device_printf(sc->sc_dev, "could not read BBP\n");
1974 rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val)
1979 for (ntries = 0; ntries < 100; ntries++) {
1980 if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY))
1982 if (rum_pause(sc, hz / 100))
1985 if (ntries == 100) {
1986 device_printf(sc->sc_dev, "could not write to RF\n");
1990 tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 |
1992 rum_write(sc, RT2573_PHY_CSR4, tmp);
1994 /* remember last written value in sc */
1995 sc->rf_regs[reg] = val;
1997 DPRINTFN(15, "RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff);
2001 rum_select_antenna(struct rum_softc *sc)
2003 uint8_t bbp4, bbp77;
2006 bbp4 = rum_bbp_read(sc, 4);
2007 bbp77 = rum_bbp_read(sc, 77);
2011 /* make sure Rx is disabled before switching antenna */
2012 tmp = rum_read(sc, RT2573_TXRX_CSR0);
2013 rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
2015 rum_bbp_write(sc, 4, bbp4);
2016 rum_bbp_write(sc, 77, bbp77);
2018 rum_write(sc, RT2573_TXRX_CSR0, tmp);
2022 * Enable multi-rate retries for frames sent at OFDM rates.
2023 * In 802.11b/g mode, allow fallback to CCK rates.
2026 rum_enable_mrr(struct rum_softc *sc)
2028 struct ieee80211com *ic = &sc->sc_ic;
2030 if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) {
2031 rum_setbits(sc, RT2573_TXRX_CSR4,
2032 RT2573_MRR_ENABLED | RT2573_MRR_CCK_FALLBACK);
2034 rum_modbits(sc, RT2573_TXRX_CSR4,
2035 RT2573_MRR_ENABLED, RT2573_MRR_CCK_FALLBACK);
2040 rum_set_txpreamble(struct rum_softc *sc)
2042 struct ieee80211com *ic = &sc->sc_ic;
2044 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
2045 rum_setbits(sc, RT2573_TXRX_CSR4, RT2573_SHORT_PREAMBLE);
2047 rum_clrbits(sc, RT2573_TXRX_CSR4, RT2573_SHORT_PREAMBLE);
2051 rum_set_basicrates(struct rum_softc *sc)
2053 struct ieee80211com *ic = &sc->sc_ic;
2055 /* update basic rate set */
2056 if (ic->ic_curmode == IEEE80211_MODE_11B) {
2057 /* 11b basic rates: 1, 2Mbps */
2058 rum_write(sc, RT2573_TXRX_CSR5, 0x3);
2059 } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) {
2060 /* 11a basic rates: 6, 12, 24Mbps */
2061 rum_write(sc, RT2573_TXRX_CSR5, 0x150);
2063 /* 11b/g basic rates: 1, 2, 5.5, 11Mbps */
2064 rum_write(sc, RT2573_TXRX_CSR5, 0xf);
2069 * Reprogram MAC/BBP to switch to a new band. Values taken from the reference
2073 rum_select_band(struct rum_softc *sc, struct ieee80211_channel *c)
2075 uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104;
2077 /* update all BBP registers that depend on the band */
2078 bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c;
2079 bbp35 = 0x50; bbp97 = 0x48; bbp98 = 0x48;
2080 if (IEEE80211_IS_CHAN_5GHZ(c)) {
2081 bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c;
2082 bbp35 += 0x10; bbp97 += 0x10; bbp98 += 0x10;
2084 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
2085 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
2086 bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10;
2090 rum_bbp_write(sc, 17, bbp17);
2091 rum_bbp_write(sc, 96, bbp96);
2092 rum_bbp_write(sc, 104, bbp104);
2094 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
2095 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
2096 rum_bbp_write(sc, 75, 0x80);
2097 rum_bbp_write(sc, 86, 0x80);
2098 rum_bbp_write(sc, 88, 0x80);
2101 rum_bbp_write(sc, 35, bbp35);
2102 rum_bbp_write(sc, 97, bbp97);
2103 rum_bbp_write(sc, 98, bbp98);
2105 if (IEEE80211_IS_CHAN_2GHZ(c)) {
2106 rum_modbits(sc, RT2573_PHY_CSR0, RT2573_PA_PE_2GHZ,
2109 rum_modbits(sc, RT2573_PHY_CSR0, RT2573_PA_PE_5GHZ,
2115 rum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c)
2117 struct ieee80211com *ic = &sc->sc_ic;
2118 const struct rfprog *rfprog;
2119 uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT;
2123 chan = ieee80211_chan2ieee(ic, c);
2124 if (chan == 0 || chan == IEEE80211_CHAN_ANY)
2127 /* select the appropriate RF settings based on what EEPROM says */
2128 rfprog = (sc->rf_rev == RT2573_RF_5225 ||
2129 sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226;
2131 /* find the settings for this channel (we know it exists) */
2132 for (i = 0; rfprog[i].chan != chan; i++);
2134 power = sc->txpow[i];
2138 } else if (power > 31) {
2139 bbp94 += power - 31;
2144 * If we are switching from the 2GHz band to the 5GHz band or
2145 * vice-versa, BBP registers need to be reprogrammed.
2147 if (c->ic_flags != ic->ic_curchan->ic_flags) {
2148 rum_select_band(sc, c);
2149 rum_select_antenna(sc);
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_rf_write(sc, RT2573_RF1, rfprog[i].r1);
2159 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
2160 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7 | 1);
2161 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
2163 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
2164 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
2165 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
2166 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
2168 rum_pause(sc, hz / 100);
2170 /* enable smart mode for MIMO-capable RFs */
2171 bbp3 = rum_bbp_read(sc, 3);
2173 bbp3 &= ~RT2573_SMART_MODE;
2174 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527)
2175 bbp3 |= RT2573_SMART_MODE;
2177 rum_bbp_write(sc, 3, bbp3);
2179 if (bbp94 != RT2573_BBPR94_DEFAULT)
2180 rum_bbp_write(sc, 94, bbp94);
2182 /* give the chip some extra time to do the switchover */
2183 rum_pause(sc, hz / 100);
2187 rum_set_maxretry(struct rum_softc *sc, struct ieee80211vap *vap)
2189 struct ieee80211_node *ni = vap->iv_bss;
2190 const struct ieee80211_txparam *tp = ni->ni_txparms;
2191 struct rum_vap *rvp = RUM_VAP(vap);
2193 rvp->maxretry = MIN(tp->maxretry, 0xf);
2195 rum_modbits(sc, RT2573_TXRX_CSR4, RT2573_SHORT_RETRY(rvp->maxretry) |
2196 RT2573_LONG_RETRY(rvp->maxretry),
2197 RT2573_SHORT_RETRY_MASK | RT2573_LONG_RETRY_MASK);
2201 * Enable TSF synchronization and tell h/w to start sending beacons for IBSS
2202 * and HostAP operating modes.
2205 rum_enable_tsf_sync(struct rum_softc *sc)
2207 struct ieee80211com *ic = &sc->sc_ic;
2208 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2212 if (vap->iv_opmode != IEEE80211_M_STA) {
2214 * Change default 16ms TBTT adjustment to 8ms.
2215 * Must be done before enabling beacon generation.
2217 if (rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8) != 0)
2221 tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000;
2223 /* set beacon interval (in 1/16ms unit) */
2224 bintval = vap->iv_bss->ni_intval;
2225 tmp |= bintval * 16;
2226 tmp |= RT2573_TSF_TIMER_EN | RT2573_TBTT_TIMER_EN;
2228 switch (vap->iv_opmode) {
2229 case IEEE80211_M_STA:
2231 * Local TSF is always updated with remote TSF on beacon
2234 tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_STA);
2236 case IEEE80211_M_IBSS:
2238 * Local TSF is updated with remote TSF on beacon reception
2239 * only if the remote TSF is greater than local TSF.
2241 tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_IBSS);
2242 tmp |= RT2573_BCN_TX_EN;
2244 case IEEE80211_M_HOSTAP:
2245 /* SYNC with nobody */
2246 tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_HOSTAP);
2247 tmp |= RT2573_BCN_TX_EN;
2250 device_printf(sc->sc_dev,
2251 "Enabling TSF failed. undefined opmode %d\n",
2256 if (rum_write(sc, RT2573_TXRX_CSR9, tmp) != 0)
2259 /* refresh current sleep time */
2260 return (rum_set_sleep_time(sc, bintval));
2264 rum_enable_tsf(struct rum_softc *sc)
2266 rum_modbits(sc, RT2573_TXRX_CSR9, RT2573_TSF_TIMER_EN |
2267 RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_DIS), 0x00ffffff);
2271 rum_abort_tsf_sync(struct rum_softc *sc)
2273 rum_clrbits(sc, RT2573_TXRX_CSR9, 0x00ffffff);
2277 rum_get_tsf(struct rum_softc *sc, uint64_t *buf)
2279 rum_read_multi(sc, RT2573_TXRX_CSR12, buf, sizeof (*buf));
2283 rum_update_slot_cb(struct rum_softc *sc, union sec_param *data, uint8_t rvp_id)
2285 struct ieee80211com *ic = &sc->sc_ic;
2288 slottime = IEEE80211_GET_SLOTTIME(ic);
2290 rum_modbits(sc, RT2573_MAC_CSR9, slottime, 0xff);
2292 DPRINTF("setting slot time to %uus\n", slottime);
2296 rum_update_slot(struct ieee80211com *ic)
2298 rum_cmd_sleepable(ic->ic_softc, NULL, 0, 0, rum_update_slot_cb);
2302 rum_wme_update(struct ieee80211com *ic)
2304 const struct wmeParams *chanp =
2305 ic->ic_wme.wme_chanParams.cap_wmeParams;
2306 struct rum_softc *sc = ic->ic_softc;
2310 error = rum_write(sc, RT2573_AIFSN_CSR,
2311 chanp[WME_AC_VO].wmep_aifsn << 12 |
2312 chanp[WME_AC_VI].wmep_aifsn << 8 |
2313 chanp[WME_AC_BK].wmep_aifsn << 4 |
2314 chanp[WME_AC_BE].wmep_aifsn);
2317 error = rum_write(sc, RT2573_CWMIN_CSR,
2318 chanp[WME_AC_VO].wmep_logcwmin << 12 |
2319 chanp[WME_AC_VI].wmep_logcwmin << 8 |
2320 chanp[WME_AC_BK].wmep_logcwmin << 4 |
2321 chanp[WME_AC_BE].wmep_logcwmin);
2324 error = rum_write(sc, RT2573_CWMAX_CSR,
2325 chanp[WME_AC_VO].wmep_logcwmax << 12 |
2326 chanp[WME_AC_VI].wmep_logcwmax << 8 |
2327 chanp[WME_AC_BK].wmep_logcwmax << 4 |
2328 chanp[WME_AC_BE].wmep_logcwmax);
2331 error = rum_write(sc, RT2573_TXOP01_CSR,
2332 chanp[WME_AC_BK].wmep_txopLimit << 16 |
2333 chanp[WME_AC_BE].wmep_txopLimit);
2336 error = rum_write(sc, RT2573_TXOP23_CSR,
2337 chanp[WME_AC_VO].wmep_txopLimit << 16 |
2338 chanp[WME_AC_VI].wmep_txopLimit);
2342 memcpy(sc->wme_params, chanp, sizeof(*chanp) * WME_NUM_AC);
2347 device_printf(sc->sc_dev, "%s: WME update failed, error %d\n",
2355 rum_set_bssid(struct rum_softc *sc, const uint8_t *bssid)
2358 rum_write(sc, RT2573_MAC_CSR4,
2359 bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24);
2360 rum_write(sc, RT2573_MAC_CSR5,
2361 bssid[4] | bssid[5] << 8 | RT2573_NUM_BSSID_MSK(1));
2365 rum_set_macaddr(struct rum_softc *sc, const uint8_t *addr)
2368 rum_write(sc, RT2573_MAC_CSR2,
2369 addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
2370 rum_write(sc, RT2573_MAC_CSR3,
2371 addr[4] | addr[5] << 8 | 0xff << 16);
2375 rum_setpromisc(struct rum_softc *sc)
2377 struct ieee80211com *ic = &sc->sc_ic;
2379 if (ic->ic_promisc == 0)
2380 rum_setbits(sc, RT2573_TXRX_CSR0, RT2573_DROP_NOT_TO_ME);
2382 rum_clrbits(sc, RT2573_TXRX_CSR0, RT2573_DROP_NOT_TO_ME);
2384 DPRINTF("%s promiscuous mode\n", ic->ic_promisc > 0 ?
2385 "entering" : "leaving");
2389 rum_update_promisc(struct ieee80211com *ic)
2391 struct rum_softc *sc = ic->ic_softc;
2400 rum_update_mcast(struct ieee80211com *ic)
2409 case RT2573_RF_2527: return "RT2527 (MIMO XR)";
2410 case RT2573_RF_2528: return "RT2528";
2411 case RT2573_RF_5225: return "RT5225 (MIMO XR)";
2412 case RT2573_RF_5226: return "RT5226";
2413 default: return "unknown";
2418 rum_read_eeprom(struct rum_softc *sc)
2425 /* read MAC address */
2426 rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, sc->sc_ic.ic_macaddr, 6);
2428 rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2);
2430 sc->rf_rev = (val >> 11) & 0x1f;
2431 sc->hw_radio = (val >> 10) & 0x1;
2432 sc->rx_ant = (val >> 4) & 0x3;
2433 sc->tx_ant = (val >> 2) & 0x3;
2434 sc->nb_ant = val & 0x3;
2436 DPRINTF("RF revision=%d\n", sc->rf_rev);
2438 rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2);
2440 sc->ext_5ghz_lna = (val >> 6) & 0x1;
2441 sc->ext_2ghz_lna = (val >> 4) & 0x1;
2443 DPRINTF("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n",
2444 sc->ext_2ghz_lna, sc->ext_5ghz_lna);
2446 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2);
2448 if ((val & 0xff) != 0xff)
2449 sc->rssi_2ghz_corr = (int8_t)(val & 0xff); /* signed */
2451 /* Only [-10, 10] is valid */
2452 if (sc->rssi_2ghz_corr < -10 || sc->rssi_2ghz_corr > 10)
2453 sc->rssi_2ghz_corr = 0;
2455 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2);
2457 if ((val & 0xff) != 0xff)
2458 sc->rssi_5ghz_corr = (int8_t)(val & 0xff); /* signed */
2460 /* Only [-10, 10] is valid */
2461 if (sc->rssi_5ghz_corr < -10 || sc->rssi_5ghz_corr > 10)
2462 sc->rssi_5ghz_corr = 0;
2464 if (sc->ext_2ghz_lna)
2465 sc->rssi_2ghz_corr -= 14;
2466 if (sc->ext_5ghz_lna)
2467 sc->rssi_5ghz_corr -= 14;
2469 DPRINTF("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n",
2470 sc->rssi_2ghz_corr, sc->rssi_5ghz_corr);
2472 rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2);
2474 if ((val & 0xff) != 0xff)
2475 sc->rffreq = val & 0xff;
2477 DPRINTF("RF freq=%d\n", sc->rffreq);
2479 /* read Tx power for all a/b/g channels */
2480 rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14);
2481 /* XXX default Tx power for 802.11a channels */
2482 memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14);
2484 for (i = 0; i < 14; i++)
2485 DPRINTF("Channel=%d Tx power=%d\n", i + 1, sc->txpow[i]);
2488 /* read default values for BBP registers */
2489 rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
2491 for (i = 0; i < 14; i++) {
2492 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
2494 DPRINTF("BBP R%d=%02x\n", sc->bbp_prom[i].reg,
2495 sc->bbp_prom[i].val);
2501 rum_bbp_wakeup(struct rum_softc *sc)
2503 unsigned int ntries;
2505 for (ntries = 0; ntries < 100; ntries++) {
2506 if (rum_read(sc, RT2573_MAC_CSR12) & 8)
2508 rum_write(sc, RT2573_MAC_CSR12, 4); /* force wakeup */
2509 if (rum_pause(sc, hz / 100))
2512 if (ntries == 100) {
2513 device_printf(sc->sc_dev,
2514 "timeout waiting for BBP/RF to wakeup\n");
2522 rum_bbp_init(struct rum_softc *sc)
2526 /* wait for BBP to be ready */
2527 for (ntries = 0; ntries < 100; ntries++) {
2528 const uint8_t val = rum_bbp_read(sc, 0);
2529 if (val != 0 && val != 0xff)
2531 if (rum_pause(sc, hz / 100))
2534 if (ntries == 100) {
2535 device_printf(sc->sc_dev, "timeout waiting for BBP\n");
2539 /* initialize BBP registers to default values */
2540 for (i = 0; i < nitems(rum_def_bbp); i++)
2541 rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val);
2543 /* write vendor-specific BBP values (from EEPROM) */
2544 for (i = 0; i < 16; i++) {
2545 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
2547 rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
2554 rum_clr_shkey_regs(struct rum_softc *sc)
2556 rum_write(sc, RT2573_SEC_CSR0, 0);
2557 rum_write(sc, RT2573_SEC_CSR1, 0);
2558 rum_write(sc, RT2573_SEC_CSR5, 0);
2562 rum_init(struct rum_softc *sc)
2564 struct ieee80211com *ic = &sc->sc_ic;
2565 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2570 if (sc->sc_running) {
2575 /* initialize MAC registers to default values */
2576 for (i = 0; i < nitems(rum_def_mac); i++)
2577 rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val);
2579 /* reset some WME parameters to default values */
2580 sc->wme_params[0].wmep_aifsn = 2;
2581 sc->wme_params[0].wmep_logcwmin = 4;
2582 sc->wme_params[0].wmep_logcwmax = 10;
2584 /* set host ready */
2585 rum_write(sc, RT2573_MAC_CSR1, RT2573_RESET_ASIC | RT2573_RESET_BBP);
2586 rum_write(sc, RT2573_MAC_CSR1, 0);
2588 /* wait for BBP/RF to wakeup */
2589 if ((ret = rum_bbp_wakeup(sc)) != 0)
2592 if ((ret = rum_bbp_init(sc)) != 0)
2595 /* select default channel */
2596 rum_select_band(sc, ic->ic_curchan);
2597 rum_select_antenna(sc);
2598 rum_set_chan(sc, ic->ic_curchan);
2600 /* clear STA registers */
2601 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
2603 /* clear security registers (if required) */
2604 if (sc->sc_clr_shkeys == 0) {
2605 rum_clr_shkey_regs(sc);
2606 sc->sc_clr_shkeys = 1;
2609 rum_set_macaddr(sc, vap ? vap->iv_myaddr : ic->ic_macaddr);
2611 /* initialize ASIC */
2612 rum_write(sc, RT2573_MAC_CSR1, RT2573_HOST_READY);
2615 * Allocate Tx and Rx xfer queues.
2617 rum_setup_tx_list(sc);
2619 /* update Rx filter */
2620 tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff;
2622 tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR;
2623 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2624 tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR |
2626 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2627 tmp |= RT2573_DROP_TODS;
2628 if (ic->ic_promisc == 0)
2629 tmp |= RT2573_DROP_NOT_TO_ME;
2631 rum_write(sc, RT2573_TXRX_CSR0, tmp);
2634 usbd_xfer_set_stall(sc->sc_xfer[RUM_BULK_WR]);
2635 usbd_transfer_start(sc->sc_xfer[RUM_BULK_RD]);
2637 end: RUM_UNLOCK(sc);
2646 rum_stop(struct rum_softc *sc)
2650 if (!sc->sc_running) {
2658 * Drain the USB transfers, if not already drained:
2660 usbd_transfer_drain(sc->sc_xfer[RUM_BULK_WR]);
2661 usbd_transfer_drain(sc->sc_xfer[RUM_BULK_RD]);
2664 rum_unsetup_tx_list(sc);
2667 rum_setbits(sc, RT2573_TXRX_CSR0, RT2573_DISABLE_RX);
2670 rum_write(sc, RT2573_MAC_CSR1, RT2573_RESET_ASIC | RT2573_RESET_BBP);
2671 rum_write(sc, RT2573_MAC_CSR1, 0);
2676 rum_load_microcode(struct rum_softc *sc, const uint8_t *ucode, size_t size)
2678 uint16_t reg = RT2573_MCU_CODE_BASE;
2681 /* copy firmware image into NIC */
2682 for (; size >= 4; reg += 4, ucode += 4, size -= 4) {
2683 err = rum_write(sc, reg, UGETDW(ucode));
2685 /* firmware already loaded ? */
2686 device_printf(sc->sc_dev, "Firmware load "
2687 "failure! (ignored)\n");
2692 err = rum_do_mcu_request(sc, RT2573_MCU_RUN);
2693 if (err != USB_ERR_NORMAL_COMPLETION) {
2694 device_printf(sc->sc_dev, "could not run firmware: %s\n",
2698 /* give the chip some time to boot */
2699 rum_pause(sc, hz / 8);
2703 rum_set_sleep_time(struct rum_softc *sc, uint16_t bintval)
2705 struct ieee80211com *ic = &sc->sc_ic;
2709 RUM_LOCK_ASSERT(sc);
2711 exp = ic->ic_lintval / bintval;
2712 delay = ic->ic_lintval % bintval;
2714 if (exp > RT2573_TBCN_EXP_MAX)
2715 exp = RT2573_TBCN_EXP_MAX;
2716 if (delay > RT2573_TBCN_DELAY_MAX)
2717 delay = RT2573_TBCN_DELAY_MAX;
2719 uerror = rum_modbits(sc, RT2573_MAC_CSR11,
2720 RT2573_TBCN_EXP(exp) |
2721 RT2573_TBCN_DELAY(delay),
2722 RT2573_TBCN_EXP(RT2573_TBCN_EXP_MAX) |
2723 RT2573_TBCN_DELAY(RT2573_TBCN_DELAY_MAX));
2725 if (uerror != USB_ERR_NORMAL_COMPLETION)
2728 sc->sc_sleep_time = IEEE80211_TU_TO_TICKS(exp * bintval + delay);
2734 rum_reset(struct ieee80211vap *vap, u_long cmd)
2736 struct ieee80211com *ic = vap->iv_ic;
2737 struct ieee80211_node *ni;
2738 struct rum_softc *sc = ic->ic_softc;
2742 case IEEE80211_IOC_POWERSAVE:
2743 case IEEE80211_IOC_PROTMODE:
2744 case IEEE80211_IOC_RTSTHRESHOLD:
2747 case IEEE80211_IOC_POWERSAVESLEEP:
2748 ni = ieee80211_ref_node(vap->iv_bss);
2751 error = rum_set_sleep_time(sc, ni->ni_intval);
2752 if (vap->iv_state == IEEE80211_S_SLEEP) {
2753 /* Use new values for wakeup timer. */
2754 rum_clrbits(sc, RT2573_MAC_CSR11, RT2573_AUTO_WAKEUP);
2755 rum_setbits(sc, RT2573_MAC_CSR11, RT2573_AUTO_WAKEUP);
2757 /* XXX send reassoc */
2760 ieee80211_free_node(ni);
2771 rum_set_beacon(struct rum_softc *sc, struct ieee80211vap *vap)
2773 struct ieee80211com *ic = vap->iv_ic;
2774 struct rum_vap *rvp = RUM_VAP(vap);
2775 struct mbuf *m = rvp->bcn_mbuf;
2776 const struct ieee80211_txparam *tp;
2777 struct rum_tx_desc desc;
2779 RUM_LOCK_ASSERT(sc);
2783 if (ic->ic_bsschan == IEEE80211_CHAN_ANYC)
2786 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)];
2787 rum_setup_tx_desc(sc, &desc, NULL, RT2573_TX_TIMESTAMP,
2788 RT2573_TX_HWSEQ, 0, 0, m->m_pkthdr.len, tp->mgmtrate);
2790 /* copy the Tx descriptor into NIC memory */
2791 if (rum_write_multi(sc, RT2573_HW_BCN_BASE(0), (uint8_t *)&desc,
2792 RT2573_TX_DESC_SIZE) != 0)
2795 /* copy beacon header and payload into NIC memory */
2796 if (rum_write_multi(sc, RT2573_HW_BCN_BASE(0) + RT2573_TX_DESC_SIZE,
2797 mtod(m, uint8_t *), m->m_pkthdr.len) != 0)
2804 rum_alloc_beacon(struct rum_softc *sc, struct ieee80211vap *vap)
2806 struct rum_vap *rvp = RUM_VAP(vap);
2807 struct ieee80211_node *ni = vap->iv_bss;
2810 if (ni->ni_chan == IEEE80211_CHAN_ANYC)
2813 m = ieee80211_beacon_alloc(ni);
2817 if (rvp->bcn_mbuf != NULL)
2818 m_freem(rvp->bcn_mbuf);
2822 return (rum_set_beacon(sc, vap));
2826 rum_update_beacon_cb(struct rum_softc *sc, union sec_param *data,
2829 struct ieee80211vap *vap = data->vap;
2831 rum_set_beacon(sc, vap);
2835 rum_update_beacon(struct ieee80211vap *vap, int item)
2837 struct ieee80211com *ic = vap->iv_ic;
2838 struct rum_softc *sc = ic->ic_softc;
2839 struct rum_vap *rvp = RUM_VAP(vap);
2840 struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
2841 struct ieee80211_node *ni = vap->iv_bss;
2842 struct mbuf *m = rvp->bcn_mbuf;
2847 m = ieee80211_beacon_alloc(ni);
2849 device_printf(sc->sc_dev,
2850 "%s: could not allocate beacon frame\n", __func__);
2858 case IEEE80211_BEACON_ERP:
2859 rum_update_slot(ic);
2861 case IEEE80211_BEACON_TIM:
2869 setbit(bo->bo_flags, item);
2870 ieee80211_beacon_update(ni, m, mcast);
2872 rum_cmd_sleepable(sc, &vap, sizeof(vap), 0, rum_update_beacon_cb);
2876 rum_common_key_set(struct rum_softc *sc, struct ieee80211_key *k,
2880 if (rum_write_multi(sc, base, k->wk_key, k->wk_keylen))
2883 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP) {
2884 if (rum_write_multi(sc, base + IEEE80211_KEYBUF_SIZE,
2887 if (rum_write_multi(sc, base + IEEE80211_KEYBUF_SIZE + 8,
2896 rum_group_key_set_cb(struct rum_softc *sc, union sec_param *data,
2899 struct ieee80211_key *k = &data->key;
2902 if (sc->sc_clr_shkeys == 0) {
2903 rum_clr_shkey_regs(sc);
2904 sc->sc_clr_shkeys = 1;
2907 mode = rum_crypto_mode(sc, k->wk_cipher->ic_cipher, k->wk_keylen);
2911 DPRINTFN(1, "setting group key %d for vap %d, mode %d "
2912 "(tx %s, rx %s)\n", k->wk_keyix, rvp_id, mode,
2913 (k->wk_flags & IEEE80211_KEY_XMIT) ? "on" : "off",
2914 (k->wk_flags & IEEE80211_KEY_RECV) ? "on" : "off");
2916 /* Install the key. */
2917 if (rum_common_key_set(sc, k, RT2573_SKEY(rvp_id, k->wk_keyix)) != 0)
2920 /* Set cipher mode. */
2921 if (rum_modbits(sc, rvp_id < 2 ? RT2573_SEC_CSR1 : RT2573_SEC_CSR5,
2922 mode << (rvp_id % 2 + k->wk_keyix) * RT2573_SKEY_MAX,
2923 RT2573_MODE_MASK << (rvp_id % 2 + k->wk_keyix) * RT2573_SKEY_MAX)
2927 /* Mark this key as valid. */
2928 if (rum_setbits(sc, RT2573_SEC_CSR0,
2929 1 << (rvp_id * RT2573_SKEY_MAX + k->wk_keyix)) != 0)
2935 device_printf(sc->sc_dev, "%s: cannot set group key %d for vap %d\n",
2936 __func__, k->wk_keyix, rvp_id);
2940 rum_group_key_del_cb(struct rum_softc *sc, union sec_param *data,
2943 struct ieee80211_key *k = &data->key;
2945 DPRINTF("%s: removing group key %d for vap %d\n", __func__,
2946 k->wk_keyix, rvp_id);
2948 rvp_id < 2 ? RT2573_SEC_CSR1 : RT2573_SEC_CSR5,
2949 RT2573_MODE_MASK << (rvp_id % 2 + k->wk_keyix) * RT2573_SKEY_MAX);
2950 rum_clrbits(sc, RT2573_SEC_CSR0,
2951 rvp_id * RT2573_SKEY_MAX + k->wk_keyix);
2955 rum_pair_key_set_cb(struct rum_softc *sc, union sec_param *data,
2958 struct ieee80211_key *k = &data->key;
2959 uint8_t buf[IEEE80211_ADDR_LEN + 1];
2962 mode = rum_crypto_mode(sc, k->wk_cipher->ic_cipher, k->wk_keylen);
2966 DPRINTFN(1, "setting pairwise key %d for vap %d, mode %d "
2967 "(tx %s, rx %s)\n", k->wk_keyix, rvp_id, mode,
2968 (k->wk_flags & IEEE80211_KEY_XMIT) ? "on" : "off",
2969 (k->wk_flags & IEEE80211_KEY_RECV) ? "on" : "off");
2971 /* Install the key. */
2972 if (rum_common_key_set(sc, k, RT2573_PKEY(k->wk_keyix)) != 0)
2975 IEEE80211_ADDR_COPY(buf, k->wk_macaddr);
2976 buf[IEEE80211_ADDR_LEN] = mode;
2978 /* Set transmitter address and cipher mode. */
2979 if (rum_write_multi(sc, RT2573_ADDR_ENTRY(k->wk_keyix),
2980 buf, sizeof buf) != 0)
2983 /* Enable key table lookup for this vap. */
2984 if (sc->vap_key_count[rvp_id]++ == 0)
2985 if (rum_setbits(sc, RT2573_SEC_CSR4, 1 << rvp_id) != 0)
2988 /* Mark this key as valid. */
2990 k->wk_keyix < 32 ? RT2573_SEC_CSR2 : RT2573_SEC_CSR3,
2991 1 << (k->wk_keyix % 32)) != 0)
2997 device_printf(sc->sc_dev,
2998 "%s: cannot set pairwise key %d, vap %d\n", __func__, k->wk_keyix,
3003 rum_pair_key_del_cb(struct rum_softc *sc, union sec_param *data,
3006 struct ieee80211_key *k = &data->key;
3008 DPRINTF("%s: removing key %d\n", __func__, k->wk_keyix);
3009 rum_clrbits(sc, (k->wk_keyix < 32) ? RT2573_SEC_CSR2 : RT2573_SEC_CSR3,
3010 1 << (k->wk_keyix % 32));
3011 sc->keys_bmap &= ~(1ULL << k->wk_keyix);
3012 if (--sc->vap_key_count[rvp_id] == 0)
3013 rum_clrbits(sc, RT2573_SEC_CSR4, 1 << rvp_id);
3017 rum_key_alloc(struct ieee80211vap *vap, struct ieee80211_key *k,
3018 ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
3020 struct rum_softc *sc = vap->iv_ic->ic_softc;
3023 if (!(&vap->iv_nw_keys[0] <= k &&
3024 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID])) {
3025 if (!(k->wk_flags & IEEE80211_KEY_SWCRYPT)) {
3027 for (i = 0; i < RT2573_ADDR_MAX; i++) {
3028 if ((sc->keys_bmap & (1ULL << i)) == 0) {
3029 sc->keys_bmap |= (1ULL << i);
3035 if (i == RT2573_ADDR_MAX) {
3036 device_printf(sc->sc_dev,
3037 "%s: no free space in the key table\n",
3044 *keyix = ieee80211_crypto_get_key_wepidx(vap, k);
3051 rum_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
3053 struct rum_softc *sc = vap->iv_ic->ic_softc;
3056 if (k->wk_flags & IEEE80211_KEY_SWCRYPT) {
3061 group = k >= &vap->iv_nw_keys[0] && k < &vap->iv_nw_keys[IEEE80211_WEP_NKID];
3063 return !rum_cmd_sleepable(sc, k, sizeof(*k), 0,
3064 group ? rum_group_key_set_cb : rum_pair_key_set_cb);
3068 rum_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
3070 struct rum_softc *sc = vap->iv_ic->ic_softc;
3073 if (k->wk_flags & IEEE80211_KEY_SWCRYPT) {
3078 group = k >= &vap->iv_nw_keys[0] && k < &vap->iv_nw_keys[IEEE80211_WEP_NKID];
3080 return !rum_cmd_sleepable(sc, k, sizeof(*k), 0,
3081 group ? rum_group_key_del_cb : rum_pair_key_del_cb);
3085 rum_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
3086 const struct ieee80211_bpf_params *params)
3088 struct rum_softc *sc = ni->ni_ic->ic_softc;
3092 /* prevent management frames from being sent if we're not ready */
3093 if (!sc->sc_running) {
3097 if (sc->tx_nfree < RUM_TX_MINFREE) {
3102 if (params == NULL) {
3104 * Legacy path; interpret frame contents to decide
3105 * precisely how to send the frame.
3107 if ((ret = rum_tx_mgt(sc, m, ni)) != 0)
3111 * Caller supplied explicit parameters to use in
3112 * sending the frame.
3114 if ((ret = rum_tx_raw(sc, m, ni, params)) != 0)
3127 rum_ratectl_start(struct rum_softc *sc, struct ieee80211_node *ni)
3129 struct ieee80211vap *vap = ni->ni_vap;
3130 struct rum_vap *rvp = RUM_VAP(vap);
3132 /* clear statistic registers (STA_CSR0 to STA_CSR5) */
3133 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
3135 usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp);
3139 rum_ratectl_timeout(void *arg)
3141 struct rum_vap *rvp = arg;
3142 struct ieee80211vap *vap = &rvp->vap;
3143 struct ieee80211com *ic = vap->iv_ic;
3145 ieee80211_runtask(ic, &rvp->ratectl_task);
3149 rum_ratectl_task(void *arg, int pending)
3151 struct rum_vap *rvp = arg;
3152 struct ieee80211vap *vap = &rvp->vap;
3153 struct rum_softc *sc = vap->iv_ic->ic_softc;
3154 struct ieee80211_ratectl_tx_stats *txs = &sc->sc_txs;
3158 /* read and clear statistic registers (STA_CSR0 to STA_CSR5) */
3159 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof(sc->sta));
3161 ok[0] = (le32toh(sc->sta[4]) & 0xffff); /* TX ok w/o retry */
3162 ok[1] = (le32toh(sc->sta[4]) >> 16); /* TX ok w/ one retry */
3163 ok[2] = (le32toh(sc->sta[5]) & 0xffff); /* TX ok w/ multiple retries */
3164 fail = (le32toh(sc->sta[5]) >> 16); /* TX retry-fail count */
3166 txs->flags = IEEE80211_RATECTL_TX_STATS_RETRIES;
3167 txs->nframes = ok[0] + ok[1] + ok[2] + fail;
3168 txs->nsuccess = txs->nframes - fail;
3170 txs->nretries = ok[1] + ok[2] * 2 + fail * (rvp->maxretry + 1);
3172 if (txs->nframes != 0)
3173 ieee80211_ratectl_tx_update(vap, txs);
3175 /* count TX retry-fail as Tx errors */
3176 if_inc_counter(vap->iv_ifp, IFCOUNTER_OERRORS, fail);
3178 usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp);
3183 rum_scan_start(struct ieee80211com *ic)
3185 struct rum_softc *sc = ic->ic_softc;
3188 rum_abort_tsf_sync(sc);
3189 rum_set_bssid(sc, ieee80211broadcastaddr);
3195 rum_scan_end(struct ieee80211com *ic)
3197 struct rum_softc *sc = ic->ic_softc;
3199 if (ic->ic_flags_ext & IEEE80211_FEXT_BGSCAN) {
3201 if (ic->ic_opmode != IEEE80211_M_AHDEMO)
3202 rum_enable_tsf_sync(sc);
3205 rum_set_bssid(sc, sc->sc_bssid);
3211 rum_set_channel(struct ieee80211com *ic)
3213 struct rum_softc *sc = ic->ic_softc;
3216 rum_set_chan(sc, ic->ic_curchan);
3221 rum_getradiocaps(struct ieee80211com *ic,
3222 int maxchans, int *nchans, struct ieee80211_channel chans[])
3224 struct rum_softc *sc = ic->ic_softc;
3225 uint8_t bands[IEEE80211_MODE_BYTES];
3227 memset(bands, 0, sizeof(bands));
3228 setbit(bands, IEEE80211_MODE_11B);
3229 setbit(bands, IEEE80211_MODE_11G);
3230 ieee80211_add_channel_list_2ghz(chans, maxchans, nchans,
3231 rum_chan_2ghz, nitems(rum_chan_2ghz), bands, 0);
3233 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_5226) {
3234 setbit(bands, IEEE80211_MODE_11A);
3235 ieee80211_add_channel_list_5ghz(chans, maxchans, nchans,
3236 rum_chan_5ghz, nitems(rum_chan_5ghz), bands, 0);
3241 rum_get_rssi(struct rum_softc *sc, uint8_t raw)
3243 struct ieee80211com *ic = &sc->sc_ic;
3246 lna = (raw >> 5) & 0x3;
3253 * NB: Since RSSI is relative to noise floor, -1 is
3254 * adequate for caller to know error happened.
3259 rssi = (2 * agc) - RT2573_NOISE_FLOOR;
3261 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) {
3262 rssi += sc->rssi_2ghz_corr;
3271 rssi += sc->rssi_5ghz_corr;
3273 if (!sc->ext_5ghz_lna && lna != 1)
3287 rum_pause(struct rum_softc *sc, int timeout)
3290 usb_pause_mtx(&sc->sc_mtx, timeout);
3294 static device_method_t rum_methods[] = {
3295 /* Device interface */
3296 DEVMETHOD(device_probe, rum_match),
3297 DEVMETHOD(device_attach, rum_attach),
3298 DEVMETHOD(device_detach, rum_detach),
3302 static driver_t rum_driver = {
3304 .methods = rum_methods,
3305 .size = sizeof(struct rum_softc),
3308 static devclass_t rum_devclass;
3310 DRIVER_MODULE(rum, uhub, rum_driver, rum_devclass, NULL, 0);
3311 MODULE_DEPEND(rum, wlan, 1, 1, 1);
3312 MODULE_DEPEND(rum, usb, 1, 1, 1);
3313 MODULE_VERSION(rum, 1);
3314 USB_PNP_HOST_INFO(rum_devs);