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.
19 * $FreeBSD: head/sys/dev/usb/wlan/if_run.c 262795 2014-03-05 18:39:27Z hselasky $
23 * Ralink Technology RT2700U/RT2800U/RT3000U/RT3900E chipset driver.
24 * http://www.ralinktech.com/
27 #include <sys/param.h>
28 #include <sys/sockio.h>
29 #include <sys/sysctl.h>
32 #include <sys/kernel.h>
33 #include <sys/socket.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
38 #include <sys/endian.h>
39 #include <sys/linker.h>
40 #include <sys/firmware.h>
46 #include <net/if_var.h>
47 #include <net/if_arp.h>
48 #include <net/ethernet.h>
49 #include <net/if_dl.h>
50 #include <net/if_media.h>
51 #include <net/if_types.h>
52 #include <net/ifq_var.h>
54 #include <netinet/in.h>
55 #include <netinet/in_systm.h>
56 #include <netinet/in_var.h>
57 #include <netinet/if_ether.h>
58 #include <netinet/ip.h>
60 #include <netproto/802_11/ieee80211_var.h>
61 #include <netproto/802_11/ieee80211_regdomain.h>
62 #include <netproto/802_11/ieee80211_radiotap.h>
63 #include <netproto/802_11/ieee80211_ratectl.h>
65 #include <bus/u4b/usb.h>
66 #include <bus/u4b/usbdi.h>
69 #define USB_DEBUG_VAR run_debug
70 #include <bus/u4b/usb_debug.h>
71 #include <bus/u4b/usb_msctest.h>
73 #include <bus/u4b/wlan/if_runreg.h>
74 #include <bus/u4b/wlan/if_runvar.h>
82 static SYSCTL_NODE(_hw_usb, OID_AUTO, run, CTLFLAG_RW, 0, "USB run");
83 SYSCTL_INT(_hw_usb_run, OID_AUTO, debug, CTLFLAG_RW, &run_debug, 0,
87 #define IEEE80211_HAS_ADDR4(wh) \
88 (((wh)->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS)
91 * Because of LOR in run_key_delete(), use atomic instead.
92 * '& RUN_CMDQ_MASQ' is to loop cmdq[].
94 #define RUN_CMDQ_GET(c) (atomic_fetchadd_32((c), 1) & RUN_CMDQ_MASQ)
96 static const STRUCT_USB_HOST_ID run_devs[] = {
97 #define RUN_DEV(v,p) { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
98 #define RUN_DEV_EJECT(v,p) \
99 { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, RUN_EJECT) }
101 RUN_DEV(ABOCOM, RT2770),
102 RUN_DEV(ABOCOM, RT2870),
103 RUN_DEV(ABOCOM, RT3070),
104 RUN_DEV(ABOCOM, RT3071),
105 RUN_DEV(ABOCOM, RT3072),
106 RUN_DEV(ABOCOM2, RT2870_1),
107 RUN_DEV(ACCTON, RT2770),
108 RUN_DEV(ACCTON, RT2870_1),
109 RUN_DEV(ACCTON, RT2870_2),
110 RUN_DEV(ACCTON, RT2870_3),
111 RUN_DEV(ACCTON, RT2870_4),
112 RUN_DEV(ACCTON, RT2870_5),
113 RUN_DEV(ACCTON, RT3070),
114 RUN_DEV(ACCTON, RT3070_1),
115 RUN_DEV(ACCTON, RT3070_2),
116 RUN_DEV(ACCTON, RT3070_3),
117 RUN_DEV(ACCTON, RT3070_4),
118 RUN_DEV(ACCTON, RT3070_5),
119 RUN_DEV(AIRTIES, RT3070),
120 RUN_DEV(ALLWIN, RT2070),
121 RUN_DEV(ALLWIN, RT2770),
122 RUN_DEV(ALLWIN, RT2870),
123 RUN_DEV(ALLWIN, RT3070),
124 RUN_DEV(ALLWIN, RT3071),
125 RUN_DEV(ALLWIN, RT3072),
126 RUN_DEV(ALLWIN, RT3572),
127 RUN_DEV(AMIGO, RT2870_1),
128 RUN_DEV(AMIGO, RT2870_2),
129 RUN_DEV(AMIT, CGWLUSB2GNR),
130 RUN_DEV(AMIT, RT2870_1),
131 RUN_DEV(AMIT2, RT2870),
132 RUN_DEV(ASUS, RT2870_1),
133 RUN_DEV(ASUS, RT2870_2),
134 RUN_DEV(ASUS, RT2870_3),
135 RUN_DEV(ASUS, RT2870_4),
136 RUN_DEV(ASUS, RT2870_5),
137 RUN_DEV(ASUS, USBN13),
138 RUN_DEV(ASUS, RT3070_1),
139 RUN_DEV(ASUS, USBN66),
140 RUN_DEV(ASUS, USB_N53),
141 RUN_DEV(ASUS2, USBN11),
142 RUN_DEV(AZUREWAVE, RT2870_1),
143 RUN_DEV(AZUREWAVE, RT2870_2),
144 RUN_DEV(AZUREWAVE, RT3070_1),
145 RUN_DEV(AZUREWAVE, RT3070_2),
146 RUN_DEV(AZUREWAVE, RT3070_3),
147 RUN_DEV(BELKIN, F9L1103),
148 RUN_DEV(BELKIN, F5D8053V3),
149 RUN_DEV(BELKIN, F5D8055),
150 RUN_DEV(BELKIN, F5D8055V2),
151 RUN_DEV(BELKIN, F6D4050V1),
152 RUN_DEV(BELKIN, F6D4050V2),
153 RUN_DEV(BELKIN, RT2870_1),
154 RUN_DEV(BELKIN, RT2870_2),
155 RUN_DEV(CISCOLINKSYS, AE1000),
156 RUN_DEV(CISCOLINKSYS2, RT3070),
157 RUN_DEV(CISCOLINKSYS3, RT3070),
158 RUN_DEV(CONCEPTRONIC2, RT2870_1),
159 RUN_DEV(CONCEPTRONIC2, RT2870_2),
160 RUN_DEV(CONCEPTRONIC2, RT2870_3),
161 RUN_DEV(CONCEPTRONIC2, RT2870_4),
162 RUN_DEV(CONCEPTRONIC2, RT2870_5),
163 RUN_DEV(CONCEPTRONIC2, RT2870_6),
164 RUN_DEV(CONCEPTRONIC2, RT2870_7),
165 RUN_DEV(CONCEPTRONIC2, RT2870_8),
166 RUN_DEV(CONCEPTRONIC2, RT3070_1),
167 RUN_DEV(CONCEPTRONIC2, RT3070_2),
168 RUN_DEV(CONCEPTRONIC2, VIGORN61),
169 RUN_DEV(COREGA, CGWLUSB300GNM),
170 RUN_DEV(COREGA, RT2870_1),
171 RUN_DEV(COREGA, RT2870_2),
172 RUN_DEV(COREGA, RT2870_3),
173 RUN_DEV(COREGA, RT3070),
174 RUN_DEV(CYBERTAN, RT2870),
175 RUN_DEV(DLINK, RT2870),
176 RUN_DEV(DLINK, RT3072),
177 RUN_DEV(DLINK, DWA127),
178 RUN_DEV(DLINK, DWA140B3),
179 RUN_DEV(DLINK, DWA160B2),
180 RUN_DEV(DLINK, DWA162),
181 RUN_DEV(DLINK2, DWA130),
182 RUN_DEV(DLINK2, RT2870_1),
183 RUN_DEV(DLINK2, RT2870_2),
184 RUN_DEV(DLINK2, RT3070_1),
185 RUN_DEV(DLINK2, RT3070_2),
186 RUN_DEV(DLINK2, RT3070_3),
187 RUN_DEV(DLINK2, RT3070_4),
188 RUN_DEV(DLINK2, RT3070_5),
189 RUN_DEV(DLINK2, RT3072),
190 RUN_DEV(DLINK2, RT3072_1),
191 RUN_DEV(EDIMAX, EW7717),
192 RUN_DEV(EDIMAX, EW7718),
193 RUN_DEV(EDIMAX, EW7733UND),
194 RUN_DEV(EDIMAX, RT2870_1),
195 RUN_DEV(ENCORE, RT3070_1),
196 RUN_DEV(ENCORE, RT3070_2),
197 RUN_DEV(ENCORE, RT3070_3),
198 RUN_DEV(GIGABYTE, GNWB31N),
199 RUN_DEV(GIGABYTE, GNWB32L),
200 RUN_DEV(GIGABYTE, RT2870_1),
201 RUN_DEV(GIGASET, RT3070_1),
202 RUN_DEV(GIGASET, RT3070_2),
203 RUN_DEV(GUILLEMOT, HWNU300),
204 RUN_DEV(HAWKING, HWUN2),
205 RUN_DEV(HAWKING, RT2870_1),
206 RUN_DEV(HAWKING, RT2870_2),
207 RUN_DEV(HAWKING, RT3070),
208 RUN_DEV(IODATA, RT3072_1),
209 RUN_DEV(IODATA, RT3072_2),
210 RUN_DEV(IODATA, RT3072_3),
211 RUN_DEV(IODATA, RT3072_4),
212 RUN_DEV(LINKSYS4, RT3070),
213 RUN_DEV(LINKSYS4, WUSB100),
214 RUN_DEV(LINKSYS4, WUSB54GCV3),
215 RUN_DEV(LINKSYS4, WUSB600N),
216 RUN_DEV(LINKSYS4, WUSB600NV2),
217 RUN_DEV(LOGITEC, RT2870_1),
218 RUN_DEV(LOGITEC, RT2870_2),
219 RUN_DEV(LOGITEC, RT2870_3),
220 RUN_DEV(LOGITEC, LANW300NU2),
221 RUN_DEV(LOGITEC, LANW150NU2),
222 RUN_DEV(LOGITEC, LANW300NU2S),
223 RUN_DEV(MELCO, RT2870_1),
224 RUN_DEV(MELCO, RT2870_2),
225 RUN_DEV(MELCO, WLIUCAG300N),
226 RUN_DEV(MELCO, WLIUCG300N),
227 RUN_DEV(MELCO, WLIUCG301N),
228 RUN_DEV(MELCO, WLIUCGN),
229 RUN_DEV(MELCO, WLIUCGNM),
230 RUN_DEV(MELCO, WLIUCGNM2),
231 RUN_DEV(MOTOROLA4, RT2770),
232 RUN_DEV(MOTOROLA4, RT3070),
233 RUN_DEV(MSI, RT3070_1),
234 RUN_DEV(MSI, RT3070_2),
235 RUN_DEV(MSI, RT3070_3),
236 RUN_DEV(MSI, RT3070_4),
237 RUN_DEV(MSI, RT3070_5),
238 RUN_DEV(MSI, RT3070_6),
239 RUN_DEV(MSI, RT3070_7),
240 RUN_DEV(MSI, RT3070_8),
241 RUN_DEV(MSI, RT3070_9),
242 RUN_DEV(MSI, RT3070_10),
243 RUN_DEV(MSI, RT3070_11),
244 RUN_DEV(OVISLINK, RT3072),
245 RUN_DEV(PARA, RT3070),
246 RUN_DEV(PEGATRON, RT2870),
247 RUN_DEV(PEGATRON, RT3070),
248 RUN_DEV(PEGATRON, RT3070_2),
249 RUN_DEV(PEGATRON, RT3070_3),
250 RUN_DEV(PHILIPS, RT2870),
251 RUN_DEV(PLANEX2, GWUS300MINIS),
252 RUN_DEV(PLANEX2, GWUSMICRON),
253 RUN_DEV(PLANEX2, RT2870),
254 RUN_DEV(PLANEX2, RT3070),
255 RUN_DEV(QCOM, RT2870),
256 RUN_DEV(QUANTA, RT3070),
257 RUN_DEV(RALINK, RT2070),
258 RUN_DEV(RALINK, RT2770),
259 RUN_DEV(RALINK, RT2870),
260 RUN_DEV(RALINK, RT3070),
261 RUN_DEV(RALINK, RT3071),
262 RUN_DEV(RALINK, RT3072),
263 RUN_DEV(RALINK, RT3370),
264 RUN_DEV(RALINK, RT3572),
265 RUN_DEV(RALINK, RT3573),
266 RUN_DEV(RALINK, RT5370),
267 RUN_DEV(RALINK, RT5572),
268 RUN_DEV(RALINK, RT8070),
269 RUN_DEV(SAMSUNG, WIS09ABGN),
270 RUN_DEV(SAMSUNG2, RT2870_1),
271 RUN_DEV(SENAO, RT2870_1),
272 RUN_DEV(SENAO, RT2870_2),
273 RUN_DEV(SENAO, RT2870_3),
274 RUN_DEV(SENAO, RT2870_4),
275 RUN_DEV(SENAO, RT3070),
276 RUN_DEV(SENAO, RT3071),
277 RUN_DEV(SENAO, RT3072_1),
278 RUN_DEV(SENAO, RT3072_2),
279 RUN_DEV(SENAO, RT3072_3),
280 RUN_DEV(SENAO, RT3072_4),
281 RUN_DEV(SENAO, RT3072_5),
282 RUN_DEV(SITECOMEU, RT2770),
283 RUN_DEV(SITECOMEU, RT2870_1),
284 RUN_DEV(SITECOMEU, RT2870_2),
285 RUN_DEV(SITECOMEU, RT2870_3),
286 RUN_DEV(SITECOMEU, RT2870_4),
287 RUN_DEV(SITECOMEU, RT3070),
288 RUN_DEV(SITECOMEU, RT3070_2),
289 RUN_DEV(SITECOMEU, RT3070_3),
290 RUN_DEV(SITECOMEU, RT3070_4),
291 RUN_DEV(SITECOMEU, RT3071),
292 RUN_DEV(SITECOMEU, RT3072_1),
293 RUN_DEV(SITECOMEU, RT3072_2),
294 RUN_DEV(SITECOMEU, RT3072_3),
295 RUN_DEV(SITECOMEU, RT3072_4),
296 RUN_DEV(SITECOMEU, RT3072_5),
297 RUN_DEV(SITECOMEU, RT3072_6),
298 RUN_DEV(SITECOMEU, WL608),
299 RUN_DEV(SPARKLAN, RT2870_1),
300 RUN_DEV(SPARKLAN, RT3070),
301 RUN_DEV(SWEEX2, LW153),
302 RUN_DEV(SWEEX2, LW303),
303 RUN_DEV(SWEEX2, LW313),
304 RUN_DEV(TOSHIBA, RT3070),
305 RUN_DEV(UMEDIA, RT2870_1),
306 RUN_DEV(ZCOM, RT2870_1),
307 RUN_DEV(ZCOM, RT2870_2),
308 RUN_DEV(ZINWELL, RT2870_1),
309 RUN_DEV(ZINWELL, RT2870_2),
310 RUN_DEV(ZINWELL, RT3070),
311 RUN_DEV(ZINWELL, RT3072_1),
312 RUN_DEV(ZINWELL, RT3072_2),
313 RUN_DEV(ZYXEL, RT2870_1),
314 RUN_DEV(ZYXEL, RT2870_2),
315 RUN_DEV_EJECT(ZYXEL, NWD2705),
316 RUN_DEV_EJECT(RALINK, RT_STOR),
321 static device_probe_t run_match;
322 static device_attach_t run_attach;
323 static device_detach_t run_detach;
325 static usb_callback_t run_bulk_rx_callback;
326 static usb_callback_t run_bulk_tx_callback0;
327 static usb_callback_t run_bulk_tx_callback1;
328 static usb_callback_t run_bulk_tx_callback2;
329 static usb_callback_t run_bulk_tx_callback3;
330 static usb_callback_t run_bulk_tx_callback4;
331 static usb_callback_t run_bulk_tx_callback5;
333 static void run_autoinst(void *, struct usb_device *,
334 struct usb_attach_arg *);
335 static int run_driver_loaded(struct module *, int, void *);
336 static void run_bulk_tx_callbackN(struct usb_xfer *xfer,
337 usb_error_t error, u_int index);
338 static struct ieee80211vap *run_vap_create(struct ieee80211com *,
339 const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
340 const uint8_t [IEEE80211_ADDR_LEN],
341 const uint8_t [IEEE80211_ADDR_LEN]);
342 static void run_vap_delete(struct ieee80211vap *);
343 static void run_cmdq_cb(void *, int);
344 static void run_setup_tx_list(struct run_softc *,
345 struct run_endpoint_queue *);
346 static void run_unsetup_tx_list(struct run_softc *,
347 struct run_endpoint_queue *);
348 static int run_load_microcode(struct run_softc *);
349 static int run_reset(struct run_softc *);
350 static usb_error_t run_do_request(struct run_softc *,
351 struct usb_device_request *, void *);
352 static int run_read(struct run_softc *, uint16_t, uint32_t *);
353 static int run_read_region_1(struct run_softc *, uint16_t, uint8_t *, int);
354 static int run_write_2(struct run_softc *, uint16_t, uint16_t);
355 static int run_write(struct run_softc *, uint16_t, uint32_t);
356 static int run_write_region_1(struct run_softc *, uint16_t,
357 const uint8_t *, int);
358 static int run_set_region_4(struct run_softc *, uint16_t, uint32_t, int);
359 static int run_efuse_read(struct run_softc *, uint16_t, uint16_t *, int);
360 static int run_efuse_read_2(struct run_softc *, uint16_t, uint16_t *);
361 static int run_eeprom_read_2(struct run_softc *, uint16_t, uint16_t *);
362 static int run_rt2870_rf_write(struct run_softc *, uint32_t);
363 static int run_rt3070_rf_read(struct run_softc *, uint8_t, uint8_t *);
364 static int run_rt3070_rf_write(struct run_softc *, uint8_t, uint8_t);
365 static int run_bbp_read(struct run_softc *, uint8_t, uint8_t *);
366 static int run_bbp_write(struct run_softc *, uint8_t, uint8_t);
367 static int run_mcu_cmd(struct run_softc *, uint8_t, uint16_t);
368 static const char *run_get_rf(uint16_t);
369 static void run_rt3593_get_txpower(struct run_softc *);
370 static void run_get_txpower(struct run_softc *);
371 static int run_read_eeprom(struct run_softc *);
372 static struct ieee80211_node *run_node_alloc(struct ieee80211vap *,
373 const uint8_t mac[IEEE80211_ADDR_LEN]);
374 static int run_media_change(struct ifnet *);
375 static int run_newstate(struct ieee80211vap *, enum ieee80211_state, int);
376 static int run_wme_update(struct ieee80211com *);
377 static void run_wme_update_cb(void *);
378 static void run_key_update_begin(struct ieee80211vap *);
379 static void run_key_update_end(struct ieee80211vap *);
380 static void run_key_set_cb(void *);
381 static int run_key_set(struct ieee80211vap *, struct ieee80211_key *,
382 const uint8_t mac[IEEE80211_ADDR_LEN]);
383 static void run_key_delete_cb(void *);
384 static int run_key_delete(struct ieee80211vap *, struct ieee80211_key *);
385 static void run_ratectl_to(void *);
386 static void run_ratectl_cb(void *, int);
387 static void run_drain_fifo(void *);
388 static void run_iter_func(void *, struct ieee80211_node *);
389 static void run_newassoc_cb(void *);
390 static void run_newassoc(struct ieee80211_node *, int);
391 static void run_rx_frame(struct run_softc *, struct mbuf *, uint32_t);
392 static void run_tx_free(struct run_endpoint_queue *pq,
393 struct run_tx_data *, int);
394 static void run_set_tx_desc(struct run_softc *, struct run_tx_data *);
395 static int run_tx(struct run_softc *, struct mbuf *,
396 struct ieee80211_node *);
397 static int run_tx_mgt(struct run_softc *, struct mbuf *,
398 struct ieee80211_node *);
399 static int run_sendprot(struct run_softc *, const struct mbuf *,
400 struct ieee80211_node *, int, int);
401 static int run_tx_param(struct run_softc *, struct mbuf *,
402 struct ieee80211_node *,
403 const struct ieee80211_bpf_params *);
404 static int run_raw_xmit(struct ieee80211_node *, struct mbuf *,
405 const struct ieee80211_bpf_params *);
406 static void run_start_locked(struct ifnet *);
407 static void run_start(struct ifnet *, struct ifaltq_subque *);
408 static int run_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
409 static void run_iq_calib(struct run_softc *, u_int);
410 static void run_set_agc(struct run_softc *, uint8_t);
411 static void run_select_chan_group(struct run_softc *, int);
412 static void run_set_rx_antenna(struct run_softc *, int);
413 static void run_rt2870_set_chan(struct run_softc *, u_int);
414 static void run_rt3070_set_chan(struct run_softc *, u_int);
415 static void run_rt3572_set_chan(struct run_softc *, u_int);
416 static void run_rt3593_set_chan(struct run_softc *, u_int);
417 static void run_rt5390_set_chan(struct run_softc *, u_int);
418 static void run_rt5592_set_chan(struct run_softc *, u_int);
419 static int run_set_chan(struct run_softc *, struct ieee80211_channel *);
420 static void run_set_channel(struct ieee80211com *);
421 static void run_scan_start(struct ieee80211com *);
422 static void run_scan_end(struct ieee80211com *);
423 static void run_update_beacon(struct ieee80211vap *, int);
424 static void run_update_beacon_cb(void *);
425 static void run_updateprot(struct ieee80211com *);
426 static void run_updateprot_cb(void *);
427 static void run_usb_timeout_cb(void *);
428 static void run_reset_livelock(struct run_softc *);
429 static void run_enable_tsf_sync(struct run_softc *);
430 static void run_enable_mrr(struct run_softc *);
431 static void run_set_txpreamble(struct run_softc *);
432 static void run_set_basicrates(struct run_softc *);
433 static void run_set_leds(struct run_softc *, uint16_t);
434 static void run_set_bssid(struct run_softc *, const uint8_t *);
435 static void run_set_macaddr(struct run_softc *, const uint8_t *);
436 static void run_updateslot(struct ifnet *);
437 static void run_updateslot_cb(void *);
438 static void run_update_mcast(struct ifnet *);
439 static int8_t run_rssi2dbm(struct run_softc *, uint8_t, uint8_t);
440 static void run_update_promisc_locked(struct ifnet *);
441 static void run_update_promisc(struct ifnet *);
442 static void run_rt5390_bbp_init(struct run_softc *);
443 static int run_bbp_init(struct run_softc *);
444 static int run_rt3070_rf_init(struct run_softc *);
445 static void run_rt3593_rf_init(struct run_softc *);
446 static void run_rt5390_rf_init(struct run_softc *);
447 static int run_rt3070_filter_calib(struct run_softc *, uint8_t, uint8_t,
449 static void run_rt3070_rf_setup(struct run_softc *);
450 static void run_rt3593_rf_setup(struct run_softc *);
451 static void run_rt5390_rf_setup(struct run_softc *);
452 static int run_txrx_enable(struct run_softc *);
453 static void run_adjust_freq_offset(struct run_softc *);
454 static void run_init(void *);
455 static void run_init_locked(struct run_softc *);
456 static void run_stop(void *);
457 static void run_delay(struct run_softc *, u_int);
459 static eventhandler_tag run_etag;
461 static const struct rt2860_rate {
464 enum ieee80211_phytype phy;
469 { 2, 0, IEEE80211_T_DS, 0, 314, 314 },
470 { 4, 1, IEEE80211_T_DS, 1, 258, 162 },
471 { 11, 2, IEEE80211_T_DS, 2, 223, 127 },
472 { 22, 3, IEEE80211_T_DS, 3, 213, 117 },
473 { 12, 0, IEEE80211_T_OFDM, 4, 60, 60 },
474 { 18, 1, IEEE80211_T_OFDM, 4, 52, 52 },
475 { 24, 2, IEEE80211_T_OFDM, 6, 48, 48 },
476 { 36, 3, IEEE80211_T_OFDM, 6, 44, 44 },
477 { 48, 4, IEEE80211_T_OFDM, 8, 44, 44 },
478 { 72, 5, IEEE80211_T_OFDM, 8, 40, 40 },
479 { 96, 6, IEEE80211_T_OFDM, 8, 40, 40 },
480 { 108, 7, IEEE80211_T_OFDM, 8, 40, 40 }
483 static const struct {
486 } rt2870_def_mac[] = {
490 static const struct {
493 } rt2860_def_bbp[] = {
495 },rt5390_def_bbp[] = {
497 },rt5592_def_bbp[] = {
502 * Default values for BBP register R196 for RT5592.
504 static const uint8_t rt5592_bbp_r196[] = {
505 0xe0, 0x1f, 0x38, 0x32, 0x08, 0x28, 0x19, 0x0a, 0xff, 0x00,
506 0x16, 0x10, 0x10, 0x0b, 0x36, 0x2c, 0x26, 0x24, 0x42, 0x36,
507 0x30, 0x2d, 0x4c, 0x46, 0x3d, 0x40, 0x3e, 0x42, 0x3d, 0x40,
508 0x3c, 0x34, 0x2c, 0x2f, 0x3c, 0x35, 0x2e, 0x2a, 0x49, 0x41,
509 0x36, 0x31, 0x30, 0x30, 0x0e, 0x0d, 0x28, 0x21, 0x1c, 0x16,
510 0x50, 0x4a, 0x43, 0x40, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00,
511 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
512 0x00, 0x00, 0x7d, 0x14, 0x32, 0x2c, 0x36, 0x4c, 0x43, 0x2c,
513 0x2e, 0x36, 0x30, 0x6e
516 static const struct rfprog {
518 uint32_t r1, r2, r3, r4;
519 } rt2860_rf2850[] = {
529 static const struct rt5592_freqs {
532 } rt5592_freqs_20mhz[] = {
534 },rt5592_freqs_40mhz[] = {
538 static const struct {
541 } rt3070_def_rf[] = {
543 },rt3572_def_rf[] = {
545 },rt3593_def_rf[] = {
547 },rt5390_def_rf[] = {
549 },rt5392_def_rf[] = {
551 },rt5592_def_rf[] = {
553 },rt5592_2ghz_def_rf[] = {
555 },rt5592_5ghz_def_rf[] = {
559 static const struct {
564 } rt5592_chan_5ghz[] = {
568 static const struct usb_config run_config[RUN_N_XFER] = {
571 .endpoint = UE_ADDR_ANY,
573 .direction = UE_DIR_OUT,
574 .bufsize = RUN_MAX_TXSZ,
575 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
576 .callback = run_bulk_tx_callback0,
577 .timeout = 5000, /* ms */
581 .endpoint = UE_ADDR_ANY,
582 .direction = UE_DIR_OUT,
584 .bufsize = RUN_MAX_TXSZ,
585 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
586 .callback = run_bulk_tx_callback1,
587 .timeout = 5000, /* ms */
591 .endpoint = UE_ADDR_ANY,
592 .direction = UE_DIR_OUT,
594 .bufsize = RUN_MAX_TXSZ,
595 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
596 .callback = run_bulk_tx_callback2,
597 .timeout = 5000, /* ms */
601 .endpoint = UE_ADDR_ANY,
602 .direction = UE_DIR_OUT,
604 .bufsize = RUN_MAX_TXSZ,
605 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
606 .callback = run_bulk_tx_callback3,
607 .timeout = 5000, /* ms */
609 [RUN_BULK_TX_HCCA] = {
611 .endpoint = UE_ADDR_ANY,
612 .direction = UE_DIR_OUT,
614 .bufsize = RUN_MAX_TXSZ,
615 .flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
616 .callback = run_bulk_tx_callback4,
617 .timeout = 5000, /* ms */
619 [RUN_BULK_TX_PRIO] = {
621 .endpoint = UE_ADDR_ANY,
622 .direction = UE_DIR_OUT,
624 .bufsize = RUN_MAX_TXSZ,
625 .flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
626 .callback = run_bulk_tx_callback5,
627 .timeout = 5000, /* ms */
631 .endpoint = UE_ADDR_ANY,
632 .direction = UE_DIR_IN,
633 .bufsize = RUN_MAX_RXSZ,
634 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
635 .callback = run_bulk_rx_callback,
640 run_autoinst(void *arg, struct usb_device *udev,
641 struct usb_attach_arg *uaa)
643 struct usb_interface *iface;
644 struct usb_interface_descriptor *id;
646 if (uaa->dev_state != UAA_DEV_READY)
649 iface = usbd_get_iface(udev, 0);
653 if (id == NULL || id->bInterfaceClass != UICLASS_MASS)
655 if (usbd_lookup_id_by_uaa(run_devs, sizeof(run_devs), uaa))
658 if (usb_msc_eject(udev, 0, MSC_EJECT_STOPUNIT) == 0)
659 uaa->dev_state = UAA_DEV_EJECTING;
663 run_driver_loaded(struct module *mod, int what, void *arg)
667 run_etag = EVENTHANDLER_REGISTER(usb_dev_configured,
668 run_autoinst, NULL, EVENTHANDLER_PRI_ANY);
671 EVENTHANDLER_DEREGISTER(usb_dev_configured, run_etag);
680 run_match(device_t self)
682 struct usb_attach_arg *uaa = device_get_ivars(self);
684 if (uaa->usb_mode != USB_MODE_HOST)
686 if (uaa->info.bConfigIndex != 0)
688 if (uaa->info.bIfaceIndex != RT2860_IFACE_INDEX)
691 return (usbd_lookup_id_by_uaa(run_devs, sizeof(run_devs), uaa));
695 run_attach(device_t self)
697 struct run_softc *sc = device_get_softc(self);
698 struct usb_attach_arg *uaa = device_get_ivars(self);
699 struct ieee80211com *ic;
703 uint8_t iface_index, bands;
704 char ethstr[ETHER_ADDRSTRLEN + 1];
706 wlan_serialize_enter();
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 lockinit(&sc->sc_lock, device_get_nameunit(sc->sc_dev),
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_lock);
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, kether_ntoa(sc->sc_bssid, ethstr));
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 #if 0 /* XXX swildner: see c3d4131842e47b168d93a0650d58d425ebeef789 */
773 ifq_set_ready(&ifp->if_snd);
777 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
778 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
780 /* set device capabilities */
782 IEEE80211_C_STA | /* station mode supported */
783 IEEE80211_C_MONITOR | /* monitor mode supported */
786 IEEE80211_C_WDS | /* 4-address traffic works */
788 IEEE80211_C_SHPREAMBLE | /* short preamble supported */
789 IEEE80211_C_SHSLOT | /* short slot time supported */
790 IEEE80211_C_WME | /* WME */
791 IEEE80211_C_WPA; /* WPA1|WPA2(RSN) */
794 IEEE80211_CRYPTO_WEP |
795 IEEE80211_CRYPTO_AES_CCM |
796 IEEE80211_CRYPTO_TKIPMIC |
797 IEEE80211_CRYPTO_TKIP;
799 ic->ic_flags |= IEEE80211_F_DATAPAD;
800 ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS;
803 setbit(&bands, IEEE80211_MODE_11B);
804 setbit(&bands, IEEE80211_MODE_11G);
805 if (sc->rf_rev == RT2860_RF_2750 || sc->rf_rev == RT2860_RF_2850 ||
806 sc->rf_rev == RT3070_RF_3052 || sc->rf_rev == RT3593_RF_3053 ||
807 sc->rf_rev == RT5592_RF_5592)
808 setbit(&bands, IEEE80211_MODE_11A);
809 ieee80211_init_channels(ic, NULL, &bands);
811 ieee80211_ifattach(ic, sc->sc_bssid);
813 ic->ic_scan_start = run_scan_start;
814 ic->ic_scan_end = run_scan_end;
815 ic->ic_set_channel = run_set_channel;
816 ic->ic_node_alloc = run_node_alloc;
817 ic->ic_newassoc = run_newassoc;
818 ic->ic_updateslot = run_updateslot;
819 ic->ic_update_mcast = run_update_mcast;
820 ic->ic_wme.wme_update = run_wme_update;
821 ic->ic_raw_xmit = run_raw_xmit;
822 ic->ic_update_promisc = run_update_promisc;
824 ic->ic_vap_create = run_vap_create;
825 ic->ic_vap_delete = run_vap_delete;
827 ieee80211_radiotap_attach(ic,
828 &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
829 RUN_TX_RADIOTAP_PRESENT,
830 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
831 RUN_RX_RADIOTAP_PRESENT);
833 TASK_INIT(&sc->cmdq_task, 0, run_cmdq_cb, sc);
834 TASK_INIT(&sc->ratectl_task, 0, run_ratectl_cb, sc);
835 usb_callout_init_mtx(&sc->ratectl_ch, &sc->sc_lock, 1);
838 ieee80211_announce(ic);
840 wlan_serialize_exit();
844 wlan_serialize_exit();
850 run_detach(device_t self)
852 struct run_softc *sc = device_get_softc(self);
853 struct ifnet *ifp = sc->sc_ifp;
854 struct ieee80211com *ic;
857 wlan_serialize_enter();
860 /* stop all USB transfers */
861 usbd_transfer_unsetup(sc->sc_xfer, RUN_N_XFER);
865 sc->ratectl_run = RUN_RATECTL_OFF;
866 sc->cmdq_run = sc->cmdq_key_set = RUN_CMDQ_ABORT;
868 /* free TX list, if any */
869 for (i = 0; i != RUN_EP_QUEUES; i++)
870 run_unsetup_tx_list(sc, &sc->sc_epq[i]);
876 usb_callout_drain(&sc->ratectl_ch);
877 ieee80211_draintask(ic, &sc->cmdq_task);
878 ieee80211_draintask(ic, &sc->ratectl_task);
879 ieee80211_ifdetach(ic);
883 lockuninit(&sc->sc_lock);
885 wlan_serialize_exit();
889 static struct ieee80211vap *
890 run_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
891 enum ieee80211_opmode opmode, int flags,
892 const uint8_t bssid[IEEE80211_ADDR_LEN],
893 const uint8_t mac[IEEE80211_ADDR_LEN])
895 struct ifnet *ifp = ic->ic_ifp;
896 struct run_softc *sc = ifp->if_softc;
898 struct ieee80211vap *vap;
901 if (sc->rvp_cnt >= RUN_VAP_MAX) {
902 if_printf(ifp, "number of VAPs maxed out\n");
907 case IEEE80211_M_STA:
908 /* enable s/w bmiss handling for sta mode */
909 flags |= IEEE80211_CLONE_NOBEACONS;
911 case IEEE80211_M_IBSS:
912 case IEEE80211_M_MONITOR:
913 case IEEE80211_M_HOSTAP:
914 case IEEE80211_M_MBSS:
915 /* other than WDS vaps, only one at a time */
916 if (!TAILQ_EMPTY(&ic->ic_vaps))
919 case IEEE80211_M_WDS:
920 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next){
921 if(vap->iv_opmode != IEEE80211_M_HOSTAP)
923 /* WDS vap's always share the local mac address. */
924 flags &= ~IEEE80211_CLONE_BSSID;
928 if_printf(ifp, "wds only supported in ap mode\n");
933 if_printf(ifp, "unknown opmode %d\n", opmode);
937 rvp = (struct run_vap *) kmalloc(sizeof(struct run_vap),
938 M_80211_VAP, M_INTWAIT | M_ZERO);
941 if (ieee80211_vap_setup(ic, vap, name, unit,
942 opmode, flags, bssid, mac) != 0) {
944 kfree(rvp, M_80211_VAP);
948 vap->iv_key_update_begin = run_key_update_begin;
949 vap->iv_key_update_end = run_key_update_end;
950 vap->iv_update_beacon = run_update_beacon;
951 vap->iv_max_aid = RT2870_WCID_MAX;
953 * To delete the right key from h/w, we need wcid.
954 * Luckily, there is unused space in ieee80211_key{}, wk_pad,
955 * and matching wcid will be written into there. So, cast
956 * some spells to remove 'const' from ieee80211_key{}
958 vap->iv_key_delete = (void *)run_key_delete;
959 vap->iv_key_set = (void *)run_key_set;
961 /* override state transition machine */
962 rvp->newstate = vap->iv_newstate;
963 vap->iv_newstate = run_newstate;
965 ieee80211_ratectl_init(vap);
966 ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */);
969 ieee80211_vap_attach(vap, run_media_change, ieee80211_media_status);
971 /* make sure id is always unique */
972 for (i = 0; i < RUN_VAP_MAX; i++) {
973 if((sc->rvp_bmap & 1 << i) == 0){
974 sc->rvp_bmap |= 1 << i;
979 if (sc->rvp_cnt++ == 0)
980 ic->ic_opmode = opmode;
982 if (opmode == IEEE80211_M_HOSTAP)
983 sc->cmdq_run = RUN_CMDQ_GO;
985 DPRINTF("rvp_id=%d bmap=%x rvp_cnt=%d\n",
986 rvp->rvp_id, sc->rvp_bmap, sc->rvp_cnt);
992 run_vap_delete(struct ieee80211vap *vap)
994 struct run_vap *rvp = RUN_VAP(vap);
996 struct ieee80211com *ic;
997 struct run_softc *sc;
1010 m_freem(rvp->beacon_mbuf);
1011 rvp->beacon_mbuf = NULL;
1013 rvp_id = rvp->rvp_id;
1014 sc->ratectl_run &= ~(1 << rvp_id);
1015 sc->rvp_bmap &= ~(1 << rvp_id);
1016 run_set_region_4(sc, RT2860_SKEY(rvp_id, 0), 0, 128);
1017 run_set_region_4(sc, RT2860_BCN_BASE(rvp_id), 0, 512);
1020 DPRINTF("vap=%p rvp_id=%d bmap=%x rvp_cnt=%d\n",
1021 vap, rvp_id, sc->rvp_bmap, sc->rvp_cnt);
1025 ieee80211_ratectl_deinit(vap);
1026 ieee80211_vap_detach(vap);
1027 kfree(rvp, M_80211_VAP);
1031 * There are numbers of functions need to be called in context thread.
1032 * Rather than creating taskqueue event for each of those functions,
1033 * here is all-for-one taskqueue callback function. This function
1034 * gurantees deferred functions are executed in the same order they
1036 * '& RUN_CMDQ_MASQ' is to loop cmdq[].
1039 run_cmdq_cb(void *arg, int pending)
1041 struct run_softc *sc = arg;
1044 /* call cmdq[].func locked */
1046 for (i = sc->cmdq_exec; sc->cmdq[i].func && pending;
1047 i = sc->cmdq_exec, pending--) {
1048 DPRINTFN(6, "cmdq_exec=%d pending=%d\n", i, pending);
1049 if (sc->cmdq_run == RUN_CMDQ_GO) {
1051 * If arg0 is NULL, callback func needs more
1052 * than one arg. So, pass ptr to cmdq struct.
1054 if (sc->cmdq[i].arg0)
1055 sc->cmdq[i].func(sc->cmdq[i].arg0);
1057 sc->cmdq[i].func(&sc->cmdq[i]);
1059 sc->cmdq[i].arg0 = NULL;
1060 sc->cmdq[i].func = NULL;
1062 sc->cmdq_exec &= RUN_CMDQ_MASQ;
1068 run_setup_tx_list(struct run_softc *sc, struct run_endpoint_queue *pq)
1070 struct run_tx_data *data;
1072 memset(pq, 0, sizeof(*pq));
1074 STAILQ_INIT(&pq->tx_qh);
1075 STAILQ_INIT(&pq->tx_fh);
1077 for (data = &pq->tx_data[0];
1078 data < &pq->tx_data[RUN_TX_RING_COUNT]; data++) {
1080 STAILQ_INSERT_TAIL(&pq->tx_fh, data, next);
1082 pq->tx_nfree = RUN_TX_RING_COUNT;
1086 run_unsetup_tx_list(struct run_softc *sc, struct run_endpoint_queue *pq)
1088 struct run_tx_data *data;
1090 /* make sure any subsequent use of the queues will fail */
1092 STAILQ_INIT(&pq->tx_fh);
1093 STAILQ_INIT(&pq->tx_qh);
1095 /* free up all node references and mbufs */
1096 for (data = &pq->tx_data[0];
1097 data < &pq->tx_data[RUN_TX_RING_COUNT]; data++) {
1098 if (data->m != NULL) {
1102 if (data->ni != NULL) {
1103 ieee80211_free_node(data->ni);
1110 run_load_microcode(struct run_softc *sc)
1112 usb_device_request_t req;
1113 const struct firmware *fw;
1117 const uint64_t *temp;
1120 wlan_assert_serialized();
1122 wlan_serialize_exit();
1123 fw = firmware_get("runfw");
1124 wlan_serialize_enter();
1126 device_printf(sc->sc_dev,
1127 "failed loadfirmware of file %s\n", "runfw");
1131 if (fw->datasize != 8192) {
1132 device_printf(sc->sc_dev,
1133 "invalid firmware size (should be 8KB)\n");
1139 * RT3071/RT3072 use a different firmware
1140 * run-rt2870 (8KB) contains both,
1141 * first half (4KB) is for rt2870,
1142 * last half is for rt3071.
1145 if ((sc->mac_ver) != 0x2860 &&
1146 (sc->mac_ver) != 0x2872 &&
1147 (sc->mac_ver) != 0x3070) {
1151 /* cheap sanity check */
1154 if (bytes != be64toh(0xffffff0210280210ULL)) {
1155 device_printf(sc->sc_dev, "firmware checksum failed\n");
1160 /* write microcode image */
1161 if (sc->sc_flags & RUN_FLAG_FWLOAD_NEEDED) {
1162 run_write_region_1(sc, RT2870_FW_BASE, base, 4096);
1163 run_write(sc, RT2860_H2M_MAILBOX_CID, 0xffffffff);
1164 run_write(sc, RT2860_H2M_MAILBOX_STATUS, 0xffffffff);
1167 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1168 req.bRequest = RT2870_RESET;
1169 USETW(req.wValue, 8);
1170 USETW(req.wIndex, 0);
1171 USETW(req.wLength, 0);
1172 if ((error = usbd_do_request(sc->sc_udev, &sc->sc_lock, &req, NULL))
1174 device_printf(sc->sc_dev, "firmware reset failed\n");
1180 run_write(sc, RT2860_H2M_BBPAGENT, 0);
1181 run_write(sc, RT2860_H2M_MAILBOX, 0);
1182 run_write(sc, RT2860_H2M_INTSRC, 0);
1183 if ((error = run_mcu_cmd(sc, RT2860_MCU_CMD_RFRESET, 0)) != 0)
1186 /* wait until microcontroller is ready */
1187 for (ntries = 0; ntries < 1000; ntries++) {
1188 if ((error = run_read(sc, RT2860_SYS_CTRL, &tmp)) != 0)
1190 if (tmp & RT2860_MCU_READY)
1194 if (ntries == 1000) {
1195 device_printf(sc->sc_dev,
1196 "timeout waiting for MCU to initialize\n");
1200 device_printf(sc->sc_dev, "firmware %s ver. %u.%u loaded\n",
1201 (base == fw->data) ? "RT2870" : "RT3071",
1202 *(base + 4092), *(base + 4093));
1205 firmware_put(fw, FIRMWARE_UNLOAD);
1210 run_reset(struct run_softc *sc)
1212 usb_device_request_t req;
1215 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1216 req.bRequest = RT2870_RESET;
1217 USETW(req.wValue, 1);
1218 USETW(req.wIndex, 0);
1219 USETW(req.wLength, 0);
1220 error = usbd_do_request(sc->sc_udev, &sc->sc_lock, &req, NULL);
1225 run_do_request(struct run_softc *sc,
1226 struct usb_device_request *req, void *data)
1231 RUN_LOCK_ASSERT(sc, MA_OWNED);
1234 err = usbd_do_request_flags(sc->sc_udev, &sc->sc_lock,
1235 req, data, 0, NULL, 250 /* ms */);
1238 DPRINTFN(1, "Control request failed, %s (retrying)\n",
1246 run_read(struct run_softc *sc, uint16_t reg, uint32_t *val)
1251 error = run_read_region_1(sc, reg, (uint8_t *)&tmp, sizeof tmp);
1253 *val = le32toh(tmp);
1260 run_read_region_1(struct run_softc *sc, uint16_t reg, uint8_t *buf, int len)
1262 usb_device_request_t req;
1264 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1265 req.bRequest = RT2870_READ_REGION_1;
1266 USETW(req.wValue, 0);
1267 USETW(req.wIndex, reg);
1268 USETW(req.wLength, len);
1270 return (run_do_request(sc, &req, buf));
1274 run_write_2(struct run_softc *sc, uint16_t reg, uint16_t val)
1276 usb_device_request_t req;
1278 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1279 req.bRequest = RT2870_WRITE_2;
1280 USETW(req.wValue, val);
1281 USETW(req.wIndex, reg);
1282 USETW(req.wLength, 0);
1284 return (run_do_request(sc, &req, NULL));
1288 run_write(struct run_softc *sc, uint16_t reg, uint32_t val)
1292 if ((error = run_write_2(sc, reg, val & 0xffff)) == 0)
1293 error = run_write_2(sc, reg + 2, val >> 16);
1298 run_write_region_1(struct run_softc *sc, uint16_t reg, const uint8_t *buf,
1304 * NB: the WRITE_REGION_1 command is not stable on RT2860.
1305 * We thus issue multiple WRITE_2 commands instead.
1307 KASSERT((len & 1) == 0, ("run_write_region_1: Data too long.\n"));
1308 for (i = 0; i < len && error == 0; i += 2)
1309 error = run_write_2(sc, reg + i, buf[i] | buf[i + 1] << 8);
1312 usb_device_request_t req;
1316 * NOTE: It appears the WRITE_REGION_1 command cannot be
1317 * passed a huge amount of data, which will crash the
1318 * firmware. Limit amount of data passed to 64-bytes at a
1326 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1327 req.bRequest = RT2870_WRITE_REGION_1;
1328 USETW(req.wValue, 0);
1329 USETW(req.wIndex, reg);
1330 USETW(req.wLength, delta);
1331 error = run_do_request(sc, &req, __DECONST(uint8_t *, buf));
1343 run_set_region_4(struct run_softc *sc, uint16_t reg, uint32_t val, int len)
1347 KASSERT((len & 3) == 0, ("run_set_region_4: Invalid data length.\n"));
1348 for (i = 0; i < len && error == 0; i += 4)
1349 error = run_write(sc, reg + i, val);
1354 run_efuse_read(struct run_softc *sc, uint16_t addr, uint16_t *val, int count)
1360 if ((error = run_read(sc, RT3070_EFUSE_CTRL, &tmp)) != 0)
1366 * Read one 16-byte block into registers EFUSE_DATA[0-3]:
1372 tmp &= ~(RT3070_EFSROM_MODE_MASK | RT3070_EFSROM_AIN_MASK);
1373 tmp |= (addr & ~0xf) << RT3070_EFSROM_AIN_SHIFT | RT3070_EFSROM_KICK;
1374 run_write(sc, RT3070_EFUSE_CTRL, tmp);
1375 for (ntries = 0; ntries < 100; ntries++) {
1376 if ((error = run_read(sc, RT3070_EFUSE_CTRL, &tmp)) != 0)
1378 if (!(tmp & RT3070_EFSROM_KICK))
1385 if ((tmp & RT3070_EFUSE_AOUT_MASK) == RT3070_EFUSE_AOUT_MASK) {
1386 *val = 0xffff; /* address not found */
1389 /* determine to which 32-bit register our 16-bit word belongs */
1390 reg = RT3070_EFUSE_DATA3 - (addr & 0xc);
1391 if ((error = run_read(sc, reg, &tmp)) != 0)
1394 tmp >>= (8 * (addr & 0x3));
1395 *val = (addr & 1) ? tmp >> 16 : tmp & 0xffff;
1400 /* Read 16-bit from eFUSE ROM for RT3xxx. */
1402 run_efuse_read_2(struct run_softc *sc, uint16_t addr, uint16_t *val)
1404 return (run_efuse_read(sc, addr, val, 2));
1408 run_eeprom_read_2(struct run_softc *sc, uint16_t addr, uint16_t *val)
1410 usb_device_request_t req;
1415 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1416 req.bRequest = RT2870_EEPROM_READ;
1417 USETW(req.wValue, 0);
1418 USETW(req.wIndex, addr);
1419 USETW(req.wLength, sizeof(tmp));
1421 error = usbd_do_request(sc->sc_udev, &sc->sc_lock, &req, &tmp);
1423 *val = le16toh(tmp);
1430 run_srom_read(struct run_softc *sc, uint16_t addr, uint16_t *val)
1432 /* either eFUSE ROM or EEPROM */
1433 return sc->sc_srom_read(sc, addr, val);
1437 run_rt2870_rf_write(struct run_softc *sc, uint32_t val)
1442 for (ntries = 0; ntries < 10; ntries++) {
1443 if ((error = run_read(sc, RT2860_RF_CSR_CFG0, &tmp)) != 0)
1445 if (!(tmp & RT2860_RF_REG_CTRL))
1451 return (run_write(sc, RT2860_RF_CSR_CFG0, val));
1455 run_rt3070_rf_read(struct run_softc *sc, uint8_t reg, uint8_t *val)
1460 for (ntries = 0; ntries < 100; ntries++) {
1461 if ((error = run_read(sc, RT3070_RF_CSR_CFG, &tmp)) != 0)
1463 if (!(tmp & RT3070_RF_KICK))
1469 tmp = RT3070_RF_KICK | reg << 8;
1470 if ((error = run_write(sc, RT3070_RF_CSR_CFG, tmp)) != 0)
1473 for (ntries = 0; ntries < 100; ntries++) {
1474 if ((error = run_read(sc, RT3070_RF_CSR_CFG, &tmp)) != 0)
1476 if (!(tmp & RT3070_RF_KICK))
1487 run_rt3070_rf_write(struct run_softc *sc, uint8_t reg, uint8_t val)
1492 for (ntries = 0; ntries < 10; ntries++) {
1493 if ((error = run_read(sc, RT3070_RF_CSR_CFG, &tmp)) != 0)
1495 if (!(tmp & RT3070_RF_KICK))
1501 tmp = RT3070_RF_WRITE | RT3070_RF_KICK | reg << 8 | val;
1502 return (run_write(sc, RT3070_RF_CSR_CFG, tmp));
1506 run_bbp_read(struct run_softc *sc, uint8_t reg, uint8_t *val)
1511 for (ntries = 0; ntries < 10; ntries++) {
1512 if ((error = run_read(sc, RT2860_BBP_CSR_CFG, &tmp)) != 0)
1514 if (!(tmp & RT2860_BBP_CSR_KICK))
1520 tmp = RT2860_BBP_CSR_READ | RT2860_BBP_CSR_KICK | reg << 8;
1521 if ((error = run_write(sc, RT2860_BBP_CSR_CFG, tmp)) != 0)
1524 for (ntries = 0; ntries < 10; ntries++) {
1525 if ((error = run_read(sc, RT2860_BBP_CSR_CFG, &tmp)) != 0)
1527 if (!(tmp & RT2860_BBP_CSR_KICK))
1538 run_bbp_write(struct run_softc *sc, uint8_t reg, uint8_t val)
1543 for (ntries = 0; ntries < 10; ntries++) {
1544 if ((error = run_read(sc, RT2860_BBP_CSR_CFG, &tmp)) != 0)
1546 if (!(tmp & RT2860_BBP_CSR_KICK))
1552 tmp = RT2860_BBP_CSR_KICK | reg << 8 | val;
1553 return (run_write(sc, RT2860_BBP_CSR_CFG, tmp));
1557 * Send a command to the 8051 microcontroller unit.
1560 run_mcu_cmd(struct run_softc *sc, uint8_t cmd, uint16_t arg)
1565 for (ntries = 0; ntries < 100; ntries++) {
1566 if ((error = run_read(sc, RT2860_H2M_MAILBOX, &tmp)) != 0)
1568 if (!(tmp & RT2860_H2M_BUSY))
1574 tmp = RT2860_H2M_BUSY | RT2860_TOKEN_NO_INTR << 16 | arg;
1575 if ((error = run_write(sc, RT2860_H2M_MAILBOX, tmp)) == 0)
1576 error = run_write(sc, RT2860_HOST_CMD, cmd);
1581 * Add `delta' (signed) to each 4-bit sub-word of a 32-bit word.
1582 * Used to adjust per-rate Tx power registers.
1584 static __inline uint32_t
1585 b4inc(uint32_t b32, int8_t delta)
1589 for (i = 0; i < 8; i++) {
1596 b32 = b32 >> 4 | b4 << 28;
1602 run_get_rf(uint16_t rev)
1605 case RT2860_RF_2820: return "RT2820";
1606 case RT2860_RF_2850: return "RT2850";
1607 case RT2860_RF_2720: return "RT2720";
1608 case RT2860_RF_2750: return "RT2750";
1609 case RT3070_RF_3020: return "RT3020";
1610 case RT3070_RF_2020: return "RT2020";
1611 case RT3070_RF_3021: return "RT3021";
1612 case RT3070_RF_3022: return "RT3022";
1613 case RT3070_RF_3052: return "RT3052";
1614 case RT3593_RF_3053: return "RT3053";
1615 case RT5592_RF_5592: return "RT5592";
1616 case RT5390_RF_5370: return "RT5370";
1617 case RT5390_RF_5372: return "RT5372";
1623 run_rt3593_get_txpower(struct run_softc *sc)
1628 /* Read power settings for 2GHz channels. */
1629 for (i = 0; i < 14; i += 2) {
1630 addr = (sc->ntxchains == 3) ? RT3593_EEPROM_PWR2GHZ_BASE1 :
1631 RT2860_EEPROM_PWR2GHZ_BASE1;
1632 run_srom_read(sc, addr + i / 2, &val);
1633 sc->txpow1[i + 0] = (int8_t)(val & 0xff);
1634 sc->txpow1[i + 1] = (int8_t)(val >> 8);
1636 addr = (sc->ntxchains == 3) ? RT3593_EEPROM_PWR2GHZ_BASE2 :
1637 RT2860_EEPROM_PWR2GHZ_BASE2;
1638 run_srom_read(sc, addr + i / 2, &val);
1639 sc->txpow2[i + 0] = (int8_t)(val & 0xff);
1640 sc->txpow2[i + 1] = (int8_t)(val >> 8);
1642 if (sc->ntxchains == 3) {
1643 run_srom_read(sc, RT3593_EEPROM_PWR2GHZ_BASE3 + i / 2,
1645 sc->txpow3[i + 0] = (int8_t)(val & 0xff);
1646 sc->txpow3[i + 1] = (int8_t)(val >> 8);
1649 /* Fix broken Tx power entries. */
1650 for (i = 0; i < 14; i++) {
1651 if (sc->txpow1[i] > 31)
1653 if (sc->txpow2[i] > 31)
1655 if (sc->ntxchains == 3) {
1656 if (sc->txpow3[i] > 31)
1660 /* Read power settings for 5GHz channels. */
1661 for (i = 0; i < 40; i += 2) {
1662 run_srom_read(sc, RT3593_EEPROM_PWR5GHZ_BASE1 + i / 2, &val);
1663 sc->txpow1[i + 14] = (int8_t)(val & 0xff);
1664 sc->txpow1[i + 15] = (int8_t)(val >> 8);
1666 run_srom_read(sc, RT3593_EEPROM_PWR5GHZ_BASE2 + i / 2, &val);
1667 sc->txpow2[i + 14] = (int8_t)(val & 0xff);
1668 sc->txpow2[i + 15] = (int8_t)(val >> 8);
1670 if (sc->ntxchains == 3) {
1671 run_srom_read(sc, RT3593_EEPROM_PWR5GHZ_BASE3 + i / 2,
1673 sc->txpow3[i + 14] = (int8_t)(val & 0xff);
1674 sc->txpow3[i + 15] = (int8_t)(val >> 8);
1680 run_get_txpower(struct run_softc *sc)
1685 /* Read power settings for 2GHz channels. */
1686 for (i = 0; i < 14; i += 2) {
1687 run_srom_read(sc, RT2860_EEPROM_PWR2GHZ_BASE1 + i / 2, &val);
1688 sc->txpow1[i + 0] = (int8_t)(val & 0xff);
1689 sc->txpow1[i + 1] = (int8_t)(val >> 8);
1691 if (sc->mac_ver != 0x5390) {
1693 RT2860_EEPROM_PWR2GHZ_BASE2 + i / 2, &val);
1694 sc->txpow2[i + 0] = (int8_t)(val & 0xff);
1695 sc->txpow2[i + 1] = (int8_t)(val >> 8);
1698 /* Fix broken Tx power entries. */
1699 for (i = 0; i < 14; i++) {
1700 if (sc->mac_ver >= 0x5390) {
1701 if (sc->txpow1[i] < 0 || sc->txpow1[i] > 27)
1704 if (sc->txpow1[i] < 0 || sc->txpow1[i] > 31)
1707 if (sc->mac_ver > 0x5390) {
1708 if (sc->txpow2[i] < 0 || sc->txpow2[i] > 27)
1710 } else if (sc->mac_ver < 0x5390) {
1711 if (sc->txpow2[i] < 0 || sc->txpow2[i] > 31)
1714 DPRINTF("chan %d: power1=%d, power2=%d\n",
1715 rt2860_rf2850[i].chan, sc->txpow1[i], sc->txpow2[i]);
1717 /* Read power settings for 5GHz channels. */
1718 for (i = 0; i < 40; i += 2) {
1719 run_srom_read(sc, RT2860_EEPROM_PWR5GHZ_BASE1 + i / 2, &val);
1720 sc->txpow1[i + 14] = (int8_t)(val & 0xff);
1721 sc->txpow1[i + 15] = (int8_t)(val >> 8);
1723 run_srom_read(sc, RT2860_EEPROM_PWR5GHZ_BASE2 + i / 2, &val);
1724 sc->txpow2[i + 14] = (int8_t)(val & 0xff);
1725 sc->txpow2[i + 15] = (int8_t)(val >> 8);
1727 /* Fix broken Tx power entries. */
1728 for (i = 0; i < 40; i++ ) {
1729 if (sc->mac_ver != 0x5592) {
1730 if (sc->txpow1[14 + i] < -7 || sc->txpow1[14 + i] > 15)
1731 sc->txpow1[14 + i] = 5;
1732 if (sc->txpow2[14 + i] < -7 || sc->txpow2[14 + i] > 15)
1733 sc->txpow2[14 + i] = 5;
1735 DPRINTF("chan %d: power1=%d, power2=%d\n",
1736 rt2860_rf2850[14 + i].chan, sc->txpow1[14 + i],
1737 sc->txpow2[14 + i]);
1742 run_read_eeprom(struct run_softc *sc)
1744 int8_t delta_2ghz, delta_5ghz;
1749 /* check whether the ROM is eFUSE ROM or EEPROM */
1750 sc->sc_srom_read = run_eeprom_read_2;
1751 if (sc->mac_ver >= 0x3070) {
1752 run_read(sc, RT3070_EFUSE_CTRL, &tmp);
1753 DPRINTF("EFUSE_CTRL=0x%08x\n", tmp);
1754 if ((tmp & RT3070_SEL_EFUSE) || sc->mac_ver == 0x3593)
1755 sc->sc_srom_read = run_efuse_read_2;
1758 /* read ROM version */
1759 run_srom_read(sc, RT2860_EEPROM_VERSION, &val);
1760 DPRINTF("EEPROM rev=%d, FAE=%d\n", val & 0xff, val >> 8);
1762 /* read MAC address */
1763 run_srom_read(sc, RT2860_EEPROM_MAC01, &val);
1764 sc->sc_bssid[0] = val & 0xff;
1765 sc->sc_bssid[1] = val >> 8;
1766 run_srom_read(sc, RT2860_EEPROM_MAC23, &val);
1767 sc->sc_bssid[2] = val & 0xff;
1768 sc->sc_bssid[3] = val >> 8;
1769 run_srom_read(sc, RT2860_EEPROM_MAC45, &val);
1770 sc->sc_bssid[4] = val & 0xff;
1771 sc->sc_bssid[5] = val >> 8;
1773 if (sc->mac_ver < 0x3593) {
1774 /* read vender BBP settings */
1775 for (i = 0; i < 10; i++) {
1776 run_srom_read(sc, RT2860_EEPROM_BBP_BASE + i, &val);
1777 sc->bbp[i].val = val & 0xff;
1778 sc->bbp[i].reg = val >> 8;
1779 DPRINTF("BBP%d=0x%02x\n", sc->bbp[i].reg,
1782 if (sc->mac_ver >= 0x3071) {
1783 /* read vendor RF settings */
1784 for (i = 0; i < 10; i++) {
1785 run_srom_read(sc, RT3071_EEPROM_RF_BASE + i,
1787 sc->rf[i].val = val & 0xff;
1788 sc->rf[i].reg = val >> 8;
1789 DPRINTF("RF%d=0x%02x\n", sc->rf[i].reg,
1795 /* read RF frequency offset from EEPROM */
1796 run_srom_read(sc, (sc->mac_ver != 0x3593) ? RT2860_EEPROM_FREQ_LEDS :
1797 RT3593_EEPROM_FREQ, &val);
1798 sc->freq = ((val & 0xff) != 0xff) ? val & 0xff : 0;
1799 DPRINTF("EEPROM freq offset %d\n", sc->freq & 0xff);
1801 run_srom_read(sc, (sc->mac_ver != 0x3593) ? RT2860_EEPROM_FREQ_LEDS :
1802 RT3593_EEPROM_FREQ_LEDS, &val);
1803 if (val >> 8 != 0xff) {
1804 /* read LEDs operating mode */
1805 sc->leds = val >> 8;
1806 run_srom_read(sc, (sc->mac_ver != 0x3593) ? RT2860_EEPROM_LED1 :
1807 RT3593_EEPROM_LED1, &sc->led[0]);
1808 run_srom_read(sc, (sc->mac_ver != 0x3593) ? RT2860_EEPROM_LED2 :
1809 RT3593_EEPROM_LED2, &sc->led[1]);
1810 run_srom_read(sc, (sc->mac_ver != 0x3593) ? RT2860_EEPROM_LED3 :
1811 RT3593_EEPROM_LED3, &sc->led[2]);
1813 /* broken EEPROM, use default settings */
1815 sc->led[0] = 0x5555;
1816 sc->led[1] = 0x2221;
1817 sc->led[2] = 0x5627; /* differs from RT2860 */
1819 DPRINTF("EEPROM LED mode=0x%02x, LEDs=0x%04x/0x%04x/0x%04x\n",
1820 sc->leds, sc->led[0], sc->led[1], sc->led[2]);
1822 /* read RF information */
1823 if (sc->mac_ver == 0x5390 || sc->mac_ver ==0x5392)
1824 run_srom_read(sc, 0x00, &val);
1826 run_srom_read(sc, RT2860_EEPROM_ANTENNA, &val);
1828 if (val == 0xffff) {
1829 device_printf(sc->sc_dev,
1830 "invalid EEPROM antenna info, using default\n");
1831 DPRINTF("invalid EEPROM antenna info, using default\n");
1832 if (sc->mac_ver == 0x3572) {
1833 /* default to RF3052 2T2R */
1834 sc->rf_rev = RT3070_RF_3052;
1837 } else if (sc->mac_ver >= 0x3070) {
1838 /* default to RF3020 1T1R */
1839 sc->rf_rev = RT3070_RF_3020;
1843 /* default to RF2820 1T2R */
1844 sc->rf_rev = RT2860_RF_2820;
1849 if (sc->mac_ver == 0x5390 || sc->mac_ver ==0x5392) {
1851 run_srom_read(sc, RT2860_EEPROM_ANTENNA, &val);
1853 sc->rf_rev = (val >> 8) & 0xf;
1854 sc->ntxchains = (val >> 4) & 0xf;
1855 sc->nrxchains = val & 0xf;
1857 DPRINTF("EEPROM RF rev=0x%04x chains=%dT%dR\n",
1858 sc->rf_rev, sc->ntxchains, sc->nrxchains);
1860 /* check if RF supports automatic Tx access gain control */
1861 run_srom_read(sc, RT2860_EEPROM_CONFIG, &val);
1862 DPRINTF("EEPROM CFG 0x%04x\n", val);
1863 /* check if driver should patch the DAC issue */
1864 if ((val >> 8) != 0xff)
1865 sc->patch_dac = (val >> 15) & 1;
1866 if ((val & 0xff) != 0xff) {
1867 sc->ext_5ghz_lna = (val >> 3) & 1;
1868 sc->ext_2ghz_lna = (val >> 2) & 1;
1869 /* check if RF supports automatic Tx access gain control */
1870 sc->calib_2ghz = sc->calib_5ghz = (val >> 1) & 1;
1871 /* check if we have a hardware radio switch */
1872 sc->rfswitch = val & 1;
1875 /* Read Tx power settings. */
1876 if (sc->mac_ver == 0x3593)
1877 run_rt3593_get_txpower(sc);
1879 run_get_txpower(sc);
1881 /* read Tx power compensation for each Tx rate */
1882 run_srom_read(sc, RT2860_EEPROM_DELTAPWR, &val);
1883 delta_2ghz = delta_5ghz = 0;
1884 if ((val & 0xff) != 0xff && (val & 0x80)) {
1885 delta_2ghz = val & 0xf;
1886 if (!(val & 0x40)) /* negative number */
1887 delta_2ghz = -delta_2ghz;
1890 if ((val & 0xff) != 0xff && (val & 0x80)) {
1891 delta_5ghz = val & 0xf;
1892 if (!(val & 0x40)) /* negative number */
1893 delta_5ghz = -delta_5ghz;
1895 DPRINTF("power compensation=%d (2GHz), %d (5GHz)\n",
1896 delta_2ghz, delta_5ghz);
1898 for (ridx = 0; ridx < 5; ridx++) {
1901 run_srom_read(sc, RT2860_EEPROM_RPWR + ridx * 2, &val);
1903 run_srom_read(sc, RT2860_EEPROM_RPWR + ridx * 2 + 1, &val);
1904 reg |= (uint32_t)val << 16;
1906 sc->txpow20mhz[ridx] = reg;
1907 sc->txpow40mhz_2ghz[ridx] = b4inc(reg, delta_2ghz);
1908 sc->txpow40mhz_5ghz[ridx] = b4inc(reg, delta_5ghz);
1910 DPRINTF("ridx %d: power 20MHz=0x%08x, 40MHz/2GHz=0x%08x, "
1911 "40MHz/5GHz=0x%08x\n", ridx, sc->txpow20mhz[ridx],
1912 sc->txpow40mhz_2ghz[ridx], sc->txpow40mhz_5ghz[ridx]);
1915 /* Read RSSI offsets and LNA gains from EEPROM. */
1916 run_srom_read(sc, (sc->mac_ver != 0x3593) ? RT2860_EEPROM_RSSI1_2GHZ :
1917 RT3593_EEPROM_RSSI1_2GHZ, &val);
1918 sc->rssi_2ghz[0] = val & 0xff; /* Ant A */
1919 sc->rssi_2ghz[1] = val >> 8; /* Ant B */
1920 run_srom_read(sc, (sc->mac_ver != 0x3593) ? RT2860_EEPROM_RSSI2_2GHZ :
1921 RT3593_EEPROM_RSSI2_2GHZ, &val);
1922 if (sc->mac_ver >= 0x3070) {
1923 if (sc->mac_ver == 0x3593) {
1924 sc->txmixgain_2ghz = 0;
1925 sc->rssi_2ghz[2] = val & 0xff; /* Ant C */
1928 * On RT3070 chips (limited to 2 Rx chains), this ROM
1929 * field contains the Tx mixer gain for the 2GHz band.
1931 if ((val & 0xff) != 0xff)
1932 sc->txmixgain_2ghz = val & 0x7;
1934 DPRINTF("tx mixer gain=%u (2GHz)\n", sc->txmixgain_2ghz);
1936 sc->rssi_2ghz[2] = val & 0xff; /* Ant C */
1937 if (sc->mac_ver == 0x3593)
1938 run_srom_read(sc, RT3593_EEPROM_LNA_5GHZ, &val);
1939 sc->lna[2] = val >> 8; /* channel group 2 */
1941 run_srom_read(sc, (sc->mac_ver != 0x3593) ? RT2860_EEPROM_RSSI1_5GHZ :
1942 RT3593_EEPROM_RSSI1_5GHZ, &val);
1943 sc->rssi_5ghz[0] = val & 0xff; /* Ant A */
1944 sc->rssi_5ghz[1] = val >> 8; /* Ant B */
1945 run_srom_read(sc, (sc->mac_ver != 0x3593) ? RT2860_EEPROM_RSSI2_5GHZ :
1946 RT3593_EEPROM_RSSI2_5GHZ, &val);
1947 if (sc->mac_ver == 0x3572) {
1949 * On RT3572 chips (limited to 2 Rx chains), this ROM
1950 * field contains the Tx mixer gain for the 5GHz band.
1952 if ((val & 0xff) != 0xff)
1953 sc->txmixgain_5ghz = val & 0x7;
1954 DPRINTF("tx mixer gain=%u (5GHz)\n", sc->txmixgain_5ghz);
1956 sc->rssi_5ghz[2] = val & 0xff; /* Ant C */
1957 if (sc->mac_ver == 0x3593) {
1958 sc->txmixgain_5ghz = 0;
1959 run_srom_read(sc, RT3593_EEPROM_LNA_5GHZ, &val);
1961 sc->lna[3] = val >> 8; /* channel group 3 */
1963 run_srom_read(sc, (sc->mac_ver != 0x3593) ? RT2860_EEPROM_LNA :
1964 RT3593_EEPROM_LNA, &val);
1965 sc->lna[0] = val & 0xff; /* channel group 0 */
1966 sc->lna[1] = val >> 8; /* channel group 1 */
1968 /* fix broken 5GHz LNA entries */
1969 if (sc->lna[2] == 0 || sc->lna[2] == 0xff) {
1970 DPRINTF("invalid LNA for channel group %d\n", 2);
1971 sc->lna[2] = sc->lna[1];
1973 if (sc->lna[3] == 0 || sc->lna[3] == 0xff) {
1974 DPRINTF("invalid LNA for channel group %d\n", 3);
1975 sc->lna[3] = sc->lna[1];
1978 /* fix broken RSSI offset entries */
1979 for (ant = 0; ant < 3; ant++) {
1980 if (sc->rssi_2ghz[ant] < -10 || sc->rssi_2ghz[ant] > 10) {
1981 DPRINTF("invalid RSSI%d offset: %d (2GHz)\n",
1982 ant + 1, sc->rssi_2ghz[ant]);
1983 sc->rssi_2ghz[ant] = 0;
1985 if (sc->rssi_5ghz[ant] < -10 || sc->rssi_5ghz[ant] > 10) {
1986 DPRINTF("invalid RSSI%d offset: %d (5GHz)\n",
1987 ant + 1, sc->rssi_5ghz[ant]);
1988 sc->rssi_5ghz[ant] = 0;
1994 static struct ieee80211_node *
1995 run_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
1997 return kmalloc(sizeof (struct run_node), M_DEVBUF, M_WAITOK | M_ZERO);
2001 run_media_change(struct ifnet *ifp)
2003 struct ieee80211vap *vap = ifp->if_softc;
2004 struct ieee80211com *ic = vap->iv_ic;
2005 const struct ieee80211_txparam *tp;
2006 struct run_softc *sc = ic->ic_ifp->if_softc;
2012 error = ieee80211_media_change(ifp);
2013 if (error != ENETRESET) {
2018 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
2019 if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) {
2020 struct ieee80211_node *ni;
2021 struct run_node *rn;
2023 rate = ic->ic_sup_rates[ic->ic_curmode].
2024 rs_rates[tp->ucastrate] & IEEE80211_RATE_VAL;
2025 for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
2026 if (rt2860_rates[ridx].rate == rate)
2028 ni = ieee80211_ref_node(vap->iv_bss);
2029 rn = (struct run_node *)ni;
2030 rn->fix_ridx = ridx;
2031 DPRINTF("rate=%d, fix_ridx=%d\n", rate, rn->fix_ridx);
2032 ieee80211_free_node(ni);
2036 if ((ifp->if_flags & IFF_UP) &&
2037 (ifp->if_flags & IFF_RUNNING)){
2038 run_init_locked(sc);
2048 run_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2050 const struct ieee80211_txparam *tp;
2051 struct ieee80211com *ic = vap->iv_ic;
2052 struct run_softc *sc = ic->ic_ifp->if_softc;
2053 struct run_vap *rvp = RUN_VAP(vap);
2054 enum ieee80211_state ostate;
2058 uint8_t restart_ratectl = 0;
2059 uint8_t bid = 1 << rvp->rvp_id;
2061 ostate = vap->iv_state;
2062 DPRINTF("%s -> %s\n",
2063 ieee80211_state_name[ostate],
2064 ieee80211_state_name[nstate]);
2068 ratectl = sc->ratectl_run; /* remember current state */
2069 sc->ratectl_run = RUN_RATECTL_OFF;
2070 usb_callout_stop(&sc->ratectl_ch);
2072 if (ostate == IEEE80211_S_RUN) {
2073 /* turn link LED off */
2074 run_set_leds(sc, RT2860_LED_RADIO);
2078 case IEEE80211_S_INIT:
2079 restart_ratectl = 1;
2081 if (ostate != IEEE80211_S_RUN)
2085 sc->runbmap &= ~bid;
2087 /* abort TSF synchronization if there is no vap running */
2088 if (--sc->running == 0) {
2089 run_read(sc, RT2860_BCN_TIME_CFG, &tmp);
2090 run_write(sc, RT2860_BCN_TIME_CFG,
2091 tmp & ~(RT2860_BCN_TX_EN | RT2860_TSF_TIMER_EN |
2092 RT2860_TBTT_TIMER_EN));
2096 case IEEE80211_S_RUN:
2097 if (!(sc->runbmap & bid)) {
2099 restart_ratectl = 1;
2103 m_freem(rvp->beacon_mbuf);
2104 rvp->beacon_mbuf = NULL;
2106 switch (vap->iv_opmode) {
2107 case IEEE80211_M_HOSTAP:
2108 case IEEE80211_M_MBSS:
2109 sc->ap_running |= bid;
2110 ic->ic_opmode = vap->iv_opmode;
2111 run_update_beacon_cb(vap);
2113 case IEEE80211_M_IBSS:
2114 sc->adhoc_running |= bid;
2115 if (!sc->ap_running)
2116 ic->ic_opmode = vap->iv_opmode;
2117 run_update_beacon_cb(vap);
2119 case IEEE80211_M_STA:
2120 sc->sta_running |= bid;
2121 if (!sc->ap_running && !sc->adhoc_running)
2122 ic->ic_opmode = vap->iv_opmode;
2124 /* read statistic counters (clear on read) */
2125 run_read_region_1(sc, RT2860_TX_STA_CNT0,
2126 (uint8_t *)sta, sizeof sta);
2130 ic->ic_opmode = vap->iv_opmode;
2134 if (vap->iv_opmode != IEEE80211_M_MONITOR) {
2135 struct ieee80211_node *ni;
2137 if (ic->ic_bsschan == IEEE80211_CHAN_ANYC) {
2141 run_updateslot(ic->ic_ifp);
2143 run_set_txpreamble(sc);
2144 run_set_basicrates(sc);
2145 ni = ieee80211_ref_node(vap->iv_bss);
2146 IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid);
2147 run_set_bssid(sc, ni->ni_bssid);
2148 ieee80211_free_node(ni);
2149 run_enable_tsf_sync(sc);
2151 /* enable automatic rate adaptation */
2152 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
2153 if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
2157 /* turn link LED on */
2158 run_set_leds(sc, RT2860_LED_RADIO |
2159 (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan) ?
2160 RT2860_LED_LINK_2GHZ : RT2860_LED_LINK_5GHZ));
2164 DPRINTFN(6, "undefined case\n");
2168 /* restart amrr for running VAPs */
2169 if ((sc->ratectl_run = ratectl) && restart_ratectl)
2170 usb_callout_reset(&sc->ratectl_ch, hz, run_ratectl_to, sc);
2174 return(rvp->newstate(vap, nstate, arg));
2179 run_wme_update_cb(void *arg)
2181 struct ieee80211com *ic = arg;
2182 struct run_softc *sc = ic->ic_ifp->if_softc;
2183 struct ieee80211_wme_state *wmesp = &ic->ic_wme;
2186 RUN_LOCK_ASSERT(sc, MA_OWNED);
2188 /* update MAC TX configuration registers */
2189 for (aci = 0; aci < WME_NUM_AC; aci++) {
2190 error = run_write(sc, RT2860_EDCA_AC_CFG(aci),
2191 wmesp->wme_params[aci].wmep_logcwmax << 16 |
2192 wmesp->wme_params[aci].wmep_logcwmin << 12 |
2193 wmesp->wme_params[aci].wmep_aifsn << 8 |
2194 wmesp->wme_params[aci].wmep_txopLimit);
2195 if (error) goto err;
2198 /* update SCH/DMA registers too */
2199 error = run_write(sc, RT2860_WMM_AIFSN_CFG,
2200 wmesp->wme_params[WME_AC_VO].wmep_aifsn << 12 |
2201 wmesp->wme_params[WME_AC_VI].wmep_aifsn << 8 |
2202 wmesp->wme_params[WME_AC_BK].wmep_aifsn << 4 |
2203 wmesp->wme_params[WME_AC_BE].wmep_aifsn);
2204 if (error) goto err;
2205 error = run_write(sc, RT2860_WMM_CWMIN_CFG,
2206 wmesp->wme_params[WME_AC_VO].wmep_logcwmin << 12 |
2207 wmesp->wme_params[WME_AC_VI].wmep_logcwmin << 8 |
2208 wmesp->wme_params[WME_AC_BK].wmep_logcwmin << 4 |
2209 wmesp->wme_params[WME_AC_BE].wmep_logcwmin);
2210 if (error) goto err;
2211 error = run_write(sc, RT2860_WMM_CWMAX_CFG,
2212 wmesp->wme_params[WME_AC_VO].wmep_logcwmax << 12 |
2213 wmesp->wme_params[WME_AC_VI].wmep_logcwmax << 8 |
2214 wmesp->wme_params[WME_AC_BK].wmep_logcwmax << 4 |
2215 wmesp->wme_params[WME_AC_BE].wmep_logcwmax);
2216 if (error) goto err;
2217 error = run_write(sc, RT2860_WMM_TXOP0_CFG,
2218 wmesp->wme_params[WME_AC_BK].wmep_txopLimit << 16 |
2219 wmesp->wme_params[WME_AC_BE].wmep_txopLimit);
2220 if (error) goto err;
2221 error = run_write(sc, RT2860_WMM_TXOP1_CFG,
2222 wmesp->wme_params[WME_AC_VO].wmep_txopLimit << 16 |
2223 wmesp->wme_params[WME_AC_VI].wmep_txopLimit);
2227 DPRINTF("WME update failed\n");
2233 run_wme_update(struct ieee80211com *ic)
2235 struct run_softc *sc = ic->ic_ifp->if_softc;
2237 #if 0 /* XXX swildner */
2238 /* sometime called without lock */
2239 if (mtx_owned(&ic->ic_comlock.mtx)) {
2240 uint32_t i = RUN_CMDQ_GET(&sc->cmdq_store);
2241 DPRINTF("cmdq_store=%d\n", i);
2242 sc->cmdq[i].func = run_wme_update_cb;
2243 sc->cmdq[i].arg0 = ic;
2244 ieee80211_runtask(ic, &sc->cmdq_task);
2250 run_wme_update_cb(ic);
2253 /* return whatever, upper layer desn't care anyway */
2258 run_key_update_begin(struct ieee80211vap *vap)
2261 * To avoid out-of-order events, both run_key_set() and
2262 * _delete() are deferred and handled by run_cmdq_cb().
2263 * So, there is nothing we need to do here.
2268 run_key_update_end(struct ieee80211vap *vap)
2274 run_key_set_cb(void *arg)
2276 struct run_cmdq *cmdq = arg;
2277 struct ieee80211vap *vap = cmdq->arg1;
2278 struct ieee80211_key *k = cmdq->k;
2279 struct ieee80211com *ic = vap->iv_ic;
2280 struct run_softc *sc = ic->ic_ifp->if_softc;
2281 struct ieee80211_node *ni;
2283 uint16_t base, associd;
2284 uint8_t mode, wcid, iv[8];
2286 RUN_LOCK_ASSERT(sc, MA_OWNED);
2288 if (vap->iv_opmode == IEEE80211_M_HOSTAP)
2289 ni = ieee80211_find_vap_node(&ic->ic_sta, vap, cmdq->mac);
2292 associd = (ni != NULL) ? ni->ni_associd : 0;
2294 /* map net80211 cipher to RT2860 security mode */
2295 switch (k->wk_cipher->ic_cipher) {
2296 case IEEE80211_CIPHER_WEP:
2297 if(k->wk_keylen < 8)
2298 mode = RT2860_MODE_WEP40;
2300 mode = RT2860_MODE_WEP104;
2302 case IEEE80211_CIPHER_TKIP:
2303 mode = RT2860_MODE_TKIP;
2305 case IEEE80211_CIPHER_AES_CCM:
2306 mode = RT2860_MODE_AES_CCMP;
2309 DPRINTF("undefined case\n");
2313 DPRINTFN(1, "associd=%x, keyix=%d, mode=%x, type=%s, tx=%s, rx=%s\n",
2314 associd, k->wk_keyix, mode,
2315 (k->wk_flags & IEEE80211_KEY_GROUP) ? "group" : "pairwise",
2316 (k->wk_flags & IEEE80211_KEY_XMIT) ? "on" : "off",
2317 (k->wk_flags & IEEE80211_KEY_RECV) ? "on" : "off");
2319 if (k->wk_flags & IEEE80211_KEY_GROUP) {
2320 wcid = 0; /* NB: update WCID0 for group keys */
2321 base = RT2860_SKEY(RUN_VAP(vap)->rvp_id, k->wk_keyix);
2323 wcid = (vap->iv_opmode == IEEE80211_M_STA) ?
2324 1 : RUN_AID2WCID(associd);
2325 base = RT2860_PKEY(wcid);
2328 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP) {
2329 if(run_write_region_1(sc, base, k->wk_key, 16))
2331 if(run_write_region_1(sc, base + 16, &k->wk_key[16], 8)) /* wk_txmic */
2333 if(run_write_region_1(sc, base + 24, &k->wk_key[24], 8)) /* wk_rxmic */
2336 /* roundup len to 16-bit: XXX fix write_region_1() instead */
2337 if(run_write_region_1(sc, base, k->wk_key, (k->wk_keylen + 1) & ~1))
2341 if (!(k->wk_flags & IEEE80211_KEY_GROUP) ||
2342 (k->wk_flags & (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV))) {
2343 /* set initial packet number in IV+EIV */
2344 if (k->wk_cipher == IEEE80211_CIPHER_WEP) {
2345 memset(iv, 0, sizeof iv);
2346 iv[3] = vap->iv_def_txkey << 6;
2348 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP) {
2349 iv[0] = k->wk_keytsc >> 8;
2350 iv[1] = (iv[0] | 0x20) & 0x7f;
2351 iv[2] = k->wk_keytsc;
2353 iv[0] = k->wk_keytsc;
2354 iv[1] = k->wk_keytsc >> 8;
2357 iv[3] = k->wk_keyix << 6 | IEEE80211_WEP_EXTIV;
2358 iv[4] = k->wk_keytsc >> 16;
2359 iv[5] = k->wk_keytsc >> 24;
2360 iv[6] = k->wk_keytsc >> 32;
2361 iv[7] = k->wk_keytsc >> 40;
2363 if (run_write_region_1(sc, RT2860_IVEIV(wcid), iv, 8))
2367 if (k->wk_flags & IEEE80211_KEY_GROUP) {
2368 /* install group key */
2369 if (run_read(sc, RT2860_SKEY_MODE_0_7, &attr))
2371 attr &= ~(0xf << (k->wk_keyix * 4));
2372 attr |= mode << (k->wk_keyix * 4);
2373 if (run_write(sc, RT2860_SKEY_MODE_0_7, attr))
2376 /* install pairwise key */
2377 if (run_read(sc, RT2860_WCID_ATTR(wcid), &attr))
2379 attr = (attr & ~0xf) | (mode << 1) | RT2860_RX_PKEY_EN;
2380 if (run_write(sc, RT2860_WCID_ATTR(wcid), attr))
2384 /* TODO create a pass-thru key entry? */
2386 /* need wcid to delete the right key later */
2391 * Don't have to be deferred, but in order to keep order of
2392 * execution, i.e. with run_key_delete(), defer this and let
2393 * run_cmdq_cb() maintain the order.
2398 run_key_set(struct ieee80211vap *vap, struct ieee80211_key *k,
2399 const uint8_t mac[IEEE80211_ADDR_LEN])
2401 struct ieee80211com *ic = vap->iv_ic;
2402 struct run_softc *sc = ic->ic_ifp->if_softc;
2405 i = RUN_CMDQ_GET(&sc->cmdq_store);
2406 DPRINTF("cmdq_store=%d\n", i);
2407 sc->cmdq[i].func = run_key_set_cb;
2408 sc->cmdq[i].arg0 = NULL;
2409 sc->cmdq[i].arg1 = vap;
2411 IEEE80211_ADDR_COPY(sc->cmdq[i].mac, mac);
2412 ieee80211_runtask(ic, &sc->cmdq_task);
2415 * To make sure key will be set when hostapd
2416 * calls iv_key_set() before if_init().
2418 if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
2420 sc->cmdq_key_set = RUN_CMDQ_GO;
2428 * If wlan is destroyed without being brought down i.e. without
2429 * wlan down or wpa_cli terminate, this function is called after
2430 * vap is gone. Don't refer it.
2433 run_key_delete_cb(void *arg)
2435 struct run_cmdq *cmdq = arg;
2436 struct run_softc *sc = cmdq->arg1;
2437 struct ieee80211_key *k = &cmdq->key;
2441 RUN_LOCK_ASSERT(sc, MA_OWNED);
2443 if (k->wk_flags & IEEE80211_KEY_GROUP) {
2444 /* remove group key */
2445 DPRINTF("removing group key\n");
2446 run_read(sc, RT2860_SKEY_MODE_0_7, &attr);
2447 attr &= ~(0xf << (k->wk_keyix * 4));
2448 run_write(sc, RT2860_SKEY_MODE_0_7, attr);
2450 /* remove pairwise key */
2451 DPRINTF("removing key for wcid %x\n", k->wk_pad);
2452 /* matching wcid was written to wk_pad in run_key_set() */
2454 run_read(sc, RT2860_WCID_ATTR(wcid), &attr);
2456 run_write(sc, RT2860_WCID_ATTR(wcid), attr);
2457 run_set_region_4(sc, RT2860_WCID_ENTRY(wcid), 0, 8);
2467 run_key_delete(struct ieee80211vap *vap, struct ieee80211_key *k)
2469 struct ieee80211com *ic = vap->iv_ic;
2470 struct run_softc *sc = ic->ic_ifp->if_softc;
2471 struct ieee80211_key *k0;
2475 * When called back, key might be gone. So, make a copy
2476 * of some values need to delete keys before deferring.
2477 * But, because of LOR with node lock, cannot use lock here.
2478 * So, use atomic instead.
2480 i = RUN_CMDQ_GET(&sc->cmdq_store);
2481 DPRINTF("cmdq_store=%d\n", i);
2482 sc->cmdq[i].func = run_key_delete_cb;
2483 sc->cmdq[i].arg0 = NULL;
2484 sc->cmdq[i].arg1 = sc;
2485 k0 = &sc->cmdq[i].key;
2486 k0->wk_flags = k->wk_flags;
2487 k0->wk_keyix = k->wk_keyix;
2488 /* matching wcid was written to wk_pad in run_key_set() */
2489 k0->wk_pad = k->wk_pad;
2490 ieee80211_runtask(ic, &sc->cmdq_task);
2491 return (1); /* return fake success */
2496 run_ratectl_to(void *arg)
2498 struct run_softc *sc = arg;
2500 /* do it in a process context, so it can go sleep */
2501 ieee80211_runtask(sc->sc_ifp->if_l2com, &sc->ratectl_task);
2502 /* next timeout will be rescheduled in the callback task */
2507 run_ratectl_cb(void *arg, int pending)
2509 struct run_softc *sc = arg;
2510 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
2511 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2516 if (sc->rvp_cnt > 1 || vap->iv_opmode != IEEE80211_M_STA) {
2518 * run_reset_livelock() doesn't do anything with AMRR,
2519 * but Ralink wants us to call it every 1 sec. So, we
2520 * piggyback here rather than creating another callout.
2521 * Livelock may occur only in HOSTAP or IBSS mode
2522 * (when h/w is sending beacons).
2525 run_reset_livelock(sc);
2526 /* just in case, there are some stats to drain */
2531 ieee80211_iterate_nodes(&ic->ic_sta, run_iter_func, sc);
2534 if(sc->ratectl_run != RUN_RATECTL_OFF)
2535 usb_callout_reset(&sc->ratectl_ch, hz, run_ratectl_to, sc);
2540 run_drain_fifo(void *arg)
2542 struct run_softc *sc = arg;
2543 struct ifnet *ifp = sc->sc_ifp;
2545 uint16_t (*wstat)[3];
2546 uint8_t wcid, mcs, pid;
2549 RUN_LOCK_ASSERT(sc, MA_OWNED);
2552 /* drain Tx status FIFO (maxsize = 16) */
2553 run_read(sc, RT2860_TX_STAT_FIFO, &stat);
2554 DPRINTFN(4, "tx stat 0x%08x\n", stat);
2555 if (!(stat & RT2860_TXQ_VLD))
2558 wcid = (stat >> RT2860_TXQ_WCID_SHIFT) & 0xff;
2560 /* if no ACK was requested, no feedback is available */
2561 if (!(stat & RT2860_TXQ_ACKREQ) || wcid > RT2870_WCID_MAX ||
2566 * Even though each stat is Tx-complete-status like format,
2567 * the device can poll stats. Because there is no guarantee
2568 * that the referring node is still around when read the stats.
2569 * So that, if we use ieee80211_ratectl_tx_update(), we will
2570 * have hard time not to refer already freed node.
2572 * To eliminate such page faults, we poll stats in softc.
2573 * Then, update the rates later with ieee80211_ratectl_tx_update().
2575 wstat = &(sc->wcid_stats[wcid]);
2576 (*wstat)[RUN_TXCNT]++;
2577 if (stat & RT2860_TXQ_OK)
2578 (*wstat)[RUN_SUCCESS]++;
2582 * Check if there were retries, ie if the Tx success rate is
2583 * different from the requested rate. Note that it works only
2584 * because we do not allow rate fallback from OFDM to CCK.
2586 mcs = (stat >> RT2860_TXQ_MCS_SHIFT) & 0x7f;
2587 pid = (stat >> RT2860_TXQ_PID_SHIFT) & 0xf;
2588 if ((retry = pid -1 - mcs) > 0) {
2589 (*wstat)[RUN_TXCNT] += retry;
2590 (*wstat)[RUN_RETRY] += retry;
2593 DPRINTFN(3, "count=%d\n", sc->fifo_cnt);
2599 run_iter_func(void *arg, struct ieee80211_node *ni)
2601 struct run_softc *sc = arg;
2602 struct ieee80211vap *vap = ni->ni_vap;
2603 struct ieee80211com *ic = ni->ni_ic;
2604 struct ifnet *ifp = ic->ic_ifp;
2605 struct run_node *rn = (void *)ni;
2606 union run_stats sta[2];
2607 uint16_t (*wstat)[3];
2608 int txcnt, success, retrycnt, error;
2612 /* Check for special case */
2613 if (sc->rvp_cnt <= 1 && vap->iv_opmode == IEEE80211_M_STA &&
2617 if (sc->rvp_cnt <= 1 && (vap->iv_opmode == IEEE80211_M_IBSS ||
2618 vap->iv_opmode == IEEE80211_M_STA)) {
2619 /* read statistic counters (clear on read) and update AMRR state */
2620 error = run_read_region_1(sc, RT2860_TX_STA_CNT0, (uint8_t *)sta,
2625 /* count failed TX as errors */
2626 ifp->if_oerrors += le16toh(sta[0].error.fail);
2628 retrycnt = le16toh(sta[1].tx.retry);
2629 success = le16toh(sta[1].tx.success);
2630 txcnt = retrycnt + success + le16toh(sta[0].error.fail);
2632 DPRINTFN(3, "retrycnt=%d success=%d failcnt=%d\n",
2633 retrycnt, success, le16toh(sta[0].error.fail));
2635 wstat = &(sc->wcid_stats[RUN_AID2WCID(ni->ni_associd)]);
2637 if (wstat == &(sc->wcid_stats[0]) ||
2638 wstat > &(sc->wcid_stats[RT2870_WCID_MAX]))
2641 txcnt = (*wstat)[RUN_TXCNT];
2642 success = (*wstat)[RUN_SUCCESS];
2643 retrycnt = (*wstat)[RUN_RETRY];
2644 DPRINTFN(3, "retrycnt=%d txcnt=%d success=%d\n",
2645 retrycnt, txcnt, success);
2647 memset(wstat, 0, sizeof(*wstat));
2650 ieee80211_ratectl_tx_update(vap, ni, &txcnt, &success, &retrycnt);
2651 rn->amrr_ridx = ieee80211_ratectl_rate(ni, NULL, 0);
2656 DPRINTFN(3, "ridx=%d\n", rn->amrr_ridx);
2660 run_newassoc_cb(void *arg)
2662 struct run_cmdq *cmdq = arg;
2663 struct ieee80211_node *ni = cmdq->arg1;
2664 struct run_softc *sc = ni->ni_vap->iv_ic->ic_ifp->if_softc;
2665 uint8_t wcid = cmdq->wcid;
2667 RUN_LOCK_ASSERT(sc, MA_OWNED);
2669 run_write_region_1(sc, RT2860_WCID_ENTRY(wcid),
2670 ni->ni_macaddr, IEEE80211_ADDR_LEN);
2672 memset(&(sc->wcid_stats[wcid]), 0, sizeof(sc->wcid_stats[wcid]));
2676 run_newassoc(struct ieee80211_node *ni, int isnew)
2678 struct run_node *rn = (void *)ni;
2679 struct ieee80211_rateset *rs = &ni->ni_rates;
2680 struct ieee80211vap *vap = ni->ni_vap;
2681 struct ieee80211com *ic = vap->iv_ic;
2682 struct run_softc *sc = ic->ic_ifp->if_softc;
2688 char ethstr[ETHER_ADDRSTRLEN + 1];
2691 wcid = (vap->iv_opmode == IEEE80211_M_STA) ?
2692 1 : RUN_AID2WCID(ni->ni_associd);
2694 if (wcid > RT2870_WCID_MAX) {
2695 device_printf(sc->sc_dev, "wcid=%d out of range\n", wcid);
2699 /* only interested in true associations */
2700 if (isnew && ni->ni_associd != 0) {
2703 * This function could is called though timeout function.
2706 uint32_t cnt = RUN_CMDQ_GET(&sc->cmdq_store);
2707 DPRINTF("cmdq_store=%d\n", cnt);
2708 sc->cmdq[cnt].func = run_newassoc_cb;
2709 sc->cmdq[cnt].arg0 = NULL;
2710 sc->cmdq[cnt].arg1 = ni;
2711 sc->cmdq[cnt].wcid = wcid;
2712 ieee80211_runtask(ic, &sc->cmdq_task);
2715 DPRINTF("new assoc isnew=%d associd=%x addr=%s\n",
2716 isnew, ni->ni_associd, kether_ntoa(ni->ni_macaddr, ethstr));
2718 for (i = 0; i < rs->rs_nrates; i++) {
2719 rate = rs->rs_rates[i] & IEEE80211_RATE_VAL;
2720 /* convert 802.11 rate to hardware rate index */
2721 for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
2722 if (rt2860_rates[ridx].rate == rate)
2725 /* determine rate of control response frames */
2726 for (j = i; j >= 0; j--) {
2727 if ((rs->rs_rates[j] & IEEE80211_RATE_BASIC) &&
2728 rt2860_rates[rn->ridx[i]].phy ==
2729 rt2860_rates[rn->ridx[j]].phy)
2733 rn->ctl_ridx[i] = rn->ridx[j];
2735 /* no basic rate found, use mandatory one */
2736 rn->ctl_ridx[i] = rt2860_rates[ridx].ctl_ridx;
2738 DPRINTF("rate=0x%02x ridx=%d ctl_ridx=%d\n",
2739 rs->rs_rates[i], rn->ridx[i], rn->ctl_ridx[i]);
2741 rate = vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)].mgmtrate;
2742 for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
2743 if (rt2860_rates[ridx].rate == rate)
2745 rn->mgt_ridx = ridx;
2746 DPRINTF("rate=%d, mgmt_ridx=%d\n", rate, rn->mgt_ridx);
2749 if(sc->ratectl_run != RUN_RATECTL_OFF)
2750 usb_callout_reset(&sc->ratectl_ch, hz, run_ratectl_to, sc);
2755 * Return the Rx chain with the highest RSSI for a given frame.
2757 static __inline uint8_t
2758 run_maxrssi_chain(struct run_softc *sc, const struct rt2860_rxwi *rxwi)
2760 uint8_t rxchain = 0;
2762 if (sc->nrxchains > 1) {
2763 if (rxwi->rssi[1] > rxwi->rssi[rxchain])
2765 if (sc->nrxchains > 2)
2766 if (rxwi->rssi[2] > rxwi->rssi[rxchain])
2773 run_rx_frame(struct run_softc *sc, struct mbuf *m, uint32_t dmalen)
2775 struct ifnet *ifp = sc->sc_ifp;
2776 struct ieee80211com *ic = ifp->if_l2com;
2777 struct ieee80211_frame *wh;
2778 struct ieee80211_node *ni;
2779 struct rt2870_rxd *rxd;
2780 struct rt2860_rxwi *rxwi;
2782 uint16_t len, rxwisize;
2786 rxwi = mtod(m, struct rt2860_rxwi *);
2787 len = le16toh(rxwi->len) & 0xfff;
2788 rxwisize = sizeof(struct rt2860_rxwi);
2789 if (sc->mac_ver == 0x5592)
2790 rxwisize += sizeof(uint64_t);
2791 else if (sc->mac_ver == 0x3593)
2792 rxwisize += sizeof(uint32_t);
2793 if (__predict_false(len > dmalen)) {
2796 DPRINTF("bad RXWI length %u > %u\n", len, dmalen);
2799 /* Rx descriptor is located at the end */
2800 rxd = (struct rt2870_rxd *)(mtod(m, caddr_t) + dmalen);
2801 flags = le32toh(rxd->flags);
2803 if (__predict_false(flags & (RT2860_RX_CRCERR | RT2860_RX_ICVERR))) {
2806 DPRINTF("%s error.\n", (flags & RT2860_RX_CRCERR)?"CRC":"ICV");
2810 m->m_data += rxwisize;
2811 m->m_pkthdr.len = m->m_len -= rxwisize;
2813 wh = mtod(m, struct ieee80211_frame *);
2815 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
2816 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
2817 m->m_flags |= M_WEP;
2820 if (flags & RT2860_RX_L2PAD) {
2821 DPRINTFN(8, "received RT2860_RX_L2PAD frame\n");
2825 ni = ieee80211_find_rxnode(ic,
2826 mtod(m, struct ieee80211_frame_min *));
2828 if (__predict_false(flags & RT2860_RX_MICERR)) {
2829 /* report MIC failures to net80211 for TKIP */
2831 ieee80211_notify_michael_failure(ni->ni_vap, wh,
2835 DPRINTF("MIC error. Someone is lying.\n");
2839 ant = run_maxrssi_chain(sc, rxwi);
2840 rssi = rxwi->rssi[ant];
2841 nf = run_rssi2dbm(sc, rssi, ant);
2843 m->m_pkthdr.rcvif = ifp;
2844 m->m_pkthdr.len = m->m_len = len;
2847 (void)ieee80211_input(ni, m, rssi, nf);
2848 ieee80211_free_node(ni);
2850 (void)ieee80211_input_all(ic, m, rssi, nf);
2853 if (__predict_false(ieee80211_radiotap_active(ic))) {
2854 struct run_rx_radiotap_header *tap = &sc->sc_rxtap;
2858 tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
2859 tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
2860 tap->wr_antsignal = rssi;
2861 tap->wr_antenna = ant;
2862 tap->wr_dbm_antsignal = run_rssi2dbm(sc, rssi, ant);
2863 tap->wr_rate = 2; /* in case it can't be found below */
2864 phy = le16toh(rxwi->phy);
2865 switch (phy & RT2860_PHY_MODE) {
2866 case RT2860_PHY_CCK:
2867 switch ((phy & RT2860_PHY_MCS) & ~RT2860_PHY_SHPRE) {
2868 case 0: tap->wr_rate = 2; break;
2869 case 1: tap->wr_rate = 4; break;
2870 case 2: tap->wr_rate = 11; break;
2871 case 3: tap->wr_rate = 22; break;
2873 if (phy & RT2860_PHY_SHPRE)
2874 tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
2876 case RT2860_PHY_OFDM:
2877 switch (phy & RT2860_PHY_MCS) {
2878 case 0: tap->wr_rate = 12; break;
2879 case 1: tap->wr_rate = 18; break;
2880 case 2: tap->wr_rate = 24; break;
2881 case 3: tap->wr_rate = 36; break;
2882 case 4: tap->wr_rate = 48; break;
2883 case 5: tap->wr_rate = 72; break;
2884 case 6: tap->wr_rate = 96; break;
2885 case 7: tap->wr_rate = 108; break;
2893 run_bulk_rx_callback(struct usb_xfer *xfer, usb_error_t error)
2895 struct run_softc *sc = usbd_xfer_softc(xfer);
2896 struct ifnet *ifp = sc->sc_ifp;
2897 struct mbuf *m = NULL;
2903 rxwisize = sizeof(struct rt2860_rxwi);
2904 if (sc->mac_ver == 0x5592)
2905 rxwisize += sizeof(uint64_t);
2906 else if (sc->mac_ver == 0x3593)
2907 rxwisize += sizeof(uint32_t);
2909 usbd_xfer_status(xfer, &xferlen, NULL, NULL, NULL);
2911 switch (USB_GET_STATE(xfer)) {
2912 case USB_ST_TRANSFERRED:
2914 DPRINTFN(15, "rx done, actlen=%d\n", xferlen);
2916 if (xferlen < (int)(sizeof(uint32_t) + rxwisize +
2917 sizeof(struct rt2870_rxd))) {
2918 DPRINTF("xfer too short %d\n", xferlen);
2928 if (sc->rx_m == NULL) {
2929 sc->rx_m = m_getjcl(MB_DONTWAIT, MT_DATA, M_PKTHDR,
2930 MJUMPAGESIZE /* xfer can be bigger than MCLBYTES */);
2932 if (sc->rx_m == NULL) {
2933 DPRINTF("could not allocate mbuf - idle with stall\n");
2935 usbd_xfer_set_stall(xfer);
2936 usbd_xfer_set_frames(xfer, 0);
2939 * Directly loading a mbuf cluster into DMA to
2940 * save some data copying. This works because
2941 * there is only one cluster.
2943 usbd_xfer_set_frame_data(xfer, 0,
2944 mtod(sc->rx_m, caddr_t), RUN_MAX_RXSZ);
2945 usbd_xfer_set_frames(xfer, 1);
2947 usbd_transfer_submit(xfer);
2950 default: /* Error */
2951 if (error != USB_ERR_CANCELLED) {
2952 /* try to clear stall first */
2953 usbd_xfer_set_stall(xfer);
2955 if (error == USB_ERR_TIMEOUT)
2956 device_printf(sc->sc_dev, "device timeout\n");
2962 if (sc->rx_m != NULL) {
2972 /* inputting all the frames must be last */
2976 m->m_pkthdr.len = m->m_len = xferlen;
2978 /* HW can aggregate multiple 802.11 frames in a single USB xfer */
2980 dmalen = le32toh(*mtod(m, uint32_t *)) & 0xffff;
2982 if ((dmalen >= (uint32_t)-8) || (dmalen == 0) ||
2983 ((dmalen & 3) != 0)) {
2984 DPRINTF("bad DMA length %u\n", dmalen);
2987 if ((dmalen + 8) > (uint32_t)xferlen) {
2988 DPRINTF("bad DMA length %u > %d\n",
2989 dmalen + 8, xferlen);
2993 /* If it is the last one or a single frame, we won't copy. */
2994 if ((xferlen -= dmalen + 8) <= 8) {
2995 /* trim 32-bit DMA-len header */
2997 m->m_pkthdr.len = m->m_len -= 4;
2998 run_rx_frame(sc, m, dmalen);
2999 m = NULL; /* don't free source buffer */
3003 /* copy aggregated frames to another mbuf */
3004 m0 = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
3005 if (__predict_false(m0 == NULL)) {
3006 DPRINTF("could not allocate mbuf\n");
3010 m_copydata(m, 4 /* skip 32-bit DMA-len header */,
3011 dmalen + sizeof(struct rt2870_rxd), mtod(m0, caddr_t));
3012 m0->m_pkthdr.len = m0->m_len =
3013 dmalen + sizeof(struct rt2870_rxd);
3014 run_rx_frame(sc, m0, dmalen);
3016 /* update data ptr */
3017 m->m_data += dmalen + 8;
3018 m->m_pkthdr.len = m->m_len -= dmalen + 8;
3021 /* make sure we free the source buffer, if any */
3028 run_tx_free(struct run_endpoint_queue *pq,
3029 struct run_tx_data *data, int txerr)
3031 if (data->m != NULL) {
3032 if (data->m->m_flags & M_TXCB)
3033 ieee80211_process_callback(data->ni, data->m,
3034 txerr ? ETIMEDOUT : 0);
3038 if (data->ni == NULL) {
3039 DPRINTF("no node\n");
3041 ieee80211_free_node(data->ni);
3046 STAILQ_INSERT_TAIL(&pq->tx_fh, data, next);
3051 run_bulk_tx_callbackN(struct usb_xfer *xfer, usb_error_t error, u_int index)
3053 struct run_softc *sc = usbd_xfer_softc(xfer);
3054 struct ifnet *ifp = sc->sc_ifp;
3055 struct ieee80211com *ic = ifp->if_l2com;
3056 struct run_tx_data *data;
3057 struct ieee80211vap *vap = NULL;
3058 struct usb_page_cache *pc;
3059 struct run_endpoint_queue *pq = &sc->sc_epq[index];
3061 usb_frlength_t size;
3065 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
3067 switch (USB_GET_STATE(xfer)) {
3068 case USB_ST_TRANSFERRED:
3069 DPRINTFN(11, "transfer complete: %d "
3070 "bytes @ index %d\n", actlen, index);
3072 data = usbd_xfer_get_priv(xfer);
3074 run_tx_free(pq, data, 0);
3075 ifq_clr_oactive(&ifp->if_snd);
3077 usbd_xfer_set_priv(xfer, NULL);
3084 data = STAILQ_FIRST(&pq->tx_qh);
3088 STAILQ_REMOVE_HEAD(&pq->tx_qh, next);
3091 size = (sc->mac_ver == 0x5592) ?
3092 sizeof(data->desc) + sizeof(uint32_t) : sizeof(data->desc);
3093 if ((m->m_pkthdr.len +
3094 size + 3 + 8) > RUN_MAX_TXSZ) {
3095 DPRINTF("data overflow, %u bytes\n",
3100 run_tx_free(pq, data, 1);
3105 pc = usbd_xfer_get_frame(xfer, 0);
3106 usbd_copy_in(pc, 0, &data->desc, size);
3107 usbd_m_copy_in(pc, size, m, 0, m->m_pkthdr.len);
3108 size += m->m_pkthdr.len;
3110 * Align end on a 4-byte boundary, pad 8 bytes (CRC +
3111 * 4-byte padding), and be sure to zero those trailing
3114 usbd_frame_zero(pc, size, ((-size) & 3) + 8);
3115 size += ((-size) & 3) + 8;
3117 vap = data->ni->ni_vap;
3118 if (ieee80211_radiotap_active_vap(vap)) {
3119 struct run_tx_radiotap_header *tap = &sc->sc_txtap;
3120 struct rt2860_txwi *txwi =
3121 (struct rt2860_txwi *)(&data->desc + sizeof(struct rt2870_txd));
3123 tap->wt_rate = rt2860_rates[data->ridx].rate;
3124 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
3125 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
3126 tap->wt_hwqueue = index;
3127 if (le16toh(txwi->phy) & RT2860_PHY_SHPRE)
3128 tap->wt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
3130 ieee80211_radiotap_tx(vap, m);
3133 DPRINTFN(11, "sending frame len=%u/%u @ index %d\n",
3134 m->m_pkthdr.len, size, index);
3136 usbd_xfer_set_frame_len(xfer, 0, size);
3137 usbd_xfer_set_priv(xfer, data);
3139 usbd_transfer_submit(xfer);
3141 run_start_locked(ifp);
3146 DPRINTF("USB transfer error, %s\n",
3147 usbd_errstr(error));
3149 data = usbd_xfer_get_priv(xfer);
3154 if(data->ni != NULL)
3155 vap = data->ni->ni_vap;
3156 run_tx_free(pq, data, error);
3157 usbd_xfer_set_priv(xfer, NULL);
3160 vap = TAILQ_FIRST(&ic->ic_vaps);
3162 if (error != USB_ERR_CANCELLED) {
3163 if (error == USB_ERR_TIMEOUT) {
3164 device_printf(sc->sc_dev, "device timeout\n");
3165 uint32_t i = RUN_CMDQ_GET(&sc->cmdq_store);
3166 DPRINTF("cmdq_store=%d\n", i);
3167 sc->cmdq[i].func = run_usb_timeout_cb;
3168 sc->cmdq[i].arg0 = vap;
3169 ieee80211_runtask(ic, &sc->cmdq_task);
3173 * Try to clear stall first, also if other
3174 * errors occur, hence clearing stall
3175 * introduces a 50 ms delay:
3177 usbd_xfer_set_stall(xfer);
3185 run_bulk_tx_callback0(struct usb_xfer *xfer, usb_error_t error)
3187 run_bulk_tx_callbackN(xfer, error, 0);
3191 run_bulk_tx_callback1(struct usb_xfer *xfer, usb_error_t error)
3193 run_bulk_tx_callbackN(xfer, error, 1);
3197 run_bulk_tx_callback2(struct usb_xfer *xfer, usb_error_t error)
3199 run_bulk_tx_callbackN(xfer, error, 2);
3203 run_bulk_tx_callback3(struct usb_xfer *xfer, usb_error_t error)
3205 run_bulk_tx_callbackN(xfer, error, 3);
3209 run_bulk_tx_callback4(struct usb_xfer *xfer, usb_error_t error)
3211 run_bulk_tx_callbackN(xfer, error, 4);
3215 run_bulk_tx_callback5(struct usb_xfer *xfer, usb_error_t error)
3217 run_bulk_tx_callbackN(xfer, error, 5);
3221 run_set_tx_desc(struct run_softc *sc, struct run_tx_data *data)
3223 struct mbuf *m = data->m;
3224 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
3225 struct ieee80211vap *vap = data->ni->ni_vap;
3226 struct ieee80211_frame *wh;
3227 struct rt2870_txd *txd;
3228 struct rt2860_txwi *txwi;
3229 uint16_t xferlen, txwisize;
3231 uint8_t ridx = data->ridx;
3234 /* get MCS code from rate index */
3235 mcs = rt2860_rates[ridx].mcs;
3237 txwisize = (sc->mac_ver == 0x5592) ?
3238 sizeof(*txwi) + sizeof(uint32_t) : sizeof(*txwi);
3239 xferlen = txwisize + m->m_pkthdr.len;
3241 /* roundup to 32-bit alignment */
3242 xferlen = (xferlen + 3) & ~3;
3244 txd = (struct rt2870_txd *)&data->desc;
3245 txd->len = htole16(xferlen);
3247 wh = mtod(m, struct ieee80211_frame *);
3250 * Ether both are true or both are false, the header
3251 * are nicely aligned to 32-bit. So, no L2 padding.
3253 if(IEEE80211_HAS_ADDR4(wh) == IEEE80211_QOS_HAS_SEQ(wh))
3258 /* setup TX Wireless Information */
3259 txwi = (struct rt2860_txwi *)(txd + 1);
3260 txwi->len = htole16(m->m_pkthdr.len - pad);
3261 if (rt2860_rates[ridx].phy == IEEE80211_T_DS) {
3262 txwi->phy = htole16(RT2860_PHY_CCK);
3263 if (ridx != RT2860_RIDX_CCK1 &&
3264 (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
3265 mcs |= RT2860_PHY_SHPRE;
3267 txwi->phy = htole16(RT2860_PHY_OFDM);
3268 txwi->phy |= htole16(mcs);
3270 /* check if RTS/CTS or CTS-to-self protection is required */
3271 if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
3272 (m->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold ||
3273 ((ic->ic_flags & IEEE80211_F_USEPROT) &&
3274 rt2860_rates[ridx].phy == IEEE80211_T_OFDM)))
3275 txwi->txop |= RT2860_TX_TXOP_HT;
3277 txwi->txop |= RT2860_TX_TXOP_BACKOFF;
3279 if (vap->iv_opmode != IEEE80211_M_STA && !IEEE80211_QOS_HAS_SEQ(wh))
3280 txwi->xflags |= RT2860_TX_NSEQ;
3283 /* This function must be called locked */
3285 run_tx(struct run_softc *sc, struct mbuf *m, struct ieee80211_node *ni)
3287 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
3288 struct ieee80211vap *vap = ni->ni_vap;
3289 struct ieee80211_frame *wh;
3290 struct ieee80211_channel *chan;
3291 const struct ieee80211_txparam *tp;
3292 struct run_node *rn = (void *)ni;
3293 struct run_tx_data *data;
3294 struct rt2870_txd *txd;
3295 struct rt2860_txwi *txwi;
3307 #if 0 /* XXX swildner: lock needed? */
3308 RUN_LOCK_ASSERT(sc, MA_OWNED);
3311 wh = mtod(m, struct ieee80211_frame *);
3313 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
3316 * There are 7 bulk endpoints: 1 for RX
3317 * and 6 for TX (4 EDCAs + HCCA + Prio).
3318 * Update 03-14-2009: some devices like the Planex GW-US300MiniS
3319 * seem to have only 4 TX bulk endpoints (Fukaumi Naoki).
3321 if ((hasqos = IEEE80211_QOS_HAS_SEQ(wh))) {
3324 if(IEEE80211_HAS_ADDR4(wh))
3325 frm = ((struct ieee80211_qosframe_addr4 *)wh)->i_qos;
3327 frm =((struct ieee80211_qosframe *)wh)->i_qos;
3329 qos = le16toh(*(const uint16_t *)frm);
3330 tid = qos & IEEE80211_QOS_TID;
3331 qid = TID_TO_WME_AC(tid);
3337 qflags = (qid < 4) ? RT2860_TX_QSEL_EDCA : RT2860_TX_QSEL_HCCA;
3339 DPRINTFN(8, "qos %d\tqid %d\ttid %d\tqflags %x\n",
3340 qos, qid, tid, qflags);
3342 chan = (ni->ni_chan != IEEE80211_CHAN_ANYC)?ni->ni_chan:ic->ic_curchan;
3343 tp = &vap->iv_txparms[ieee80211_chan2mode(chan)];
3345 /* pickup a rate index */
3346 if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
3347 type != IEEE80211_FC0_TYPE_DATA) {
3348 ridx = (ic->ic_curmode == IEEE80211_MODE_11A) ?
3349 RT2860_RIDX_OFDM6 : RT2860_RIDX_CCK1;
3350 ctl_ridx = rt2860_rates[ridx].ctl_ridx;
3352 if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
3353 ridx = rn->fix_ridx;
3355 ridx = rn->amrr_ridx;
3356 ctl_ridx = rt2860_rates[ridx].ctl_ridx;
3359 if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
3360 (!hasqos || (qos & IEEE80211_QOS_ACKPOLICY) !=
3361 IEEE80211_QOS_ACKPOLICY_NOACK)) {
3362 xflags |= RT2860_TX_ACK;
3363 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
3364 dur = rt2860_rates[ctl_ridx].sp_ack_dur;
3366 dur = rt2860_rates[ctl_ridx].lp_ack_dur;
3367 USETW(wh->i_dur, dur);
3370 /* reserve slots for mgmt packets, just in case */
3371 if (sc->sc_epq[qid].tx_nfree < 3) {
3372 DPRINTFN(10, "tx ring %d is full\n", qid);
3376 data = STAILQ_FIRST(&sc->sc_epq[qid].tx_fh);
3377 STAILQ_REMOVE_HEAD(&sc->sc_epq[qid].tx_fh, next);
3378 sc->sc_epq[qid].tx_nfree--;
3380 txd = (struct rt2870_txd *)&data->desc;
3381 txd->flags = qflags;
3382 txwi = (struct rt2860_txwi *)(txd + 1);
3383 txwi->xflags = xflags;
3384 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
3387 txwi->wcid = (vap->iv_opmode == IEEE80211_M_STA) ?
3388 1 : RUN_AID2WCID(ni->ni_associd);
3390 /* clear leftover garbage bits */
3398 run_set_tx_desc(sc, data);
3401 * The chip keeps track of 2 kind of Tx stats,
3402 * * TX_STAT_FIFO, for per WCID stats, and
3403 * * TX_STA_CNT0 for all-TX-in-one stats.
3405 * To use FIFO stats, we need to store MCS into the driver-private
3406 * PacketID field. So that, we can tell whose stats when we read them.
3407 * We add 1 to the MCS because setting the PacketID field to 0 means
3408 * that we don't want feedback in TX_STAT_FIFO.
3409 * And, that's what we want for STA mode, since TX_STA_CNT0 does the job.
3411 * FIFO stats doesn't count Tx with WCID 0xff, so we do this in run_tx().
3413 if (sc->rvp_cnt > 1 || vap->iv_opmode == IEEE80211_M_HOSTAP ||
3414 vap->iv_opmode == IEEE80211_M_MBSS) {
3415 uint16_t pid = (rt2860_rates[ridx].mcs + 1) & 0xf;
3416 txwi->len |= htole16(pid << RT2860_TX_PID_SHIFT);
3419 * Unlike PCI based devices, we don't get any interrupt from
3420 * USB devices, so we simulate FIFO-is-full interrupt here.
3421 * Ralink recomends to drain FIFO stats every 100 ms, but 16 slots
3422 * quickly get fulled. To prevent overflow, increment a counter on
3423 * every FIFO stat request, so we know how many slots are left.
3424 * We do this only in HOSTAP or multiple vap mode since FIFO stats
3425 * are used only in those modes.
3426 * We just drain stats. AMRR gets updated every 1 sec by
3427 * run_ratectl_cb() via callout.
3428 * Call it early. Otherwise overflow.
3430 if (sc->fifo_cnt++ == 10) {
3432 * With multiple vaps or if_bridge, if_start() is called
3433 * with a non-sleepable lock, tcpinp. So, need to defer.
3435 uint32_t i = RUN_CMDQ_GET(&sc->cmdq_store);
3436 DPRINTFN(6, "cmdq_store=%d\n", i);
3437 sc->cmdq[i].func = run_drain_fifo;
3438 sc->cmdq[i].arg0 = sc;
3439 ieee80211_runtask(ic, &sc->cmdq_task);
3443 STAILQ_INSERT_TAIL(&sc->sc_epq[qid].tx_qh, data, next);
3446 usbd_transfer_start(sc->sc_xfer[qid]);
3449 DPRINTFN(8, "sending data frame len=%d rate=%d qid=%d\n",
3450 m->m_pkthdr.len + (int)(sizeof(struct rt2870_txd) +
3451 sizeof(struct rt2860_txwi)), rt2860_rates[ridx].rate, qid);
3457 run_tx_mgt(struct run_softc *sc, struct mbuf *m, struct ieee80211_node *ni)
3459 struct ifnet *ifp = sc->sc_ifp;
3460 struct ieee80211com *ic = ifp->if_l2com;
3461 struct run_node *rn = (void *)ni;
3462 struct run_tx_data *data;
3463 struct ieee80211_frame *wh;
3464 struct rt2870_txd *txd;
3465 struct rt2860_txwi *txwi;
3467 uint8_t ridx = rn->mgt_ridx;
3472 RUN_LOCK_ASSERT(sc, MA_OWNED);
3474 wh = mtod(m, struct ieee80211_frame *);
3476 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
3478 /* tell hardware to add timestamp for probe responses */
3480 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
3481 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
3482 wflags |= RT2860_TX_TS;
3483 else if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
3484 xflags |= RT2860_TX_ACK;
3486 dur = ieee80211_ack_duration(ic->ic_rt, rt2860_rates[ridx].rate,
3487 ic->ic_flags & IEEE80211_F_SHPREAMBLE);
3488 USETW(wh->i_dur, dur);
3491 if (sc->sc_epq[0].tx_nfree == 0) {
3492 /* let caller free mbuf */
3493 ifq_set_oactive(&ifp->if_snd);
3496 data = STAILQ_FIRST(&sc->sc_epq[0].tx_fh);
3497 STAILQ_REMOVE_HEAD(&sc->sc_epq[0].tx_fh, next);
3498 sc->sc_epq[0].tx_nfree--;
3500 txd = (struct rt2870_txd *)&data->desc;
3501 txd->flags = RT2860_TX_QSEL_EDCA;
3502 txwi = (struct rt2860_txwi *)(txd + 1);
3504 txwi->flags = wflags;
3505 txwi->xflags = xflags;
3506 txwi->txop = 0; /* clear leftover garbage bits */
3512 run_set_tx_desc(sc, data);
3514 DPRINTFN(10, "sending mgt frame len=%d rate=%d\n", m->m_pkthdr.len +
3515 (int)(sizeof(struct rt2870_txd) + sizeof(struct rt2860_txwi)),
3516 rt2860_rates[ridx].rate);
3518 STAILQ_INSERT_TAIL(&sc->sc_epq[0].tx_qh, data, next);
3520 usbd_transfer_start(sc->sc_xfer[0]);
3526 run_sendprot(struct run_softc *sc,
3527 const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate)
3529 struct ieee80211com *ic = ni->ni_ic;
3530 struct ieee80211_frame *wh;
3531 struct run_tx_data *data;
3532 struct rt2870_txd *txd;
3533 struct rt2860_txwi *txwi;
3545 RUN_LOCK_ASSERT(sc, MA_OWNED);
3547 KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY,
3548 ("protection %d", prot));
3550 wh = mtod(m, struct ieee80211_frame *);
3551 pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
3552 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
3554 protrate = ieee80211_ctl_rate(ic->ic_rt, rate);
3555 ackrate = ieee80211_ack_rate(ic->ic_rt, rate);
3557 isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0;
3558 dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort)
3559 + ieee80211_ack_duration(ic->ic_rt, rate, isshort);
3560 wflags = RT2860_TX_FRAG;
3562 /* check that there are free slots before allocating the mbuf */
3563 if (sc->sc_epq[0].tx_nfree == 0) {
3564 /* let caller free mbuf */
3565 ifq_set_oactive(&sc->sc_ifp->if_snd);
3569 if (prot == IEEE80211_PROT_RTSCTS) {
3570 /* NB: CTS is the same size as an ACK */
3571 dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort);
3572 xflags |= RT2860_TX_ACK;
3573 mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur);
3575 mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur);
3577 if (mprot == NULL) {
3578 sc->sc_ifp->if_oerrors++;
3579 DPRINTF("could not allocate mbuf\n");
3583 data = STAILQ_FIRST(&sc->sc_epq[0].tx_fh);
3584 STAILQ_REMOVE_HEAD(&sc->sc_epq[0].tx_fh, next);
3585 sc->sc_epq[0].tx_nfree--;
3587 txd = (struct rt2870_txd *)&data->desc;
3588 txd->flags = RT2860_TX_QSEL_EDCA;
3589 txwi = (struct rt2860_txwi *)(txd + 1);
3591 txwi->flags = wflags;
3592 txwi->xflags = xflags;
3593 txwi->txop = 0; /* clear leftover garbage bits */
3596 data->ni = ieee80211_ref_node(ni);
3598 for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
3599 if (rt2860_rates[ridx].rate == protrate)
3603 run_set_tx_desc(sc, data);
3605 DPRINTFN(1, "sending prot len=%u rate=%u\n",
3606 m->m_pkthdr.len, rate);
3608 STAILQ_INSERT_TAIL(&sc->sc_epq[0].tx_qh, data, next);
3610 usbd_transfer_start(sc->sc_xfer[0]);
3616 run_tx_param(struct run_softc *sc, struct mbuf *m, struct ieee80211_node *ni,
3617 const struct ieee80211_bpf_params *params)
3619 struct ieee80211com *ic = ni->ni_ic;
3620 struct ieee80211_frame *wh;
3621 struct run_tx_data *data;
3622 struct rt2870_txd *txd;
3623 struct rt2860_txwi *txwi;
3627 uint8_t opflags = 0;
3631 RUN_LOCK_ASSERT(sc, MA_OWNED);
3633 KASSERT(params != NULL, ("no raw xmit params"));
3635 wh = mtod(m, struct ieee80211_frame *);
3636 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
3638 rate = params->ibp_rate0;
3639 if (!ieee80211_isratevalid(ic->ic_rt, rate)) {
3640 /* let caller free mbuf */
3644 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
3645 xflags |= RT2860_TX_ACK;
3646 if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) {
3647 error = run_sendprot(sc, m, ni,
3648 params->ibp_flags & IEEE80211_BPF_RTS ?
3649 IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY,
3652 /* let caller free mbuf */
3655 opflags |= /*XXX RT2573_TX_LONG_RETRY |*/ RT2860_TX_TXOP_SIFS;
3658 if (sc->sc_epq[0].tx_nfree == 0) {
3659 /* let caller free mbuf */
3660 ifq_set_oactive(&sc->sc_ifp->if_snd);
3661 DPRINTF("sending raw frame, but tx ring is full\n");
3664 data = STAILQ_FIRST(&sc->sc_epq[0].tx_fh);
3665 STAILQ_REMOVE_HEAD(&sc->sc_epq[0].tx_fh, next);
3666 sc->sc_epq[0].tx_nfree--;
3668 txd = (struct rt2870_txd *)&data->desc;
3669 txd->flags = RT2860_TX_QSEL_EDCA;
3670 txwi = (struct rt2860_txwi *)(txd + 1);
3672 txwi->xflags = xflags;
3673 txwi->txop = opflags;
3674 txwi->flags = 0; /* clear leftover garbage bits */
3678 for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
3679 if (rt2860_rates[ridx].rate == rate)
3683 run_set_tx_desc(sc, data);
3685 DPRINTFN(10, "sending raw frame len=%u rate=%u\n",
3686 m->m_pkthdr.len, rate);
3688 STAILQ_INSERT_TAIL(&sc->sc_epq[0].tx_qh, data, next);
3690 usbd_transfer_start(sc->sc_xfer[0]);
3696 run_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
3697 const struct ieee80211_bpf_params *params)
3699 struct ifnet *ifp = ni->ni_ic->ic_ifp;
3700 struct run_softc *sc = ifp->if_softc;
3705 /* prevent management frames from being sent if we're not ready */
3706 if (!(ifp->if_flags & IFF_RUNNING)) {
3711 if (params == NULL) {
3713 if ((error = run_tx_mgt(sc, m, ni)) != 0) {
3715 DPRINTF("mgt tx failed\n");
3719 /* tx raw packet with param */
3720 if ((error = run_tx_param(sc, m, ni, params)) != 0) {
3722 DPRINTF("tx with param failed\n");
3735 ieee80211_free_node(ni);
3742 run_start_locked(struct ifnet *ifp)
3744 struct run_softc *sc = ifp->if_softc;
3745 struct ieee80211_node *ni;
3746 struct mbuf *m = NULL;
3748 if ((ifp->if_flags & IFF_RUNNING) == 0)
3752 /* send data frames */
3753 m = ifq_dequeue(&ifp->if_snd);
3757 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
3758 if (run_tx(sc, m, ni) != 0) {
3759 ifq_prepend(&ifp->if_snd, m);
3760 ifq_set_oactive(&ifp->if_snd);
3767 run_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
3769 ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
3770 run_start_locked(ifp);
3774 run_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
3776 struct run_softc *sc = ifp->if_softc;
3777 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
3778 struct ifreq *ifr = (struct ifreq *) data;
3783 error = sc->sc_detached ? ENXIO : 0;
3791 if (ifp->if_flags & IFF_UP) {
3792 if (!(ifp->if_flags & IFF_RUNNING)){
3794 run_init_locked(sc);
3796 run_update_promisc_locked(ifp);
3798 if (ifp->if_flags & IFF_RUNNING &&
3799 (ic->ic_nrunning == 0 || sc->rvp_cnt <= 1)) {
3805 ieee80211_start_all(ic);
3808 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
3811 error = ether_ioctl(ifp, cmd, data);
3822 run_iq_calib(struct run_softc *sc, u_int chan)
3827 run_bbp_write(sc, 158, 0x2c);
3829 run_efuse_read(sc, RT5390_EEPROM_IQ_GAIN_CAL_TX0_2GHZ, &val, 1);
3830 else if (chan <= 64) {
3832 RT5390_EEPROM_IQ_GAIN_CAL_TX0_CH36_TO_CH64_5GHZ,
3834 } else if (chan <= 138) {
3836 RT5390_EEPROM_IQ_GAIN_CAL_TX0_CH100_TO_CH138_5GHZ,
3838 } else if (chan <= 165) {
3840 RT5390_EEPROM_IQ_GAIN_CAL_TX0_CH140_TO_CH165_5GHZ,
3844 run_bbp_write(sc, 159, val);
3847 run_bbp_write(sc, 158, 0x2d);
3849 run_efuse_read(sc, RT5390_EEPROM_IQ_PHASE_CAL_TX0_2GHZ,
3851 } else if (chan <= 64) {
3853 RT5390_EEPROM_IQ_PHASE_CAL_TX0_CH36_TO_CH64_5GHZ,
3855 } else if (chan <= 138) {
3857 RT5390_EEPROM_IQ_PHASE_CAL_TX0_CH100_TO_CH138_5GHZ,
3859 } else if (chan <= 165) {
3861 RT5390_EEPROM_IQ_PHASE_CAL_TX0_CH140_TO_CH165_5GHZ,
3865 run_bbp_write(sc, 159, val);
3868 run_bbp_write(sc, 158, 0x4a);
3870 run_efuse_read(sc, RT5390_EEPROM_IQ_GAIN_CAL_TX1_2GHZ,
3872 } else if (chan <= 64) {
3874 RT5390_EEPROM_IQ_GAIN_CAL_TX1_CH36_TO_CH64_5GHZ,
3876 } else if (chan <= 138) {
3878 RT5390_EEPROM_IQ_GAIN_CAL_TX1_CH100_TO_CH138_5GHZ,
3880 } else if (chan <= 165) {
3882 RT5390_EEPROM_IQ_GAIN_CAL_TX1_CH140_TO_CH165_5GHZ,
3886 run_bbp_write(sc, 159, val);
3889 run_bbp_write(sc, 158, 0x4b);
3891 run_efuse_read(sc, RT5390_EEPROM_IQ_PHASE_CAL_TX1_2GHZ,
3893 } else if (chan <= 64) {
3895 RT5390_EEPROM_IQ_PHASE_CAL_TX1_CH36_TO_CH64_5GHZ,
3897 } else if (chan <= 138) {
3899 RT5390_EEPROM_IQ_PHASE_CAL_TX1_CH100_TO_CH138_5GHZ,
3901 } else if (chan <= 165) {
3903 RT5390_EEPROM_IQ_PHASE_CAL_TX1_CH140_TO_CH165_5GHZ,
3907 run_bbp_write(sc, 159, val);
3909 /* RF IQ compensation control. */
3910 run_bbp_write(sc, 158, 0x04);
3911 run_efuse_read(sc, RT5390_EEPROM_RF_IQ_COMPENSATION_CTL,
3913 run_bbp_write(sc, 159, val);
3915 /* RF IQ imbalance compensation control. */
3916 run_bbp_write(sc, 158, 0x03);
3918 RT5390_EEPROM_RF_IQ_IMBALANCE_COMPENSATION_CTL, &val, 1);
3919 run_bbp_write(sc, 159, val);
3923 run_set_agc(struct run_softc *sc, uint8_t agc)
3927 if (sc->mac_ver == 0x3572) {
3928 run_bbp_read(sc, 27, &bbp);
3930 run_bbp_write(sc, 27, bbp | 0 << 5); /* select Rx0 */
3931 run_bbp_write(sc, 66, agc);
3932 run_bbp_write(sc, 27, bbp | 1 << 5); /* select Rx1 */
3933 run_bbp_write(sc, 66, agc);
3935 run_bbp_write(sc, 66, agc);
3939 run_select_chan_group(struct run_softc *sc, int group)
3944 run_bbp_write(sc, 62, 0x37 - sc->lna[group]);
3945 run_bbp_write(sc, 63, 0x37 - sc->lna[group]);
3946 run_bbp_write(sc, 64, 0x37 - sc->lna[group]);
3947 if (sc->mac_ver < 0x3572)
3948 run_bbp_write(sc, 86, 0x00);
3950 if (sc->mac_ver == 0x3593) {
3951 run_bbp_write(sc, 77, 0x98);
3952 run_bbp_write(sc, 83, (group == 0) ? 0x8a : 0x9a);
3956 if (sc->ext_2ghz_lna) {
3957 if (sc->mac_ver >= 0x5390)
3958 run_bbp_write(sc, 75, 0x52);
3960 run_bbp_write(sc, 82, 0x62);
3961 run_bbp_write(sc, 75, 0x46);
3964 if (sc->mac_ver == 0x5592) {
3965 run_bbp_write(sc, 79, 0x1c);
3966 run_bbp_write(sc, 80, 0x0e);
3967 run_bbp_write(sc, 81, 0x3a);
3968 run_bbp_write(sc, 82, 0x62);
3970 run_bbp_write(sc, 195, 0x80);
3971 run_bbp_write(sc, 196, 0xe0);
3972 run_bbp_write(sc, 195, 0x81);
3973 run_bbp_write(sc, 196, 0x1f);
3974 run_bbp_write(sc, 195, 0x82);
3975 run_bbp_write(sc, 196, 0x38);
3976 run_bbp_write(sc, 195, 0x83);
3977 run_bbp_write(sc, 196, 0x32);
3978 run_bbp_write(sc, 195, 0x85);
3979 run_bbp_write(sc, 196, 0x28);
3980 run_bbp_write(sc, 195, 0x86);
3981 run_bbp_write(sc, 196, 0x19);
3982 } else if (sc->mac_ver >= 0x5390)
3983 run_bbp_write(sc, 75, 0x50);
3985 run_bbp_write(sc, 82,
3986 (sc->mac_ver == 0x3593) ? 0x62 : 0x84);
3987 run_bbp_write(sc, 75, 0x50);
3991 if (sc->mac_ver == 0x5592) {
3992 run_bbp_write(sc, 79, 0x18);
3993 run_bbp_write(sc, 80, 0x08);
3994 run_bbp_write(sc, 81, 0x38);
3995 run_bbp_write(sc, 82, 0x92);
3997 run_bbp_write(sc, 195, 0x80);
3998 run_bbp_write(sc, 196, 0xf0);
3999 run_bbp_write(sc, 195, 0x81);
4000 run_bbp_write(sc, 196, 0x1e);
4001 run_bbp_write(sc, 195, 0x82);
4002 run_bbp_write(sc, 196, 0x28);
4003 run_bbp_write(sc, 195, 0x83);
4004 run_bbp_write(sc, 196, 0x20);
4005 run_bbp_write(sc, 195, 0x85);
4006 run_bbp_write(sc, 196, 0x7f);
4007 run_bbp_write(sc, 195, 0x86);
4008 run_bbp_write(sc, 196, 0x7f);
4009 } else if (sc->mac_ver == 0x3572)
4010 run_bbp_write(sc, 82, 0x94);
4012 run_bbp_write(sc, 82,
4013 (sc->mac_ver == 0x3593) ? 0x82 : 0xf2);
4014 if (sc->ext_5ghz_lna)
4015 run_bbp_write(sc, 75, 0x46);
4017 run_bbp_write(sc, 75, 0x50);
4020 run_read(sc, RT2860_TX_BAND_CFG, &tmp);
4021 tmp &= ~(RT2860_5G_BAND_SEL_N | RT2860_5G_BAND_SEL_P);
4022 tmp |= (group == 0) ? RT2860_5G_BAND_SEL_N : RT2860_5G_BAND_SEL_P;
4023 run_write(sc, RT2860_TX_BAND_CFG, tmp);
4025 /* enable appropriate Power Amplifiers and Low Noise Amplifiers */
4026 tmp = RT2860_RFTR_EN | RT2860_TRSW_EN | RT2860_LNA_PE0_EN;
4027 if (sc->mac_ver == 0x3593)
4028 tmp |= 1 << 29 | 1 << 28;
4029 if (sc->nrxchains > 1)
4030 tmp |= RT2860_LNA_PE1_EN;
4031 if (group == 0) { /* 2GHz */
4032 tmp |= RT2860_PA_PE_G0_EN;
4033 if (sc->ntxchains > 1)
4034 tmp |= RT2860_PA_PE_G1_EN;
4035 if (sc->mac_ver == 0x3593) {
4036 if (sc->ntxchains > 2)
4040 tmp |= RT2860_PA_PE_A0_EN;
4041 if (sc->ntxchains > 1)
4042 tmp |= RT2860_PA_PE_A1_EN;
4044 if (sc->mac_ver == 0x3572) {
4045 run_rt3070_rf_write(sc, 8, 0x00);
4046 run_write(sc, RT2860_TX_PIN_CFG, tmp);
4047 run_rt3070_rf_write(sc, 8, 0x80);
4049 run_write(sc, RT2860_TX_PIN_CFG, tmp);
4051 if (sc->mac_ver == 0x5592) {
4052 run_bbp_write(sc, 195, 0x8d);
4053 run_bbp_write(sc, 196, 0x1a);
4056 if (sc->mac_ver == 0x3593) {
4057 run_read(sc, RT2860_GPIO_CTRL, &tmp);
4061 tmp = (tmp & ~0x00009090) | 0x00000090;
4062 run_write(sc, RT2860_GPIO_CTRL, tmp);
4065 /* set initial AGC value */
4066 if (group == 0) { /* 2GHz band */
4067 if (sc->mac_ver >= 0x3070)
4068 agc = 0x1c + sc->lna[0] * 2;
4070 agc = 0x2e + sc->lna[0];
4071 } else { /* 5GHz band */
4072 if (sc->mac_ver == 0x5592)
4073 agc = 0x24 + sc->lna[group] * 2;
4074 else if (sc->mac_ver == 0x3572 || sc->mac_ver == 0x3593)
4075 agc = 0x22 + (sc->lna[group] * 5) / 3;
4077 agc = 0x32 + (sc->lna[group] * 5) / 3;
4079 run_set_agc(sc, agc);
4083 run_rt2870_set_chan(struct run_softc *sc, u_int chan)
4085 const struct rfprog *rfprog = rt2860_rf2850;
4086 uint32_t r2, r3, r4;
4087 int8_t txpow1, txpow2;
4090 /* find the settings for this channel (we know it exists) */
4091 for (i = 0; rfprog[i].chan != chan; i++);
4094 if (sc->ntxchains == 1)
4095 r2 |= 1 << 14; /* 1T: disable Tx chain 2 */
4096 if (sc->nrxchains == 1)
4097 r2 |= 1 << 17 | 1 << 6; /* 1R: disable Rx chains 2 & 3 */
4098 else if (sc->nrxchains == 2)
4099 r2 |= 1 << 6; /* 2R: disable Rx chain 3 */
4101 /* use Tx power values from EEPROM */
4102 txpow1 = sc->txpow1[i];
4103 txpow2 = sc->txpow2[i];
4105 /* Initialize RF R3 and R4. */
4106 r3 = rfprog[i].r3 & 0xffffc1ff;
4107 r4 = (rfprog[i].r4 & ~(0x001f87c0)) | (sc->freq << 15);
4110 txpow1 = (txpow1 > 0xf) ? (0xf) : (txpow1);
4111 r3 |= (txpow1 << 10) | (1 << 9);
4115 /* txpow1 is not possible larger than 15. */
4116 r3 |= (txpow1 << 10);
4119 txpow2 = (txpow2 > 0xf) ? (0xf) : (txpow2);
4120 r4 |= (txpow2 << 7) | (1 << 6);
4123 r4 |= (txpow2 << 7);
4126 /* Set Tx0 power. */
4127 r3 |= (txpow1 << 9);
4129 /* Set frequency offset and Tx1 power. */
4130 r4 |= (txpow2 << 6);
4133 run_rt2870_rf_write(sc, rfprog[i].r1);
4134 run_rt2870_rf_write(sc, r2);
4135 run_rt2870_rf_write(sc, r3 & ~(1 << 2));
4136 run_rt2870_rf_write(sc, r4);
4140 run_rt2870_rf_write(sc, rfprog[i].r1);
4141 run_rt2870_rf_write(sc, r2);
4142 run_rt2870_rf_write(sc, r3 | (1 << 2));
4143 run_rt2870_rf_write(sc, r4);
4147 run_rt2870_rf_write(sc, rfprog[i].r1);
4148 run_rt2870_rf_write(sc, r2);
4149 run_rt2870_rf_write(sc, r3 & ~(1 << 2));
4150 run_rt2870_rf_write(sc, r4);
4154 run_rt3070_set_chan(struct run_softc *sc, u_int chan)
4156 int8_t txpow1, txpow2;
4160 /* find the settings for this channel (we know it exists) */
4161 for (i = 0; rt2860_rf2850[i].chan != chan; i++);
4163 /* use Tx power values from EEPROM */
4164 txpow1 = sc->txpow1[i];
4165 txpow2 = sc->txpow2[i];
4167 run_rt3070_rf_write(sc, 2, rt3070_freqs[i].n);
4169 /* RT3370/RT3390: RF R3 [7:4] is not reserved bits. */
4170 run_rt3070_rf_read(sc, 3, &rf);
4171 rf = (rf & ~0x0f) | rt3070_freqs[i].k;
4172 run_rt3070_rf_write(sc, 3, rf);
4174 run_rt3070_rf_read(sc, 6, &rf);
4175 rf = (rf & ~0x03) | rt3070_freqs[i].r;
4176 run_rt3070_rf_write(sc, 6, rf);
4179 run_rt3070_rf_read(sc, 12, &rf);
4180 rf = (rf & ~0x1f) | txpow1;
4181 run_rt3070_rf_write(sc, 12, rf);
4184 run_rt3070_rf_read(sc, 13, &rf);
4185 rf = (rf & ~0x1f) | txpow2;
4186 run_rt3070_rf_write(sc, 13, rf);
4188 run_rt3070_rf_read(sc, 1, &rf);
4190 if (sc->ntxchains == 1)
4191 rf |= 1 << 7 | 1 << 5; /* 1T: disable Tx chains 2 & 3 */
4192 else if (sc->ntxchains == 2)
4193 rf |= 1 << 7; /* 2T: disable Tx chain 3 */
4194 if (sc->nrxchains == 1)
4195 rf |= 1 << 6 | 1 << 4; /* 1R: disable Rx chains 2 & 3 */
4196 else if (sc->nrxchains == 2)
4197 rf |= 1 << 6; /* 2R: disable Rx chain 3 */
4198 run_rt3070_rf_write(sc, 1, rf);
4201 run_rt3070_rf_read(sc, 23, &rf);
4202 rf = (rf & ~0x7f) | sc->freq;
4203 run_rt3070_rf_write(sc, 23, rf);
4205 /* program RF filter */
4206 run_rt3070_rf_read(sc, 24, &rf); /* Tx */
4207 rf = (rf & ~0x3f) | sc->rf24_20mhz;
4208 run_rt3070_rf_write(sc, 24, rf);
4209 run_rt3070_rf_read(sc, 31, &rf); /* Rx */
4210 rf = (rf & ~0x3f) | sc->rf24_20mhz;
4211 run_rt3070_rf_write(sc, 31, rf);
4213 /* enable RF tuning */
4214 run_rt3070_rf_read(sc, 7, &rf);
4215 run_rt3070_rf_write(sc, 7, rf | 0x01);
4219 run_rt3572_set_chan(struct run_softc *sc, u_int chan)
4221 int8_t txpow1, txpow2;
4226 /* find the settings for this channel (we know it exists) */
4227 for (i = 0; rt2860_rf2850[i].chan != chan; i++);
4229 /* use Tx power values from EEPROM */
4230 txpow1 = sc->txpow1[i];
4231 txpow2 = sc->txpow2[i];
4234 run_bbp_write(sc, 25, sc->bbp25);
4235 run_bbp_write(sc, 26, sc->bbp26);
4237 /* enable IQ phase correction */
4238 run_bbp_write(sc, 25, 0x09);
4239 run_bbp_write(sc, 26, 0xff);
4242 run_rt3070_rf_write(sc, 2, rt3070_freqs[i].n);
4243 run_rt3070_rf_write(sc, 3, rt3070_freqs[i].k);
4244 run_rt3070_rf_read(sc, 6, &rf);
4245 rf = (rf & ~0x0f) | rt3070_freqs[i].r;
4246 rf |= (chan <= 14) ? 0x08 : 0x04;
4247 run_rt3070_rf_write(sc, 6, rf);
4250 run_rt3070_rf_read(sc, 5, &rf);
4251 rf &= ~(0x08 | 0x04);
4252 rf |= (chan <= 14) ? 0x04 : 0x08;
4253 run_rt3070_rf_write(sc, 5, rf);
4255 /* set Tx power for chain 0 */
4259 rf = 0xe0 | (txpow1 & 0xc) << 1 | (txpow1 & 0x3);
4260 run_rt3070_rf_write(sc, 12, rf);
4262 /* set Tx power for chain 1 */
4266 rf = 0xe0 | (txpow2 & 0xc) << 1 | (txpow2 & 0x3);
4267 run_rt3070_rf_write(sc, 13, rf);
4269 /* set Tx/Rx streams */
4270 run_rt3070_rf_read(sc, 1, &rf);
4272 if (sc->ntxchains == 1)
4273 rf |= 1 << 7 | 1 << 5; /* 1T: disable Tx chains 2 & 3 */
4274 else if (sc->ntxchains == 2)
4275 rf |= 1 << 7; /* 2T: disable Tx chain 3 */
4276 if (sc->nrxchains == 1)
4277 rf |= 1 << 6 | 1 << 4; /* 1R: disable Rx chains 2 & 3 */
4278 else if (sc->nrxchains == 2)
4279 rf |= 1 << 6; /* 2R: disable Rx chain 3 */
4280 run_rt3070_rf_write(sc, 1, rf);
4283 run_rt3070_rf_read(sc, 23, &rf);
4284 rf = (rf & ~0x7f) | sc->freq;
4285 run_rt3070_rf_write(sc, 23, rf);
4287 /* program RF filter */
4288 rf = sc->rf24_20mhz;
4289 run_rt3070_rf_write(sc, 24, rf); /* Tx */
4290 run_rt3070_rf_write(sc, 31, rf); /* Rx */
4292 /* enable RF tuning */
4293 run_rt3070_rf_read(sc, 7, &rf);
4294 rf = (chan <= 14) ? 0xd8 : ((rf & ~0xc8) | 0x14);
4295 run_rt3070_rf_write(sc, 7, rf);
4298 rf = (chan <= 14) ? 0xc3 : 0xc0;
4299 run_rt3070_rf_write(sc, 9, rf);
4301 /* set loop filter 1 */
4302 run_rt3070_rf_write(sc, 10, 0xf1);
4303 /* set loop filter 2 */
4304 run_rt3070_rf_write(sc, 11, (chan <= 14) ? 0xb9 : 0x00);
4307 run_rt3070_rf_write(sc, 15, (chan <= 14) ? 0x53 : 0x43);
4310 rf = 0x48 | sc->txmixgain_2ghz;
4312 rf = 0x78 | sc->txmixgain_5ghz;
4313 run_rt3070_rf_write(sc, 16, rf);
4316 run_rt3070_rf_write(sc, 17, 0x23);
4320 else if (chan <= 64)
4322 else if (chan <= 128)
4326 run_rt3070_rf_write(sc, 19, rf);
4331 else if (chan <= 64)
4333 else if (chan <= 128)
4337 run_rt3070_rf_write(sc, 20, rf);
4342 else if (chan <= 64)
4346 run_rt3070_rf_write(sc, 25, rf);
4349 run_rt3070_rf_write(sc, 26, (chan <= 14) ? 0x85 : 0x87);
4351 run_rt3070_rf_write(sc, 27, (chan <= 14) ? 0x00 : 0x01);
4353 run_rt3070_rf_write(sc, 29, (chan <= 14) ? 0x9b : 0x9f);
4355 run_read(sc, RT2860_GPIO_CTRL, &tmp);
4359 run_write(sc, RT2860_GPIO_CTRL, tmp);
4361 /* enable RF tuning */
4362 run_rt3070_rf_read(sc, 7, &rf);
4363 run_rt3070_rf_write(sc, 7, rf | 0x01);
4369 run_rt3593_set_chan(struct run_softc *sc, u_int chan)
4371 int8_t txpow1, txpow2, txpow3;
4375 /* find the settings for this channel (we know it exists) */
4376 for (i = 0; rt2860_rf2850[i].chan != chan; i++);
4378 /* use Tx power values from EEPROM */
4379 txpow1 = sc->txpow1[i];
4380 txpow2 = sc->txpow2[i];
4381 txpow3 = (sc->ntxchains == 3) ? sc->txpow3[i] : 0;
4384 run_bbp_write(sc, 25, sc->bbp25);
4385 run_bbp_write(sc, 26, sc->bbp26);
4387 /* Enable IQ phase correction. */
4388 run_bbp_write(sc, 25, 0x09);
4389 run_bbp_write(sc, 26, 0xff);
4392 run_rt3070_rf_write(sc, 8, rt3070_freqs[i].n);
4393 run_rt3070_rf_write(sc, 9, rt3070_freqs[i].k & 0x0f);
4394 run_rt3070_rf_read(sc, 11, &rf);
4395 rf = (rf & ~0x03) | (rt3070_freqs[i].r & 0x03);
4396 run_rt3070_rf_write(sc, 11, rf);
4399 run_rt3070_rf_read(sc, 11, &rf);
4401 rf |= (chan <= 14) ? 0x44 : 0x48;
4402 run_rt3070_rf_write(sc, 11, rf);
4407 rf = 0x40 | ((txpow1 & 0x18) << 1) | (txpow1 & 0x07);
4408 run_rt3070_rf_write(sc, 53, rf);
4413 rf = 0x40 | ((txpow2 & 0x18) << 1) | (txpow2 & 0x07);
4414 run_rt3070_rf_write(sc, 55, rf);
4419 rf = 0x40 | ((txpow3 & 0x18) << 1) | (txpow3 & 0x07);
4420 run_rt3070_rf_write(sc, 54, rf);
4422 rf = RT3070_RF_BLOCK | RT3070_PLL_PD;
4423 if (sc->ntxchains == 3)
4424 rf |= RT3070_TX0_PD | RT3070_TX1_PD | RT3070_TX2_PD;
4426 rf |= RT3070_TX0_PD | RT3070_TX1_PD;
4427 rf |= RT3070_RX0_PD | RT3070_RX1_PD | RT3070_RX2_PD;
4428 run_rt3070_rf_write(sc, 1, rf);
4430 run_adjust_freq_offset(sc);
4432 run_rt3070_rf_write(sc, 31, (chan <= 14) ? 0xa0 : 0x80);
4434 h20mhz = (sc->rf24_20mhz & 0x20) >> 5;
4435 run_rt3070_rf_read(sc, 30, &rf);
4436 rf = (rf & ~0x06) | (h20mhz << 1) | (h20mhz << 2);
4437 run_rt3070_rf_write(sc, 30, rf);
4439 run_rt3070_rf_read(sc, 36, &rf);
4444 run_rt3070_rf_write(sc, 36, rf);
4447 run_rt3070_rf_write(sc, 34, (chan <= 14) ? 0x3c : 0x20);
4448 /* Set pfd_delay. */
4449 run_rt3070_rf_write(sc, 12, (chan <= 14) ? 0x1a : 0x12);
4451 /* Set vco bias current control. */
4452 run_rt3070_rf_read(sc, 6, &rf);
4456 else if (chan <= 128)
4460 run_rt3070_rf_write(sc, 6, rf);
4462 run_rt3070_rf_read(sc, 30, &rf);
4463 rf = (rf & ~0x18) | 0x10;
4464 run_rt3070_rf_write(sc, 30, rf);
4466 run_rt3070_rf_write(sc, 10, (chan <= 14) ? 0xd3 : 0xd8);
4467 run_rt3070_rf_write(sc, 13, (chan <= 14) ? 0x12 : 0x23);
4469 run_rt3070_rf_read(sc, 51, &rf);
4470 rf = (rf & ~0x03) | 0x01;
4471 run_rt3070_rf_write(sc, 51, rf);
4472 /* Set tx_mx1_cc. */
4473 run_rt3070_rf_read(sc, 51, &rf);
4475 rf |= (chan <= 14) ? 0x14 : 0x10;
4476 run_rt3070_rf_write(sc, 51, rf);
4477 /* Set tx_mx1_ic. */
4478 run_rt3070_rf_read(sc, 51, &rf);
4480 rf |= (chan <= 14) ? 0x60 : 0x40;
4481 run_rt3070_rf_write(sc, 51, rf);
4482 /* Set tx_lo1_ic. */
4483 run_rt3070_rf_read(sc, 49, &rf);
4485 rf |= (chan <= 14) ? 0x0c : 0x08;
4486 run_rt3070_rf_write(sc, 49, rf);
4487 /* Set tx_lo1_en. */
4488 run_rt3070_rf_read(sc, 50, &rf);
4489 run_rt3070_rf_write(sc, 50, rf & ~0x20);
4491 run_rt3070_rf_read(sc, 57, &rf);
4493 rf |= (chan <= 14) ? 0x6c : 0x3c;
4494 run_rt3070_rf_write(sc, 57, rf);
4495 /* Set rx_mix1_ic, rxa_lnactr, lna_vc, lna_inbias_en and lna_en. */
4496 run_rt3070_rf_write(sc, 44, (chan <= 14) ? 0x93 : 0x9b);
4497 /* Set drv_gnd_a, tx_vga_cc_a and tx_mx2_gain. */
4498 run_rt3070_rf_write(sc, 52, (chan <= 14) ? 0x45 : 0x05);
4499 /* Enable VCO calibration. */
4500 run_rt3070_rf_read(sc, 3, &rf);
4501 rf &= ~RT5390_VCOCAL;
4502 rf |= (chan <= 14) ? RT5390_VCOCAL : 0xbe;
4503 run_rt3070_rf_write(sc, 3, rf);
4507 else if (chan <= 64)
4509 else if (chan <= 128)
4513 run_rt3070_rf_write(sc, 39, rf);
4516 else if (chan <= 64)
4518 else if (chan <= 128)
4522 run_rt3070_rf_write(sc, 45, rf);
4524 /* Set FEQ/AEQ control. */
4525 run_bbp_write(sc, 105, 0x34);
4529 run_rt5390_set_chan(struct run_softc *sc, u_int chan)
4531 int8_t txpow1, txpow2;
4535 /* find the settings for this channel (we know it exists) */
4536 for (i = 0; rt2860_rf2850[i].chan != chan; i++);
4538 /* use Tx power values from EEPROM */
4539 txpow1 = sc->txpow1[i];
4540 txpow2 = sc->txpow2[i];
4542 run_rt3070_rf_write(sc, 8, rt3070_freqs[i].n);
4543 run_rt3070_rf_write(sc, 9, rt3070_freqs[i].k & 0x0f);
4544 run_rt3070_rf_read(sc, 11, &rf);
4545 rf = (rf & ~0x03) | (rt3070_freqs[i].r & 0x03);
4546 run_rt3070_rf_write(sc, 11, rf);
4548 run_rt3070_rf_read(sc, 49, &rf);
4549 rf = (rf & ~0x3f) | (txpow1 & 0x3f);
4550 /* The valid range of the RF R49 is 0x00 to 0x27. */
4551 if ((rf & 0x3f) > 0x27)
4552 rf = (rf & ~0x3f) | 0x27;
4553 run_rt3070_rf_write(sc, 49, rf);
4555 if (sc->mac_ver == 0x5392) {
4556 run_rt3070_rf_read(sc, 50, &rf);
4557 rf = (rf & ~0x3f) | (txpow2 & 0x3f);
4558 /* The valid range of the RF R50 is 0x00 to 0x27. */
4559 if ((rf & 0x3f) > 0x27)
4560 rf = (rf & ~0x3f) | 0x27;
4561 run_rt3070_rf_write(sc, 50, rf);
4564 run_rt3070_rf_read(sc, 1, &rf);
4565 rf |= RT3070_RF_BLOCK | RT3070_PLL_PD | RT3070_RX0_PD | RT3070_TX0_PD;
4566 if (sc->mac_ver == 0x5392)
4567 rf |= RT3070_RX1_PD | RT3070_TX1_PD;
4568 run_rt3070_rf_write(sc, 1, rf);
4570 if (sc->mac_ver != 0x5392) {
4571 run_rt3070_rf_read(sc, 2, &rf);
4573 run_rt3070_rf_write(sc, 2, rf);
4576 run_rt3070_rf_write(sc, 2, rf);
4579 run_adjust_freq_offset(sc);
4581 if (sc->mac_ver == 0x5392) {
4582 /* Fix for RT5392C. */
4583 if (sc->mac_rev >= 0x0223) {
4586 else if (chan >= 5 && chan <= 7)
4590 run_rt3070_rf_write(sc, 23, rf);
4596 else if (chan >= 6 && chan <= 7)
4598 else if (chan >= 8 && chan <= 10)
4602 run_rt3070_rf_write(sc, 59, rf);
4608 run_rt3070_rf_write(sc, 59, rf);
4611 /* Fix for RT5390F. */
4612 if (sc->mac_rev >= 0x0502) {
4617 run_rt3070_rf_write(sc, 55, rf);
4621 else if (chan == 12)
4625 run_rt3070_rf_write(sc, 59, rf);
4627 run_rt3070_rf_write(sc, 55, 0x44);
4628 run_rt3070_rf_write(sc, 59, 0x8f);
4632 /* Enable VCO calibration. */
4633 run_rt3070_rf_read(sc, 3, &rf);
4634 rf |= RT5390_VCOCAL;
4635 run_rt3070_rf_write(sc, 3, rf);
4639 run_rt5592_set_chan(struct run_softc *sc, u_int chan)
4641 const struct rt5592_freqs *freqs;
4643 uint8_t reg, rf, txpow_bound;
4644 int8_t txpow1, txpow2;
4647 run_read(sc, RT5592_DEBUG_INDEX, &tmp);
4648 freqs = (tmp & RT5592_SEL_XTAL) ?
4649 rt5592_freqs_40mhz : rt5592_freqs_20mhz;
4651 /* find the settings for this channel (we know it exists) */
4652 for (i = 0; rt2860_rf2850[i].chan != chan; i++, freqs++);
4654 /* use Tx power values from EEPROM */
4655 txpow1 = sc->txpow1[i];
4656 txpow2 = sc->txpow2[i];
4658 run_read(sc, RT3070_LDO_CFG0, &tmp);
4662 run_write(sc, RT3070_LDO_CFG0, tmp);
4665 run_rt3070_rf_write(sc, 8, freqs->n & 0xff);
4666 run_rt3070_rf_read(sc, 9, &rf);
4668 rf |= ((freqs->n & 0x0100) >> 8) << 4;
4669 run_rt3070_rf_write(sc, 9, rf);
4672 run_rt3070_rf_read(sc, 9, &rf);
4674 rf |= (freqs->k & 0x0f);
4675 run_rt3070_rf_write(sc, 9, rf);
4678 run_rt3070_rf_read(sc, 11, &rf);
4680 rf |= ((freqs->m - 0x8) & 0x3) << 2;
4681 run_rt3070_rf_write(sc, 11, rf);
4682 run_rt3070_rf_read(sc, 9, &rf);
4684 rf |= (((freqs->m - 0x8) & 0x4) >> 2) << 7;
4685 run_rt3070_rf_write(sc, 9, rf);
4688 run_rt3070_rf_read(sc, 11, &rf);
4690 rf |= (freqs->r - 0x1);
4691 run_rt3070_rf_write(sc, 11, rf);
4694 /* Initialize RF registers for 2GHZ. */
4695 for (i = 0; i < NELEM(rt5592_2ghz_def_rf); i++) {
4696 run_rt3070_rf_write(sc, rt5592_2ghz_def_rf[i].reg,
4697 rt5592_2ghz_def_rf[i].val);
4700 rf = (chan <= 10) ? 0x07 : 0x06;
4701 run_rt3070_rf_write(sc, 23, rf);
4702 run_rt3070_rf_write(sc, 59, rf);
4704 run_rt3070_rf_write(sc, 55, 0x43);
4707 * RF R49/R50 Tx power ALC code.
4708 * G-band bit<7:6>=1:0, bit<5:0> range from 0x0 ~ 0x27.
4713 /* Initialize RF registers for 5GHZ. */
4714 for (i = 0; i < NELEM(rt5592_5ghz_def_rf); i++) {
4715 run_rt3070_rf_write(sc, rt5592_5ghz_def_rf[i].reg,
4716 rt5592_5ghz_def_rf[i].val);
4718 for (i = 0; i < NELEM(rt5592_chan_5ghz); i++) {
4719 if (chan >= rt5592_chan_5ghz[i].firstchan &&
4720 chan <= rt5592_chan_5ghz[i].lastchan) {
4721 run_rt3070_rf_write(sc, rt5592_chan_5ghz[i].reg,
4722 rt5592_chan_5ghz[i].val);
4727 * RF R49/R50 Tx power ALC code.
4728 * A-band bit<7:6>=1:1, bit<5:0> range from 0x0 ~ 0x2b.
4734 /* RF R49 ch0 Tx power ALC code. */
4735 run_rt3070_rf_read(sc, 49, &rf);
4738 rf = (rf & ~0x3f) | (txpow1 & 0x3f);
4739 if ((rf & 0x3f) > txpow_bound)
4740 rf = (rf & ~0x3f) | txpow_bound;
4741 run_rt3070_rf_write(sc, 49, rf);
4743 /* RF R50 ch1 Tx power ALC code. */
4744 run_rt3070_rf_read(sc, 50, &rf);
4745 rf &= ~(1 << 7 | 1 << 6);
4747 rf = (rf & ~0x3f) | (txpow2 & 0x3f);
4748 if ((rf & 0x3f) > txpow_bound)
4749 rf = (rf & ~0x3f) | txpow_bound;
4750 run_rt3070_rf_write(sc, 50, rf);
4752 /* Enable RF_BLOCK, PLL_PD, RX0_PD, and TX0_PD. */
4753 run_rt3070_rf_read(sc, 1, &rf);
4754 rf |= (RT3070_RF_BLOCK | RT3070_PLL_PD | RT3070_RX0_PD | RT3070_TX0_PD);
4755 if (sc->ntxchains > 1)
4756 rf |= RT3070_TX1_PD;
4757 if (sc->nrxchains > 1)
4758 rf |= RT3070_RX1_PD;
4759 run_rt3070_rf_write(sc, 1, rf);
4761 run_rt3070_rf_write(sc, 6, 0xe4);
4763 run_rt3070_rf_write(sc, 30, 0x10);
4764 run_rt3070_rf_write(sc, 31, 0x80);
4765 run_rt3070_rf_write(sc, 32, 0x80);
4767 run_adjust_freq_offset(sc);
4769 /* Enable VCO calibration. */
4770 run_rt3070_rf_read(sc, 3, &rf);
4771 rf |= RT5390_VCOCAL;
4772 run_rt3070_rf_write(sc, 3, rf);
4776 run_set_rx_antenna(struct run_softc *sc, int aux)
4782 if (sc->rf_rev == RT5390_RF_5370) {
4783 run_bbp_read(sc, 152, &bbp152);
4784 run_bbp_write(sc, 152, bbp152 & ~0x80);
4786 run_mcu_cmd(sc, RT2860_MCU_CMD_ANTSEL, 0);
4787 run_read(sc, RT2860_GPIO_CTRL, &tmp);
4788 run_write(sc, RT2860_GPIO_CTRL, (tmp & ~0x0808) | 0x08);
4791 if (sc->rf_rev == RT5390_RF_5370) {
4792 run_bbp_read(sc, 152, &bbp152);
4793 run_bbp_write(sc, 152, bbp152 | 0x80);
4795 run_mcu_cmd(sc, RT2860_MCU_CMD_ANTSEL, 1);
4796 run_read(sc, RT2860_GPIO_CTRL, &tmp);
4797 run_write(sc, RT2860_GPIO_CTRL, tmp & ~0x0808);
4803 run_set_chan(struct run_softc *sc, struct ieee80211_channel *c)
4805 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
4808 chan = ieee80211_chan2ieee(ic, c);
4809 if (chan == 0 || chan == IEEE80211_CHAN_ANY)
4812 if (sc->mac_ver == 0x5592)
4813 run_rt5592_set_chan(sc, chan);
4814 else if (sc->mac_ver >= 0x5390)
4815 run_rt5390_set_chan(sc, chan);
4816 else if (sc->mac_ver == 0x3593)
4817 run_rt3593_set_chan(sc, chan);
4818 else if (sc->mac_ver == 0x3572)
4819 run_rt3572_set_chan(sc, chan);
4820 else if (sc->mac_ver >= 0x3070)
4821 run_rt3070_set_chan(sc, chan);
4823 run_rt2870_set_chan(sc, chan);
4825 /* determine channel group */
4828 else if (chan <= 64)
4830 else if (chan <= 128)
4835 /* XXX necessary only when group has changed! */
4836 run_select_chan_group(sc, group);
4840 /* Perform IQ calibration. */
4841 if (sc->mac_ver >= 0x5392)
4842 run_iq_calib(sc, chan);
4848 run_set_channel(struct ieee80211com *ic)
4850 struct run_softc *sc = ic->ic_ifp->if_softc;
4853 run_set_chan(sc, ic->ic_curchan);
4860 run_scan_start(struct ieee80211com *ic)
4862 struct run_softc *sc = ic->ic_ifp->if_softc;
4867 /* abort TSF synchronization */
4868 run_read(sc, RT2860_BCN_TIME_CFG, &tmp);
4869 run_write(sc, RT2860_BCN_TIME_CFG,
4870 tmp & ~(RT2860_BCN_TX_EN | RT2860_TSF_TIMER_EN |
4871 RT2860_TBTT_TIMER_EN));
4872 run_set_bssid(sc, sc->sc_ifp->if_broadcastaddr);
4880 run_scan_end(struct ieee80211com *ic)
4882 struct run_softc *sc = ic->ic_ifp->if_softc;
4886 run_enable_tsf_sync(sc);
4887 /* XXX keep local copy */
4888 run_set_bssid(sc, sc->sc_bssid);
4896 * Could be called from ieee80211_node_timeout()
4897 * (non-sleepable thread)
4900 run_update_beacon(struct ieee80211vap *vap, int item)
4902 struct ieee80211com *ic = vap->iv_ic;
4903 struct run_softc *sc = ic->ic_ifp->if_softc;
4904 struct run_vap *rvp = RUN_VAP(vap);
4908 KASSERT(vap != NULL, ("no beacon"));
4911 case IEEE80211_BEACON_ERP:
4912 run_updateslot(ic->ic_ifp);
4914 case IEEE80211_BEACON_HTINFO:
4917 case IEEE80211_BEACON_TIM:
4924 setbit(rvp->bo.bo_flags, item);
4925 ieee80211_beacon_update(vap->iv_bss, &rvp->bo, rvp->beacon_mbuf, mcast);
4927 i = RUN_CMDQ_GET(&sc->cmdq_store);
4928 DPRINTF("cmdq_store=%d\n", i);
4929 sc->cmdq[i].func = run_update_beacon_cb;
4930 sc->cmdq[i].arg0 = vap;
4931 ieee80211_runtask(ic, &sc->cmdq_task);
4937 run_update_beacon_cb(void *arg)
4939 struct ieee80211vap *vap = arg;
4940 struct run_vap *rvp = RUN_VAP(vap);
4941 struct ieee80211com *ic = vap->iv_ic;
4942 struct run_softc *sc = ic->ic_ifp->if_softc;
4943 struct rt2860_txwi txwi;
4948 if (vap->iv_bss->ni_chan == IEEE80211_CHAN_ANYC)
4950 if (ic->ic_bsschan == IEEE80211_CHAN_ANYC)
4954 * No need to call ieee80211_beacon_update(), run_update_beacon()
4955 * is taking care of apropriate calls.
4957 if (rvp->beacon_mbuf == NULL) {
4958 rvp->beacon_mbuf = ieee80211_beacon_alloc(vap->iv_bss,
4960 if (rvp->beacon_mbuf == NULL)
4963 m = rvp->beacon_mbuf;
4965 memset(&txwi, 0, sizeof(txwi));
4967 txwi.len = htole16(m->m_pkthdr.len);
4969 /* send beacons at the lowest available rate */
4970 ridx = (ic->ic_curmode == IEEE80211_MODE_11A) ?
4971 RT2860_RIDX_OFDM6 : RT2860_RIDX_CCK1;
4972 txwi.phy = htole16(rt2860_rates[ridx].mcs);
4973 if (rt2860_rates[ridx].phy == IEEE80211_T_OFDM)
4974 txwi.phy |= htole16(RT2860_PHY_OFDM);
4975 txwi.txop = RT2860_TX_TXOP_HT;
4976 txwi.flags = RT2860_TX_TS;
4977 txwi.xflags = RT2860_TX_NSEQ;
4979 txwisize = (sc->mac_ver == 0x5592) ?
4980 sizeof(txwi) + sizeof(uint32_t) : sizeof(txwi);
4981 run_write_region_1(sc, RT2860_BCN_BASE(rvp->rvp_id), (uint8_t *)&txwi,
4983 run_write_region_1(sc, RT2860_BCN_BASE(rvp->rvp_id) + txwisize,
4984 mtod(m, uint8_t *), (m->m_pkthdr.len + 1) & ~1);
4988 run_updateprot(struct ieee80211com *ic)
4990 struct run_softc *sc = ic->ic_ifp->if_softc;
4993 i = RUN_CMDQ_GET(&sc->cmdq_store);
4994 DPRINTF("cmdq_store=%d\n", i);
4995 sc->cmdq[i].func = run_updateprot_cb;
4996 sc->cmdq[i].arg0 = ic;
4997 ieee80211_runtask(ic, &sc->cmdq_task);
5001 run_updateprot_cb(void *arg)
5003 struct ieee80211com *ic = arg;
5004 struct run_softc *sc = ic->ic_ifp->if_softc;
5007 tmp = RT2860_RTSTH_EN | RT2860_PROT_NAV_SHORT | RT2860_TXOP_ALLOW_ALL;
5008 /* setup protection frame rate (MCS code) */
5009 tmp |= (ic->ic_curmode == IEEE80211_MODE_11A) ?
5010 rt2860_rates[RT2860_RIDX_OFDM6].mcs :
5011 rt2860_rates[RT2860_RIDX_CCK11].mcs;
5013 /* CCK frames don't require protection */
5014 run_write(sc, RT2860_CCK_PROT_CFG, tmp);
5015 if (ic->ic_flags & IEEE80211_F_USEPROT) {
5016 if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
5017 tmp |= RT2860_PROT_CTRL_RTS_CTS;
5018 else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
5019 tmp |= RT2860_PROT_CTRL_CTS;
5021 run_write(sc, RT2860_OFDM_PROT_CFG, tmp);
5025 run_usb_timeout_cb(void *arg)
5027 struct ieee80211vap *vap = arg;
5028 struct run_softc *sc = vap->iv_ic->ic_ifp->if_softc;
5030 RUN_LOCK_ASSERT(sc, MA_OWNED);
5032 if(vap->iv_state == IEEE80211_S_RUN &&
5033 vap->iv_opmode != IEEE80211_M_STA)
5034 run_reset_livelock(sc);
5035 else if (vap->iv_state == IEEE80211_S_SCAN) {
5036 DPRINTF("timeout caused by scan\n");
5038 ieee80211_cancel_scan(vap);
5040 DPRINTF("timeout by unknown cause\n");
5044 run_reset_livelock(struct run_softc *sc)
5048 RUN_LOCK_ASSERT(sc, MA_OWNED);
5051 * In IBSS or HostAP modes (when the hardware sends beacons), the MAC
5052 * can run into a livelock and start sending CTS-to-self frames like
5053 * crazy if protection is enabled. Reset MAC/BBP for a while
5055 run_read(sc, RT2860_DEBUG, &tmp);
5056 DPRINTFN(3, "debug reg %08x\n", tmp);
5057 if ((tmp & (1 << 29)) && (tmp & (1 << 7 | 1 << 5))) {
5058 DPRINTF("CTS-to-self livelock detected\n");
5059 run_write(sc, RT2860_MAC_SYS_CTRL, RT2860_MAC_SRST);
5061 run_write(sc, RT2860_MAC_SYS_CTRL,
5062 RT2860_MAC_RX_EN | RT2860_MAC_TX_EN);
5067 run_update_promisc_locked(struct ifnet *ifp)
5069 struct run_softc *sc = ifp->if_softc;
5072 run_read(sc, RT2860_RX_FILTR_CFG, &tmp);
5074 tmp |= RT2860_DROP_UC_NOME;
5075 if (ifp->if_flags & IFF_PROMISC)
5076 tmp &= ~RT2860_DROP_UC_NOME;
5078 run_write(sc, RT2860_RX_FILTR_CFG, tmp);
5080 DPRINTF("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
5081 "entering" : "leaving");
5085 run_update_promisc(struct ifnet *ifp)
5087 struct run_softc *sc = ifp->if_softc;
5089 if ((ifp->if_flags & IFF_RUNNING) == 0)
5093 run_update_promisc_locked(ifp);
5098 run_enable_tsf_sync(struct run_softc *sc)
5100 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
5101 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
5104 DPRINTF("rvp_id=%d ic_opmode=%d\n", RUN_VAP(vap)->rvp_id,
5107 run_read(sc, RT2860_BCN_TIME_CFG, &tmp);
5109 tmp |= vap->iv_bss->ni_intval * 16;
5110 tmp |= RT2860_TSF_TIMER_EN | RT2860_TBTT_TIMER_EN;
5112 if (ic->ic_opmode == IEEE80211_M_STA) {
5114 * Local TSF is always updated with remote TSF on beacon
5117 tmp |= 1 << RT2860_TSF_SYNC_MODE_SHIFT;
5118 } else if (ic->ic_opmode == IEEE80211_M_IBSS) {
5119 tmp |= RT2860_BCN_TX_EN;
5121 * Local TSF is updated with remote TSF on beacon reception
5122 * only if the remote TSF is greater than local TSF.
5124 tmp |= 2 << RT2860_TSF_SYNC_MODE_SHIFT;
5125 } else if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
5126 ic->ic_opmode == IEEE80211_M_MBSS) {
5127 tmp |= RT2860_BCN_TX_EN;
5128 /* SYNC with nobody */
5129 tmp |= 3 << RT2860_TSF_SYNC_MODE_SHIFT;
5131 DPRINTF("Enabling TSF failed. undefined opmode\n");
5135 run_write(sc, RT2860_BCN_TIME_CFG, tmp);
5139 run_enable_mrr(struct run_softc *sc)
5141 #define CCK(mcs) (mcs)
5142 #define OFDM(mcs) (1 << 3 | (mcs))
5143 run_write(sc, RT2860_LG_FBK_CFG0,
5144 OFDM(6) << 28 | /* 54->48 */
5145 OFDM(5) << 24 | /* 48->36 */
5146 OFDM(4) << 20 | /* 36->24 */
5147 OFDM(3) << 16 | /* 24->18 */
5148 OFDM(2) << 12 | /* 18->12 */
5149 OFDM(1) << 8 | /* 12-> 9 */
5150 OFDM(0) << 4 | /* 9-> 6 */
5151 OFDM(0)); /* 6-> 6 */
5153 run_write(sc, RT2860_LG_FBK_CFG1,
5154 CCK(2) << 12 | /* 11->5.5 */
5155 CCK(1) << 8 | /* 5.5-> 2 */
5156 CCK(0) << 4 | /* 2-> 1 */
5157 CCK(0)); /* 1-> 1 */
5163 run_set_txpreamble(struct run_softc *sc)
5165 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
5168 run_read(sc, RT2860_AUTO_RSP_CFG, &tmp);
5169 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
5170 tmp |= RT2860_CCK_SHORT_EN;
5172 tmp &= ~RT2860_CCK_SHORT_EN;
5173 run_write(sc, RT2860_AUTO_RSP_CFG, tmp);
5177 run_set_basicrates(struct run_softc *sc)
5179 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
5181 /* set basic rates mask */
5182 if (ic->ic_curmode == IEEE80211_MODE_11B)
5183 run_write(sc, RT2860_LEGACY_BASIC_RATE, 0x003);
5184 else if (ic->ic_curmode == IEEE80211_MODE_11A)
5185 run_write(sc, RT2860_LEGACY_BASIC_RATE, 0x150);
5187 run_write(sc, RT2860_LEGACY_BASIC_RATE, 0x15f);
5191 run_set_leds(struct run_softc *sc, uint16_t which)
5193 (void)run_mcu_cmd(sc, RT2860_MCU_CMD_LEDS,
5194 which | (sc->leds & 0x7f));
5198 run_set_bssid(struct run_softc *sc, const uint8_t *bssid)
5200 run_write(sc, RT2860_MAC_BSSID_DW0,
5201 bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24);
5202 run_write(sc, RT2860_MAC_BSSID_DW1,
5203 bssid[4] | bssid[5] << 8);
5207 run_set_macaddr(struct run_softc *sc, const uint8_t *addr)
5209 run_write(sc, RT2860_MAC_ADDR_DW0,
5210 addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
5211 run_write(sc, RT2860_MAC_ADDR_DW1,
5212 addr[4] | addr[5] << 8 | 0xff << 16);
5216 run_updateslot(struct ifnet *ifp)
5218 struct run_softc *sc = ifp->if_softc;
5219 struct ieee80211com *ic = ifp->if_l2com;
5222 i = RUN_CMDQ_GET(&sc->cmdq_store);
5223 DPRINTF("cmdq_store=%d\n", i);
5224 sc->cmdq[i].func = run_updateslot_cb;
5225 sc->cmdq[i].arg0 = ifp;
5226 ieee80211_runtask(ic, &sc->cmdq_task);
5233 run_updateslot_cb(void *arg)
5235 struct ifnet *ifp = arg;
5236 struct run_softc *sc = ifp->if_softc;
5237 struct ieee80211com *ic = ifp->if_l2com;
5240 run_read(sc, RT2860_BKOFF_SLOT_CFG, &tmp);
5242 tmp |= (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
5243 run_write(sc, RT2860_BKOFF_SLOT_CFG, tmp);
5247 run_update_mcast(struct ifnet *ifp)
5249 /* h/w filter supports getting everything or nothing */
5250 ifp->if_flags |= IFF_ALLMULTI;
5254 run_rssi2dbm(struct run_softc *sc, uint8_t rssi, uint8_t rxchain)
5256 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
5257 struct ieee80211_channel *c = ic->ic_curchan;
5260 if (IEEE80211_IS_CHAN_5GHZ(c)) {
5261 u_int chan = ieee80211_chan2ieee(ic, c);
5262 delta = sc->rssi_5ghz[rxchain];
5264 /* determine channel group */
5266 delta -= sc->lna[1];
5267 else if (chan <= 128)
5268 delta -= sc->lna[2];
5270 delta -= sc->lna[3];
5272 delta = sc->rssi_2ghz[rxchain] - sc->lna[0];
5274 return (-12 - delta - rssi);
5278 run_rt5390_bbp_init(struct run_softc *sc)
5283 /* Apply maximum likelihood detection for 2 stream case. */
5284 run_bbp_read(sc, 105, &bbp);
5285 if (sc->nrxchains > 1)
5286 run_bbp_write(sc, 105, bbp | RT5390_MLD);
5288 /* Avoid data lost and CRC error. */
5289 run_bbp_read(sc, 4, &bbp);
5290 run_bbp_write(sc, 4, bbp | RT5390_MAC_IF_CTRL);
5292 if (sc->mac_ver == 0x5592) {
5293 for (i = 0; i < NELEM(rt5592_def_bbp); i++) {
5294 run_bbp_write(sc, rt5592_def_bbp[i].reg,
5295 rt5592_def_bbp[i].val);
5297 for (i = 0; i < NELEM(rt5592_bbp_r196); i++) {
5298 run_bbp_write(sc, 195, i + 0x80);
5299 run_bbp_write(sc, 196, rt5592_bbp_r196[i]);
5302 for (i = 0; i < NELEM(rt5390_def_bbp); i++) {
5303 run_bbp_write(sc, rt5390_def_bbp[i].reg,
5304 rt5390_def_bbp[i].val);
5307 if (sc->mac_ver == 0x5392) {
5308 run_bbp_write(sc, 88, 0x90);
5309 run_bbp_write(sc, 95, 0x9a);
5310 run_bbp_write(sc, 98, 0x12);
5311 run_bbp_write(sc, 106, 0x12);
5312 run_bbp_write(sc, 134, 0xd0);
5313 run_bbp_write(sc, 135, 0xf6);
5314 run_bbp_write(sc, 148, 0x84);
5317 run_bbp_read(sc, 152, &bbp);
5318 run_bbp_write(sc, 152, bbp | 0x80);
5320 /* Fix BBP254 for RT5592C. */
5321 if (sc->mac_ver == 0x5592 && sc->mac_rev >= 0x0221) {
5322 run_bbp_read(sc, 254, &bbp);
5323 run_bbp_write(sc, 254, bbp | 0x80);
5326 /* Disable hardware antenna diversity. */
5327 if (sc->mac_ver == 0x5390)
5328 run_bbp_write(sc, 154, 0);
5330 /* Initialize Rx CCK/OFDM frequency offset report. */
5331 run_bbp_write(sc, 142, 1);
5332 run_bbp_write(sc, 143, 57);
5336 run_bbp_init(struct run_softc *sc)
5338 int i, error, ntries;
5341 /* wait for BBP to wake up */
5342 for (ntries = 0; ntries < 20; ntries++) {
5343 if ((error = run_bbp_read(sc, 0, &bbp0)) != 0)
5345 if (bbp0 != 0 && bbp0 != 0xff)
5351 /* initialize BBP registers to default values */
5352 if (sc->mac_ver >= 0x5390)
5353 run_rt5390_bbp_init(sc);
5355 for (i = 0; i < NELEM(rt2860_def_bbp); i++) {
5356 run_bbp_write(sc, rt2860_def_bbp[i].reg,
5357 rt2860_def_bbp[i].val);
5361 if (sc->mac_ver == 0x3593) {
5362 run_bbp_write(sc, 79, 0x13);
5363 run_bbp_write(sc, 80, 0x05);
5364 run_bbp_write(sc, 81, 0x33);
5365 run_bbp_write(sc, 86, 0x46);
5366 run_bbp_write(sc, 137, 0x0f);
5369 /* fix BBP84 for RT2860E */
5370 if (sc->mac_ver == 0x2860 && sc->mac_rev != 0x0101)
5371 run_bbp_write(sc, 84, 0x19);
5373 if (sc->mac_ver >= 0x3070 && (sc->mac_ver != 0x3593 &&
5374 sc->mac_ver != 0x5592)) {
5375 run_bbp_write(sc, 79, 0x13);
5376 run_bbp_write(sc, 80, 0x05);
5377 run_bbp_write(sc, 81, 0x33);
5378 } else if (sc->mac_ver == 0x2860 && sc->mac_rev == 0x0100) {
5379 run_bbp_write(sc, 69, 0x16);
5380 run_bbp_write(sc, 73, 0x12);
5386 run_rt3070_rf_init(struct run_softc *sc)
5389 uint8_t bbp4, mingain, rf, target;
5392 run_rt3070_rf_read(sc, 30, &rf);
5393 /* toggle RF R30 bit 7 */
5394 run_rt3070_rf_write(sc, 30, rf | 0x80);
5396 run_rt3070_rf_write(sc, 30, rf & ~0x80);
5398 /* initialize RF registers to default value */
5399 if (sc->mac_ver == 0x3572) {
5400 for (i = 0; i < NELEM(rt3572_def_rf); i++) {
5401 run_rt3070_rf_write(sc, rt3572_def_rf[i].reg,
5402 rt3572_def_rf[i].val);
5405 for (i = 0; i < NELEM(rt3070_def_rf); i++) {
5406 run_rt3070_rf_write(sc, rt3070_def_rf[i].reg,
5407 rt3070_def_rf[i].val);
5411 if (sc->mac_ver == 0x3070 && sc->mac_rev < 0x0201) {
5413 * Change voltage from 1.2V to 1.35V for RT3070.
5414 * The DAC issue (RT3070_LDO_CFG0) has been fixed
5417 run_read(sc, RT3070_LDO_CFG0, &tmp);
5418 tmp = (tmp & ~0x0f000000) | 0x0d000000;
5419 run_write(sc, RT3070_LDO_CFG0, tmp);
5421 } else if (sc->mac_ver == 0x3071) {
5422 run_rt3070_rf_read(sc, 6, &rf);
5423 run_rt3070_rf_write(sc, 6, rf | 0x40);
5424 run_rt3070_rf_write(sc, 31, 0x14);
5426 run_read(sc, RT3070_LDO_CFG0, &tmp);
5428 if (sc->mac_rev < 0x0211)
5429 tmp |= 0x0d000000; /* 1.3V */
5431 tmp |= 0x01000000; /* 1.2V */
5432 run_write(sc, RT3070_LDO_CFG0, tmp);
5434 /* patch LNA_PE_G1 */
5435 run_read(sc, RT3070_GPIO_SWITCH, &tmp);
5436 run_write(sc, RT3070_GPIO_SWITCH, tmp & ~0x20);
5438 } else if (sc->mac_ver == 0x3572) {
5439 run_rt3070_rf_read(sc, 6, &rf);
5440 run_rt3070_rf_write(sc, 6, rf | 0x40);
5442 /* increase voltage from 1.2V to 1.35V */
5443 run_read(sc, RT3070_LDO_CFG0, &tmp);
5444 tmp = (tmp & ~0x1f000000) | 0x0d000000;
5445 run_write(sc, RT3070_LDO_CFG0, tmp);
5447 if (sc->mac_rev < 0x0211 || !sc->patch_dac) {
5448 run_delay(sc, 1); /* wait for 1msec */
5449 /* decrease voltage back to 1.2V */
5450 tmp = (tmp & ~0x1f000000) | 0x01000000;
5451 run_write(sc, RT3070_LDO_CFG0, tmp);
5455 /* select 20MHz bandwidth */
5456 run_rt3070_rf_read(sc, 31, &rf);
5457 run_rt3070_rf_write(sc, 31, rf & ~0x20);
5459 /* calibrate filter for 20MHz bandwidth */
5460 sc->rf24_20mhz = 0x1f; /* default value */
5461 target = (sc->mac_ver < 0x3071) ? 0x16 : 0x13;
5462 run_rt3070_filter_calib(sc, 0x07, target, &sc->rf24_20mhz);
5464 /* select 40MHz bandwidth */
5465 run_bbp_read(sc, 4, &bbp4);
5466 run_bbp_write(sc, 4, (bbp4 & ~0x18) | 0x10);
5467 run_rt3070_rf_read(sc, 31, &rf);
5468 run_rt3070_rf_write(sc, 31, rf | 0x20);
5470 /* calibrate filter for 40MHz bandwidth */
5471 sc->rf24_40mhz = 0x2f; /* default value */
5472 target = (sc->mac_ver < 0x3071) ? 0x19 : 0x15;
5473 run_rt3070_filter_calib(sc, 0x27, target, &sc->rf24_40mhz);
5475 /* go back to 20MHz bandwidth */
5476 run_bbp_read(sc, 4, &bbp4);
5477 run_bbp_write(sc, 4, bbp4 & ~0x18);
5479 if (sc->mac_ver == 0x3572) {
5480 /* save default BBP registers 25 and 26 values */
5481 run_bbp_read(sc, 25, &sc->bbp25);
5482 run_bbp_read(sc, 26, &sc->bbp26);
5483 } else if (sc->mac_rev < 0x0201 || sc->mac_rev < 0x0211)
5484 run_rt3070_rf_write(sc, 27, 0x03);
5486 run_read(sc, RT3070_OPT_14, &tmp);
5487 run_write(sc, RT3070_OPT_14, tmp | 1);
5489 if (sc->mac_ver == 0x3070 || sc->mac_ver == 0x3071) {
5490 run_rt3070_rf_read(sc, 17, &rf);
5491 rf &= ~RT3070_TX_LO1;
5492 if ((sc->mac_ver == 0x3070 ||
5493 (sc->mac_ver == 0x3071 && sc->mac_rev >= 0x0211)) &&
5495 rf |= 0x20; /* fix for long range Rx issue */
5496 mingain = (sc->mac_ver == 0x3070) ? 1 : 2;
5497 if (sc->txmixgain_2ghz >= mingain)
5498 rf = (rf & ~0x7) | sc->txmixgain_2ghz;
5499 run_rt3070_rf_write(sc, 17, rf);
5502 if (sc->mac_rev == 0x3071) {
5503 run_rt3070_rf_read(sc, 1, &rf);
5504 rf &= ~(RT3070_RX0_PD | RT3070_TX0_PD);
5505 rf |= RT3070_RF_BLOCK | RT3070_RX1_PD | RT3070_TX1_PD;
5506 run_rt3070_rf_write(sc, 1, rf);
5508 run_rt3070_rf_read(sc, 15, &rf);
5509 run_rt3070_rf_write(sc, 15, rf & ~RT3070_TX_LO2);
5511 run_rt3070_rf_read(sc, 20, &rf);
5512 run_rt3070_rf_write(sc, 20, rf & ~RT3070_RX_LO1);
5514 run_rt3070_rf_read(sc, 21, &rf);
5515 run_rt3070_rf_write(sc, 21, rf & ~RT3070_RX_LO2);
5518 if (sc->mac_ver == 0x3070 || sc->mac_ver == 0x3071) {
5519 /* fix Tx to Rx IQ glitch by raising RF voltage */
5520 run_rt3070_rf_read(sc, 27, &rf);
5522 if (sc->mac_rev < 0x0211)
5524 run_rt3070_rf_write(sc, 27, rf);
5530 run_rt3593_rf_init(struct run_softc *sc)
5536 /* Disable the GPIO bits 4 and 7 for LNA PE control. */
5537 run_read(sc, RT3070_GPIO_SWITCH, &tmp);
5538 tmp &= ~(1 << 4 | 1 << 7);
5539 run_write(sc, RT3070_GPIO_SWITCH, tmp);
5541 /* Initialize RF registers to default value. */
5542 for (i = 0; i < NELEM(rt3593_def_rf); i++) {
5543 run_rt3070_rf_write(sc, rt3593_def_rf[i].reg,
5544 rt3593_def_rf[i].val);
5547 /* Toggle RF R2 to initiate calibration. */
5548 run_rt3070_rf_write(sc, 2, RT5390_RESCAL);
5550 /* Initialize RF frequency offset. */
5551 run_adjust_freq_offset(sc);
5553 run_rt3070_rf_read(sc, 18, &rf);
5554 run_rt3070_rf_write(sc, 18, rf | RT3593_AUTOTUNE_BYPASS);
5557 * Increase voltage from 1.2V to 1.35V, wait for 1 msec to
5558 * decrease voltage back to 1.2V.
5560 run_read(sc, RT3070_LDO_CFG0, &tmp);
5561 tmp = (tmp & ~0x1f000000) | 0x0d000000;
5562 run_write(sc, RT3070_LDO_CFG0, tmp);
5564 tmp = (tmp & ~0x1f000000) | 0x01000000;
5565 run_write(sc, RT3070_LDO_CFG0, tmp);
5567 sc->rf24_20mhz = 0x1f;
5568 sc->rf24_40mhz = 0x2f;
5570 /* Save default BBP registers 25 and 26 values. */
5571 run_bbp_read(sc, 25, &sc->bbp25);
5572 run_bbp_read(sc, 26, &sc->bbp26);
5574 run_read(sc, RT3070_OPT_14, &tmp);
5575 run_write(sc, RT3070_OPT_14, tmp | 1);
5579 run_rt5390_rf_init(struct run_softc *sc)
5585 /* Toggle RF R2 to initiate calibration. */
5586 if (sc->mac_ver == 0x5390) {
5587 run_rt3070_rf_read(sc, 2, &rf);
5588 run_rt3070_rf_write(sc, 2, rf | RT5390_RESCAL);
5590 run_rt3070_rf_write(sc, 2, rf & ~RT5390_RESCAL);
5592 run_rt3070_rf_write(sc, 2, RT5390_RESCAL);
5596 /* Initialize RF registers to default value. */
5597 if (sc->mac_ver == 0x5592) {
5598 for (i = 0; i < NELEM(rt5592_def_rf); i++) {
5599 run_rt3070_rf_write(sc, rt5592_def_rf[i].reg,
5600 rt5592_def_rf[i].val);
5602 /* Initialize RF frequency offset. */
5603 run_adjust_freq_offset(sc);
5604 } else if (sc->mac_ver == 0x5392) {
5605 for (i = 0; i < NELEM(rt5392_def_rf); i++) {
5606 run_rt3070_rf_write(sc, rt5392_def_rf[i].reg,
5607 rt5392_def_rf[i].val);
5609 if (sc->mac_rev >= 0x0223) {
5610 run_rt3070_rf_write(sc, 23, 0x0f);
5611 run_rt3070_rf_write(sc, 24, 0x3e);
5612 run_rt3070_rf_write(sc, 51, 0x32);
5613 run_rt3070_rf_write(sc, 53, 0x22);
5614 run_rt3070_rf_write(sc, 56, 0xc1);
5615 run_rt3070_rf_write(sc, 59, 0x0f);
5618 for (i = 0; i < NELEM(rt5390_def_rf); i++) {
5619 run_rt3070_rf_write(sc, rt5390_def_rf[i].reg,
5620 rt5390_def_rf[i].val);
5622 if (sc->mac_rev >= 0x0502) {
5623 run_rt3070_rf_write(sc, 6, 0xe0);
5624 run_rt3070_rf_write(sc, 25, 0x80);
5625 run_rt3070_rf_write(sc, 46, 0x73);
5626 run_rt3070_rf_write(sc, 53, 0x00);
5627 run_rt3070_rf_write(sc, 56, 0x42);
5628 run_rt3070_rf_write(sc, 61, 0xd1);
5632 sc->rf24_20mhz = 0x1f; /* default value */
5633 sc->rf24_40mhz = (sc->mac_ver == 0x5592) ? 0 : 0x2f;
5635 if (sc->mac_rev < 0x0211)
5636 run_rt3070_rf_write(sc, 27, 0x3);
5638 run_read(sc, RT3070_OPT_14, &tmp);
5639 run_write(sc, RT3070_OPT_14, tmp | 1);
5643 run_rt3070_filter_calib(struct run_softc *sc, uint8_t init, uint8_t target,
5647 uint8_t bbp55_pb, bbp55_sb, delta;
5650 /* program filter */
5651 run_rt3070_rf_read(sc, 24, &rf24);
5652 rf24 = (rf24 & 0xc0) | init; /* initial filter value */
5653 run_rt3070_rf_write(sc, 24, rf24);
5655 /* enable baseband loopback mode */
5656 run_rt3070_rf_read(sc, 22, &rf22);
5657 run_rt3070_rf_write(sc, 22, rf22 | 0x01);
5659 /* set power and frequency of passband test tone */
5660 run_bbp_write(sc, 24, 0x00);
5661 for (ntries = 0; ntries < 100; ntries++) {
5662 /* transmit test tone */
5663 run_bbp_write(sc, 25, 0x90);
5665 /* read received power */
5666 run_bbp_read(sc, 55, &bbp55_pb);
5673 /* set power and frequency of stopband test tone */
5674 run_bbp_write(sc, 24, 0x06);
5675 for (ntries = 0; ntries < 100; ntries++) {
5676 /* transmit test tone */
5677 run_bbp_write(sc, 25, 0x90);
5679 /* read received power */
5680 run_bbp_read(sc, 55, &bbp55_sb);
5682 delta = bbp55_pb - bbp55_sb;
5686 /* reprogram filter */
5688 run_rt3070_rf_write(sc, 24, rf24);
5692 rf24--; /* backtrack */
5694 run_rt3070_rf_write(sc, 24, rf24);
5697 /* restore initial state */
5698 run_bbp_write(sc, 24, 0x00);
5700 /* disable baseband loopback mode */
5701 run_rt3070_rf_read(sc, 22, &rf22);
5702 run_rt3070_rf_write(sc, 22, rf22 & ~0x01);
5708 run_rt3070_rf_setup(struct run_softc *sc)
5713 if (sc->mac_ver == 0x3572) {
5714 /* enable DC filter */
5715 if (sc->mac_rev >= 0x0201)
5716 run_bbp_write(sc, 103, 0xc0);
5718 run_bbp_read(sc, 138, &bbp);
5719 if (sc->ntxchains == 1)
5720 bbp |= 0x20; /* turn off DAC1 */
5721 if (sc->nrxchains == 1)
5722 bbp &= ~0x02; /* turn off ADC1 */
5723 run_bbp_write(sc, 138, bbp);
5725 if (sc->mac_rev >= 0x0211) {
5726 /* improve power consumption */
5727 run_bbp_read(sc, 31, &bbp);
5728 run_bbp_write(sc, 31, bbp & ~0x03);
5731 run_rt3070_rf_read(sc, 16, &rf);
5732 rf = (rf & ~0x07) | sc->txmixgain_2ghz;
5733 run_rt3070_rf_write(sc, 16, rf);
5735 } else if (sc->mac_ver == 0x3071) {
5736 if (sc->mac_rev >= 0x0211) {
5737 /* enable DC filter */
5738 run_bbp_write(sc, 103, 0xc0);
5740 /* improve power consumption */
5741 run_bbp_read(sc, 31, &bbp);
5742 run_bbp_write(sc, 31, bbp & ~0x03);
5745 run_bbp_read(sc, 138, &bbp);
5746 if (sc->ntxchains == 1)
5747 bbp |= 0x20; /* turn off DAC1 */
5748 if (sc->nrxchains == 1)
5749 bbp &= ~0x02; /* turn off ADC1 */
5750 run_bbp_write(sc, 138, bbp);
5752 run_write(sc, RT2860_TX_SW_CFG1, 0);
5753 if (sc->mac_rev < 0x0211) {
5754 run_write(sc, RT2860_TX_SW_CFG2,
5755 sc->patch_dac ? 0x2c : 0x0f);
5757 run_write(sc, RT2860_TX_SW_CFG2, 0);
5759 } else if (sc->mac_ver == 0x3070) {
5760 if (sc->mac_rev >= 0x0201) {
5761 /* enable DC filter */
5762 run_bbp_write(sc, 103, 0xc0);
5764 /* improve power consumption */
5765 run_bbp_read(sc, 31, &bbp);
5766 run_bbp_write(sc, 31, bbp & ~0x03);
5769 if (sc->mac_rev < 0x0201) {
5770 run_write(sc, RT2860_TX_SW_CFG1, 0);
5771 run_write(sc, RT2860_TX_SW_CFG2, 0x2c);
5773 run_write(sc, RT2860_TX_SW_CFG2, 0);
5776 /* initialize RF registers from ROM for >=RT3071*/
5777 if (sc->mac_ver >= 0x3071) {
5778 for (i = 0; i < 10; i++) {
5779 if (sc->rf[i].reg == 0 || sc->rf[i].reg == 0xff)
5781 run_rt3070_rf_write(sc, sc->rf[i].reg, sc->rf[i].val);
5787 run_rt3593_rf_setup(struct run_softc *sc)
5791 if (sc->mac_rev >= 0x0211) {
5792 /* Enable DC filter. */
5793 run_bbp_write(sc, 103, 0xc0);
5795 run_write(sc, RT2860_TX_SW_CFG1, 0);
5796 if (sc->mac_rev < 0x0211) {
5797 run_write(sc, RT2860_TX_SW_CFG2,
5798 sc->patch_dac ? 0x2c : 0x0f);
5800 run_write(sc, RT2860_TX_SW_CFG2, 0);
5802 run_rt3070_rf_read(sc, 50, &rf);
5803 run_rt3070_rf_write(sc, 50, rf & ~RT3593_TX_LO2);
5805 run_rt3070_rf_read(sc, 51, &rf);
5806 rf = (rf & ~(RT3593_TX_LO1 | 0x0c)) |
5807 ((sc->txmixgain_2ghz & 0x07) << 2);
5808 run_rt3070_rf_write(sc, 51, rf);
5810 run_rt3070_rf_read(sc, 38, &rf);
5811 run_rt3070_rf_write(sc, 38, rf & ~RT5390_RX_LO1);
5813 run_rt3070_rf_read(sc, 39, &rf);
5814 run_rt3070_rf_write(sc, 39, rf & ~RT5390_RX_LO2);
5816 run_rt3070_rf_read(sc, 1, &rf);
5817 run_rt3070_rf_write(sc, 1, rf & ~(RT3070_RF_BLOCK | RT3070_PLL_PD));
5819 run_rt3070_rf_read(sc, 30, &rf);
5820 rf = (rf & ~0x18) | 0x10;
5821 run_rt3070_rf_write(sc, 30, rf);
5823 /* Apply maximum likelihood detection for 2 stream case. */
5824 run_bbp_read(sc, 105, &bbp);
5825 if (sc->nrxchains > 1)
5826 run_bbp_write(sc, 105, bbp | RT5390_MLD);
5828 /* Avoid data lost and CRC error. */
5829 run_bbp_read(sc, 4, &bbp);
5830 run_bbp_write(sc, 4, bbp | RT5390_MAC_IF_CTRL);
5832 run_bbp_write(sc, 92, 0x02);
5833 run_bbp_write(sc, 82, 0x82);
5834 run_bbp_write(sc, 106, 0x05);
5835 run_bbp_write(sc, 104, 0x92);
5836 run_bbp_write(sc, 88, 0x90);
5837 run_bbp_write(sc, 148, 0xc8);
5838 run_bbp_write(sc, 47, 0x48);
5839 run_bbp_write(sc, 120, 0x50);
5841 run_bbp_write(sc, 163, 0x9d);
5844 run_bbp_write(sc, 142, 0x06);
5845 run_bbp_write(sc, 143, 0xa0);
5846 run_bbp_write(sc, 142, 0x07);
5847 run_bbp_write(sc, 143, 0xa1);
5848 run_bbp_write(sc, 142, 0x08);
5849 run_bbp_write(sc, 143, 0xa2);
5851 run_bbp_write(sc, 31, 0x08);
5852 run_bbp_write(sc, 68, 0x0b);
5853 run_bbp_write(sc, 105, 0x04);
5857 run_rt5390_rf_setup(struct run_softc *sc)
5861 if (sc->mac_rev >= 0x0211) {
5862 /* Enable DC filter. */
5863 run_bbp_write(sc, 103, 0xc0);
5865 if (sc->mac_ver != 0x5592) {
5866 /* Improve power consumption. */
5867 run_bbp_read(sc, 31, &bbp);
5868 run_bbp_write(sc, 31, bbp & ~0x03);
5872 run_bbp_read(sc, 138, &bbp);
5873 if (sc->ntxchains == 1)
5874 bbp |= 0x20; /* turn off DAC1 */
5875 if (sc->nrxchains == 1)
5876 bbp &= ~0x02; /* turn off ADC1 */
5877 run_bbp_write(sc, 138, bbp);
5879 run_rt3070_rf_read(sc, 38, &rf);
5880 run_rt3070_rf_write(sc, 38, rf & ~RT5390_RX_LO1);
5882 run_rt3070_rf_read(sc, 39, &rf);
5883 run_rt3070_rf_write(sc, 39, rf & ~RT5390_RX_LO2);
5885 /* Avoid data lost and CRC error. */
5886 run_bbp_read(sc, 4, &bbp);
5887 run_bbp_write(sc, 4, bbp | RT5390_MAC_IF_CTRL);
5889 run_rt3070_rf_read(sc, 30, &rf);
5890 rf = (rf & ~0x18) | 0x10;
5891 run_rt3070_rf_write(sc, 30, rf);
5893 if (sc->mac_ver != 0x5592) {
5894 run_write(sc, RT2860_TX_SW_CFG1, 0);
5895 if (sc->mac_rev < 0x0211) {
5896 run_write(sc, RT2860_TX_SW_CFG2,
5897 sc->patch_dac ? 0x2c : 0x0f);
5899 run_write(sc, RT2860_TX_SW_CFG2, 0);
5904 run_txrx_enable(struct run_softc *sc)
5906 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
5910 run_write(sc, RT2860_MAC_SYS_CTRL, RT2860_MAC_TX_EN);
5911 for (ntries = 0; ntries < 200; ntries++) {
5912 if ((error = run_read(sc, RT2860_WPDMA_GLO_CFG, &tmp)) != 0)
5914 if ((tmp & (RT2860_TX_DMA_BUSY | RT2860_RX_DMA_BUSY)) == 0)
5923 tmp |= RT2860_RX_DMA_EN | RT2860_TX_DMA_EN | RT2860_TX_WB_DDONE;
5924 run_write(sc, RT2860_WPDMA_GLO_CFG, tmp);
5926 /* enable Rx bulk aggregation (set timeout and limit) */
5927 tmp = RT2860_USB_TX_EN | RT2860_USB_RX_EN | RT2860_USB_RX_AGG_EN |
5928 RT2860_USB_RX_AGG_TO(128) | RT2860_USB_RX_AGG_LMT(2);
5929 run_write(sc, RT2860_USB_DMA_CFG, tmp);
5932 tmp = RT2860_DROP_CRC_ERR | RT2860_DROP_PHY_ERR;
5933 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
5934 tmp |= RT2860_DROP_UC_NOME | RT2860_DROP_DUPL |
5935 RT2860_DROP_CTS | RT2860_DROP_BA | RT2860_DROP_ACK |
5936 RT2860_DROP_VER_ERR | RT2860_DROP_CTRL_RSV |
5937 RT2860_DROP_CFACK | RT2860_DROP_CFEND;
5938 if (ic->ic_opmode == IEEE80211_M_STA)
5939 tmp |= RT2860_DROP_RTS | RT2860_DROP_PSPOLL;
5941 run_write(sc, RT2860_RX_FILTR_CFG, tmp);
5943 run_write(sc, RT2860_MAC_SYS_CTRL,
5944 RT2860_MAC_RX_EN | RT2860_MAC_TX_EN);
5950 run_adjust_freq_offset(struct run_softc *sc)
5954 run_rt3070_rf_read(sc, 17, &rf);
5956 rf = (rf & ~0x7f) | (sc->freq & 0x7f);
5960 run_mcu_cmd(sc, 0x74, (tmp << 8 ) | rf);
5964 run_init_locked(struct run_softc *sc)
5966 struct ifnet *ifp = sc->sc_ifp;
5967 struct ieee80211com *ic = ifp->if_l2com;
5974 if (ic->ic_nrunning > 1)
5979 if (run_load_microcode(sc) != 0) {
5980 device_printf(sc->sc_dev, "could not load 8051 microcode\n");
5984 for (ntries = 0; ntries < 100; ntries++) {
5985 if (run_read(sc, RT2860_ASIC_VER_ID, &tmp) != 0)
5987 if (tmp != 0 && tmp != 0xffffffff)
5994 for (i = 0; i != RUN_EP_QUEUES; i++)
5995 run_setup_tx_list(sc, &sc->sc_epq[i]);
5997 run_set_macaddr(sc, IF_LLADDR(ifp));
5999 for (ntries = 0; ntries < 100; ntries++) {
6000 if (run_read(sc, RT2860_WPDMA_GLO_CFG, &tmp) != 0)
6002 if ((tmp & (RT2860_TX_DMA_BUSY | RT2860_RX_DMA_BUSY)) == 0)
6006 if (ntries == 100) {
6007 device_printf(sc->sc_dev, "timeout waiting for DMA engine\n");
6011 tmp |= RT2860_TX_WB_DDONE;
6012 run_write(sc, RT2860_WPDMA_GLO_CFG, tmp);
6014 /* turn off PME_OEN to solve high-current issue */
6015 run_read(sc, RT2860_SYS_CTRL, &tmp);
6016 run_write(sc, RT2860_SYS_CTRL, tmp & ~RT2860_PME_OEN);
6018 run_write(sc, RT2860_MAC_SYS_CTRL,
6019 RT2860_BBP_HRST | RT2860_MAC_SRST);
6020 run_write(sc, RT2860_USB_DMA_CFG, 0);
6022 if (run_reset(sc) != 0) {
6023 device_printf(sc->sc_dev, "could not reset chipset\n");
6027 run_write(sc, RT2860_MAC_SYS_CTRL, 0);
6029 /* init Tx power for all Tx rates (from EEPROM) */
6030 for (ridx = 0; ridx < 5; ridx++) {
6031 if (sc->txpow20mhz[ridx] == 0xffffffff)
6033 run_write(sc, RT2860_TX_PWR_CFG(ridx), sc->txpow20mhz[ridx]);
6036 for (i = 0; i < NELEM(rt2870_def_mac); i++)
6037 run_write(sc, rt2870_def_mac[i].reg, rt2870_def_mac[i].val);
6038 run_write(sc, RT2860_WMM_AIFSN_CFG, 0x00002273);
6039 run_write(sc, RT2860_WMM_CWMIN_CFG, 0x00002344);
6040 run_write(sc, RT2860_WMM_CWMAX_CFG, 0x000034aa);
6042 if (sc->mac_ver >= 0x5390) {
6043 run_write(sc, RT2860_TX_SW_CFG0,
6044 4 << RT2860_DLY_PAPE_EN_SHIFT | 4);
6045 if (sc->mac_ver >= 0x5392) {
6046 run_write(sc, RT2860_MAX_LEN_CFG, 0x00002fff);
6047 if (sc->mac_ver == 0x5592) {
6048 run_write(sc, RT2860_HT_FBK_CFG1, 0xedcba980);
6049 run_write(sc, RT2860_TXOP_HLDR_ET, 0x00000082);
6051 run_write(sc, RT2860_HT_FBK_CFG1, 0xedcb4980);
6052 run_write(sc, RT2860_LG_FBK_CFG0, 0xedcba322);
6055 } else if (sc->mac_ver == 0x3593) {
6056 run_write(sc, RT2860_TX_SW_CFG0,
6057 4 << RT2860_DLY_PAPE_EN_SHIFT | 2);
6058 } else if (sc->mac_ver >= 0x3070) {
6059 /* set delay of PA_PE assertion to 1us (unit of 0.25us) */
6060 run_write(sc, RT2860_TX_SW_CFG0,
6061 4 << RT2860_DLY_PAPE_EN_SHIFT);
6064 /* wait while MAC is busy */
6065 for (ntries = 0; ntries < 100; ntries++) {
6066 if (run_read(sc, RT2860_MAC_STATUS_REG, &tmp) != 0)
6068 if (!(tmp & (RT2860_RX_STATUS_BUSY | RT2860_TX_STATUS_BUSY)))
6075 /* clear Host to MCU mailbox */
6076 run_write(sc, RT2860_H2M_BBPAGENT, 0);
6077 run_write(sc, RT2860_H2M_MAILBOX, 0);
6080 if (run_bbp_init(sc) != 0) {
6081 device_printf(sc->sc_dev, "could not initialize BBP\n");
6085 /* abort TSF synchronization */
6086 run_read(sc, RT2860_BCN_TIME_CFG, &tmp);
6087 tmp &= ~(RT2860_BCN_TX_EN | RT2860_TSF_TIMER_EN |
6088 RT2860_TBTT_TIMER_EN);
6089 run_write(sc, RT2860_BCN_TIME_CFG, tmp);
6091 /* clear RX WCID search table */
6092 run_set_region_4(sc, RT2860_WCID_ENTRY(0), 0, 512);
6093 /* clear WCID attribute table */
6094 run_set_region_4(sc, RT2860_WCID_ATTR(0), 0, 8 * 32);
6096 /* hostapd sets a key before init. So, don't clear it. */
6097 if (sc->cmdq_key_set != RUN_CMDQ_GO) {
6098 /* clear shared key table */
6099 run_set_region_4(sc, RT2860_SKEY(0, 0), 0, 8 * 32);
6100 /* clear shared key mode */
6101 run_set_region_4(sc, RT2860_SKEY_MODE_0_7, 0, 4);
6104 run_read(sc, RT2860_US_CYC_CNT, &tmp);
6105 tmp = (tmp & ~0xff) | 0x1e;
6106 run_write(sc, RT2860_US_CYC_CNT, tmp);
6108 if (sc->mac_rev != 0x0101)
6109 run_write(sc, RT2860_TXOP_CTRL_CFG, 0x0000583f);
6111 run_write(sc, RT2860_WMM_TXOP0_CFG, 0);
6112 run_write(sc, RT2860_WMM_TXOP1_CFG, 48 << 16 | 96);
6114 /* write vendor-specific BBP values (from EEPROM) */
6115 if (sc->mac_ver < 0x3593) {
6116 for (i = 0; i < 10; i++) {
6117 if (sc->bbp[i].reg == 0 || sc->bbp[i].reg == 0xff)
6119 run_bbp_write(sc, sc->bbp[i].reg, sc->bbp[i].val);
6123 /* select Main antenna for 1T1R devices */
6124 if (sc->rf_rev == RT3070_RF_3020 || sc->rf_rev == RT5390_RF_5370)
6125 run_set_rx_antenna(sc, 0);
6127 /* send LEDs operating mode to microcontroller */
6128 (void)run_mcu_cmd(sc, RT2860_MCU_CMD_LED1, sc->led[0]);
6129 (void)run_mcu_cmd(sc, RT2860_MCU_CMD_LED2, sc->led[1]);
6130 (void)run_mcu_cmd(sc, RT2860_MCU_CMD_LED3, sc->led[2]);
6132 if (sc->mac_ver >= 0x5390)
6133 run_rt5390_rf_init(sc);
6134 else if (sc->mac_ver == 0x3593)
6135 run_rt3593_rf_init(sc);
6136 else if (sc->mac_ver >= 0x3070)
6137 run_rt3070_rf_init(sc);
6139 /* disable non-existing Rx chains */
6140 run_bbp_read(sc, 3, &bbp3);
6141 bbp3 &= ~(1 << 3 | 1 << 4);
6142 if (sc->nrxchains == 2)
6144 else if (sc->nrxchains == 3)
6146 run_bbp_write(sc, 3, bbp3);
6148 /* disable non-existing Tx chains */
6149 run_bbp_read(sc, 1, &bbp1);
6150 if (sc->ntxchains == 1)
6151 bbp1 &= ~(1 << 3 | 1 << 4);
6152 run_bbp_write(sc, 1, bbp1);
6154 if (sc->mac_ver >= 0x5390)
6155 run_rt5390_rf_setup(sc);
6156 else if (sc->mac_ver == 0x3593)
6157 run_rt3593_rf_setup(sc);
6158 else if (sc->mac_ver >= 0x3070)
6159 run_rt3070_rf_setup(sc);
6161 /* select default channel */
6162 run_set_chan(sc, ic->ic_curchan);
6164 /* setup initial protection mode */
6165 run_updateprot_cb(ic);
6167 /* turn radio LED on */
6168 run_set_leds(sc, RT2860_LED_RADIO);
6170 ifq_clr_oactive(&ifp->if_snd);
6171 ifp->if_flags |= IFF_RUNNING;
6172 sc->cmdq_run = RUN_CMDQ_GO;
6174 for (i = 0; i != RUN_N_XFER; i++)
6175 usbd_xfer_set_stall(sc->sc_xfer[i]);
6177 usbd_transfer_start(sc->sc_xfer[RUN_BULK_RX]);
6179 if (run_txrx_enable(sc) != 0)
6191 struct run_softc *sc = arg;
6192 struct ifnet *ifp = sc->sc_ifp;
6193 struct ieee80211com *ic = ifp->if_l2com;
6196 run_init_locked(sc);
6199 if (ifp->if_flags & IFF_RUNNING)
6200 ieee80211_start_all(ic);
6206 struct run_softc *sc = (struct run_softc *)arg;
6207 struct ifnet *ifp = sc->sc_ifp;
6212 RUN_LOCK_ASSERT(sc, MA_OWNED);
6214 if (ifp->if_flags & IFF_RUNNING)
6215 run_set_leds(sc, 0); /* turn all LEDs off */
6217 ifp->if_flags &= ~IFF_RUNNING;
6218 ifq_clr_oactive(&ifp->if_snd);
6220 sc->ratectl_run = RUN_RATECTL_OFF;
6221 sc->cmdq_run = sc->cmdq_key_set;
6225 for(i = 0; i < RUN_N_XFER; i++)
6226 usbd_transfer_drain(sc->sc_xfer[i]);
6230 if (sc->rx_m != NULL) {
6235 /* Disable Tx/Rx DMA. */
6236 if (run_read(sc, RT2860_WPDMA_GLO_CFG, &tmp) != 0)
6238 tmp &= ~(RT2860_RX_DMA_EN | RT2860_TX_DMA_EN);
6239 run_write(sc, RT2860_WPDMA_GLO_CFG, tmp);
6241 for (ntries = 0; ntries < 100; ntries++) {
6242 if (run_read(sc, RT2860_WPDMA_GLO_CFG, &tmp) != 0)
6244 if ((tmp & (RT2860_TX_DMA_BUSY | RT2860_RX_DMA_BUSY)) == 0)
6248 if (ntries == 100) {
6249 device_printf(sc->sc_dev, "timeout waiting for DMA engine\n");
6254 run_read(sc, RT2860_MAC_SYS_CTRL, &tmp);
6255 tmp &= ~(RT2860_MAC_RX_EN | RT2860_MAC_TX_EN);
6256 run_write(sc, RT2860_MAC_SYS_CTRL, tmp);
6258 /* wait for pending Tx to complete */
6259 for (ntries = 0; ntries < 100; ntries++) {
6260 if (run_read(sc, RT2860_TXRXQ_PCNT, &tmp) != 0) {
6261 DPRINTF("Cannot read Tx queue count\n");
6264 if ((tmp & RT2860_TX2Q_PCNT_MASK) == 0) {
6265 DPRINTF("All Tx cleared\n");
6271 DPRINTF("There are still pending Tx\n");
6273 run_write(sc, RT2860_USB_DMA_CFG, 0);
6275 run_write(sc, RT2860_MAC_SYS_CTRL, RT2860_BBP_HRST | RT2860_MAC_SRST);
6276 run_write(sc, RT2860_MAC_SYS_CTRL, 0);
6278 for (i = 0; i != RUN_EP_QUEUES; i++)
6279 run_unsetup_tx_list(sc, &sc->sc_epq[i]);
6283 run_delay(struct run_softc *sc, u_int ms)
6285 usb_pause_ls(lockowned(&sc->sc_lock) ? &sc->sc_lock : NULL,
6286 &wlan_global_serializer,
6287 USB_MS_TO_TICKS(ms));
6290 static device_method_t run_methods[] = {
6291 /* Device interface */
6292 DEVMETHOD(device_probe, run_match),
6293 DEVMETHOD(device_attach, run_attach),
6294 DEVMETHOD(device_detach, run_detach),
6298 static driver_t run_driver = {
6300 .methods = run_methods,
6301 .size = sizeof(struct run_softc)
6304 static devclass_t run_devclass;
6306 DRIVER_MODULE(run, uhub, run_driver, run_devclass, run_driver_loaded, NULL);
6307 MODULE_DEPEND(run, wlan, 1, 1, 1);
6308 MODULE_DEPEND(run, usb, 1, 1, 1);
6309 MODULE_DEPEND(run, firmware, 1, 1, 1);
6310 MODULE_VERSION(run, 1);