1 /* $FreeBSD: head/sys/dev/usb/wlan/if_rum.c 298895 2016-05-01 18:53:12Z avos $ */
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.
23 * Ralink Technology RT2501USB/RT2601USB chipset driver
24 * http://www.ralinktech.com.tw/
27 #include <sys/param.h>
28 #include <sys/sockio.h>
29 #include <sys/sysctl.h>
32 #include <sys/kernel.h>
33 #include <sys/socket.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
38 #include <sys/endian.h>
44 #include <net/if_var.h>
45 #include <net/if_arp.h>
46 #include <net/ethernet.h>
47 #include <net/if_dl.h>
48 #include <net/if_media.h>
49 #include <net/if_types.h>
51 #include <netproto/802_11/ieee80211_var.h>
52 #include <netproto/802_11/ieee80211_regdomain.h>
53 #include <netproto/802_11/ieee80211_radiotap.h>
54 #include <netproto/802_11/ieee80211_ratectl.h>
56 #include <bus/u4b/usb.h>
57 #include <bus/u4b/usbdi.h>
60 #define USB_DEBUG_VAR rum_debug
61 #include <bus/u4b/usb_debug.h>
63 #include <bus/u4b/wlan/if_rumreg.h>
64 #include <bus/u4b/wlan/if_rumvar.h>
65 #include <bus/u4b/wlan/if_rumfw.h>
68 static int rum_debug = 0;
70 static SYSCTL_NODE(_hw_usb, OID_AUTO, rum, CTLFLAG_RW, 0, "USB rum");
71 SYSCTL_INT(_hw_usb_rum, OID_AUTO, debug, CTLFLAG_RW, &rum_debug, 0,
75 static const STRUCT_USB_HOST_ID rum_devs[] = {
76 #define RUM_DEV(v,p) { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
77 RUM_DEV(ABOCOM, HWU54DM),
78 RUM_DEV(ABOCOM, RT2573_2),
79 RUM_DEV(ABOCOM, RT2573_3),
80 RUM_DEV(ABOCOM, RT2573_4),
81 RUM_DEV(ABOCOM, WUG2700),
82 RUM_DEV(AMIT, CGWLUSB2GO),
83 RUM_DEV(ASUS, RT2573_1),
84 RUM_DEV(ASUS, RT2573_2),
85 RUM_DEV(BELKIN, F5D7050A),
86 RUM_DEV(BELKIN, F5D9050V3),
87 RUM_DEV(CISCOLINKSYS, WUSB54GC),
88 RUM_DEV(CISCOLINKSYS, WUSB54GR),
89 RUM_DEV(CONCEPTRONIC2, C54RU2),
90 RUM_DEV(COREGA, CGWLUSB2GL),
91 RUM_DEV(COREGA, CGWLUSB2GPX),
92 RUM_DEV(DICKSMITH, CWD854F),
93 RUM_DEV(DICKSMITH, RT2573),
94 RUM_DEV(EDIMAX, EW7318USG),
95 RUM_DEV(DLINK2, DWLG122C1),
96 RUM_DEV(DLINK2, WUA1340),
97 RUM_DEV(DLINK2, DWA111),
98 RUM_DEV(DLINK2, DWA110),
99 RUM_DEV(GIGABYTE, GNWB01GS),
100 RUM_DEV(GIGABYTE, GNWI05GS),
101 RUM_DEV(GIGASET, RT2573),
102 RUM_DEV(GOODWAY, RT2573),
103 RUM_DEV(GUILLEMOT, HWGUSB254LB),
104 RUM_DEV(GUILLEMOT, HWGUSB254V2AP),
105 RUM_DEV(HUAWEI3COM, WUB320G),
106 RUM_DEV(MELCO, G54HP),
107 RUM_DEV(MELCO, SG54HP),
108 RUM_DEV(MELCO, SG54HG),
109 RUM_DEV(MELCO, WLIUCG),
110 RUM_DEV(MELCO, WLRUCG),
111 RUM_DEV(MELCO, WLRUCGAOSS),
112 RUM_DEV(MSI, RT2573_1),
113 RUM_DEV(MSI, RT2573_2),
114 RUM_DEV(MSI, RT2573_3),
115 RUM_DEV(MSI, RT2573_4),
116 RUM_DEV(NOVATECH, RT2573),
117 RUM_DEV(PLANEX2, GWUS54HP),
118 RUM_DEV(PLANEX2, GWUS54MINI2),
119 RUM_DEV(PLANEX2, GWUSMM),
120 RUM_DEV(QCOM, RT2573),
121 RUM_DEV(QCOM, RT2573_2),
122 RUM_DEV(QCOM, RT2573_3),
123 RUM_DEV(RALINK, RT2573),
124 RUM_DEV(RALINK, RT2573_2),
125 RUM_DEV(RALINK, RT2671),
126 RUM_DEV(SITECOMEU, WL113R2),
127 RUM_DEV(SITECOMEU, WL172),
128 RUM_DEV(SPARKLAN, RT2573),
129 RUM_DEV(SURECOM, RT2573),
133 static device_probe_t rum_match;
134 static device_attach_t rum_attach;
135 static device_detach_t rum_detach;
137 static usb_callback_t rum_bulk_read_callback;
138 static usb_callback_t rum_bulk_write_callback;
140 static usb_error_t rum_do_request(struct rum_softc *sc,
141 struct usb_device_request *req, void *data);
142 static usb_error_t rum_do_mcu_request(struct rum_softc *sc, int);
143 static struct ieee80211vap *rum_vap_create(struct ieee80211com *,
144 const char [IFNAMSIZ], int, enum ieee80211_opmode,
145 int, const uint8_t [IEEE80211_ADDR_LEN],
146 const uint8_t [IEEE80211_ADDR_LEN]);
147 static void rum_vap_delete(struct ieee80211vap *);
148 static void rum_cmdq_cb(void *, int);
149 static int rum_cmd_sleepable(struct rum_softc *, const void *,
150 size_t, uint8_t, CMD_FUNC_PROTO);
151 static void rum_tx_free(struct rum_tx_data *, int);
152 static void rum_setup_tx_list(struct rum_softc *);
153 static void rum_unsetup_tx_list(struct rum_softc *);
154 static void rum_beacon_miss(struct ieee80211vap *);
155 static void rum_sta_recv_mgmt(struct ieee80211_node *,
157 const struct ieee80211_rx_stats *, int, int);
158 static int rum_set_power_state(struct rum_softc *, int);
159 static int rum_newstate(struct ieee80211vap *,
160 enum ieee80211_state, int);
161 static uint8_t rum_crypto_mode(struct rum_softc *, u_int, int);
162 static void rum_setup_tx_desc(struct rum_softc *,
163 struct rum_tx_desc *, struct ieee80211_key *,
164 uint32_t, uint8_t, uint8_t, int, int, int);
165 static uint32_t rum_tx_crypto_flags(struct rum_softc *,
166 struct ieee80211_node *,
167 const struct ieee80211_key *);
168 static int rum_tx_mgt(struct rum_softc *, struct mbuf *,
169 struct ieee80211_node *);
170 static int rum_tx_raw(struct rum_softc *, struct mbuf *,
171 struct ieee80211_node *,
172 const struct ieee80211_bpf_params *);
173 static int rum_tx_data(struct rum_softc *, struct mbuf *,
174 struct ieee80211_node *);
175 static int rum_transmit(struct ieee80211com *, struct mbuf *);
176 static void rum_start(struct rum_softc *);
177 static void rum_parent(struct ieee80211com *);
178 static void rum_eeprom_read(struct rum_softc *, uint16_t, void *,
180 static uint32_t rum_read(struct rum_softc *, uint16_t);
181 static void rum_read_multi(struct rum_softc *, uint16_t, void *,
183 static usb_error_t rum_write(struct rum_softc *, uint16_t, uint32_t);
184 static usb_error_t rum_write_multi(struct rum_softc *, uint16_t, void *,
186 static usb_error_t rum_setbits(struct rum_softc *, uint16_t, uint32_t);
187 static usb_error_t rum_clrbits(struct rum_softc *, uint16_t, uint32_t);
188 static usb_error_t rum_modbits(struct rum_softc *, uint16_t, uint32_t,
190 static int rum_bbp_busy(struct rum_softc *);
191 static void rum_bbp_write(struct rum_softc *, uint8_t, uint8_t);
192 static uint8_t rum_bbp_read(struct rum_softc *, uint8_t);
193 static void rum_rf_write(struct rum_softc *, uint8_t, uint32_t);
194 static void rum_select_antenna(struct rum_softc *);
195 static void rum_enable_mrr(struct rum_softc *);
196 static void rum_set_txpreamble(struct rum_softc *);
197 static void rum_set_basicrates(struct rum_softc *);
198 static void rum_select_band(struct rum_softc *,
199 struct ieee80211_channel *);
200 static void rum_set_chan(struct rum_softc *,
201 struct ieee80211_channel *);
202 static void rum_set_maxretry(struct rum_softc *,
203 struct ieee80211vap *);
204 static int rum_enable_tsf_sync(struct rum_softc *);
205 static void rum_enable_tsf(struct rum_softc *);
206 static void rum_abort_tsf_sync(struct rum_softc *);
207 static void rum_get_tsf(struct rum_softc *, uint64_t *);
208 static void rum_update_slot_cb(struct rum_softc *,
209 union sec_param *, uint8_t);
210 static void rum_update_slot(struct ieee80211com *);
211 static int rum_wme_update(struct ieee80211com *);
212 static void rum_set_bssid(struct rum_softc *, const uint8_t *);
213 static void rum_set_macaddr(struct rum_softc *, const uint8_t *);
214 static void rum_update_mcast(struct ieee80211com *);
215 static void rum_update_promisc(struct ieee80211com *);
216 static void rum_setpromisc(struct rum_softc *);
217 static const char *rum_get_rf(int);
218 static void rum_read_eeprom(struct rum_softc *);
219 static int rum_bbp_wakeup(struct rum_softc *);
220 static int rum_bbp_init(struct rum_softc *);
221 static void rum_clr_shkey_regs(struct rum_softc *);
222 static int rum_init(struct rum_softc *);
223 static void rum_stop(struct rum_softc *);
224 static void rum_load_microcode(struct rum_softc *, const uint8_t *,
226 static int rum_set_sleep_time(struct rum_softc *, uint16_t);
227 static int rum_reset(struct ieee80211vap *, u_long);
228 static int rum_set_beacon(struct rum_softc *,
229 struct ieee80211vap *);
230 static int rum_alloc_beacon(struct rum_softc *,
231 struct ieee80211vap *);
232 static void rum_update_beacon_cb(struct rum_softc *,
233 union sec_param *, uint8_t);
234 static void rum_update_beacon(struct ieee80211vap *, int);
235 static int rum_common_key_set(struct rum_softc *,
236 struct ieee80211_key *, uint16_t);
237 static void rum_group_key_set_cb(struct rum_softc *,
238 union sec_param *, uint8_t);
239 static void rum_group_key_del_cb(struct rum_softc *,
240 union sec_param *, uint8_t);
241 static void rum_pair_key_set_cb(struct rum_softc *,
242 union sec_param *, uint8_t);
243 static void rum_pair_key_del_cb(struct rum_softc *,
244 union sec_param *, uint8_t);
245 static int rum_key_alloc(struct ieee80211vap *,
246 struct ieee80211_key *, ieee80211_keyix *,
248 static int rum_key_set(struct ieee80211vap *,
249 const struct ieee80211_key *);
250 static int rum_key_delete(struct ieee80211vap *,
251 const struct ieee80211_key *);
252 static int rum_raw_xmit(struct ieee80211_node *, struct mbuf *,
253 const struct ieee80211_bpf_params *);
254 static void rum_scan_start(struct ieee80211com *);
255 static void rum_scan_end(struct ieee80211com *);
256 static void rum_set_channel(struct ieee80211com *);
257 static void rum_getradiocaps(struct ieee80211com *, int, int *,
258 struct ieee80211_channel[]);
259 static int rum_get_rssi(struct rum_softc *, uint8_t);
260 static void rum_ratectl_start(struct rum_softc *,
261 struct ieee80211_node *);
262 static void rum_ratectl_timeout(void *);
263 static void rum_ratectl_task(void *, int);
264 static int rum_pause(struct rum_softc *, int);
266 static const struct {
270 { RT2573_TXRX_CSR0, 0x025fb032 },
271 { RT2573_TXRX_CSR1, 0x9eaa9eaf },
272 { RT2573_TXRX_CSR2, 0x8a8b8c8d },
273 { RT2573_TXRX_CSR3, 0x00858687 },
274 { RT2573_TXRX_CSR7, 0x2e31353b },
275 { RT2573_TXRX_CSR8, 0x2a2a2a2c },
276 { RT2573_TXRX_CSR15, 0x0000000f },
277 { RT2573_MAC_CSR6, 0x00000fff },
278 { RT2573_MAC_CSR8, 0x016c030a },
279 { RT2573_MAC_CSR10, 0x00000718 },
280 { RT2573_MAC_CSR12, 0x00000004 },
281 { RT2573_MAC_CSR13, 0x00007f00 },
282 { RT2573_SEC_CSR2, 0x00000000 },
283 { RT2573_SEC_CSR3, 0x00000000 },
284 { RT2573_SEC_CSR4, 0x00000000 },
285 { RT2573_PHY_CSR1, 0x000023b0 },
286 { RT2573_PHY_CSR5, 0x00040a06 },
287 { RT2573_PHY_CSR6, 0x00080606 },
288 { RT2573_PHY_CSR7, 0x00000408 },
289 { RT2573_AIFSN_CSR, 0x00002273 },
290 { RT2573_CWMIN_CSR, 0x00002344 },
291 { RT2573_CWMAX_CSR, 0x000034aa }
294 static const struct {
326 static const uint8_t rum_chan_2ghz[] =
327 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };
329 static const uint8_t rum_chan_5ghz[] =
330 { 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64,
331 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140,
332 149, 153, 157, 161, 165 };
334 static const struct rfprog {
336 uint32_t r1, r2, r3, r4;
338 { 1, 0x00b03, 0x001e1, 0x1a014, 0x30282 },
339 { 2, 0x00b03, 0x001e1, 0x1a014, 0x30287 },
340 { 3, 0x00b03, 0x001e2, 0x1a014, 0x30282 },
341 { 4, 0x00b03, 0x001e2, 0x1a014, 0x30287 },
342 { 5, 0x00b03, 0x001e3, 0x1a014, 0x30282 },
343 { 6, 0x00b03, 0x001e3, 0x1a014, 0x30287 },
344 { 7, 0x00b03, 0x001e4, 0x1a014, 0x30282 },
345 { 8, 0x00b03, 0x001e4, 0x1a014, 0x30287 },
346 { 9, 0x00b03, 0x001e5, 0x1a014, 0x30282 },
347 { 10, 0x00b03, 0x001e5, 0x1a014, 0x30287 },
348 { 11, 0x00b03, 0x001e6, 0x1a014, 0x30282 },
349 { 12, 0x00b03, 0x001e6, 0x1a014, 0x30287 },
350 { 13, 0x00b03, 0x001e7, 0x1a014, 0x30282 },
351 { 14, 0x00b03, 0x001e8, 0x1a014, 0x30284 },
353 { 34, 0x00b03, 0x20266, 0x36014, 0x30282 },
354 { 38, 0x00b03, 0x20267, 0x36014, 0x30284 },
355 { 42, 0x00b03, 0x20268, 0x36014, 0x30286 },
356 { 46, 0x00b03, 0x20269, 0x36014, 0x30288 },
358 { 36, 0x00b03, 0x00266, 0x26014, 0x30288 },
359 { 40, 0x00b03, 0x00268, 0x26014, 0x30280 },
360 { 44, 0x00b03, 0x00269, 0x26014, 0x30282 },
361 { 48, 0x00b03, 0x0026a, 0x26014, 0x30284 },
362 { 52, 0x00b03, 0x0026b, 0x26014, 0x30286 },
363 { 56, 0x00b03, 0x0026c, 0x26014, 0x30288 },
364 { 60, 0x00b03, 0x0026e, 0x26014, 0x30280 },
365 { 64, 0x00b03, 0x0026f, 0x26014, 0x30282 },
367 { 100, 0x00b03, 0x0028a, 0x2e014, 0x30280 },
368 { 104, 0x00b03, 0x0028b, 0x2e014, 0x30282 },
369 { 108, 0x00b03, 0x0028c, 0x2e014, 0x30284 },
370 { 112, 0x00b03, 0x0028d, 0x2e014, 0x30286 },
371 { 116, 0x00b03, 0x0028e, 0x2e014, 0x30288 },
372 { 120, 0x00b03, 0x002a0, 0x2e014, 0x30280 },
373 { 124, 0x00b03, 0x002a1, 0x2e014, 0x30282 },
374 { 128, 0x00b03, 0x002a2, 0x2e014, 0x30284 },
375 { 132, 0x00b03, 0x002a3, 0x2e014, 0x30286 },
376 { 136, 0x00b03, 0x002a4, 0x2e014, 0x30288 },
377 { 140, 0x00b03, 0x002a6, 0x2e014, 0x30280 },
379 { 149, 0x00b03, 0x002a8, 0x2e014, 0x30287 },
380 { 153, 0x00b03, 0x002a9, 0x2e014, 0x30289 },
381 { 157, 0x00b03, 0x002ab, 0x2e014, 0x30281 },
382 { 161, 0x00b03, 0x002ac, 0x2e014, 0x30283 },
383 { 165, 0x00b03, 0x002ad, 0x2e014, 0x30285 }
385 { 1, 0x00b33, 0x011e1, 0x1a014, 0x30282 },
386 { 2, 0x00b33, 0x011e1, 0x1a014, 0x30287 },
387 { 3, 0x00b33, 0x011e2, 0x1a014, 0x30282 },
388 { 4, 0x00b33, 0x011e2, 0x1a014, 0x30287 },
389 { 5, 0x00b33, 0x011e3, 0x1a014, 0x30282 },
390 { 6, 0x00b33, 0x011e3, 0x1a014, 0x30287 },
391 { 7, 0x00b33, 0x011e4, 0x1a014, 0x30282 },
392 { 8, 0x00b33, 0x011e4, 0x1a014, 0x30287 },
393 { 9, 0x00b33, 0x011e5, 0x1a014, 0x30282 },
394 { 10, 0x00b33, 0x011e5, 0x1a014, 0x30287 },
395 { 11, 0x00b33, 0x011e6, 0x1a014, 0x30282 },
396 { 12, 0x00b33, 0x011e6, 0x1a014, 0x30287 },
397 { 13, 0x00b33, 0x011e7, 0x1a014, 0x30282 },
398 { 14, 0x00b33, 0x011e8, 0x1a014, 0x30284 },
400 { 34, 0x00b33, 0x01266, 0x26014, 0x30282 },
401 { 38, 0x00b33, 0x01267, 0x26014, 0x30284 },
402 { 42, 0x00b33, 0x01268, 0x26014, 0x30286 },
403 { 46, 0x00b33, 0x01269, 0x26014, 0x30288 },
405 { 36, 0x00b33, 0x01266, 0x26014, 0x30288 },
406 { 40, 0x00b33, 0x01268, 0x26014, 0x30280 },
407 { 44, 0x00b33, 0x01269, 0x26014, 0x30282 },
408 { 48, 0x00b33, 0x0126a, 0x26014, 0x30284 },
409 { 52, 0x00b33, 0x0126b, 0x26014, 0x30286 },
410 { 56, 0x00b33, 0x0126c, 0x26014, 0x30288 },
411 { 60, 0x00b33, 0x0126e, 0x26014, 0x30280 },
412 { 64, 0x00b33, 0x0126f, 0x26014, 0x30282 },
414 { 100, 0x00b33, 0x0128a, 0x2e014, 0x30280 },
415 { 104, 0x00b33, 0x0128b, 0x2e014, 0x30282 },
416 { 108, 0x00b33, 0x0128c, 0x2e014, 0x30284 },
417 { 112, 0x00b33, 0x0128d, 0x2e014, 0x30286 },
418 { 116, 0x00b33, 0x0128e, 0x2e014, 0x30288 },
419 { 120, 0x00b33, 0x012a0, 0x2e014, 0x30280 },
420 { 124, 0x00b33, 0x012a1, 0x2e014, 0x30282 },
421 { 128, 0x00b33, 0x012a2, 0x2e014, 0x30284 },
422 { 132, 0x00b33, 0x012a3, 0x2e014, 0x30286 },
423 { 136, 0x00b33, 0x012a4, 0x2e014, 0x30288 },
424 { 140, 0x00b33, 0x012a6, 0x2e014, 0x30280 },
426 { 149, 0x00b33, 0x012a8, 0x2e014, 0x30287 },
427 { 153, 0x00b33, 0x012a9, 0x2e014, 0x30289 },
428 { 157, 0x00b33, 0x012ab, 0x2e014, 0x30281 },
429 { 161, 0x00b33, 0x012ac, 0x2e014, 0x30283 },
430 { 165, 0x00b33, 0x012ad, 0x2e014, 0x30285 }
433 static const struct usb_config rum_config[RUM_N_TRANSFER] = {
436 .endpoint = UE_ADDR_ANY,
437 .direction = UE_DIR_OUT,
438 .bufsize = (MCLBYTES + RT2573_TX_DESC_SIZE + 8),
439 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
440 .callback = rum_bulk_write_callback,
441 .timeout = 5000, /* ms */
445 .endpoint = UE_ADDR_ANY,
446 .direction = UE_DIR_IN,
447 .bufsize = (MCLBYTES + RT2573_RX_DESC_SIZE),
448 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
449 .callback = rum_bulk_read_callback,
454 rum_match(device_t self)
456 struct usb_attach_arg *uaa = device_get_ivars(self);
458 if (uaa->usb_mode != USB_MODE_HOST)
460 if (uaa->info.bConfigIndex != 0)
462 if (uaa->info.bIfaceIndex != RT2573_IFACE_INDEX)
465 return (usbd_lookup_id_by_uaa(rum_devs, sizeof(rum_devs), uaa));
469 rum_attach(device_t self)
471 struct usb_attach_arg *uaa = device_get_ivars(self);
472 struct rum_softc *sc = device_get_softc(self);
473 struct ieee80211com *ic = &sc->sc_ic;
478 wlan_serialize_enter();
479 device_set_usb_desc(self);
480 sc->sc_udev = uaa->device;
484 RUM_CMDQ_LOCK_INIT(sc);
485 mbufq_init(&sc->sc_snd, ifqmaxlen);
487 iface_index = RT2573_IFACE_INDEX;
488 error = usbd_transfer_setup(uaa->device, &iface_index,
489 sc->sc_xfer, rum_config, RUM_N_TRANSFER, sc, &sc->sc_lock);
491 device_printf(self, "could not allocate USB transfers, "
492 "err=%s\n", usbd_errstr(error));
497 /* retrieve RT2573 rev. no */
498 for (ntries = 0; ntries < 100; ntries++) {
499 if ((tmp = rum_read(sc, RT2573_MAC_CSR0)) != 0)
501 if (rum_pause(sc, hz / 100))
505 device_printf(sc->sc_dev, "timeout waiting for chip to settle\n");
510 /* retrieve MAC address and various other things from EEPROM */
513 device_printf(sc->sc_dev, "MAC/BBP RT2573 (rev 0x%05x), RF %s\n",
514 tmp, rum_get_rf(sc->rf_rev));
516 rum_load_microcode(sc, rt2573_ucode, sizeof(rt2573_ucode));
520 ic->ic_name = device_get_nameunit(self);
521 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
523 /* set device capabilities */
525 IEEE80211_C_STA /* station mode supported */
526 | IEEE80211_C_IBSS /* IBSS mode supported */
527 | IEEE80211_C_MONITOR /* monitor mode supported */
528 | IEEE80211_C_HOSTAP /* HostAp mode supported */
529 | IEEE80211_C_AHDEMO /* adhoc demo mode */
530 | IEEE80211_C_TXPMGT /* tx power management */
531 | IEEE80211_C_SHPREAMBLE /* short preamble supported */
532 | IEEE80211_C_SHSLOT /* short slot time supported */
533 | IEEE80211_C_BGSCAN /* bg scanning supported */
534 | IEEE80211_C_WPA /* 802.11i */
535 | IEEE80211_C_WME /* 802.11e */
536 | IEEE80211_C_PMGT /* Station-side power mgmt */
537 | IEEE80211_C_SWSLEEP /* net80211 managed power mgmt */
541 IEEE80211_CRYPTO_WEP |
542 IEEE80211_CRYPTO_AES_CCM |
543 IEEE80211_CRYPTO_TKIPMIC |
544 IEEE80211_CRYPTO_TKIP;
546 rum_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
549 ieee80211_ifattach(ic);
550 ic->ic_update_promisc = rum_update_promisc;
551 ic->ic_raw_xmit = rum_raw_xmit;
552 ic->ic_scan_start = rum_scan_start;
553 ic->ic_scan_end = rum_scan_end;
554 ic->ic_set_channel = rum_set_channel;
555 ic->ic_getradiocaps = rum_getradiocaps;
556 ic->ic_transmit = rum_transmit;
557 ic->ic_parent = rum_parent;
558 ic->ic_vap_create = rum_vap_create;
559 ic->ic_vap_delete = rum_vap_delete;
560 ic->ic_updateslot = rum_update_slot;
561 ic->ic_wme.wme_update = rum_wme_update;
562 ic->ic_update_mcast = rum_update_mcast;
564 ieee80211_radiotap_attach(ic,
565 &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
566 RT2573_TX_RADIOTAP_PRESENT,
567 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
568 RT2573_RX_RADIOTAP_PRESENT);
570 TASK_INIT(&sc->cmdq_task, 0, rum_cmdq_cb, sc);
573 ieee80211_announce(ic);
575 wlan_serialize_exit();
579 wlan_serialize_exit();
581 return (ENXIO); /* failure */
585 rum_detach(device_t self)
587 struct rum_softc *sc = device_get_softc(self);
588 struct ieee80211com *ic = &sc->sc_ic;
590 /* Prevent further ioctls */
595 /* stop all USB transfers */
596 usbd_transfer_unsetup(sc->sc_xfer, RUM_N_TRANSFER);
598 /* free TX list, if any */
600 rum_unsetup_tx_list(sc);
603 if (ic->ic_softc == sc) {
604 ieee80211_draintask(ic, &sc->cmdq_task);
605 ieee80211_ifdetach(ic);
608 mbufq_drain(&sc->sc_snd);
609 RUM_CMDQ_LOCK_DESTROY(sc);
610 RUM_LOCK_DESTROY(sc);
616 rum_do_request(struct rum_softc *sc,
617 struct usb_device_request *req, void *data)
623 err = usbd_do_request_flags(sc->sc_udev, &sc->sc_lock,
624 req, data, 0, NULL, 250 /* ms */);
628 DPRINTFN(1, "Control request failed, %s (retrying)\n",
630 if (rum_pause(sc, hz / 100))
637 rum_do_mcu_request(struct rum_softc *sc, int request)
639 struct usb_device_request req;
641 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
642 req.bRequest = RT2573_MCU_CNTL;
643 USETW(req.wValue, request);
644 USETW(req.wIndex, 0);
645 USETW(req.wLength, 0);
647 return (rum_do_request(sc, &req, NULL));
650 static struct ieee80211vap *
651 rum_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
652 enum ieee80211_opmode opmode, int flags,
653 const uint8_t bssid[IEEE80211_ADDR_LEN],
654 const uint8_t mac[IEEE80211_ADDR_LEN])
656 struct rum_softc *sc = ic->ic_softc;
658 struct ieee80211vap *vap;
660 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */
662 rvp = kmalloc(sizeof(struct rum_vap), M_80211_VAP, M_WAITOK | M_ZERO);
664 /* enable s/w bmiss handling for sta mode */
666 if (ieee80211_vap_setup(ic, vap, name, unit, opmode,
667 flags | IEEE80211_CLONE_NOBEACONS, bssid) != 0) {
669 kfree(rvp, M_80211_VAP);
673 /* override state transition machine */
674 rvp->newstate = vap->iv_newstate;
675 vap->iv_newstate = rum_newstate;
676 vap->iv_key_alloc = rum_key_alloc;
677 vap->iv_key_set = rum_key_set;
678 vap->iv_key_delete = rum_key_delete;
679 vap->iv_update_beacon = rum_update_beacon;
680 vap->iv_reset = rum_reset;
681 vap->iv_max_aid = RT2573_ADDR_MAX;
683 if (opmode == IEEE80211_M_STA) {
685 * Move device to the sleep state when
686 * beacon is received and there is no data for us.
688 * Used only for IEEE80211_S_SLEEP state.
690 rvp->recv_mgmt = vap->iv_recv_mgmt;
691 vap->iv_recv_mgmt = rum_sta_recv_mgmt;
693 /* Ignored while sleeping. */
694 rvp->bmiss = vap->iv_bmiss;
695 vap->iv_bmiss = rum_beacon_miss;
698 usb_callout_init_mtx(&rvp->ratectl_ch, &sc->sc_lock, 0);
699 TASK_INIT(&rvp->ratectl_task, 0, rum_ratectl_task, rvp);
700 ieee80211_ratectl_init(vap);
701 ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */);
703 ieee80211_vap_attach(vap, ieee80211_media_change,
704 ieee80211_media_status, mac);
705 ic->ic_opmode = opmode;
710 rum_vap_delete(struct ieee80211vap *vap)
712 struct rum_vap *rvp = RUM_VAP(vap);
713 struct ieee80211com *ic = vap->iv_ic;
715 m_freem(rvp->bcn_mbuf);
716 usb_callout_drain(&rvp->ratectl_ch);
717 ieee80211_draintask(ic, &rvp->ratectl_task);
718 ieee80211_ratectl_deinit(vap);
719 ieee80211_vap_detach(vap);
720 kfree(rvp, M_80211_VAP);
724 rum_cmdq_cb(void *arg, int pending)
726 struct rum_softc *sc = arg;
730 while (sc->cmdq[sc->cmdq_first].func != NULL) {
731 rc = &sc->cmdq[sc->cmdq_first];
735 rc->func(sc, &rc->data, rc->rvp_id);
739 memset(rc, 0, sizeof (*rc));
740 sc->cmdq_first = (sc->cmdq_first + 1) % RUM_CMDQ_SIZE;
746 rum_cmd_sleepable(struct rum_softc *sc, const void *ptr, size_t len,
747 uint8_t rvp_id, CMD_FUNC_PROTO)
749 struct ieee80211com *ic = &sc->sc_ic;
751 KASSERT(len <= sizeof(union sec_param), ("buffer overflow"));
754 if (sc->cmdq[sc->cmdq_last].func != NULL) {
755 device_printf(sc->sc_dev, "%s: cmdq overflow\n", __func__);
762 memcpy(&sc->cmdq[sc->cmdq_last].data, ptr, len);
763 sc->cmdq[sc->cmdq_last].rvp_id = rvp_id;
764 sc->cmdq[sc->cmdq_last].func = func;
765 sc->cmdq_last = (sc->cmdq_last + 1) % RUM_CMDQ_SIZE;
768 ieee80211_runtask(ic, &sc->cmdq_task);
774 rum_tx_free(struct rum_tx_data *data, int txerr)
776 struct rum_softc *sc = data->sc;
778 if (data->m != NULL) {
779 ieee80211_tx_complete(data->ni, data->m, txerr);
783 STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
788 rum_setup_tx_list(struct rum_softc *sc)
790 struct rum_tx_data *data;
794 STAILQ_INIT(&sc->tx_q);
795 STAILQ_INIT(&sc->tx_free);
797 for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
798 data = &sc->tx_data[i];
801 STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
807 rum_unsetup_tx_list(struct rum_softc *sc)
809 struct rum_tx_data *data;
812 /* make sure any subsequent use of the queues will fail */
814 STAILQ_INIT(&sc->tx_q);
815 STAILQ_INIT(&sc->tx_free);
817 /* free up all node references and mbufs */
818 for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
819 data = &sc->tx_data[i];
821 if (data->m != NULL) {
825 if (data->ni != NULL) {
826 ieee80211_free_node(data->ni);
833 rum_beacon_miss(struct ieee80211vap *vap)
835 struct ieee80211com *ic = vap->iv_ic;
836 struct rum_softc *sc = ic->ic_softc;
837 struct rum_vap *rvp = RUM_VAP(vap);
841 if (sc->sc_sleeping && sc->sc_sleep_end < ticks) {
842 DPRINTFN(12, "dropping 'sleeping' bit, "
843 "device must be awake now\n");
848 sleep = sc->sc_sleeping;
855 DPRINTFN(13, "bmiss event is ignored whilst sleeping\n");
860 rum_sta_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m, int subtype,
861 const struct ieee80211_rx_stats *rxs,
864 struct ieee80211vap *vap = ni->ni_vap;
865 struct rum_softc *sc = vap->iv_ic->ic_softc;
866 struct rum_vap *rvp = RUM_VAP(vap);
868 if (vap->iv_state == IEEE80211_S_SLEEP &&
869 subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
871 DPRINTFN(12, "beacon, mybss %d (flags %02X)\n",
872 !!(sc->last_rx_flags & RT2573_RX_MYBSS),
875 if ((sc->last_rx_flags & (RT2573_RX_MYBSS | RT2573_RX_BC)) ==
876 (RT2573_RX_MYBSS | RT2573_RX_BC)) {
878 * Put it to sleep here; in case if there is a data
879 * for us, iv_recv_mgmt() will wakeup the device via
880 * SLEEP -> RUN state transition.
882 rum_set_power_state(sc, 1);
887 rvp->recv_mgmt(ni, m, subtype, rxs, rssi, nf);
891 rum_set_power_state(struct rum_softc *sc, int sleep)
897 DPRINTFN(12, "moving to %s state (sleep time %u)\n",
898 sleep ? "sleep" : "awake", sc->sc_sleep_time);
900 uerror = rum_do_mcu_request(sc,
901 sleep ? RT2573_MCU_SLEEP : RT2573_MCU_WAKEUP);
902 if (uerror != USB_ERR_NORMAL_COMPLETION) {
903 device_printf(sc->sc_dev,
904 "%s: could not change power state: %s\n",
905 __func__, usbd_errstr(uerror));
909 sc->sc_sleeping = !!sleep;
910 sc->sc_sleep_end = sleep ? ticks + sc->sc_sleep_time : 0;
916 rum_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
918 struct rum_vap *rvp = RUM_VAP(vap);
919 struct ieee80211com *ic = vap->iv_ic;
920 struct rum_softc *sc = ic->ic_softc;
921 const struct ieee80211_txparam *tp;
922 enum ieee80211_state ostate;
923 struct ieee80211_node *ni;
927 ostate = vap->iv_state;
928 DPRINTF("%s -> %s\n",
929 ieee80211_state_name[ostate],
930 ieee80211_state_name[nstate]);
932 IEEE80211_UNLOCK(ic);
934 usb_callout_stop(&rvp->ratectl_ch);
936 if (ostate == IEEE80211_S_SLEEP && vap->iv_opmode == IEEE80211_M_STA) {
937 rum_clrbits(sc, RT2573_TXRX_CSR4, RT2573_ACKCTS_PWRMGT);
938 rum_clrbits(sc, RT2573_MAC_CSR11, RT2573_AUTO_WAKEUP);
942 * any subsequent TX will wakeup it anyway
944 (void) rum_set_power_state(sc, 0);
948 case IEEE80211_S_INIT:
949 if (ostate == IEEE80211_S_RUN)
950 rum_abort_tsf_sync(sc);
954 case IEEE80211_S_RUN:
955 if (ostate == IEEE80211_S_SLEEP)
956 break; /* already handled */
958 ni = ieee80211_ref_node(vap->iv_bss);
960 if (vap->iv_opmode != IEEE80211_M_MONITOR) {
961 if (ic->ic_bsschan == IEEE80211_CHAN_ANYC ||
962 ni->ni_chan == IEEE80211_CHAN_ANYC) {
966 rum_update_slot_cb(sc, NULL, 0);
968 rum_set_txpreamble(sc);
969 rum_set_basicrates(sc);
970 rum_set_maxretry(sc, vap);
971 IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid);
972 rum_set_bssid(sc, sc->sc_bssid);
975 if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
976 vap->iv_opmode == IEEE80211_M_IBSS) {
977 if ((ret = rum_alloc_beacon(sc, vap)) != 0)
981 if (vap->iv_opmode != IEEE80211_M_MONITOR &&
982 vap->iv_opmode != IEEE80211_M_AHDEMO) {
983 if ((ret = rum_enable_tsf_sync(sc)) != 0)
988 /* enable automatic rate adaptation */
989 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
990 if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
991 rum_ratectl_start(sc, ni);
993 ieee80211_free_node(ni);
995 case IEEE80211_S_SLEEP:
996 /* Implemented for STA mode only. */
997 if (vap->iv_opmode != IEEE80211_M_STA)
1000 uerror = rum_setbits(sc, RT2573_MAC_CSR11, RT2573_AUTO_WAKEUP);
1001 if (uerror != USB_ERR_NORMAL_COMPLETION) {
1006 uerror = rum_setbits(sc, RT2573_TXRX_CSR4, RT2573_ACKCTS_PWRMGT);
1007 if (uerror != USB_ERR_NORMAL_COMPLETION) {
1012 ret = rum_set_power_state(sc, 1);
1014 device_printf(sc->sc_dev,
1015 "%s: could not move to the SLEEP state: %s\n",
1016 __func__, usbd_errstr(uerror));
1024 return (ret == 0 ? rvp->newstate(vap, nstate, arg) : ret);
1028 rum_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
1030 struct rum_softc *sc = usbd_xfer_softc(xfer);
1031 struct ieee80211vap *vap;
1032 struct rum_tx_data *data;
1034 struct usb_page_cache *pc;
1038 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
1040 switch (USB_GET_STATE(xfer)) {
1041 case USB_ST_TRANSFERRED:
1042 DPRINTFN(11, "transfer complete, %d bytes\n", actlen);
1044 /* free resources */
1045 data = usbd_xfer_get_priv(xfer);
1046 rum_tx_free(data, 0);
1047 usbd_xfer_set_priv(xfer, NULL);
1052 data = STAILQ_FIRST(&sc->tx_q);
1054 STAILQ_REMOVE_HEAD(&sc->tx_q, next);
1057 if (m->m_pkthdr.len > (int)(MCLBYTES + RT2573_TX_DESC_SIZE)) {
1058 DPRINTFN(0, "data overflow, %u bytes\n",
1060 m->m_pkthdr.len = (MCLBYTES + RT2573_TX_DESC_SIZE);
1062 pc = usbd_xfer_get_frame(xfer, 0);
1063 usbd_copy_in(pc, 0, &data->desc, RT2573_TX_DESC_SIZE);
1064 usbd_m_copy_in(pc, RT2573_TX_DESC_SIZE, m, 0,
1067 vap = data->ni->ni_vap;
1068 if (ieee80211_radiotap_active_vap(vap)) {
1069 struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
1072 tap->wt_rate = data->rate;
1073 rum_get_tsf(sc, &tap->wt_tsf);
1074 tap->wt_antenna = sc->tx_ant;
1076 ieee80211_radiotap_tx(vap, m);
1079 /* align end on a 4-bytes boundary */
1080 len = (RT2573_TX_DESC_SIZE + m->m_pkthdr.len + 3) & ~3;
1081 if ((len % 64) == 0)
1084 DPRINTFN(11, "sending frame len=%u xferlen=%u\n",
1085 m->m_pkthdr.len, len);
1087 usbd_xfer_set_frame_len(xfer, 0, len);
1088 usbd_xfer_set_priv(xfer, data);
1090 usbd_transfer_submit(xfer);
1095 default: /* Error */
1096 DPRINTFN(11, "transfer error, %s\n",
1097 usbd_errstr(error));
1099 #if defined(__DragonFly__)
1100 ++sc->sc_ic.ic_oerrors;
1102 counter_u64_add(sc->sc_ic.ic_oerrors, 1);
1104 data = usbd_xfer_get_priv(xfer);
1106 rum_tx_free(data, error);
1107 usbd_xfer_set_priv(xfer, NULL);
1110 if (error != USB_ERR_CANCELLED) {
1111 if (error == USB_ERR_TIMEOUT)
1112 device_printf(sc->sc_dev, "device timeout\n");
1115 * Try to clear stall first, also if other
1116 * errors occur, hence clearing stall
1117 * introduces a 50 ms delay:
1119 usbd_xfer_set_stall(xfer);
1127 rum_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
1129 struct rum_softc *sc = usbd_xfer_softc(xfer);
1130 struct ieee80211com *ic = &sc->sc_ic;
1131 struct ieee80211_frame_min *wh;
1132 struct ieee80211_node *ni;
1133 struct mbuf *m = NULL;
1134 struct usb_page_cache *pc;
1139 usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
1141 switch (USB_GET_STATE(xfer)) {
1142 case USB_ST_TRANSFERRED:
1144 DPRINTFN(15, "rx done, actlen=%d\n", len);
1146 if (len < (int)(RT2573_RX_DESC_SIZE + IEEE80211_MIN_LEN)) {
1147 DPRINTF("%s: xfer too short %d\n",
1148 device_get_nameunit(sc->sc_dev), len);
1149 #if defined(__DragonFly__)
1152 counter_u64_add(ic->ic_ierrors, 1);
1157 len -= RT2573_RX_DESC_SIZE;
1158 pc = usbd_xfer_get_frame(xfer, 0);
1159 usbd_copy_out(pc, 0, &sc->sc_rx_desc, RT2573_RX_DESC_SIZE);
1161 rssi = rum_get_rssi(sc, sc->sc_rx_desc.rssi);
1162 flags = le32toh(sc->sc_rx_desc.flags);
1163 sc->last_rx_flags = flags;
1164 if (flags & RT2573_RX_CRC_ERROR) {
1166 * This should not happen since we did not
1167 * request to receive those frames when we
1168 * filled RUM_TXRX_CSR2:
1170 DPRINTFN(5, "PHY or CRC error\n");
1171 #if defined(__DragonFly__)
1174 counter_u64_add(ic->ic_ierrors, 1);
1178 if ((flags & RT2573_RX_DEC_MASK) != RT2573_RX_DEC_OK) {
1179 switch (flags & RT2573_RX_DEC_MASK) {
1180 case RT2573_RX_IV_ERROR:
1181 DPRINTFN(5, "IV/EIV error\n");
1183 case RT2573_RX_MIC_ERROR:
1184 DPRINTFN(5, "MIC error\n");
1186 case RT2573_RX_KEY_ERROR:
1187 DPRINTFN(5, "Key error\n");
1190 #if defined(__DragonFly__)
1193 counter_u64_add(ic->ic_ierrors, 1);
1198 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1200 DPRINTF("could not allocate mbuf\n");
1201 #if defined(__DragonFly__)
1204 counter_u64_add(ic->ic_ierrors, 1);
1208 usbd_copy_out(pc, RT2573_RX_DESC_SIZE,
1209 mtod(m, uint8_t *), len);
1211 wh = mtod(m, struct ieee80211_frame_min *);
1213 if ((wh->i_fc[1] & IEEE80211_FC1_PROTECTED) &&
1214 (flags & RT2573_RX_CIP_MASK) !=
1215 RT2573_RX_CIP_MODE(RT2573_MODE_NOSEC)) {
1216 wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
1217 m->m_flags |= M_WEP;
1221 m->m_pkthdr.len = m->m_len = (flags >> 16) & 0xfff;
1223 if (ieee80211_radiotap_active(ic)) {
1224 struct rum_rx_radiotap_header *tap = &sc->sc_rxtap;
1227 tap->wr_rate = ieee80211_plcp2rate(sc->sc_rx_desc.rate,
1228 (flags & RT2573_RX_OFDM) ?
1229 IEEE80211_T_OFDM : IEEE80211_T_CCK);
1230 rum_get_tsf(sc, &tap->wr_tsf);
1231 tap->wr_antsignal = RT2573_NOISE_FLOOR + rssi;
1232 tap->wr_antnoise = RT2573_NOISE_FLOOR;
1233 tap->wr_antenna = sc->rx_ant;
1238 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1239 usbd_transfer_submit(xfer);
1242 * At the end of a USB callback it is always safe to unlock
1243 * the private mutex of a device! That is why we do the
1244 * "ieee80211_input" here, and not some lines up!
1248 if (m->m_len >= sizeof(struct ieee80211_frame_min))
1249 ni = ieee80211_find_rxnode(ic, wh);
1254 (void) ieee80211_input(ni, m, rssi,
1255 RT2573_NOISE_FLOOR);
1256 ieee80211_free_node(ni);
1258 (void) ieee80211_input_all(ic, m, rssi,
1259 RT2573_NOISE_FLOOR);
1265 default: /* Error */
1266 if (error != USB_ERR_CANCELLED) {
1267 /* try to clear stall first */
1268 usbd_xfer_set_stall(xfer);
1276 rum_plcp_signal(int rate)
1279 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1280 case 12: return 0xb;
1281 case 18: return 0xf;
1282 case 24: return 0xa;
1283 case 36: return 0xe;
1284 case 48: return 0x9;
1285 case 72: return 0xd;
1286 case 96: return 0x8;
1287 case 108: return 0xc;
1289 /* CCK rates (NB: not IEEE std, device-specific) */
1292 case 11: return 0x2;
1293 case 22: return 0x3;
1295 return 0xff; /* XXX unsupported/unknown rate */
1299 * Map net80211 cipher to RT2573 security mode.
1302 rum_crypto_mode(struct rum_softc *sc, u_int cipher, int keylen)
1305 case IEEE80211_CIPHER_WEP:
1306 return (keylen < 8 ? RT2573_MODE_WEP40 : RT2573_MODE_WEP104);
1307 case IEEE80211_CIPHER_TKIP:
1308 return RT2573_MODE_TKIP;
1309 case IEEE80211_CIPHER_AES_CCM:
1310 return RT2573_MODE_AES_CCMP;
1312 device_printf(sc->sc_dev, "unknown cipher %d\n", cipher);
1318 rum_setup_tx_desc(struct rum_softc *sc, struct rum_tx_desc *desc,
1319 struct ieee80211_key *k, uint32_t flags, uint8_t xflags, uint8_t qid,
1320 int hdrlen, int len, int rate)
1322 struct ieee80211com *ic = &sc->sc_ic;
1323 struct wmeParams *wmep = &sc->wme_params[qid];
1324 uint16_t plcp_length;
1327 flags |= RT2573_TX_VALID;
1330 if (k != NULL && !(k->wk_flags & IEEE80211_KEY_SWCRYPT)) {
1331 const struct ieee80211_cipher *cip = k->wk_cipher;
1333 len += cip->ic_header + cip->ic_trailer + cip->ic_miclen;
1335 desc->eiv = 0; /* for WEP */
1336 cip->ic_setiv(k, (uint8_t *)&desc->iv);
1339 /* setup PLCP fields */
1340 desc->plcp_signal = rum_plcp_signal(rate);
1341 desc->plcp_service = 4;
1343 len += IEEE80211_CRC_LEN;
1344 if (ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) {
1345 flags |= RT2573_TX_OFDM;
1347 plcp_length = len & 0xfff;
1348 desc->plcp_length_hi = plcp_length >> 6;
1349 desc->plcp_length_lo = plcp_length & 0x3f;
1352 rate = 2; /* avoid division by zero */
1353 plcp_length = howmany(16 * len, rate);
1355 remainder = (16 * len) % 22;
1356 if (remainder != 0 && remainder < 7)
1357 desc->plcp_service |= RT2573_PLCP_LENGEXT;
1359 desc->plcp_length_hi = plcp_length >> 8;
1360 desc->plcp_length_lo = plcp_length & 0xff;
1362 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1363 desc->plcp_signal |= 0x08;
1366 desc->flags = htole32(flags);
1367 desc->hdrlen = hdrlen;
1368 desc->xflags = xflags;
1370 desc->wme = htole16(RT2573_QID(qid) |
1371 RT2573_AIFSN(wmep->wmep_aifsn) |
1372 RT2573_LOGCWMIN(wmep->wmep_logcwmin) |
1373 RT2573_LOGCWMAX(wmep->wmep_logcwmax));
1377 rum_sendprot(struct rum_softc *sc,
1378 const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate)
1380 struct ieee80211com *ic = ni->ni_ic;
1381 const struct ieee80211_frame *wh;
1382 struct rum_tx_data *data;
1384 int protrate, pktlen, flags, isshort;
1387 RUM_LOCK_ASSERT(sc);
1388 KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY,
1389 ("protection %d", prot));
1391 wh = mtod(m, const struct ieee80211_frame *);
1392 pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
1394 protrate = ieee80211_ctl_rate(ic->ic_rt, rate);
1396 isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0;
1397 dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort)
1398 + ieee80211_ack_duration(ic->ic_rt, rate, isshort);
1400 if (prot == IEEE80211_PROT_RTSCTS) {
1401 /* NB: CTS is the same size as an ACK */
1402 dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort);
1403 flags |= RT2573_TX_NEED_ACK;
1404 mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur);
1406 mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur);
1408 if (mprot == NULL) {
1409 /* XXX stat + msg */
1412 data = STAILQ_FIRST(&sc->tx_free);
1413 STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1417 data->ni = ieee80211_ref_node(ni);
1418 data->rate = protrate;
1419 rum_setup_tx_desc(sc, &data->desc, NULL, flags, 0, 0, 0,
1420 mprot->m_pkthdr.len, protrate);
1422 STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1423 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1429 rum_tx_crypto_flags(struct rum_softc *sc, struct ieee80211_node *ni,
1430 const struct ieee80211_key *k)
1432 struct ieee80211vap *vap = ni->ni_vap;
1437 if (!(k->wk_flags & IEEE80211_KEY_SWCRYPT)) {
1438 cipher = k->wk_cipher->ic_cipher;
1440 mode = rum_crypto_mode(sc, cipher, k->wk_keylen);
1444 flags |= RT2573_TX_CIP_MODE(mode);
1446 /* Do not trust GROUP flag */
1447 if (!(k >= &vap->iv_nw_keys[0] &&
1448 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID]))
1449 flags |= RT2573_TX_KEY_PAIR;
1451 pos += 0 * RT2573_SKEY_MAX; /* vap id */
1453 flags |= RT2573_TX_KEY_ID(pos);
1455 if (cipher == IEEE80211_CIPHER_TKIP)
1456 flags |= RT2573_TX_TKIPMIC;
1463 rum_tx_mgt(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1465 struct ieee80211vap *vap = ni->ni_vap;
1466 struct ieee80211com *ic = &sc->sc_ic;
1467 struct rum_tx_data *data;
1468 struct ieee80211_frame *wh;
1469 const struct ieee80211_txparam *tp;
1470 struct ieee80211_key *k = NULL;
1473 uint8_t ac, type, xflags = 0;
1476 RUM_LOCK_ASSERT(sc);
1478 data = STAILQ_FIRST(&sc->tx_free);
1479 STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1482 wh = mtod(m0, struct ieee80211_frame *);
1483 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1484 hdrlen = ieee80211_anyhdrsize(wh);
1485 ac = M_WME_GETAC(m0);
1487 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1488 k = ieee80211_crypto_get_txkey(ni, m0);
1492 if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) &&
1493 !k->wk_cipher->ic_encap(k, m0))
1496 wh = mtod(m0, struct ieee80211_frame *);
1499 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1501 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1502 flags |= RT2573_TX_NEED_ACK;
1504 dur = ieee80211_ack_duration(ic->ic_rt, tp->mgmtrate,
1505 ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1506 USETW(wh->i_dur, dur);
1508 /* tell hardware to add timestamp for probe responses */
1509 if (type == IEEE80211_FC0_TYPE_MGT &&
1510 (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
1511 IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1512 flags |= RT2573_TX_TIMESTAMP;
1515 if (type != IEEE80211_FC0_TYPE_CTL && !IEEE80211_QOS_HAS_SEQ(wh))
1516 xflags |= RT2573_TX_HWSEQ;
1519 flags |= rum_tx_crypto_flags(sc, ni, k);
1523 data->rate = tp->mgmtrate;
1525 rum_setup_tx_desc(sc, &data->desc, k, flags, xflags, ac, hdrlen,
1526 m0->m_pkthdr.len, tp->mgmtrate);
1528 DPRINTFN(10, "sending mgt frame len=%d rate=%d\n",
1529 m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, tp->mgmtrate);
1531 STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1532 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1538 rum_tx_raw(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
1539 const struct ieee80211_bpf_params *params)
1541 struct ieee80211com *ic = ni->ni_ic;
1542 struct ieee80211_frame *wh;
1543 struct rum_tx_data *data;
1545 uint8_t ac, type, xflags = 0;
1548 RUM_LOCK_ASSERT(sc);
1550 wh = mtod(m0, struct ieee80211_frame *);
1551 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1553 ac = params->ibp_pri & 3;
1555 rate = params->ibp_rate0;
1556 if (!ieee80211_isratevalid(ic->ic_rt, rate))
1560 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
1561 flags |= RT2573_TX_NEED_ACK;
1562 if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) {
1563 error = rum_sendprot(sc, m0, ni,
1564 params->ibp_flags & IEEE80211_BPF_RTS ?
1565 IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY,
1567 if (error || sc->tx_nfree == 0)
1570 flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
1573 if (type != IEEE80211_FC0_TYPE_CTL && !IEEE80211_QOS_HAS_SEQ(wh))
1574 xflags |= RT2573_TX_HWSEQ;
1576 data = STAILQ_FIRST(&sc->tx_free);
1577 STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1584 /* XXX need to setup descriptor ourself */
1585 rum_setup_tx_desc(sc, &data->desc, NULL, flags, xflags, ac, 0,
1586 m0->m_pkthdr.len, rate);
1588 DPRINTFN(10, "sending raw frame len=%u rate=%u\n",
1589 m0->m_pkthdr.len, rate);
1591 STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1592 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1598 rum_tx_data(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1600 struct ieee80211vap *vap = ni->ni_vap;
1601 struct ieee80211com *ic = &sc->sc_ic;
1602 struct rum_tx_data *data;
1603 struct ieee80211_frame *wh;
1604 const struct ieee80211_txparam *tp;
1605 struct ieee80211_key *k = NULL;
1608 uint8_t ac, type, qos, xflags = 0;
1609 int error, hdrlen, rate;
1611 RUM_LOCK_ASSERT(sc);
1613 wh = mtod(m0, struct ieee80211_frame *);
1614 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1615 hdrlen = ieee80211_anyhdrsize(wh);
1617 if (IEEE80211_QOS_HAS_SEQ(wh))
1618 qos = ((const struct ieee80211_qosframe *)wh)->i_qos[0];
1621 ac = M_WME_GETAC(m0);
1623 tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
1624 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1625 rate = tp->mcastrate;
1626 else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
1627 rate = tp->ucastrate;
1629 rate = ni->ni_txrate;
1631 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1632 k = ieee80211_crypto_get_txkey(ni, m0);
1637 if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) &&
1638 !k->wk_cipher->ic_encap(k, m0)) {
1643 /* packet header may have moved, reset our local pointer */
1644 wh = mtod(m0, struct ieee80211_frame *);
1647 if (type != IEEE80211_FC0_TYPE_CTL && !IEEE80211_QOS_HAS_SEQ(wh))
1648 xflags |= RT2573_TX_HWSEQ;
1650 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1651 int prot = IEEE80211_PROT_NONE;
1652 if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold)
1653 prot = IEEE80211_PROT_RTSCTS;
1654 else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
1655 ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM)
1656 prot = ic->ic_protmode;
1657 if (prot != IEEE80211_PROT_NONE) {
1658 error = rum_sendprot(sc, m0, ni, prot, rate);
1659 if (error || sc->tx_nfree == 0) {
1663 flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
1668 flags |= rum_tx_crypto_flags(sc, ni, k);
1670 data = STAILQ_FIRST(&sc->tx_free);
1671 STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1678 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1679 /* Unicast frame, check if an ACK is expected. */
1680 if (!qos || (qos & IEEE80211_QOS_ACKPOLICY) !=
1681 IEEE80211_QOS_ACKPOLICY_NOACK)
1682 flags |= RT2573_TX_NEED_ACK;
1684 dur = ieee80211_ack_duration(ic->ic_rt, rate,
1685 ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1686 USETW(wh->i_dur, dur);
1689 rum_setup_tx_desc(sc, &data->desc, k, flags, xflags, ac, hdrlen,
1690 m0->m_pkthdr.len, rate);
1692 DPRINTFN(10, "sending frame len=%d rate=%d\n",
1693 m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, rate);
1695 STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1696 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1702 rum_transmit(struct ieee80211com *ic, struct mbuf *m)
1704 struct rum_softc *sc = ic->ic_softc;
1708 if (!sc->sc_running) {
1712 error = mbufq_enqueue(&sc->sc_snd, m);
1724 rum_start(struct rum_softc *sc)
1726 struct ieee80211_node *ni;
1729 RUM_LOCK_ASSERT(sc);
1731 if (!sc->sc_running)
1734 while (sc->tx_nfree >= RUM_TX_MINFREE &&
1735 (m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
1736 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1737 if (rum_tx_data(sc, m, ni) != 0) {
1738 if_inc_counter(ni->ni_vap->iv_ifp,
1739 IFCOUNTER_OERRORS, 1);
1740 ieee80211_free_node(ni);
1747 rum_parent(struct ieee80211com *ic)
1749 struct rum_softc *sc = ic->ic_softc;
1750 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1753 if (sc->sc_detached) {
1759 if (ic->ic_nrunning > 0) {
1760 if (rum_init(sc) == 0)
1761 ieee80211_start_all(ic);
1763 ieee80211_stop(vap);
1769 rum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len)
1771 struct usb_device_request req;
1774 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1775 req.bRequest = RT2573_READ_EEPROM;
1776 USETW(req.wValue, 0);
1777 USETW(req.wIndex, addr);
1778 USETW(req.wLength, len);
1780 error = rum_do_request(sc, &req, buf);
1782 device_printf(sc->sc_dev, "could not read EEPROM: %s\n",
1783 usbd_errstr(error));
1788 rum_read(struct rum_softc *sc, uint16_t reg)
1792 rum_read_multi(sc, reg, &val, sizeof val);
1794 return le32toh(val);
1798 rum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len)
1800 struct usb_device_request req;
1803 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1804 req.bRequest = RT2573_READ_MULTI_MAC;
1805 USETW(req.wValue, 0);
1806 USETW(req.wIndex, reg);
1807 USETW(req.wLength, len);
1809 error = rum_do_request(sc, &req, buf);
1811 device_printf(sc->sc_dev,
1812 "could not multi read MAC register: %s\n",
1813 usbd_errstr(error));
1818 rum_write(struct rum_softc *sc, uint16_t reg, uint32_t val)
1820 uint32_t tmp = htole32(val);
1822 return (rum_write_multi(sc, reg, &tmp, sizeof tmp));
1826 rum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len)
1828 struct usb_device_request req;
1832 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1833 req.bRequest = RT2573_WRITE_MULTI_MAC;
1834 USETW(req.wValue, 0);
1836 /* write at most 64 bytes at a time */
1837 for (offset = 0; offset < len; offset += 64) {
1838 USETW(req.wIndex, reg + offset);
1839 USETW(req.wLength, MIN(len - offset, 64));
1841 error = rum_do_request(sc, &req, (char *)buf + offset);
1843 device_printf(sc->sc_dev,
1844 "could not multi write MAC register: %s\n",
1845 usbd_errstr(error));
1850 return (USB_ERR_NORMAL_COMPLETION);
1854 rum_setbits(struct rum_softc *sc, uint16_t reg, uint32_t mask)
1856 return (rum_write(sc, reg, rum_read(sc, reg) | mask));
1860 rum_clrbits(struct rum_softc *sc, uint16_t reg, uint32_t mask)
1862 return (rum_write(sc, reg, rum_read(sc, reg) & ~mask));
1866 rum_modbits(struct rum_softc *sc, uint16_t reg, uint32_t set, uint32_t unset)
1868 return (rum_write(sc, reg, (rum_read(sc, reg) & ~unset) | set));
1872 rum_bbp_busy(struct rum_softc *sc)
1876 for (ntries = 0; ntries < 100; ntries++) {
1877 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1879 if (rum_pause(sc, hz / 100))
1889 rum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val)
1893 DPRINTFN(2, "reg=0x%08x\n", reg);
1895 if (rum_bbp_busy(sc) != 0) {
1896 device_printf(sc->sc_dev, "could not write to BBP\n");
1900 tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val;
1901 rum_write(sc, RT2573_PHY_CSR3, tmp);
1905 rum_bbp_read(struct rum_softc *sc, uint8_t reg)
1910 DPRINTFN(2, "reg=0x%08x\n", reg);
1912 if (rum_bbp_busy(sc) != 0) {
1913 device_printf(sc->sc_dev, "could not read BBP\n");
1917 val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8;
1918 rum_write(sc, RT2573_PHY_CSR3, val);
1920 for (ntries = 0; ntries < 100; ntries++) {
1921 val = rum_read(sc, RT2573_PHY_CSR3);
1922 if (!(val & RT2573_BBP_BUSY))
1924 if (rum_pause(sc, hz / 100))
1928 device_printf(sc->sc_dev, "could not read BBP\n");
1933 rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val)
1938 for (ntries = 0; ntries < 100; ntries++) {
1939 if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY))
1941 if (rum_pause(sc, hz / 100))
1944 if (ntries == 100) {
1945 device_printf(sc->sc_dev, "could not write to RF\n");
1949 tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 |
1951 rum_write(sc, RT2573_PHY_CSR4, tmp);
1953 /* remember last written value in sc */
1954 sc->rf_regs[reg] = val;
1956 DPRINTFN(15, "RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff);
1960 rum_select_antenna(struct rum_softc *sc)
1962 uint8_t bbp4, bbp77;
1965 bbp4 = rum_bbp_read(sc, 4);
1966 bbp77 = rum_bbp_read(sc, 77);
1970 /* make sure Rx is disabled before switching antenna */
1971 tmp = rum_read(sc, RT2573_TXRX_CSR0);
1972 rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
1974 rum_bbp_write(sc, 4, bbp4);
1975 rum_bbp_write(sc, 77, bbp77);
1977 rum_write(sc, RT2573_TXRX_CSR0, tmp);
1981 * Enable multi-rate retries for frames sent at OFDM rates.
1982 * In 802.11b/g mode, allow fallback to CCK rates.
1985 rum_enable_mrr(struct rum_softc *sc)
1987 struct ieee80211com *ic = &sc->sc_ic;
1989 if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) {
1990 rum_setbits(sc, RT2573_TXRX_CSR4,
1991 RT2573_MRR_ENABLED | RT2573_MRR_CCK_FALLBACK);
1993 rum_modbits(sc, RT2573_TXRX_CSR4,
1994 RT2573_MRR_ENABLED, RT2573_MRR_CCK_FALLBACK);
1999 rum_set_txpreamble(struct rum_softc *sc)
2001 struct ieee80211com *ic = &sc->sc_ic;
2003 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
2004 rum_setbits(sc, RT2573_TXRX_CSR4, RT2573_SHORT_PREAMBLE);
2006 rum_clrbits(sc, RT2573_TXRX_CSR4, RT2573_SHORT_PREAMBLE);
2010 rum_set_basicrates(struct rum_softc *sc)
2012 struct ieee80211com *ic = &sc->sc_ic;
2014 /* update basic rate set */
2015 if (ic->ic_curmode == IEEE80211_MODE_11B) {
2016 /* 11b basic rates: 1, 2Mbps */
2017 rum_write(sc, RT2573_TXRX_CSR5, 0x3);
2018 } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) {
2019 /* 11a basic rates: 6, 12, 24Mbps */
2020 rum_write(sc, RT2573_TXRX_CSR5, 0x150);
2022 /* 11b/g basic rates: 1, 2, 5.5, 11Mbps */
2023 rum_write(sc, RT2573_TXRX_CSR5, 0xf);
2028 * Reprogram MAC/BBP to switch to a new band. Values taken from the reference
2032 rum_select_band(struct rum_softc *sc, struct ieee80211_channel *c)
2034 uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104;
2036 /* update all BBP registers that depend on the band */
2037 bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c;
2038 bbp35 = 0x50; bbp97 = 0x48; bbp98 = 0x48;
2039 if (IEEE80211_IS_CHAN_5GHZ(c)) {
2040 bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c;
2041 bbp35 += 0x10; bbp97 += 0x10; bbp98 += 0x10;
2043 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
2044 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
2045 bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10;
2049 rum_bbp_write(sc, 17, bbp17);
2050 rum_bbp_write(sc, 96, bbp96);
2051 rum_bbp_write(sc, 104, bbp104);
2053 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
2054 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
2055 rum_bbp_write(sc, 75, 0x80);
2056 rum_bbp_write(sc, 86, 0x80);
2057 rum_bbp_write(sc, 88, 0x80);
2060 rum_bbp_write(sc, 35, bbp35);
2061 rum_bbp_write(sc, 97, bbp97);
2062 rum_bbp_write(sc, 98, bbp98);
2064 if (IEEE80211_IS_CHAN_2GHZ(c)) {
2065 rum_modbits(sc, RT2573_PHY_CSR0, RT2573_PA_PE_2GHZ,
2068 rum_modbits(sc, RT2573_PHY_CSR0, RT2573_PA_PE_5GHZ,
2074 rum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c)
2076 struct ieee80211com *ic = &sc->sc_ic;
2077 const struct rfprog *rfprog;
2078 uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT;
2082 chan = ieee80211_chan2ieee(ic, c);
2083 if (chan == 0 || chan == IEEE80211_CHAN_ANY)
2086 /* select the appropriate RF settings based on what EEPROM says */
2087 rfprog = (sc->rf_rev == RT2573_RF_5225 ||
2088 sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226;
2090 /* find the settings for this channel (we know it exists) */
2091 for (i = 0; rfprog[i].chan != chan; i++);
2093 power = sc->txpow[i];
2097 } else if (power > 31) {
2098 bbp94 += power - 31;
2103 * If we are switching from the 2GHz band to the 5GHz band or
2104 * vice-versa, BBP registers need to be reprogrammed.
2106 if (c->ic_flags != ic->ic_curchan->ic_flags) {
2107 rum_select_band(sc, c);
2108 rum_select_antenna(sc);
2112 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
2113 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
2114 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
2115 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
2117 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
2118 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
2119 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7 | 1);
2120 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
2122 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
2123 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
2124 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
2125 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
2127 rum_pause(sc, hz / 100);
2129 /* enable smart mode for MIMO-capable RFs */
2130 bbp3 = rum_bbp_read(sc, 3);
2132 bbp3 &= ~RT2573_SMART_MODE;
2133 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527)
2134 bbp3 |= RT2573_SMART_MODE;
2136 rum_bbp_write(sc, 3, bbp3);
2138 if (bbp94 != RT2573_BBPR94_DEFAULT)
2139 rum_bbp_write(sc, 94, bbp94);
2141 /* give the chip some extra time to do the switchover */
2142 rum_pause(sc, hz / 100);
2146 rum_set_maxretry(struct rum_softc *sc, struct ieee80211vap *vap)
2148 const struct ieee80211_txparam *tp;
2149 struct ieee80211_node *ni = vap->iv_bss;
2150 struct rum_vap *rvp = RUM_VAP(vap);
2152 tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
2153 rvp->maxretry = tp->maxretry < 0xf ? tp->maxretry : 0xf;
2155 rum_modbits(sc, RT2573_TXRX_CSR4, RT2573_SHORT_RETRY(rvp->maxretry) |
2156 RT2573_LONG_RETRY(rvp->maxretry),
2157 RT2573_SHORT_RETRY_MASK | RT2573_LONG_RETRY_MASK);
2161 * Enable TSF synchronization and tell h/w to start sending beacons for IBSS
2162 * and HostAP operating modes.
2165 rum_enable_tsf_sync(struct rum_softc *sc)
2167 struct ieee80211com *ic = &sc->sc_ic;
2168 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2172 if (vap->iv_opmode != IEEE80211_M_STA) {
2174 * Change default 16ms TBTT adjustment to 8ms.
2175 * Must be done before enabling beacon generation.
2177 if (rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8) != 0)
2181 tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000;
2183 /* set beacon interval (in 1/16ms unit) */
2184 bintval = vap->iv_bss->ni_intval;
2185 tmp |= bintval * 16;
2186 tmp |= RT2573_TSF_TIMER_EN | RT2573_TBTT_TIMER_EN;
2188 switch (vap->iv_opmode) {
2189 case IEEE80211_M_STA:
2191 * Local TSF is always updated with remote TSF on beacon
2194 tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_STA);
2196 case IEEE80211_M_IBSS:
2198 * Local TSF is updated with remote TSF on beacon reception
2199 * only if the remote TSF is greater than local TSF.
2201 tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_IBSS);
2202 tmp |= RT2573_BCN_TX_EN;
2204 case IEEE80211_M_HOSTAP:
2205 /* SYNC with nobody */
2206 tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_HOSTAP);
2207 tmp |= RT2573_BCN_TX_EN;
2210 device_printf(sc->sc_dev,
2211 "Enabling TSF failed. undefined opmode %d\n",
2216 if (rum_write(sc, RT2573_TXRX_CSR9, tmp) != 0)
2219 /* refresh current sleep time */
2220 return (rum_set_sleep_time(sc, bintval));
2224 rum_enable_tsf(struct rum_softc *sc)
2226 rum_modbits(sc, RT2573_TXRX_CSR9, RT2573_TSF_TIMER_EN |
2227 RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_DIS), 0x00ffffff);
2231 rum_abort_tsf_sync(struct rum_softc *sc)
2233 rum_clrbits(sc, RT2573_TXRX_CSR9, 0x00ffffff);
2237 rum_get_tsf(struct rum_softc *sc, uint64_t *buf)
2239 rum_read_multi(sc, RT2573_TXRX_CSR12, buf, sizeof (*buf));
2243 rum_update_slot_cb(struct rum_softc *sc, union sec_param *data, uint8_t rvp_id)
2245 struct ieee80211com *ic = &sc->sc_ic;
2248 slottime = IEEE80211_GET_SLOTTIME(ic);
2250 rum_modbits(sc, RT2573_MAC_CSR9, slottime, 0xff);
2252 DPRINTF("setting slot time to %uus\n", slottime);
2256 rum_update_slot(struct ieee80211com *ic)
2258 rum_cmd_sleepable(ic->ic_softc, NULL, 0, 0, rum_update_slot_cb);
2262 rum_wme_update(struct ieee80211com *ic)
2264 const struct wmeParams *chanp =
2265 ic->ic_wme.wme_chanParams.cap_wmeParams;
2266 struct rum_softc *sc = ic->ic_softc;
2270 error = rum_write(sc, RT2573_AIFSN_CSR,
2271 chanp[WME_AC_VO].wmep_aifsn << 12 |
2272 chanp[WME_AC_VI].wmep_aifsn << 8 |
2273 chanp[WME_AC_BK].wmep_aifsn << 4 |
2274 chanp[WME_AC_BE].wmep_aifsn);
2277 error = rum_write(sc, RT2573_CWMIN_CSR,
2278 chanp[WME_AC_VO].wmep_logcwmin << 12 |
2279 chanp[WME_AC_VI].wmep_logcwmin << 8 |
2280 chanp[WME_AC_BK].wmep_logcwmin << 4 |
2281 chanp[WME_AC_BE].wmep_logcwmin);
2284 error = rum_write(sc, RT2573_CWMAX_CSR,
2285 chanp[WME_AC_VO].wmep_logcwmax << 12 |
2286 chanp[WME_AC_VI].wmep_logcwmax << 8 |
2287 chanp[WME_AC_BK].wmep_logcwmax << 4 |
2288 chanp[WME_AC_BE].wmep_logcwmax);
2291 error = rum_write(sc, RT2573_TXOP01_CSR,
2292 chanp[WME_AC_BK].wmep_txopLimit << 16 |
2293 chanp[WME_AC_BE].wmep_txopLimit);
2296 error = rum_write(sc, RT2573_TXOP23_CSR,
2297 chanp[WME_AC_VO].wmep_txopLimit << 16 |
2298 chanp[WME_AC_VI].wmep_txopLimit);
2302 memcpy(sc->wme_params, chanp, sizeof(*chanp) * WME_NUM_AC);
2307 device_printf(sc->sc_dev, "%s: WME update failed, error %d\n",
2315 rum_set_bssid(struct rum_softc *sc, const uint8_t *bssid)
2318 rum_write(sc, RT2573_MAC_CSR4,
2319 bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24);
2320 rum_write(sc, RT2573_MAC_CSR5,
2321 bssid[4] | bssid[5] << 8 | RT2573_NUM_BSSID_MSK(1));
2325 rum_set_macaddr(struct rum_softc *sc, const uint8_t *addr)
2328 rum_write(sc, RT2573_MAC_CSR2,
2329 addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
2330 rum_write(sc, RT2573_MAC_CSR3,
2331 addr[4] | addr[5] << 8 | 0xff << 16);
2335 rum_setpromisc(struct rum_softc *sc)
2337 struct ieee80211com *ic = &sc->sc_ic;
2339 if (ic->ic_promisc == 0)
2340 rum_setbits(sc, RT2573_TXRX_CSR0, RT2573_DROP_NOT_TO_ME);
2342 rum_clrbits(sc, RT2573_TXRX_CSR0, RT2573_DROP_NOT_TO_ME);
2344 DPRINTF("%s promiscuous mode\n", ic->ic_promisc > 0 ?
2345 "entering" : "leaving");
2349 rum_update_promisc(struct ieee80211com *ic)
2351 struct rum_softc *sc = ic->ic_softc;
2360 rum_update_mcast(struct ieee80211com *ic)
2369 case RT2573_RF_2527: return "RT2527 (MIMO XR)";
2370 case RT2573_RF_2528: return "RT2528";
2371 case RT2573_RF_5225: return "RT5225 (MIMO XR)";
2372 case RT2573_RF_5226: return "RT5226";
2373 default: return "unknown";
2378 rum_read_eeprom(struct rum_softc *sc)
2385 /* read MAC address */
2386 rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, sc->sc_ic.ic_macaddr, 6);
2388 rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2);
2390 sc->rf_rev = (val >> 11) & 0x1f;
2391 sc->hw_radio = (val >> 10) & 0x1;
2392 sc->rx_ant = (val >> 4) & 0x3;
2393 sc->tx_ant = (val >> 2) & 0x3;
2394 sc->nb_ant = val & 0x3;
2396 DPRINTF("RF revision=%d\n", sc->rf_rev);
2398 rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2);
2400 sc->ext_5ghz_lna = (val >> 6) & 0x1;
2401 sc->ext_2ghz_lna = (val >> 4) & 0x1;
2403 DPRINTF("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n",
2404 sc->ext_2ghz_lna, sc->ext_5ghz_lna);
2406 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2);
2408 if ((val & 0xff) != 0xff)
2409 sc->rssi_2ghz_corr = (int8_t)(val & 0xff); /* signed */
2411 /* Only [-10, 10] is valid */
2412 if (sc->rssi_2ghz_corr < -10 || sc->rssi_2ghz_corr > 10)
2413 sc->rssi_2ghz_corr = 0;
2415 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2);
2417 if ((val & 0xff) != 0xff)
2418 sc->rssi_5ghz_corr = (int8_t)(val & 0xff); /* signed */
2420 /* Only [-10, 10] is valid */
2421 if (sc->rssi_5ghz_corr < -10 || sc->rssi_5ghz_corr > 10)
2422 sc->rssi_5ghz_corr = 0;
2424 if (sc->ext_2ghz_lna)
2425 sc->rssi_2ghz_corr -= 14;
2426 if (sc->ext_5ghz_lna)
2427 sc->rssi_5ghz_corr -= 14;
2429 DPRINTF("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n",
2430 sc->rssi_2ghz_corr, sc->rssi_5ghz_corr);
2432 rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2);
2434 if ((val & 0xff) != 0xff)
2435 sc->rffreq = val & 0xff;
2437 DPRINTF("RF freq=%d\n", sc->rffreq);
2439 /* read Tx power for all a/b/g channels */
2440 rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14);
2441 /* XXX default Tx power for 802.11a channels */
2442 memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14);
2444 for (i = 0; i < 14; i++)
2445 DPRINTF("Channel=%d Tx power=%d\n", i + 1, sc->txpow[i]);
2448 /* read default values for BBP registers */
2449 rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
2451 for (i = 0; i < 14; i++) {
2452 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
2454 DPRINTF("BBP R%d=%02x\n", sc->bbp_prom[i].reg,
2455 sc->bbp_prom[i].val);
2461 rum_bbp_wakeup(struct rum_softc *sc)
2463 unsigned int ntries;
2465 for (ntries = 0; ntries < 100; ntries++) {
2466 if (rum_read(sc, RT2573_MAC_CSR12) & 8)
2468 rum_write(sc, RT2573_MAC_CSR12, 4); /* force wakeup */
2469 if (rum_pause(sc, hz / 100))
2472 if (ntries == 100) {
2473 device_printf(sc->sc_dev,
2474 "timeout waiting for BBP/RF to wakeup\n");
2482 rum_bbp_init(struct rum_softc *sc)
2486 /* wait for BBP to be ready */
2487 for (ntries = 0; ntries < 100; ntries++) {
2488 const uint8_t val = rum_bbp_read(sc, 0);
2489 if (val != 0 && val != 0xff)
2491 if (rum_pause(sc, hz / 100))
2494 if (ntries == 100) {
2495 device_printf(sc->sc_dev, "timeout waiting for BBP\n");
2499 /* initialize BBP registers to default values */
2500 for (i = 0; i < nitems(rum_def_bbp); i++)
2501 rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val);
2503 /* write vendor-specific BBP values (from EEPROM) */
2504 for (i = 0; i < 16; i++) {
2505 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
2507 rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
2514 rum_clr_shkey_regs(struct rum_softc *sc)
2516 rum_write(sc, RT2573_SEC_CSR0, 0);
2517 rum_write(sc, RT2573_SEC_CSR1, 0);
2518 rum_write(sc, RT2573_SEC_CSR5, 0);
2522 rum_init(struct rum_softc *sc)
2524 struct ieee80211com *ic = &sc->sc_ic;
2525 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2530 if (sc->sc_running) {
2535 /* initialize MAC registers to default values */
2536 for (i = 0; i < nitems(rum_def_mac); i++)
2537 rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val);
2539 /* reset some WME parameters to default values */
2540 sc->wme_params[0].wmep_aifsn = 2;
2541 sc->wme_params[0].wmep_logcwmin = 4;
2542 sc->wme_params[0].wmep_logcwmax = 10;
2544 /* set host ready */
2545 rum_write(sc, RT2573_MAC_CSR1, RT2573_RESET_ASIC | RT2573_RESET_BBP);
2546 rum_write(sc, RT2573_MAC_CSR1, 0);
2548 /* wait for BBP/RF to wakeup */
2549 if ((ret = rum_bbp_wakeup(sc)) != 0)
2552 if ((ret = rum_bbp_init(sc)) != 0)
2555 /* select default channel */
2556 rum_select_band(sc, ic->ic_curchan);
2557 rum_select_antenna(sc);
2558 rum_set_chan(sc, ic->ic_curchan);
2560 /* clear STA registers */
2561 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
2563 /* clear security registers (if required) */
2564 if (sc->sc_clr_shkeys == 0) {
2565 rum_clr_shkey_regs(sc);
2566 sc->sc_clr_shkeys = 1;
2569 rum_set_macaddr(sc, vap ? vap->iv_myaddr : ic->ic_macaddr);
2571 /* initialize ASIC */
2572 rum_write(sc, RT2573_MAC_CSR1, RT2573_HOST_READY);
2575 * Allocate Tx and Rx xfer queues.
2577 rum_setup_tx_list(sc);
2579 /* update Rx filter */
2580 tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff;
2582 tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR;
2583 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2584 tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR |
2586 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2587 tmp |= RT2573_DROP_TODS;
2588 if (ic->ic_promisc == 0)
2589 tmp |= RT2573_DROP_NOT_TO_ME;
2591 rum_write(sc, RT2573_TXRX_CSR0, tmp);
2594 usbd_xfer_set_stall(sc->sc_xfer[RUM_BULK_WR]);
2595 usbd_transfer_start(sc->sc_xfer[RUM_BULK_RD]);
2597 end: RUM_UNLOCK(sc);
2606 rum_stop(struct rum_softc *sc)
2610 if (!sc->sc_running) {
2618 * Drain the USB transfers, if not already drained:
2620 usbd_transfer_drain(sc->sc_xfer[RUM_BULK_WR]);
2621 usbd_transfer_drain(sc->sc_xfer[RUM_BULK_RD]);
2624 rum_unsetup_tx_list(sc);
2627 rum_setbits(sc, RT2573_TXRX_CSR0, RT2573_DISABLE_RX);
2630 rum_write(sc, RT2573_MAC_CSR1, RT2573_RESET_ASIC | RT2573_RESET_BBP);
2631 rum_write(sc, RT2573_MAC_CSR1, 0);
2636 rum_load_microcode(struct rum_softc *sc, const uint8_t *ucode, size_t size)
2638 uint16_t reg = RT2573_MCU_CODE_BASE;
2641 /* copy firmware image into NIC */
2642 for (; size >= 4; reg += 4, ucode += 4, size -= 4) {
2643 err = rum_write(sc, reg, UGETDW(ucode));
2645 /* firmware already loaded ? */
2646 device_printf(sc->sc_dev, "Firmware load "
2647 "failure! (ignored)\n");
2652 err = rum_do_mcu_request(sc, RT2573_MCU_RUN);
2653 if (err != USB_ERR_NORMAL_COMPLETION) {
2654 device_printf(sc->sc_dev, "could not run firmware: %s\n",
2658 /* give the chip some time to boot */
2659 rum_pause(sc, hz / 8);
2663 rum_set_sleep_time(struct rum_softc *sc, uint16_t bintval)
2665 struct ieee80211com *ic = &sc->sc_ic;
2669 RUM_LOCK_ASSERT(sc);
2671 exp = ic->ic_lintval / bintval;
2672 delay = ic->ic_lintval % bintval;
2674 if (exp > RT2573_TBCN_EXP_MAX)
2675 exp = RT2573_TBCN_EXP_MAX;
2676 if (delay > RT2573_TBCN_DELAY_MAX)
2677 delay = RT2573_TBCN_DELAY_MAX;
2679 uerror = rum_modbits(sc, RT2573_MAC_CSR11,
2680 RT2573_TBCN_EXP(exp) |
2681 RT2573_TBCN_DELAY(delay),
2682 RT2573_TBCN_EXP(RT2573_TBCN_EXP_MAX) |
2683 RT2573_TBCN_DELAY(RT2573_TBCN_DELAY_MAX));
2685 if (uerror != USB_ERR_NORMAL_COMPLETION)
2688 sc->sc_sleep_time = IEEE80211_TU_TO_TICKS(exp * bintval + delay);
2694 rum_reset(struct ieee80211vap *vap, u_long cmd)
2696 struct ieee80211com *ic = vap->iv_ic;
2697 struct ieee80211_node *ni;
2698 struct rum_softc *sc = ic->ic_softc;
2702 case IEEE80211_IOC_POWERSAVE:
2705 case IEEE80211_IOC_POWERSAVESLEEP:
2706 ni = ieee80211_ref_node(vap->iv_bss);
2709 error = rum_set_sleep_time(sc, ni->ni_intval);
2710 if (vap->iv_state == IEEE80211_S_SLEEP) {
2711 /* Use new values for wakeup timer. */
2712 rum_clrbits(sc, RT2573_MAC_CSR11, RT2573_AUTO_WAKEUP);
2713 rum_setbits(sc, RT2573_MAC_CSR11, RT2573_AUTO_WAKEUP);
2715 /* XXX send reassoc */
2718 ieee80211_free_node(ni);
2729 rum_set_beacon(struct rum_softc *sc, struct ieee80211vap *vap)
2731 struct ieee80211com *ic = vap->iv_ic;
2732 struct rum_vap *rvp = RUM_VAP(vap);
2733 struct mbuf *m = rvp->bcn_mbuf;
2734 const struct ieee80211_txparam *tp;
2735 struct rum_tx_desc desc;
2737 RUM_LOCK_ASSERT(sc);
2741 if (ic->ic_bsschan == IEEE80211_CHAN_ANYC)
2744 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)];
2745 rum_setup_tx_desc(sc, &desc, NULL, RT2573_TX_TIMESTAMP,
2746 RT2573_TX_HWSEQ, 0, 0, m->m_pkthdr.len, tp->mgmtrate);
2748 /* copy the Tx descriptor into NIC memory */
2749 if (rum_write_multi(sc, RT2573_HW_BCN_BASE(0), (uint8_t *)&desc,
2750 RT2573_TX_DESC_SIZE) != 0)
2753 /* copy beacon header and payload into NIC memory */
2754 if (rum_write_multi(sc, RT2573_HW_BCN_BASE(0) + RT2573_TX_DESC_SIZE,
2755 mtod(m, uint8_t *), m->m_pkthdr.len) != 0)
2762 rum_alloc_beacon(struct rum_softc *sc, struct ieee80211vap *vap)
2764 struct rum_vap *rvp = RUM_VAP(vap);
2765 struct ieee80211_node *ni = vap->iv_bss;
2768 if (ni->ni_chan == IEEE80211_CHAN_ANYC)
2771 m = ieee80211_beacon_alloc(ni);
2775 if (rvp->bcn_mbuf != NULL)
2776 m_freem(rvp->bcn_mbuf);
2780 return (rum_set_beacon(sc, vap));
2784 rum_update_beacon_cb(struct rum_softc *sc, union sec_param *data,
2787 struct ieee80211vap *vap = data->vap;
2789 rum_set_beacon(sc, vap);
2793 rum_update_beacon(struct ieee80211vap *vap, int item)
2795 struct ieee80211com *ic = vap->iv_ic;
2796 struct rum_softc *sc = ic->ic_softc;
2797 struct rum_vap *rvp = RUM_VAP(vap);
2798 struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
2799 struct ieee80211_node *ni = vap->iv_bss;
2800 struct mbuf *m = rvp->bcn_mbuf;
2805 m = ieee80211_beacon_alloc(ni);
2807 device_printf(sc->sc_dev,
2808 "%s: could not allocate beacon frame\n", __func__);
2816 case IEEE80211_BEACON_ERP:
2817 rum_update_slot(ic);
2819 case IEEE80211_BEACON_TIM:
2827 setbit(bo->bo_flags, item);
2828 ieee80211_beacon_update(ni, m, mcast);
2830 rum_cmd_sleepable(sc, &vap, sizeof(vap), 0, rum_update_beacon_cb);
2834 rum_common_key_set(struct rum_softc *sc, struct ieee80211_key *k,
2838 if (rum_write_multi(sc, base, k->wk_key, k->wk_keylen))
2841 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP) {
2842 if (rum_write_multi(sc, base + IEEE80211_KEYBUF_SIZE,
2845 if (rum_write_multi(sc, base + IEEE80211_KEYBUF_SIZE + 8,
2854 rum_group_key_set_cb(struct rum_softc *sc, union sec_param *data,
2857 struct ieee80211_key *k = &data->key;
2860 if (sc->sc_clr_shkeys == 0) {
2861 rum_clr_shkey_regs(sc);
2862 sc->sc_clr_shkeys = 1;
2865 mode = rum_crypto_mode(sc, k->wk_cipher->ic_cipher, k->wk_keylen);
2869 DPRINTFN(1, "setting group key %d for vap %d, mode %d "
2870 "(tx %s, rx %s)\n", k->wk_keyix, rvp_id, mode,
2871 (k->wk_flags & IEEE80211_KEY_XMIT) ? "on" : "off",
2872 (k->wk_flags & IEEE80211_KEY_RECV) ? "on" : "off");
2874 /* Install the key. */
2875 if (rum_common_key_set(sc, k, RT2573_SKEY(rvp_id, k->wk_keyix)) != 0)
2878 /* Set cipher mode. */
2879 if (rum_modbits(sc, rvp_id < 2 ? RT2573_SEC_CSR1 : RT2573_SEC_CSR5,
2880 mode << (rvp_id % 2 + k->wk_keyix) * RT2573_SKEY_MAX,
2881 RT2573_MODE_MASK << (rvp_id % 2 + k->wk_keyix) * RT2573_SKEY_MAX)
2885 /* Mark this key as valid. */
2886 if (rum_setbits(sc, RT2573_SEC_CSR0,
2887 1 << (rvp_id * RT2573_SKEY_MAX + k->wk_keyix)) != 0)
2893 device_printf(sc->sc_dev, "%s: cannot set group key %d for vap %d\n",
2894 __func__, k->wk_keyix, rvp_id);
2898 rum_group_key_del_cb(struct rum_softc *sc, union sec_param *data,
2901 struct ieee80211_key *k = &data->key;
2903 DPRINTF("%s: removing group key %d for vap %d\n", __func__,
2904 k->wk_keyix, rvp_id);
2906 rvp_id < 2 ? RT2573_SEC_CSR1 : RT2573_SEC_CSR5,
2907 RT2573_MODE_MASK << (rvp_id % 2 + k->wk_keyix) * RT2573_SKEY_MAX);
2908 rum_clrbits(sc, RT2573_SEC_CSR0,
2909 rvp_id * RT2573_SKEY_MAX + k->wk_keyix);
2913 rum_pair_key_set_cb(struct rum_softc *sc, union sec_param *data,
2916 struct ieee80211_key *k = &data->key;
2917 uint8_t buf[IEEE80211_ADDR_LEN + 1];
2920 mode = rum_crypto_mode(sc, k->wk_cipher->ic_cipher, k->wk_keylen);
2924 DPRINTFN(1, "setting pairwise key %d for vap %d, mode %d "
2925 "(tx %s, rx %s)\n", k->wk_keyix, rvp_id, mode,
2926 (k->wk_flags & IEEE80211_KEY_XMIT) ? "on" : "off",
2927 (k->wk_flags & IEEE80211_KEY_RECV) ? "on" : "off");
2929 /* Install the key. */
2930 if (rum_common_key_set(sc, k, RT2573_PKEY(k->wk_keyix)) != 0)
2933 IEEE80211_ADDR_COPY(buf, k->wk_macaddr);
2934 buf[IEEE80211_ADDR_LEN] = mode;
2936 /* Set transmitter address and cipher mode. */
2937 if (rum_write_multi(sc, RT2573_ADDR_ENTRY(k->wk_keyix),
2938 buf, sizeof buf) != 0)
2941 /* Enable key table lookup for this vap. */
2942 if (sc->vap_key_count[rvp_id]++ == 0)
2943 if (rum_setbits(sc, RT2573_SEC_CSR4, 1 << rvp_id) != 0)
2946 /* Mark this key as valid. */
2948 k->wk_keyix < 32 ? RT2573_SEC_CSR2 : RT2573_SEC_CSR3,
2949 1 << (k->wk_keyix % 32)) != 0)
2955 device_printf(sc->sc_dev,
2956 "%s: cannot set pairwise key %d, vap %d\n", __func__, k->wk_keyix,
2961 rum_pair_key_del_cb(struct rum_softc *sc, union sec_param *data,
2964 struct ieee80211_key *k = &data->key;
2966 DPRINTF("%s: removing key %d\n", __func__, k->wk_keyix);
2967 rum_clrbits(sc, (k->wk_keyix < 32) ? RT2573_SEC_CSR2 : RT2573_SEC_CSR3,
2968 1 << (k->wk_keyix % 32));
2969 sc->keys_bmap &= ~(1ULL << k->wk_keyix);
2970 if (--sc->vap_key_count[rvp_id] == 0)
2971 rum_clrbits(sc, RT2573_SEC_CSR4, 1 << rvp_id);
2975 rum_key_alloc(struct ieee80211vap *vap, struct ieee80211_key *k,
2976 ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
2978 struct rum_softc *sc = vap->iv_ic->ic_softc;
2981 if (!(&vap->iv_nw_keys[0] <= k &&
2982 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID])) {
2983 if (!(k->wk_flags & IEEE80211_KEY_SWCRYPT)) {
2985 for (i = 0; i < RT2573_ADDR_MAX; i++) {
2986 if ((sc->keys_bmap & (1ULL << i)) == 0) {
2987 sc->keys_bmap |= (1ULL << i);
2993 if (i == RT2573_ADDR_MAX) {
2994 device_printf(sc->sc_dev,
2995 "%s: no free space in the key table\n",
3002 *keyix = k - vap->iv_nw_keys;
3009 rum_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
3011 struct rum_softc *sc = vap->iv_ic->ic_softc;
3014 if (k->wk_flags & IEEE80211_KEY_SWCRYPT) {
3019 group = k >= &vap->iv_nw_keys[0] && k < &vap->iv_nw_keys[IEEE80211_WEP_NKID];
3021 return !rum_cmd_sleepable(sc, k, sizeof(*k), 0,
3022 group ? rum_group_key_set_cb : rum_pair_key_set_cb);
3026 rum_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
3028 struct rum_softc *sc = vap->iv_ic->ic_softc;
3031 if (k->wk_flags & IEEE80211_KEY_SWCRYPT) {
3036 group = k >= &vap->iv_nw_keys[0] && k < &vap->iv_nw_keys[IEEE80211_WEP_NKID];
3038 return !rum_cmd_sleepable(sc, k, sizeof(*k), 0,
3039 group ? rum_group_key_del_cb : rum_pair_key_del_cb);
3043 rum_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
3044 const struct ieee80211_bpf_params *params)
3046 struct rum_softc *sc = ni->ni_ic->ic_softc;
3050 /* prevent management frames from being sent if we're not ready */
3051 if (!sc->sc_running) {
3055 if (sc->tx_nfree < RUM_TX_MINFREE) {
3060 if (params == NULL) {
3062 * Legacy path; interpret frame contents to decide
3063 * precisely how to send the frame.
3065 if ((ret = rum_tx_mgt(sc, m, ni)) != 0)
3069 * Caller supplied explicit parameters to use in
3070 * sending the frame.
3072 if ((ret = rum_tx_raw(sc, m, ni, params)) != 0)
3085 rum_ratectl_start(struct rum_softc *sc, struct ieee80211_node *ni)
3087 struct ieee80211vap *vap = ni->ni_vap;
3088 struct rum_vap *rvp = RUM_VAP(vap);
3090 /* clear statistic registers (STA_CSR0 to STA_CSR5) */
3091 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
3093 usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp);
3097 rum_ratectl_timeout(void *arg)
3099 struct rum_vap *rvp = arg;
3100 struct ieee80211vap *vap = &rvp->vap;
3101 struct ieee80211com *ic = vap->iv_ic;
3103 ieee80211_runtask(ic, &rvp->ratectl_task);
3107 rum_ratectl_task(void *arg, int pending)
3109 struct rum_vap *rvp = arg;
3110 struct ieee80211vap *vap = &rvp->vap;
3111 struct rum_softc *sc = vap->iv_ic->ic_softc;
3112 struct ieee80211_node *ni;
3114 int sum, success, retrycnt;
3117 /* read and clear statistic registers (STA_CSR0 to STA_CSR5) */
3118 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof(sc->sta));
3120 ok[0] = (le32toh(sc->sta[4]) & 0xffff); /* TX ok w/o retry */
3121 ok[1] = (le32toh(sc->sta[4]) >> 16); /* TX ok w/ one retry */
3122 ok[2] = (le32toh(sc->sta[5]) & 0xffff); /* TX ok w/ multiple retries */
3123 fail = (le32toh(sc->sta[5]) >> 16); /* TX retry-fail count */
3125 success = ok[0] + ok[1] + ok[2];
3126 sum = success + fail;
3128 retrycnt = ok[1] + ok[2] * 2 + fail * (rvp->maxretry + 1);
3131 ni = ieee80211_ref_node(vap->iv_bss);
3132 ieee80211_ratectl_tx_update(vap, ni, &sum, &ok, &retrycnt);
3133 (void) ieee80211_ratectl_rate(ni, NULL, 0);
3134 ieee80211_free_node(ni);
3137 /* count TX retry-fail as Tx errors */
3138 if_inc_counter(vap->iv_ifp, IFCOUNTER_OERRORS, fail);
3140 usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp);
3145 rum_scan_start(struct ieee80211com *ic)
3147 struct rum_softc *sc = ic->ic_softc;
3150 rum_abort_tsf_sync(sc);
3151 rum_set_bssid(sc, ieee80211broadcastaddr);
3157 rum_scan_end(struct ieee80211com *ic)
3159 struct rum_softc *sc = ic->ic_softc;
3161 if (ic->ic_flags_ext & IEEE80211_FEXT_BGSCAN) {
3163 if (ic->ic_opmode != IEEE80211_M_AHDEMO)
3164 rum_enable_tsf_sync(sc);
3167 rum_set_bssid(sc, sc->sc_bssid);
3173 rum_set_channel(struct ieee80211com *ic)
3175 struct rum_softc *sc = ic->ic_softc;
3178 rum_set_chan(sc, ic->ic_curchan);
3183 rum_getradiocaps(struct ieee80211com *ic,
3184 int maxchans, int *nchans, struct ieee80211_channel chans[])
3186 struct rum_softc *sc = ic->ic_softc;
3187 uint8_t bands[IEEE80211_MODE_BYTES];
3189 memset(bands, 0, sizeof(bands));
3190 setbit(bands, IEEE80211_MODE_11B);
3191 setbit(bands, IEEE80211_MODE_11G);
3192 ieee80211_add_channel_list_2ghz(chans, maxchans, nchans,
3193 rum_chan_2ghz, nitems(rum_chan_2ghz), bands, 0);
3195 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_5226) {
3196 setbit(bands, IEEE80211_MODE_11A);
3197 ieee80211_add_channel_list_5ghz(chans, maxchans, nchans,
3198 rum_chan_5ghz, nitems(rum_chan_5ghz), bands, 0);
3203 rum_get_rssi(struct rum_softc *sc, uint8_t raw)
3205 struct ieee80211com *ic = &sc->sc_ic;
3208 lna = (raw >> 5) & 0x3;
3215 * NB: Since RSSI is relative to noise floor, -1 is
3216 * adequate for caller to know error happened.
3221 rssi = (2 * agc) - RT2573_NOISE_FLOOR;
3223 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) {
3224 rssi += sc->rssi_2ghz_corr;
3233 rssi += sc->rssi_5ghz_corr;
3235 if (!sc->ext_5ghz_lna && lna != 1)
3249 rum_pause(struct rum_softc *sc, int timeout)
3252 usb_pause_mtx(&sc->sc_lock, timeout);
3256 static device_method_t rum_methods[] = {
3257 /* Device interface */
3258 DEVMETHOD(device_probe, rum_match),
3259 DEVMETHOD(device_attach, rum_attach),
3260 DEVMETHOD(device_detach, rum_detach),
3264 static driver_t rum_driver = {
3266 .methods = rum_methods,
3267 .size = sizeof(struct rum_softc),
3270 static devclass_t rum_devclass;
3272 DRIVER_MODULE(rum, uhub, rum_driver, rum_devclass, NULL, NULL);
3273 MODULE_DEPEND(rum, wlan, 1, 1, 1);
3274 MODULE_DEPEND(rum, usb, 1, 1, 1);
3275 MODULE_VERSION(rum, 1);
3276 #if 0 /* Not implemented by DragonFly */
3277 USB_PNP_HOST_INFO(rum_devs);