1
2 /*-
3 * Copyright (c) 2005-2007 Damien Bergamini <damien.bergamini@free.fr>
4 * Copyright (c) 2006 Niall O'Higgins <niallo@openbsd.org>
5 * Copyright (c) 2007-2008 Hans Petter Selasky <hselasky@FreeBSD.org>
6 * Copyright (c) 2015 Andriy Voskoboinyk <avos@FreeBSD.org>
7 *
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 */
20
21 /*-
22 * Ralink Technology RT2501USB/RT2601USB chipset driver
23 * http://www.ralinktech.com.tw/
24 */
25
26 #include "opt_wlan.h"
27
28 #include <sys/param.h>
29 #include <sys/sockio.h>
30 #include <sys/sysctl.h>
31 #include <sys/lock.h>
32 #include <sys/mutex.h>
33 #include <sys/mbuf.h>
34 #include <sys/kernel.h>
35 #include <sys/socket.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <sys/bus.h>
40 #include <sys/endian.h>
41 #include <sys/kdb.h>
42
43 #include <net/bpf.h>
44 #include <net/if.h>
45 #include <net/if_var.h>
46 #include <net/if_arp.h>
47 #include <net/ethernet.h>
48 #include <net/if_dl.h>
49 #include <net/if_media.h>
50 #include <net/if_types.h>
51
52 #ifdef INET
53 #include <netinet/in.h>
54 #include <netinet/in_systm.h>
55 #include <netinet/in_var.h>
56 #include <netinet/if_ether.h>
57 #include <netinet/ip.h>
58 #endif
59
60 #include <net80211/ieee80211_var.h>
61 #include <net80211/ieee80211_regdomain.h>
62 #include <net80211/ieee80211_radiotap.h>
63 #include <net80211/ieee80211_ratectl.h>
64
65 #include <dev/usb/usb.h>
66 #include <dev/usb/usbdi.h>
67 #include "usbdevs.h"
68
69 #define USB_DEBUG_VAR rum_debug
70 #include <dev/usb/usb_debug.h>
71
72 #include <dev/usb/wlan/if_rumreg.h>
73 #include <dev/usb/wlan/if_rumvar.h>
74 #include <dev/usb/wlan/if_rumfw.h>
75
76 #ifdef USB_DEBUG
77 static int rum_debug = 0;
78
79 static SYSCTL_NODE(_hw_usb, OID_AUTO, rum, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
80 "USB rum");
81 SYSCTL_INT(_hw_usb_rum, OID_AUTO, debug, CTLFLAG_RWTUN, &rum_debug, 0,
82 "Debug level");
83 #endif
84
85 static const STRUCT_USB_HOST_ID rum_devs[] = {
86 #define RUM_DEV(v,p) { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
87 RUM_DEV(ABOCOM, HWU54DM),
88 RUM_DEV(ABOCOM, RT2573_2),
89 RUM_DEV(ABOCOM, RT2573_3),
90 RUM_DEV(ABOCOM, RT2573_4),
91 RUM_DEV(ABOCOM, WUG2700),
92 RUM_DEV(AMIT, CGWLUSB2GO),
93 RUM_DEV(ASUS, RT2573_1),
94 RUM_DEV(ASUS, RT2573_2),
95 RUM_DEV(BELKIN, F5D7050A),
96 RUM_DEV(BELKIN, F5D9050V3),
97 RUM_DEV(CISCOLINKSYS, WUSB54GC),
98 RUM_DEV(CISCOLINKSYS, WUSB54GR),
99 RUM_DEV(CONCEPTRONIC2, C54RU2),
100 RUM_DEV(COREGA, CGWLUSB2GL),
101 RUM_DEV(COREGA, CGWLUSB2GPX),
102 RUM_DEV(DICKSMITH, CWD854F),
103 RUM_DEV(DICKSMITH, RT2573),
104 RUM_DEV(EDIMAX, EW7318USG),
105 RUM_DEV(DLINK2, DWLG122C1),
106 RUM_DEV(DLINK2, WUA1340),
107 RUM_DEV(DLINK2, DWA111),
108 RUM_DEV(DLINK2, DWA110),
109 RUM_DEV(GIGABYTE, GNWB01GS),
110 RUM_DEV(GIGABYTE, GNWI05GS),
111 RUM_DEV(GIGASET, RT2573),
112 RUM_DEV(GOODWAY, RT2573),
113 RUM_DEV(GUILLEMOT, HWGUSB254LB),
114 RUM_DEV(GUILLEMOT, HWGUSB254V2AP),
115 RUM_DEV(HUAWEI3COM, WUB320G),
116 RUM_DEV(MELCO, G54HP),
117 RUM_DEV(MELCO, SG54HP),
118 RUM_DEV(MELCO, SG54HG),
119 RUM_DEV(MELCO, WLIUCG),
120 RUM_DEV(MELCO, WLRUCG),
121 RUM_DEV(MELCO, WLRUCGAOSS),
122 RUM_DEV(MSI, RT2573_1),
123 RUM_DEV(MSI, RT2573_2),
124 RUM_DEV(MSI, RT2573_3),
125 RUM_DEV(MSI, RT2573_4),
126 RUM_DEV(NOVATECH, RT2573),
127 RUM_DEV(PLANEX2, GWUS54HP),
128 RUM_DEV(PLANEX2, GWUS54MINI2),
129 RUM_DEV(PLANEX2, GWUSMM),
130 RUM_DEV(QCOM, RT2573),
131 RUM_DEV(QCOM, RT2573_2),
132 RUM_DEV(QCOM, RT2573_3),
133 RUM_DEV(RALINK, RT2573),
134 RUM_DEV(RALINK, RT2573_2),
135 RUM_DEV(RALINK, RT2671),
136 RUM_DEV(SITECOMEU, WL113R2),
137 RUM_DEV(SITECOMEU, WL172),
138 RUM_DEV(SPARKLAN, RT2573),
139 RUM_DEV(SURECOM, RT2573),
140 #undef RUM_DEV
141 };
142
143 static device_probe_t rum_match;
144 static device_attach_t rum_attach;
145 static device_detach_t rum_detach;
146
147 static usb_callback_t rum_bulk_read_callback;
148 static usb_callback_t rum_bulk_write_callback;
149
150 static usb_error_t rum_do_request(struct rum_softc *sc,
151 struct usb_device_request *req, void *data);
152 static usb_error_t rum_do_mcu_request(struct rum_softc *sc, int);
153 static struct ieee80211vap *rum_vap_create(struct ieee80211com *,
154 const char [IFNAMSIZ], int, enum ieee80211_opmode,
155 int, const uint8_t [IEEE80211_ADDR_LEN],
156 const uint8_t [IEEE80211_ADDR_LEN]);
157 static void rum_vap_delete(struct ieee80211vap *);
158 static void rum_cmdq_cb(void *, int);
159 static int rum_cmd_sleepable(struct rum_softc *, const void *,
160 size_t, uint8_t, CMD_FUNC_PROTO);
161 static void rum_tx_free(struct rum_tx_data *, int);
162 static void rum_setup_tx_list(struct rum_softc *);
163 static void rum_reset_tx_list(struct rum_softc *,
164 struct ieee80211vap *);
165 static void rum_unsetup_tx_list(struct rum_softc *);
166 static void rum_beacon_miss(struct ieee80211vap *);
167 static void rum_sta_recv_mgmt(struct ieee80211_node *,
168 struct mbuf *, int,
169 const struct ieee80211_rx_stats *, int, int);
170 static int rum_set_power_state(struct rum_softc *, int);
171 static int rum_newstate(struct ieee80211vap *,
172 enum ieee80211_state, int);
173 static uint8_t rum_crypto_mode(struct rum_softc *, u_int, int);
174 static void rum_setup_tx_desc(struct rum_softc *,
175 struct rum_tx_desc *, struct ieee80211_key *,
176 uint32_t, uint8_t, uint8_t, int, int, int);
177 static uint32_t rum_tx_crypto_flags(struct rum_softc *,
178 struct ieee80211_node *,
179 const struct ieee80211_key *);
180 static int rum_tx_mgt(struct rum_softc *, struct mbuf *,
181 struct ieee80211_node *);
182 static int rum_tx_raw(struct rum_softc *, struct mbuf *,
183 struct ieee80211_node *,
184 const struct ieee80211_bpf_params *);
185 static int rum_tx_data(struct rum_softc *, struct mbuf *,
186 struct ieee80211_node *);
187 static int rum_transmit(struct ieee80211com *, struct mbuf *);
188 static void rum_start(struct rum_softc *);
189 static void rum_parent(struct ieee80211com *);
190 static void rum_eeprom_read(struct rum_softc *, uint16_t, void *,
191 int);
192 static uint32_t rum_read(struct rum_softc *, uint16_t);
193 static void rum_read_multi(struct rum_softc *, uint16_t, void *,
194 int);
195 static usb_error_t rum_write(struct rum_softc *, uint16_t, uint32_t);
196 static usb_error_t rum_write_multi(struct rum_softc *, uint16_t, void *,
197 size_t);
198 static usb_error_t rum_setbits(struct rum_softc *, uint16_t, uint32_t);
199 static usb_error_t rum_clrbits(struct rum_softc *, uint16_t, uint32_t);
200 static usb_error_t rum_modbits(struct rum_softc *, uint16_t, uint32_t,
201 uint32_t);
202 static int rum_bbp_busy(struct rum_softc *);
203 static void rum_bbp_write(struct rum_softc *, uint8_t, uint8_t);
204 static uint8_t rum_bbp_read(struct rum_softc *, uint8_t);
205 static void rum_rf_write(struct rum_softc *, uint8_t, uint32_t);
206 static void rum_select_antenna(struct rum_softc *);
207 static void rum_enable_mrr(struct rum_softc *);
208 static void rum_set_txpreamble(struct rum_softc *);
209 static void rum_set_basicrates(struct rum_softc *);
210 static void rum_select_band(struct rum_softc *,
211 struct ieee80211_channel *);
212 static void rum_set_chan(struct rum_softc *,
213 struct ieee80211_channel *);
214 static void rum_set_maxretry(struct rum_softc *,
215 struct ieee80211vap *);
216 static int rum_enable_tsf_sync(struct rum_softc *);
217 static void rum_enable_tsf(struct rum_softc *);
218 static void rum_abort_tsf_sync(struct rum_softc *);
219 static void rum_get_tsf(struct rum_softc *, uint64_t *);
220 static void rum_update_slot_cb(struct rum_softc *,
221 union sec_param *, uint8_t);
222 static void rum_update_slot(struct ieee80211com *);
223 static int rum_wme_update(struct ieee80211com *);
224 static void rum_set_bssid(struct rum_softc *, const uint8_t *);
225 static void rum_set_macaddr(struct rum_softc *, const uint8_t *);
226 static void rum_update_mcast(struct ieee80211com *);
227 static void rum_update_promisc(struct ieee80211com *);
228 static void rum_setpromisc(struct rum_softc *);
229 static const char *rum_get_rf(int);
230 static void rum_read_eeprom(struct rum_softc *);
231 static int rum_bbp_wakeup(struct rum_softc *);
232 static int rum_bbp_init(struct rum_softc *);
233 static void rum_clr_shkey_regs(struct rum_softc *);
234 static int rum_init(struct rum_softc *);
235 static void rum_stop(struct rum_softc *);
236 static void rum_load_microcode(struct rum_softc *, const uint8_t *,
237 size_t);
238 static int rum_set_sleep_time(struct rum_softc *, uint16_t);
239 static int rum_reset(struct ieee80211vap *, u_long);
240 static int rum_set_beacon(struct rum_softc *,
241 struct ieee80211vap *);
242 static int rum_alloc_beacon(struct rum_softc *,
243 struct ieee80211vap *);
244 static void rum_update_beacon_cb(struct rum_softc *,
245 union sec_param *, uint8_t);
246 static void rum_update_beacon(struct ieee80211vap *, int);
247 static int rum_common_key_set(struct rum_softc *,
248 struct ieee80211_key *, uint16_t);
249 static void rum_group_key_set_cb(struct rum_softc *,
250 union sec_param *, uint8_t);
251 static void rum_group_key_del_cb(struct rum_softc *,
252 union sec_param *, uint8_t);
253 static void rum_pair_key_set_cb(struct rum_softc *,
254 union sec_param *, uint8_t);
255 static void rum_pair_key_del_cb(struct rum_softc *,
256 union sec_param *, uint8_t);
257 static int rum_key_alloc(struct ieee80211vap *,
258 struct ieee80211_key *, ieee80211_keyix *,
259 ieee80211_keyix *);
260 static int rum_key_set(struct ieee80211vap *,
261 const struct ieee80211_key *);
262 static int rum_key_delete(struct ieee80211vap *,
263 const struct ieee80211_key *);
264 static int rum_raw_xmit(struct ieee80211_node *, struct mbuf *,
265 const struct ieee80211_bpf_params *);
266 static void rum_scan_start(struct ieee80211com *);
267 static void rum_scan_end(struct ieee80211com *);
268 static void rum_set_channel(struct ieee80211com *);
269 static void rum_getradiocaps(struct ieee80211com *, int, int *,
270 struct ieee80211_channel[]);
271 static int rum_get_rssi(struct rum_softc *, uint8_t);
272 static void rum_ratectl_start(struct rum_softc *,
273 struct ieee80211_node *);
274 static void rum_ratectl_timeout(void *);
275 static void rum_ratectl_task(void *, int);
276 static int rum_pause(struct rum_softc *, int);
277
278 static const struct {
279 uint32_t reg;
280 uint32_t val;
281 } rum_def_mac[] = {
282 { RT2573_TXRX_CSR0, 0x025fb032 },
283 { RT2573_TXRX_CSR1, 0x9eaa9eaf },
284 { RT2573_TXRX_CSR2, 0x8a8b8c8d },
285 { RT2573_TXRX_CSR3, 0x00858687 },
286 { RT2573_TXRX_CSR7, 0x2e31353b },
287 { RT2573_TXRX_CSR8, 0x2a2a2a2c },
288 { RT2573_TXRX_CSR15, 0x0000000f },
289 { RT2573_MAC_CSR6, 0x00000fff },
290 { RT2573_MAC_CSR8, 0x016c030a },
291 { RT2573_MAC_CSR10, 0x00000718 },
292 { RT2573_MAC_CSR12, 0x00000004 },
293 { RT2573_MAC_CSR13, 0x00007f00 },
294 { RT2573_SEC_CSR2, 0x00000000 },
295 { RT2573_SEC_CSR3, 0x00000000 },
296 { RT2573_SEC_CSR4, 0x00000000 },
297 { RT2573_PHY_CSR1, 0x000023b0 },
298 { RT2573_PHY_CSR5, 0x00040a06 },
299 { RT2573_PHY_CSR6, 0x00080606 },
300 { RT2573_PHY_CSR7, 0x00000408 },
301 { RT2573_AIFSN_CSR, 0x00002273 },
302 { RT2573_CWMIN_CSR, 0x00002344 },
303 { RT2573_CWMAX_CSR, 0x000034aa }
304 };
305
306 static const struct {
307 uint8_t reg;
308 uint8_t val;
309 } rum_def_bbp[] = {
310 { 3, 0x80 },
311 { 15, 0x30 },
312 { 17, 0x20 },
313 { 21, 0xc8 },
314 { 22, 0x38 },
315 { 23, 0x06 },
316 { 24, 0xfe },
317 { 25, 0x0a },
318 { 26, 0x0d },
319 { 32, 0x0b },
320 { 34, 0x12 },
321 { 37, 0x07 },
322 { 39, 0xf8 },
323 { 41, 0x60 },
324 { 53, 0x10 },
325 { 54, 0x18 },
326 { 60, 0x10 },
327 { 61, 0x04 },
328 { 62, 0x04 },
329 { 75, 0xfe },
330 { 86, 0xfe },
331 { 88, 0xfe },
332 { 90, 0x0f },
333 { 99, 0x00 },
334 { 102, 0x16 },
335 { 107, 0x04 }
336 };
337
338 static const uint8_t rum_chan_5ghz[] =
339 { 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64,
340 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140,
341 149, 153, 157, 161, 165 };
342
343 static const struct rfprog {
344 uint8_t chan;
345 uint32_t r1, r2, r3, r4;
346 } rum_rf5226[] = {
347 { 1, 0x00b03, 0x001e1, 0x1a014, 0x30282 },
348 { 2, 0x00b03, 0x001e1, 0x1a014, 0x30287 },
349 { 3, 0x00b03, 0x001e2, 0x1a014, 0x30282 },
350 { 4, 0x00b03, 0x001e2, 0x1a014, 0x30287 },
351 { 5, 0x00b03, 0x001e3, 0x1a014, 0x30282 },
352 { 6, 0x00b03, 0x001e3, 0x1a014, 0x30287 },
353 { 7, 0x00b03, 0x001e4, 0x1a014, 0x30282 },
354 { 8, 0x00b03, 0x001e4, 0x1a014, 0x30287 },
355 { 9, 0x00b03, 0x001e5, 0x1a014, 0x30282 },
356 { 10, 0x00b03, 0x001e5, 0x1a014, 0x30287 },
357 { 11, 0x00b03, 0x001e6, 0x1a014, 0x30282 },
358 { 12, 0x00b03, 0x001e6, 0x1a014, 0x30287 },
359 { 13, 0x00b03, 0x001e7, 0x1a014, 0x30282 },
360 { 14, 0x00b03, 0x001e8, 0x1a014, 0x30284 },
361
362 { 34, 0x00b03, 0x20266, 0x36014, 0x30282 },
363 { 38, 0x00b03, 0x20267, 0x36014, 0x30284 },
364 { 42, 0x00b03, 0x20268, 0x36014, 0x30286 },
365 { 46, 0x00b03, 0x20269, 0x36014, 0x30288 },
366
367 { 36, 0x00b03, 0x00266, 0x26014, 0x30288 },
368 { 40, 0x00b03, 0x00268, 0x26014, 0x30280 },
369 { 44, 0x00b03, 0x00269, 0x26014, 0x30282 },
370 { 48, 0x00b03, 0x0026a, 0x26014, 0x30284 },
371 { 52, 0x00b03, 0x0026b, 0x26014, 0x30286 },
372 { 56, 0x00b03, 0x0026c, 0x26014, 0x30288 },
373 { 60, 0x00b03, 0x0026e, 0x26014, 0x30280 },
374 { 64, 0x00b03, 0x0026f, 0x26014, 0x30282 },
375
376 { 100, 0x00b03, 0x0028a, 0x2e014, 0x30280 },
377 { 104, 0x00b03, 0x0028b, 0x2e014, 0x30282 },
378 { 108, 0x00b03, 0x0028c, 0x2e014, 0x30284 },
379 { 112, 0x00b03, 0x0028d, 0x2e014, 0x30286 },
380 { 116, 0x00b03, 0x0028e, 0x2e014, 0x30288 },
381 { 120, 0x00b03, 0x002a0, 0x2e014, 0x30280 },
382 { 124, 0x00b03, 0x002a1, 0x2e014, 0x30282 },
383 { 128, 0x00b03, 0x002a2, 0x2e014, 0x30284 },
384 { 132, 0x00b03, 0x002a3, 0x2e014, 0x30286 },
385 { 136, 0x00b03, 0x002a4, 0x2e014, 0x30288 },
386 { 140, 0x00b03, 0x002a6, 0x2e014, 0x30280 },
387
388 { 149, 0x00b03, 0x002a8, 0x2e014, 0x30287 },
389 { 153, 0x00b03, 0x002a9, 0x2e014, 0x30289 },
390 { 157, 0x00b03, 0x002ab, 0x2e014, 0x30281 },
391 { 161, 0x00b03, 0x002ac, 0x2e014, 0x30283 },
392 { 165, 0x00b03, 0x002ad, 0x2e014, 0x30285 }
393 }, rum_rf5225[] = {
394 { 1, 0x00b33, 0x011e1, 0x1a014, 0x30282 },
395 { 2, 0x00b33, 0x011e1, 0x1a014, 0x30287 },
396 { 3, 0x00b33, 0x011e2, 0x1a014, 0x30282 },
397 { 4, 0x00b33, 0x011e2, 0x1a014, 0x30287 },
398 { 5, 0x00b33, 0x011e3, 0x1a014, 0x30282 },
399 { 6, 0x00b33, 0x011e3, 0x1a014, 0x30287 },
400 { 7, 0x00b33, 0x011e4, 0x1a014, 0x30282 },
401 { 8, 0x00b33, 0x011e4, 0x1a014, 0x30287 },
402 { 9, 0x00b33, 0x011e5, 0x1a014, 0x30282 },
403 { 10, 0x00b33, 0x011e5, 0x1a014, 0x30287 },
404 { 11, 0x00b33, 0x011e6, 0x1a014, 0x30282 },
405 { 12, 0x00b33, 0x011e6, 0x1a014, 0x30287 },
406 { 13, 0x00b33, 0x011e7, 0x1a014, 0x30282 },
407 { 14, 0x00b33, 0x011e8, 0x1a014, 0x30284 },
408
409 { 34, 0x00b33, 0x01266, 0x26014, 0x30282 },
410 { 38, 0x00b33, 0x01267, 0x26014, 0x30284 },
411 { 42, 0x00b33, 0x01268, 0x26014, 0x30286 },
412 { 46, 0x00b33, 0x01269, 0x26014, 0x30288 },
413
414 { 36, 0x00b33, 0x01266, 0x26014, 0x30288 },
415 { 40, 0x00b33, 0x01268, 0x26014, 0x30280 },
416 { 44, 0x00b33, 0x01269, 0x26014, 0x30282 },
417 { 48, 0x00b33, 0x0126a, 0x26014, 0x30284 },
418 { 52, 0x00b33, 0x0126b, 0x26014, 0x30286 },
419 { 56, 0x00b33, 0x0126c, 0x26014, 0x30288 },
420 { 60, 0x00b33, 0x0126e, 0x26014, 0x30280 },
421 { 64, 0x00b33, 0x0126f, 0x26014, 0x30282 },
422
423 { 100, 0x00b33, 0x0128a, 0x2e014, 0x30280 },
424 { 104, 0x00b33, 0x0128b, 0x2e014, 0x30282 },
425 { 108, 0x00b33, 0x0128c, 0x2e014, 0x30284 },
426 { 112, 0x00b33, 0x0128d, 0x2e014, 0x30286 },
427 { 116, 0x00b33, 0x0128e, 0x2e014, 0x30288 },
428 { 120, 0x00b33, 0x012a0, 0x2e014, 0x30280 },
429 { 124, 0x00b33, 0x012a1, 0x2e014, 0x30282 },
430 { 128, 0x00b33, 0x012a2, 0x2e014, 0x30284 },
431 { 132, 0x00b33, 0x012a3, 0x2e014, 0x30286 },
432 { 136, 0x00b33, 0x012a4, 0x2e014, 0x30288 },
433 { 140, 0x00b33, 0x012a6, 0x2e014, 0x30280 },
434
435 { 149, 0x00b33, 0x012a8, 0x2e014, 0x30287 },
436 { 153, 0x00b33, 0x012a9, 0x2e014, 0x30289 },
437 { 157, 0x00b33, 0x012ab, 0x2e014, 0x30281 },
438 { 161, 0x00b33, 0x012ac, 0x2e014, 0x30283 },
439 { 165, 0x00b33, 0x012ad, 0x2e014, 0x30285 }
440 };
441
442 static const struct usb_config rum_config[RUM_N_TRANSFER] = {
443 [RUM_BULK_WR] = {
444 .type = UE_BULK,
445 .endpoint = UE_ADDR_ANY,
446 .direction = UE_DIR_OUT,
447 .bufsize = (MCLBYTES + RT2573_TX_DESC_SIZE + 8),
448 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
449 .callback = rum_bulk_write_callback,
450 .timeout = 5000, /* ms */
451 },
452 [RUM_BULK_RD] = {
453 .type = UE_BULK,
454 .endpoint = UE_ADDR_ANY,
455 .direction = UE_DIR_IN,
456 .bufsize = (MCLBYTES + RT2573_RX_DESC_SIZE),
457 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
458 .callback = rum_bulk_read_callback,
459 },
460 };
461
462 static int
rum_match(device_t self)463 rum_match(device_t self)
464 {
465 struct usb_attach_arg *uaa = device_get_ivars(self);
466
467 if (uaa->usb_mode != USB_MODE_HOST)
468 return (ENXIO);
469 if (uaa->info.bConfigIndex != 0)
470 return (ENXIO);
471 if (uaa->info.bIfaceIndex != RT2573_IFACE_INDEX)
472 return (ENXIO);
473
474 return (usbd_lookup_id_by_uaa(rum_devs, sizeof(rum_devs), uaa));
475 }
476
477 static int
rum_attach(device_t self)478 rum_attach(device_t self)
479 {
480 struct usb_attach_arg *uaa = device_get_ivars(self);
481 struct rum_softc *sc = device_get_softc(self);
482 struct ieee80211com *ic = &sc->sc_ic;
483 uint32_t tmp;
484 uint8_t iface_index;
485 int error, ntries;
486
487 device_set_usb_desc(self);
488 sc->sc_udev = uaa->device;
489 sc->sc_dev = self;
490
491 RUM_LOCK_INIT(sc);
492 RUM_CMDQ_LOCK_INIT(sc);
493 mbufq_init(&sc->sc_snd, ifqmaxlen);
494
495 iface_index = RT2573_IFACE_INDEX;
496 error = usbd_transfer_setup(uaa->device, &iface_index,
497 sc->sc_xfer, rum_config, RUM_N_TRANSFER, sc, &sc->sc_mtx);
498 if (error) {
499 device_printf(self, "could not allocate USB transfers, "
500 "err=%s\n", usbd_errstr(error));
501 goto detach;
502 }
503
504 RUM_LOCK(sc);
505 /* retrieve RT2573 rev. no */
506 for (ntries = 0; ntries < 100; ntries++) {
507 if ((tmp = rum_read(sc, RT2573_MAC_CSR0)) != 0)
508 break;
509 if (rum_pause(sc, hz / 100))
510 break;
511 }
512 if (ntries == 100) {
513 device_printf(sc->sc_dev, "timeout waiting for chip to settle\n");
514 RUM_UNLOCK(sc);
515 goto detach;
516 }
517
518 /* retrieve MAC address and various other things from EEPROM */
519 rum_read_eeprom(sc);
520
521 device_printf(sc->sc_dev, "MAC/BBP RT2573 (rev 0x%05x), RF %s\n",
522 tmp, rum_get_rf(sc->rf_rev));
523
524 rum_load_microcode(sc, rt2573_ucode, sizeof(rt2573_ucode));
525 RUM_UNLOCK(sc);
526
527 ic->ic_softc = sc;
528 ic->ic_name = device_get_nameunit(self);
529 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
530
531 /* set device capabilities */
532 ic->ic_caps =
533 IEEE80211_C_STA /* station mode supported */
534 | IEEE80211_C_IBSS /* IBSS mode supported */
535 | IEEE80211_C_MONITOR /* monitor mode supported */
536 | IEEE80211_C_HOSTAP /* HostAp mode supported */
537 | IEEE80211_C_AHDEMO /* adhoc demo mode */
538 | IEEE80211_C_TXPMGT /* tx power management */
539 | IEEE80211_C_SHPREAMBLE /* short preamble supported */
540 | IEEE80211_C_SHSLOT /* short slot time supported */
541 | IEEE80211_C_BGSCAN /* bg scanning supported */
542 | IEEE80211_C_WPA /* 802.11i */
543 | IEEE80211_C_WME /* 802.11e */
544 | IEEE80211_C_PMGT /* Station-side power mgmt */
545 | IEEE80211_C_SWSLEEP /* net80211 managed power mgmt */
546 ;
547
548 ic->ic_cryptocaps =
549 IEEE80211_CRYPTO_WEP |
550 IEEE80211_CRYPTO_AES_CCM |
551 IEEE80211_CRYPTO_TKIPMIC |
552 IEEE80211_CRYPTO_TKIP;
553
554 rum_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
555 ic->ic_channels);
556
557 ieee80211_ifattach(ic);
558 ic->ic_update_promisc = rum_update_promisc;
559 ic->ic_raw_xmit = rum_raw_xmit;
560 ic->ic_scan_start = rum_scan_start;
561 ic->ic_scan_end = rum_scan_end;
562 ic->ic_set_channel = rum_set_channel;
563 ic->ic_getradiocaps = rum_getradiocaps;
564 ic->ic_transmit = rum_transmit;
565 ic->ic_parent = rum_parent;
566 ic->ic_vap_create = rum_vap_create;
567 ic->ic_vap_delete = rum_vap_delete;
568 ic->ic_updateslot = rum_update_slot;
569 ic->ic_wme.wme_update = rum_wme_update;
570 ic->ic_update_mcast = rum_update_mcast;
571
572 ieee80211_radiotap_attach(ic,
573 &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
574 RT2573_TX_RADIOTAP_PRESENT,
575 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
576 RT2573_RX_RADIOTAP_PRESENT);
577
578 TASK_INIT(&sc->cmdq_task, 0, rum_cmdq_cb, sc);
579
580 if (bootverbose)
581 ieee80211_announce(ic);
582
583 return (0);
584
585 detach:
586 rum_detach(self);
587 return (ENXIO); /* failure */
588 }
589
590 static int
rum_detach(device_t self)591 rum_detach(device_t self)
592 {
593 struct rum_softc *sc = device_get_softc(self);
594 struct ieee80211com *ic = &sc->sc_ic;
595
596 /* Prevent further ioctls */
597 RUM_LOCK(sc);
598 sc->sc_detached = 1;
599 RUM_UNLOCK(sc);
600
601 /* stop all USB transfers */
602 usbd_transfer_unsetup(sc->sc_xfer, RUM_N_TRANSFER);
603
604 /* free TX list, if any */
605 RUM_LOCK(sc);
606 rum_unsetup_tx_list(sc);
607 RUM_UNLOCK(sc);
608
609 if (ic->ic_softc == sc) {
610 ieee80211_draintask(ic, &sc->cmdq_task);
611 ieee80211_ifdetach(ic);
612 }
613
614 mbufq_drain(&sc->sc_snd);
615 RUM_CMDQ_LOCK_DESTROY(sc);
616 RUM_LOCK_DESTROY(sc);
617
618 return (0);
619 }
620
621 static usb_error_t
rum_do_request(struct rum_softc * sc,struct usb_device_request * req,void * data)622 rum_do_request(struct rum_softc *sc,
623 struct usb_device_request *req, void *data)
624 {
625 usb_error_t err;
626 int ntries = 10;
627
628 while (ntries--) {
629 err = usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
630 req, data, 0, NULL, 250 /* ms */);
631 if (err == 0)
632 break;
633
634 DPRINTFN(1, "Control request failed, %s (retrying)\n",
635 usbd_errstr(err));
636 if (rum_pause(sc, hz / 100))
637 break;
638 }
639 return (err);
640 }
641
642 static usb_error_t
rum_do_mcu_request(struct rum_softc * sc,int request)643 rum_do_mcu_request(struct rum_softc *sc, int request)
644 {
645 struct usb_device_request req;
646
647 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
648 req.bRequest = RT2573_MCU_CNTL;
649 USETW(req.wValue, request);
650 USETW(req.wIndex, 0);
651 USETW(req.wLength, 0);
652
653 return (rum_do_request(sc, &req, NULL));
654 }
655
656 static struct ieee80211vap *
rum_vap_create(struct ieee80211com * ic,const char name[IFNAMSIZ],int unit,enum ieee80211_opmode opmode,int flags,const uint8_t bssid[IEEE80211_ADDR_LEN],const uint8_t mac[IEEE80211_ADDR_LEN])657 rum_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
658 enum ieee80211_opmode opmode, int flags,
659 const uint8_t bssid[IEEE80211_ADDR_LEN],
660 const uint8_t mac[IEEE80211_ADDR_LEN])
661 {
662 struct rum_softc *sc = ic->ic_softc;
663 struct rum_vap *rvp;
664 struct ieee80211vap *vap;
665
666 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */
667 return NULL;
668 rvp = malloc(sizeof(struct rum_vap), M_80211_VAP, M_WAITOK | M_ZERO);
669 vap = &rvp->vap;
670 /* enable s/w bmiss handling for sta mode */
671
672 if (ieee80211_vap_setup(ic, vap, name, unit, opmode,
673 flags | IEEE80211_CLONE_NOBEACONS, bssid) != 0) {
674 /* out of memory */
675 free(rvp, M_80211_VAP);
676 return (NULL);
677 }
678
679 /* override state transition machine */
680 rvp->newstate = vap->iv_newstate;
681 vap->iv_newstate = rum_newstate;
682 vap->iv_key_alloc = rum_key_alloc;
683 vap->iv_key_set = rum_key_set;
684 vap->iv_key_delete = rum_key_delete;
685 vap->iv_update_beacon = rum_update_beacon;
686 vap->iv_reset = rum_reset;
687 vap->iv_max_aid = RT2573_ADDR_MAX;
688
689 if (opmode == IEEE80211_M_STA) {
690 /*
691 * Move device to the sleep state when
692 * beacon is received and there is no data for us.
693 *
694 * Used only for IEEE80211_S_SLEEP state.
695 */
696 rvp->recv_mgmt = vap->iv_recv_mgmt;
697 vap->iv_recv_mgmt = rum_sta_recv_mgmt;
698
699 /* Ignored while sleeping. */
700 rvp->bmiss = vap->iv_bmiss;
701 vap->iv_bmiss = rum_beacon_miss;
702 }
703
704 usb_callout_init_mtx(&rvp->ratectl_ch, &sc->sc_mtx, 0);
705 TASK_INIT(&rvp->ratectl_task, 0, rum_ratectl_task, rvp);
706 ieee80211_ratectl_init(vap);
707 ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */);
708 /* complete setup */
709 ieee80211_vap_attach(vap, ieee80211_media_change,
710 ieee80211_media_status, mac);
711 ic->ic_opmode = opmode;
712 return vap;
713 }
714
715 static void
rum_vap_delete(struct ieee80211vap * vap)716 rum_vap_delete(struct ieee80211vap *vap)
717 {
718 struct rum_vap *rvp = RUM_VAP(vap);
719 struct ieee80211com *ic = vap->iv_ic;
720 struct rum_softc *sc = ic->ic_softc;
721 int i;
722
723 /* Put vap into INIT state. */
724 ieee80211_new_state(vap, IEEE80211_S_INIT, -1);
725 for (i = 0; i < NET80211_IV_NSTATE_NUM; i++)
726 ieee80211_draintask(ic, &vap->iv_nstate_task[i]);
727
728 RUM_LOCK(sc);
729 /* Cancel any unfinished Tx. */
730 rum_reset_tx_list(sc, vap);
731 RUM_UNLOCK(sc);
732
733 usb_callout_drain(&rvp->ratectl_ch);
734 ieee80211_draintask(ic, &rvp->ratectl_task);
735 ieee80211_ratectl_deinit(vap);
736 ieee80211_vap_detach(vap);
737 m_freem(rvp->bcn_mbuf);
738 free(rvp, M_80211_VAP);
739 }
740
741 static void
rum_cmdq_cb(void * arg,int pending)742 rum_cmdq_cb(void *arg, int pending)
743 {
744 struct rum_softc *sc = arg;
745 struct rum_cmdq *rc;
746
747 RUM_CMDQ_LOCK(sc);
748 while (sc->cmdq[sc->cmdq_first].func != NULL) {
749 rc = &sc->cmdq[sc->cmdq_first];
750 RUM_CMDQ_UNLOCK(sc);
751
752 RUM_LOCK(sc);
753 rc->func(sc, &rc->data, rc->rvp_id);
754 RUM_UNLOCK(sc);
755
756 RUM_CMDQ_LOCK(sc);
757 memset(rc, 0, sizeof (*rc));
758 sc->cmdq_first = (sc->cmdq_first + 1) % RUM_CMDQ_SIZE;
759 }
760 RUM_CMDQ_UNLOCK(sc);
761 }
762
763 static int
rum_cmd_sleepable(struct rum_softc * sc,const void * ptr,size_t len,uint8_t rvp_id,CMD_FUNC_PROTO)764 rum_cmd_sleepable(struct rum_softc *sc, const void *ptr, size_t len,
765 uint8_t rvp_id, CMD_FUNC_PROTO)
766 {
767 struct ieee80211com *ic = &sc->sc_ic;
768
769 KASSERT(len <= sizeof(union sec_param), ("buffer overflow"));
770
771 RUM_CMDQ_LOCK(sc);
772 if (sc->cmdq[sc->cmdq_last].func != NULL) {
773 device_printf(sc->sc_dev, "%s: cmdq overflow\n", __func__);
774 RUM_CMDQ_UNLOCK(sc);
775
776 return EAGAIN;
777 }
778
779 if (ptr != NULL)
780 memcpy(&sc->cmdq[sc->cmdq_last].data, ptr, len);
781 sc->cmdq[sc->cmdq_last].rvp_id = rvp_id;
782 sc->cmdq[sc->cmdq_last].func = func;
783 sc->cmdq_last = (sc->cmdq_last + 1) % RUM_CMDQ_SIZE;
784 RUM_CMDQ_UNLOCK(sc);
785
786 ieee80211_runtask(ic, &sc->cmdq_task);
787
788 return 0;
789 }
790
791 static void
rum_tx_free(struct rum_tx_data * data,int txerr)792 rum_tx_free(struct rum_tx_data *data, int txerr)
793 {
794 struct rum_softc *sc = data->sc;
795
796 if (data->m != NULL) {
797 ieee80211_tx_complete(data->ni, data->m, txerr);
798 data->m = NULL;
799 data->ni = NULL;
800 }
801 STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
802 sc->tx_nfree++;
803 }
804
805 static void
rum_setup_tx_list(struct rum_softc * sc)806 rum_setup_tx_list(struct rum_softc *sc)
807 {
808 struct rum_tx_data *data;
809 int i;
810
811 sc->tx_nfree = 0;
812 STAILQ_INIT(&sc->tx_q);
813 STAILQ_INIT(&sc->tx_free);
814
815 for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
816 data = &sc->tx_data[i];
817
818 data->sc = sc;
819 STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
820 sc->tx_nfree++;
821 }
822 }
823
824 static void
rum_reset_tx_list(struct rum_softc * sc,struct ieee80211vap * vap)825 rum_reset_tx_list(struct rum_softc *sc, struct ieee80211vap *vap)
826 {
827 struct rum_tx_data *data, *tmp;
828
829 KASSERT(vap != NULL, ("%s: vap is NULL\n", __func__));
830
831 STAILQ_FOREACH_SAFE(data, &sc->tx_q, next, tmp) {
832 if (data->ni != NULL && data->ni->ni_vap == vap) {
833 ieee80211_free_node(data->ni);
834 data->ni = NULL;
835
836 KASSERT(data->m != NULL, ("%s: m is NULL\n",
837 __func__));
838 m_freem(data->m);
839 data->m = NULL;
840
841 STAILQ_REMOVE(&sc->tx_q, data, rum_tx_data, next);
842 STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
843 sc->tx_nfree++;
844 }
845 }
846 }
847
848 static void
rum_unsetup_tx_list(struct rum_softc * sc)849 rum_unsetup_tx_list(struct rum_softc *sc)
850 {
851 struct rum_tx_data *data;
852 int i;
853
854 /* make sure any subsequent use of the queues will fail */
855 sc->tx_nfree = 0;
856 STAILQ_INIT(&sc->tx_q);
857 STAILQ_INIT(&sc->tx_free);
858
859 /* free up all node references and mbufs */
860 for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
861 data = &sc->tx_data[i];
862
863 if (data->m != NULL) {
864 m_freem(data->m);
865 data->m = NULL;
866 }
867 if (data->ni != NULL) {
868 ieee80211_free_node(data->ni);
869 data->ni = NULL;
870 }
871 }
872 }
873
874 static void
rum_beacon_miss(struct ieee80211vap * vap)875 rum_beacon_miss(struct ieee80211vap *vap)
876 {
877 struct ieee80211com *ic = vap->iv_ic;
878 struct rum_softc *sc = ic->ic_softc;
879 struct rum_vap *rvp = RUM_VAP(vap);
880 int sleep;
881
882 RUM_LOCK(sc);
883 if (sc->sc_sleeping && sc->sc_sleep_end < ticks) {
884 DPRINTFN(12, "dropping 'sleeping' bit, "
885 "device must be awake now\n");
886
887 sc->sc_sleeping = 0;
888 }
889
890 sleep = sc->sc_sleeping;
891 RUM_UNLOCK(sc);
892
893 if (!sleep)
894 rvp->bmiss(vap);
895 #ifdef USB_DEBUG
896 else
897 DPRINTFN(13, "bmiss event is ignored whilst sleeping\n");
898 #endif
899 }
900
901 static void
rum_sta_recv_mgmt(struct ieee80211_node * ni,struct mbuf * m,int subtype,const struct ieee80211_rx_stats * rxs,int rssi,int nf)902 rum_sta_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m, int subtype,
903 const struct ieee80211_rx_stats *rxs,
904 int rssi, int nf)
905 {
906 struct ieee80211vap *vap = ni->ni_vap;
907 struct rum_softc *sc = vap->iv_ic->ic_softc;
908 struct rum_vap *rvp = RUM_VAP(vap);
909
910 if (vap->iv_state == IEEE80211_S_SLEEP &&
911 subtype == IEEE80211_FC0_SUBTYPE_BEACON) {
912 RUM_LOCK(sc);
913 DPRINTFN(12, "beacon, mybss %d (flags %02X)\n",
914 !!(sc->last_rx_flags & RT2573_RX_MYBSS),
915 sc->last_rx_flags);
916
917 if ((sc->last_rx_flags & (RT2573_RX_MYBSS | RT2573_RX_BC)) ==
918 (RT2573_RX_MYBSS | RT2573_RX_BC)) {
919 /*
920 * Put it to sleep here; in case if there is a data
921 * for us, iv_recv_mgmt() will wakeup the device via
922 * SLEEP -> RUN state transition.
923 */
924 rum_set_power_state(sc, 1);
925 }
926 RUM_UNLOCK(sc);
927 }
928
929 rvp->recv_mgmt(ni, m, subtype, rxs, rssi, nf);
930 }
931
932 static int
rum_set_power_state(struct rum_softc * sc,int sleep)933 rum_set_power_state(struct rum_softc *sc, int sleep)
934 {
935 usb_error_t uerror;
936
937 RUM_LOCK_ASSERT(sc);
938
939 DPRINTFN(12, "moving to %s state (sleep time %u)\n",
940 sleep ? "sleep" : "awake", sc->sc_sleep_time);
941
942 uerror = rum_do_mcu_request(sc,
943 sleep ? RT2573_MCU_SLEEP : RT2573_MCU_WAKEUP);
944 if (uerror != USB_ERR_NORMAL_COMPLETION) {
945 device_printf(sc->sc_dev,
946 "%s: could not change power state: %s\n",
947 __func__, usbd_errstr(uerror));
948 return (EIO);
949 }
950
951 sc->sc_sleeping = !!sleep;
952 sc->sc_sleep_end = sleep ? ticks + sc->sc_sleep_time : 0;
953
954 return (0);
955 }
956
957 static int
rum_newstate(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)958 rum_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
959 {
960 struct rum_vap *rvp = RUM_VAP(vap);
961 struct ieee80211com *ic = vap->iv_ic;
962 struct rum_softc *sc = ic->ic_softc;
963 const struct ieee80211_txparam *tp;
964 enum ieee80211_state ostate;
965 struct ieee80211_node *ni;
966 usb_error_t uerror;
967 int ret = 0;
968
969 ostate = vap->iv_state;
970 DPRINTF("%s -> %s\n",
971 ieee80211_state_name[ostate],
972 ieee80211_state_name[nstate]);
973
974 IEEE80211_UNLOCK(ic);
975 RUM_LOCK(sc);
976 usb_callout_stop(&rvp->ratectl_ch);
977
978 if (ostate == IEEE80211_S_SLEEP && vap->iv_opmode == IEEE80211_M_STA) {
979 rum_clrbits(sc, RT2573_TXRX_CSR4, RT2573_ACKCTS_PWRMGT);
980 rum_clrbits(sc, RT2573_MAC_CSR11, RT2573_AUTO_WAKEUP);
981
982 /*
983 * Ignore any errors;
984 * any subsequent TX will wakeup it anyway
985 */
986 (void) rum_set_power_state(sc, 0);
987 }
988
989 switch (nstate) {
990 case IEEE80211_S_INIT:
991 if (ostate == IEEE80211_S_RUN)
992 rum_abort_tsf_sync(sc);
993
994 break;
995
996 case IEEE80211_S_RUN:
997 if (ostate == IEEE80211_S_SLEEP)
998 break; /* already handled */
999
1000 ni = ieee80211_ref_node(vap->iv_bss);
1001
1002 if (vap->iv_opmode != IEEE80211_M_MONITOR) {
1003 if (ic->ic_bsschan == IEEE80211_CHAN_ANYC ||
1004 ni->ni_chan == IEEE80211_CHAN_ANYC) {
1005 ret = EINVAL;
1006 goto run_fail;
1007 }
1008 rum_update_slot_cb(sc, NULL, 0);
1009 rum_enable_mrr(sc);
1010 rum_set_txpreamble(sc);
1011 rum_set_basicrates(sc);
1012 rum_set_maxretry(sc, vap);
1013 IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid);
1014 rum_set_bssid(sc, sc->sc_bssid);
1015 }
1016
1017 if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
1018 vap->iv_opmode == IEEE80211_M_IBSS) {
1019 if ((ret = rum_alloc_beacon(sc, vap)) != 0)
1020 goto run_fail;
1021 }
1022
1023 if (vap->iv_opmode != IEEE80211_M_MONITOR &&
1024 vap->iv_opmode != IEEE80211_M_AHDEMO) {
1025 if ((ret = rum_enable_tsf_sync(sc)) != 0)
1026 goto run_fail;
1027 } else
1028 rum_enable_tsf(sc);
1029
1030 /* enable automatic rate adaptation */
1031 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1032 if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
1033 rum_ratectl_start(sc, ni);
1034 run_fail:
1035 ieee80211_free_node(ni);
1036 break;
1037 case IEEE80211_S_SLEEP:
1038 /* Implemented for STA mode only. */
1039 if (vap->iv_opmode != IEEE80211_M_STA)
1040 break;
1041
1042 uerror = rum_setbits(sc, RT2573_MAC_CSR11, RT2573_AUTO_WAKEUP);
1043 if (uerror != USB_ERR_NORMAL_COMPLETION) {
1044 ret = EIO;
1045 break;
1046 }
1047
1048 uerror = rum_setbits(sc, RT2573_TXRX_CSR4, RT2573_ACKCTS_PWRMGT);
1049 if (uerror != USB_ERR_NORMAL_COMPLETION) {
1050 ret = EIO;
1051 break;
1052 }
1053
1054 ret = rum_set_power_state(sc, 1);
1055 if (ret != 0) {
1056 device_printf(sc->sc_dev,
1057 "%s: could not move to the SLEEP state: %s\n",
1058 __func__, usbd_errstr(uerror));
1059 }
1060 break;
1061 default:
1062 break;
1063 }
1064 RUM_UNLOCK(sc);
1065 IEEE80211_LOCK(ic);
1066 return (ret == 0 ? rvp->newstate(vap, nstate, arg) : ret);
1067 }
1068
1069 static void
rum_bulk_write_callback(struct usb_xfer * xfer,usb_error_t error)1070 rum_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
1071 {
1072 struct rum_softc *sc = usbd_xfer_softc(xfer);
1073 struct ieee80211vap *vap;
1074 struct rum_tx_data *data;
1075 struct mbuf *m;
1076 struct usb_page_cache *pc;
1077 unsigned len;
1078 int actlen, sumlen;
1079
1080 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
1081
1082 switch (USB_GET_STATE(xfer)) {
1083 case USB_ST_TRANSFERRED:
1084 DPRINTFN(11, "transfer complete, %d bytes\n", actlen);
1085
1086 /* free resources */
1087 data = usbd_xfer_get_priv(xfer);
1088 rum_tx_free(data, 0);
1089 usbd_xfer_set_priv(xfer, NULL);
1090
1091 /* FALLTHROUGH */
1092 case USB_ST_SETUP:
1093 tr_setup:
1094 data = STAILQ_FIRST(&sc->tx_q);
1095 if (data) {
1096 STAILQ_REMOVE_HEAD(&sc->tx_q, next);
1097 m = data->m;
1098
1099 if (m->m_pkthdr.len > (int)(MCLBYTES + RT2573_TX_DESC_SIZE)) {
1100 DPRINTFN(0, "data overflow, %u bytes\n",
1101 m->m_pkthdr.len);
1102 m->m_pkthdr.len = (MCLBYTES + RT2573_TX_DESC_SIZE);
1103 }
1104 pc = usbd_xfer_get_frame(xfer, 0);
1105 usbd_copy_in(pc, 0, &data->desc, RT2573_TX_DESC_SIZE);
1106 usbd_m_copy_in(pc, RT2573_TX_DESC_SIZE, m, 0,
1107 m->m_pkthdr.len);
1108
1109 vap = data->ni->ni_vap;
1110 if (ieee80211_radiotap_active_vap(vap)) {
1111 struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
1112
1113 tap->wt_flags = 0;
1114 tap->wt_rate = data->rate;
1115 tap->wt_antenna = sc->tx_ant;
1116
1117 ieee80211_radiotap_tx(vap, m);
1118 }
1119
1120 /* align end on a 4-bytes boundary */
1121 len = (RT2573_TX_DESC_SIZE + m->m_pkthdr.len + 3) & ~3;
1122 if ((len % 64) == 0)
1123 len += 4;
1124
1125 DPRINTFN(11, "sending frame len=%u xferlen=%u\n",
1126 m->m_pkthdr.len, len);
1127
1128 usbd_xfer_set_frame_len(xfer, 0, len);
1129 usbd_xfer_set_priv(xfer, data);
1130
1131 usbd_transfer_submit(xfer);
1132 }
1133 rum_start(sc);
1134 break;
1135
1136 default: /* Error */
1137 DPRINTFN(11, "transfer error, %s\n",
1138 usbd_errstr(error));
1139
1140 counter_u64_add(sc->sc_ic.ic_oerrors, 1);
1141 data = usbd_xfer_get_priv(xfer);
1142 if (data != NULL) {
1143 rum_tx_free(data, error);
1144 usbd_xfer_set_priv(xfer, NULL);
1145 }
1146
1147 if (error != USB_ERR_CANCELLED) {
1148 if (error == USB_ERR_TIMEOUT)
1149 device_printf(sc->sc_dev, "device timeout\n");
1150
1151 /*
1152 * Try to clear stall first, also if other
1153 * errors occur, hence clearing stall
1154 * introduces a 50 ms delay:
1155 */
1156 usbd_xfer_set_stall(xfer);
1157 goto tr_setup;
1158 }
1159 break;
1160 }
1161 }
1162
1163 static void
rum_bulk_read_callback(struct usb_xfer * xfer,usb_error_t error)1164 rum_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
1165 {
1166 struct rum_softc *sc = usbd_xfer_softc(xfer);
1167 struct ieee80211com *ic = &sc->sc_ic;
1168 struct ieee80211_frame_min *wh;
1169 struct ieee80211_node *ni;
1170 struct mbuf *m = NULL;
1171 struct usb_page_cache *pc;
1172 uint32_t flags;
1173 uint8_t rssi = 0;
1174 int len;
1175
1176 usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
1177
1178 switch (USB_GET_STATE(xfer)) {
1179 case USB_ST_TRANSFERRED:
1180
1181 DPRINTFN(15, "rx done, actlen=%d\n", len);
1182
1183 if (len < RT2573_RX_DESC_SIZE) {
1184 DPRINTF("%s: xfer too short %d\n",
1185 device_get_nameunit(sc->sc_dev), len);
1186 counter_u64_add(ic->ic_ierrors, 1);
1187 goto tr_setup;
1188 }
1189
1190 len -= RT2573_RX_DESC_SIZE;
1191 pc = usbd_xfer_get_frame(xfer, 0);
1192 usbd_copy_out(pc, 0, &sc->sc_rx_desc, RT2573_RX_DESC_SIZE);
1193
1194 rssi = rum_get_rssi(sc, sc->sc_rx_desc.rssi);
1195 flags = le32toh(sc->sc_rx_desc.flags);
1196 sc->last_rx_flags = flags;
1197 if (len < ((flags >> 16) & 0xfff)) {
1198 DPRINTFN(5, "%s: frame is truncated from %d to %d "
1199 "bytes\n", device_get_nameunit(sc->sc_dev),
1200 (flags >> 16) & 0xfff, len);
1201 counter_u64_add(ic->ic_ierrors, 1);
1202 goto tr_setup;
1203 }
1204 len = (flags >> 16) & 0xfff;
1205 if (len < sizeof(struct ieee80211_frame_ack)) {
1206 DPRINTFN(5, "%s: frame too short %d\n",
1207 device_get_nameunit(sc->sc_dev), len);
1208 counter_u64_add(ic->ic_ierrors, 1);
1209 goto tr_setup;
1210 }
1211 if (flags & RT2573_RX_CRC_ERROR) {
1212 /*
1213 * This should not happen since we did not
1214 * request to receive those frames when we
1215 * filled RUM_TXRX_CSR2:
1216 */
1217 DPRINTFN(5, "PHY or CRC error\n");
1218 counter_u64_add(ic->ic_ierrors, 1);
1219 goto tr_setup;
1220 }
1221 if ((flags & RT2573_RX_DEC_MASK) != RT2573_RX_DEC_OK) {
1222 switch (flags & RT2573_RX_DEC_MASK) {
1223 case RT2573_RX_IV_ERROR:
1224 DPRINTFN(5, "IV/EIV error\n");
1225 break;
1226 case RT2573_RX_MIC_ERROR:
1227 DPRINTFN(5, "MIC error\n");
1228 break;
1229 case RT2573_RX_KEY_ERROR:
1230 DPRINTFN(5, "Key error\n");
1231 break;
1232 }
1233 counter_u64_add(ic->ic_ierrors, 1);
1234 goto tr_setup;
1235 }
1236
1237 m = m_get2(len, M_NOWAIT, MT_DATA, M_PKTHDR);
1238 if (m == NULL) {
1239 DPRINTF("could not allocate mbuf\n");
1240 counter_u64_add(ic->ic_ierrors, 1);
1241 goto tr_setup;
1242 }
1243 usbd_copy_out(pc, RT2573_RX_DESC_SIZE,
1244 mtod(m, uint8_t *), len);
1245
1246 wh = mtod(m, struct ieee80211_frame_min *);
1247
1248 if ((wh->i_fc[1] & IEEE80211_FC1_PROTECTED) &&
1249 (flags & RT2573_RX_CIP_MASK) !=
1250 RT2573_RX_CIP_MODE(RT2573_MODE_NOSEC)) {
1251 wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
1252 m->m_flags |= M_WEP;
1253 }
1254
1255 /* finalize mbuf */
1256 m->m_pkthdr.len = m->m_len = len;
1257
1258 if (ieee80211_radiotap_active(ic)) {
1259 struct rum_rx_radiotap_header *tap = &sc->sc_rxtap;
1260
1261 tap->wr_flags = 0;
1262 tap->wr_rate = ieee80211_plcp2rate(sc->sc_rx_desc.rate,
1263 (flags & RT2573_RX_OFDM) ?
1264 IEEE80211_T_OFDM : IEEE80211_T_CCK);
1265 rum_get_tsf(sc, &tap->wr_tsf);
1266 tap->wr_antsignal = RT2573_NOISE_FLOOR + rssi;
1267 tap->wr_antnoise = RT2573_NOISE_FLOOR;
1268 tap->wr_antenna = sc->rx_ant;
1269 }
1270 /* FALLTHROUGH */
1271 case USB_ST_SETUP:
1272 tr_setup:
1273 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1274 usbd_transfer_submit(xfer);
1275
1276 /*
1277 * At the end of a USB callback it is always safe to unlock
1278 * the private mutex of a device! That is why we do the
1279 * "ieee80211_input" here, and not some lines up!
1280 */
1281 RUM_UNLOCK(sc);
1282 if (m) {
1283 if (m->m_len >= sizeof(struct ieee80211_frame_min))
1284 ni = ieee80211_find_rxnode(ic, wh);
1285 else
1286 ni = NULL;
1287
1288 if (ni != NULL) {
1289 (void) ieee80211_input(ni, m, rssi,
1290 RT2573_NOISE_FLOOR);
1291 ieee80211_free_node(ni);
1292 } else
1293 (void) ieee80211_input_all(ic, m, rssi,
1294 RT2573_NOISE_FLOOR);
1295 }
1296 RUM_LOCK(sc);
1297 rum_start(sc);
1298 return;
1299
1300 default: /* Error */
1301 if (error != USB_ERR_CANCELLED) {
1302 /* try to clear stall first */
1303 usbd_xfer_set_stall(xfer);
1304 goto tr_setup;
1305 }
1306 return;
1307 }
1308 }
1309
1310 static uint8_t
rum_plcp_signal(int rate)1311 rum_plcp_signal(int rate)
1312 {
1313 switch (rate) {
1314 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1315 case 12: return 0xb;
1316 case 18: return 0xf;
1317 case 24: return 0xa;
1318 case 36: return 0xe;
1319 case 48: return 0x9;
1320 case 72: return 0xd;
1321 case 96: return 0x8;
1322 case 108: return 0xc;
1323
1324 /* CCK rates (NB: not IEEE std, device-specific) */
1325 case 2: return 0x0;
1326 case 4: return 0x1;
1327 case 11: return 0x2;
1328 case 22: return 0x3;
1329 }
1330 return 0xff; /* XXX unsupported/unknown rate */
1331 }
1332
1333 /*
1334 * Map net80211 cipher to RT2573 security mode.
1335 */
1336 static uint8_t
rum_crypto_mode(struct rum_softc * sc,u_int cipher,int keylen)1337 rum_crypto_mode(struct rum_softc *sc, u_int cipher, int keylen)
1338 {
1339 switch (cipher) {
1340 case IEEE80211_CIPHER_WEP:
1341 return (keylen < 8 ? RT2573_MODE_WEP40 : RT2573_MODE_WEP104);
1342 case IEEE80211_CIPHER_TKIP:
1343 return RT2573_MODE_TKIP;
1344 case IEEE80211_CIPHER_AES_CCM:
1345 return RT2573_MODE_AES_CCMP;
1346 default:
1347 device_printf(sc->sc_dev, "unknown cipher %d\n", cipher);
1348 return 0;
1349 }
1350 }
1351
1352 static void
rum_setup_tx_desc(struct rum_softc * sc,struct rum_tx_desc * desc,struct ieee80211_key * k,uint32_t flags,uint8_t xflags,uint8_t qid,int hdrlen,int len,int rate)1353 rum_setup_tx_desc(struct rum_softc *sc, struct rum_tx_desc *desc,
1354 struct ieee80211_key *k, uint32_t flags, uint8_t xflags, uint8_t qid,
1355 int hdrlen, int len, int rate)
1356 {
1357 struct ieee80211com *ic = &sc->sc_ic;
1358 struct wmeParams *wmep = &sc->wme_params[qid];
1359 uint16_t plcp_length;
1360 int remainder;
1361
1362 flags |= RT2573_TX_VALID;
1363 flags |= len << 16;
1364
1365 if (k != NULL && !(k->wk_flags & IEEE80211_KEY_SWCRYPT)) {
1366 const struct ieee80211_cipher *cip = k->wk_cipher;
1367
1368 len += cip->ic_header + cip->ic_trailer + cip->ic_miclen;
1369
1370 desc->eiv = 0; /* for WEP */
1371 cip->ic_setiv(k, (uint8_t *)&desc->iv);
1372 }
1373
1374 /* setup PLCP fields */
1375 desc->plcp_signal = rum_plcp_signal(rate);
1376 desc->plcp_service = 4;
1377
1378 len += IEEE80211_CRC_LEN;
1379 if (ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) {
1380 flags |= RT2573_TX_OFDM;
1381
1382 plcp_length = len & 0xfff;
1383 desc->plcp_length_hi = plcp_length >> 6;
1384 desc->plcp_length_lo = plcp_length & 0x3f;
1385 } else {
1386 if (rate == 0)
1387 rate = 2; /* avoid division by zero */
1388 plcp_length = howmany(16 * len, rate);
1389 if (rate == 22) {
1390 remainder = (16 * len) % 22;
1391 if (remainder != 0 && remainder < 7)
1392 desc->plcp_service |= RT2573_PLCP_LENGEXT;
1393 }
1394 desc->plcp_length_hi = plcp_length >> 8;
1395 desc->plcp_length_lo = plcp_length & 0xff;
1396
1397 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1398 desc->plcp_signal |= 0x08;
1399 }
1400
1401 desc->flags = htole32(flags);
1402 desc->hdrlen = hdrlen;
1403 desc->xflags = xflags;
1404
1405 desc->wme = htole16(RT2573_QID(qid) |
1406 RT2573_AIFSN(wmep->wmep_aifsn) |
1407 RT2573_LOGCWMIN(wmep->wmep_logcwmin) |
1408 RT2573_LOGCWMAX(wmep->wmep_logcwmax));
1409 }
1410
1411 static int
rum_sendprot(struct rum_softc * sc,const struct mbuf * m,struct ieee80211_node * ni,int prot,int rate)1412 rum_sendprot(struct rum_softc *sc,
1413 const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate)
1414 {
1415 struct ieee80211com *ic = ni->ni_ic;
1416 struct rum_tx_data *data;
1417 struct mbuf *mprot;
1418 int protrate, flags;
1419
1420 RUM_LOCK_ASSERT(sc);
1421
1422 mprot = ieee80211_alloc_prot(ni, m, rate, prot);
1423 if (mprot == NULL) {
1424 if_inc_counter(ni->ni_vap->iv_ifp, IFCOUNTER_OERRORS, 1);
1425 device_printf(sc->sc_dev,
1426 "could not allocate mbuf for protection mode %d\n", prot);
1427 return (ENOBUFS);
1428 }
1429
1430 protrate = ieee80211_ctl_rate(ic->ic_rt, rate);
1431 flags = 0;
1432 if (prot == IEEE80211_PROT_RTSCTS)
1433 flags |= RT2573_TX_NEED_ACK;
1434
1435 data = STAILQ_FIRST(&sc->tx_free);
1436 STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1437 sc->tx_nfree--;
1438
1439 data->m = mprot;
1440 data->ni = ieee80211_ref_node(ni);
1441 data->rate = protrate;
1442 rum_setup_tx_desc(sc, &data->desc, NULL, flags, 0, 0, 0,
1443 mprot->m_pkthdr.len, protrate);
1444
1445 STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1446 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1447
1448 return 0;
1449 }
1450
1451 static uint32_t
rum_tx_crypto_flags(struct rum_softc * sc,struct ieee80211_node * ni,const struct ieee80211_key * k)1452 rum_tx_crypto_flags(struct rum_softc *sc, struct ieee80211_node *ni,
1453 const struct ieee80211_key *k)
1454 {
1455 struct ieee80211vap *vap = ni->ni_vap;
1456 u_int cipher;
1457 uint32_t flags = 0;
1458 uint8_t mode, pos;
1459
1460 if (!(k->wk_flags & IEEE80211_KEY_SWCRYPT)) {
1461 cipher = k->wk_cipher->ic_cipher;
1462 pos = k->wk_keyix;
1463 mode = rum_crypto_mode(sc, cipher, k->wk_keylen);
1464 if (mode == 0)
1465 return 0;
1466
1467 flags |= RT2573_TX_CIP_MODE(mode);
1468
1469 /* Do not trust GROUP flag */
1470 if (ieee80211_is_key_unicast(vap, k))
1471 flags |= RT2573_TX_KEY_PAIR;
1472 else
1473 pos += 0 * RT2573_SKEY_MAX; /* vap id */
1474
1475 flags |= RT2573_TX_KEY_ID(pos);
1476
1477 if (cipher == IEEE80211_CIPHER_TKIP)
1478 flags |= RT2573_TX_TKIPMIC;
1479 }
1480
1481 return flags;
1482 }
1483
1484 static int
rum_tx_mgt(struct rum_softc * sc,struct mbuf * m0,struct ieee80211_node * ni)1485 rum_tx_mgt(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1486 {
1487 const struct ieee80211_txparam *tp = ni->ni_txparms;
1488 struct ieee80211com *ic = &sc->sc_ic;
1489 struct rum_tx_data *data;
1490 struct ieee80211_frame *wh;
1491 struct ieee80211_key *k = NULL;
1492 uint32_t flags = 0;
1493 uint16_t dur;
1494 uint8_t ac, type, xflags = 0;
1495 int hdrlen;
1496
1497 RUM_LOCK_ASSERT(sc);
1498
1499 data = STAILQ_FIRST(&sc->tx_free);
1500 STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1501 sc->tx_nfree--;
1502
1503 wh = mtod(m0, struct ieee80211_frame *);
1504 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1505 hdrlen = ieee80211_anyhdrsize(wh);
1506 ac = M_WME_GETAC(m0);
1507
1508 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1509 k = ieee80211_crypto_get_txkey(ni, m0);
1510 if (k == NULL)
1511 return (ENOENT);
1512
1513 if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) &&
1514 !k->wk_cipher->ic_encap(k, m0))
1515 return (ENOBUFS);
1516
1517 wh = mtod(m0, struct ieee80211_frame *);
1518 }
1519
1520 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1521 flags |= RT2573_TX_NEED_ACK;
1522
1523 dur = ieee80211_ack_duration(ic->ic_rt, tp->mgmtrate,
1524 ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1525 USETW(wh->i_dur, dur);
1526
1527 /* tell hardware to add timestamp for probe responses */
1528 if (IEEE80211_IS_MGMT_PROBE_RESP(wh))
1529 flags |= RT2573_TX_TIMESTAMP;
1530 }
1531
1532 if (type != IEEE80211_FC0_TYPE_CTL && !IEEE80211_QOS_HAS_SEQ(wh))
1533 xflags |= RT2573_TX_HWSEQ;
1534
1535 if (k != NULL)
1536 flags |= rum_tx_crypto_flags(sc, ni, k);
1537
1538 data->m = m0;
1539 data->ni = ni;
1540 data->rate = tp->mgmtrate;
1541
1542 rum_setup_tx_desc(sc, &data->desc, k, flags, xflags, ac, hdrlen,
1543 m0->m_pkthdr.len, tp->mgmtrate);
1544
1545 DPRINTFN(10, "sending mgt frame len=%d rate=%d\n",
1546 m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, tp->mgmtrate);
1547
1548 STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1549 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1550
1551 return (0);
1552 }
1553
1554 static int
rum_tx_raw(struct rum_softc * sc,struct mbuf * m0,struct ieee80211_node * ni,const struct ieee80211_bpf_params * params)1555 rum_tx_raw(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
1556 const struct ieee80211_bpf_params *params)
1557 {
1558 struct ieee80211com *ic = ni->ni_ic;
1559 struct ieee80211_frame *wh;
1560 struct rum_tx_data *data;
1561 uint32_t flags;
1562 uint8_t ac, type, xflags = 0;
1563 int rate, error;
1564
1565 RUM_LOCK_ASSERT(sc);
1566
1567 wh = mtod(m0, struct ieee80211_frame *);
1568 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1569
1570 ac = params->ibp_pri & 3;
1571
1572 rate = params->ibp_rate0;
1573 if (!ieee80211_isratevalid(ic->ic_rt, rate))
1574 return (EINVAL);
1575
1576 flags = 0;
1577 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
1578 flags |= RT2573_TX_NEED_ACK;
1579 if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) {
1580 error = rum_sendprot(sc, m0, ni,
1581 params->ibp_flags & IEEE80211_BPF_RTS ?
1582 IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY,
1583 rate);
1584 if (error || sc->tx_nfree == 0)
1585 return (ENOBUFS);
1586
1587 flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
1588 }
1589
1590 if (type != IEEE80211_FC0_TYPE_CTL && !IEEE80211_QOS_HAS_SEQ(wh))
1591 xflags |= RT2573_TX_HWSEQ;
1592
1593 data = STAILQ_FIRST(&sc->tx_free);
1594 STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1595 sc->tx_nfree--;
1596
1597 data->m = m0;
1598 data->ni = ni;
1599 data->rate = rate;
1600
1601 /* XXX need to setup descriptor ourself */
1602 rum_setup_tx_desc(sc, &data->desc, NULL, flags, xflags, ac, 0,
1603 m0->m_pkthdr.len, rate);
1604
1605 DPRINTFN(10, "sending raw frame len=%u rate=%u\n",
1606 m0->m_pkthdr.len, rate);
1607
1608 STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1609 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1610
1611 return 0;
1612 }
1613
1614 static int
rum_tx_data(struct rum_softc * sc,struct mbuf * m0,struct ieee80211_node * ni)1615 rum_tx_data(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1616 {
1617 struct ieee80211vap *vap = ni->ni_vap;
1618 struct ieee80211com *ic = &sc->sc_ic;
1619 struct rum_tx_data *data;
1620 struct ieee80211_frame *wh;
1621 const struct ieee80211_txparam *tp = ni->ni_txparms;
1622 struct ieee80211_key *k = NULL;
1623 uint32_t flags = 0;
1624 uint16_t dur;
1625 uint8_t ac, type, qos, xflags = 0;
1626 int error, hdrlen, rate;
1627
1628 RUM_LOCK_ASSERT(sc);
1629
1630 wh = mtod(m0, struct ieee80211_frame *);
1631 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1632 hdrlen = ieee80211_anyhdrsize(wh);
1633
1634 if (IEEE80211_QOS_HAS_SEQ(wh))
1635 qos = ((const struct ieee80211_qosframe *)wh)->i_qos[0];
1636 else
1637 qos = 0;
1638 ac = M_WME_GETAC(m0);
1639
1640 if (m0->m_flags & M_EAPOL)
1641 rate = tp->mgmtrate;
1642 else if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1643 rate = tp->mcastrate;
1644 else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
1645 rate = tp->ucastrate;
1646 else {
1647 (void) ieee80211_ratectl_rate(ni, NULL, 0);
1648 rate = ieee80211_node_get_txrate_dot11rate(ni);
1649 }
1650
1651 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1652 k = ieee80211_crypto_get_txkey(ni, m0);
1653 if (k == NULL) {
1654 m_freem(m0);
1655 return (ENOENT);
1656 }
1657 if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) &&
1658 !k->wk_cipher->ic_encap(k, m0)) {
1659 m_freem(m0);
1660 return (ENOBUFS);
1661 }
1662
1663 /* packet header may have moved, reset our local pointer */
1664 wh = mtod(m0, struct ieee80211_frame *);
1665 }
1666
1667 if (type != IEEE80211_FC0_TYPE_CTL && !IEEE80211_QOS_HAS_SEQ(wh))
1668 xflags |= RT2573_TX_HWSEQ;
1669
1670 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1671 int prot = IEEE80211_PROT_NONE;
1672 if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold)
1673 prot = IEEE80211_PROT_RTSCTS;
1674 else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
1675 ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM)
1676 prot = ic->ic_protmode;
1677 if (prot != IEEE80211_PROT_NONE) {
1678 error = rum_sendprot(sc, m0, ni, prot, rate);
1679 if (error || sc->tx_nfree == 0) {
1680 m_freem(m0);
1681 return ENOBUFS;
1682 }
1683 flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
1684 }
1685 }
1686
1687 if (k != NULL)
1688 flags |= rum_tx_crypto_flags(sc, ni, k);
1689
1690 data = STAILQ_FIRST(&sc->tx_free);
1691 STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1692 sc->tx_nfree--;
1693
1694 data->m = m0;
1695 data->ni = ni;
1696 data->rate = rate;
1697
1698 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1699 /* Unicast frame, check if an ACK is expected. */
1700 if (!qos || (qos & IEEE80211_QOS_ACKPOLICY) !=
1701 IEEE80211_QOS_ACKPOLICY_NOACK)
1702 flags |= RT2573_TX_NEED_ACK;
1703
1704 dur = ieee80211_ack_duration(ic->ic_rt, rate,
1705 ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1706 USETW(wh->i_dur, dur);
1707 }
1708
1709 rum_setup_tx_desc(sc, &data->desc, k, flags, xflags, ac, hdrlen,
1710 m0->m_pkthdr.len, rate);
1711
1712 DPRINTFN(10, "sending frame len=%d rate=%d\n",
1713 m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, rate);
1714
1715 STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1716 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1717
1718 return 0;
1719 }
1720
1721 static int
rum_transmit(struct ieee80211com * ic,struct mbuf * m)1722 rum_transmit(struct ieee80211com *ic, struct mbuf *m)
1723 {
1724 struct rum_softc *sc = ic->ic_softc;
1725 int error;
1726
1727 RUM_LOCK(sc);
1728 if (!sc->sc_running) {
1729 RUM_UNLOCK(sc);
1730 return (ENXIO);
1731 }
1732 error = mbufq_enqueue(&sc->sc_snd, m);
1733 if (error) {
1734 RUM_UNLOCK(sc);
1735 return (error);
1736 }
1737 rum_start(sc);
1738 RUM_UNLOCK(sc);
1739
1740 return (0);
1741 }
1742
1743 static void
rum_start(struct rum_softc * sc)1744 rum_start(struct rum_softc *sc)
1745 {
1746 struct ieee80211_node *ni;
1747 struct mbuf *m;
1748
1749 RUM_LOCK_ASSERT(sc);
1750
1751 if (!sc->sc_running)
1752 return;
1753
1754 while (sc->tx_nfree >= RUM_TX_MINFREE &&
1755 (m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
1756 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1757 if (rum_tx_data(sc, m, ni) != 0) {
1758 if_inc_counter(ni->ni_vap->iv_ifp,
1759 IFCOUNTER_OERRORS, 1);
1760 ieee80211_free_node(ni);
1761 break;
1762 }
1763 }
1764 }
1765
1766 static void
rum_parent(struct ieee80211com * ic)1767 rum_parent(struct ieee80211com *ic)
1768 {
1769 struct rum_softc *sc = ic->ic_softc;
1770 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1771
1772 RUM_LOCK(sc);
1773 if (sc->sc_detached) {
1774 RUM_UNLOCK(sc);
1775 return;
1776 }
1777 RUM_UNLOCK(sc);
1778
1779 if (ic->ic_nrunning > 0) {
1780 if (rum_init(sc) == 0)
1781 ieee80211_start_all(ic);
1782 else
1783 ieee80211_stop(vap);
1784 } else
1785 rum_stop(sc);
1786 }
1787
1788 static void
rum_eeprom_read(struct rum_softc * sc,uint16_t addr,void * buf,int len)1789 rum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len)
1790 {
1791 struct usb_device_request req;
1792 usb_error_t error;
1793
1794 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1795 req.bRequest = RT2573_READ_EEPROM;
1796 USETW(req.wValue, 0);
1797 USETW(req.wIndex, addr);
1798 USETW(req.wLength, len);
1799
1800 error = rum_do_request(sc, &req, buf);
1801 if (error != 0) {
1802 device_printf(sc->sc_dev, "could not read EEPROM: %s\n",
1803 usbd_errstr(error));
1804 }
1805 }
1806
1807 static uint32_t
rum_read(struct rum_softc * sc,uint16_t reg)1808 rum_read(struct rum_softc *sc, uint16_t reg)
1809 {
1810 uint32_t val;
1811
1812 rum_read_multi(sc, reg, &val, sizeof val);
1813
1814 return le32toh(val);
1815 }
1816
1817 static void
rum_read_multi(struct rum_softc * sc,uint16_t reg,void * buf,int len)1818 rum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len)
1819 {
1820 struct usb_device_request req;
1821 usb_error_t error;
1822
1823 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1824 req.bRequest = RT2573_READ_MULTI_MAC;
1825 USETW(req.wValue, 0);
1826 USETW(req.wIndex, reg);
1827 USETW(req.wLength, len);
1828
1829 error = rum_do_request(sc, &req, buf);
1830 if (error != 0) {
1831 device_printf(sc->sc_dev,
1832 "could not multi read MAC register: %s\n",
1833 usbd_errstr(error));
1834 }
1835 }
1836
1837 static usb_error_t
rum_write(struct rum_softc * sc,uint16_t reg,uint32_t val)1838 rum_write(struct rum_softc *sc, uint16_t reg, uint32_t val)
1839 {
1840 uint32_t tmp = htole32(val);
1841
1842 return (rum_write_multi(sc, reg, &tmp, sizeof tmp));
1843 }
1844
1845 static usb_error_t
rum_write_multi(struct rum_softc * sc,uint16_t reg,void * buf,size_t len)1846 rum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len)
1847 {
1848 struct usb_device_request req;
1849 usb_error_t error;
1850 size_t offset;
1851
1852 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1853 req.bRequest = RT2573_WRITE_MULTI_MAC;
1854 USETW(req.wValue, 0);
1855
1856 /* write at most 64 bytes at a time */
1857 for (offset = 0; offset < len; offset += 64) {
1858 USETW(req.wIndex, reg + offset);
1859 USETW(req.wLength, MIN(len - offset, 64));
1860
1861 error = rum_do_request(sc, &req, (char *)buf + offset);
1862 if (error != 0) {
1863 device_printf(sc->sc_dev,
1864 "could not multi write MAC register: %s\n",
1865 usbd_errstr(error));
1866 return (error);
1867 }
1868 }
1869
1870 return (USB_ERR_NORMAL_COMPLETION);
1871 }
1872
1873 static usb_error_t
rum_setbits(struct rum_softc * sc,uint16_t reg,uint32_t mask)1874 rum_setbits(struct rum_softc *sc, uint16_t reg, uint32_t mask)
1875 {
1876 return (rum_write(sc, reg, rum_read(sc, reg) | mask));
1877 }
1878
1879 static usb_error_t
rum_clrbits(struct rum_softc * sc,uint16_t reg,uint32_t mask)1880 rum_clrbits(struct rum_softc *sc, uint16_t reg, uint32_t mask)
1881 {
1882 return (rum_write(sc, reg, rum_read(sc, reg) & ~mask));
1883 }
1884
1885 static usb_error_t
rum_modbits(struct rum_softc * sc,uint16_t reg,uint32_t set,uint32_t unset)1886 rum_modbits(struct rum_softc *sc, uint16_t reg, uint32_t set, uint32_t unset)
1887 {
1888 return (rum_write(sc, reg, (rum_read(sc, reg) & ~unset) | set));
1889 }
1890
1891 static int
rum_bbp_busy(struct rum_softc * sc)1892 rum_bbp_busy(struct rum_softc *sc)
1893 {
1894 int ntries;
1895
1896 for (ntries = 0; ntries < 100; ntries++) {
1897 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1898 break;
1899 if (rum_pause(sc, hz / 100))
1900 break;
1901 }
1902 if (ntries == 100)
1903 return (ETIMEDOUT);
1904
1905 return (0);
1906 }
1907
1908 static void
rum_bbp_write(struct rum_softc * sc,uint8_t reg,uint8_t val)1909 rum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val)
1910 {
1911 uint32_t tmp;
1912
1913 DPRINTFN(2, "reg=0x%08x\n", reg);
1914
1915 if (rum_bbp_busy(sc) != 0) {
1916 device_printf(sc->sc_dev, "could not write to BBP\n");
1917 return;
1918 }
1919
1920 tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val;
1921 rum_write(sc, RT2573_PHY_CSR3, tmp);
1922 }
1923
1924 static uint8_t
rum_bbp_read(struct rum_softc * sc,uint8_t reg)1925 rum_bbp_read(struct rum_softc *sc, uint8_t reg)
1926 {
1927 uint32_t val;
1928 int ntries;
1929
1930 DPRINTFN(2, "reg=0x%08x\n", reg);
1931
1932 if (rum_bbp_busy(sc) != 0) {
1933 device_printf(sc->sc_dev, "could not read BBP\n");
1934 return 0;
1935 }
1936
1937 val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8;
1938 rum_write(sc, RT2573_PHY_CSR3, val);
1939
1940 for (ntries = 0; ntries < 100; ntries++) {
1941 val = rum_read(sc, RT2573_PHY_CSR3);
1942 if (!(val & RT2573_BBP_BUSY))
1943 return val & 0xff;
1944 if (rum_pause(sc, hz / 100))
1945 break;
1946 }
1947
1948 device_printf(sc->sc_dev, "could not read BBP\n");
1949 return 0;
1950 }
1951
1952 static void
rum_rf_write(struct rum_softc * sc,uint8_t reg,uint32_t val)1953 rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val)
1954 {
1955 uint32_t tmp;
1956 int ntries;
1957
1958 for (ntries = 0; ntries < 100; ntries++) {
1959 if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY))
1960 break;
1961 if (rum_pause(sc, hz / 100))
1962 break;
1963 }
1964 if (ntries == 100) {
1965 device_printf(sc->sc_dev, "could not write to RF\n");
1966 return;
1967 }
1968
1969 tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 |
1970 (reg & 3);
1971 rum_write(sc, RT2573_PHY_CSR4, tmp);
1972
1973 /* remember last written value in sc */
1974 sc->rf_regs[reg] = val;
1975
1976 DPRINTFN(15, "RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff);
1977 }
1978
1979 static void
rum_select_antenna(struct rum_softc * sc)1980 rum_select_antenna(struct rum_softc *sc)
1981 {
1982 uint8_t bbp4, bbp77;
1983 uint32_t tmp;
1984
1985 bbp4 = rum_bbp_read(sc, 4);
1986 bbp77 = rum_bbp_read(sc, 77);
1987
1988 /* TBD */
1989
1990 /* make sure Rx is disabled before switching antenna */
1991 tmp = rum_read(sc, RT2573_TXRX_CSR0);
1992 rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
1993
1994 rum_bbp_write(sc, 4, bbp4);
1995 rum_bbp_write(sc, 77, bbp77);
1996
1997 rum_write(sc, RT2573_TXRX_CSR0, tmp);
1998 }
1999
2000 /*
2001 * Enable multi-rate retries for frames sent at OFDM rates.
2002 * In 802.11b/g mode, allow fallback to CCK rates.
2003 */
2004 static void
rum_enable_mrr(struct rum_softc * sc)2005 rum_enable_mrr(struct rum_softc *sc)
2006 {
2007 struct ieee80211com *ic = &sc->sc_ic;
2008
2009 if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) {
2010 rum_setbits(sc, RT2573_TXRX_CSR4,
2011 RT2573_MRR_ENABLED | RT2573_MRR_CCK_FALLBACK);
2012 } else {
2013 rum_modbits(sc, RT2573_TXRX_CSR4,
2014 RT2573_MRR_ENABLED, RT2573_MRR_CCK_FALLBACK);
2015 }
2016 }
2017
2018 static void
rum_set_txpreamble(struct rum_softc * sc)2019 rum_set_txpreamble(struct rum_softc *sc)
2020 {
2021 struct ieee80211com *ic = &sc->sc_ic;
2022
2023 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
2024 rum_setbits(sc, RT2573_TXRX_CSR4, RT2573_SHORT_PREAMBLE);
2025 else
2026 rum_clrbits(sc, RT2573_TXRX_CSR4, RT2573_SHORT_PREAMBLE);
2027 }
2028
2029 static void
rum_set_basicrates(struct rum_softc * sc)2030 rum_set_basicrates(struct rum_softc *sc)
2031 {
2032 struct ieee80211com *ic = &sc->sc_ic;
2033
2034 /* update basic rate set */
2035 if (ic->ic_curmode == IEEE80211_MODE_11B) {
2036 /* 11b basic rates: 1, 2Mbps */
2037 rum_write(sc, RT2573_TXRX_CSR5, 0x3);
2038 } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) {
2039 /* 11a basic rates: 6, 12, 24Mbps */
2040 rum_write(sc, RT2573_TXRX_CSR5, 0x150);
2041 } else {
2042 /* 11b/g basic rates: 1, 2, 5.5, 11Mbps */
2043 rum_write(sc, RT2573_TXRX_CSR5, 0xf);
2044 }
2045 }
2046
2047 /*
2048 * Reprogram MAC/BBP to switch to a new band. Values taken from the reference
2049 * driver.
2050 */
2051 static void
rum_select_band(struct rum_softc * sc,struct ieee80211_channel * c)2052 rum_select_band(struct rum_softc *sc, struct ieee80211_channel *c)
2053 {
2054 uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104;
2055
2056 /* update all BBP registers that depend on the band */
2057 bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c;
2058 bbp35 = 0x50; bbp97 = 0x48; bbp98 = 0x48;
2059 if (IEEE80211_IS_CHAN_5GHZ(c)) {
2060 bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c;
2061 bbp35 += 0x10; bbp97 += 0x10; bbp98 += 0x10;
2062 }
2063 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
2064 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
2065 bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10;
2066 }
2067
2068 sc->bbp17 = bbp17;
2069 rum_bbp_write(sc, 17, bbp17);
2070 rum_bbp_write(sc, 96, bbp96);
2071 rum_bbp_write(sc, 104, bbp104);
2072
2073 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
2074 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
2075 rum_bbp_write(sc, 75, 0x80);
2076 rum_bbp_write(sc, 86, 0x80);
2077 rum_bbp_write(sc, 88, 0x80);
2078 }
2079
2080 rum_bbp_write(sc, 35, bbp35);
2081 rum_bbp_write(sc, 97, bbp97);
2082 rum_bbp_write(sc, 98, bbp98);
2083
2084 if (IEEE80211_IS_CHAN_2GHZ(c)) {
2085 rum_modbits(sc, RT2573_PHY_CSR0, RT2573_PA_PE_2GHZ,
2086 RT2573_PA_PE_5GHZ);
2087 } else {
2088 rum_modbits(sc, RT2573_PHY_CSR0, RT2573_PA_PE_5GHZ,
2089 RT2573_PA_PE_2GHZ);
2090 }
2091 }
2092
2093 static void
rum_set_chan(struct rum_softc * sc,struct ieee80211_channel * c)2094 rum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c)
2095 {
2096 struct ieee80211com *ic = &sc->sc_ic;
2097 const struct rfprog *rfprog;
2098 uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT;
2099 int8_t power;
2100 int i, chan;
2101
2102 chan = ieee80211_chan2ieee(ic, c);
2103 if (chan == 0 || chan == IEEE80211_CHAN_ANY)
2104 return;
2105
2106 /* select the appropriate RF settings based on what EEPROM says */
2107 rfprog = (sc->rf_rev == RT2573_RF_5225 ||
2108 sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226;
2109
2110 /* find the settings for this channel (we know it exists) */
2111 for (i = 0; rfprog[i].chan != chan; i++);
2112
2113 power = sc->txpow[i];
2114 if (power < 0) {
2115 bbp94 += power;
2116 power = 0;
2117 } else if (power > 31) {
2118 bbp94 += power - 31;
2119 power = 31;
2120 }
2121
2122 /*
2123 * If we are switching from the 2GHz band to the 5GHz band or
2124 * vice-versa, BBP registers need to be reprogrammed.
2125 */
2126 if (c->ic_flags != ic->ic_curchan->ic_flags) {
2127 rum_select_band(sc, c);
2128 rum_select_antenna(sc);
2129 }
2130 ic->ic_curchan = c;
2131
2132 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
2133 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
2134 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
2135 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
2136
2137 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
2138 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
2139 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7 | 1);
2140 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
2141
2142 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
2143 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
2144 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
2145 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
2146
2147 rum_pause(sc, hz / 100);
2148
2149 /* enable smart mode for MIMO-capable RFs */
2150 bbp3 = rum_bbp_read(sc, 3);
2151
2152 bbp3 &= ~RT2573_SMART_MODE;
2153 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527)
2154 bbp3 |= RT2573_SMART_MODE;
2155
2156 rum_bbp_write(sc, 3, bbp3);
2157
2158 if (bbp94 != RT2573_BBPR94_DEFAULT)
2159 rum_bbp_write(sc, 94, bbp94);
2160
2161 /* give the chip some extra time to do the switchover */
2162 rum_pause(sc, hz / 100);
2163 }
2164
2165 static void
rum_set_maxretry(struct rum_softc * sc,struct ieee80211vap * vap)2166 rum_set_maxretry(struct rum_softc *sc, struct ieee80211vap *vap)
2167 {
2168 struct ieee80211_node *ni = vap->iv_bss;
2169 const struct ieee80211_txparam *tp = ni->ni_txparms;
2170 struct rum_vap *rvp = RUM_VAP(vap);
2171
2172 rvp->maxretry = MIN(tp->maxretry, 0xf);
2173
2174 rum_modbits(sc, RT2573_TXRX_CSR4, RT2573_SHORT_RETRY(rvp->maxretry) |
2175 RT2573_LONG_RETRY(rvp->maxretry),
2176 RT2573_SHORT_RETRY_MASK | RT2573_LONG_RETRY_MASK);
2177 }
2178
2179 /*
2180 * Enable TSF synchronization and tell h/w to start sending beacons for IBSS
2181 * and HostAP operating modes.
2182 */
2183 static int
rum_enable_tsf_sync(struct rum_softc * sc)2184 rum_enable_tsf_sync(struct rum_softc *sc)
2185 {
2186 struct ieee80211com *ic = &sc->sc_ic;
2187 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2188 uint32_t tmp;
2189 uint16_t bintval;
2190
2191 if (vap->iv_opmode != IEEE80211_M_STA) {
2192 /*
2193 * Change default 16ms TBTT adjustment to 8ms.
2194 * Must be done before enabling beacon generation.
2195 */
2196 if (rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8) != 0)
2197 return EIO;
2198 }
2199
2200 tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000;
2201
2202 /* set beacon interval (in 1/16ms unit) */
2203 bintval = vap->iv_bss->ni_intval;
2204 tmp |= bintval * 16;
2205 tmp |= RT2573_TSF_TIMER_EN | RT2573_TBTT_TIMER_EN;
2206
2207 switch (vap->iv_opmode) {
2208 case IEEE80211_M_STA:
2209 /*
2210 * Local TSF is always updated with remote TSF on beacon
2211 * reception.
2212 */
2213 tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_STA);
2214 break;
2215 case IEEE80211_M_IBSS:
2216 /*
2217 * Local TSF is updated with remote TSF on beacon reception
2218 * only if the remote TSF is greater than local TSF.
2219 */
2220 tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_IBSS);
2221 tmp |= RT2573_BCN_TX_EN;
2222 break;
2223 case IEEE80211_M_HOSTAP:
2224 /* SYNC with nobody */
2225 tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_HOSTAP);
2226 tmp |= RT2573_BCN_TX_EN;
2227 break;
2228 default:
2229 device_printf(sc->sc_dev,
2230 "Enabling TSF failed. undefined opmode %d\n",
2231 vap->iv_opmode);
2232 return EINVAL;
2233 }
2234
2235 if (rum_write(sc, RT2573_TXRX_CSR9, tmp) != 0)
2236 return EIO;
2237
2238 /* refresh current sleep time */
2239 return (rum_set_sleep_time(sc, bintval));
2240 }
2241
2242 static void
rum_enable_tsf(struct rum_softc * sc)2243 rum_enable_tsf(struct rum_softc *sc)
2244 {
2245 rum_modbits(sc, RT2573_TXRX_CSR9, RT2573_TSF_TIMER_EN |
2246 RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_DIS), 0x00ffffff);
2247 }
2248
2249 static void
rum_abort_tsf_sync(struct rum_softc * sc)2250 rum_abort_tsf_sync(struct rum_softc *sc)
2251 {
2252 rum_clrbits(sc, RT2573_TXRX_CSR9, 0x00ffffff);
2253 }
2254
2255 static void
rum_get_tsf(struct rum_softc * sc,uint64_t * buf)2256 rum_get_tsf(struct rum_softc *sc, uint64_t *buf)
2257 {
2258 rum_read_multi(sc, RT2573_TXRX_CSR12, buf, sizeof (*buf));
2259 }
2260
2261 static void
rum_update_slot_cb(struct rum_softc * sc,union sec_param * data,uint8_t rvp_id)2262 rum_update_slot_cb(struct rum_softc *sc, union sec_param *data, uint8_t rvp_id)
2263 {
2264 struct ieee80211com *ic = &sc->sc_ic;
2265 uint8_t slottime;
2266
2267 slottime = IEEE80211_GET_SLOTTIME(ic);
2268
2269 rum_modbits(sc, RT2573_MAC_CSR9, slottime, 0xff);
2270
2271 DPRINTF("setting slot time to %uus\n", slottime);
2272 }
2273
2274 static void
rum_update_slot(struct ieee80211com * ic)2275 rum_update_slot(struct ieee80211com *ic)
2276 {
2277 rum_cmd_sleepable(ic->ic_softc, NULL, 0, 0, rum_update_slot_cb);
2278 }
2279
2280 static int
rum_wme_update(struct ieee80211com * ic)2281 rum_wme_update(struct ieee80211com *ic)
2282 {
2283 struct chanAccParams chp;
2284 const struct wmeParams *chanp;
2285 struct rum_softc *sc = ic->ic_softc;
2286 int error = 0;
2287
2288 ieee80211_wme_ic_getparams(ic, &chp);
2289 chanp = chp.cap_wmeParams;
2290
2291 RUM_LOCK(sc);
2292 error = rum_write(sc, RT2573_AIFSN_CSR,
2293 chanp[WME_AC_VO].wmep_aifsn << 12 |
2294 chanp[WME_AC_VI].wmep_aifsn << 8 |
2295 chanp[WME_AC_BK].wmep_aifsn << 4 |
2296 chanp[WME_AC_BE].wmep_aifsn);
2297 if (error)
2298 goto print_err;
2299 error = rum_write(sc, RT2573_CWMIN_CSR,
2300 chanp[WME_AC_VO].wmep_logcwmin << 12 |
2301 chanp[WME_AC_VI].wmep_logcwmin << 8 |
2302 chanp[WME_AC_BK].wmep_logcwmin << 4 |
2303 chanp[WME_AC_BE].wmep_logcwmin);
2304 if (error)
2305 goto print_err;
2306 error = rum_write(sc, RT2573_CWMAX_CSR,
2307 chanp[WME_AC_VO].wmep_logcwmax << 12 |
2308 chanp[WME_AC_VI].wmep_logcwmax << 8 |
2309 chanp[WME_AC_BK].wmep_logcwmax << 4 |
2310 chanp[WME_AC_BE].wmep_logcwmax);
2311 if (error)
2312 goto print_err;
2313 error = rum_write(sc, RT2573_TXOP01_CSR,
2314 chanp[WME_AC_BK].wmep_txopLimit << 16 |
2315 chanp[WME_AC_BE].wmep_txopLimit);
2316 if (error)
2317 goto print_err;
2318 error = rum_write(sc, RT2573_TXOP23_CSR,
2319 chanp[WME_AC_VO].wmep_txopLimit << 16 |
2320 chanp[WME_AC_VI].wmep_txopLimit);
2321 if (error)
2322 goto print_err;
2323
2324 memcpy(sc->wme_params, chanp, sizeof(*chanp) * WME_NUM_AC);
2325
2326 print_err:
2327 RUM_UNLOCK(sc);
2328 if (error != 0) {
2329 device_printf(sc->sc_dev, "%s: WME update failed, error %d\n",
2330 __func__, error);
2331 }
2332
2333 return (error);
2334 }
2335
2336 static void
rum_set_bssid(struct rum_softc * sc,const uint8_t * bssid)2337 rum_set_bssid(struct rum_softc *sc, const uint8_t *bssid)
2338 {
2339
2340 rum_write(sc, RT2573_MAC_CSR4,
2341 bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24);
2342 rum_write(sc, RT2573_MAC_CSR5,
2343 bssid[4] | bssid[5] << 8 | RT2573_NUM_BSSID_MSK(1));
2344 }
2345
2346 static void
rum_set_macaddr(struct rum_softc * sc,const uint8_t * addr)2347 rum_set_macaddr(struct rum_softc *sc, const uint8_t *addr)
2348 {
2349
2350 rum_write(sc, RT2573_MAC_CSR2,
2351 addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
2352 rum_write(sc, RT2573_MAC_CSR3,
2353 addr[4] | addr[5] << 8 | 0xff << 16);
2354 }
2355
2356 static void
rum_setpromisc(struct rum_softc * sc)2357 rum_setpromisc(struct rum_softc *sc)
2358 {
2359 struct ieee80211com *ic = &sc->sc_ic;
2360
2361 if (ic->ic_promisc == 0)
2362 rum_setbits(sc, RT2573_TXRX_CSR0, RT2573_DROP_NOT_TO_ME);
2363 else
2364 rum_clrbits(sc, RT2573_TXRX_CSR0, RT2573_DROP_NOT_TO_ME);
2365
2366 DPRINTF("%s promiscuous mode\n", ic->ic_promisc > 0 ?
2367 "entering" : "leaving");
2368 }
2369
2370 static void
rum_update_promisc(struct ieee80211com * ic)2371 rum_update_promisc(struct ieee80211com *ic)
2372 {
2373 struct rum_softc *sc = ic->ic_softc;
2374
2375 RUM_LOCK(sc);
2376 if (sc->sc_running)
2377 rum_setpromisc(sc);
2378 RUM_UNLOCK(sc);
2379 }
2380
2381 static void
rum_update_mcast(struct ieee80211com * ic)2382 rum_update_mcast(struct ieee80211com *ic)
2383 {
2384 /* Ignore. */
2385 }
2386
2387 static const char *
rum_get_rf(int rev)2388 rum_get_rf(int rev)
2389 {
2390 switch (rev) {
2391 case RT2573_RF_2527: return "RT2527 (MIMO XR)";
2392 case RT2573_RF_2528: return "RT2528";
2393 case RT2573_RF_5225: return "RT5225 (MIMO XR)";
2394 case RT2573_RF_5226: return "RT5226";
2395 default: return "unknown";
2396 }
2397 }
2398
2399 static void
rum_read_eeprom(struct rum_softc * sc)2400 rum_read_eeprom(struct rum_softc *sc)
2401 {
2402 uint16_t val;
2403 #ifdef RUM_DEBUG
2404 int i;
2405 #endif
2406
2407 /* read MAC address */
2408 rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, sc->sc_ic.ic_macaddr, 6);
2409
2410 rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2);
2411 val = le16toh(val);
2412 sc->rf_rev = (val >> 11) & 0x1f;
2413 sc->hw_radio = (val >> 10) & 0x1;
2414 sc->rx_ant = (val >> 4) & 0x3;
2415 sc->tx_ant = (val >> 2) & 0x3;
2416 sc->nb_ant = val & 0x3;
2417
2418 DPRINTF("RF revision=%d\n", sc->rf_rev);
2419
2420 rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2);
2421 val = le16toh(val);
2422 sc->ext_5ghz_lna = (val >> 6) & 0x1;
2423 sc->ext_2ghz_lna = (val >> 4) & 0x1;
2424
2425 DPRINTF("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n",
2426 sc->ext_2ghz_lna, sc->ext_5ghz_lna);
2427
2428 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2);
2429 val = le16toh(val);
2430 if ((val & 0xff) != 0xff)
2431 sc->rssi_2ghz_corr = (int8_t)(val & 0xff); /* signed */
2432
2433 /* Only [-10, 10] is valid */
2434 if (sc->rssi_2ghz_corr < -10 || sc->rssi_2ghz_corr > 10)
2435 sc->rssi_2ghz_corr = 0;
2436
2437 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2);
2438 val = le16toh(val);
2439 if ((val & 0xff) != 0xff)
2440 sc->rssi_5ghz_corr = (int8_t)(val & 0xff); /* signed */
2441
2442 /* Only [-10, 10] is valid */
2443 if (sc->rssi_5ghz_corr < -10 || sc->rssi_5ghz_corr > 10)
2444 sc->rssi_5ghz_corr = 0;
2445
2446 if (sc->ext_2ghz_lna)
2447 sc->rssi_2ghz_corr -= 14;
2448 if (sc->ext_5ghz_lna)
2449 sc->rssi_5ghz_corr -= 14;
2450
2451 DPRINTF("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n",
2452 sc->rssi_2ghz_corr, sc->rssi_5ghz_corr);
2453
2454 rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2);
2455 val = le16toh(val);
2456 if ((val & 0xff) != 0xff)
2457 sc->rffreq = val & 0xff;
2458
2459 DPRINTF("RF freq=%d\n", sc->rffreq);
2460
2461 /* read Tx power for all a/b/g channels */
2462 rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14);
2463 /* XXX default Tx power for 802.11a channels */
2464 memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14);
2465 #ifdef RUM_DEBUG
2466 for (i = 0; i < 14; i++)
2467 DPRINTF("Channel=%d Tx power=%d\n", i + 1, sc->txpow[i]);
2468 #endif
2469
2470 /* read default values for BBP registers */
2471 rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
2472 #ifdef RUM_DEBUG
2473 for (i = 0; i < 14; i++) {
2474 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
2475 continue;
2476 DPRINTF("BBP R%d=%02x\n", sc->bbp_prom[i].reg,
2477 sc->bbp_prom[i].val);
2478 }
2479 #endif
2480 }
2481
2482 static int
rum_bbp_wakeup(struct rum_softc * sc)2483 rum_bbp_wakeup(struct rum_softc *sc)
2484 {
2485 unsigned ntries;
2486
2487 for (ntries = 0; ntries < 100; ntries++) {
2488 if (rum_read(sc, RT2573_MAC_CSR12) & 8)
2489 break;
2490 rum_write(sc, RT2573_MAC_CSR12, 4); /* force wakeup */
2491 if (rum_pause(sc, hz / 100))
2492 break;
2493 }
2494 if (ntries == 100) {
2495 device_printf(sc->sc_dev,
2496 "timeout waiting for BBP/RF to wakeup\n");
2497 return (ETIMEDOUT);
2498 }
2499
2500 return (0);
2501 }
2502
2503 static int
rum_bbp_init(struct rum_softc * sc)2504 rum_bbp_init(struct rum_softc *sc)
2505 {
2506 int i, ntries;
2507
2508 /* wait for BBP to be ready */
2509 for (ntries = 0; ntries < 100; ntries++) {
2510 const uint8_t val = rum_bbp_read(sc, 0);
2511 if (val != 0 && val != 0xff)
2512 break;
2513 if (rum_pause(sc, hz / 100))
2514 break;
2515 }
2516 if (ntries == 100) {
2517 device_printf(sc->sc_dev, "timeout waiting for BBP\n");
2518 return EIO;
2519 }
2520
2521 /* initialize BBP registers to default values */
2522 for (i = 0; i < nitems(rum_def_bbp); i++)
2523 rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val);
2524
2525 /* write vendor-specific BBP values (from EEPROM) */
2526 for (i = 0; i < 16; i++) {
2527 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
2528 continue;
2529 rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
2530 }
2531
2532 return 0;
2533 }
2534
2535 static void
rum_clr_shkey_regs(struct rum_softc * sc)2536 rum_clr_shkey_regs(struct rum_softc *sc)
2537 {
2538 rum_write(sc, RT2573_SEC_CSR0, 0);
2539 rum_write(sc, RT2573_SEC_CSR1, 0);
2540 rum_write(sc, RT2573_SEC_CSR5, 0);
2541 }
2542
2543 static int
rum_init(struct rum_softc * sc)2544 rum_init(struct rum_softc *sc)
2545 {
2546 struct ieee80211com *ic = &sc->sc_ic;
2547 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2548 uint32_t tmp;
2549 int i, ret;
2550
2551 RUM_LOCK(sc);
2552 if (sc->sc_running) {
2553 ret = 0;
2554 goto end;
2555 }
2556
2557 /* initialize MAC registers to default values */
2558 for (i = 0; i < nitems(rum_def_mac); i++)
2559 rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val);
2560
2561 /* reset some WME parameters to default values */
2562 sc->wme_params[0].wmep_aifsn = 2;
2563 sc->wme_params[0].wmep_logcwmin = 4;
2564 sc->wme_params[0].wmep_logcwmax = 10;
2565
2566 /* set host ready */
2567 rum_write(sc, RT2573_MAC_CSR1, RT2573_RESET_ASIC | RT2573_RESET_BBP);
2568 rum_write(sc, RT2573_MAC_CSR1, 0);
2569
2570 /* wait for BBP/RF to wakeup */
2571 if ((ret = rum_bbp_wakeup(sc)) != 0)
2572 goto end;
2573
2574 if ((ret = rum_bbp_init(sc)) != 0)
2575 goto end;
2576
2577 /* select default channel */
2578 rum_select_band(sc, ic->ic_curchan);
2579 rum_select_antenna(sc);
2580 rum_set_chan(sc, ic->ic_curchan);
2581
2582 /* clear STA registers */
2583 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
2584
2585 /* clear security registers (if required) */
2586 if (sc->sc_clr_shkeys == 0) {
2587 rum_clr_shkey_regs(sc);
2588 sc->sc_clr_shkeys = 1;
2589 }
2590
2591 rum_set_macaddr(sc, vap ? vap->iv_myaddr : ic->ic_macaddr);
2592
2593 /* initialize ASIC */
2594 rum_write(sc, RT2573_MAC_CSR1, RT2573_HOST_READY);
2595
2596 /*
2597 * Allocate Tx and Rx xfer queues.
2598 */
2599 rum_setup_tx_list(sc);
2600
2601 /* update Rx filter */
2602 tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff;
2603
2604 tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR;
2605 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2606 tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR |
2607 RT2573_DROP_ACKCTS;
2608 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2609 tmp |= RT2573_DROP_TODS;
2610 if (ic->ic_promisc == 0)
2611 tmp |= RT2573_DROP_NOT_TO_ME;
2612 }
2613 rum_write(sc, RT2573_TXRX_CSR0, tmp);
2614
2615 sc->sc_running = 1;
2616 usbd_xfer_set_stall(sc->sc_xfer[RUM_BULK_WR]);
2617 usbd_transfer_start(sc->sc_xfer[RUM_BULK_RD]);
2618
2619 end: RUM_UNLOCK(sc);
2620
2621 if (ret != 0)
2622 rum_stop(sc);
2623
2624 return ret;
2625 }
2626
2627 static void
rum_stop(struct rum_softc * sc)2628 rum_stop(struct rum_softc *sc)
2629 {
2630
2631 RUM_LOCK(sc);
2632 if (!sc->sc_running) {
2633 RUM_UNLOCK(sc);
2634 return;
2635 }
2636 sc->sc_running = 0;
2637 RUM_UNLOCK(sc);
2638
2639 /*
2640 * Drain the USB transfers, if not already drained:
2641 */
2642 usbd_transfer_drain(sc->sc_xfer[RUM_BULK_WR]);
2643 usbd_transfer_drain(sc->sc_xfer[RUM_BULK_RD]);
2644
2645 RUM_LOCK(sc);
2646 rum_unsetup_tx_list(sc);
2647
2648 /* disable Rx */
2649 rum_setbits(sc, RT2573_TXRX_CSR0, RT2573_DISABLE_RX);
2650
2651 /* reset ASIC */
2652 rum_write(sc, RT2573_MAC_CSR1, RT2573_RESET_ASIC | RT2573_RESET_BBP);
2653 rum_write(sc, RT2573_MAC_CSR1, 0);
2654 RUM_UNLOCK(sc);
2655 }
2656
2657 static void
rum_load_microcode(struct rum_softc * sc,const uint8_t * ucode,size_t size)2658 rum_load_microcode(struct rum_softc *sc, const uint8_t *ucode, size_t size)
2659 {
2660 uint16_t reg = RT2573_MCU_CODE_BASE;
2661 usb_error_t err;
2662
2663 /* copy firmware image into NIC */
2664 for (; size >= 4; reg += 4, ucode += 4, size -= 4) {
2665 err = rum_write(sc, reg, UGETDW(ucode));
2666 if (err) {
2667 /* firmware already loaded ? */
2668 device_printf(sc->sc_dev, "Firmware load "
2669 "failure! (ignored)\n");
2670 break;
2671 }
2672 }
2673
2674 err = rum_do_mcu_request(sc, RT2573_MCU_RUN);
2675 if (err != USB_ERR_NORMAL_COMPLETION) {
2676 device_printf(sc->sc_dev, "could not run firmware: %s\n",
2677 usbd_errstr(err));
2678 }
2679
2680 /* give the chip some time to boot */
2681 rum_pause(sc, hz / 8);
2682 }
2683
2684 static int
rum_set_sleep_time(struct rum_softc * sc,uint16_t bintval)2685 rum_set_sleep_time(struct rum_softc *sc, uint16_t bintval)
2686 {
2687 struct ieee80211com *ic = &sc->sc_ic;
2688 usb_error_t uerror;
2689 int exp, delay;
2690
2691 RUM_LOCK_ASSERT(sc);
2692
2693 exp = ic->ic_lintval / bintval;
2694 delay = ic->ic_lintval % bintval;
2695
2696 if (exp > RT2573_TBCN_EXP_MAX)
2697 exp = RT2573_TBCN_EXP_MAX;
2698 if (delay > RT2573_TBCN_DELAY_MAX)
2699 delay = RT2573_TBCN_DELAY_MAX;
2700
2701 uerror = rum_modbits(sc, RT2573_MAC_CSR11,
2702 RT2573_TBCN_EXP(exp) |
2703 RT2573_TBCN_DELAY(delay),
2704 RT2573_TBCN_EXP(RT2573_TBCN_EXP_MAX) |
2705 RT2573_TBCN_DELAY(RT2573_TBCN_DELAY_MAX));
2706
2707 if (uerror != USB_ERR_NORMAL_COMPLETION)
2708 return (EIO);
2709
2710 sc->sc_sleep_time = IEEE80211_TU_TO_TICKS(exp * bintval + delay);
2711
2712 return (0);
2713 }
2714
2715 static int
rum_reset(struct ieee80211vap * vap,u_long cmd)2716 rum_reset(struct ieee80211vap *vap, u_long cmd)
2717 {
2718 struct ieee80211com *ic = vap->iv_ic;
2719 struct ieee80211_node *ni;
2720 struct rum_softc *sc = ic->ic_softc;
2721 int error;
2722
2723 switch (cmd) {
2724 case IEEE80211_IOC_POWERSAVE:
2725 case IEEE80211_IOC_PROTMODE:
2726 case IEEE80211_IOC_RTSTHRESHOLD:
2727 error = 0;
2728 break;
2729 case IEEE80211_IOC_POWERSAVESLEEP:
2730 ni = ieee80211_ref_node(vap->iv_bss);
2731
2732 RUM_LOCK(sc);
2733 error = rum_set_sleep_time(sc, ni->ni_intval);
2734 if (vap->iv_state == IEEE80211_S_SLEEP) {
2735 /* Use new values for wakeup timer. */
2736 rum_clrbits(sc, RT2573_MAC_CSR11, RT2573_AUTO_WAKEUP);
2737 rum_setbits(sc, RT2573_MAC_CSR11, RT2573_AUTO_WAKEUP);
2738 }
2739 /* XXX send reassoc */
2740 RUM_UNLOCK(sc);
2741
2742 ieee80211_free_node(ni);
2743 break;
2744 default:
2745 error = ENETRESET;
2746 break;
2747 }
2748
2749 return (error);
2750 }
2751
2752 static int
rum_set_beacon(struct rum_softc * sc,struct ieee80211vap * vap)2753 rum_set_beacon(struct rum_softc *sc, struct ieee80211vap *vap)
2754 {
2755 struct ieee80211com *ic = vap->iv_ic;
2756 struct rum_vap *rvp = RUM_VAP(vap);
2757 struct mbuf *m = rvp->bcn_mbuf;
2758 const struct ieee80211_txparam *tp;
2759 struct rum_tx_desc desc;
2760
2761 RUM_LOCK_ASSERT(sc);
2762
2763 if (m == NULL)
2764 return EINVAL;
2765 if (ic->ic_bsschan == IEEE80211_CHAN_ANYC)
2766 return EINVAL;
2767
2768 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)];
2769 rum_setup_tx_desc(sc, &desc, NULL, RT2573_TX_TIMESTAMP,
2770 RT2573_TX_HWSEQ, 0, 0, m->m_pkthdr.len, tp->mgmtrate);
2771
2772 /* copy the Tx descriptor into NIC memory */
2773 if (rum_write_multi(sc, RT2573_HW_BCN_BASE(0), (uint8_t *)&desc,
2774 RT2573_TX_DESC_SIZE) != 0)
2775 return EIO;
2776
2777 /* copy beacon header and payload into NIC memory */
2778 if (rum_write_multi(sc, RT2573_HW_BCN_BASE(0) + RT2573_TX_DESC_SIZE,
2779 mtod(m, uint8_t *), m->m_pkthdr.len) != 0)
2780 return EIO;
2781
2782 return 0;
2783 }
2784
2785 static int
rum_alloc_beacon(struct rum_softc * sc,struct ieee80211vap * vap)2786 rum_alloc_beacon(struct rum_softc *sc, struct ieee80211vap *vap)
2787 {
2788 struct rum_vap *rvp = RUM_VAP(vap);
2789 struct ieee80211_node *ni = vap->iv_bss;
2790 struct mbuf *m;
2791
2792 if (ni->ni_chan == IEEE80211_CHAN_ANYC)
2793 return EINVAL;
2794
2795 m = ieee80211_beacon_alloc(ni);
2796 if (m == NULL)
2797 return ENOMEM;
2798
2799 if (rvp->bcn_mbuf != NULL)
2800 m_freem(rvp->bcn_mbuf);
2801
2802 rvp->bcn_mbuf = m;
2803
2804 return (rum_set_beacon(sc, vap));
2805 }
2806
2807 static void
rum_update_beacon_cb(struct rum_softc * sc,union sec_param * data,uint8_t rvp_id)2808 rum_update_beacon_cb(struct rum_softc *sc, union sec_param *data,
2809 uint8_t rvp_id)
2810 {
2811 struct ieee80211vap *vap = data->vap;
2812
2813 rum_set_beacon(sc, vap);
2814 }
2815
2816 static void
rum_update_beacon(struct ieee80211vap * vap,int item)2817 rum_update_beacon(struct ieee80211vap *vap, int item)
2818 {
2819 struct ieee80211com *ic = vap->iv_ic;
2820 struct rum_softc *sc = ic->ic_softc;
2821 struct rum_vap *rvp = RUM_VAP(vap);
2822 struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
2823 struct ieee80211_node *ni = vap->iv_bss;
2824 struct mbuf *m = rvp->bcn_mbuf;
2825 int mcast = 0;
2826
2827 RUM_LOCK(sc);
2828 if (m == NULL) {
2829 m = ieee80211_beacon_alloc(ni);
2830 if (m == NULL) {
2831 device_printf(sc->sc_dev,
2832 "%s: could not allocate beacon frame\n", __func__);
2833 RUM_UNLOCK(sc);
2834 return;
2835 }
2836 rvp->bcn_mbuf = m;
2837 }
2838
2839 switch (item) {
2840 case IEEE80211_BEACON_ERP:
2841 rum_update_slot(ic);
2842 break;
2843 case IEEE80211_BEACON_TIM:
2844 mcast = 1; /*TODO*/
2845 break;
2846 default:
2847 break;
2848 }
2849 RUM_UNLOCK(sc);
2850
2851 setbit(bo->bo_flags, item);
2852 ieee80211_beacon_update(ni, m, mcast);
2853
2854 rum_cmd_sleepable(sc, &vap, sizeof(vap), 0, rum_update_beacon_cb);
2855 }
2856
2857 static int
rum_common_key_set(struct rum_softc * sc,struct ieee80211_key * k,uint16_t base)2858 rum_common_key_set(struct rum_softc *sc, struct ieee80211_key *k,
2859 uint16_t base)
2860 {
2861
2862 if (rum_write_multi(sc, base, k->wk_key, k->wk_keylen))
2863 return EIO;
2864
2865 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP) {
2866 if (rum_write_multi(sc, base + IEEE80211_KEYBUF_SIZE,
2867 k->wk_txmic, 8))
2868 return EIO;
2869 if (rum_write_multi(sc, base + IEEE80211_KEYBUF_SIZE + 8,
2870 k->wk_rxmic, 8))
2871 return EIO;
2872 }
2873
2874 return 0;
2875 }
2876
2877 static void
rum_group_key_set_cb(struct rum_softc * sc,union sec_param * data,uint8_t rvp_id)2878 rum_group_key_set_cb(struct rum_softc *sc, union sec_param *data,
2879 uint8_t rvp_id)
2880 {
2881 struct ieee80211_key *k = &data->key;
2882 uint8_t mode;
2883
2884 if (sc->sc_clr_shkeys == 0) {
2885 rum_clr_shkey_regs(sc);
2886 sc->sc_clr_shkeys = 1;
2887 }
2888
2889 mode = rum_crypto_mode(sc, k->wk_cipher->ic_cipher, k->wk_keylen);
2890 if (mode == 0)
2891 goto print_err;
2892
2893 DPRINTFN(1, "setting group key %d for vap %d, mode %d "
2894 "(tx %s, rx %s)\n", k->wk_keyix, rvp_id, mode,
2895 (k->wk_flags & IEEE80211_KEY_XMIT) ? "on" : "off",
2896 (k->wk_flags & IEEE80211_KEY_RECV) ? "on" : "off");
2897
2898 /* Install the key. */
2899 if (rum_common_key_set(sc, k, RT2573_SKEY(rvp_id, k->wk_keyix)) != 0)
2900 goto print_err;
2901
2902 /* Set cipher mode. */
2903 if (rum_modbits(sc, rvp_id < 2 ? RT2573_SEC_CSR1 : RT2573_SEC_CSR5,
2904 mode << (rvp_id % 2 + k->wk_keyix) * RT2573_SKEY_MAX,
2905 RT2573_MODE_MASK << (rvp_id % 2 + k->wk_keyix) * RT2573_SKEY_MAX)
2906 != 0)
2907 goto print_err;
2908
2909 /* Mark this key as valid. */
2910 if (rum_setbits(sc, RT2573_SEC_CSR0,
2911 1 << (rvp_id * RT2573_SKEY_MAX + k->wk_keyix)) != 0)
2912 goto print_err;
2913
2914 return;
2915
2916 print_err:
2917 device_printf(sc->sc_dev, "%s: cannot set group key %d for vap %d\n",
2918 __func__, k->wk_keyix, rvp_id);
2919 }
2920
2921 static void
rum_group_key_del_cb(struct rum_softc * sc,union sec_param * data,uint8_t rvp_id)2922 rum_group_key_del_cb(struct rum_softc *sc, union sec_param *data,
2923 uint8_t rvp_id)
2924 {
2925 struct ieee80211_key *k = &data->key;
2926
2927 DPRINTF("%s: removing group key %d for vap %d\n", __func__,
2928 k->wk_keyix, rvp_id);
2929 rum_clrbits(sc,
2930 rvp_id < 2 ? RT2573_SEC_CSR1 : RT2573_SEC_CSR5,
2931 RT2573_MODE_MASK << (rvp_id % 2 + k->wk_keyix) * RT2573_SKEY_MAX);
2932 rum_clrbits(sc, RT2573_SEC_CSR0,
2933 rvp_id * RT2573_SKEY_MAX + k->wk_keyix);
2934 }
2935
2936 static void
rum_pair_key_set_cb(struct rum_softc * sc,union sec_param * data,uint8_t rvp_id)2937 rum_pair_key_set_cb(struct rum_softc *sc, union sec_param *data,
2938 uint8_t rvp_id)
2939 {
2940 struct ieee80211_key *k = &data->key;
2941 uint8_t buf[IEEE80211_ADDR_LEN + 1];
2942 uint8_t mode;
2943
2944 mode = rum_crypto_mode(sc, k->wk_cipher->ic_cipher, k->wk_keylen);
2945 if (mode == 0)
2946 goto print_err;
2947
2948 DPRINTFN(1, "setting pairwise key %d for vap %d, mode %d "
2949 "(tx %s, rx %s)\n", k->wk_keyix, rvp_id, mode,
2950 (k->wk_flags & IEEE80211_KEY_XMIT) ? "on" : "off",
2951 (k->wk_flags & IEEE80211_KEY_RECV) ? "on" : "off");
2952
2953 /* Install the key. */
2954 if (rum_common_key_set(sc, k, RT2573_PKEY(k->wk_keyix)) != 0)
2955 goto print_err;
2956
2957 IEEE80211_ADDR_COPY(buf, k->wk_macaddr);
2958 buf[IEEE80211_ADDR_LEN] = mode;
2959
2960 /* Set transmitter address and cipher mode. */
2961 if (rum_write_multi(sc, RT2573_ADDR_ENTRY(k->wk_keyix),
2962 buf, sizeof buf) != 0)
2963 goto print_err;
2964
2965 /* Enable key table lookup for this vap. */
2966 if (sc->vap_key_count[rvp_id]++ == 0)
2967 if (rum_setbits(sc, RT2573_SEC_CSR4, 1 << rvp_id) != 0)
2968 goto print_err;
2969
2970 /* Mark this key as valid. */
2971 if (rum_setbits(sc,
2972 k->wk_keyix < 32 ? RT2573_SEC_CSR2 : RT2573_SEC_CSR3,
2973 1 << (k->wk_keyix % 32)) != 0)
2974 goto print_err;
2975
2976 return;
2977
2978 print_err:
2979 device_printf(sc->sc_dev,
2980 "%s: cannot set pairwise key %d, vap %d\n", __func__, k->wk_keyix,
2981 rvp_id);
2982 }
2983
2984 static void
rum_pair_key_del_cb(struct rum_softc * sc,union sec_param * data,uint8_t rvp_id)2985 rum_pair_key_del_cb(struct rum_softc *sc, union sec_param *data,
2986 uint8_t rvp_id)
2987 {
2988 struct ieee80211_key *k = &data->key;
2989
2990 DPRINTF("%s: removing key %d\n", __func__, k->wk_keyix);
2991 rum_clrbits(sc, (k->wk_keyix < 32) ? RT2573_SEC_CSR2 : RT2573_SEC_CSR3,
2992 1 << (k->wk_keyix % 32));
2993 sc->keys_bmap &= ~(1ULL << k->wk_keyix);
2994 if (--sc->vap_key_count[rvp_id] == 0)
2995 rum_clrbits(sc, RT2573_SEC_CSR4, 1 << rvp_id);
2996 }
2997
2998 static int
rum_key_alloc(struct ieee80211vap * vap,struct ieee80211_key * k,ieee80211_keyix * keyix,ieee80211_keyix * rxkeyix)2999 rum_key_alloc(struct ieee80211vap *vap, struct ieee80211_key *k,
3000 ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
3001 {
3002 struct rum_softc *sc = vap->iv_ic->ic_softc;
3003 uint8_t i;
3004
3005 if (ieee80211_is_key_unicast(vap, k)) {
3006 if (!(k->wk_flags & IEEE80211_KEY_SWCRYPT)) {
3007 RUM_LOCK(sc);
3008 for (i = 0; i < RT2573_ADDR_MAX; i++) {
3009 if ((sc->keys_bmap & (1ULL << i)) == 0) {
3010 sc->keys_bmap |= (1ULL << i);
3011 *keyix = i;
3012 break;
3013 }
3014 }
3015 RUM_UNLOCK(sc);
3016 if (i == RT2573_ADDR_MAX) {
3017 device_printf(sc->sc_dev,
3018 "%s: no free space in the key table\n",
3019 __func__);
3020 return 0;
3021 }
3022 } else
3023 *keyix = 0;
3024 } else {
3025 *keyix = ieee80211_crypto_get_key_wepidx(vap, k);
3026 }
3027 *rxkeyix = *keyix;
3028 return 1;
3029 }
3030
3031 static int
rum_key_set(struct ieee80211vap * vap,const struct ieee80211_key * k)3032 rum_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
3033 {
3034 struct rum_softc *sc = vap->iv_ic->ic_softc;
3035 int group;
3036
3037 if (k->wk_flags & IEEE80211_KEY_SWCRYPT) {
3038 /* Not for us. */
3039 return 1;
3040 }
3041
3042 group = ieee80211_is_key_global(vap, k);
3043
3044 return !rum_cmd_sleepable(sc, k, sizeof(*k), 0,
3045 group ? rum_group_key_set_cb : rum_pair_key_set_cb);
3046 }
3047
3048 static int
rum_key_delete(struct ieee80211vap * vap,const struct ieee80211_key * k)3049 rum_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
3050 {
3051 struct rum_softc *sc = vap->iv_ic->ic_softc;
3052 int group;
3053
3054 if (k->wk_flags & IEEE80211_KEY_SWCRYPT) {
3055 /* Not for us. */
3056 return 1;
3057 }
3058
3059 group = ieee80211_is_key_global(vap, k);
3060
3061 return !rum_cmd_sleepable(sc, k, sizeof(*k), 0,
3062 group ? rum_group_key_del_cb : rum_pair_key_del_cb);
3063 }
3064
3065 static int
rum_raw_xmit(struct ieee80211_node * ni,struct mbuf * m,const struct ieee80211_bpf_params * params)3066 rum_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
3067 const struct ieee80211_bpf_params *params)
3068 {
3069 struct rum_softc *sc = ni->ni_ic->ic_softc;
3070 int ret;
3071
3072 RUM_LOCK(sc);
3073 /* prevent management frames from being sent if we're not ready */
3074 if (!sc->sc_running) {
3075 ret = ENETDOWN;
3076 goto bad;
3077 }
3078 if (sc->tx_nfree < RUM_TX_MINFREE) {
3079 ret = EIO;
3080 goto bad;
3081 }
3082
3083 if (params == NULL) {
3084 /*
3085 * Legacy path; interpret frame contents to decide
3086 * precisely how to send the frame.
3087 */
3088 if ((ret = rum_tx_mgt(sc, m, ni)) != 0)
3089 goto bad;
3090 } else {
3091 /*
3092 * Caller supplied explicit parameters to use in
3093 * sending the frame.
3094 */
3095 if ((ret = rum_tx_raw(sc, m, ni, params)) != 0)
3096 goto bad;
3097 }
3098 RUM_UNLOCK(sc);
3099
3100 return 0;
3101 bad:
3102 RUM_UNLOCK(sc);
3103 m_freem(m);
3104 return ret;
3105 }
3106
3107 static void
rum_ratectl_start(struct rum_softc * sc,struct ieee80211_node * ni)3108 rum_ratectl_start(struct rum_softc *sc, struct ieee80211_node *ni)
3109 {
3110 struct ieee80211vap *vap = ni->ni_vap;
3111 struct rum_vap *rvp = RUM_VAP(vap);
3112
3113 /* clear statistic registers (STA_CSR0 to STA_CSR5) */
3114 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
3115
3116 usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp);
3117 }
3118
3119 static void
rum_ratectl_timeout(void * arg)3120 rum_ratectl_timeout(void *arg)
3121 {
3122 struct rum_vap *rvp = arg;
3123 struct ieee80211vap *vap = &rvp->vap;
3124 struct ieee80211com *ic = vap->iv_ic;
3125
3126 ieee80211_runtask(ic, &rvp->ratectl_task);
3127 }
3128
3129 static void
rum_ratectl_task(void * arg,int pending)3130 rum_ratectl_task(void *arg, int pending)
3131 {
3132 struct rum_vap *rvp = arg;
3133 struct ieee80211vap *vap = &rvp->vap;
3134 struct rum_softc *sc = vap->iv_ic->ic_softc;
3135 struct ieee80211_ratectl_tx_stats *txs = &sc->sc_txs;
3136 int ok[3], fail;
3137
3138 RUM_LOCK(sc);
3139 /* read and clear statistic registers (STA_CSR0 to STA_CSR5) */
3140 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof(sc->sta));
3141
3142 ok[0] = (le32toh(sc->sta[4]) & 0xffff); /* TX ok w/o retry */
3143 ok[1] = (le32toh(sc->sta[4]) >> 16); /* TX ok w/ one retry */
3144 ok[2] = (le32toh(sc->sta[5]) & 0xffff); /* TX ok w/ multiple retries */
3145 fail = (le32toh(sc->sta[5]) >> 16); /* TX retry-fail count */
3146
3147 txs->flags = IEEE80211_RATECTL_TX_STATS_RETRIES;
3148 txs->nframes = ok[0] + ok[1] + ok[2] + fail;
3149 txs->nsuccess = txs->nframes - fail;
3150 /* XXX at least */
3151 txs->nretries = ok[1] + ok[2] * 2 + fail * (rvp->maxretry + 1);
3152
3153 if (txs->nframes != 0)
3154 ieee80211_ratectl_tx_update(vap, txs);
3155
3156 /* count TX retry-fail as Tx errors */
3157 if_inc_counter(vap->iv_ifp, IFCOUNTER_OERRORS, fail);
3158
3159 usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp);
3160 RUM_UNLOCK(sc);
3161 }
3162
3163 static void
rum_scan_start(struct ieee80211com * ic)3164 rum_scan_start(struct ieee80211com *ic)
3165 {
3166 struct rum_softc *sc = ic->ic_softc;
3167
3168 RUM_LOCK(sc);
3169 rum_abort_tsf_sync(sc);
3170 rum_set_bssid(sc, ieee80211broadcastaddr);
3171 RUM_UNLOCK(sc);
3172
3173 }
3174
3175 static void
rum_scan_end(struct ieee80211com * ic)3176 rum_scan_end(struct ieee80211com *ic)
3177 {
3178 struct rum_softc *sc = ic->ic_softc;
3179
3180 if (ic->ic_flags_ext & IEEE80211_FEXT_BGSCAN) {
3181 RUM_LOCK(sc);
3182 if (ic->ic_opmode != IEEE80211_M_AHDEMO)
3183 rum_enable_tsf_sync(sc);
3184 else
3185 rum_enable_tsf(sc);
3186 rum_set_bssid(sc, sc->sc_bssid);
3187 RUM_UNLOCK(sc);
3188 }
3189 }
3190
3191 static void
rum_set_channel(struct ieee80211com * ic)3192 rum_set_channel(struct ieee80211com *ic)
3193 {
3194 struct rum_softc *sc = ic->ic_softc;
3195
3196 RUM_LOCK(sc);
3197 rum_set_chan(sc, ic->ic_curchan);
3198 RUM_UNLOCK(sc);
3199 }
3200
3201 static void
rum_getradiocaps(struct ieee80211com * ic,int maxchans,int * nchans,struct ieee80211_channel chans[])3202 rum_getradiocaps(struct ieee80211com *ic,
3203 int maxchans, int *nchans, struct ieee80211_channel chans[])
3204 {
3205 struct rum_softc *sc = ic->ic_softc;
3206 uint8_t bands[IEEE80211_MODE_BYTES];
3207
3208 memset(bands, 0, sizeof(bands));
3209 setbit(bands, IEEE80211_MODE_11B);
3210 setbit(bands, IEEE80211_MODE_11G);
3211 ieee80211_add_channels_default_2ghz(chans, maxchans, nchans, bands, 0);
3212
3213 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_5226) {
3214 setbit(bands, IEEE80211_MODE_11A);
3215 ieee80211_add_channel_list_5ghz(chans, maxchans, nchans,
3216 rum_chan_5ghz, nitems(rum_chan_5ghz), bands, 0);
3217 }
3218 }
3219
3220 static int
rum_get_rssi(struct rum_softc * sc,uint8_t raw)3221 rum_get_rssi(struct rum_softc *sc, uint8_t raw)
3222 {
3223 struct ieee80211com *ic = &sc->sc_ic;
3224 int lna, agc, rssi;
3225
3226 lna = (raw >> 5) & 0x3;
3227 agc = raw & 0x1f;
3228
3229 if (lna == 0) {
3230 /*
3231 * No RSSI mapping
3232 *
3233 * NB: Since RSSI is relative to noise floor, -1 is
3234 * adequate for caller to know error happened.
3235 */
3236 return -1;
3237 }
3238
3239 rssi = (2 * agc) - RT2573_NOISE_FLOOR;
3240
3241 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) {
3242 rssi += sc->rssi_2ghz_corr;
3243
3244 if (lna == 1)
3245 rssi -= 64;
3246 else if (lna == 2)
3247 rssi -= 74;
3248 else if (lna == 3)
3249 rssi -= 90;
3250 } else {
3251 rssi += sc->rssi_5ghz_corr;
3252
3253 if (!sc->ext_5ghz_lna && lna != 1)
3254 rssi += 4;
3255
3256 if (lna == 1)
3257 rssi -= 64;
3258 else if (lna == 2)
3259 rssi -= 86;
3260 else if (lna == 3)
3261 rssi -= 100;
3262 }
3263 return rssi;
3264 }
3265
3266 static int
rum_pause(struct rum_softc * sc,int timeout)3267 rum_pause(struct rum_softc *sc, int timeout)
3268 {
3269
3270 usb_pause_mtx(&sc->sc_mtx, timeout);
3271 return (0);
3272 }
3273
3274 static device_method_t rum_methods[] = {
3275 /* Device interface */
3276 DEVMETHOD(device_probe, rum_match),
3277 DEVMETHOD(device_attach, rum_attach),
3278 DEVMETHOD(device_detach, rum_detach),
3279 DEVMETHOD_END
3280 };
3281
3282 static driver_t rum_driver = {
3283 .name = "rum",
3284 .methods = rum_methods,
3285 .size = sizeof(struct rum_softc),
3286 };
3287
3288 DRIVER_MODULE(rum, uhub, rum_driver, NULL, NULL);
3289 MODULE_DEPEND(rum, wlan, 1, 1, 1);
3290 MODULE_DEPEND(rum, usb, 1, 1, 1);
3291 MODULE_VERSION(rum, 1);
3292 USB_PNP_HOST_INFO(rum_devs);
3293