2 * Copyright (c) 2008,2010 Damien Bergamini <damien.bergamini@free.fr>
3 * ported to FreeBSD by Akinori Furukoshi <moonlightakkiy@yahoo.ca>
4 * USB Consulting, Hans Petter Selasky <hselasky@freebsd.org>
5 * Copyright (c) 2013-2014 Kevin Lo
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <sys/cdefs.h>
21 __FBSDID("$FreeBSD$");
24 * Ralink Technology RT2700U/RT2800U/RT3000U/RT3900E chipset driver.
25 * http://www.ralinktech.com/
28 #include <sys/param.h>
29 #include <sys/sockio.h>
30 #include <sys/sysctl.h>
32 #include <sys/mutex.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>
40 #include <sys/endian.h>
41 #include <sys/linker.h>
42 #include <sys/firmware.h>
45 #include <machine/bus.h>
46 #include <machine/resource.h>
51 #include <net/if_arp.h>
52 #include <net/ethernet.h>
53 #include <net/if_dl.h>
54 #include <net/if_media.h>
55 #include <net/if_types.h>
57 #include <netinet/in.h>
58 #include <netinet/in_systm.h>
59 #include <netinet/in_var.h>
60 #include <netinet/if_ether.h>
61 #include <netinet/ip.h>
63 #include <net80211/ieee80211_var.h>
64 #include <net80211/ieee80211_regdomain.h>
65 #include <net80211/ieee80211_radiotap.h>
66 #include <net80211/ieee80211_ratectl.h>
68 #include <dev/usb/usb.h>
69 #include <dev/usb/usbdi.h>
72 #define USB_DEBUG_VAR run_debug
73 #include <dev/usb/usb_debug.h>
74 #include <dev/usb/usb_msctest.h>
76 #include <dev/usb/wlan/if_runreg.h>
77 #include <dev/usb/wlan/if_runvar.h>
85 static SYSCTL_NODE(_hw_usb, OID_AUTO, run, CTLFLAG_RW, 0, "USB run");
86 SYSCTL_INT(_hw_usb_run, OID_AUTO, debug, CTLFLAG_RW, &run_debug, 0,
90 #define IEEE80211_HAS_ADDR4(wh) \
91 (((wh)->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS)
94 * Because of LOR in run_key_delete(), use atomic instead.
95 * '& RUN_CMDQ_MASQ' is to loop cmdq[].
97 #define RUN_CMDQ_GET(c) (atomic_fetchadd_32((c), 1) & RUN_CMDQ_MASQ)
99 static const STRUCT_USB_HOST_ID run_devs[] = {
100 #define RUN_DEV(v,p) { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
101 #define RUN_DEV_EJECT(v,p) \
102 { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, RUN_EJECT) }
104 RUN_DEV(ABOCOM, RT2770),
105 RUN_DEV(ABOCOM, RT2870),
106 RUN_DEV(ABOCOM, RT3070),
107 RUN_DEV(ABOCOM, RT3071),
108 RUN_DEV(ABOCOM, RT3072),
109 RUN_DEV(ABOCOM2, RT2870_1),
110 RUN_DEV(ACCTON, RT2770),
111 RUN_DEV(ACCTON, RT2870_1),
112 RUN_DEV(ACCTON, RT2870_2),
113 RUN_DEV(ACCTON, RT2870_3),
114 RUN_DEV(ACCTON, RT2870_4),
115 RUN_DEV(ACCTON, RT2870_5),
116 RUN_DEV(ACCTON, RT3070),
117 RUN_DEV(ACCTON, RT3070_1),
118 RUN_DEV(ACCTON, RT3070_2),
119 RUN_DEV(ACCTON, RT3070_3),
120 RUN_DEV(ACCTON, RT3070_4),
121 RUN_DEV(ACCTON, RT3070_5),
122 RUN_DEV(AIRTIES, RT3070),
123 RUN_DEV(ALLWIN, RT2070),
124 RUN_DEV(ALLWIN, RT2770),
125 RUN_DEV(ALLWIN, RT2870),
126 RUN_DEV(ALLWIN, RT3070),
127 RUN_DEV(ALLWIN, RT3071),
128 RUN_DEV(ALLWIN, RT3072),
129 RUN_DEV(ALLWIN, RT3572),
130 RUN_DEV(AMIGO, RT2870_1),
131 RUN_DEV(AMIGO, RT2870_2),
132 RUN_DEV(AMIT, CGWLUSB2GNR),
133 RUN_DEV(AMIT, RT2870_1),
134 RUN_DEV(AMIT2, RT2870),
135 RUN_DEV(ASUS, RT2870_1),
136 RUN_DEV(ASUS, RT2870_2),
137 RUN_DEV(ASUS, RT2870_3),
138 RUN_DEV(ASUS, RT2870_4),
139 RUN_DEV(ASUS, RT2870_5),
140 RUN_DEV(ASUS, USBN13),
141 RUN_DEV(ASUS, RT3070_1),
142 RUN_DEV(ASUS, USBN66),
143 RUN_DEV(ASUS, USB_N53),
144 RUN_DEV(ASUS2, USBN11),
145 RUN_DEV(AZUREWAVE, RT2870_1),
146 RUN_DEV(AZUREWAVE, RT2870_2),
147 RUN_DEV(AZUREWAVE, RT3070_1),
148 RUN_DEV(AZUREWAVE, RT3070_2),
149 RUN_DEV(AZUREWAVE, RT3070_3),
150 RUN_DEV(BELKIN, F9L1103),
151 RUN_DEV(BELKIN, F5D8053V3),
152 RUN_DEV(BELKIN, F5D8055),
153 RUN_DEV(BELKIN, F5D8055V2),
154 RUN_DEV(BELKIN, F6D4050V1),
155 RUN_DEV(BELKIN, F6D4050V2),
156 RUN_DEV(BELKIN, RT2870_1),
157 RUN_DEV(BELKIN, RT2870_2),
158 RUN_DEV(CISCOLINKSYS, AE1000),
159 RUN_DEV(CISCOLINKSYS2, RT3070),
160 RUN_DEV(CISCOLINKSYS3, RT3070),
161 RUN_DEV(CONCEPTRONIC2, RT2870_1),
162 RUN_DEV(CONCEPTRONIC2, RT2870_2),
163 RUN_DEV(CONCEPTRONIC2, RT2870_3),
164 RUN_DEV(CONCEPTRONIC2, RT2870_4),
165 RUN_DEV(CONCEPTRONIC2, RT2870_5),
166 RUN_DEV(CONCEPTRONIC2, RT2870_6),
167 RUN_DEV(CONCEPTRONIC2, RT2870_7),
168 RUN_DEV(CONCEPTRONIC2, RT2870_8),
169 RUN_DEV(CONCEPTRONIC2, RT3070_1),
170 RUN_DEV(CONCEPTRONIC2, RT3070_2),
171 RUN_DEV(CONCEPTRONIC2, VIGORN61),
172 RUN_DEV(COREGA, CGWLUSB300GNM),
173 RUN_DEV(COREGA, RT2870_1),
174 RUN_DEV(COREGA, RT2870_2),
175 RUN_DEV(COREGA, RT2870_3),
176 RUN_DEV(COREGA, RT3070),
177 RUN_DEV(CYBERTAN, RT2870),
178 RUN_DEV(DLINK, RT2870),
179 RUN_DEV(DLINK, RT3072),
180 RUN_DEV(DLINK, DWA127),
181 RUN_DEV(DLINK, DWA140B3),
182 RUN_DEV(DLINK, DWA160B2),
183 RUN_DEV(DLINK, DWA140D1),
184 RUN_DEV(DLINK, DWA162),
185 RUN_DEV(DLINK2, DWA130),
186 RUN_DEV(DLINK2, RT2870_1),
187 RUN_DEV(DLINK2, RT2870_2),
188 RUN_DEV(DLINK2, RT3070_1),
189 RUN_DEV(DLINK2, RT3070_2),
190 RUN_DEV(DLINK2, RT3070_3),
191 RUN_DEV(DLINK2, RT3070_4),
192 RUN_DEV(DLINK2, RT3070_5),
193 RUN_DEV(DLINK2, RT3072),
194 RUN_DEV(DLINK2, RT3072_1),
195 RUN_DEV(EDIMAX, EW7717),
196 RUN_DEV(EDIMAX, EW7718),
197 RUN_DEV(EDIMAX, EW7733UND),
198 RUN_DEV(EDIMAX, RT2870_1),
199 RUN_DEV(ENCORE, RT3070_1),
200 RUN_DEV(ENCORE, RT3070_2),
201 RUN_DEV(ENCORE, RT3070_3),
202 RUN_DEV(GIGABYTE, GNWB31N),
203 RUN_DEV(GIGABYTE, GNWB32L),
204 RUN_DEV(GIGABYTE, RT2870_1),
205 RUN_DEV(GIGASET, RT3070_1),
206 RUN_DEV(GIGASET, RT3070_2),
207 RUN_DEV(GUILLEMOT, HWNU300),
208 RUN_DEV(HAWKING, HWUN2),
209 RUN_DEV(HAWKING, RT2870_1),
210 RUN_DEV(HAWKING, RT2870_2),
211 RUN_DEV(HAWKING, RT3070),
212 RUN_DEV(IODATA, RT3072_1),
213 RUN_DEV(IODATA, RT3072_2),
214 RUN_DEV(IODATA, RT3072_3),
215 RUN_DEV(IODATA, RT3072_4),
216 RUN_DEV(LINKSYS4, RT3070),
217 RUN_DEV(LINKSYS4, WUSB100),
218 RUN_DEV(LINKSYS4, WUSB54GCV3),
219 RUN_DEV(LINKSYS4, WUSB600N),
220 RUN_DEV(LINKSYS4, WUSB600NV2),
221 RUN_DEV(LOGITEC, RT2870_1),
222 RUN_DEV(LOGITEC, RT2870_2),
223 RUN_DEV(LOGITEC, RT2870_3),
224 RUN_DEV(LOGITEC, LANW300NU2),
225 RUN_DEV(LOGITEC, LANW150NU2),
226 RUN_DEV(LOGITEC, LANW300NU2S),
227 RUN_DEV(MELCO, RT2870_1),
228 RUN_DEV(MELCO, RT2870_2),
229 RUN_DEV(MELCO, WLIUCAG300N),
230 RUN_DEV(MELCO, WLIUCG300N),
231 RUN_DEV(MELCO, WLIUCG301N),
232 RUN_DEV(MELCO, WLIUCGN),
233 RUN_DEV(MELCO, WLIUCGNM),
234 RUN_DEV(MELCO, WLIUCGNM2),
235 RUN_DEV(MOTOROLA4, RT2770),
236 RUN_DEV(MOTOROLA4, RT3070),
237 RUN_DEV(MSI, RT3070_1),
238 RUN_DEV(MSI, RT3070_2),
239 RUN_DEV(MSI, RT3070_3),
240 RUN_DEV(MSI, RT3070_4),
241 RUN_DEV(MSI, RT3070_5),
242 RUN_DEV(MSI, RT3070_6),
243 RUN_DEV(MSI, RT3070_7),
244 RUN_DEV(MSI, RT3070_8),
245 RUN_DEV(MSI, RT3070_9),
246 RUN_DEV(MSI, RT3070_10),
247 RUN_DEV(MSI, RT3070_11),
248 RUN_DEV(OVISLINK, RT3072),
249 RUN_DEV(PARA, RT3070),
250 RUN_DEV(PEGATRON, RT2870),
251 RUN_DEV(PEGATRON, RT3070),
252 RUN_DEV(PEGATRON, RT3070_2),
253 RUN_DEV(PEGATRON, RT3070_3),
254 RUN_DEV(PHILIPS, RT2870),
255 RUN_DEV(PLANEX2, GWUS300MINIS),
256 RUN_DEV(PLANEX2, GWUSMICRON),
257 RUN_DEV(PLANEX2, RT2870),
258 RUN_DEV(PLANEX2, RT3070),
259 RUN_DEV(QCOM, RT2870),
260 RUN_DEV(QUANTA, RT3070),
261 RUN_DEV(RALINK, RT2070),
262 RUN_DEV(RALINK, RT2770),
263 RUN_DEV(RALINK, RT2870),
264 RUN_DEV(RALINK, RT3070),
265 RUN_DEV(RALINK, RT3071),
266 RUN_DEV(RALINK, RT3072),
267 RUN_DEV(RALINK, RT3370),
268 RUN_DEV(RALINK, RT3572),
269 RUN_DEV(RALINK, RT3573),
270 RUN_DEV(RALINK, RT5370),
271 RUN_DEV(RALINK, RT5572),
272 RUN_DEV(RALINK, RT8070),
273 RUN_DEV(SAMSUNG, WIS09ABGN),
274 RUN_DEV(SAMSUNG2, RT2870_1),
275 RUN_DEV(SENAO, RT2870_1),
276 RUN_DEV(SENAO, RT2870_2),
277 RUN_DEV(SENAO, RT2870_3),
278 RUN_DEV(SENAO, RT2870_4),
279 RUN_DEV(SENAO, RT3070),
280 RUN_DEV(SENAO, RT3071),
281 RUN_DEV(SENAO, RT3072_1),
282 RUN_DEV(SENAO, RT3072_2),
283 RUN_DEV(SENAO, RT3072_3),
284 RUN_DEV(SENAO, RT3072_4),
285 RUN_DEV(SENAO, RT3072_5),
286 RUN_DEV(SITECOMEU, RT2770),
287 RUN_DEV(SITECOMEU, RT2870_1),
288 RUN_DEV(SITECOMEU, RT2870_2),
289 RUN_DEV(SITECOMEU, RT2870_3),
290 RUN_DEV(SITECOMEU, RT2870_4),
291 RUN_DEV(SITECOMEU, RT3070),
292 RUN_DEV(SITECOMEU, RT3070_2),
293 RUN_DEV(SITECOMEU, RT3070_3),
294 RUN_DEV(SITECOMEU, RT3070_4),
295 RUN_DEV(SITECOMEU, RT3071),
296 RUN_DEV(SITECOMEU, RT3072_1),
297 RUN_DEV(SITECOMEU, RT3072_2),
298 RUN_DEV(SITECOMEU, RT3072_3),
299 RUN_DEV(SITECOMEU, RT3072_4),
300 RUN_DEV(SITECOMEU, RT3072_5),
301 RUN_DEV(SITECOMEU, RT3072_6),
302 RUN_DEV(SITECOMEU, WL608),
303 RUN_DEV(SPARKLAN, RT2870_1),
304 RUN_DEV(SPARKLAN, RT3070),
305 RUN_DEV(SWEEX2, LW153),
306 RUN_DEV(SWEEX2, LW303),
307 RUN_DEV(SWEEX2, LW313),
308 RUN_DEV(TOSHIBA, RT3070),
309 RUN_DEV(UMEDIA, RT2870_1),
310 RUN_DEV(ZCOM, RT2870_1),
311 RUN_DEV(ZCOM, RT2870_2),
312 RUN_DEV(ZINWELL, RT2870_1),
313 RUN_DEV(ZINWELL, RT2870_2),
314 RUN_DEV(ZINWELL, RT3070),
315 RUN_DEV(ZINWELL, RT3072_1),
316 RUN_DEV(ZINWELL, RT3072_2),
317 RUN_DEV(ZYXEL, RT2870_1),
318 RUN_DEV(ZYXEL, RT2870_2),
319 RUN_DEV_EJECT(ZYXEL, NWD2705),
320 RUN_DEV_EJECT(RALINK, RT_STOR),
325 static device_probe_t run_match;
326 static device_attach_t run_attach;
327 static device_detach_t run_detach;
329 static usb_callback_t run_bulk_rx_callback;
330 static usb_callback_t run_bulk_tx_callback0;
331 static usb_callback_t run_bulk_tx_callback1;
332 static usb_callback_t run_bulk_tx_callback2;
333 static usb_callback_t run_bulk_tx_callback3;
334 static usb_callback_t run_bulk_tx_callback4;
335 static usb_callback_t run_bulk_tx_callback5;
337 static void run_autoinst(void *, struct usb_device *,
338 struct usb_attach_arg *);
339 static int run_driver_loaded(struct module *, int, void *);
340 static void run_bulk_tx_callbackN(struct usb_xfer *xfer,
341 usb_error_t error, u_int index);
342 static struct ieee80211vap *run_vap_create(struct ieee80211com *,
343 const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
344 const uint8_t [IEEE80211_ADDR_LEN],
345 const uint8_t [IEEE80211_ADDR_LEN]);
346 static void run_vap_delete(struct ieee80211vap *);
347 static void run_cmdq_cb(void *, int);
348 static void run_setup_tx_list(struct run_softc *,
349 struct run_endpoint_queue *);
350 static void run_unsetup_tx_list(struct run_softc *,
351 struct run_endpoint_queue *);
352 static int run_load_microcode(struct run_softc *);
353 static int run_reset(struct run_softc *);
354 static usb_error_t run_do_request(struct run_softc *,
355 struct usb_device_request *, void *);
356 static int run_read(struct run_softc *, uint16_t, uint32_t *);
357 static int run_read_region_1(struct run_softc *, uint16_t, uint8_t *, int);
358 static int run_write_2(struct run_softc *, uint16_t, uint16_t);
359 static int run_write(struct run_softc *, uint16_t, uint32_t);
360 static int run_write_region_1(struct run_softc *, uint16_t,
361 const uint8_t *, int);
362 static int run_set_region_4(struct run_softc *, uint16_t, uint32_t, int);
363 static int run_efuse_read(struct run_softc *, uint16_t, uint16_t *, int);
364 static int run_efuse_read_2(struct run_softc *, uint16_t, uint16_t *);
365 static int run_eeprom_read_2(struct run_softc *, uint16_t, uint16_t *);
366 static int run_rt2870_rf_write(struct run_softc *, uint32_t);
367 static int run_rt3070_rf_read(struct run_softc *, uint8_t, uint8_t *);
368 static int run_rt3070_rf_write(struct run_softc *, uint8_t, uint8_t);
369 static int run_bbp_read(struct run_softc *, uint8_t, uint8_t *);
370 static int run_bbp_write(struct run_softc *, uint8_t, uint8_t);
371 static int run_mcu_cmd(struct run_softc *, uint8_t, uint16_t);
372 static const char *run_get_rf(uint16_t);
373 static void run_rt3593_get_txpower(struct run_softc *);
374 static void run_get_txpower(struct run_softc *);
375 static int run_read_eeprom(struct run_softc *);
376 static struct ieee80211_node *run_node_alloc(struct ieee80211vap *,
377 const uint8_t mac[IEEE80211_ADDR_LEN]);
378 static int run_media_change(struct ifnet *);
379 static int run_newstate(struct ieee80211vap *, enum ieee80211_state, int);
380 static int run_wme_update(struct ieee80211com *);
381 static void run_wme_update_cb(void *);
382 static void run_key_update_begin(struct ieee80211vap *);
383 static void run_key_update_end(struct ieee80211vap *);
384 static void run_key_set_cb(void *);
385 static int run_key_set(struct ieee80211vap *, struct ieee80211_key *,
386 const uint8_t mac[IEEE80211_ADDR_LEN]);
387 static void run_key_delete_cb(void *);
388 static int run_key_delete(struct ieee80211vap *, struct ieee80211_key *);
389 static void run_ratectl_to(void *);
390 static void run_ratectl_cb(void *, int);
391 static void run_drain_fifo(void *);
392 static void run_iter_func(void *, struct ieee80211_node *);
393 static void run_newassoc_cb(void *);
394 static void run_newassoc(struct ieee80211_node *, int);
395 static void run_rx_frame(struct run_softc *, struct mbuf *, uint32_t);
396 static void run_tx_free(struct run_endpoint_queue *pq,
397 struct run_tx_data *, int);
398 static void run_set_tx_desc(struct run_softc *, struct run_tx_data *);
399 static int run_tx(struct run_softc *, struct mbuf *,
400 struct ieee80211_node *);
401 static int run_tx_mgt(struct run_softc *, struct mbuf *,
402 struct ieee80211_node *);
403 static int run_sendprot(struct run_softc *, const struct mbuf *,
404 struct ieee80211_node *, int, int);
405 static int run_tx_param(struct run_softc *, struct mbuf *,
406 struct ieee80211_node *,
407 const struct ieee80211_bpf_params *);
408 static int run_raw_xmit(struct ieee80211_node *, struct mbuf *,
409 const struct ieee80211_bpf_params *);
410 static void run_start(struct ifnet *);
411 static int run_ioctl(struct ifnet *, u_long, caddr_t);
412 static void run_iq_calib(struct run_softc *, u_int);
413 static void run_set_agc(struct run_softc *, uint8_t);
414 static void run_select_chan_group(struct run_softc *, int);
415 static void run_set_rx_antenna(struct run_softc *, int);
416 static void run_rt2870_set_chan(struct run_softc *, u_int);
417 static void run_rt3070_set_chan(struct run_softc *, u_int);
418 static void run_rt3572_set_chan(struct run_softc *, u_int);
419 static void run_rt3593_set_chan(struct run_softc *, u_int);
420 static void run_rt5390_set_chan(struct run_softc *, u_int);
421 static void run_rt5592_set_chan(struct run_softc *, u_int);
422 static int run_set_chan(struct run_softc *, struct ieee80211_channel *);
423 static void run_set_channel(struct ieee80211com *);
424 static void run_scan_start(struct ieee80211com *);
425 static void run_scan_end(struct ieee80211com *);
426 static void run_update_beacon(struct ieee80211vap *, int);
427 static void run_update_beacon_cb(void *);
428 static void run_updateprot(struct ieee80211com *);
429 static void run_updateprot_cb(void *);
430 static void run_usb_timeout_cb(void *);
431 static void run_reset_livelock(struct run_softc *);
432 static void run_enable_tsf_sync(struct run_softc *);
433 static void run_enable_mrr(struct run_softc *);
434 static void run_set_txpreamble(struct run_softc *);
435 static void run_set_basicrates(struct run_softc *);
436 static void run_set_leds(struct run_softc *, uint16_t);
437 static void run_set_bssid(struct run_softc *, const uint8_t *);
438 static void run_set_macaddr(struct run_softc *, const uint8_t *);
439 static void run_updateslot(struct ifnet *);
440 static void run_updateslot_cb(void *);
441 static void run_update_mcast(struct ifnet *);
442 static int8_t run_rssi2dbm(struct run_softc *, uint8_t, uint8_t);
443 static void run_update_promisc_locked(struct ifnet *);
444 static void run_update_promisc(struct ifnet *);
445 static void run_rt5390_bbp_init(struct run_softc *);
446 static int run_bbp_init(struct run_softc *);
447 static int run_rt3070_rf_init(struct run_softc *);
448 static void run_rt3593_rf_init(struct run_softc *);
449 static void run_rt5390_rf_init(struct run_softc *);
450 static int run_rt3070_filter_calib(struct run_softc *, uint8_t, uint8_t,
452 static void run_rt3070_rf_setup(struct run_softc *);
453 static void run_rt3593_rf_setup(struct run_softc *);
454 static void run_rt5390_rf_setup(struct run_softc *);
455 static int run_txrx_enable(struct run_softc *);
456 static void run_adjust_freq_offset(struct run_softc *);
457 static void run_init(void *);
458 static void run_init_locked(struct run_softc *);
459 static void run_stop(void *);
460 static void run_delay(struct run_softc *, u_int);
462 static eventhandler_tag run_etag;
464 static const struct rt2860_rate {
467 enum ieee80211_phytype phy;
472 { 2, 0, IEEE80211_T_DS, 0, 314, 314 },
473 { 4, 1, IEEE80211_T_DS, 1, 258, 162 },
474 { 11, 2, IEEE80211_T_DS, 2, 223, 127 },
475 { 22, 3, IEEE80211_T_DS, 3, 213, 117 },
476 { 12, 0, IEEE80211_T_OFDM, 4, 60, 60 },
477 { 18, 1, IEEE80211_T_OFDM, 4, 52, 52 },
478 { 24, 2, IEEE80211_T_OFDM, 6, 48, 48 },
479 { 36, 3, IEEE80211_T_OFDM, 6, 44, 44 },
480 { 48, 4, IEEE80211_T_OFDM, 8, 44, 44 },
481 { 72, 5, IEEE80211_T_OFDM, 8, 40, 40 },
482 { 96, 6, IEEE80211_T_OFDM, 8, 40, 40 },
483 { 108, 7, IEEE80211_T_OFDM, 8, 40, 40 }
486 static const struct {
489 } rt2870_def_mac[] = {
493 static const struct {
496 } rt2860_def_bbp[] = {
498 },rt5390_def_bbp[] = {
500 },rt5592_def_bbp[] = {
505 * Default values for BBP register R196 for RT5592.
507 static const uint8_t rt5592_bbp_r196[] = {
508 0xe0, 0x1f, 0x38, 0x32, 0x08, 0x28, 0x19, 0x0a, 0xff, 0x00,
509 0x16, 0x10, 0x10, 0x0b, 0x36, 0x2c, 0x26, 0x24, 0x42, 0x36,
510 0x30, 0x2d, 0x4c, 0x46, 0x3d, 0x40, 0x3e, 0x42, 0x3d, 0x40,
511 0x3c, 0x34, 0x2c, 0x2f, 0x3c, 0x35, 0x2e, 0x2a, 0x49, 0x41,
512 0x36, 0x31, 0x30, 0x30, 0x0e, 0x0d, 0x28, 0x21, 0x1c, 0x16,
513 0x50, 0x4a, 0x43, 0x40, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00,
514 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
515 0x00, 0x00, 0x7d, 0x14, 0x32, 0x2c, 0x36, 0x4c, 0x43, 0x2c,
516 0x2e, 0x36, 0x30, 0x6e
519 static const struct rfprog {
521 uint32_t r1, r2, r3, r4;
522 } rt2860_rf2850[] = {
532 static const struct rt5592_freqs {
535 } rt5592_freqs_20mhz[] = {
537 },rt5592_freqs_40mhz[] = {
541 static const struct {
544 } rt3070_def_rf[] = {
546 },rt3572_def_rf[] = {
548 },rt3593_def_rf[] = {
550 },rt5390_def_rf[] = {
552 },rt5392_def_rf[] = {
554 },rt5592_def_rf[] = {
556 },rt5592_2ghz_def_rf[] = {
558 },rt5592_5ghz_def_rf[] = {
562 static const struct {
567 } rt5592_chan_5ghz[] = {
571 static const struct usb_config run_config[RUN_N_XFER] = {
574 .endpoint = UE_ADDR_ANY,
576 .direction = UE_DIR_OUT,
577 .bufsize = RUN_MAX_TXSZ,
578 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
579 .callback = run_bulk_tx_callback0,
580 .timeout = 5000, /* ms */
584 .endpoint = UE_ADDR_ANY,
585 .direction = UE_DIR_OUT,
587 .bufsize = RUN_MAX_TXSZ,
588 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
589 .callback = run_bulk_tx_callback1,
590 .timeout = 5000, /* ms */
594 .endpoint = UE_ADDR_ANY,
595 .direction = UE_DIR_OUT,
597 .bufsize = RUN_MAX_TXSZ,
598 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
599 .callback = run_bulk_tx_callback2,
600 .timeout = 5000, /* ms */
604 .endpoint = UE_ADDR_ANY,
605 .direction = UE_DIR_OUT,
607 .bufsize = RUN_MAX_TXSZ,
608 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
609 .callback = run_bulk_tx_callback3,
610 .timeout = 5000, /* ms */
612 [RUN_BULK_TX_HCCA] = {
614 .endpoint = UE_ADDR_ANY,
615 .direction = UE_DIR_OUT,
617 .bufsize = RUN_MAX_TXSZ,
618 .flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
619 .callback = run_bulk_tx_callback4,
620 .timeout = 5000, /* ms */
622 [RUN_BULK_TX_PRIO] = {
624 .endpoint = UE_ADDR_ANY,
625 .direction = UE_DIR_OUT,
627 .bufsize = RUN_MAX_TXSZ,
628 .flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
629 .callback = run_bulk_tx_callback5,
630 .timeout = 5000, /* ms */
634 .endpoint = UE_ADDR_ANY,
635 .direction = UE_DIR_IN,
636 .bufsize = RUN_MAX_RXSZ,
637 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
638 .callback = run_bulk_rx_callback,
643 run_autoinst(void *arg, struct usb_device *udev,
644 struct usb_attach_arg *uaa)
646 struct usb_interface *iface;
647 struct usb_interface_descriptor *id;
649 if (uaa->dev_state != UAA_DEV_READY)
652 iface = usbd_get_iface(udev, 0);
656 if (id == NULL || id->bInterfaceClass != UICLASS_MASS)
658 if (usbd_lookup_id_by_uaa(run_devs, sizeof(run_devs), uaa))
661 if (usb_msc_eject(udev, 0, MSC_EJECT_STOPUNIT) == 0)
662 uaa->dev_state = UAA_DEV_EJECTING;
666 run_driver_loaded(struct module *mod, int what, void *arg)
670 run_etag = EVENTHANDLER_REGISTER(usb_dev_configured,
671 run_autoinst, NULL, EVENTHANDLER_PRI_ANY);
674 EVENTHANDLER_DEREGISTER(usb_dev_configured, run_etag);
683 run_match(device_t self)
685 struct usb_attach_arg *uaa = device_get_ivars(self);
687 if (uaa->usb_mode != USB_MODE_HOST)
689 if (uaa->info.bConfigIndex != 0)
691 if (uaa->info.bIfaceIndex != RT2860_IFACE_INDEX)
694 return (usbd_lookup_id_by_uaa(run_devs, sizeof(run_devs), uaa));
698 run_attach(device_t self)
700 struct run_softc *sc = device_get_softc(self);
701 struct usb_attach_arg *uaa = device_get_ivars(self);
702 struct ieee80211com *ic;
706 uint8_t iface_index, bands;
708 device_set_usb_desc(self);
709 sc->sc_udev = uaa->device;
711 if (USB_GET_DRIVER_INFO(uaa) != RUN_EJECT)
712 sc->sc_flags |= RUN_FLAG_FWLOAD_NEEDED;
714 mtx_init(&sc->sc_mtx, device_get_nameunit(sc->sc_dev),
715 MTX_NETWORK_LOCK, MTX_DEF);
717 iface_index = RT2860_IFACE_INDEX;
719 error = usbd_transfer_setup(uaa->device, &iface_index,
720 sc->sc_xfer, run_config, RUN_N_XFER, sc, &sc->sc_mtx);
722 device_printf(self, "could not allocate USB transfers, "
723 "err=%s\n", usbd_errstr(error));
729 /* wait for the chip to settle */
730 for (ntries = 0; ntries < 100; ntries++) {
731 if (run_read(sc, RT2860_ASIC_VER_ID, &ver) != 0) {
735 if (ver != 0 && ver != 0xffffffff)
740 device_printf(sc->sc_dev,
741 "timeout waiting for NIC to initialize\n");
745 sc->mac_ver = ver >> 16;
746 sc->mac_rev = ver & 0xffff;
748 /* retrieve RF rev. no and various other things from EEPROM */
751 device_printf(sc->sc_dev,
752 "MAC/BBP RT%04X (rev 0x%04X), RF %s (MIMO %dT%dR), address %s\n",
753 sc->mac_ver, sc->mac_rev, run_get_rf(sc->rf_rev),
754 sc->ntxchains, sc->nrxchains, ether_sprintf(sc->sc_bssid));
758 ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
760 device_printf(sc->sc_dev, "can not if_alloc()\n");
766 if_initname(ifp, "run", device_get_unit(sc->sc_dev));
767 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
768 ifp->if_init = run_init;
769 ifp->if_ioctl = run_ioctl;
770 ifp->if_start = run_start;
771 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
772 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
773 IFQ_SET_READY(&ifp->if_snd);
776 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
777 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
779 /* set device capabilities */
781 IEEE80211_C_STA | /* station mode supported */
782 IEEE80211_C_MONITOR | /* monitor mode supported */
785 IEEE80211_C_WDS | /* 4-address traffic works */
787 IEEE80211_C_SHPREAMBLE | /* short preamble supported */
788 IEEE80211_C_SHSLOT | /* short slot time supported */
789 IEEE80211_C_WME | /* WME */
790 IEEE80211_C_WPA; /* WPA1|WPA2(RSN) */
793 IEEE80211_CRYPTO_WEP |
794 IEEE80211_CRYPTO_AES_CCM |
795 IEEE80211_CRYPTO_TKIPMIC |
796 IEEE80211_CRYPTO_TKIP;
798 ic->ic_flags |= IEEE80211_F_DATAPAD;
799 ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS;
802 setbit(&bands, IEEE80211_MODE_11B);
803 setbit(&bands, IEEE80211_MODE_11G);
804 if (sc->rf_rev == RT2860_RF_2750 || sc->rf_rev == RT2860_RF_2850 ||
805 sc->rf_rev == RT3070_RF_3052 || sc->rf_rev == RT3593_RF_3053 ||
806 sc->rf_rev == RT5592_RF_5592)
807 setbit(&bands, IEEE80211_MODE_11A);
808 ieee80211_init_channels(ic, NULL, &bands);
810 ieee80211_ifattach(ic, sc->sc_bssid);
812 ic->ic_scan_start = run_scan_start;
813 ic->ic_scan_end = run_scan_end;
814 ic->ic_set_channel = run_set_channel;
815 ic->ic_node_alloc = run_node_alloc;
816 ic->ic_newassoc = run_newassoc;
817 ic->ic_updateslot = run_updateslot;
818 ic->ic_update_mcast = run_update_mcast;
819 ic->ic_wme.wme_update = run_wme_update;
820 ic->ic_raw_xmit = run_raw_xmit;
821 ic->ic_update_promisc = run_update_promisc;
823 ic->ic_vap_create = run_vap_create;
824 ic->ic_vap_delete = run_vap_delete;
826 ieee80211_radiotap_attach(ic,
827 &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
828 RUN_TX_RADIOTAP_PRESENT,
829 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
830 RUN_RX_RADIOTAP_PRESENT);
832 TASK_INIT(&sc->cmdq_task, 0, run_cmdq_cb, sc);
833 TASK_INIT(&sc->ratectl_task, 0, run_ratectl_cb, sc);
834 usb_callout_init_mtx(&sc->ratectl_ch, &sc->sc_mtx, 0);
837 ieee80211_announce(ic);
847 run_detach(device_t self)
849 struct run_softc *sc = device_get_softc(self);
850 struct ifnet *ifp = sc->sc_ifp;
851 struct ieee80211com *ic;
858 /* stop all USB transfers */
859 usbd_transfer_unsetup(sc->sc_xfer, RUN_N_XFER);
862 sc->ratectl_run = RUN_RATECTL_OFF;
863 sc->cmdq_run = sc->cmdq_key_set = RUN_CMDQ_ABORT;
865 /* free TX list, if any */
866 for (i = 0; i != RUN_EP_QUEUES; i++)
867 run_unsetup_tx_list(sc, &sc->sc_epq[i]);
873 usb_callout_drain(&sc->ratectl_ch);
874 ieee80211_draintask(ic, &sc->cmdq_task);
875 ieee80211_draintask(ic, &sc->ratectl_task);
876 ieee80211_ifdetach(ic);
880 mtx_destroy(&sc->sc_mtx);
885 static struct ieee80211vap *
886 run_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
887 enum ieee80211_opmode opmode, int flags,
888 const uint8_t bssid[IEEE80211_ADDR_LEN],
889 const uint8_t mac[IEEE80211_ADDR_LEN])
891 struct ifnet *ifp = ic->ic_ifp;
892 struct run_softc *sc = ifp->if_softc;
894 struct ieee80211vap *vap;
897 if (sc->rvp_cnt >= RUN_VAP_MAX) {
898 if_printf(ifp, "number of VAPs maxed out\n");
903 case IEEE80211_M_STA:
904 /* enable s/w bmiss handling for sta mode */
905 flags |= IEEE80211_CLONE_NOBEACONS;
907 case IEEE80211_M_IBSS:
908 case IEEE80211_M_MONITOR:
909 case IEEE80211_M_HOSTAP:
910 case IEEE80211_M_MBSS:
911 /* other than WDS vaps, only one at a time */
912 if (!TAILQ_EMPTY(&ic->ic_vaps))
915 case IEEE80211_M_WDS:
916 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next){
917 if(vap->iv_opmode != IEEE80211_M_HOSTAP)
919 /* WDS vap's always share the local mac address. */
920 flags &= ~IEEE80211_CLONE_BSSID;
924 if_printf(ifp, "wds only supported in ap mode\n");
929 if_printf(ifp, "unknown opmode %d\n", opmode);
933 rvp = (struct run_vap *) malloc(sizeof(struct run_vap),
934 M_80211_VAP, M_NOWAIT | M_ZERO);
939 if (ieee80211_vap_setup(ic, vap, name, unit,
940 opmode, flags, bssid, mac) != 0) {
942 free(rvp, M_80211_VAP);
946 vap->iv_key_update_begin = run_key_update_begin;
947 vap->iv_key_update_end = run_key_update_end;
948 vap->iv_update_beacon = run_update_beacon;
949 vap->iv_max_aid = RT2870_WCID_MAX;
951 * To delete the right key from h/w, we need wcid.
952 * Luckily, there is unused space in ieee80211_key{}, wk_pad,
953 * and matching wcid will be written into there. So, cast
954 * some spells to remove 'const' from ieee80211_key{}
956 vap->iv_key_delete = (void *)run_key_delete;
957 vap->iv_key_set = (void *)run_key_set;
959 /* override state transition machine */
960 rvp->newstate = vap->iv_newstate;
961 vap->iv_newstate = run_newstate;
963 ieee80211_ratectl_init(vap);
964 ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */);
967 ieee80211_vap_attach(vap, run_media_change, ieee80211_media_status);
969 /* make sure id is always unique */
970 for (i = 0; i < RUN_VAP_MAX; i++) {
971 if((sc->rvp_bmap & 1 << i) == 0){
972 sc->rvp_bmap |= 1 << i;
977 if (sc->rvp_cnt++ == 0)
978 ic->ic_opmode = opmode;
980 if (opmode == IEEE80211_M_HOSTAP)
981 sc->cmdq_run = RUN_CMDQ_GO;
983 DPRINTF("rvp_id=%d bmap=%x rvp_cnt=%d\n",
984 rvp->rvp_id, sc->rvp_bmap, sc->rvp_cnt);
990 run_vap_delete(struct ieee80211vap *vap)
992 struct run_vap *rvp = RUN_VAP(vap);
994 struct ieee80211com *ic;
995 struct run_softc *sc;
1008 m_freem(rvp->beacon_mbuf);
1009 rvp->beacon_mbuf = NULL;
1011 rvp_id = rvp->rvp_id;
1012 sc->ratectl_run &= ~(1 << rvp_id);
1013 sc->rvp_bmap &= ~(1 << rvp_id);
1014 run_set_region_4(sc, RT2860_SKEY(rvp_id, 0), 0, 128);
1015 run_set_region_4(sc, RT2860_BCN_BASE(rvp_id), 0, 512);
1018 DPRINTF("vap=%p rvp_id=%d bmap=%x rvp_cnt=%d\n",
1019 vap, rvp_id, sc->rvp_bmap, sc->rvp_cnt);
1023 ieee80211_ratectl_deinit(vap);
1024 ieee80211_vap_detach(vap);
1025 free(rvp, M_80211_VAP);
1029 * There are numbers of functions need to be called in context thread.
1030 * Rather than creating taskqueue event for each of those functions,
1031 * here is all-for-one taskqueue callback function. This function
1032 * gurantees deferred functions are executed in the same order they
1034 * '& RUN_CMDQ_MASQ' is to loop cmdq[].
1037 run_cmdq_cb(void *arg, int pending)
1039 struct run_softc *sc = arg;
1042 /* call cmdq[].func locked */
1044 for (i = sc->cmdq_exec; sc->cmdq[i].func && pending;
1045 i = sc->cmdq_exec, pending--) {
1046 DPRINTFN(6, "cmdq_exec=%d pending=%d\n", i, pending);
1047 if (sc->cmdq_run == RUN_CMDQ_GO) {
1049 * If arg0 is NULL, callback func needs more
1050 * than one arg. So, pass ptr to cmdq struct.
1052 if (sc->cmdq[i].arg0)
1053 sc->cmdq[i].func(sc->cmdq[i].arg0);
1055 sc->cmdq[i].func(&sc->cmdq[i]);
1057 sc->cmdq[i].arg0 = NULL;
1058 sc->cmdq[i].func = NULL;
1060 sc->cmdq_exec &= RUN_CMDQ_MASQ;
1066 run_setup_tx_list(struct run_softc *sc, struct run_endpoint_queue *pq)
1068 struct run_tx_data *data;
1070 memset(pq, 0, sizeof(*pq));
1072 STAILQ_INIT(&pq->tx_qh);
1073 STAILQ_INIT(&pq->tx_fh);
1075 for (data = &pq->tx_data[0];
1076 data < &pq->tx_data[RUN_TX_RING_COUNT]; data++) {
1078 STAILQ_INSERT_TAIL(&pq->tx_fh, data, next);
1080 pq->tx_nfree = RUN_TX_RING_COUNT;
1084 run_unsetup_tx_list(struct run_softc *sc, struct run_endpoint_queue *pq)
1086 struct run_tx_data *data;
1088 /* make sure any subsequent use of the queues will fail */
1090 STAILQ_INIT(&pq->tx_fh);
1091 STAILQ_INIT(&pq->tx_qh);
1093 /* free up all node references and mbufs */
1094 for (data = &pq->tx_data[0];
1095 data < &pq->tx_data[RUN_TX_RING_COUNT]; data++) {
1096 if (data->m != NULL) {
1100 if (data->ni != NULL) {
1101 ieee80211_free_node(data->ni);
1108 run_load_microcode(struct run_softc *sc)
1110 usb_device_request_t req;
1111 const struct firmware *fw;
1115 const uint64_t *temp;
1119 fw = firmware_get("runfw");
1122 device_printf(sc->sc_dev,
1123 "failed loadfirmware of file %s\n", "runfw");
1127 if (fw->datasize != 8192) {
1128 device_printf(sc->sc_dev,
1129 "invalid firmware size (should be 8KB)\n");
1135 * RT3071/RT3072 use a different firmware
1136 * run-rt2870 (8KB) contains both,
1137 * first half (4KB) is for rt2870,
1138 * last half is for rt3071.
1141 if ((sc->mac_ver) != 0x2860 &&
1142 (sc->mac_ver) != 0x2872 &&
1143 (sc->mac_ver) != 0x3070) {
1147 /* cheap sanity check */
1150 if (bytes != be64toh(0xffffff0210280210ULL)) {
1151 device_printf(sc->sc_dev, "firmware checksum failed\n");
1156 /* write microcode image */
1157 if (sc->sc_flags & RUN_FLAG_FWLOAD_NEEDED) {
1158 run_write_region_1(sc, RT2870_FW_BASE, base, 4096);
1159 run_write(sc, RT2860_H2M_MAILBOX_CID, 0xffffffff);
1160 run_write(sc, RT2860_H2M_MAILBOX_STATUS, 0xffffffff);
1163 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1164 req.bRequest = RT2870_RESET;
1165 USETW(req.wValue, 8);
1166 USETW(req.wIndex, 0);
1167 USETW(req.wLength, 0);
1168 if ((error = usbd_do_request(sc->sc_udev, &sc->sc_mtx, &req, NULL))
1170 device_printf(sc->sc_dev, "firmware reset failed\n");
1176 run_write(sc, RT2860_H2M_BBPAGENT, 0);
1177 run_write(sc, RT2860_H2M_MAILBOX, 0);
1178 run_write(sc, RT2860_H2M_INTSRC, 0);
1179 if ((error = run_mcu_cmd(sc, RT2860_MCU_CMD_RFRESET, 0)) != 0)
1182 /* wait until microcontroller is ready */
1183 for (ntries = 0; ntries < 1000; ntries++) {
1184 if ((error = run_read(sc, RT2860_SYS_CTRL, &tmp)) != 0)
1186 if (tmp & RT2860_MCU_READY)
1190 if (ntries == 1000) {
1191 device_printf(sc->sc_dev,
1192 "timeout waiting for MCU to initialize\n");
1196 device_printf(sc->sc_dev, "firmware %s ver. %u.%u loaded\n",
1197 (base == fw->data) ? "RT2870" : "RT3071",
1198 *(base + 4092), *(base + 4093));
1201 firmware_put(fw, FIRMWARE_UNLOAD);
1206 run_reset(struct run_softc *sc)
1208 usb_device_request_t req;
1210 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1211 req.bRequest = RT2870_RESET;
1212 USETW(req.wValue, 1);
1213 USETW(req.wIndex, 0);
1214 USETW(req.wLength, 0);
1215 return (usbd_do_request(sc->sc_udev, &sc->sc_mtx, &req, NULL));
1219 run_do_request(struct run_softc *sc,
1220 struct usb_device_request *req, void *data)
1225 RUN_LOCK_ASSERT(sc, MA_OWNED);
1228 err = usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
1229 req, data, 0, NULL, 250 /* ms */);
1232 DPRINTFN(1, "Control request failed, %s (retrying)\n",
1240 run_read(struct run_softc *sc, uint16_t reg, uint32_t *val)
1245 error = run_read_region_1(sc, reg, (uint8_t *)&tmp, sizeof tmp);
1247 *val = le32toh(tmp);
1254 run_read_region_1(struct run_softc *sc, uint16_t reg, uint8_t *buf, int len)
1256 usb_device_request_t req;
1258 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1259 req.bRequest = RT2870_READ_REGION_1;
1260 USETW(req.wValue, 0);
1261 USETW(req.wIndex, reg);
1262 USETW(req.wLength, len);
1264 return (run_do_request(sc, &req, buf));
1268 run_write_2(struct run_softc *sc, uint16_t reg, uint16_t val)
1270 usb_device_request_t req;
1272 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1273 req.bRequest = RT2870_WRITE_2;
1274 USETW(req.wValue, val);
1275 USETW(req.wIndex, reg);
1276 USETW(req.wLength, 0);
1278 return (run_do_request(sc, &req, NULL));
1282 run_write(struct run_softc *sc, uint16_t reg, uint32_t val)
1286 if ((error = run_write_2(sc, reg, val & 0xffff)) == 0)
1287 error = run_write_2(sc, reg + 2, val >> 16);
1292 run_write_region_1(struct run_softc *sc, uint16_t reg, const uint8_t *buf,
1298 * NB: the WRITE_REGION_1 command is not stable on RT2860.
1299 * We thus issue multiple WRITE_2 commands instead.
1301 KASSERT((len & 1) == 0, ("run_write_region_1: Data too long.\n"));
1302 for (i = 0; i < len && error == 0; i += 2)
1303 error = run_write_2(sc, reg + i, buf[i] | buf[i + 1] << 8);
1306 usb_device_request_t req;
1310 * NOTE: It appears the WRITE_REGION_1 command cannot be
1311 * passed a huge amount of data, which will crash the
1312 * firmware. Limit amount of data passed to 64-bytes at a
1320 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1321 req.bRequest = RT2870_WRITE_REGION_1;
1322 USETW(req.wValue, 0);
1323 USETW(req.wIndex, reg);
1324 USETW(req.wLength, delta);
1325 error = run_do_request(sc, &req, __DECONST(uint8_t *, buf));
1337 run_set_region_4(struct run_softc *sc, uint16_t reg, uint32_t val, int len)
1341 KASSERT((len & 3) == 0, ("run_set_region_4: Invalid data length.\n"));
1342 for (i = 0; i < len && error == 0; i += 4)
1343 error = run_write(sc, reg + i, val);
1348 run_efuse_read(struct run_softc *sc, uint16_t addr, uint16_t *val, int count)
1354 if ((error = run_read(sc, RT3070_EFUSE_CTRL, &tmp)) != 0)
1360 * Read one 16-byte block into registers EFUSE_DATA[0-3]:
1366 tmp &= ~(RT3070_EFSROM_MODE_MASK | RT3070_EFSROM_AIN_MASK);
1367 tmp |= (addr & ~0xf) << RT3070_EFSROM_AIN_SHIFT | RT3070_EFSROM_KICK;
1368 run_write(sc, RT3070_EFUSE_CTRL, tmp);
1369 for (ntries = 0; ntries < 100; ntries++) {
1370 if ((error = run_read(sc, RT3070_EFUSE_CTRL, &tmp)) != 0)
1372 if (!(tmp & RT3070_EFSROM_KICK))
1379 if ((tmp & RT3070_EFUSE_AOUT_MASK) == RT3070_EFUSE_AOUT_MASK) {
1380 *val = 0xffff; /* address not found */
1383 /* determine to which 32-bit register our 16-bit word belongs */
1384 reg = RT3070_EFUSE_DATA3 - (addr & 0xc);
1385 if ((error = run_read(sc, reg, &tmp)) != 0)
1388 tmp >>= (8 * (addr & 0x3));
1389 *val = (addr & 1) ? tmp >> 16 : tmp & 0xffff;
1394 /* Read 16-bit from eFUSE ROM for RT3xxx. */
1396 run_efuse_read_2(struct run_softc *sc, uint16_t addr, uint16_t *val)
1398 return (run_efuse_read(sc, addr, val, 2));
1402 run_eeprom_read_2(struct run_softc *sc, uint16_t addr, uint16_t *val)
1404 usb_device_request_t req;
1409 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1410 req.bRequest = RT2870_EEPROM_READ;
1411 USETW(req.wValue, 0);
1412 USETW(req.wIndex, addr);
1413 USETW(req.wLength, sizeof(tmp));
1415 error = usbd_do_request(sc->sc_udev, &sc->sc_mtx, &req, &tmp);
1417 *val = le16toh(tmp);
1424 run_srom_read(struct run_softc *sc, uint16_t addr, uint16_t *val)
1426 /* either eFUSE ROM or EEPROM */
1427 return sc->sc_srom_read(sc, addr, val);
1431 run_rt2870_rf_write(struct run_softc *sc, uint32_t val)
1436 for (ntries = 0; ntries < 10; ntries++) {
1437 if ((error = run_read(sc, RT2860_RF_CSR_CFG0, &tmp)) != 0)
1439 if (!(tmp & RT2860_RF_REG_CTRL))
1445 return (run_write(sc, RT2860_RF_CSR_CFG0, val));
1449 run_rt3070_rf_read(struct run_softc *sc, uint8_t reg, uint8_t *val)
1454 for (ntries = 0; ntries < 100; ntries++) {
1455 if ((error = run_read(sc, RT3070_RF_CSR_CFG, &tmp)) != 0)
1457 if (!(tmp & RT3070_RF_KICK))
1463 tmp = RT3070_RF_KICK | reg << 8;
1464 if ((error = run_write(sc, RT3070_RF_CSR_CFG, tmp)) != 0)
1467 for (ntries = 0; ntries < 100; ntries++) {
1468 if ((error = run_read(sc, RT3070_RF_CSR_CFG, &tmp)) != 0)
1470 if (!(tmp & RT3070_RF_KICK))
1481 run_rt3070_rf_write(struct run_softc *sc, uint8_t reg, uint8_t val)
1486 for (ntries = 0; ntries < 10; ntries++) {
1487 if ((error = run_read(sc, RT3070_RF_CSR_CFG, &tmp)) != 0)
1489 if (!(tmp & RT3070_RF_KICK))
1495 tmp = RT3070_RF_WRITE | RT3070_RF_KICK | reg << 8 | val;
1496 return (run_write(sc, RT3070_RF_CSR_CFG, tmp));
1500 run_bbp_read(struct run_softc *sc, uint8_t reg, uint8_t *val)
1505 for (ntries = 0; ntries < 10; ntries++) {
1506 if ((error = run_read(sc, RT2860_BBP_CSR_CFG, &tmp)) != 0)
1508 if (!(tmp & RT2860_BBP_CSR_KICK))
1514 tmp = RT2860_BBP_CSR_READ | RT2860_BBP_CSR_KICK | reg << 8;
1515 if ((error = run_write(sc, RT2860_BBP_CSR_CFG, tmp)) != 0)
1518 for (ntries = 0; ntries < 10; ntries++) {
1519 if ((error = run_read(sc, RT2860_BBP_CSR_CFG, &tmp)) != 0)
1521 if (!(tmp & RT2860_BBP_CSR_KICK))
1532 run_bbp_write(struct run_softc *sc, uint8_t reg, uint8_t val)
1537 for (ntries = 0; ntries < 10; ntries++) {
1538 if ((error = run_read(sc, RT2860_BBP_CSR_CFG, &tmp)) != 0)
1540 if (!(tmp & RT2860_BBP_CSR_KICK))
1546 tmp = RT2860_BBP_CSR_KICK | reg << 8 | val;
1547 return (run_write(sc, RT2860_BBP_CSR_CFG, tmp));
1551 * Send a command to the 8051 microcontroller unit.
1554 run_mcu_cmd(struct run_softc *sc, uint8_t cmd, uint16_t arg)
1559 for (ntries = 0; ntries < 100; ntries++) {
1560 if ((error = run_read(sc, RT2860_H2M_MAILBOX, &tmp)) != 0)
1562 if (!(tmp & RT2860_H2M_BUSY))
1568 tmp = RT2860_H2M_BUSY | RT2860_TOKEN_NO_INTR << 16 | arg;
1569 if ((error = run_write(sc, RT2860_H2M_MAILBOX, tmp)) == 0)
1570 error = run_write(sc, RT2860_HOST_CMD, cmd);
1575 * Add `delta' (signed) to each 4-bit sub-word of a 32-bit word.
1576 * Used to adjust per-rate Tx power registers.
1578 static __inline uint32_t
1579 b4inc(uint32_t b32, int8_t delta)
1583 for (i = 0; i < 8; i++) {
1590 b32 = b32 >> 4 | b4 << 28;
1596 run_get_rf(uint16_t rev)
1599 case RT2860_RF_2820: return "RT2820";
1600 case RT2860_RF_2850: return "RT2850";
1601 case RT2860_RF_2720: return "RT2720";
1602 case RT2860_RF_2750: return "RT2750";
1603 case RT3070_RF_3020: return "RT3020";
1604 case RT3070_RF_2020: return "RT2020";
1605 case RT3070_RF_3021: return "RT3021";
1606 case RT3070_RF_3022: return "RT3022";
1607 case RT3070_RF_3052: return "RT3052";
1608 case RT3593_RF_3053: return "RT3053";
1609 case RT5592_RF_5592: return "RT5592";
1610 case RT5390_RF_5370: return "RT5370";
1611 case RT5390_RF_5372: return "RT5372";
1617 run_rt3593_get_txpower(struct run_softc *sc)
1622 /* Read power settings for 2GHz channels. */
1623 for (i = 0; i < 14; i += 2) {
1624 addr = (sc->ntxchains == 3) ? RT3593_EEPROM_PWR2GHZ_BASE1 :
1625 RT2860_EEPROM_PWR2GHZ_BASE1;
1626 run_srom_read(sc, addr + i / 2, &val);
1627 sc->txpow1[i + 0] = (int8_t)(val & 0xff);
1628 sc->txpow1[i + 1] = (int8_t)(val >> 8);
1630 addr = (sc->ntxchains == 3) ? RT3593_EEPROM_PWR2GHZ_BASE2 :
1631 RT2860_EEPROM_PWR2GHZ_BASE2;
1632 run_srom_read(sc, addr + i / 2, &val);
1633 sc->txpow2[i + 0] = (int8_t)(val & 0xff);
1634 sc->txpow2[i + 1] = (int8_t)(val >> 8);
1636 if (sc->ntxchains == 3) {
1637 run_srom_read(sc, RT3593_EEPROM_PWR2GHZ_BASE3 + i / 2,
1639 sc->txpow3[i + 0] = (int8_t)(val & 0xff);
1640 sc->txpow3[i + 1] = (int8_t)(val >> 8);
1643 /* Fix broken Tx power entries. */
1644 for (i = 0; i < 14; i++) {
1645 if (sc->txpow1[i] > 31)
1647 if (sc->txpow2[i] > 31)
1649 if (sc->ntxchains == 3) {
1650 if (sc->txpow3[i] > 31)
1654 /* Read power settings for 5GHz channels. */
1655 for (i = 0; i < 40; i += 2) {
1656 run_srom_read(sc, RT3593_EEPROM_PWR5GHZ_BASE1 + i / 2, &val);
1657 sc->txpow1[i + 14] = (int8_t)(val & 0xff);
1658 sc->txpow1[i + 15] = (int8_t)(val >> 8);
1660 run_srom_read(sc, RT3593_EEPROM_PWR5GHZ_BASE2 + i / 2, &val);
1661 sc->txpow2[i + 14] = (int8_t)(val & 0xff);
1662 sc->txpow2[i + 15] = (int8_t)(val >> 8);
1664 if (sc->ntxchains == 3) {
1665 run_srom_read(sc, RT3593_EEPROM_PWR5GHZ_BASE3 + i / 2,
1667 sc->txpow3[i + 14] = (int8_t)(val & 0xff);
1668 sc->txpow3[i + 15] = (int8_t)(val >> 8);
1674 run_get_txpower(struct run_softc *sc)
1679 /* Read power settings for 2GHz channels. */
1680 for (i = 0; i < 14; i += 2) {
1681 run_srom_read(sc, RT2860_EEPROM_PWR2GHZ_BASE1 + i / 2, &val);
1682 sc->txpow1[i + 0] = (int8_t)(val & 0xff);
1683 sc->txpow1[i + 1] = (int8_t)(val >> 8);
1685 if (sc->mac_ver != 0x5390) {
1687 RT2860_EEPROM_PWR2GHZ_BASE2 + i / 2, &val);
1688 sc->txpow2[i + 0] = (int8_t)(val & 0xff);
1689 sc->txpow2[i + 1] = (int8_t)(val >> 8);
1692 /* Fix broken Tx power entries. */
1693 for (i = 0; i < 14; i++) {
1694 if (sc->mac_ver >= 0x5390) {
1695 if (sc->txpow1[i] < 0 || sc->txpow1[i] > 27)
1698 if (sc->txpow1[i] < 0 || sc->txpow1[i] > 31)
1701 if (sc->mac_ver > 0x5390) {
1702 if (sc->txpow2[i] < 0 || sc->txpow2[i] > 27)
1704 } else if (sc->mac_ver < 0x5390) {
1705 if (sc->txpow2[i] < 0 || sc->txpow2[i] > 31)
1708 DPRINTF("chan %d: power1=%d, power2=%d\n",
1709 rt2860_rf2850[i].chan, sc->txpow1[i], sc->txpow2[i]);
1711 /* Read power settings for 5GHz channels. */
1712 for (i = 0; i < 40; i += 2) {
1713 run_srom_read(sc, RT2860_EEPROM_PWR5GHZ_BASE1 + i / 2, &val);
1714 sc->txpow1[i + 14] = (int8_t)(val & 0xff);
1715 sc->txpow1[i + 15] = (int8_t)(val >> 8);
1717 run_srom_read(sc, RT2860_EEPROM_PWR5GHZ_BASE2 + i / 2, &val);
1718 sc->txpow2[i + 14] = (int8_t)(val & 0xff);
1719 sc->txpow2[i + 15] = (int8_t)(val >> 8);
1721 /* Fix broken Tx power entries. */
1722 for (i = 0; i < 40; i++ ) {
1723 if (sc->mac_ver != 0x5592) {
1724 if (sc->txpow1[14 + i] < -7 || sc->txpow1[14 + i] > 15)
1725 sc->txpow1[14 + i] = 5;
1726 if (sc->txpow2[14 + i] < -7 || sc->txpow2[14 + i] > 15)
1727 sc->txpow2[14 + i] = 5;
1729 DPRINTF("chan %d: power1=%d, power2=%d\n",
1730 rt2860_rf2850[14 + i].chan, sc->txpow1[14 + i],
1731 sc->txpow2[14 + i]);
1736 run_read_eeprom(struct run_softc *sc)
1738 int8_t delta_2ghz, delta_5ghz;
1743 /* check whether the ROM is eFUSE ROM or EEPROM */
1744 sc->sc_srom_read = run_eeprom_read_2;
1745 if (sc->mac_ver >= 0x3070) {
1746 run_read(sc, RT3070_EFUSE_CTRL, &tmp);
1747 DPRINTF("EFUSE_CTRL=0x%08x\n", tmp);
1748 if ((tmp & RT3070_SEL_EFUSE) || sc->mac_ver == 0x3593)
1749 sc->sc_srom_read = run_efuse_read_2;
1752 /* read ROM version */
1753 run_srom_read(sc, RT2860_EEPROM_VERSION, &val);
1754 DPRINTF("EEPROM rev=%d, FAE=%d\n", val & 0xff, val >> 8);
1756 /* read MAC address */
1757 run_srom_read(sc, RT2860_EEPROM_MAC01, &val);
1758 sc->sc_bssid[0] = val & 0xff;
1759 sc->sc_bssid[1] = val >> 8;
1760 run_srom_read(sc, RT2860_EEPROM_MAC23, &val);
1761 sc->sc_bssid[2] = val & 0xff;
1762 sc->sc_bssid[3] = val >> 8;
1763 run_srom_read(sc, RT2860_EEPROM_MAC45, &val);
1764 sc->sc_bssid[4] = val & 0xff;
1765 sc->sc_bssid[5] = val >> 8;
1767 if (sc->mac_ver < 0x3593) {
1768 /* read vender BBP settings */
1769 for (i = 0; i < 10; i++) {
1770 run_srom_read(sc, RT2860_EEPROM_BBP_BASE + i, &val);
1771 sc->bbp[i].val = val & 0xff;
1772 sc->bbp[i].reg = val >> 8;
1773 DPRINTF("BBP%d=0x%02x\n", sc->bbp[i].reg,
1776 if (sc->mac_ver >= 0x3071) {
1777 /* read vendor RF settings */
1778 for (i = 0; i < 10; i++) {
1779 run_srom_read(sc, RT3071_EEPROM_RF_BASE + i,
1781 sc->rf[i].val = val & 0xff;
1782 sc->rf[i].reg = val >> 8;
1783 DPRINTF("RF%d=0x%02x\n", sc->rf[i].reg,
1789 /* read RF frequency offset from EEPROM */
1790 run_srom_read(sc, (sc->mac_ver != 0x3593) ? RT2860_EEPROM_FREQ_LEDS :
1791 RT3593_EEPROM_FREQ, &val);
1792 sc->freq = ((val & 0xff) != 0xff) ? val & 0xff : 0;
1793 DPRINTF("EEPROM freq offset %d\n", sc->freq & 0xff);
1795 run_srom_read(sc, (sc->mac_ver != 0x3593) ? RT2860_EEPROM_FREQ_LEDS :
1796 RT3593_EEPROM_FREQ_LEDS, &val);
1797 if (val >> 8 != 0xff) {
1798 /* read LEDs operating mode */
1799 sc->leds = val >> 8;
1800 run_srom_read(sc, (sc->mac_ver != 0x3593) ? RT2860_EEPROM_LED1 :
1801 RT3593_EEPROM_LED1, &sc->led[0]);
1802 run_srom_read(sc, (sc->mac_ver != 0x3593) ? RT2860_EEPROM_LED2 :
1803 RT3593_EEPROM_LED2, &sc->led[1]);
1804 run_srom_read(sc, (sc->mac_ver != 0x3593) ? RT2860_EEPROM_LED3 :
1805 RT3593_EEPROM_LED3, &sc->led[2]);
1807 /* broken EEPROM, use default settings */
1809 sc->led[0] = 0x5555;
1810 sc->led[1] = 0x2221;
1811 sc->led[2] = 0x5627; /* differs from RT2860 */
1813 DPRINTF("EEPROM LED mode=0x%02x, LEDs=0x%04x/0x%04x/0x%04x\n",
1814 sc->leds, sc->led[0], sc->led[1], sc->led[2]);
1816 /* read RF information */
1817 if (sc->mac_ver == 0x5390 || sc->mac_ver ==0x5392)
1818 run_srom_read(sc, 0x00, &val);
1820 run_srom_read(sc, RT2860_EEPROM_ANTENNA, &val);
1822 if (val == 0xffff) {
1823 device_printf(sc->sc_dev,
1824 "invalid EEPROM antenna info, using default\n");
1825 DPRINTF("invalid EEPROM antenna info, using default\n");
1826 if (sc->mac_ver == 0x3572) {
1827 /* default to RF3052 2T2R */
1828 sc->rf_rev = RT3070_RF_3052;
1831 } else if (sc->mac_ver >= 0x3070) {
1832 /* default to RF3020 1T1R */
1833 sc->rf_rev = RT3070_RF_3020;
1837 /* default to RF2820 1T2R */
1838 sc->rf_rev = RT2860_RF_2820;
1843 if (sc->mac_ver == 0x5390 || sc->mac_ver ==0x5392) {
1845 run_srom_read(sc, RT2860_EEPROM_ANTENNA, &val);
1847 sc->rf_rev = (val >> 8) & 0xf;
1848 sc->ntxchains = (val >> 4) & 0xf;
1849 sc->nrxchains = val & 0xf;
1851 DPRINTF("EEPROM RF rev=0x%04x chains=%dT%dR\n",
1852 sc->rf_rev, sc->ntxchains, sc->nrxchains);
1854 /* check if RF supports automatic Tx access gain control */
1855 run_srom_read(sc, RT2860_EEPROM_CONFIG, &val);
1856 DPRINTF("EEPROM CFG 0x%04x\n", val);
1857 /* check if driver should patch the DAC issue */
1858 if ((val >> 8) != 0xff)
1859 sc->patch_dac = (val >> 15) & 1;
1860 if ((val & 0xff) != 0xff) {
1861 sc->ext_5ghz_lna = (val >> 3) & 1;
1862 sc->ext_2ghz_lna = (val >> 2) & 1;
1863 /* check if RF supports automatic Tx access gain control */
1864 sc->calib_2ghz = sc->calib_5ghz = (val >> 1) & 1;
1865 /* check if we have a hardware radio switch */
1866 sc->rfswitch = val & 1;
1869 /* Read Tx power settings. */
1870 if (sc->mac_ver == 0x3593)
1871 run_rt3593_get_txpower(sc);
1873 run_get_txpower(sc);
1875 /* read Tx power compensation for each Tx rate */
1876 run_srom_read(sc, RT2860_EEPROM_DELTAPWR, &val);
1877 delta_2ghz = delta_5ghz = 0;
1878 if ((val & 0xff) != 0xff && (val & 0x80)) {
1879 delta_2ghz = val & 0xf;
1880 if (!(val & 0x40)) /* negative number */
1881 delta_2ghz = -delta_2ghz;
1884 if ((val & 0xff) != 0xff && (val & 0x80)) {
1885 delta_5ghz = val & 0xf;
1886 if (!(val & 0x40)) /* negative number */
1887 delta_5ghz = -delta_5ghz;
1889 DPRINTF("power compensation=%d (2GHz), %d (5GHz)\n",
1890 delta_2ghz, delta_5ghz);
1892 for (ridx = 0; ridx < 5; ridx++) {
1895 run_srom_read(sc, RT2860_EEPROM_RPWR + ridx * 2, &val);
1897 run_srom_read(sc, RT2860_EEPROM_RPWR + ridx * 2 + 1, &val);
1898 reg |= (uint32_t)val << 16;
1900 sc->txpow20mhz[ridx] = reg;
1901 sc->txpow40mhz_2ghz[ridx] = b4inc(reg, delta_2ghz);
1902 sc->txpow40mhz_5ghz[ridx] = b4inc(reg, delta_5ghz);
1904 DPRINTF("ridx %d: power 20MHz=0x%08x, 40MHz/2GHz=0x%08x, "
1905 "40MHz/5GHz=0x%08x\n", ridx, sc->txpow20mhz[ridx],
1906 sc->txpow40mhz_2ghz[ridx], sc->txpow40mhz_5ghz[ridx]);
1909 /* Read RSSI offsets and LNA gains from EEPROM. */
1910 run_srom_read(sc, (sc->mac_ver != 0x3593) ? RT2860_EEPROM_RSSI1_2GHZ :
1911 RT3593_EEPROM_RSSI1_2GHZ, &val);
1912 sc->rssi_2ghz[0] = val & 0xff; /* Ant A */
1913 sc->rssi_2ghz[1] = val >> 8; /* Ant B */
1914 run_srom_read(sc, (sc->mac_ver != 0x3593) ? RT2860_EEPROM_RSSI2_2GHZ :
1915 RT3593_EEPROM_RSSI2_2GHZ, &val);
1916 if (sc->mac_ver >= 0x3070) {
1917 if (sc->mac_ver == 0x3593) {
1918 sc->txmixgain_2ghz = 0;
1919 sc->rssi_2ghz[2] = val & 0xff; /* Ant C */
1922 * On RT3070 chips (limited to 2 Rx chains), this ROM
1923 * field contains the Tx mixer gain for the 2GHz band.
1925 if ((val & 0xff) != 0xff)
1926 sc->txmixgain_2ghz = val & 0x7;
1928 DPRINTF("tx mixer gain=%u (2GHz)\n", sc->txmixgain_2ghz);
1930 sc->rssi_2ghz[2] = val & 0xff; /* Ant C */
1931 if (sc->mac_ver == 0x3593)
1932 run_srom_read(sc, RT3593_EEPROM_LNA_5GHZ, &val);
1933 sc->lna[2] = val >> 8; /* channel group 2 */
1935 run_srom_read(sc, (sc->mac_ver != 0x3593) ? RT2860_EEPROM_RSSI1_5GHZ :
1936 RT3593_EEPROM_RSSI1_5GHZ, &val);
1937 sc->rssi_5ghz[0] = val & 0xff; /* Ant A */
1938 sc->rssi_5ghz[1] = val >> 8; /* Ant B */
1939 run_srom_read(sc, (sc->mac_ver != 0x3593) ? RT2860_EEPROM_RSSI2_5GHZ :
1940 RT3593_EEPROM_RSSI2_5GHZ, &val);
1941 if (sc->mac_ver == 0x3572) {
1943 * On RT3572 chips (limited to 2 Rx chains), this ROM
1944 * field contains the Tx mixer gain for the 5GHz band.
1946 if ((val & 0xff) != 0xff)
1947 sc->txmixgain_5ghz = val & 0x7;
1948 DPRINTF("tx mixer gain=%u (5GHz)\n", sc->txmixgain_5ghz);
1950 sc->rssi_5ghz[2] = val & 0xff; /* Ant C */
1951 if (sc->mac_ver == 0x3593) {
1952 sc->txmixgain_5ghz = 0;
1953 run_srom_read(sc, RT3593_EEPROM_LNA_5GHZ, &val);
1955 sc->lna[3] = val >> 8; /* channel group 3 */
1957 run_srom_read(sc, (sc->mac_ver != 0x3593) ? RT2860_EEPROM_LNA :
1958 RT3593_EEPROM_LNA, &val);
1959 sc->lna[0] = val & 0xff; /* channel group 0 */
1960 sc->lna[1] = val >> 8; /* channel group 1 */
1962 /* fix broken 5GHz LNA entries */
1963 if (sc->lna[2] == 0 || sc->lna[2] == 0xff) {
1964 DPRINTF("invalid LNA for channel group %d\n", 2);
1965 sc->lna[2] = sc->lna[1];
1967 if (sc->lna[3] == 0 || sc->lna[3] == 0xff) {
1968 DPRINTF("invalid LNA for channel group %d\n", 3);
1969 sc->lna[3] = sc->lna[1];
1972 /* fix broken RSSI offset entries */
1973 for (ant = 0; ant < 3; ant++) {
1974 if (sc->rssi_2ghz[ant] < -10 || sc->rssi_2ghz[ant] > 10) {
1975 DPRINTF("invalid RSSI%d offset: %d (2GHz)\n",
1976 ant + 1, sc->rssi_2ghz[ant]);
1977 sc->rssi_2ghz[ant] = 0;
1979 if (sc->rssi_5ghz[ant] < -10 || sc->rssi_5ghz[ant] > 10) {
1980 DPRINTF("invalid RSSI%d offset: %d (5GHz)\n",
1981 ant + 1, sc->rssi_5ghz[ant]);
1982 sc->rssi_5ghz[ant] = 0;
1988 static struct ieee80211_node *
1989 run_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
1991 return malloc(sizeof (struct run_node), M_DEVBUF, M_NOWAIT | M_ZERO);
1995 run_media_change(struct ifnet *ifp)
1997 struct ieee80211vap *vap = ifp->if_softc;
1998 struct ieee80211com *ic = vap->iv_ic;
1999 const struct ieee80211_txparam *tp;
2000 struct run_softc *sc = ic->ic_ifp->if_softc;
2006 error = ieee80211_media_change(ifp);
2007 if (error != ENETRESET) {
2012 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
2013 if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) {
2014 struct ieee80211_node *ni;
2015 struct run_node *rn;
2017 rate = ic->ic_sup_rates[ic->ic_curmode].
2018 rs_rates[tp->ucastrate] & IEEE80211_RATE_VAL;
2019 for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
2020 if (rt2860_rates[ridx].rate == rate)
2022 ni = ieee80211_ref_node(vap->iv_bss);
2023 rn = (struct run_node *)ni;
2024 rn->fix_ridx = ridx;
2025 DPRINTF("rate=%d, fix_ridx=%d\n", rate, rn->fix_ridx);
2026 ieee80211_free_node(ni);
2030 if ((ifp->if_flags & IFF_UP) &&
2031 (ifp->if_drv_flags & IFF_DRV_RUNNING)){
2032 run_init_locked(sc);
2042 run_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2044 const struct ieee80211_txparam *tp;
2045 struct ieee80211com *ic = vap->iv_ic;
2046 struct run_softc *sc = ic->ic_ifp->if_softc;
2047 struct run_vap *rvp = RUN_VAP(vap);
2048 enum ieee80211_state ostate;
2052 uint8_t restart_ratectl = 0;
2053 uint8_t bid = 1 << rvp->rvp_id;
2055 ostate = vap->iv_state;
2056 DPRINTF("%s -> %s\n",
2057 ieee80211_state_name[ostate],
2058 ieee80211_state_name[nstate]);
2060 IEEE80211_UNLOCK(ic);
2063 ratectl = sc->ratectl_run; /* remember current state */
2064 sc->ratectl_run = RUN_RATECTL_OFF;
2065 usb_callout_stop(&sc->ratectl_ch);
2067 if (ostate == IEEE80211_S_RUN) {
2068 /* turn link LED off */
2069 run_set_leds(sc, RT2860_LED_RADIO);
2073 case IEEE80211_S_INIT:
2074 restart_ratectl = 1;
2076 if (ostate != IEEE80211_S_RUN)
2080 sc->runbmap &= ~bid;
2082 /* abort TSF synchronization if there is no vap running */
2083 if (--sc->running == 0) {
2084 run_read(sc, RT2860_BCN_TIME_CFG, &tmp);
2085 run_write(sc, RT2860_BCN_TIME_CFG,
2086 tmp & ~(RT2860_BCN_TX_EN | RT2860_TSF_TIMER_EN |
2087 RT2860_TBTT_TIMER_EN));
2091 case IEEE80211_S_RUN:
2092 if (!(sc->runbmap & bid)) {
2094 restart_ratectl = 1;
2098 m_freem(rvp->beacon_mbuf);
2099 rvp->beacon_mbuf = NULL;
2101 switch (vap->iv_opmode) {
2102 case IEEE80211_M_HOSTAP:
2103 case IEEE80211_M_MBSS:
2104 sc->ap_running |= bid;
2105 ic->ic_opmode = vap->iv_opmode;
2106 run_update_beacon_cb(vap);
2108 case IEEE80211_M_IBSS:
2109 sc->adhoc_running |= bid;
2110 if (!sc->ap_running)
2111 ic->ic_opmode = vap->iv_opmode;
2112 run_update_beacon_cb(vap);
2114 case IEEE80211_M_STA:
2115 sc->sta_running |= bid;
2116 if (!sc->ap_running && !sc->adhoc_running)
2117 ic->ic_opmode = vap->iv_opmode;
2119 /* read statistic counters (clear on read) */
2120 run_read_region_1(sc, RT2860_TX_STA_CNT0,
2121 (uint8_t *)sta, sizeof sta);
2125 ic->ic_opmode = vap->iv_opmode;
2129 if (vap->iv_opmode != IEEE80211_M_MONITOR) {
2130 struct ieee80211_node *ni;
2132 if (ic->ic_bsschan == IEEE80211_CHAN_ANYC) {
2137 run_updateslot(ic->ic_ifp);
2139 run_set_txpreamble(sc);
2140 run_set_basicrates(sc);
2141 ni = ieee80211_ref_node(vap->iv_bss);
2142 IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid);
2143 run_set_bssid(sc, ni->ni_bssid);
2144 ieee80211_free_node(ni);
2145 run_enable_tsf_sync(sc);
2147 /* enable automatic rate adaptation */
2148 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
2149 if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
2153 /* turn link LED on */
2154 run_set_leds(sc, RT2860_LED_RADIO |
2155 (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan) ?
2156 RT2860_LED_LINK_2GHZ : RT2860_LED_LINK_5GHZ));
2160 DPRINTFN(6, "undefined case\n");
2164 /* restart amrr for running VAPs */
2165 if ((sc->ratectl_run = ratectl) && restart_ratectl)
2166 usb_callout_reset(&sc->ratectl_ch, hz, run_ratectl_to, sc);
2171 return(rvp->newstate(vap, nstate, arg));
2176 run_wme_update_cb(void *arg)
2178 struct ieee80211com *ic = arg;
2179 struct run_softc *sc = ic->ic_ifp->if_softc;
2180 struct ieee80211_wme_state *wmesp = &ic->ic_wme;
2183 RUN_LOCK_ASSERT(sc, MA_OWNED);
2185 /* update MAC TX configuration registers */
2186 for (aci = 0; aci < WME_NUM_AC; aci++) {
2187 error = run_write(sc, RT2860_EDCA_AC_CFG(aci),
2188 wmesp->wme_params[aci].wmep_logcwmax << 16 |
2189 wmesp->wme_params[aci].wmep_logcwmin << 12 |
2190 wmesp->wme_params[aci].wmep_aifsn << 8 |
2191 wmesp->wme_params[aci].wmep_txopLimit);
2192 if (error) goto err;
2195 /* update SCH/DMA registers too */
2196 error = run_write(sc, RT2860_WMM_AIFSN_CFG,
2197 wmesp->wme_params[WME_AC_VO].wmep_aifsn << 12 |
2198 wmesp->wme_params[WME_AC_VI].wmep_aifsn << 8 |
2199 wmesp->wme_params[WME_AC_BK].wmep_aifsn << 4 |
2200 wmesp->wme_params[WME_AC_BE].wmep_aifsn);
2201 if (error) goto err;
2202 error = run_write(sc, RT2860_WMM_CWMIN_CFG,
2203 wmesp->wme_params[WME_AC_VO].wmep_logcwmin << 12 |
2204 wmesp->wme_params[WME_AC_VI].wmep_logcwmin << 8 |
2205 wmesp->wme_params[WME_AC_BK].wmep_logcwmin << 4 |
2206 wmesp->wme_params[WME_AC_BE].wmep_logcwmin);
2207 if (error) goto err;
2208 error = run_write(sc, RT2860_WMM_CWMAX_CFG,
2209 wmesp->wme_params[WME_AC_VO].wmep_logcwmax << 12 |
2210 wmesp->wme_params[WME_AC_VI].wmep_logcwmax << 8 |
2211 wmesp->wme_params[WME_AC_BK].wmep_logcwmax << 4 |
2212 wmesp->wme_params[WME_AC_BE].wmep_logcwmax);
2213 if (error) goto err;
2214 error = run_write(sc, RT2860_WMM_TXOP0_CFG,
2215 wmesp->wme_params[WME_AC_BK].wmep_txopLimit << 16 |
2216 wmesp->wme_params[WME_AC_BE].wmep_txopLimit);
2217 if (error) goto err;
2218 error = run_write(sc, RT2860_WMM_TXOP1_CFG,
2219 wmesp->wme_params[WME_AC_VO].wmep_txopLimit << 16 |
2220 wmesp->wme_params[WME_AC_VI].wmep_txopLimit);
2224 DPRINTF("WME update failed\n");
2230 run_wme_update(struct ieee80211com *ic)
2232 struct run_softc *sc = ic->ic_ifp->if_softc;
2234 /* sometime called wothout lock */
2235 if (mtx_owned(&ic->ic_comlock.mtx)) {
2236 uint32_t i = RUN_CMDQ_GET(&sc->cmdq_store);
2237 DPRINTF("cmdq_store=%d\n", i);
2238 sc->cmdq[i].func = run_wme_update_cb;
2239 sc->cmdq[i].arg0 = ic;
2240 ieee80211_runtask(ic, &sc->cmdq_task);
2245 run_wme_update_cb(ic);
2248 /* return whatever, upper layer desn't care anyway */
2253 run_key_update_begin(struct ieee80211vap *vap)
2256 * To avoid out-of-order events, both run_key_set() and
2257 * _delete() are deferred and handled by run_cmdq_cb().
2258 * So, there is nothing we need to do here.
2263 run_key_update_end(struct ieee80211vap *vap)
2269 run_key_set_cb(void *arg)
2271 struct run_cmdq *cmdq = arg;
2272 struct ieee80211vap *vap = cmdq->arg1;
2273 struct ieee80211_key *k = cmdq->k;
2274 struct ieee80211com *ic = vap->iv_ic;
2275 struct run_softc *sc = ic->ic_ifp->if_softc;
2276 struct ieee80211_node *ni;
2278 uint16_t base, associd;
2279 uint8_t mode, wcid, iv[8];
2281 RUN_LOCK_ASSERT(sc, MA_OWNED);
2283 if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2284 ni = ieee80211_find_vap_node(&ic->ic_sta, vap, cmdq->mac);
2287 associd = (ni != NULL) ? ni->ni_associd : 0;
2289 /* map net80211 cipher to RT2860 security mode */
2290 switch (k->wk_cipher->ic_cipher) {
2291 case IEEE80211_CIPHER_WEP:
2292 if(k->wk_keylen < 8)
2293 mode = RT2860_MODE_WEP40;
2295 mode = RT2860_MODE_WEP104;
2297 case IEEE80211_CIPHER_TKIP:
2298 mode = RT2860_MODE_TKIP;
2300 case IEEE80211_CIPHER_AES_CCM:
2301 mode = RT2860_MODE_AES_CCMP;
2304 DPRINTF("undefined case\n");
2308 DPRINTFN(1, "associd=%x, keyix=%d, mode=%x, type=%s, tx=%s, rx=%s\n",
2309 associd, k->wk_keyix, mode,
2310 (k->wk_flags & IEEE80211_KEY_GROUP) ? "group" : "pairwise",
2311 (k->wk_flags & IEEE80211_KEY_XMIT) ? "on" : "off",
2312 (k->wk_flags & IEEE80211_KEY_RECV) ? "on" : "off");
2314 if (k->wk_flags & IEEE80211_KEY_GROUP) {
2315 wcid = 0; /* NB: update WCID0 for group keys */
2316 base = RT2860_SKEY(RUN_VAP(vap)->rvp_id, k->wk_keyix);
2318 wcid = (vap->iv_opmode == IEEE80211_M_STA) ?
2319 1 : RUN_AID2WCID(associd);
2320 base = RT2860_PKEY(wcid);
2323 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP) {
2324 if(run_write_region_1(sc, base, k->wk_key, 16))
2326 if(run_write_region_1(sc, base + 16, &k->wk_key[16], 8)) /* wk_txmic */
2328 if(run_write_region_1(sc, base + 24, &k->wk_key[24], 8)) /* wk_rxmic */
2331 /* roundup len to 16-bit: XXX fix write_region_1() instead */
2332 if(run_write_region_1(sc, base, k->wk_key, (k->wk_keylen + 1) & ~1))
2336 if (!(k->wk_flags & IEEE80211_KEY_GROUP) ||
2337 (k->wk_flags & (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV))) {
2338 /* set initial packet number in IV+EIV */
2339 if (k->wk_cipher == IEEE80211_CIPHER_WEP) {
2340 memset(iv, 0, sizeof iv);
2341 iv[3] = vap->iv_def_txkey << 6;
2343 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP) {
2344 iv[0] = k->wk_keytsc >> 8;
2345 iv[1] = (iv[0] | 0x20) & 0x7f;
2346 iv[2] = k->wk_keytsc;
2348 iv[0] = k->wk_keytsc;
2349 iv[1] = k->wk_keytsc >> 8;
2352 iv[3] = k->wk_keyix << 6 | IEEE80211_WEP_EXTIV;
2353 iv[4] = k->wk_keytsc >> 16;
2354 iv[5] = k->wk_keytsc >> 24;
2355 iv[6] = k->wk_keytsc >> 32;
2356 iv[7] = k->wk_keytsc >> 40;
2358 if (run_write_region_1(sc, RT2860_IVEIV(wcid), iv, 8))
2362 if (k->wk_flags & IEEE80211_KEY_GROUP) {
2363 /* install group key */
2364 if (run_read(sc, RT2860_SKEY_MODE_0_7, &attr))
2366 attr &= ~(0xf << (k->wk_keyix * 4));
2367 attr |= mode << (k->wk_keyix * 4);
2368 if (run_write(sc, RT2860_SKEY_MODE_0_7, attr))
2371 /* install pairwise key */
2372 if (run_read(sc, RT2860_WCID_ATTR(wcid), &attr))
2374 attr = (attr & ~0xf) | (mode << 1) | RT2860_RX_PKEY_EN;
2375 if (run_write(sc, RT2860_WCID_ATTR(wcid), attr))
2379 /* TODO create a pass-thru key entry? */
2381 /* need wcid to delete the right key later */
2386 * Don't have to be deferred, but in order to keep order of
2387 * execution, i.e. with run_key_delete(), defer this and let
2388 * run_cmdq_cb() maintain the order.
2393 run_key_set(struct ieee80211vap *vap, struct ieee80211_key *k,
2394 const uint8_t mac[IEEE80211_ADDR_LEN])
2396 struct ieee80211com *ic = vap->iv_ic;
2397 struct run_softc *sc = ic->ic_ifp->if_softc;
2400 i = RUN_CMDQ_GET(&sc->cmdq_store);
2401 DPRINTF("cmdq_store=%d\n", i);
2402 sc->cmdq[i].func = run_key_set_cb;
2403 sc->cmdq[i].arg0 = NULL;
2404 sc->cmdq[i].arg1 = vap;
2406 IEEE80211_ADDR_COPY(sc->cmdq[i].mac, mac);
2407 ieee80211_runtask(ic, &sc->cmdq_task);
2410 * To make sure key will be set when hostapd
2411 * calls iv_key_set() before if_init().
2413 if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
2415 sc->cmdq_key_set = RUN_CMDQ_GO;
2423 * If wlan is destroyed without being brought down i.e. without
2424 * wlan down or wpa_cli terminate, this function is called after
2425 * vap is gone. Don't refer it.
2428 run_key_delete_cb(void *arg)
2430 struct run_cmdq *cmdq = arg;
2431 struct run_softc *sc = cmdq->arg1;
2432 struct ieee80211_key *k = &cmdq->key;
2436 RUN_LOCK_ASSERT(sc, MA_OWNED);
2438 if (k->wk_flags & IEEE80211_KEY_GROUP) {
2439 /* remove group key */
2440 DPRINTF("removing group key\n");
2441 run_read(sc, RT2860_SKEY_MODE_0_7, &attr);
2442 attr &= ~(0xf << (k->wk_keyix * 4));
2443 run_write(sc, RT2860_SKEY_MODE_0_7, attr);
2445 /* remove pairwise key */
2446 DPRINTF("removing key for wcid %x\n", k->wk_pad);
2447 /* matching wcid was written to wk_pad in run_key_set() */
2449 run_read(sc, RT2860_WCID_ATTR(wcid), &attr);
2451 run_write(sc, RT2860_WCID_ATTR(wcid), attr);
2452 run_set_region_4(sc, RT2860_WCID_ENTRY(wcid), 0, 8);
2462 run_key_delete(struct ieee80211vap *vap, struct ieee80211_key *k)
2464 struct ieee80211com *ic = vap->iv_ic;
2465 struct run_softc *sc = ic->ic_ifp->if_softc;
2466 struct ieee80211_key *k0;
2470 * When called back, key might be gone. So, make a copy
2471 * of some values need to delete keys before deferring.
2472 * But, because of LOR with node lock, cannot use lock here.
2473 * So, use atomic instead.
2475 i = RUN_CMDQ_GET(&sc->cmdq_store);
2476 DPRINTF("cmdq_store=%d\n", i);
2477 sc->cmdq[i].func = run_key_delete_cb;
2478 sc->cmdq[i].arg0 = NULL;
2479 sc->cmdq[i].arg1 = sc;
2480 k0 = &sc->cmdq[i].key;
2481 k0->wk_flags = k->wk_flags;
2482 k0->wk_keyix = k->wk_keyix;
2483 /* matching wcid was written to wk_pad in run_key_set() */
2484 k0->wk_pad = k->wk_pad;
2485 ieee80211_runtask(ic, &sc->cmdq_task);
2486 return (1); /* return fake success */
2491 run_ratectl_to(void *arg)
2493 struct run_softc *sc = arg;
2495 /* do it in a process context, so it can go sleep */
2496 ieee80211_runtask(sc->sc_ifp->if_l2com, &sc->ratectl_task);
2497 /* next timeout will be rescheduled in the callback task */
2502 run_ratectl_cb(void *arg, int pending)
2504 struct run_softc *sc = arg;
2505 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
2506 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2511 if (sc->rvp_cnt > 1 || vap->iv_opmode != IEEE80211_M_STA) {
2513 * run_reset_livelock() doesn't do anything with AMRR,
2514 * but Ralink wants us to call it every 1 sec. So, we
2515 * piggyback here rather than creating another callout.
2516 * Livelock may occur only in HOSTAP or IBSS mode
2517 * (when h/w is sending beacons).
2520 run_reset_livelock(sc);
2521 /* just in case, there are some stats to drain */
2526 ieee80211_iterate_nodes(&ic->ic_sta, run_iter_func, sc);
2529 if(sc->ratectl_run != RUN_RATECTL_OFF)
2530 usb_callout_reset(&sc->ratectl_ch, hz, run_ratectl_to, sc);
2535 run_drain_fifo(void *arg)
2537 struct run_softc *sc = arg;
2538 struct ifnet *ifp = sc->sc_ifp;
2540 uint16_t (*wstat)[3];
2541 uint8_t wcid, mcs, pid;
2544 RUN_LOCK_ASSERT(sc, MA_OWNED);
2547 /* drain Tx status FIFO (maxsize = 16) */
2548 run_read(sc, RT2860_TX_STAT_FIFO, &stat);
2549 DPRINTFN(4, "tx stat 0x%08x\n", stat);
2550 if (!(stat & RT2860_TXQ_VLD))
2553 wcid = (stat >> RT2860_TXQ_WCID_SHIFT) & 0xff;
2555 /* if no ACK was requested, no feedback is available */
2556 if (!(stat & RT2860_TXQ_ACKREQ) || wcid > RT2870_WCID_MAX ||
2561 * Even though each stat is Tx-complete-status like format,
2562 * the device can poll stats. Because there is no guarantee
2563 * that the referring node is still around when read the stats.
2564 * So that, if we use ieee80211_ratectl_tx_update(), we will
2565 * have hard time not to refer already freed node.
2567 * To eliminate such page faults, we poll stats in softc.
2568 * Then, update the rates later with ieee80211_ratectl_tx_update().
2570 wstat = &(sc->wcid_stats[wcid]);
2571 (*wstat)[RUN_TXCNT]++;
2572 if (stat & RT2860_TXQ_OK)
2573 (*wstat)[RUN_SUCCESS]++;
2577 * Check if there were retries, ie if the Tx success rate is
2578 * different from the requested rate. Note that it works only
2579 * because we do not allow rate fallback from OFDM to CCK.
2581 mcs = (stat >> RT2860_TXQ_MCS_SHIFT) & 0x7f;
2582 pid = (stat >> RT2860_TXQ_PID_SHIFT) & 0xf;
2583 if ((retry = pid -1 - mcs) > 0) {
2584 (*wstat)[RUN_TXCNT] += retry;
2585 (*wstat)[RUN_RETRY] += retry;
2588 DPRINTFN(3, "count=%d\n", sc->fifo_cnt);
2594 run_iter_func(void *arg, struct ieee80211_node *ni)
2596 struct run_softc *sc = arg;
2597 struct ieee80211vap *vap = ni->ni_vap;
2598 struct ieee80211com *ic = ni->ni_ic;
2599 struct ifnet *ifp = ic->ic_ifp;
2600 struct run_node *rn = (void *)ni;
2601 union run_stats sta[2];
2602 uint16_t (*wstat)[3];
2603 int txcnt, success, retrycnt, error;
2607 /* Check for special case */
2608 if (sc->rvp_cnt <= 1 && vap->iv_opmode == IEEE80211_M_STA &&
2612 if (sc->rvp_cnt <= 1 && (vap->iv_opmode == IEEE80211_M_IBSS ||
2613 vap->iv_opmode == IEEE80211_M_STA)) {
2614 /* read statistic counters (clear on read) and update AMRR state */
2615 error = run_read_region_1(sc, RT2860_TX_STA_CNT0, (uint8_t *)sta,
2620 /* count failed TX as errors */
2621 ifp->if_oerrors += le16toh(sta[0].error.fail);
2623 retrycnt = le16toh(sta[1].tx.retry);
2624 success = le16toh(sta[1].tx.success);
2625 txcnt = retrycnt + success + le16toh(sta[0].error.fail);
2627 DPRINTFN(3, "retrycnt=%d success=%d failcnt=%d\n",
2628 retrycnt, success, le16toh(sta[0].error.fail));
2630 wstat = &(sc->wcid_stats[RUN_AID2WCID(ni->ni_associd)]);
2632 if (wstat == &(sc->wcid_stats[0]) ||
2633 wstat > &(sc->wcid_stats[RT2870_WCID_MAX]))
2636 txcnt = (*wstat)[RUN_TXCNT];
2637 success = (*wstat)[RUN_SUCCESS];
2638 retrycnt = (*wstat)[RUN_RETRY];
2639 DPRINTFN(3, "retrycnt=%d txcnt=%d success=%d\n",
2640 retrycnt, txcnt, success);
2642 memset(wstat, 0, sizeof(*wstat));
2645 ieee80211_ratectl_tx_update(vap, ni, &txcnt, &success, &retrycnt);
2646 rn->amrr_ridx = ieee80211_ratectl_rate(ni, NULL, 0);
2651 DPRINTFN(3, "ridx=%d\n", rn->amrr_ridx);
2655 run_newassoc_cb(void *arg)
2657 struct run_cmdq *cmdq = arg;
2658 struct ieee80211_node *ni = cmdq->arg1;
2659 struct run_softc *sc = ni->ni_vap->iv_ic->ic_ifp->if_softc;
2660 uint8_t wcid = cmdq->wcid;
2662 RUN_LOCK_ASSERT(sc, MA_OWNED);
2664 run_write_region_1(sc, RT2860_WCID_ENTRY(wcid),
2665 ni->ni_macaddr, IEEE80211_ADDR_LEN);
2667 memset(&(sc->wcid_stats[wcid]), 0, sizeof(sc->wcid_stats[wcid]));
2671 run_newassoc(struct ieee80211_node *ni, int isnew)
2673 struct run_node *rn = (void *)ni;
2674 struct ieee80211_rateset *rs = &ni->ni_rates;
2675 struct ieee80211vap *vap = ni->ni_vap;
2676 struct ieee80211com *ic = vap->iv_ic;
2677 struct run_softc *sc = ic->ic_ifp->if_softc;
2683 wcid = (vap->iv_opmode == IEEE80211_M_STA) ?
2684 1 : RUN_AID2WCID(ni->ni_associd);
2686 if (wcid > RT2870_WCID_MAX) {
2687 device_printf(sc->sc_dev, "wcid=%d out of range\n", wcid);
2691 /* only interested in true associations */
2692 if (isnew && ni->ni_associd != 0) {
2695 * This function could is called though timeout function.
2698 uint32_t cnt = RUN_CMDQ_GET(&sc->cmdq_store);
2699 DPRINTF("cmdq_store=%d\n", cnt);
2700 sc->cmdq[cnt].func = run_newassoc_cb;
2701 sc->cmdq[cnt].arg0 = NULL;
2702 sc->cmdq[cnt].arg1 = ni;
2703 sc->cmdq[cnt].wcid = wcid;
2704 ieee80211_runtask(ic, &sc->cmdq_task);
2707 DPRINTF("new assoc isnew=%d associd=%x addr=%s\n",
2708 isnew, ni->ni_associd, ether_sprintf(ni->ni_macaddr));
2710 for (i = 0; i < rs->rs_nrates; i++) {
2711 rate = rs->rs_rates[i] & IEEE80211_RATE_VAL;
2712 /* convert 802.11 rate to hardware rate index */
2713 for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
2714 if (rt2860_rates[ridx].rate == rate)
2717 /* determine rate of control response frames */
2718 for (j = i; j >= 0; j--) {
2719 if ((rs->rs_rates[j] & IEEE80211_RATE_BASIC) &&
2720 rt2860_rates[rn->ridx[i]].phy ==
2721 rt2860_rates[rn->ridx[j]].phy)
2725 rn->ctl_ridx[i] = rn->ridx[j];
2727 /* no basic rate found, use mandatory one */
2728 rn->ctl_ridx[i] = rt2860_rates[ridx].ctl_ridx;
2730 DPRINTF("rate=0x%02x ridx=%d ctl_ridx=%d\n",
2731 rs->rs_rates[i], rn->ridx[i], rn->ctl_ridx[i]);
2733 rate = vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)].mgmtrate;
2734 for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
2735 if (rt2860_rates[ridx].rate == rate)
2737 rn->mgt_ridx = ridx;
2738 DPRINTF("rate=%d, mgmt_ridx=%d\n", rate, rn->mgt_ridx);
2741 if(sc->ratectl_run != RUN_RATECTL_OFF)
2742 usb_callout_reset(&sc->ratectl_ch, hz, run_ratectl_to, sc);
2747 * Return the Rx chain with the highest RSSI for a given frame.
2749 static __inline uint8_t
2750 run_maxrssi_chain(struct run_softc *sc, const struct rt2860_rxwi *rxwi)
2752 uint8_t rxchain = 0;
2754 if (sc->nrxchains > 1) {
2755 if (rxwi->rssi[1] > rxwi->rssi[rxchain])
2757 if (sc->nrxchains > 2)
2758 if (rxwi->rssi[2] > rxwi->rssi[rxchain])
2765 run_rx_frame(struct run_softc *sc, struct mbuf *m, uint32_t dmalen)
2767 struct ifnet *ifp = sc->sc_ifp;
2768 struct ieee80211com *ic = ifp->if_l2com;
2769 struct ieee80211_frame *wh;
2770 struct ieee80211_node *ni;
2771 struct rt2870_rxd *rxd;
2772 struct rt2860_rxwi *rxwi;
2774 uint16_t len, rxwisize;
2778 rxwi = mtod(m, struct rt2860_rxwi *);
2779 len = le16toh(rxwi->len) & 0xfff;
2780 rxwisize = sizeof(struct rt2860_rxwi);
2781 if (sc->mac_ver == 0x5592)
2782 rxwisize += sizeof(uint64_t);
2783 else if (sc->mac_ver == 0x3593)
2784 rxwisize += sizeof(uint32_t);
2785 if (__predict_false(len > dmalen)) {
2788 DPRINTF("bad RXWI length %u > %u\n", len, dmalen);
2791 /* Rx descriptor is located at the end */
2792 rxd = (struct rt2870_rxd *)(mtod(m, caddr_t) + dmalen);
2793 flags = le32toh(rxd->flags);
2795 if (__predict_false(flags & (RT2860_RX_CRCERR | RT2860_RX_ICVERR))) {
2798 DPRINTF("%s error.\n", (flags & RT2860_RX_CRCERR)?"CRC":"ICV");
2802 m->m_data += rxwisize;
2803 m->m_pkthdr.len = m->m_len -= rxwisize;
2805 wh = mtod(m, struct ieee80211_frame *);
2807 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
2808 wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
2809 m->m_flags |= M_WEP;
2812 if (flags & RT2860_RX_L2PAD) {
2813 DPRINTFN(8, "received RT2860_RX_L2PAD frame\n");
2817 ni = ieee80211_find_rxnode(ic,
2818 mtod(m, struct ieee80211_frame_min *));
2820 if (__predict_false(flags & RT2860_RX_MICERR)) {
2821 /* report MIC failures to net80211 for TKIP */
2823 ieee80211_notify_michael_failure(ni->ni_vap, wh,
2827 DPRINTF("MIC error. Someone is lying.\n");
2831 ant = run_maxrssi_chain(sc, rxwi);
2832 rssi = rxwi->rssi[ant];
2833 nf = run_rssi2dbm(sc, rssi, ant);
2835 m->m_pkthdr.rcvif = ifp;
2836 m->m_pkthdr.len = m->m_len = len;
2839 (void)ieee80211_input(ni, m, rssi, nf);
2840 ieee80211_free_node(ni);
2842 (void)ieee80211_input_all(ic, m, rssi, nf);
2845 if (__predict_false(ieee80211_radiotap_active(ic))) {
2846 struct run_rx_radiotap_header *tap = &sc->sc_rxtap;
2850 tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
2851 tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
2852 tap->wr_antsignal = rssi;
2853 tap->wr_antenna = ant;
2854 tap->wr_dbm_antsignal = run_rssi2dbm(sc, rssi, ant);
2855 tap->wr_rate = 2; /* in case it can't be found below */
2856 phy = le16toh(rxwi->phy);
2857 switch (phy & RT2860_PHY_MODE) {
2858 case RT2860_PHY_CCK:
2859 switch ((phy & RT2860_PHY_MCS) & ~RT2860_PHY_SHPRE) {
2860 case 0: tap->wr_rate = 2; break;
2861 case 1: tap->wr_rate = 4; break;
2862 case 2: tap->wr_rate = 11; break;
2863 case 3: tap->wr_rate = 22; break;
2865 if (phy & RT2860_PHY_SHPRE)
2866 tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
2868 case RT2860_PHY_OFDM:
2869 switch (phy & RT2860_PHY_MCS) {
2870 case 0: tap->wr_rate = 12; break;
2871 case 1: tap->wr_rate = 18; break;
2872 case 2: tap->wr_rate = 24; break;
2873 case 3: tap->wr_rate = 36; break;
2874 case 4: tap->wr_rate = 48; break;
2875 case 5: tap->wr_rate = 72; break;
2876 case 6: tap->wr_rate = 96; break;
2877 case 7: tap->wr_rate = 108; break;
2885 run_bulk_rx_callback(struct usb_xfer *xfer, usb_error_t error)
2887 struct run_softc *sc = usbd_xfer_softc(xfer);
2888 struct ifnet *ifp = sc->sc_ifp;
2889 struct mbuf *m = NULL;
2895 rxwisize = sizeof(struct rt2860_rxwi);
2896 if (sc->mac_ver == 0x5592)
2897 rxwisize += sizeof(uint64_t);
2898 else if (sc->mac_ver == 0x3593)
2899 rxwisize += sizeof(uint32_t);
2901 usbd_xfer_status(xfer, &xferlen, NULL, NULL, NULL);
2903 switch (USB_GET_STATE(xfer)) {
2904 case USB_ST_TRANSFERRED:
2906 DPRINTFN(15, "rx done, actlen=%d\n", xferlen);
2908 if (xferlen < (int)(sizeof(uint32_t) + rxwisize +
2909 sizeof(struct rt2870_rxd))) {
2910 DPRINTF("xfer too short %d\n", xferlen);
2920 if (sc->rx_m == NULL) {
2921 sc->rx_m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR,
2922 MJUMPAGESIZE /* xfer can be bigger than MCLBYTES */);
2924 if (sc->rx_m == NULL) {
2925 DPRINTF("could not allocate mbuf - idle with stall\n");
2927 usbd_xfer_set_stall(xfer);
2928 usbd_xfer_set_frames(xfer, 0);
2931 * Directly loading a mbuf cluster into DMA to
2932 * save some data copying. This works because
2933 * there is only one cluster.
2935 usbd_xfer_set_frame_data(xfer, 0,
2936 mtod(sc->rx_m, caddr_t), RUN_MAX_RXSZ);
2937 usbd_xfer_set_frames(xfer, 1);
2939 usbd_transfer_submit(xfer);
2942 default: /* Error */
2943 if (error != USB_ERR_CANCELLED) {
2944 /* try to clear stall first */
2945 usbd_xfer_set_stall(xfer);
2947 if (error == USB_ERR_TIMEOUT)
2948 device_printf(sc->sc_dev, "device timeout\n");
2954 if (sc->rx_m != NULL) {
2964 /* inputting all the frames must be last */
2968 m->m_pkthdr.len = m->m_len = xferlen;
2970 /* HW can aggregate multiple 802.11 frames in a single USB xfer */
2972 dmalen = le32toh(*mtod(m, uint32_t *)) & 0xffff;
2974 if ((dmalen >= (uint32_t)-8) || (dmalen == 0) ||
2975 ((dmalen & 3) != 0)) {
2976 DPRINTF("bad DMA length %u\n", dmalen);
2979 if ((dmalen + 8) > (uint32_t)xferlen) {
2980 DPRINTF("bad DMA length %u > %d\n",
2981 dmalen + 8, xferlen);
2985 /* If it is the last one or a single frame, we won't copy. */
2986 if ((xferlen -= dmalen + 8) <= 8) {
2987 /* trim 32-bit DMA-len header */
2989 m->m_pkthdr.len = m->m_len -= 4;
2990 run_rx_frame(sc, m, dmalen);
2991 m = NULL; /* don't free source buffer */
2995 /* copy aggregated frames to another mbuf */
2996 m0 = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2997 if (__predict_false(m0 == NULL)) {
2998 DPRINTF("could not allocate mbuf\n");
3002 m_copydata(m, 4 /* skip 32-bit DMA-len header */,
3003 dmalen + sizeof(struct rt2870_rxd), mtod(m0, caddr_t));
3004 m0->m_pkthdr.len = m0->m_len =
3005 dmalen + sizeof(struct rt2870_rxd);
3006 run_rx_frame(sc, m0, dmalen);
3008 /* update data ptr */
3009 m->m_data += dmalen + 8;
3010 m->m_pkthdr.len = m->m_len -= dmalen + 8;
3013 /* make sure we free the source buffer, if any */
3020 run_tx_free(struct run_endpoint_queue *pq,
3021 struct run_tx_data *data, int txerr)
3023 if (data->m != NULL) {
3024 if (data->m->m_flags & M_TXCB)
3025 ieee80211_process_callback(data->ni, data->m,
3026 txerr ? ETIMEDOUT : 0);
3030 if (data->ni == NULL) {
3031 DPRINTF("no node\n");
3033 ieee80211_free_node(data->ni);
3038 STAILQ_INSERT_TAIL(&pq->tx_fh, data, next);
3043 run_bulk_tx_callbackN(struct usb_xfer *xfer, usb_error_t error, u_int index)
3045 struct run_softc *sc = usbd_xfer_softc(xfer);
3046 struct ifnet *ifp = sc->sc_ifp;
3047 struct ieee80211com *ic = ifp->if_l2com;
3048 struct run_tx_data *data;
3049 struct ieee80211vap *vap = NULL;
3050 struct usb_page_cache *pc;
3051 struct run_endpoint_queue *pq = &sc->sc_epq[index];
3053 usb_frlength_t size;
3057 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
3059 switch (USB_GET_STATE(xfer)) {
3060 case USB_ST_TRANSFERRED:
3061 DPRINTFN(11, "transfer complete: %d "
3062 "bytes @ index %d\n", actlen, index);
3064 data = usbd_xfer_get_priv(xfer);
3066 run_tx_free(pq, data, 0);
3067 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3069 usbd_xfer_set_priv(xfer, NULL);
3076 data = STAILQ_FIRST(&pq->tx_qh);
3080 STAILQ_REMOVE_HEAD(&pq->tx_qh, next);
3083 size = (sc->mac_ver == 0x5592) ?
3084 sizeof(data->desc) + sizeof(uint32_t) : sizeof(data->desc);
3085 if ((m->m_pkthdr.len +
3086 size + 3 + 8) > RUN_MAX_TXSZ) {
3087 DPRINTF("data overflow, %u bytes\n",
3092 run_tx_free(pq, data, 1);
3097 pc = usbd_xfer_get_frame(xfer, 0);
3098 usbd_copy_in(pc, 0, &data->desc, size);
3099 usbd_m_copy_in(pc, size, m, 0, m->m_pkthdr.len);
3100 size += m->m_pkthdr.len;
3102 * Align end on a 4-byte boundary, pad 8 bytes (CRC +
3103 * 4-byte padding), and be sure to zero those trailing
3106 usbd_frame_zero(pc, size, ((-size) & 3) + 8);
3107 size += ((-size) & 3) + 8;
3109 vap = data->ni->ni_vap;
3110 if (ieee80211_radiotap_active_vap(vap)) {
3111 struct run_tx_radiotap_header *tap = &sc->sc_txtap;
3112 struct rt2860_txwi *txwi =
3113 (struct rt2860_txwi *)(&data->desc + sizeof(struct rt2870_txd));
3115 tap->wt_rate = rt2860_rates[data->ridx].rate;
3116 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
3117 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
3118 tap->wt_hwqueue = index;
3119 if (le16toh(txwi->phy) & RT2860_PHY_SHPRE)
3120 tap->wt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
3122 ieee80211_radiotap_tx(vap, m);
3125 DPRINTFN(11, "sending frame len=%u/%u @ index %d\n",
3126 m->m_pkthdr.len, size, index);
3128 usbd_xfer_set_frame_len(xfer, 0, size);
3129 usbd_xfer_set_priv(xfer, data);
3131 usbd_transfer_submit(xfer);
3140 DPRINTF("USB transfer error, %s\n",
3141 usbd_errstr(error));
3143 data = usbd_xfer_get_priv(xfer);
3148 if(data->ni != NULL)
3149 vap = data->ni->ni_vap;
3150 run_tx_free(pq, data, error);
3151 usbd_xfer_set_priv(xfer, NULL);
3154 vap = TAILQ_FIRST(&ic->ic_vaps);
3156 if (error != USB_ERR_CANCELLED) {
3157 if (error == USB_ERR_TIMEOUT) {
3158 device_printf(sc->sc_dev, "device timeout\n");
3159 uint32_t i = RUN_CMDQ_GET(&sc->cmdq_store);
3160 DPRINTF("cmdq_store=%d\n", i);
3161 sc->cmdq[i].func = run_usb_timeout_cb;
3162 sc->cmdq[i].arg0 = vap;
3163 ieee80211_runtask(ic, &sc->cmdq_task);
3167 * Try to clear stall first, also if other
3168 * errors occur, hence clearing stall
3169 * introduces a 50 ms delay:
3171 usbd_xfer_set_stall(xfer);
3179 run_bulk_tx_callback0(struct usb_xfer *xfer, usb_error_t error)
3181 run_bulk_tx_callbackN(xfer, error, 0);
3185 run_bulk_tx_callback1(struct usb_xfer *xfer, usb_error_t error)
3187 run_bulk_tx_callbackN(xfer, error, 1);
3191 run_bulk_tx_callback2(struct usb_xfer *xfer, usb_error_t error)
3193 run_bulk_tx_callbackN(xfer, error, 2);
3197 run_bulk_tx_callback3(struct usb_xfer *xfer, usb_error_t error)
3199 run_bulk_tx_callbackN(xfer, error, 3);
3203 run_bulk_tx_callback4(struct usb_xfer *xfer, usb_error_t error)
3205 run_bulk_tx_callbackN(xfer, error, 4);
3209 run_bulk_tx_callback5(struct usb_xfer *xfer, usb_error_t error)
3211 run_bulk_tx_callbackN(xfer, error, 5);
3215 run_set_tx_desc(struct run_softc *sc, struct run_tx_data *data)
3217 struct mbuf *m = data->m;
3218 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
3219 struct ieee80211vap *vap = data->ni->ni_vap;
3220 struct ieee80211_frame *wh;
3221 struct rt2870_txd *txd;
3222 struct rt2860_txwi *txwi;
3223 uint16_t xferlen, txwisize;
3225 uint8_t ridx = data->ridx;
3228 /* get MCS code from rate index */
3229 mcs = rt2860_rates[ridx].mcs;
3231 txwisize = (sc->mac_ver == 0x5592) ?
3232 sizeof(*txwi) + sizeof(uint32_t) : sizeof(*txwi);
3233 xferlen = txwisize + m->m_pkthdr.len;
3235 /* roundup to 32-bit alignment */
3236 xferlen = (xferlen + 3) & ~3;
3238 txd = (struct rt2870_txd *)&data->desc;
3239 txd->len = htole16(xferlen);
3241 wh = mtod(m, struct ieee80211_frame *);
3244 * Ether both are true or both are false, the header
3245 * are nicely aligned to 32-bit. So, no L2 padding.
3247 if(IEEE80211_HAS_ADDR4(wh) == IEEE80211_QOS_HAS_SEQ(wh))
3252 /* setup TX Wireless Information */
3253 txwi = (struct rt2860_txwi *)(txd + 1);
3254 txwi->len = htole16(m->m_pkthdr.len - pad);
3255 if (rt2860_rates[ridx].phy == IEEE80211_T_DS) {
3256 txwi->phy = htole16(RT2860_PHY_CCK);
3257 if (ridx != RT2860_RIDX_CCK1 &&
3258 (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
3259 mcs |= RT2860_PHY_SHPRE;
3261 txwi->phy = htole16(RT2860_PHY_OFDM);
3262 txwi->phy |= htole16(mcs);
3264 /* check if RTS/CTS or CTS-to-self protection is required */
3265 if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
3266 (m->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold ||
3267 ((ic->ic_flags & IEEE80211_F_USEPROT) &&
3268 rt2860_rates[ridx].phy == IEEE80211_T_OFDM)))
3269 txwi->txop |= RT2860_TX_TXOP_HT;
3271 txwi->txop |= RT2860_TX_TXOP_BACKOFF;
3273 if (vap->iv_opmode != IEEE80211_M_STA && !IEEE80211_QOS_HAS_SEQ(wh))
3274 txwi->xflags |= RT2860_TX_NSEQ;
3277 /* This function must be called locked */
3279 run_tx(struct run_softc *sc, struct mbuf *m, struct ieee80211_node *ni)
3281 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
3282 struct ieee80211vap *vap = ni->ni_vap;
3283 struct ieee80211_frame *wh;
3284 struct ieee80211_channel *chan;
3285 const struct ieee80211_txparam *tp;
3286 struct run_node *rn = (void *)ni;
3287 struct run_tx_data *data;
3288 struct rt2870_txd *txd;
3289 struct rt2860_txwi *txwi;
3301 RUN_LOCK_ASSERT(sc, MA_OWNED);
3303 wh = mtod(m, struct ieee80211_frame *);
3305 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
3308 * There are 7 bulk endpoints: 1 for RX
3309 * and 6 for TX (4 EDCAs + HCCA + Prio).
3310 * Update 03-14-2009: some devices like the Planex GW-US300MiniS
3311 * seem to have only 4 TX bulk endpoints (Fukaumi Naoki).
3313 if ((hasqos = IEEE80211_QOS_HAS_SEQ(wh))) {
3316 if(IEEE80211_HAS_ADDR4(wh))
3317 frm = ((struct ieee80211_qosframe_addr4 *)wh)->i_qos;
3319 frm =((struct ieee80211_qosframe *)wh)->i_qos;
3321 qos = le16toh(*(const uint16_t *)frm);
3322 tid = qos & IEEE80211_QOS_TID;
3323 qid = TID_TO_WME_AC(tid);
3329 qflags = (qid < 4) ? RT2860_TX_QSEL_EDCA : RT2860_TX_QSEL_HCCA;
3331 DPRINTFN(8, "qos %d\tqid %d\ttid %d\tqflags %x\n",
3332 qos, qid, tid, qflags);
3334 chan = (ni->ni_chan != IEEE80211_CHAN_ANYC)?ni->ni_chan:ic->ic_curchan;
3335 tp = &vap->iv_txparms[ieee80211_chan2mode(chan)];
3337 /* pickup a rate index */
3338 if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
3339 type != IEEE80211_FC0_TYPE_DATA) {
3340 ridx = (ic->ic_curmode == IEEE80211_MODE_11A) ?
3341 RT2860_RIDX_OFDM6 : RT2860_RIDX_CCK1;
3342 ctl_ridx = rt2860_rates[ridx].ctl_ridx;
3344 if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
3345 ridx = rn->fix_ridx;
3347 ridx = rn->amrr_ridx;
3348 ctl_ridx = rt2860_rates[ridx].ctl_ridx;
3351 if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
3352 (!hasqos || (qos & IEEE80211_QOS_ACKPOLICY) !=
3353 IEEE80211_QOS_ACKPOLICY_NOACK)) {
3354 xflags |= RT2860_TX_ACK;
3355 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
3356 dur = rt2860_rates[ctl_ridx].sp_ack_dur;
3358 dur = rt2860_rates[ctl_ridx].lp_ack_dur;
3359 USETW(wh->i_dur, dur);
3362 /* reserve slots for mgmt packets, just in case */
3363 if (sc->sc_epq[qid].tx_nfree < 3) {
3364 DPRINTFN(10, "tx ring %d is full\n", qid);
3368 data = STAILQ_FIRST(&sc->sc_epq[qid].tx_fh);
3369 STAILQ_REMOVE_HEAD(&sc->sc_epq[qid].tx_fh, next);
3370 sc->sc_epq[qid].tx_nfree--;
3372 txd = (struct rt2870_txd *)&data->desc;
3373 txd->flags = qflags;
3374 txwi = (struct rt2860_txwi *)(txd + 1);
3375 txwi->xflags = xflags;
3376 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
3379 txwi->wcid = (vap->iv_opmode == IEEE80211_M_STA) ?
3380 1 : RUN_AID2WCID(ni->ni_associd);
3382 /* clear leftover garbage bits */
3390 run_set_tx_desc(sc, data);
3393 * The chip keeps track of 2 kind of Tx stats,
3394 * * TX_STAT_FIFO, for per WCID stats, and
3395 * * TX_STA_CNT0 for all-TX-in-one stats.
3397 * To use FIFO stats, we need to store MCS into the driver-private
3398 * PacketID field. So that, we can tell whose stats when we read them.
3399 * We add 1 to the MCS because setting the PacketID field to 0 means
3400 * that we don't want feedback in TX_STAT_FIFO.
3401 * And, that's what we want for STA mode, since TX_STA_CNT0 does the job.
3403 * FIFO stats doesn't count Tx with WCID 0xff, so we do this in run_tx().
3405 if (sc->rvp_cnt > 1 || vap->iv_opmode == IEEE80211_M_HOSTAP ||
3406 vap->iv_opmode == IEEE80211_M_MBSS) {
3407 uint16_t pid = (rt2860_rates[ridx].mcs + 1) & 0xf;
3408 txwi->len |= htole16(pid << RT2860_TX_PID_SHIFT);
3411 * Unlike PCI based devices, we don't get any interrupt from
3412 * USB devices, so we simulate FIFO-is-full interrupt here.
3413 * Ralink recomends to drain FIFO stats every 100 ms, but 16 slots
3414 * quickly get fulled. To prevent overflow, increment a counter on
3415 * every FIFO stat request, so we know how many slots are left.
3416 * We do this only in HOSTAP or multiple vap mode since FIFO stats
3417 * are used only in those modes.
3418 * We just drain stats. AMRR gets updated every 1 sec by
3419 * run_ratectl_cb() via callout.
3420 * Call it early. Otherwise overflow.
3422 if (sc->fifo_cnt++ == 10) {
3424 * With multiple vaps or if_bridge, if_start() is called
3425 * with a non-sleepable lock, tcpinp. So, need to defer.
3427 uint32_t i = RUN_CMDQ_GET(&sc->cmdq_store);
3428 DPRINTFN(6, "cmdq_store=%d\n", i);
3429 sc->cmdq[i].func = run_drain_fifo;
3430 sc->cmdq[i].arg0 = sc;
3431 ieee80211_runtask(ic, &sc->cmdq_task);
3435 STAILQ_INSERT_TAIL(&sc->sc_epq[qid].tx_qh, data, next);
3437 usbd_transfer_start(sc->sc_xfer[qid]);
3439 DPRINTFN(8, "sending data frame len=%d rate=%d qid=%d\n",
3440 m->m_pkthdr.len + (int)(sizeof(struct rt2870_txd) +
3441 sizeof(struct rt2860_txwi)), rt2860_rates[ridx].rate, qid);
3447 run_tx_mgt(struct run_softc *sc, struct mbuf *m, struct ieee80211_node *ni)
3449 struct ifnet *ifp = sc->sc_ifp;
3450 struct ieee80211com *ic = ifp->if_l2com;
3451 struct run_node *rn = (void *)ni;
3452 struct run_tx_data *data;
3453 struct ieee80211_frame *wh;
3454 struct rt2870_txd *txd;
3455 struct rt2860_txwi *txwi;
3457 uint8_t ridx = rn->mgt_ridx;
3462 RUN_LOCK_ASSERT(sc, MA_OWNED);
3464 wh = mtod(m, struct ieee80211_frame *);
3466 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
3468 /* tell hardware to add timestamp for probe responses */
3470 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
3471 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
3472 wflags |= RT2860_TX_TS;
3473 else if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
3474 xflags |= RT2860_TX_ACK;
3476 dur = ieee80211_ack_duration(ic->ic_rt, rt2860_rates[ridx].rate,
3477 ic->ic_flags & IEEE80211_F_SHPREAMBLE);
3478 USETW(wh->i_dur, dur);
3481 if (sc->sc_epq[0].tx_nfree == 0) {
3482 /* let caller free mbuf */
3483 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3486 data = STAILQ_FIRST(&sc->sc_epq[0].tx_fh);
3487 STAILQ_REMOVE_HEAD(&sc->sc_epq[0].tx_fh, next);
3488 sc->sc_epq[0].tx_nfree--;
3490 txd = (struct rt2870_txd *)&data->desc;
3491 txd->flags = RT2860_TX_QSEL_EDCA;
3492 txwi = (struct rt2860_txwi *)(txd + 1);
3494 txwi->flags = wflags;
3495 txwi->xflags = xflags;
3496 txwi->txop = 0; /* clear leftover garbage bits */
3502 run_set_tx_desc(sc, data);
3504 DPRINTFN(10, "sending mgt frame len=%d rate=%d\n", m->m_pkthdr.len +
3505 (int)(sizeof(struct rt2870_txd) + sizeof(struct rt2860_txwi)),
3506 rt2860_rates[ridx].rate);
3508 STAILQ_INSERT_TAIL(&sc->sc_epq[0].tx_qh, data, next);
3510 usbd_transfer_start(sc->sc_xfer[0]);
3516 run_sendprot(struct run_softc *sc,
3517 const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate)
3519 struct ieee80211com *ic = ni->ni_ic;
3520 struct ieee80211_frame *wh;
3521 struct run_tx_data *data;
3522 struct rt2870_txd *txd;
3523 struct rt2860_txwi *txwi;
3535 RUN_LOCK_ASSERT(sc, MA_OWNED);
3537 KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY,
3538 ("protection %d", prot));
3540 wh = mtod(m, struct ieee80211_frame *);
3541 pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
3542 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
3544 protrate = ieee80211_ctl_rate(ic->ic_rt, rate);
3545 ackrate = ieee80211_ack_rate(ic->ic_rt, rate);
3547 isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0;
3548 dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort)
3549 + ieee80211_ack_duration(ic->ic_rt, rate, isshort);
3550 wflags = RT2860_TX_FRAG;
3552 /* check that there are free slots before allocating the mbuf */
3553 if (sc->sc_epq[0].tx_nfree == 0) {
3554 /* let caller free mbuf */
3555 sc->sc_ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3559 if (prot == IEEE80211_PROT_RTSCTS) {
3560 /* NB: CTS is the same size as an ACK */
3561 dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort);
3562 xflags |= RT2860_TX_ACK;
3563 mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur);
3565 mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur);
3567 if (mprot == NULL) {
3568 sc->sc_ifp->if_oerrors++;
3569 DPRINTF("could not allocate mbuf\n");
3573 data = STAILQ_FIRST(&sc->sc_epq[0].tx_fh);
3574 STAILQ_REMOVE_HEAD(&sc->sc_epq[0].tx_fh, next);
3575 sc->sc_epq[0].tx_nfree--;
3577 txd = (struct rt2870_txd *)&data->desc;
3578 txd->flags = RT2860_TX_QSEL_EDCA;
3579 txwi = (struct rt2860_txwi *)(txd + 1);
3581 txwi->flags = wflags;
3582 txwi->xflags = xflags;
3583 txwi->txop = 0; /* clear leftover garbage bits */
3586 data->ni = ieee80211_ref_node(ni);
3588 for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
3589 if (rt2860_rates[ridx].rate == protrate)
3593 run_set_tx_desc(sc, data);
3595 DPRINTFN(1, "sending prot len=%u rate=%u\n",
3596 m->m_pkthdr.len, rate);
3598 STAILQ_INSERT_TAIL(&sc->sc_epq[0].tx_qh, data, next);
3600 usbd_transfer_start(sc->sc_xfer[0]);
3606 run_tx_param(struct run_softc *sc, struct mbuf *m, struct ieee80211_node *ni,
3607 const struct ieee80211_bpf_params *params)
3609 struct ieee80211com *ic = ni->ni_ic;
3610 struct ieee80211_frame *wh;
3611 struct run_tx_data *data;
3612 struct rt2870_txd *txd;
3613 struct rt2860_txwi *txwi;
3617 uint8_t opflags = 0;
3621 RUN_LOCK_ASSERT(sc, MA_OWNED);
3623 KASSERT(params != NULL, ("no raw xmit params"));
3625 wh = mtod(m, struct ieee80211_frame *);
3626 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
3628 rate = params->ibp_rate0;
3629 if (!ieee80211_isratevalid(ic->ic_rt, rate)) {
3630 /* let caller free mbuf */
3634 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
3635 xflags |= RT2860_TX_ACK;
3636 if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) {
3637 error = run_sendprot(sc, m, ni,
3638 params->ibp_flags & IEEE80211_BPF_RTS ?
3639 IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY,
3642 /* let caller free mbuf */
3645 opflags |= /*XXX RT2573_TX_LONG_RETRY |*/ RT2860_TX_TXOP_SIFS;
3648 if (sc->sc_epq[0].tx_nfree == 0) {
3649 /* let caller free mbuf */
3650 sc->sc_ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3651 DPRINTF("sending raw frame, but tx ring is full\n");
3654 data = STAILQ_FIRST(&sc->sc_epq[0].tx_fh);
3655 STAILQ_REMOVE_HEAD(&sc->sc_epq[0].tx_fh, next);
3656 sc->sc_epq[0].tx_nfree--;
3658 txd = (struct rt2870_txd *)&data->desc;
3659 txd->flags = RT2860_TX_QSEL_EDCA;
3660 txwi = (struct rt2860_txwi *)(txd + 1);
3662 txwi->xflags = xflags;
3663 txwi->txop = opflags;
3664 txwi->flags = 0; /* clear leftover garbage bits */
3668 for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
3669 if (rt2860_rates[ridx].rate == rate)
3673 run_set_tx_desc(sc, data);
3675 DPRINTFN(10, "sending raw frame len=%u rate=%u\n",
3676 m->m_pkthdr.len, rate);
3678 STAILQ_INSERT_TAIL(&sc->sc_epq[0].tx_qh, data, next);
3680 usbd_transfer_start(sc->sc_xfer[0]);
3686 run_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
3687 const struct ieee80211_bpf_params *params)
3689 struct ifnet *ifp = ni->ni_ic->ic_ifp;
3690 struct run_softc *sc = ifp->if_softc;
3695 /* prevent management frames from being sent if we're not ready */
3696 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
3701 if (params == NULL) {
3703 if ((error = run_tx_mgt(sc, m, ni)) != 0) {
3705 DPRINTF("mgt tx failed\n");
3709 /* tx raw packet with param */
3710 if ((error = run_tx_param(sc, m, ni, params)) != 0) {
3712 DPRINTF("tx with param failed\n");
3725 ieee80211_free_node(ni);
3732 run_start(struct ifnet *ifp)
3734 struct run_softc *sc = ifp->if_softc;
3735 struct ieee80211_node *ni;
3740 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
3746 /* send data frames */
3747 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
3751 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
3752 if (run_tx(sc, m, ni) != 0) {
3753 IFQ_DRV_PREPEND(&ifp->if_snd, m);
3754 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3763 run_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
3765 struct run_softc *sc = ifp->if_softc;
3766 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
3767 struct ifreq *ifr = (struct ifreq *) data;
3772 error = sc->sc_detached ? ENXIO : 0;
3780 if (ifp->if_flags & IFF_UP) {
3781 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)){
3783 run_init_locked(sc);
3785 run_update_promisc_locked(ifp);
3787 if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
3788 (ic->ic_nrunning == 0 || sc->rvp_cnt <= 1)) {
3794 ieee80211_start_all(ic);
3797 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
3800 error = ether_ioctl(ifp, cmd, data);
3811 run_iq_calib(struct run_softc *sc, u_int chan)
3816 run_bbp_write(sc, 158, 0x2c);
3818 run_efuse_read(sc, RT5390_EEPROM_IQ_GAIN_CAL_TX0_2GHZ, &val, 1);
3819 else if (chan <= 64) {
3821 RT5390_EEPROM_IQ_GAIN_CAL_TX0_CH36_TO_CH64_5GHZ,
3823 } else if (chan <= 138) {
3825 RT5390_EEPROM_IQ_GAIN_CAL_TX0_CH100_TO_CH138_5GHZ,
3827 } else if (chan <= 165) {
3829 RT5390_EEPROM_IQ_GAIN_CAL_TX0_CH140_TO_CH165_5GHZ,
3833 run_bbp_write(sc, 159, val);
3836 run_bbp_write(sc, 158, 0x2d);
3838 run_efuse_read(sc, RT5390_EEPROM_IQ_PHASE_CAL_TX0_2GHZ,
3840 } else if (chan <= 64) {
3842 RT5390_EEPROM_IQ_PHASE_CAL_TX0_CH36_TO_CH64_5GHZ,
3844 } else if (chan <= 138) {
3846 RT5390_EEPROM_IQ_PHASE_CAL_TX0_CH100_TO_CH138_5GHZ,
3848 } else if (chan <= 165) {
3850 RT5390_EEPROM_IQ_PHASE_CAL_TX0_CH140_TO_CH165_5GHZ,
3854 run_bbp_write(sc, 159, val);
3857 run_bbp_write(sc, 158, 0x4a);
3859 run_efuse_read(sc, RT5390_EEPROM_IQ_GAIN_CAL_TX1_2GHZ,
3861 } else if (chan <= 64) {
3863 RT5390_EEPROM_IQ_GAIN_CAL_TX1_CH36_TO_CH64_5GHZ,
3865 } else if (chan <= 138) {
3867 RT5390_EEPROM_IQ_GAIN_CAL_TX1_CH100_TO_CH138_5GHZ,
3869 } else if (chan <= 165) {
3871 RT5390_EEPROM_IQ_GAIN_CAL_TX1_CH140_TO_CH165_5GHZ,
3875 run_bbp_write(sc, 159, val);
3878 run_bbp_write(sc, 158, 0x4b);
3880 run_efuse_read(sc, RT5390_EEPROM_IQ_PHASE_CAL_TX1_2GHZ,
3882 } else if (chan <= 64) {
3884 RT5390_EEPROM_IQ_PHASE_CAL_TX1_CH36_TO_CH64_5GHZ,
3886 } else if (chan <= 138) {
3888 RT5390_EEPROM_IQ_PHASE_CAL_TX1_CH100_TO_CH138_5GHZ,
3890 } else if (chan <= 165) {
3892 RT5390_EEPROM_IQ_PHASE_CAL_TX1_CH140_TO_CH165_5GHZ,
3896 run_bbp_write(sc, 159, val);
3898 /* RF IQ compensation control. */
3899 run_bbp_write(sc, 158, 0x04);
3900 run_efuse_read(sc, RT5390_EEPROM_RF_IQ_COMPENSATION_CTL,
3902 run_bbp_write(sc, 159, val);
3904 /* RF IQ imbalance compensation control. */
3905 run_bbp_write(sc, 158, 0x03);
3907 RT5390_EEPROM_RF_IQ_IMBALANCE_COMPENSATION_CTL, &val, 1);
3908 run_bbp_write(sc, 159, val);
3912 run_set_agc(struct run_softc *sc, uint8_t agc)
3916 if (sc->mac_ver == 0x3572) {
3917 run_bbp_read(sc, 27, &bbp);
3919 run_bbp_write(sc, 27, bbp | 0 << 5); /* select Rx0 */
3920 run_bbp_write(sc, 66, agc);
3921 run_bbp_write(sc, 27, bbp | 1 << 5); /* select Rx1 */
3922 run_bbp_write(sc, 66, agc);
3924 run_bbp_write(sc, 66, agc);
3928 run_select_chan_group(struct run_softc *sc, int group)
3933 run_bbp_write(sc, 62, 0x37 - sc->lna[group]);
3934 run_bbp_write(sc, 63, 0x37 - sc->lna[group]);
3935 run_bbp_write(sc, 64, 0x37 - sc->lna[group]);
3936 if (sc->mac_ver < 0x3572)
3937 run_bbp_write(sc, 86, 0x00);
3939 if (sc->mac_ver == 0x3593) {
3940 run_bbp_write(sc, 77, 0x98);
3941 run_bbp_write(sc, 83, (group == 0) ? 0x8a : 0x9a);
3945 if (sc->ext_2ghz_lna) {
3946 if (sc->mac_ver >= 0x5390)
3947 run_bbp_write(sc, 75, 0x52);
3949 run_bbp_write(sc, 82, 0x62);
3950 run_bbp_write(sc, 75, 0x46);
3953 if (sc->mac_ver == 0x5592) {
3954 run_bbp_write(sc, 79, 0x1c);
3955 run_bbp_write(sc, 80, 0x0e);
3956 run_bbp_write(sc, 81, 0x3a);
3957 run_bbp_write(sc, 82, 0x62);
3959 run_bbp_write(sc, 195, 0x80);
3960 run_bbp_write(sc, 196, 0xe0);
3961 run_bbp_write(sc, 195, 0x81);
3962 run_bbp_write(sc, 196, 0x1f);
3963 run_bbp_write(sc, 195, 0x82);
3964 run_bbp_write(sc, 196, 0x38);
3965 run_bbp_write(sc, 195, 0x83);
3966 run_bbp_write(sc, 196, 0x32);
3967 run_bbp_write(sc, 195, 0x85);
3968 run_bbp_write(sc, 196, 0x28);
3969 run_bbp_write(sc, 195, 0x86);
3970 run_bbp_write(sc, 196, 0x19);
3971 } else if (sc->mac_ver >= 0x5390)
3972 run_bbp_write(sc, 75, 0x50);
3974 run_bbp_write(sc, 82,
3975 (sc->mac_ver == 0x3593) ? 0x62 : 0x84);
3976 run_bbp_write(sc, 75, 0x50);
3980 if (sc->mac_ver == 0x5592) {
3981 run_bbp_write(sc, 79, 0x18);
3982 run_bbp_write(sc, 80, 0x08);
3983 run_bbp_write(sc, 81, 0x38);
3984 run_bbp_write(sc, 82, 0x92);
3986 run_bbp_write(sc, 195, 0x80);
3987 run_bbp_write(sc, 196, 0xf0);
3988 run_bbp_write(sc, 195, 0x81);
3989 run_bbp_write(sc, 196, 0x1e);
3990 run_bbp_write(sc, 195, 0x82);
3991 run_bbp_write(sc, 196, 0x28);
3992 run_bbp_write(sc, 195, 0x83);
3993 run_bbp_write(sc, 196, 0x20);
3994 run_bbp_write(sc, 195, 0x85);
3995 run_bbp_write(sc, 196, 0x7f);
3996 run_bbp_write(sc, 195, 0x86);
3997 run_bbp_write(sc, 196, 0x7f);
3998 } else if (sc->mac_ver == 0x3572)
3999 run_bbp_write(sc, 82, 0x94);
4001 run_bbp_write(sc, 82,
4002 (sc->mac_ver == 0x3593) ? 0x82 : 0xf2);
4003 if (sc->ext_5ghz_lna)
4004 run_bbp_write(sc, 75, 0x46);
4006 run_bbp_write(sc, 75, 0x50);
4009 run_read(sc, RT2860_TX_BAND_CFG, &tmp);
4010 tmp &= ~(RT2860_5G_BAND_SEL_N | RT2860_5G_BAND_SEL_P);
4011 tmp |= (group == 0) ? RT2860_5G_BAND_SEL_N : RT2860_5G_BAND_SEL_P;
4012 run_write(sc, RT2860_TX_BAND_CFG, tmp);
4014 /* enable appropriate Power Amplifiers and Low Noise Amplifiers */
4015 tmp = RT2860_RFTR_EN | RT2860_TRSW_EN | RT2860_LNA_PE0_EN;
4016 if (sc->mac_ver == 0x3593)
4017 tmp |= 1 << 29 | 1 << 28;
4018 if (sc->nrxchains > 1)
4019 tmp |= RT2860_LNA_PE1_EN;
4020 if (group == 0) { /* 2GHz */
4021 tmp |= RT2860_PA_PE_G0_EN;
4022 if (sc->ntxchains > 1)
4023 tmp |= RT2860_PA_PE_G1_EN;
4024 if (sc->mac_ver == 0x3593) {
4025 if (sc->ntxchains > 2)
4029 tmp |= RT2860_PA_PE_A0_EN;
4030 if (sc->ntxchains > 1)
4031 tmp |= RT2860_PA_PE_A1_EN;
4033 if (sc->mac_ver == 0x3572) {
4034 run_rt3070_rf_write(sc, 8, 0x00);
4035 run_write(sc, RT2860_TX_PIN_CFG, tmp);
4036 run_rt3070_rf_write(sc, 8, 0x80);
4038 run_write(sc, RT2860_TX_PIN_CFG, tmp);
4040 if (sc->mac_ver == 0x5592) {
4041 run_bbp_write(sc, 195, 0x8d);
4042 run_bbp_write(sc, 196, 0x1a);
4045 if (sc->mac_ver == 0x3593) {
4046 run_read(sc, RT2860_GPIO_CTRL, &tmp);
4050 tmp = (tmp & ~0x00009090) | 0x00000090;
4051 run_write(sc, RT2860_GPIO_CTRL, tmp);
4054 /* set initial AGC value */
4055 if (group == 0) { /* 2GHz band */
4056 if (sc->mac_ver >= 0x3070)
4057 agc = 0x1c + sc->lna[0] * 2;
4059 agc = 0x2e + sc->lna[0];
4060 } else { /* 5GHz band */
4061 if (sc->mac_ver == 0x5592)
4062 agc = 0x24 + sc->lna[group] * 2;
4063 else if (sc->mac_ver == 0x3572 || sc->mac_ver == 0x3593)
4064 agc = 0x22 + (sc->lna[group] * 5) / 3;
4066 agc = 0x32 + (sc->lna[group] * 5) / 3;
4068 run_set_agc(sc, agc);
4072 run_rt2870_set_chan(struct run_softc *sc, u_int chan)
4074 const struct rfprog *rfprog = rt2860_rf2850;
4075 uint32_t r2, r3, r4;
4076 int8_t txpow1, txpow2;
4079 /* find the settings for this channel (we know it exists) */
4080 for (i = 0; rfprog[i].chan != chan; i++);
4083 if (sc->ntxchains == 1)
4084 r2 |= 1 << 14; /* 1T: disable Tx chain 2 */
4085 if (sc->nrxchains == 1)
4086 r2 |= 1 << 17 | 1 << 6; /* 1R: disable Rx chains 2 & 3 */
4087 else if (sc->nrxchains == 2)
4088 r2 |= 1 << 6; /* 2R: disable Rx chain 3 */
4090 /* use Tx power values from EEPROM */
4091 txpow1 = sc->txpow1[i];
4092 txpow2 = sc->txpow2[i];
4094 /* Initialize RF R3 and R4. */
4095 r3 = rfprog[i].r3 & 0xffffc1ff;
4096 r4 = (rfprog[i].r4 & ~(0x001f87c0)) | (sc->freq << 15);
4099 txpow1 = (txpow1 > 0xf) ? (0xf) : (txpow1);
4100 r3 |= (txpow1 << 10) | (1 << 9);
4104 /* txpow1 is not possible larger than 15. */
4105 r3 |= (txpow1 << 10);
4108 txpow2 = (txpow2 > 0xf) ? (0xf) : (txpow2);
4109 r4 |= (txpow2 << 7) | (1 << 6);
4112 r4 |= (txpow2 << 7);
4115 /* Set Tx0 power. */
4116 r3 |= (txpow1 << 9);
4118 /* Set frequency offset and Tx1 power. */
4119 r4 |= (txpow2 << 6);
4122 run_rt2870_rf_write(sc, rfprog[i].r1);
4123 run_rt2870_rf_write(sc, r2);
4124 run_rt2870_rf_write(sc, r3 & ~(1 << 2));
4125 run_rt2870_rf_write(sc, r4);
4129 run_rt2870_rf_write(sc, rfprog[i].r1);
4130 run_rt2870_rf_write(sc, r2);
4131 run_rt2870_rf_write(sc, r3 | (1 << 2));
4132 run_rt2870_rf_write(sc, r4);
4136 run_rt2870_rf_write(sc, rfprog[i].r1);
4137 run_rt2870_rf_write(sc, r2);
4138 run_rt2870_rf_write(sc, r3 & ~(1 << 2));
4139 run_rt2870_rf_write(sc, r4);
4143 run_rt3070_set_chan(struct run_softc *sc, u_int chan)
4145 int8_t txpow1, txpow2;
4149 /* find the settings for this channel (we know it exists) */
4150 for (i = 0; rt2860_rf2850[i].chan != chan; i++);
4152 /* use Tx power values from EEPROM */
4153 txpow1 = sc->txpow1[i];
4154 txpow2 = sc->txpow2[i];
4156 run_rt3070_rf_write(sc, 2, rt3070_freqs[i].n);
4158 /* RT3370/RT3390: RF R3 [7:4] is not reserved bits. */
4159 run_rt3070_rf_read(sc, 3, &rf);
4160 rf = (rf & ~0x0f) | rt3070_freqs[i].k;
4161 run_rt3070_rf_write(sc, 3, rf);
4163 run_rt3070_rf_read(sc, 6, &rf);
4164 rf = (rf & ~0x03) | rt3070_freqs[i].r;
4165 run_rt3070_rf_write(sc, 6, rf);
4168 run_rt3070_rf_read(sc, 12, &rf);
4169 rf = (rf & ~0x1f) | txpow1;
4170 run_rt3070_rf_write(sc, 12, rf);
4173 run_rt3070_rf_read(sc, 13, &rf);
4174 rf = (rf & ~0x1f) | txpow2;
4175 run_rt3070_rf_write(sc, 13, rf);
4177 run_rt3070_rf_read(sc, 1, &rf);
4179 if (sc->ntxchains == 1)
4180 rf |= 1 << 7 | 1 << 5; /* 1T: disable Tx chains 2 & 3 */
4181 else if (sc->ntxchains == 2)
4182 rf |= 1 << 7; /* 2T: disable Tx chain 3 */
4183 if (sc->nrxchains == 1)
4184 rf |= 1 << 6 | 1 << 4; /* 1R: disable Rx chains 2 & 3 */
4185 else if (sc->nrxchains == 2)
4186 rf |= 1 << 6; /* 2R: disable Rx chain 3 */
4187 run_rt3070_rf_write(sc, 1, rf);
4190 run_rt3070_rf_read(sc, 23, &rf);
4191 rf = (rf & ~0x7f) | sc->freq;
4192 run_rt3070_rf_write(sc, 23, rf);
4194 /* program RF filter */
4195 run_rt3070_rf_read(sc, 24, &rf); /* Tx */
4196 rf = (rf & ~0x3f) | sc->rf24_20mhz;
4197 run_rt3070_rf_write(sc, 24, rf);
4198 run_rt3070_rf_read(sc, 31, &rf); /* Rx */
4199 rf = (rf & ~0x3f) | sc->rf24_20mhz;
4200 run_rt3070_rf_write(sc, 31, rf);
4202 /* enable RF tuning */
4203 run_rt3070_rf_read(sc, 7, &rf);
4204 run_rt3070_rf_write(sc, 7, rf | 0x01);
4208 run_rt3572_set_chan(struct run_softc *sc, u_int chan)
4210 int8_t txpow1, txpow2;
4215 /* find the settings for this channel (we know it exists) */
4216 for (i = 0; rt2860_rf2850[i].chan != chan; i++);
4218 /* use Tx power values from EEPROM */
4219 txpow1 = sc->txpow1[i];
4220 txpow2 = sc->txpow2[i];
4223 run_bbp_write(sc, 25, sc->bbp25);
4224 run_bbp_write(sc, 26, sc->bbp26);
4226 /* enable IQ phase correction */
4227 run_bbp_write(sc, 25, 0x09);
4228 run_bbp_write(sc, 26, 0xff);
4231 run_rt3070_rf_write(sc, 2, rt3070_freqs[i].n);
4232 run_rt3070_rf_write(sc, 3, rt3070_freqs[i].k);
4233 run_rt3070_rf_read(sc, 6, &rf);
4234 rf = (rf & ~0x0f) | rt3070_freqs[i].r;
4235 rf |= (chan <= 14) ? 0x08 : 0x04;
4236 run_rt3070_rf_write(sc, 6, rf);
4239 run_rt3070_rf_read(sc, 5, &rf);
4240 rf &= ~(0x08 | 0x04);
4241 rf |= (chan <= 14) ? 0x04 : 0x08;
4242 run_rt3070_rf_write(sc, 5, rf);
4244 /* set Tx power for chain 0 */
4248 rf = 0xe0 | (txpow1 & 0xc) << 1 | (txpow1 & 0x3);
4249 run_rt3070_rf_write(sc, 12, rf);
4251 /* set Tx power for chain 1 */
4255 rf = 0xe0 | (txpow2 & 0xc) << 1 | (txpow2 & 0x3);
4256 run_rt3070_rf_write(sc, 13, rf);
4258 /* set Tx/Rx streams */
4259 run_rt3070_rf_read(sc, 1, &rf);
4261 if (sc->ntxchains == 1)
4262 rf |= 1 << 7 | 1 << 5; /* 1T: disable Tx chains 2 & 3 */
4263 else if (sc->ntxchains == 2)
4264 rf |= 1 << 7; /* 2T: disable Tx chain 3 */
4265 if (sc->nrxchains == 1)
4266 rf |= 1 << 6 | 1 << 4; /* 1R: disable Rx chains 2 & 3 */
4267 else if (sc->nrxchains == 2)
4268 rf |= 1 << 6; /* 2R: disable Rx chain 3 */
4269 run_rt3070_rf_write(sc, 1, rf);
4272 run_rt3070_rf_read(sc, 23, &rf);
4273 rf = (rf & ~0x7f) | sc->freq;
4274 run_rt3070_rf_write(sc, 23, rf);
4276 /* program RF filter */
4277 rf = sc->rf24_20mhz;
4278 run_rt3070_rf_write(sc, 24, rf); /* Tx */
4279 run_rt3070_rf_write(sc, 31, rf); /* Rx */
4281 /* enable RF tuning */
4282 run_rt3070_rf_read(sc, 7, &rf);
4283 rf = (chan <= 14) ? 0xd8 : ((rf & ~0xc8) | 0x14);
4284 run_rt3070_rf_write(sc, 7, rf);
4287 rf = (chan <= 14) ? 0xc3 : 0xc0;
4288 run_rt3070_rf_write(sc, 9, rf);
4290 /* set loop filter 1 */
4291 run_rt3070_rf_write(sc, 10, 0xf1);
4292 /* set loop filter 2 */
4293 run_rt3070_rf_write(sc, 11, (chan <= 14) ? 0xb9 : 0x00);
4296 run_rt3070_rf_write(sc, 15, (chan <= 14) ? 0x53 : 0x43);
4299 rf = 0x48 | sc->txmixgain_2ghz;
4301 rf = 0x78 | sc->txmixgain_5ghz;
4302 run_rt3070_rf_write(sc, 16, rf);
4305 run_rt3070_rf_write(sc, 17, 0x23);
4309 else if (chan <= 64)
4311 else if (chan <= 128)
4315 run_rt3070_rf_write(sc, 19, rf);
4320 else if (chan <= 64)
4322 else if (chan <= 128)
4326 run_rt3070_rf_write(sc, 20, rf);
4331 else if (chan <= 64)
4335 run_rt3070_rf_write(sc, 25, rf);
4338 run_rt3070_rf_write(sc, 26, (chan <= 14) ? 0x85 : 0x87);
4340 run_rt3070_rf_write(sc, 27, (chan <= 14) ? 0x00 : 0x01);
4342 run_rt3070_rf_write(sc, 29, (chan <= 14) ? 0x9b : 0x9f);
4344 run_read(sc, RT2860_GPIO_CTRL, &tmp);
4348 run_write(sc, RT2860_GPIO_CTRL, tmp);
4350 /* enable RF tuning */
4351 run_rt3070_rf_read(sc, 7, &rf);
4352 run_rt3070_rf_write(sc, 7, rf | 0x01);
4358 run_rt3593_set_chan(struct run_softc *sc, u_int chan)
4360 int8_t txpow1, txpow2, txpow3;
4364 /* find the settings for this channel (we know it exists) */
4365 for (i = 0; rt2860_rf2850[i].chan != chan; i++);
4367 /* use Tx power values from EEPROM */
4368 txpow1 = sc->txpow1[i];
4369 txpow2 = sc->txpow2[i];
4370 txpow3 = (sc->ntxchains == 3) ? sc->txpow3[i] : 0;
4373 run_bbp_write(sc, 25, sc->bbp25);
4374 run_bbp_write(sc, 26, sc->bbp26);
4376 /* Enable IQ phase correction. */
4377 run_bbp_write(sc, 25, 0x09);
4378 run_bbp_write(sc, 26, 0xff);
4381 run_rt3070_rf_write(sc, 8, rt3070_freqs[i].n);
4382 run_rt3070_rf_write(sc, 9, rt3070_freqs[i].k & 0x0f);
4383 run_rt3070_rf_read(sc, 11, &rf);
4384 rf = (rf & ~0x03) | (rt3070_freqs[i].r & 0x03);
4385 run_rt3070_rf_write(sc, 11, rf);
4388 run_rt3070_rf_read(sc, 11, &rf);
4390 rf |= (chan <= 14) ? 0x44 : 0x48;
4391 run_rt3070_rf_write(sc, 11, rf);
4396 rf = 0x40 | ((txpow1 & 0x18) << 1) | (txpow1 & 0x07);
4397 run_rt3070_rf_write(sc, 53, rf);
4402 rf = 0x40 | ((txpow2 & 0x18) << 1) | (txpow2 & 0x07);
4403 run_rt3070_rf_write(sc, 55, rf);
4408 rf = 0x40 | ((txpow3 & 0x18) << 1) | (txpow3 & 0x07);
4409 run_rt3070_rf_write(sc, 54, rf);
4411 rf = RT3070_RF_BLOCK | RT3070_PLL_PD;
4412 if (sc->ntxchains == 3)
4413 rf |= RT3070_TX0_PD | RT3070_TX1_PD | RT3070_TX2_PD;
4415 rf |= RT3070_TX0_PD | RT3070_TX1_PD;
4416 rf |= RT3070_RX0_PD | RT3070_RX1_PD | RT3070_RX2_PD;
4417 run_rt3070_rf_write(sc, 1, rf);
4419 run_adjust_freq_offset(sc);
4421 run_rt3070_rf_write(sc, 31, (chan <= 14) ? 0xa0 : 0x80);
4423 h20mhz = (sc->rf24_20mhz & 0x20) >> 5;
4424 run_rt3070_rf_read(sc, 30, &rf);
4425 rf = (rf & ~0x06) | (h20mhz << 1) | (h20mhz << 2);
4426 run_rt3070_rf_write(sc, 30, rf);
4428 run_rt3070_rf_read(sc, 36, &rf);
4433 run_rt3070_rf_write(sc, 36, rf);
4436 run_rt3070_rf_write(sc, 34, (chan <= 14) ? 0x3c : 0x20);
4437 /* Set pfd_delay. */
4438 run_rt3070_rf_write(sc, 12, (chan <= 14) ? 0x1a : 0x12);
4440 /* Set vco bias current control. */
4441 run_rt3070_rf_read(sc, 6, &rf);
4445 else if (chan <= 128)
4449 run_rt3070_rf_write(sc, 6, rf);
4451 run_rt3070_rf_read(sc, 30, &rf);
4452 rf = (rf & ~0x18) | 0x10;
4453 run_rt3070_rf_write(sc, 30, rf);
4455 run_rt3070_rf_write(sc, 10, (chan <= 14) ? 0xd3 : 0xd8);
4456 run_rt3070_rf_write(sc, 13, (chan <= 14) ? 0x12 : 0x23);
4458 run_rt3070_rf_read(sc, 51, &rf);
4459 rf = (rf & ~0x03) | 0x01;
4460 run_rt3070_rf_write(sc, 51, rf);
4461 /* Set tx_mx1_cc. */
4462 run_rt3070_rf_read(sc, 51, &rf);
4464 rf |= (chan <= 14) ? 0x14 : 0x10;
4465 run_rt3070_rf_write(sc, 51, rf);
4466 /* Set tx_mx1_ic. */
4467 run_rt3070_rf_read(sc, 51, &rf);
4469 rf |= (chan <= 14) ? 0x60 : 0x40;
4470 run_rt3070_rf_write(sc, 51, rf);
4471 /* Set tx_lo1_ic. */
4472 run_rt3070_rf_read(sc, 49, &rf);
4474 rf |= (chan <= 14) ? 0x0c : 0x08;
4475 run_rt3070_rf_write(sc, 49, rf);
4476 /* Set tx_lo1_en. */
4477 run_rt3070_rf_read(sc, 50, &rf);
4478 run_rt3070_rf_write(sc, 50, rf & ~0x20);
4480 run_rt3070_rf_read(sc, 57, &rf);
4482 rf |= (chan <= 14) ? 0x6c : 0x3c;
4483 run_rt3070_rf_write(sc, 57, rf);
4484 /* Set rx_mix1_ic, rxa_lnactr, lna_vc, lna_inbias_en and lna_en. */
4485 run_rt3070_rf_write(sc, 44, (chan <= 14) ? 0x93 : 0x9b);
4486 /* Set drv_gnd_a, tx_vga_cc_a and tx_mx2_gain. */
4487 run_rt3070_rf_write(sc, 52, (chan <= 14) ? 0x45 : 0x05);
4488 /* Enable VCO calibration. */
4489 run_rt3070_rf_read(sc, 3, &rf);
4490 rf &= ~RT5390_VCOCAL;
4491 rf |= (chan <= 14) ? RT5390_VCOCAL : 0xbe;
4492 run_rt3070_rf_write(sc, 3, rf);
4496 else if (chan <= 64)
4498 else if (chan <= 128)
4502 run_rt3070_rf_write(sc, 39, rf);
4505 else if (chan <= 64)
4507 else if (chan <= 128)
4511 run_rt3070_rf_write(sc, 45, rf);
4513 /* Set FEQ/AEQ control. */
4514 run_bbp_write(sc, 105, 0x34);
4518 run_rt5390_set_chan(struct run_softc *sc, u_int chan)
4520 int8_t txpow1, txpow2;
4524 /* find the settings for this channel (we know it exists) */
4525 for (i = 0; rt2860_rf2850[i].chan != chan; i++);
4527 /* use Tx power values from EEPROM */
4528 txpow1 = sc->txpow1[i];
4529 txpow2 = sc->txpow2[i];
4531 run_rt3070_rf_write(sc, 8, rt3070_freqs[i].n);
4532 run_rt3070_rf_write(sc, 9, rt3070_freqs[i].k & 0x0f);
4533 run_rt3070_rf_read(sc, 11, &rf);
4534 rf = (rf & ~0x03) | (rt3070_freqs[i].r & 0x03);
4535 run_rt3070_rf_write(sc, 11, rf);
4537 run_rt3070_rf_read(sc, 49, &rf);
4538 rf = (rf & ~0x3f) | (txpow1 & 0x3f);
4539 /* The valid range of the RF R49 is 0x00 to 0x27. */
4540 if ((rf & 0x3f) > 0x27)
4541 rf = (rf & ~0x3f) | 0x27;
4542 run_rt3070_rf_write(sc, 49, rf);
4544 if (sc->mac_ver == 0x5392) {
4545 run_rt3070_rf_read(sc, 50, &rf);
4546 rf = (rf & ~0x3f) | (txpow2 & 0x3f);
4547 /* The valid range of the RF R50 is 0x00 to 0x27. */
4548 if ((rf & 0x3f) > 0x27)
4549 rf = (rf & ~0x3f) | 0x27;
4550 run_rt3070_rf_write(sc, 50, rf);
4553 run_rt3070_rf_read(sc, 1, &rf);
4554 rf |= RT3070_RF_BLOCK | RT3070_PLL_PD | RT3070_RX0_PD | RT3070_TX0_PD;
4555 if (sc->mac_ver == 0x5392)
4556 rf |= RT3070_RX1_PD | RT3070_TX1_PD;
4557 run_rt3070_rf_write(sc, 1, rf);
4559 if (sc->mac_ver != 0x5392) {
4560 run_rt3070_rf_read(sc, 2, &rf);
4562 run_rt3070_rf_write(sc, 2, rf);
4565 run_rt3070_rf_write(sc, 2, rf);
4568 run_adjust_freq_offset(sc);
4570 if (sc->mac_ver == 0x5392) {
4571 /* Fix for RT5392C. */
4572 if (sc->mac_rev >= 0x0223) {
4575 else if (chan >= 5 && chan <= 7)
4579 run_rt3070_rf_write(sc, 23, rf);
4585 else if (chan >= 6 && chan <= 7)
4587 else if (chan >= 8 && chan <= 10)
4591 run_rt3070_rf_write(sc, 59, rf);
4597 run_rt3070_rf_write(sc, 59, rf);
4600 /* Fix for RT5390F. */
4601 if (sc->mac_rev >= 0x0502) {
4606 run_rt3070_rf_write(sc, 55, rf);
4610 else if (chan == 12)
4614 run_rt3070_rf_write(sc, 59, rf);
4616 run_rt3070_rf_write(sc, 55, 0x44);
4617 run_rt3070_rf_write(sc, 59, 0x8f);
4621 /* Enable VCO calibration. */
4622 run_rt3070_rf_read(sc, 3, &rf);
4623 rf |= RT5390_VCOCAL;
4624 run_rt3070_rf_write(sc, 3, rf);
4628 run_rt5592_set_chan(struct run_softc *sc, u_int chan)
4630 const struct rt5592_freqs *freqs;
4632 uint8_t reg, rf, txpow_bound;
4633 int8_t txpow1, txpow2;
4636 run_read(sc, RT5592_DEBUG_INDEX, &tmp);
4637 freqs = (tmp & RT5592_SEL_XTAL) ?
4638 rt5592_freqs_40mhz : rt5592_freqs_20mhz;
4640 /* find the settings for this channel (we know it exists) */
4641 for (i = 0; rt2860_rf2850[i].chan != chan; i++, freqs++);
4643 /* use Tx power values from EEPROM */
4644 txpow1 = sc->txpow1[i];
4645 txpow2 = sc->txpow2[i];
4647 run_read(sc, RT3070_LDO_CFG0, &tmp);
4651 run_write(sc, RT3070_LDO_CFG0, tmp);
4654 run_rt3070_rf_write(sc, 8, freqs->n & 0xff);
4655 run_rt3070_rf_read(sc, 9, &rf);
4657 rf |= ((freqs->n & 0x0100) >> 8) << 4;
4658 run_rt3070_rf_write(sc, 9, rf);
4661 run_rt3070_rf_read(sc, 9, &rf);
4663 rf |= (freqs->k & 0x0f);
4664 run_rt3070_rf_write(sc, 9, rf);
4667 run_rt3070_rf_read(sc, 11, &rf);
4669 rf |= ((freqs->m - 0x8) & 0x3) << 2;
4670 run_rt3070_rf_write(sc, 11, rf);
4671 run_rt3070_rf_read(sc, 9, &rf);
4673 rf |= (((freqs->m - 0x8) & 0x4) >> 2) << 7;
4674 run_rt3070_rf_write(sc, 9, rf);
4677 run_rt3070_rf_read(sc, 11, &rf);
4679 rf |= (freqs->r - 0x1);
4680 run_rt3070_rf_write(sc, 11, rf);
4683 /* Initialize RF registers for 2GHZ. */
4684 for (i = 0; i < nitems(rt5592_2ghz_def_rf); i++) {
4685 run_rt3070_rf_write(sc, rt5592_2ghz_def_rf[i].reg,
4686 rt5592_2ghz_def_rf[i].val);
4689 rf = (chan <= 10) ? 0x07 : 0x06;
4690 run_rt3070_rf_write(sc, 23, rf);
4691 run_rt3070_rf_write(sc, 59, rf);
4693 run_rt3070_rf_write(sc, 55, 0x43);
4696 * RF R49/R50 Tx power ALC code.
4697 * G-band bit<7:6>=1:0, bit<5:0> range from 0x0 ~ 0x27.
4702 /* Initialize RF registers for 5GHZ. */
4703 for (i = 0; i < nitems(rt5592_5ghz_def_rf); i++) {
4704 run_rt3070_rf_write(sc, rt5592_5ghz_def_rf[i].reg,
4705 rt5592_5ghz_def_rf[i].val);
4707 for (i = 0; i < nitems(rt5592_chan_5ghz); i++) {
4708 if (chan >= rt5592_chan_5ghz[i].firstchan &&
4709 chan <= rt5592_chan_5ghz[i].lastchan) {
4710 run_rt3070_rf_write(sc, rt5592_chan_5ghz[i].reg,
4711 rt5592_chan_5ghz[i].val);
4716 * RF R49/R50 Tx power ALC code.
4717 * A-band bit<7:6>=1:1, bit<5:0> range from 0x0 ~ 0x2b.
4723 /* RF R49 ch0 Tx power ALC code. */
4724 run_rt3070_rf_read(sc, 49, &rf);
4727 rf = (rf & ~0x3f) | (txpow1 & 0x3f);
4728 if ((rf & 0x3f) > txpow_bound)
4729 rf = (rf & ~0x3f) | txpow_bound;
4730 run_rt3070_rf_write(sc, 49, rf);
4732 /* RF R50 ch1 Tx power ALC code. */
4733 run_rt3070_rf_read(sc, 50, &rf);
4734 rf &= ~(1 << 7 | 1 << 6);
4736 rf = (rf & ~0x3f) | (txpow2 & 0x3f);
4737 if ((rf & 0x3f) > txpow_bound)
4738 rf = (rf & ~0x3f) | txpow_bound;
4739 run_rt3070_rf_write(sc, 50, rf);
4741 /* Enable RF_BLOCK, PLL_PD, RX0_PD, and TX0_PD. */
4742 run_rt3070_rf_read(sc, 1, &rf);
4743 rf |= (RT3070_RF_BLOCK | RT3070_PLL_PD | RT3070_RX0_PD | RT3070_TX0_PD);
4744 if (sc->ntxchains > 1)
4745 rf |= RT3070_TX1_PD;
4746 if (sc->nrxchains > 1)
4747 rf |= RT3070_RX1_PD;
4748 run_rt3070_rf_write(sc, 1, rf);
4750 run_rt3070_rf_write(sc, 6, 0xe4);
4752 run_rt3070_rf_write(sc, 30, 0x10);
4753 run_rt3070_rf_write(sc, 31, 0x80);
4754 run_rt3070_rf_write(sc, 32, 0x80);
4756 run_adjust_freq_offset(sc);
4758 /* Enable VCO calibration. */
4759 run_rt3070_rf_read(sc, 3, &rf);
4760 rf |= RT5390_VCOCAL;
4761 run_rt3070_rf_write(sc, 3, rf);
4765 run_set_rx_antenna(struct run_softc *sc, int aux)
4771 if (sc->rf_rev == RT5390_RF_5370) {
4772 run_bbp_read(sc, 152, &bbp152);
4773 run_bbp_write(sc, 152, bbp152 & ~0x80);
4775 run_mcu_cmd(sc, RT2860_MCU_CMD_ANTSEL, 0);
4776 run_read(sc, RT2860_GPIO_CTRL, &tmp);
4777 run_write(sc, RT2860_GPIO_CTRL, (tmp & ~0x0808) | 0x08);
4780 if (sc->rf_rev == RT5390_RF_5370) {
4781 run_bbp_read(sc, 152, &bbp152);
4782 run_bbp_write(sc, 152, bbp152 | 0x80);
4784 run_mcu_cmd(sc, RT2860_MCU_CMD_ANTSEL, 1);
4785 run_read(sc, RT2860_GPIO_CTRL, &tmp);
4786 run_write(sc, RT2860_GPIO_CTRL, tmp & ~0x0808);
4792 run_set_chan(struct run_softc *sc, struct ieee80211_channel *c)
4794 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
4797 chan = ieee80211_chan2ieee(ic, c);
4798 if (chan == 0 || chan == IEEE80211_CHAN_ANY)
4801 if (sc->mac_ver == 0x5592)
4802 run_rt5592_set_chan(sc, chan);
4803 else if (sc->mac_ver >= 0x5390)
4804 run_rt5390_set_chan(sc, chan);
4805 else if (sc->mac_ver == 0x3593)
4806 run_rt3593_set_chan(sc, chan);
4807 else if (sc->mac_ver == 0x3572)
4808 run_rt3572_set_chan(sc, chan);
4809 else if (sc->mac_ver >= 0x3070)
4810 run_rt3070_set_chan(sc, chan);
4812 run_rt2870_set_chan(sc, chan);
4814 /* determine channel group */
4817 else if (chan <= 64)
4819 else if (chan <= 128)
4824 /* XXX necessary only when group has changed! */
4825 run_select_chan_group(sc, group);
4829 /* Perform IQ calibration. */
4830 if (sc->mac_ver >= 0x5392)
4831 run_iq_calib(sc, chan);
4837 run_set_channel(struct ieee80211com *ic)
4839 struct run_softc *sc = ic->ic_ifp->if_softc;
4842 run_set_chan(sc, ic->ic_curchan);
4849 run_scan_start(struct ieee80211com *ic)
4851 struct run_softc *sc = ic->ic_ifp->if_softc;
4856 /* abort TSF synchronization */
4857 run_read(sc, RT2860_BCN_TIME_CFG, &tmp);
4858 run_write(sc, RT2860_BCN_TIME_CFG,
4859 tmp & ~(RT2860_BCN_TX_EN | RT2860_TSF_TIMER_EN |
4860 RT2860_TBTT_TIMER_EN));
4861 run_set_bssid(sc, sc->sc_ifp->if_broadcastaddr);
4869 run_scan_end(struct ieee80211com *ic)
4871 struct run_softc *sc = ic->ic_ifp->if_softc;
4875 run_enable_tsf_sync(sc);
4876 /* XXX keep local copy */
4877 run_set_bssid(sc, sc->sc_bssid);
4885 * Could be called from ieee80211_node_timeout()
4886 * (non-sleepable thread)
4889 run_update_beacon(struct ieee80211vap *vap, int item)
4891 struct ieee80211com *ic = vap->iv_ic;
4892 struct run_softc *sc = ic->ic_ifp->if_softc;
4893 struct run_vap *rvp = RUN_VAP(vap);
4897 KASSERT(vap != NULL, ("no beacon"));
4900 case IEEE80211_BEACON_ERP:
4901 run_updateslot(ic->ic_ifp);
4903 case IEEE80211_BEACON_HTINFO:
4906 case IEEE80211_BEACON_TIM:
4913 setbit(rvp->bo.bo_flags, item);
4914 ieee80211_beacon_update(vap->iv_bss, &rvp->bo, rvp->beacon_mbuf, mcast);
4916 i = RUN_CMDQ_GET(&sc->cmdq_store);
4917 DPRINTF("cmdq_store=%d\n", i);
4918 sc->cmdq[i].func = run_update_beacon_cb;
4919 sc->cmdq[i].arg0 = vap;
4920 ieee80211_runtask(ic, &sc->cmdq_task);
4926 run_update_beacon_cb(void *arg)
4928 struct ieee80211vap *vap = arg;
4929 struct run_vap *rvp = RUN_VAP(vap);
4930 struct ieee80211com *ic = vap->iv_ic;
4931 struct run_softc *sc = ic->ic_ifp->if_softc;
4932 struct rt2860_txwi txwi;
4937 if (vap->iv_bss->ni_chan == IEEE80211_CHAN_ANYC)
4939 if (ic->ic_bsschan == IEEE80211_CHAN_ANYC)
4943 * No need to call ieee80211_beacon_update(), run_update_beacon()
4944 * is taking care of apropriate calls.
4946 if (rvp->beacon_mbuf == NULL) {
4947 rvp->beacon_mbuf = ieee80211_beacon_alloc(vap->iv_bss,
4949 if (rvp->beacon_mbuf == NULL)
4952 m = rvp->beacon_mbuf;
4954 memset(&txwi, 0, sizeof(txwi));
4956 txwi.len = htole16(m->m_pkthdr.len);
4958 /* send beacons at the lowest available rate */
4959 ridx = (ic->ic_curmode == IEEE80211_MODE_11A) ?
4960 RT2860_RIDX_OFDM6 : RT2860_RIDX_CCK1;
4961 txwi.phy = htole16(rt2860_rates[ridx].mcs);
4962 if (rt2860_rates[ridx].phy == IEEE80211_T_OFDM)
4963 txwi.phy |= htole16(RT2860_PHY_OFDM);
4964 txwi.txop = RT2860_TX_TXOP_HT;
4965 txwi.flags = RT2860_TX_TS;
4966 txwi.xflags = RT2860_TX_NSEQ;
4968 txwisize = (sc->mac_ver == 0x5592) ?
4969 sizeof(txwi) + sizeof(uint32_t) : sizeof(txwi);
4970 run_write_region_1(sc, RT2860_BCN_BASE(rvp->rvp_id), (uint8_t *)&txwi,
4972 run_write_region_1(sc, RT2860_BCN_BASE(rvp->rvp_id) + txwisize,
4973 mtod(m, uint8_t *), (m->m_pkthdr.len + 1) & ~1);
4977 run_updateprot(struct ieee80211com *ic)
4979 struct run_softc *sc = ic->ic_ifp->if_softc;
4982 i = RUN_CMDQ_GET(&sc->cmdq_store);
4983 DPRINTF("cmdq_store=%d\n", i);
4984 sc->cmdq[i].func = run_updateprot_cb;
4985 sc->cmdq[i].arg0 = ic;
4986 ieee80211_runtask(ic, &sc->cmdq_task);
4990 run_updateprot_cb(void *arg)
4992 struct ieee80211com *ic = arg;
4993 struct run_softc *sc = ic->ic_ifp->if_softc;
4996 tmp = RT2860_RTSTH_EN | RT2860_PROT_NAV_SHORT | RT2860_TXOP_ALLOW_ALL;
4997 /* setup protection frame rate (MCS code) */
4998 tmp |= (ic->ic_curmode == IEEE80211_MODE_11A) ?
4999 rt2860_rates[RT2860_RIDX_OFDM6].mcs :
5000 rt2860_rates[RT2860_RIDX_CCK11].mcs;
5002 /* CCK frames don't require protection */
5003 run_write(sc, RT2860_CCK_PROT_CFG, tmp);
5004 if (ic->ic_flags & IEEE80211_F_USEPROT) {
5005 if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
5006 tmp |= RT2860_PROT_CTRL_RTS_CTS;
5007 else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
5008 tmp |= RT2860_PROT_CTRL_CTS;
5010 run_write(sc, RT2860_OFDM_PROT_CFG, tmp);
5014 run_usb_timeout_cb(void *arg)
5016 struct ieee80211vap *vap = arg;
5017 struct run_softc *sc = vap->iv_ic->ic_ifp->if_softc;
5019 RUN_LOCK_ASSERT(sc, MA_OWNED);
5021 if(vap->iv_state == IEEE80211_S_RUN &&
5022 vap->iv_opmode != IEEE80211_M_STA)
5023 run_reset_livelock(sc);
5024 else if (vap->iv_state == IEEE80211_S_SCAN) {
5025 DPRINTF("timeout caused by scan\n");
5027 ieee80211_cancel_scan(vap);
5029 DPRINTF("timeout by unknown cause\n");
5033 run_reset_livelock(struct run_softc *sc)
5037 RUN_LOCK_ASSERT(sc, MA_OWNED);
5040 * In IBSS or HostAP modes (when the hardware sends beacons), the MAC
5041 * can run into a livelock and start sending CTS-to-self frames like
5042 * crazy if protection is enabled. Reset MAC/BBP for a while
5044 run_read(sc, RT2860_DEBUG, &tmp);
5045 DPRINTFN(3, "debug reg %08x\n", tmp);
5046 if ((tmp & (1 << 29)) && (tmp & (1 << 7 | 1 << 5))) {
5047 DPRINTF("CTS-to-self livelock detected\n");
5048 run_write(sc, RT2860_MAC_SYS_CTRL, RT2860_MAC_SRST);
5050 run_write(sc, RT2860_MAC_SYS_CTRL,
5051 RT2860_MAC_RX_EN | RT2860_MAC_TX_EN);
5056 run_update_promisc_locked(struct ifnet *ifp)
5058 struct run_softc *sc = ifp->if_softc;
5061 run_read(sc, RT2860_RX_FILTR_CFG, &tmp);
5063 tmp |= RT2860_DROP_UC_NOME;
5064 if (ifp->if_flags & IFF_PROMISC)
5065 tmp &= ~RT2860_DROP_UC_NOME;
5067 run_write(sc, RT2860_RX_FILTR_CFG, tmp);
5069 DPRINTF("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
5070 "entering" : "leaving");
5074 run_update_promisc(struct ifnet *ifp)
5076 struct run_softc *sc = ifp->if_softc;
5078 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
5082 run_update_promisc_locked(ifp);
5087 run_enable_tsf_sync(struct run_softc *sc)
5089 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
5090 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
5093 DPRINTF("rvp_id=%d ic_opmode=%d\n", RUN_VAP(vap)->rvp_id,
5096 run_read(sc, RT2860_BCN_TIME_CFG, &tmp);
5098 tmp |= vap->iv_bss->ni_intval * 16;
5099 tmp |= RT2860_TSF_TIMER_EN | RT2860_TBTT_TIMER_EN;
5101 if (ic->ic_opmode == IEEE80211_M_STA) {
5103 * Local TSF is always updated with remote TSF on beacon
5106 tmp |= 1 << RT2860_TSF_SYNC_MODE_SHIFT;
5107 } else if (ic->ic_opmode == IEEE80211_M_IBSS) {
5108 tmp |= RT2860_BCN_TX_EN;
5110 * Local TSF is updated with remote TSF on beacon reception
5111 * only if the remote TSF is greater than local TSF.
5113 tmp |= 2 << RT2860_TSF_SYNC_MODE_SHIFT;
5114 } else if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
5115 ic->ic_opmode == IEEE80211_M_MBSS) {
5116 tmp |= RT2860_BCN_TX_EN;
5117 /* SYNC with nobody */
5118 tmp |= 3 << RT2860_TSF_SYNC_MODE_SHIFT;
5120 DPRINTF("Enabling TSF failed. undefined opmode\n");
5124 run_write(sc, RT2860_BCN_TIME_CFG, tmp);
5128 run_enable_mrr(struct run_softc *sc)
5130 #define CCK(mcs) (mcs)
5131 #define OFDM(mcs) (1 << 3 | (mcs))
5132 run_write(sc, RT2860_LG_FBK_CFG0,
5133 OFDM(6) << 28 | /* 54->48 */
5134 OFDM(5) << 24 | /* 48->36 */
5135 OFDM(4) << 20 | /* 36->24 */
5136 OFDM(3) << 16 | /* 24->18 */
5137 OFDM(2) << 12 | /* 18->12 */
5138 OFDM(1) << 8 | /* 12-> 9 */
5139 OFDM(0) << 4 | /* 9-> 6 */
5140 OFDM(0)); /* 6-> 6 */
5142 run_write(sc, RT2860_LG_FBK_CFG1,
5143 CCK(2) << 12 | /* 11->5.5 */
5144 CCK(1) << 8 | /* 5.5-> 2 */
5145 CCK(0) << 4 | /* 2-> 1 */
5146 CCK(0)); /* 1-> 1 */
5152 run_set_txpreamble(struct run_softc *sc)
5154 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
5157 run_read(sc, RT2860_AUTO_RSP_CFG, &tmp);
5158 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
5159 tmp |= RT2860_CCK_SHORT_EN;
5161 tmp &= ~RT2860_CCK_SHORT_EN;
5162 run_write(sc, RT2860_AUTO_RSP_CFG, tmp);
5166 run_set_basicrates(struct run_softc *sc)
5168 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
5170 /* set basic rates mask */
5171 if (ic->ic_curmode == IEEE80211_MODE_11B)
5172 run_write(sc, RT2860_LEGACY_BASIC_RATE, 0x003);
5173 else if (ic->ic_curmode == IEEE80211_MODE_11A)
5174 run_write(sc, RT2860_LEGACY_BASIC_RATE, 0x150);
5176 run_write(sc, RT2860_LEGACY_BASIC_RATE, 0x15f);
5180 run_set_leds(struct run_softc *sc, uint16_t which)
5182 (void)run_mcu_cmd(sc, RT2860_MCU_CMD_LEDS,
5183 which | (sc->leds & 0x7f));
5187 run_set_bssid(struct run_softc *sc, const uint8_t *bssid)
5189 run_write(sc, RT2860_MAC_BSSID_DW0,
5190 bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24);
5191 run_write(sc, RT2860_MAC_BSSID_DW1,
5192 bssid[4] | bssid[5] << 8);
5196 run_set_macaddr(struct run_softc *sc, const uint8_t *addr)
5198 run_write(sc, RT2860_MAC_ADDR_DW0,
5199 addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
5200 run_write(sc, RT2860_MAC_ADDR_DW1,
5201 addr[4] | addr[5] << 8 | 0xff << 16);
5205 run_updateslot(struct ifnet *ifp)
5207 struct run_softc *sc = ifp->if_softc;
5208 struct ieee80211com *ic = ifp->if_l2com;
5211 i = RUN_CMDQ_GET(&sc->cmdq_store);
5212 DPRINTF("cmdq_store=%d\n", i);
5213 sc->cmdq[i].func = run_updateslot_cb;
5214 sc->cmdq[i].arg0 = ifp;
5215 ieee80211_runtask(ic, &sc->cmdq_task);
5222 run_updateslot_cb(void *arg)
5224 struct ifnet *ifp = arg;
5225 struct run_softc *sc = ifp->if_softc;
5226 struct ieee80211com *ic = ifp->if_l2com;
5229 run_read(sc, RT2860_BKOFF_SLOT_CFG, &tmp);
5231 tmp |= (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
5232 run_write(sc, RT2860_BKOFF_SLOT_CFG, tmp);
5236 run_update_mcast(struct ifnet *ifp)
5238 /* h/w filter supports getting everything or nothing */
5239 ifp->if_flags |= IFF_ALLMULTI;
5243 run_rssi2dbm(struct run_softc *sc, uint8_t rssi, uint8_t rxchain)
5245 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
5246 struct ieee80211_channel *c = ic->ic_curchan;
5249 if (IEEE80211_IS_CHAN_5GHZ(c)) {
5250 u_int chan = ieee80211_chan2ieee(ic, c);
5251 delta = sc->rssi_5ghz[rxchain];
5253 /* determine channel group */
5255 delta -= sc->lna[1];
5256 else if (chan <= 128)
5257 delta -= sc->lna[2];
5259 delta -= sc->lna[3];
5261 delta = sc->rssi_2ghz[rxchain] - sc->lna[0];
5263 return (-12 - delta - rssi);
5267 run_rt5390_bbp_init(struct run_softc *sc)
5272 /* Apply maximum likelihood detection for 2 stream case. */
5273 run_bbp_read(sc, 105, &bbp);
5274 if (sc->nrxchains > 1)
5275 run_bbp_write(sc, 105, bbp | RT5390_MLD);
5277 /* Avoid data lost and CRC error. */
5278 run_bbp_read(sc, 4, &bbp);
5279 run_bbp_write(sc, 4, bbp | RT5390_MAC_IF_CTRL);
5281 if (sc->mac_ver == 0x5592) {
5282 for (i = 0; i < nitems(rt5592_def_bbp); i++) {
5283 run_bbp_write(sc, rt5592_def_bbp[i].reg,
5284 rt5592_def_bbp[i].val);
5286 for (i = 0; i < nitems(rt5592_bbp_r196); i++) {
5287 run_bbp_write(sc, 195, i + 0x80);
5288 run_bbp_write(sc, 196, rt5592_bbp_r196[i]);
5291 for (i = 0; i < nitems(rt5390_def_bbp); i++) {
5292 run_bbp_write(sc, rt5390_def_bbp[i].reg,
5293 rt5390_def_bbp[i].val);
5296 if (sc->mac_ver == 0x5392) {
5297 run_bbp_write(sc, 88, 0x90);
5298 run_bbp_write(sc, 95, 0x9a);
5299 run_bbp_write(sc, 98, 0x12);
5300 run_bbp_write(sc, 106, 0x12);
5301 run_bbp_write(sc, 134, 0xd0);
5302 run_bbp_write(sc, 135, 0xf6);
5303 run_bbp_write(sc, 148, 0x84);
5306 run_bbp_read(sc, 152, &bbp);
5307 run_bbp_write(sc, 152, bbp | 0x80);
5309 /* Fix BBP254 for RT5592C. */
5310 if (sc->mac_ver == 0x5592 && sc->mac_rev >= 0x0221) {
5311 run_bbp_read(sc, 254, &bbp);
5312 run_bbp_write(sc, 254, bbp | 0x80);
5315 /* Disable hardware antenna diversity. */
5316 if (sc->mac_ver == 0x5390)
5317 run_bbp_write(sc, 154, 0);
5319 /* Initialize Rx CCK/OFDM frequency offset report. */
5320 run_bbp_write(sc, 142, 1);
5321 run_bbp_write(sc, 143, 57);
5325 run_bbp_init(struct run_softc *sc)
5327 int i, error, ntries;
5330 /* wait for BBP to wake up */
5331 for (ntries = 0; ntries < 20; ntries++) {
5332 if ((error = run_bbp_read(sc, 0, &bbp0)) != 0)
5334 if (bbp0 != 0 && bbp0 != 0xff)
5340 /* initialize BBP registers to default values */
5341 if (sc->mac_ver >= 0x5390)
5342 run_rt5390_bbp_init(sc);
5344 for (i = 0; i < nitems(rt2860_def_bbp); i++) {
5345 run_bbp_write(sc, rt2860_def_bbp[i].reg,
5346 rt2860_def_bbp[i].val);
5350 if (sc->mac_ver == 0x3593) {
5351 run_bbp_write(sc, 79, 0x13);
5352 run_bbp_write(sc, 80, 0x05);
5353 run_bbp_write(sc, 81, 0x33);
5354 run_bbp_write(sc, 86, 0x46);
5355 run_bbp_write(sc, 137, 0x0f);
5358 /* fix BBP84 for RT2860E */
5359 if (sc->mac_ver == 0x2860 && sc->mac_rev != 0x0101)
5360 run_bbp_write(sc, 84, 0x19);
5362 if (sc->mac_ver >= 0x3070 && (sc->mac_ver != 0x3593 &&
5363 sc->mac_ver != 0x5592)) {
5364 run_bbp_write(sc, 79, 0x13);
5365 run_bbp_write(sc, 80, 0x05);
5366 run_bbp_write(sc, 81, 0x33);
5367 } else if (sc->mac_ver == 0x2860 && sc->mac_rev == 0x0100) {
5368 run_bbp_write(sc, 69, 0x16);
5369 run_bbp_write(sc, 73, 0x12);
5375 run_rt3070_rf_init(struct run_softc *sc)
5378 uint8_t bbp4, mingain, rf, target;
5381 run_rt3070_rf_read(sc, 30, &rf);
5382 /* toggle RF R30 bit 7 */
5383 run_rt3070_rf_write(sc, 30, rf | 0x80);
5385 run_rt3070_rf_write(sc, 30, rf & ~0x80);
5387 /* initialize RF registers to default value */
5388 if (sc->mac_ver == 0x3572) {
5389 for (i = 0; i < nitems(rt3572_def_rf); i++) {
5390 run_rt3070_rf_write(sc, rt3572_def_rf[i].reg,
5391 rt3572_def_rf[i].val);
5394 for (i = 0; i < nitems(rt3070_def_rf); i++) {
5395 run_rt3070_rf_write(sc, rt3070_def_rf[i].reg,
5396 rt3070_def_rf[i].val);
5400 if (sc->mac_ver == 0x3070 && sc->mac_rev < 0x0201) {
5402 * Change voltage from 1.2V to 1.35V for RT3070.
5403 * The DAC issue (RT3070_LDO_CFG0) has been fixed
5406 run_read(sc, RT3070_LDO_CFG0, &tmp);
5407 tmp = (tmp & ~0x0f000000) | 0x0d000000;
5408 run_write(sc, RT3070_LDO_CFG0, tmp);
5410 } else if (sc->mac_ver == 0x3071) {
5411 run_rt3070_rf_read(sc, 6, &rf);
5412 run_rt3070_rf_write(sc, 6, rf | 0x40);
5413 run_rt3070_rf_write(sc, 31, 0x14);
5415 run_read(sc, RT3070_LDO_CFG0, &tmp);
5417 if (sc->mac_rev < 0x0211)
5418 tmp |= 0x0d000000; /* 1.3V */
5420 tmp |= 0x01000000; /* 1.2V */
5421 run_write(sc, RT3070_LDO_CFG0, tmp);
5423 /* patch LNA_PE_G1 */
5424 run_read(sc, RT3070_GPIO_SWITCH, &tmp);
5425 run_write(sc, RT3070_GPIO_SWITCH, tmp & ~0x20);
5427 } else if (sc->mac_ver == 0x3572) {
5428 run_rt3070_rf_read(sc, 6, &rf);
5429 run_rt3070_rf_write(sc, 6, rf | 0x40);
5431 /* increase voltage from 1.2V to 1.35V */
5432 run_read(sc, RT3070_LDO_CFG0, &tmp);
5433 tmp = (tmp & ~0x1f000000) | 0x0d000000;
5434 run_write(sc, RT3070_LDO_CFG0, tmp);
5436 if (sc->mac_rev < 0x0211 || !sc->patch_dac) {
5437 run_delay(sc, 1); /* wait for 1msec */
5438 /* decrease voltage back to 1.2V */
5439 tmp = (tmp & ~0x1f000000) | 0x01000000;
5440 run_write(sc, RT3070_LDO_CFG0, tmp);
5444 /* select 20MHz bandwidth */
5445 run_rt3070_rf_read(sc, 31, &rf);
5446 run_rt3070_rf_write(sc, 31, rf & ~0x20);
5448 /* calibrate filter for 20MHz bandwidth */
5449 sc->rf24_20mhz = 0x1f; /* default value */
5450 target = (sc->mac_ver < 0x3071) ? 0x16 : 0x13;
5451 run_rt3070_filter_calib(sc, 0x07, target, &sc->rf24_20mhz);
5453 /* select 40MHz bandwidth */
5454 run_bbp_read(sc, 4, &bbp4);
5455 run_bbp_write(sc, 4, (bbp4 & ~0x18) | 0x10);
5456 run_rt3070_rf_read(sc, 31, &rf);
5457 run_rt3070_rf_write(sc, 31, rf | 0x20);
5459 /* calibrate filter for 40MHz bandwidth */
5460 sc->rf24_40mhz = 0x2f; /* default value */
5461 target = (sc->mac_ver < 0x3071) ? 0x19 : 0x15;
5462 run_rt3070_filter_calib(sc, 0x27, target, &sc->rf24_40mhz);
5464 /* go back to 20MHz bandwidth */
5465 run_bbp_read(sc, 4, &bbp4);
5466 run_bbp_write(sc, 4, bbp4 & ~0x18);
5468 if (sc->mac_ver == 0x3572) {
5469 /* save default BBP registers 25 and 26 values */
5470 run_bbp_read(sc, 25, &sc->bbp25);
5471 run_bbp_read(sc, 26, &sc->bbp26);
5472 } else if (sc->mac_rev < 0x0201 || sc->mac_rev < 0x0211)
5473 run_rt3070_rf_write(sc, 27, 0x03);
5475 run_read(sc, RT3070_OPT_14, &tmp);
5476 run_write(sc, RT3070_OPT_14, tmp | 1);
5478 if (sc->mac_ver == 0x3070 || sc->mac_ver == 0x3071) {
5479 run_rt3070_rf_read(sc, 17, &rf);
5480 rf &= ~RT3070_TX_LO1;
5481 if ((sc->mac_ver == 0x3070 ||
5482 (sc->mac_ver == 0x3071 && sc->mac_rev >= 0x0211)) &&
5484 rf |= 0x20; /* fix for long range Rx issue */
5485 mingain = (sc->mac_ver == 0x3070) ? 1 : 2;
5486 if (sc->txmixgain_2ghz >= mingain)
5487 rf = (rf & ~0x7) | sc->txmixgain_2ghz;
5488 run_rt3070_rf_write(sc, 17, rf);
5491 if (sc->mac_rev == 0x3071) {
5492 run_rt3070_rf_read(sc, 1, &rf);
5493 rf &= ~(RT3070_RX0_PD | RT3070_TX0_PD);
5494 rf |= RT3070_RF_BLOCK | RT3070_RX1_PD | RT3070_TX1_PD;
5495 run_rt3070_rf_write(sc, 1, rf);
5497 run_rt3070_rf_read(sc, 15, &rf);
5498 run_rt3070_rf_write(sc, 15, rf & ~RT3070_TX_LO2);
5500 run_rt3070_rf_read(sc, 20, &rf);
5501 run_rt3070_rf_write(sc, 20, rf & ~RT3070_RX_LO1);
5503 run_rt3070_rf_read(sc, 21, &rf);
5504 run_rt3070_rf_write(sc, 21, rf & ~RT3070_RX_LO2);
5507 if (sc->mac_ver == 0x3070 || sc->mac_ver == 0x3071) {
5508 /* fix Tx to Rx IQ glitch by raising RF voltage */
5509 run_rt3070_rf_read(sc, 27, &rf);
5511 if (sc->mac_rev < 0x0211)
5513 run_rt3070_rf_write(sc, 27, rf);
5519 run_rt3593_rf_init(struct run_softc *sc)
5525 /* Disable the GPIO bits 4 and 7 for LNA PE control. */
5526 run_read(sc, RT3070_GPIO_SWITCH, &tmp);
5527 tmp &= ~(1 << 4 | 1 << 7);
5528 run_write(sc, RT3070_GPIO_SWITCH, tmp);
5530 /* Initialize RF registers to default value. */
5531 for (i = 0; i < nitems(rt3593_def_rf); i++) {
5532 run_rt3070_rf_write(sc, rt3593_def_rf[i].reg,
5533 rt3593_def_rf[i].val);
5536 /* Toggle RF R2 to initiate calibration. */
5537 run_rt3070_rf_write(sc, 2, RT5390_RESCAL);
5539 /* Initialize RF frequency offset. */
5540 run_adjust_freq_offset(sc);
5542 run_rt3070_rf_read(sc, 18, &rf);
5543 run_rt3070_rf_write(sc, 18, rf | RT3593_AUTOTUNE_BYPASS);
5546 * Increase voltage from 1.2V to 1.35V, wait for 1 msec to
5547 * decrease voltage back to 1.2V.
5549 run_read(sc, RT3070_LDO_CFG0, &tmp);
5550 tmp = (tmp & ~0x1f000000) | 0x0d000000;
5551 run_write(sc, RT3070_LDO_CFG0, tmp);
5553 tmp = (tmp & ~0x1f000000) | 0x01000000;
5554 run_write(sc, RT3070_LDO_CFG0, tmp);
5556 sc->rf24_20mhz = 0x1f;
5557 sc->rf24_40mhz = 0x2f;
5559 /* Save default BBP registers 25 and 26 values. */
5560 run_bbp_read(sc, 25, &sc->bbp25);
5561 run_bbp_read(sc, 26, &sc->bbp26);
5563 run_read(sc, RT3070_OPT_14, &tmp);
5564 run_write(sc, RT3070_OPT_14, tmp | 1);
5568 run_rt5390_rf_init(struct run_softc *sc)
5574 /* Toggle RF R2 to initiate calibration. */
5575 if (sc->mac_ver == 0x5390) {
5576 run_rt3070_rf_read(sc, 2, &rf);
5577 run_rt3070_rf_write(sc, 2, rf | RT5390_RESCAL);
5579 run_rt3070_rf_write(sc, 2, rf & ~RT5390_RESCAL);
5581 run_rt3070_rf_write(sc, 2, RT5390_RESCAL);
5585 /* Initialize RF registers to default value. */
5586 if (sc->mac_ver == 0x5592) {
5587 for (i = 0; i < nitems(rt5592_def_rf); i++) {
5588 run_rt3070_rf_write(sc, rt5592_def_rf[i].reg,
5589 rt5592_def_rf[i].val);
5591 /* Initialize RF frequency offset. */
5592 run_adjust_freq_offset(sc);
5593 } else if (sc->mac_ver == 0x5392) {
5594 for (i = 0; i < nitems(rt5392_def_rf); i++) {
5595 run_rt3070_rf_write(sc, rt5392_def_rf[i].reg,
5596 rt5392_def_rf[i].val);
5598 if (sc->mac_rev >= 0x0223) {
5599 run_rt3070_rf_write(sc, 23, 0x0f);
5600 run_rt3070_rf_write(sc, 24, 0x3e);
5601 run_rt3070_rf_write(sc, 51, 0x32);
5602 run_rt3070_rf_write(sc, 53, 0x22);
5603 run_rt3070_rf_write(sc, 56, 0xc1);
5604 run_rt3070_rf_write(sc, 59, 0x0f);
5607 for (i = 0; i < nitems(rt5390_def_rf); i++) {
5608 run_rt3070_rf_write(sc, rt5390_def_rf[i].reg,
5609 rt5390_def_rf[i].val);
5611 if (sc->mac_rev >= 0x0502) {
5612 run_rt3070_rf_write(sc, 6, 0xe0);
5613 run_rt3070_rf_write(sc, 25, 0x80);
5614 run_rt3070_rf_write(sc, 46, 0x73);
5615 run_rt3070_rf_write(sc, 53, 0x00);
5616 run_rt3070_rf_write(sc, 56, 0x42);
5617 run_rt3070_rf_write(sc, 61, 0xd1);
5621 sc->rf24_20mhz = 0x1f; /* default value */
5622 sc->rf24_40mhz = (sc->mac_ver == 0x5592) ? 0 : 0x2f;
5624 if (sc->mac_rev < 0x0211)
5625 run_rt3070_rf_write(sc, 27, 0x3);
5627 run_read(sc, RT3070_OPT_14, &tmp);
5628 run_write(sc, RT3070_OPT_14, tmp | 1);
5632 run_rt3070_filter_calib(struct run_softc *sc, uint8_t init, uint8_t target,
5636 uint8_t bbp55_pb, bbp55_sb, delta;
5639 /* program filter */
5640 run_rt3070_rf_read(sc, 24, &rf24);
5641 rf24 = (rf24 & 0xc0) | init; /* initial filter value */
5642 run_rt3070_rf_write(sc, 24, rf24);
5644 /* enable baseband loopback mode */
5645 run_rt3070_rf_read(sc, 22, &rf22);
5646 run_rt3070_rf_write(sc, 22, rf22 | 0x01);
5648 /* set power and frequency of passband test tone */
5649 run_bbp_write(sc, 24, 0x00);
5650 for (ntries = 0; ntries < 100; ntries++) {
5651 /* transmit test tone */
5652 run_bbp_write(sc, 25, 0x90);
5654 /* read received power */
5655 run_bbp_read(sc, 55, &bbp55_pb);
5662 /* set power and frequency of stopband test tone */
5663 run_bbp_write(sc, 24, 0x06);
5664 for (ntries = 0; ntries < 100; ntries++) {
5665 /* transmit test tone */
5666 run_bbp_write(sc, 25, 0x90);
5668 /* read received power */
5669 run_bbp_read(sc, 55, &bbp55_sb);
5671 delta = bbp55_pb - bbp55_sb;
5675 /* reprogram filter */
5677 run_rt3070_rf_write(sc, 24, rf24);
5681 rf24--; /* backtrack */
5683 run_rt3070_rf_write(sc, 24, rf24);
5686 /* restore initial state */
5687 run_bbp_write(sc, 24, 0x00);
5689 /* disable baseband loopback mode */
5690 run_rt3070_rf_read(sc, 22, &rf22);
5691 run_rt3070_rf_write(sc, 22, rf22 & ~0x01);
5697 run_rt3070_rf_setup(struct run_softc *sc)
5702 if (sc->mac_ver == 0x3572) {
5703 /* enable DC filter */
5704 if (sc->mac_rev >= 0x0201)
5705 run_bbp_write(sc, 103, 0xc0);
5707 run_bbp_read(sc, 138, &bbp);
5708 if (sc->ntxchains == 1)
5709 bbp |= 0x20; /* turn off DAC1 */
5710 if (sc->nrxchains == 1)
5711 bbp &= ~0x02; /* turn off ADC1 */
5712 run_bbp_write(sc, 138, bbp);
5714 if (sc->mac_rev >= 0x0211) {
5715 /* improve power consumption */
5716 run_bbp_read(sc, 31, &bbp);
5717 run_bbp_write(sc, 31, bbp & ~0x03);
5720 run_rt3070_rf_read(sc, 16, &rf);
5721 rf = (rf & ~0x07) | sc->txmixgain_2ghz;
5722 run_rt3070_rf_write(sc, 16, rf);
5724 } else if (sc->mac_ver == 0x3071) {
5725 if (sc->mac_rev >= 0x0211) {
5726 /* enable DC filter */
5727 run_bbp_write(sc, 103, 0xc0);
5729 /* improve power consumption */
5730 run_bbp_read(sc, 31, &bbp);
5731 run_bbp_write(sc, 31, bbp & ~0x03);
5734 run_bbp_read(sc, 138, &bbp);
5735 if (sc->ntxchains == 1)
5736 bbp |= 0x20; /* turn off DAC1 */
5737 if (sc->nrxchains == 1)
5738 bbp &= ~0x02; /* turn off ADC1 */
5739 run_bbp_write(sc, 138, bbp);
5741 run_write(sc, RT2860_TX_SW_CFG1, 0);
5742 if (sc->mac_rev < 0x0211) {
5743 run_write(sc, RT2860_TX_SW_CFG2,
5744 sc->patch_dac ? 0x2c : 0x0f);
5746 run_write(sc, RT2860_TX_SW_CFG2, 0);
5748 } else if (sc->mac_ver == 0x3070) {
5749 if (sc->mac_rev >= 0x0201) {
5750 /* enable DC filter */
5751 run_bbp_write(sc, 103, 0xc0);
5753 /* improve power consumption */
5754 run_bbp_read(sc, 31, &bbp);
5755 run_bbp_write(sc, 31, bbp & ~0x03);
5758 if (sc->mac_rev < 0x0201) {
5759 run_write(sc, RT2860_TX_SW_CFG1, 0);
5760 run_write(sc, RT2860_TX_SW_CFG2, 0x2c);
5762 run_write(sc, RT2860_TX_SW_CFG2, 0);
5765 /* initialize RF registers from ROM for >=RT3071*/
5766 if (sc->mac_ver >= 0x3071) {
5767 for (i = 0; i < 10; i++) {
5768 if (sc->rf[i].reg == 0 || sc->rf[i].reg == 0xff)
5770 run_rt3070_rf_write(sc, sc->rf[i].reg, sc->rf[i].val);
5776 run_rt3593_rf_setup(struct run_softc *sc)
5780 if (sc->mac_rev >= 0x0211) {
5781 /* Enable DC filter. */
5782 run_bbp_write(sc, 103, 0xc0);
5784 run_write(sc, RT2860_TX_SW_CFG1, 0);
5785 if (sc->mac_rev < 0x0211) {
5786 run_write(sc, RT2860_TX_SW_CFG2,
5787 sc->patch_dac ? 0x2c : 0x0f);
5789 run_write(sc, RT2860_TX_SW_CFG2, 0);
5791 run_rt3070_rf_read(sc, 50, &rf);
5792 run_rt3070_rf_write(sc, 50, rf & ~RT3593_TX_LO2);
5794 run_rt3070_rf_read(sc, 51, &rf);
5795 rf = (rf & ~(RT3593_TX_LO1 | 0x0c)) |
5796 ((sc->txmixgain_2ghz & 0x07) << 2);
5797 run_rt3070_rf_write(sc, 51, rf);
5799 run_rt3070_rf_read(sc, 38, &rf);
5800 run_rt3070_rf_write(sc, 38, rf & ~RT5390_RX_LO1);
5802 run_rt3070_rf_read(sc, 39, &rf);
5803 run_rt3070_rf_write(sc, 39, rf & ~RT5390_RX_LO2);
5805 run_rt3070_rf_read(sc, 1, &rf);
5806 run_rt3070_rf_write(sc, 1, rf & ~(RT3070_RF_BLOCK | RT3070_PLL_PD));
5808 run_rt3070_rf_read(sc, 30, &rf);
5809 rf = (rf & ~0x18) | 0x10;
5810 run_rt3070_rf_write(sc, 30, rf);
5812 /* Apply maximum likelihood detection for 2 stream case. */
5813 run_bbp_read(sc, 105, &bbp);
5814 if (sc->nrxchains > 1)
5815 run_bbp_write(sc, 105, bbp | RT5390_MLD);
5817 /* Avoid data lost and CRC error. */
5818 run_bbp_read(sc, 4, &bbp);
5819 run_bbp_write(sc, 4, bbp | RT5390_MAC_IF_CTRL);
5821 run_bbp_write(sc, 92, 0x02);
5822 run_bbp_write(sc, 82, 0x82);
5823 run_bbp_write(sc, 106, 0x05);
5824 run_bbp_write(sc, 104, 0x92);
5825 run_bbp_write(sc, 88, 0x90);
5826 run_bbp_write(sc, 148, 0xc8);
5827 run_bbp_write(sc, 47, 0x48);
5828 run_bbp_write(sc, 120, 0x50);
5830 run_bbp_write(sc, 163, 0x9d);
5833 run_bbp_write(sc, 142, 0x06);
5834 run_bbp_write(sc, 143, 0xa0);
5835 run_bbp_write(sc, 142, 0x07);
5836 run_bbp_write(sc, 143, 0xa1);
5837 run_bbp_write(sc, 142, 0x08);
5838 run_bbp_write(sc, 143, 0xa2);
5840 run_bbp_write(sc, 31, 0x08);
5841 run_bbp_write(sc, 68, 0x0b);
5842 run_bbp_write(sc, 105, 0x04);
5846 run_rt5390_rf_setup(struct run_softc *sc)
5850 if (sc->mac_rev >= 0x0211) {
5851 /* Enable DC filter. */
5852 run_bbp_write(sc, 103, 0xc0);
5854 if (sc->mac_ver != 0x5592) {
5855 /* Improve power consumption. */
5856 run_bbp_read(sc, 31, &bbp);
5857 run_bbp_write(sc, 31, bbp & ~0x03);
5861 run_bbp_read(sc, 138, &bbp);
5862 if (sc->ntxchains == 1)
5863 bbp |= 0x20; /* turn off DAC1 */
5864 if (sc->nrxchains == 1)
5865 bbp &= ~0x02; /* turn off ADC1 */
5866 run_bbp_write(sc, 138, bbp);
5868 run_rt3070_rf_read(sc, 38, &rf);
5869 run_rt3070_rf_write(sc, 38, rf & ~RT5390_RX_LO1);
5871 run_rt3070_rf_read(sc, 39, &rf);
5872 run_rt3070_rf_write(sc, 39, rf & ~RT5390_RX_LO2);
5874 /* Avoid data lost and CRC error. */
5875 run_bbp_read(sc, 4, &bbp);
5876 run_bbp_write(sc, 4, bbp | RT5390_MAC_IF_CTRL);
5878 run_rt3070_rf_read(sc, 30, &rf);
5879 rf = (rf & ~0x18) | 0x10;
5880 run_rt3070_rf_write(sc, 30, rf);
5882 if (sc->mac_ver != 0x5592) {
5883 run_write(sc, RT2860_TX_SW_CFG1, 0);
5884 if (sc->mac_rev < 0x0211) {
5885 run_write(sc, RT2860_TX_SW_CFG2,
5886 sc->patch_dac ? 0x2c : 0x0f);
5888 run_write(sc, RT2860_TX_SW_CFG2, 0);
5893 run_txrx_enable(struct run_softc *sc)
5895 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
5899 run_write(sc, RT2860_MAC_SYS_CTRL, RT2860_MAC_TX_EN);
5900 for (ntries = 0; ntries < 200; ntries++) {
5901 if ((error = run_read(sc, RT2860_WPDMA_GLO_CFG, &tmp)) != 0)
5903 if ((tmp & (RT2860_TX_DMA_BUSY | RT2860_RX_DMA_BUSY)) == 0)
5912 tmp |= RT2860_RX_DMA_EN | RT2860_TX_DMA_EN | RT2860_TX_WB_DDONE;
5913 run_write(sc, RT2860_WPDMA_GLO_CFG, tmp);
5915 /* enable Rx bulk aggregation (set timeout and limit) */
5916 tmp = RT2860_USB_TX_EN | RT2860_USB_RX_EN | RT2860_USB_RX_AGG_EN |
5917 RT2860_USB_RX_AGG_TO(128) | RT2860_USB_RX_AGG_LMT(2);
5918 run_write(sc, RT2860_USB_DMA_CFG, tmp);
5921 tmp = RT2860_DROP_CRC_ERR | RT2860_DROP_PHY_ERR;
5922 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
5923 tmp |= RT2860_DROP_UC_NOME | RT2860_DROP_DUPL |
5924 RT2860_DROP_CTS | RT2860_DROP_BA | RT2860_DROP_ACK |
5925 RT2860_DROP_VER_ERR | RT2860_DROP_CTRL_RSV |
5926 RT2860_DROP_CFACK | RT2860_DROP_CFEND;
5927 if (ic->ic_opmode == IEEE80211_M_STA)
5928 tmp |= RT2860_DROP_RTS | RT2860_DROP_PSPOLL;
5930 run_write(sc, RT2860_RX_FILTR_CFG, tmp);
5932 run_write(sc, RT2860_MAC_SYS_CTRL,
5933 RT2860_MAC_RX_EN | RT2860_MAC_TX_EN);
5939 run_adjust_freq_offset(struct run_softc *sc)
5943 run_rt3070_rf_read(sc, 17, &rf);
5945 rf = (rf & ~0x7f) | (sc->freq & 0x7f);
5949 run_mcu_cmd(sc, 0x74, (tmp << 8 ) | rf);
5953 run_init_locked(struct run_softc *sc)
5955 struct ifnet *ifp = sc->sc_ifp;
5956 struct ieee80211com *ic = ifp->if_l2com;
5963 if (ic->ic_nrunning > 1)
5968 if (run_load_microcode(sc) != 0) {
5969 device_printf(sc->sc_dev, "could not load 8051 microcode\n");
5973 for (ntries = 0; ntries < 100; ntries++) {
5974 if (run_read(sc, RT2860_ASIC_VER_ID, &tmp) != 0)
5976 if (tmp != 0 && tmp != 0xffffffff)
5983 for (i = 0; i != RUN_EP_QUEUES; i++)
5984 run_setup_tx_list(sc, &sc->sc_epq[i]);
5986 run_set_macaddr(sc, IF_LLADDR(ifp));
5988 for (ntries = 0; ntries < 100; ntries++) {
5989 if (run_read(sc, RT2860_WPDMA_GLO_CFG, &tmp) != 0)
5991 if ((tmp & (RT2860_TX_DMA_BUSY | RT2860_RX_DMA_BUSY)) == 0)
5995 if (ntries == 100) {
5996 device_printf(sc->sc_dev, "timeout waiting for DMA engine\n");
6000 tmp |= RT2860_TX_WB_DDONE;
6001 run_write(sc, RT2860_WPDMA_GLO_CFG, tmp);
6003 /* turn off PME_OEN to solve high-current issue */
6004 run_read(sc, RT2860_SYS_CTRL, &tmp);
6005 run_write(sc, RT2860_SYS_CTRL, tmp & ~RT2860_PME_OEN);
6007 run_write(sc, RT2860_MAC_SYS_CTRL,
6008 RT2860_BBP_HRST | RT2860_MAC_SRST);
6009 run_write(sc, RT2860_USB_DMA_CFG, 0);
6011 if (run_reset(sc) != 0) {
6012 device_printf(sc->sc_dev, "could not reset chipset\n");
6016 run_write(sc, RT2860_MAC_SYS_CTRL, 0);
6018 /* init Tx power for all Tx rates (from EEPROM) */
6019 for (ridx = 0; ridx < 5; ridx++) {
6020 if (sc->txpow20mhz[ridx] == 0xffffffff)
6022 run_write(sc, RT2860_TX_PWR_CFG(ridx), sc->txpow20mhz[ridx]);
6025 for (i = 0; i < nitems(rt2870_def_mac); i++)
6026 run_write(sc, rt2870_def_mac[i].reg, rt2870_def_mac[i].val);
6027 run_write(sc, RT2860_WMM_AIFSN_CFG, 0x00002273);
6028 run_write(sc, RT2860_WMM_CWMIN_CFG, 0x00002344);
6029 run_write(sc, RT2860_WMM_CWMAX_CFG, 0x000034aa);
6031 if (sc->mac_ver >= 0x5390) {
6032 run_write(sc, RT2860_TX_SW_CFG0,
6033 4 << RT2860_DLY_PAPE_EN_SHIFT | 4);
6034 if (sc->mac_ver >= 0x5392) {
6035 run_write(sc, RT2860_MAX_LEN_CFG, 0x00002fff);
6036 if (sc->mac_ver == 0x5592) {
6037 run_write(sc, RT2860_HT_FBK_CFG1, 0xedcba980);
6038 run_write(sc, RT2860_TXOP_HLDR_ET, 0x00000082);
6040 run_write(sc, RT2860_HT_FBK_CFG1, 0xedcb4980);
6041 run_write(sc, RT2860_LG_FBK_CFG0, 0xedcba322);
6044 } else if (sc->mac_ver == 0x3593) {
6045 run_write(sc, RT2860_TX_SW_CFG0,
6046 4 << RT2860_DLY_PAPE_EN_SHIFT | 2);
6047 } else if (sc->mac_ver >= 0x3070) {
6048 /* set delay of PA_PE assertion to 1us (unit of 0.25us) */
6049 run_write(sc, RT2860_TX_SW_CFG0,
6050 4 << RT2860_DLY_PAPE_EN_SHIFT);
6053 /* wait while MAC is busy */
6054 for (ntries = 0; ntries < 100; ntries++) {
6055 if (run_read(sc, RT2860_MAC_STATUS_REG, &tmp) != 0)
6057 if (!(tmp & (RT2860_RX_STATUS_BUSY | RT2860_TX_STATUS_BUSY)))
6064 /* clear Host to MCU mailbox */
6065 run_write(sc, RT2860_H2M_BBPAGENT, 0);
6066 run_write(sc, RT2860_H2M_MAILBOX, 0);
6069 if (run_bbp_init(sc) != 0) {
6070 device_printf(sc->sc_dev, "could not initialize BBP\n");
6074 /* abort TSF synchronization */
6075 run_read(sc, RT2860_BCN_TIME_CFG, &tmp);
6076 tmp &= ~(RT2860_BCN_TX_EN | RT2860_TSF_TIMER_EN |
6077 RT2860_TBTT_TIMER_EN);
6078 run_write(sc, RT2860_BCN_TIME_CFG, tmp);
6080 /* clear RX WCID search table */
6081 run_set_region_4(sc, RT2860_WCID_ENTRY(0), 0, 512);
6082 /* clear WCID attribute table */
6083 run_set_region_4(sc, RT2860_WCID_ATTR(0), 0, 8 * 32);
6085 /* hostapd sets a key before init. So, don't clear it. */
6086 if (sc->cmdq_key_set != RUN_CMDQ_GO) {
6087 /* clear shared key table */
6088 run_set_region_4(sc, RT2860_SKEY(0, 0), 0, 8 * 32);
6089 /* clear shared key mode */
6090 run_set_region_4(sc, RT2860_SKEY_MODE_0_7, 0, 4);
6093 run_read(sc, RT2860_US_CYC_CNT, &tmp);
6094 tmp = (tmp & ~0xff) | 0x1e;
6095 run_write(sc, RT2860_US_CYC_CNT, tmp);
6097 if (sc->mac_rev != 0x0101)
6098 run_write(sc, RT2860_TXOP_CTRL_CFG, 0x0000583f);
6100 run_write(sc, RT2860_WMM_TXOP0_CFG, 0);
6101 run_write(sc, RT2860_WMM_TXOP1_CFG, 48 << 16 | 96);
6103 /* write vendor-specific BBP values (from EEPROM) */
6104 if (sc->mac_ver < 0x3593) {
6105 for (i = 0; i < 10; i++) {
6106 if (sc->bbp[i].reg == 0 || sc->bbp[i].reg == 0xff)
6108 run_bbp_write(sc, sc->bbp[i].reg, sc->bbp[i].val);
6112 /* select Main antenna for 1T1R devices */
6113 if (sc->rf_rev == RT3070_RF_3020 || sc->rf_rev == RT5390_RF_5370)
6114 run_set_rx_antenna(sc, 0);
6116 /* send LEDs operating mode to microcontroller */
6117 (void)run_mcu_cmd(sc, RT2860_MCU_CMD_LED1, sc->led[0]);
6118 (void)run_mcu_cmd(sc, RT2860_MCU_CMD_LED2, sc->led[1]);
6119 (void)run_mcu_cmd(sc, RT2860_MCU_CMD_LED3, sc->led[2]);
6121 if (sc->mac_ver >= 0x5390)
6122 run_rt5390_rf_init(sc);
6123 else if (sc->mac_ver == 0x3593)
6124 run_rt3593_rf_init(sc);
6125 else if (sc->mac_ver >= 0x3070)
6126 run_rt3070_rf_init(sc);
6128 /* disable non-existing Rx chains */
6129 run_bbp_read(sc, 3, &bbp3);
6130 bbp3 &= ~(1 << 3 | 1 << 4);
6131 if (sc->nrxchains == 2)
6133 else if (sc->nrxchains == 3)
6135 run_bbp_write(sc, 3, bbp3);
6137 /* disable non-existing Tx chains */
6138 run_bbp_read(sc, 1, &bbp1);
6139 if (sc->ntxchains == 1)
6140 bbp1 &= ~(1 << 3 | 1 << 4);
6141 run_bbp_write(sc, 1, bbp1);
6143 if (sc->mac_ver >= 0x5390)
6144 run_rt5390_rf_setup(sc);
6145 else if (sc->mac_ver == 0x3593)
6146 run_rt3593_rf_setup(sc);
6147 else if (sc->mac_ver >= 0x3070)
6148 run_rt3070_rf_setup(sc);
6150 /* select default channel */
6151 run_set_chan(sc, ic->ic_curchan);
6153 /* setup initial protection mode */
6154 run_updateprot_cb(ic);
6156 /* turn radio LED on */
6157 run_set_leds(sc, RT2860_LED_RADIO);
6159 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
6160 ifp->if_drv_flags |= IFF_DRV_RUNNING;
6161 sc->cmdq_run = RUN_CMDQ_GO;
6163 for (i = 0; i != RUN_N_XFER; i++)
6164 usbd_xfer_set_stall(sc->sc_xfer[i]);
6166 usbd_transfer_start(sc->sc_xfer[RUN_BULK_RX]);
6168 if (run_txrx_enable(sc) != 0)
6180 struct run_softc *sc = arg;
6181 struct ifnet *ifp = sc->sc_ifp;
6182 struct ieee80211com *ic = ifp->if_l2com;
6185 run_init_locked(sc);
6188 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
6189 ieee80211_start_all(ic);
6195 struct run_softc *sc = (struct run_softc *)arg;
6196 struct ifnet *ifp = sc->sc_ifp;
6201 RUN_LOCK_ASSERT(sc, MA_OWNED);
6203 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
6204 run_set_leds(sc, 0); /* turn all LEDs off */
6206 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
6208 sc->ratectl_run = RUN_RATECTL_OFF;
6209 sc->cmdq_run = sc->cmdq_key_set;
6213 for(i = 0; i < RUN_N_XFER; i++)
6214 usbd_transfer_drain(sc->sc_xfer[i]);
6218 if (sc->rx_m != NULL) {
6223 /* Disable Tx/Rx DMA. */
6224 if (run_read(sc, RT2860_WPDMA_GLO_CFG, &tmp) != 0)
6226 tmp &= ~(RT2860_RX_DMA_EN | RT2860_TX_DMA_EN);
6227 run_write(sc, RT2860_WPDMA_GLO_CFG, tmp);
6229 for (ntries = 0; ntries < 100; ntries++) {
6230 if (run_read(sc, RT2860_WPDMA_GLO_CFG, &tmp) != 0)
6232 if ((tmp & (RT2860_TX_DMA_BUSY | RT2860_RX_DMA_BUSY)) == 0)
6236 if (ntries == 100) {
6237 device_printf(sc->sc_dev, "timeout waiting for DMA engine\n");
6242 run_read(sc, RT2860_MAC_SYS_CTRL, &tmp);
6243 tmp &= ~(RT2860_MAC_RX_EN | RT2860_MAC_TX_EN);
6244 run_write(sc, RT2860_MAC_SYS_CTRL, tmp);
6246 /* wait for pending Tx to complete */
6247 for (ntries = 0; ntries < 100; ntries++) {
6248 if (run_read(sc, RT2860_TXRXQ_PCNT, &tmp) != 0) {
6249 DPRINTF("Cannot read Tx queue count\n");
6252 if ((tmp & RT2860_TX2Q_PCNT_MASK) == 0) {
6253 DPRINTF("All Tx cleared\n");
6259 DPRINTF("There are still pending Tx\n");
6261 run_write(sc, RT2860_USB_DMA_CFG, 0);
6263 run_write(sc, RT2860_MAC_SYS_CTRL, RT2860_BBP_HRST | RT2860_MAC_SRST);
6264 run_write(sc, RT2860_MAC_SYS_CTRL, 0);
6266 for (i = 0; i != RUN_EP_QUEUES; i++)
6267 run_unsetup_tx_list(sc, &sc->sc_epq[i]);
6271 run_delay(struct run_softc *sc, u_int ms)
6273 usb_pause_mtx(mtx_owned(&sc->sc_mtx) ?
6274 &sc->sc_mtx : NULL, USB_MS_TO_TICKS(ms));
6277 static device_method_t run_methods[] = {
6278 /* Device interface */
6279 DEVMETHOD(device_probe, run_match),
6280 DEVMETHOD(device_attach, run_attach),
6281 DEVMETHOD(device_detach, run_detach),
6285 static driver_t run_driver = {
6287 .methods = run_methods,
6288 .size = sizeof(struct run_softc)
6291 static devclass_t run_devclass;
6293 DRIVER_MODULE(run, uhub, run_driver, run_devclass, run_driver_loaded, NULL);
6294 MODULE_DEPEND(run, wlan, 1, 1, 1);
6295 MODULE_DEPEND(run, usb, 1, 1, 1);
6296 MODULE_DEPEND(run, firmware, 1, 1, 1);
6297 MODULE_VERSION(run, 1);