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>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/cdefs.h>
20 __FBSDID("$FreeBSD$");
23 * Ralink Technology RT2700U/RT2800U/RT3000U chipset driver.
24 * http://www.ralinktech.com/
27 #include <sys/param.h>
28 #include <sys/sockio.h>
29 #include <sys/sysctl.h>
31 #include <sys/mutex.h>
33 #include <sys/kernel.h>
34 #include <sys/socket.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
39 #include <sys/endian.h>
40 #include <sys/linker.h>
41 #include <sys/firmware.h>
44 #include <machine/bus.h>
45 #include <machine/resource.h>
50 #include <net/if_arp.h>
51 #include <net/ethernet.h>
52 #include <net/if_dl.h>
53 #include <net/if_media.h>
54 #include <net/if_types.h>
56 #include <netinet/in.h>
57 #include <netinet/in_systm.h>
58 #include <netinet/in_var.h>
59 #include <netinet/if_ether.h>
60 #include <netinet/ip.h>
62 #include <net80211/ieee80211_var.h>
63 #include <net80211/ieee80211_regdomain.h>
64 #include <net80211/ieee80211_radiotap.h>
65 #include <net80211/ieee80211_ratectl.h>
67 #include <dev/usb/usb.h>
68 #include <dev/usb/usbdi.h>
71 #define USB_DEBUG_VAR run_debug
72 #include <dev/usb/usb_debug.h>
74 #include <dev/usb/wlan/if_runreg.h>
75 #include <dev/usb/wlan/if_runvar.h>
77 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
85 SYSCTL_NODE(_hw_usb, OID_AUTO, run, CTLFLAG_RW, 0, "USB run");
86 SYSCTL_INT(_hw_usb_run, OID_AUTO, debug, CTLFLAG_RW, &run_debug, 0,
90 #define IEEE80211_HAS_ADDR4(wh) \
91 (((wh)->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS)
94 * Because of LOR in run_key_delete(), use atomic instead.
95 * '& RUN_CMDQ_MASQ' is to loop cmdq[].
97 #define RUN_CMDQ_GET(c) (atomic_fetchadd_32((c), 1) & RUN_CMDQ_MASQ)
99 static const STRUCT_USB_HOST_ID run_devs[] = {
100 #define RUN_DEV(v,p) { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
101 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(ASUS2, USBN11),
140 RUN_DEV(AZUREWAVE, RT2870_1),
141 RUN_DEV(AZUREWAVE, RT2870_2),
142 RUN_DEV(AZUREWAVE, RT3070_1),
143 RUN_DEV(AZUREWAVE, RT3070_2),
144 RUN_DEV(AZUREWAVE, RT3070_3),
145 RUN_DEV(BELKIN, F5D8053V3),
146 RUN_DEV(BELKIN, F5D8055),
147 RUN_DEV(BELKIN, F6D4050V1),
148 RUN_DEV(BELKIN, RT2870_1),
149 RUN_DEV(BELKIN, RT2870_2),
150 RUN_DEV(CISCOLINKSYS2, RT3070),
151 RUN_DEV(CISCOLINKSYS3, RT3070),
152 RUN_DEV(CONCEPTRONIC2, RT2870_1),
153 RUN_DEV(CONCEPTRONIC2, RT2870_2),
154 RUN_DEV(CONCEPTRONIC2, RT2870_3),
155 RUN_DEV(CONCEPTRONIC2, RT2870_4),
156 RUN_DEV(CONCEPTRONIC2, RT2870_5),
157 RUN_DEV(CONCEPTRONIC2, RT2870_6),
158 RUN_DEV(CONCEPTRONIC2, RT2870_7),
159 RUN_DEV(CONCEPTRONIC2, RT2870_8),
160 RUN_DEV(CONCEPTRONIC2, RT3070_1),
161 RUN_DEV(CONCEPTRONIC2, RT3070_2),
162 RUN_DEV(CONCEPTRONIC2, VIGORN61),
163 RUN_DEV(COREGA, CGWLUSB300GNM),
164 RUN_DEV(COREGA, RT2870_1),
165 RUN_DEV(COREGA, RT2870_2),
166 RUN_DEV(COREGA, RT2870_3),
167 RUN_DEV(COREGA, RT3070),
168 RUN_DEV(CYBERTAN, RT2870),
169 RUN_DEV(DLINK, RT2870),
170 RUN_DEV(DLINK, RT3072),
171 RUN_DEV(DLINK2, DWA130),
172 RUN_DEV(DLINK2, RT2870_1),
173 RUN_DEV(DLINK2, RT2870_2),
174 RUN_DEV(DLINK2, RT3070_1),
175 RUN_DEV(DLINK2, RT3070_2),
176 RUN_DEV(DLINK2, RT3070_3),
177 RUN_DEV(DLINK2, RT3070_4),
178 RUN_DEV(DLINK2, RT3070_5),
179 RUN_DEV(DLINK2, RT3072),
180 RUN_DEV(DLINK2, RT3072_1),
181 RUN_DEV(EDIMAX, EW7717),
182 RUN_DEV(EDIMAX, EW7718),
183 RUN_DEV(EDIMAX, RT2870_1),
184 RUN_DEV(ENCORE, RT3070_1),
185 RUN_DEV(ENCORE, RT3070_2),
186 RUN_DEV(ENCORE, RT3070_3),
187 RUN_DEV(GIGABYTE, GNWB31N),
188 RUN_DEV(GIGABYTE, GNWB32L),
189 RUN_DEV(GIGABYTE, RT2870_1),
190 RUN_DEV(GIGASET, RT3070_1),
191 RUN_DEV(GIGASET, RT3070_2),
192 RUN_DEV(GUILLEMOT, HWNU300),
193 RUN_DEV(HAWKING, HWUN2),
194 RUN_DEV(HAWKING, RT2870_1),
195 RUN_DEV(HAWKING, RT2870_2),
196 RUN_DEV(HAWKING, RT3070),
197 RUN_DEV(IODATA, RT3072_1),
198 RUN_DEV(IODATA, RT3072_2),
199 RUN_DEV(IODATA, RT3072_3),
200 RUN_DEV(IODATA, RT3072_4),
201 RUN_DEV(LINKSYS4, RT3070),
202 RUN_DEV(LINKSYS4, WUSB100),
203 RUN_DEV(LINKSYS4, WUSB54GCV3),
204 RUN_DEV(LINKSYS4, WUSB600N),
205 RUN_DEV(LINKSYS4, WUSB600NV2),
206 RUN_DEV(LOGITEC, RT2870_1),
207 RUN_DEV(LOGITEC, RT2870_2),
208 RUN_DEV(LOGITEC, RT2870_3),
209 RUN_DEV(MELCO, RT2870_1),
210 RUN_DEV(MELCO, RT2870_2),
211 RUN_DEV(MELCO, WLIUCAG300N),
212 RUN_DEV(MELCO, WLIUCG300N),
213 RUN_DEV(MELCO, WLIUCG301N),
214 RUN_DEV(MELCO, WLIUCGN),
215 RUN_DEV(MOTOROLA4, RT2770),
216 RUN_DEV(MOTOROLA4, RT3070),
217 RUN_DEV(MSI, RT3070_1),
218 RUN_DEV(MSI, RT3070_2),
219 RUN_DEV(MSI, RT3070_3),
220 RUN_DEV(MSI, RT3070_4),
221 RUN_DEV(MSI, RT3070_5),
222 RUN_DEV(MSI, RT3070_6),
223 RUN_DEV(MSI, RT3070_7),
224 RUN_DEV(MSI, RT3070_8),
225 RUN_DEV(MSI, RT3070_9),
226 RUN_DEV(MSI, RT3070_10),
227 RUN_DEV(MSI, RT3070_11),
228 RUN_DEV(OVISLINK, RT3072),
229 RUN_DEV(PARA, RT3070),
230 RUN_DEV(PEGATRON, RT2870),
231 RUN_DEV(PEGATRON, RT3070),
232 RUN_DEV(PEGATRON, RT3070_2),
233 RUN_DEV(PEGATRON, RT3070_3),
234 RUN_DEV(PHILIPS, RT2870),
235 RUN_DEV(PLANEX2, GWUS300MINIS),
236 RUN_DEV(PLANEX2, GWUSMICRON),
237 RUN_DEV(PLANEX2, RT2870),
238 RUN_DEV(PLANEX2, RT3070),
239 RUN_DEV(QCOM, RT2870),
240 RUN_DEV(QUANTA, RT3070),
241 RUN_DEV(RALINK, RT2070),
242 RUN_DEV(RALINK, RT2770),
243 RUN_DEV(RALINK, RT2870),
244 RUN_DEV(RALINK, RT3070),
245 RUN_DEV(RALINK, RT3071),
246 RUN_DEV(RALINK, RT3072),
247 RUN_DEV(RALINK, RT3370),
248 RUN_DEV(RALINK, RT3572),
249 RUN_DEV(RALINK, RT8070),
250 RUN_DEV(SAMSUNG2, RT2870_1),
251 RUN_DEV(SENAO, RT2870_1),
252 RUN_DEV(SENAO, RT2870_2),
253 RUN_DEV(SENAO, RT2870_3),
254 RUN_DEV(SENAO, RT2870_4),
255 RUN_DEV(SENAO, RT3070),
256 RUN_DEV(SENAO, RT3071),
257 RUN_DEV(SENAO, RT3072_1),
258 RUN_DEV(SENAO, RT3072_2),
259 RUN_DEV(SENAO, RT3072_3),
260 RUN_DEV(SENAO, RT3072_4),
261 RUN_DEV(SENAO, RT3072_5),
262 RUN_DEV(SITECOMEU, RT2770),
263 RUN_DEV(SITECOMEU, RT2870_1),
264 RUN_DEV(SITECOMEU, RT2870_2),
265 RUN_DEV(SITECOMEU, RT2870_3),
266 RUN_DEV(SITECOMEU, RT2870_4),
267 RUN_DEV(SITECOMEU, RT3070),
268 RUN_DEV(SITECOMEU, RT3070_2),
269 RUN_DEV(SITECOMEU, RT3070_3),
270 RUN_DEV(SITECOMEU, RT3070_4),
271 RUN_DEV(SITECOMEU, RT3071),
272 RUN_DEV(SITECOMEU, RT3072_1),
273 RUN_DEV(SITECOMEU, RT3072_2),
274 RUN_DEV(SITECOMEU, RT3072_3),
275 RUN_DEV(SITECOMEU, RT3072_4),
276 RUN_DEV(SITECOMEU, RT3072_5),
277 RUN_DEV(SITECOMEU, RT3072_6),
278 RUN_DEV(SITECOMEU, WL608),
279 RUN_DEV(SPARKLAN, RT2870_1),
280 RUN_DEV(SPARKLAN, RT3070),
281 RUN_DEV(SWEEX2, LW153),
282 RUN_DEV(SWEEX2, LW303),
283 RUN_DEV(SWEEX2, LW313),
284 RUN_DEV(TOSHIBA, RT3070),
285 RUN_DEV(UMEDIA, RT2870_1),
286 RUN_DEV(ZCOM, RT2870_1),
287 RUN_DEV(ZCOM, RT2870_2),
288 RUN_DEV(ZINWELL, RT2870_1),
289 RUN_DEV(ZINWELL, RT2870_2),
290 RUN_DEV(ZINWELL, RT3070),
291 RUN_DEV(ZINWELL, RT3072_1),
292 RUN_DEV(ZINWELL, RT3072_2),
293 RUN_DEV(ZYXEL, RT2870_1),
294 RUN_DEV(ZYXEL, RT2870_2),
298 static device_probe_t run_match;
299 static device_attach_t run_attach;
300 static device_detach_t run_detach;
302 static usb_callback_t run_bulk_rx_callback;
303 static usb_callback_t run_bulk_tx_callback0;
304 static usb_callback_t run_bulk_tx_callback1;
305 static usb_callback_t run_bulk_tx_callback2;
306 static usb_callback_t run_bulk_tx_callback3;
307 static usb_callback_t run_bulk_tx_callback4;
308 static usb_callback_t run_bulk_tx_callback5;
310 static void run_bulk_tx_callbackN(struct usb_xfer *xfer,
311 usb_error_t error, unsigned int index);
312 static struct ieee80211vap *run_vap_create(struct ieee80211com *,
313 const char name[IFNAMSIZ], int unit, int opmode, int flags,
314 const uint8_t bssid[IEEE80211_ADDR_LEN], const uint8_t
315 mac[IEEE80211_ADDR_LEN]);
316 static void run_vap_delete(struct ieee80211vap *);
317 static void run_cmdq_cb(void *, int);
318 static void run_setup_tx_list(struct run_softc *,
319 struct run_endpoint_queue *);
320 static void run_unsetup_tx_list(struct run_softc *,
321 struct run_endpoint_queue *);
322 static int run_load_microcode(struct run_softc *);
323 static int run_reset(struct run_softc *);
324 static usb_error_t run_do_request(struct run_softc *,
325 struct usb_device_request *, void *);
326 static int run_read(struct run_softc *, uint16_t, uint32_t *);
327 static int run_read_region_1(struct run_softc *, uint16_t, uint8_t *, int);
328 static int run_write_2(struct run_softc *, uint16_t, uint16_t);
329 static int run_write(struct run_softc *, uint16_t, uint32_t);
330 static int run_write_region_1(struct run_softc *, uint16_t,
331 const uint8_t *, int);
332 static int run_set_region_4(struct run_softc *, uint16_t, uint32_t, int);
333 static int run_efuse_read_2(struct run_softc *, uint16_t, uint16_t *);
334 static int run_eeprom_read_2(struct run_softc *, uint16_t, uint16_t *);
335 static int run_rt2870_rf_write(struct run_softc *, uint8_t, uint32_t);
336 static int run_rt3070_rf_read(struct run_softc *, uint8_t, uint8_t *);
337 static int run_rt3070_rf_write(struct run_softc *, uint8_t, uint8_t);
338 static int run_bbp_read(struct run_softc *, uint8_t, uint8_t *);
339 static int run_bbp_write(struct run_softc *, uint8_t, uint8_t);
340 static int run_mcu_cmd(struct run_softc *, uint8_t, uint16_t);
341 static const char *run_get_rf(int);
342 static int run_read_eeprom(struct run_softc *);
343 static struct ieee80211_node *run_node_alloc(struct ieee80211vap *,
344 const uint8_t mac[IEEE80211_ADDR_LEN]);
345 static int run_media_change(struct ifnet *);
346 static int run_newstate(struct ieee80211vap *, enum ieee80211_state, int);
347 static int run_wme_update(struct ieee80211com *);
348 static void run_wme_update_cb(void *);
349 static void run_key_update_begin(struct ieee80211vap *);
350 static void run_key_update_end(struct ieee80211vap *);
351 static void run_key_set_cb(void *);
352 static int run_key_set(struct ieee80211vap *, struct ieee80211_key *,
353 const uint8_t mac[IEEE80211_ADDR_LEN]);
354 static void run_key_delete_cb(void *);
355 static int run_key_delete(struct ieee80211vap *, struct ieee80211_key *);
356 static void run_ratectl_to(void *);
357 static void run_ratectl_cb(void *, int);
358 static void run_drain_fifo(void *);
359 static void run_iter_func(void *, struct ieee80211_node *);
360 static void run_newassoc_cb(void *);
361 static void run_newassoc(struct ieee80211_node *, int);
362 static void run_rx_frame(struct run_softc *, struct mbuf *, uint32_t);
363 static void run_tx_free(struct run_endpoint_queue *pq,
364 struct run_tx_data *, int);
365 static void run_set_tx_desc(struct run_softc *, struct run_tx_data *);
366 static int run_tx(struct run_softc *, struct mbuf *,
367 struct ieee80211_node *);
368 static int run_tx_mgt(struct run_softc *, struct mbuf *,
369 struct ieee80211_node *);
370 static int run_sendprot(struct run_softc *, const struct mbuf *,
371 struct ieee80211_node *, int, int);
372 static int run_tx_param(struct run_softc *, struct mbuf *,
373 struct ieee80211_node *,
374 const struct ieee80211_bpf_params *);
375 static int run_raw_xmit(struct ieee80211_node *, struct mbuf *,
376 const struct ieee80211_bpf_params *);
377 static void run_start(struct ifnet *);
378 static int run_ioctl(struct ifnet *, u_long, caddr_t);
379 static void run_set_agc(struct run_softc *, uint8_t);
380 static void run_select_chan_group(struct run_softc *, int);
381 static void run_set_rx_antenna(struct run_softc *, int);
382 static void run_rt2870_set_chan(struct run_softc *, u_int);
383 static void run_rt3070_set_chan(struct run_softc *, u_int);
384 static void run_rt3572_set_chan(struct run_softc *, u_int);
385 static int run_set_chan(struct run_softc *, struct ieee80211_channel *);
386 static void run_set_channel(struct ieee80211com *);
387 static void run_scan_start(struct ieee80211com *);
388 static void run_scan_end(struct ieee80211com *);
389 static void run_update_beacon(struct ieee80211vap *, int);
390 static void run_update_beacon_cb(void *);
391 static void run_updateprot(struct ieee80211com *);
392 static void run_updateprot_cb(void *);
393 static void run_usb_timeout_cb(void *);
394 static void run_reset_livelock(struct run_softc *);
395 static void run_enable_tsf_sync(struct run_softc *);
396 static void run_enable_mrr(struct run_softc *);
397 static void run_set_txpreamble(struct run_softc *);
398 static void run_set_basicrates(struct run_softc *);
399 static void run_set_leds(struct run_softc *, uint16_t);
400 static void run_set_bssid(struct run_softc *, const uint8_t *);
401 static void run_set_macaddr(struct run_softc *, const uint8_t *);
402 static void run_updateslot(struct ifnet *);
403 static void run_updateslot_cb(void *);
404 static void run_update_mcast(struct ifnet *);
405 static int8_t run_rssi2dbm(struct run_softc *, uint8_t, uint8_t);
406 static void run_update_promisc_locked(struct ifnet *);
407 static void run_update_promisc(struct ifnet *);
408 static int run_bbp_init(struct run_softc *);
409 static int run_rt3070_rf_init(struct run_softc *);
410 static int run_rt3070_filter_calib(struct run_softc *, uint8_t, uint8_t,
412 static void run_rt3070_rf_setup(struct run_softc *);
413 static int run_txrx_enable(struct run_softc *);
414 static void run_init(void *);
415 static void run_init_locked(struct run_softc *);
416 static void run_stop(void *);
417 static void run_delay(struct run_softc *, unsigned int);
419 static const struct {
422 } rt2870_def_mac[] = {
426 static const struct {
429 } rt2860_def_bbp[] = {
433 static const struct rfprog {
435 uint32_t r1, r2, r3, r4;
436 } rt2860_rf2850[] = {
446 static const struct {
449 } rt3070_def_rf[] = {
451 },rt3572_def_rf[] = {
455 static const struct usb_config run_config[RUN_N_XFER] = {
458 .endpoint = UE_ADDR_ANY,
460 .direction = UE_DIR_OUT,
461 .bufsize = RUN_MAX_TXSZ,
462 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
463 .callback = run_bulk_tx_callback0,
464 .timeout = 5000, /* ms */
468 .endpoint = UE_ADDR_ANY,
469 .direction = UE_DIR_OUT,
471 .bufsize = RUN_MAX_TXSZ,
472 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
473 .callback = run_bulk_tx_callback1,
474 .timeout = 5000, /* ms */
478 .endpoint = UE_ADDR_ANY,
479 .direction = UE_DIR_OUT,
481 .bufsize = RUN_MAX_TXSZ,
482 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
483 .callback = run_bulk_tx_callback2,
484 .timeout = 5000, /* ms */
488 .endpoint = UE_ADDR_ANY,
489 .direction = UE_DIR_OUT,
491 .bufsize = RUN_MAX_TXSZ,
492 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
493 .callback = run_bulk_tx_callback3,
494 .timeout = 5000, /* ms */
496 [RUN_BULK_TX_HCCA] = {
498 .endpoint = UE_ADDR_ANY,
499 .direction = UE_DIR_OUT,
501 .bufsize = RUN_MAX_TXSZ,
502 .flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
503 .callback = run_bulk_tx_callback4,
504 .timeout = 5000, /* ms */
506 [RUN_BULK_TX_PRIO] = {
508 .endpoint = UE_ADDR_ANY,
509 .direction = UE_DIR_OUT,
511 .bufsize = RUN_MAX_TXSZ,
512 .flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
513 .callback = run_bulk_tx_callback5,
514 .timeout = 5000, /* ms */
518 .endpoint = UE_ADDR_ANY,
519 .direction = UE_DIR_IN,
520 .bufsize = RUN_MAX_RXSZ,
521 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
522 .callback = run_bulk_rx_callback,
527 run_match(device_t self)
529 struct usb_attach_arg *uaa = device_get_ivars(self);
531 if (uaa->usb_mode != USB_MODE_HOST)
533 if (uaa->info.bConfigIndex != 0)
535 if (uaa->info.bIfaceIndex != RT2860_IFACE_INDEX)
538 return (usbd_lookup_id_by_uaa(run_devs, sizeof(run_devs), uaa));
542 run_attach(device_t self)
544 struct run_softc *sc = device_get_softc(self);
545 struct usb_attach_arg *uaa = device_get_ivars(self);
546 struct ieee80211com *ic;
549 int i, ntries, error;
550 uint8_t iface_index, bands;
552 device_set_usb_desc(self);
553 sc->sc_udev = uaa->device;
556 mtx_init(&sc->sc_mtx, device_get_nameunit(sc->sc_dev),
557 MTX_NETWORK_LOCK, MTX_DEF);
559 iface_index = RT2860_IFACE_INDEX;
561 error = usbd_transfer_setup(uaa->device, &iface_index,
562 sc->sc_xfer, run_config, RUN_N_XFER, sc, &sc->sc_mtx);
564 device_printf(self, "could not allocate USB transfers, "
565 "err=%s\n", usbd_errstr(error));
571 /* wait for the chip to settle */
572 for (ntries = 0; ntries < 100; ntries++) {
573 if (run_read(sc, RT2860_ASIC_VER_ID, &ver) != 0) {
577 if (ver != 0 && ver != 0xffffffff)
582 device_printf(sc->sc_dev,
583 "timeout waiting for NIC to initialize\n");
587 sc->mac_ver = ver >> 16;
588 sc->mac_rev = ver & 0xffff;
590 /* retrieve RF rev. no and various other things from EEPROM */
593 device_printf(sc->sc_dev,
594 "MAC/BBP RT%04X (rev 0x%04X), RF %s (MIMO %dT%dR), address %s\n",
595 sc->mac_ver, sc->mac_rev, run_get_rf(sc->rf_rev),
596 sc->ntxchains, sc->nrxchains, ether_sprintf(sc->sc_bssid));
598 if ((error = run_load_microcode(sc)) != 0) {
599 device_printf(sc->sc_dev, "could not load 8051 microcode\n");
606 ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
608 device_printf(sc->sc_dev, "can not if_alloc()\n");
614 if_initname(ifp, "run", device_get_unit(sc->sc_dev));
615 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
616 ifp->if_init = run_init;
617 ifp->if_ioctl = run_ioctl;
618 ifp->if_start = run_start;
619 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
620 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
621 IFQ_SET_READY(&ifp->if_snd);
624 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
625 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
627 /* set device capabilities */
629 IEEE80211_C_STA | /* station mode supported */
630 IEEE80211_C_MONITOR | /* monitor mode supported */
633 IEEE80211_C_WDS | /* 4-address traffic works */
635 IEEE80211_C_SHPREAMBLE | /* short preamble supported */
636 IEEE80211_C_SHSLOT | /* short slot time supported */
637 IEEE80211_C_WME | /* WME */
638 IEEE80211_C_WPA; /* WPA1|WPA2(RSN) */
641 IEEE80211_CRYPTO_WEP |
642 IEEE80211_CRYPTO_AES_CCM |
643 IEEE80211_CRYPTO_TKIPMIC |
644 IEEE80211_CRYPTO_TKIP;
646 ic->ic_flags |= IEEE80211_F_DATAPAD;
647 ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS;
650 setbit(&bands, IEEE80211_MODE_11B);
651 setbit(&bands, IEEE80211_MODE_11G);
652 ieee80211_init_channels(ic, NULL, &bands);
655 * Do this by own because h/w supports
656 * more channels than ieee80211_init_channels()
658 if (sc->rf_rev == RT2860_RF_2750 ||
659 sc->rf_rev == RT2860_RF_2850 ||
660 sc->rf_rev == RT3070_RF_3052) {
661 /* set supported .11a rates */
662 for (i = 14; i < nitems(rt2860_rf2850); i++) {
663 uint8_t chan = rt2860_rf2850[i].chan;
664 ic->ic_channels[ic->ic_nchans].ic_freq =
665 ieee80211_ieee2mhz(chan, IEEE80211_CHAN_A);
666 ic->ic_channels[ic->ic_nchans].ic_ieee = chan;
667 ic->ic_channels[ic->ic_nchans].ic_flags = IEEE80211_CHAN_A;
668 ic->ic_channels[ic->ic_nchans].ic_extieee = 0;
673 ieee80211_ifattach(ic, sc->sc_bssid);
675 ic->ic_scan_start = run_scan_start;
676 ic->ic_scan_end = run_scan_end;
677 ic->ic_set_channel = run_set_channel;
678 ic->ic_node_alloc = run_node_alloc;
679 ic->ic_newassoc = run_newassoc;
680 ic->ic_updateslot = run_updateslot;
681 ic->ic_update_mcast = run_update_mcast;
682 ic->ic_wme.wme_update = run_wme_update;
683 ic->ic_raw_xmit = run_raw_xmit;
684 ic->ic_update_promisc = run_update_promisc;
686 ic->ic_vap_create = run_vap_create;
687 ic->ic_vap_delete = run_vap_delete;
689 ieee80211_radiotap_attach(ic,
690 &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
691 RUN_TX_RADIOTAP_PRESENT,
692 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
693 RUN_RX_RADIOTAP_PRESENT);
695 TASK_INIT(&sc->cmdq_task, 0, run_cmdq_cb, sc);
696 TASK_INIT(&sc->ratectl_task, 0, run_ratectl_cb, sc);
697 callout_init((struct callout *)&sc->ratectl_ch, 1);
700 ieee80211_announce(ic);
710 run_detach(device_t self)
712 struct run_softc *sc = device_get_softc(self);
713 struct ifnet *ifp = sc->sc_ifp;
714 struct ieee80211com *ic;
717 /* stop all USB transfers */
718 usbd_transfer_unsetup(sc->sc_xfer, RUN_N_XFER);
722 sc->ratectl_run = RUN_RATECTL_OFF;
723 sc->cmdq_run = sc->cmdq_key_set = RUN_CMDQ_ABORT;
725 /* free TX list, if any */
726 for (i = 0; i != RUN_EP_QUEUES; i++)
727 run_unsetup_tx_list(sc, &sc->sc_epq[i]);
733 usb_callout_drain(&sc->ratectl_ch);
734 ieee80211_draintask(ic, &sc->cmdq_task);
735 ieee80211_draintask(ic, &sc->ratectl_task);
736 ieee80211_ifdetach(ic);
740 mtx_destroy(&sc->sc_mtx);
745 static struct ieee80211vap *
746 run_vap_create(struct ieee80211com *ic,
747 const char name[IFNAMSIZ], int unit, int opmode, int flags,
748 const uint8_t bssid[IEEE80211_ADDR_LEN],
749 const uint8_t mac[IEEE80211_ADDR_LEN])
751 struct ifnet *ifp = ic->ic_ifp;
752 struct run_softc *sc = ifp->if_softc;
754 struct ieee80211vap *vap;
757 if (sc->rvp_cnt >= RUN_VAP_MAX) {
758 if_printf(ifp, "number of VAPs maxed out\n");
763 case IEEE80211_M_STA:
764 /* enable s/w bmiss handling for sta mode */
765 flags |= IEEE80211_CLONE_NOBEACONS;
767 case IEEE80211_M_IBSS:
768 case IEEE80211_M_MONITOR:
769 case IEEE80211_M_HOSTAP:
770 case IEEE80211_M_MBSS:
771 /* other than WDS vaps, only one at a time */
772 if (!TAILQ_EMPTY(&ic->ic_vaps))
775 case IEEE80211_M_WDS:
776 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next){
777 if(vap->iv_opmode != IEEE80211_M_HOSTAP)
779 /* WDS vap's always share the local mac address. */
780 flags &= ~IEEE80211_CLONE_BSSID;
784 if_printf(ifp, "wds only supported in ap mode\n");
789 if_printf(ifp, "unknown opmode %d\n", opmode);
793 rvp = (struct run_vap *) malloc(sizeof(struct run_vap),
794 M_80211_VAP, M_NOWAIT | M_ZERO);
798 ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac);
800 vap->iv_key_update_begin = run_key_update_begin;
801 vap->iv_key_update_end = run_key_update_end;
802 vap->iv_update_beacon = run_update_beacon;
803 vap->iv_max_aid = RT2870_WCID_MAX;
805 * To delete the right key from h/w, we need wcid.
806 * Luckily, there is unused space in ieee80211_key{}, wk_pad,
807 * and matching wcid will be written into there. So, cast
808 * some spells to remove 'const' from ieee80211_key{}
810 vap->iv_key_delete = (void *)run_key_delete;
811 vap->iv_key_set = (void *)run_key_set;
813 /* override state transition machine */
814 rvp->newstate = vap->iv_newstate;
815 vap->iv_newstate = run_newstate;
817 ieee80211_ratectl_init(vap);
818 ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */);
821 ieee80211_vap_attach(vap, run_media_change, ieee80211_media_status);
823 /* make sure id is always unique */
824 for (i = 0; i < RUN_VAP_MAX; i++) {
825 if((sc->rvp_bmap & 1 << i) == 0){
826 sc->rvp_bmap |= 1 << i;
831 if (sc->rvp_cnt++ == 0)
832 ic->ic_opmode = opmode;
834 if (opmode == IEEE80211_M_HOSTAP)
835 sc->cmdq_run = RUN_CMDQ_GO;
837 DPRINTF("rvp_id=%d bmap=%x rvp_cnt=%d\n",
838 rvp->rvp_id, sc->rvp_bmap, sc->rvp_cnt);
844 run_vap_delete(struct ieee80211vap *vap)
846 struct run_vap *rvp = RUN_VAP(vap);
848 struct ieee80211com *ic;
849 struct run_softc *sc;
862 m_freem(rvp->beacon_mbuf);
863 rvp->beacon_mbuf = NULL;
865 rvp_id = rvp->rvp_id;
866 sc->ratectl_run &= ~(1 << rvp_id);
867 sc->rvp_bmap &= ~(1 << rvp_id);
868 run_set_region_4(sc, RT2860_SKEY(rvp_id, 0), 0, 128);
869 run_set_region_4(sc, RT2860_BCN_BASE(rvp_id), 0, 512);
872 DPRINTF("vap=%p rvp_id=%d bmap=%x rvp_cnt=%d\n",
873 vap, rvp_id, sc->rvp_bmap, sc->rvp_cnt);
877 ieee80211_ratectl_deinit(vap);
878 ieee80211_vap_detach(vap);
879 free(rvp, M_80211_VAP);
883 * There are numbers of functions need to be called in context thread.
884 * Rather than creating taskqueue event for each of those functions,
885 * here is all-for-one taskqueue callback function. This function
886 * gurantees deferred functions are executed in the same order they
888 * '& RUN_CMDQ_MASQ' is to loop cmdq[].
891 run_cmdq_cb(void *arg, int pending)
893 struct run_softc *sc = arg;
896 /* call cmdq[].func locked */
898 for (i = sc->cmdq_exec; sc->cmdq[i].func && pending;
899 i = sc->cmdq_exec, pending--) {
900 DPRINTFN(6, "cmdq_exec=%d pending=%d\n", i, pending);
901 if (sc->cmdq_run == RUN_CMDQ_GO) {
903 * If arg0 is NULL, callback func needs more
904 * than one arg. So, pass ptr to cmdq struct.
906 if (sc->cmdq[i].arg0)
907 sc->cmdq[i].func(sc->cmdq[i].arg0);
909 sc->cmdq[i].func(&sc->cmdq[i]);
911 sc->cmdq[i].arg0 = NULL;
912 sc->cmdq[i].func = NULL;
914 sc->cmdq_exec &= RUN_CMDQ_MASQ;
920 run_setup_tx_list(struct run_softc *sc, struct run_endpoint_queue *pq)
922 struct run_tx_data *data;
924 memset(pq, 0, sizeof(*pq));
926 STAILQ_INIT(&pq->tx_qh);
927 STAILQ_INIT(&pq->tx_fh);
929 for (data = &pq->tx_data[0];
930 data < &pq->tx_data[RUN_TX_RING_COUNT]; data++) {
932 STAILQ_INSERT_TAIL(&pq->tx_fh, data, next);
934 pq->tx_nfree = RUN_TX_RING_COUNT;
938 run_unsetup_tx_list(struct run_softc *sc, struct run_endpoint_queue *pq)
940 struct run_tx_data *data;
942 /* make sure any subsequent use of the queues will fail */
944 STAILQ_INIT(&pq->tx_fh);
945 STAILQ_INIT(&pq->tx_qh);
947 /* free up all node references and mbufs */
948 for (data = &pq->tx_data[0];
949 data < &pq->tx_data[RUN_TX_RING_COUNT]; data++) {
950 if (data->m != NULL) {
954 if (data->ni != NULL) {
955 ieee80211_free_node(data->ni);
962 run_load_microcode(struct run_softc *sc)
964 usb_device_request_t req;
965 const struct firmware *fw;
969 const uint64_t *temp;
973 fw = firmware_get("runfw");
976 device_printf(sc->sc_dev,
977 "failed loadfirmware of file %s\n", "runfw");
981 if (fw->datasize != 8192) {
982 device_printf(sc->sc_dev,
983 "invalid firmware size (should be 8KB)\n");
989 * RT3071/RT3072 use a different firmware
990 * run-rt2870 (8KB) contains both,
991 * first half (4KB) is for rt2870,
992 * last half is for rt3071.
995 if ((sc->mac_ver) != 0x2860 &&
996 (sc->mac_ver) != 0x2872 &&
997 (sc->mac_ver) != 0x3070) {
1001 /* cheap sanity check */
1004 if (bytes != be64toh(0xffffff0210280210)) {
1005 device_printf(sc->sc_dev, "firmware checksum failed\n");
1010 run_read(sc, RT2860_ASIC_VER_ID, &tmp);
1011 /* write microcode image */
1012 run_write_region_1(sc, RT2870_FW_BASE, base, 4096);
1013 run_write(sc, RT2860_H2M_MAILBOX_CID, 0xffffffff);
1014 run_write(sc, RT2860_H2M_MAILBOX_STATUS, 0xffffffff);
1016 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1017 req.bRequest = RT2870_RESET;
1018 USETW(req.wValue, 8);
1019 USETW(req.wIndex, 0);
1020 USETW(req.wLength, 0);
1021 if ((error = usbd_do_request(sc->sc_udev, &sc->sc_mtx, &req, NULL))
1023 device_printf(sc->sc_dev, "firmware reset failed\n");
1029 run_write(sc, RT2860_H2M_MAILBOX, 0);
1030 if ((error = run_mcu_cmd(sc, RT2860_MCU_CMD_RFRESET, 0)) != 0)
1033 /* wait until microcontroller is ready */
1034 for (ntries = 0; ntries < 1000; ntries++) {
1035 if ((error = run_read(sc, RT2860_SYS_CTRL, &tmp)) != 0) {
1038 if (tmp & RT2860_MCU_READY)
1042 if (ntries == 1000) {
1043 device_printf(sc->sc_dev,
1044 "timeout waiting for MCU to initialize\n");
1048 device_printf(sc->sc_dev, "firmware %s loaded\n",
1049 (base == fw->data) ? "RT2870" : "RT3071");
1052 firmware_put(fw, FIRMWARE_UNLOAD);
1057 run_reset(struct run_softc *sc)
1059 usb_device_request_t req;
1061 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1062 req.bRequest = RT2870_RESET;
1063 USETW(req.wValue, 1);
1064 USETW(req.wIndex, 0);
1065 USETW(req.wLength, 0);
1066 return (usbd_do_request(sc->sc_udev, &sc->sc_mtx, &req, NULL));
1070 run_do_request(struct run_softc *sc,
1071 struct usb_device_request *req, void *data)
1076 RUN_LOCK_ASSERT(sc, MA_OWNED);
1079 err = usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
1080 req, data, 0, NULL, 250 /* ms */);
1083 DPRINTFN(1, "Control request failed, %s (retrying)\n",
1091 run_read(struct run_softc *sc, uint16_t reg, uint32_t *val)
1096 error = run_read_region_1(sc, reg, (uint8_t *)&tmp, sizeof tmp);
1098 *val = le32toh(tmp);
1105 run_read_region_1(struct run_softc *sc, uint16_t reg, uint8_t *buf, int len)
1107 usb_device_request_t req;
1109 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1110 req.bRequest = RT2870_READ_REGION_1;
1111 USETW(req.wValue, 0);
1112 USETW(req.wIndex, reg);
1113 USETW(req.wLength, len);
1115 return (run_do_request(sc, &req, buf));
1119 run_write_2(struct run_softc *sc, uint16_t reg, uint16_t val)
1121 usb_device_request_t req;
1123 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1124 req.bRequest = RT2870_WRITE_2;
1125 USETW(req.wValue, val);
1126 USETW(req.wIndex, reg);
1127 USETW(req.wLength, 0);
1129 return (run_do_request(sc, &req, NULL));
1133 run_write(struct run_softc *sc, uint16_t reg, uint32_t val)
1137 if ((error = run_write_2(sc, reg, val & 0xffff)) == 0)
1138 error = run_write_2(sc, reg + 2, val >> 16);
1143 run_write_region_1(struct run_softc *sc, uint16_t reg, const uint8_t *buf,
1149 * NB: the WRITE_REGION_1 command is not stable on RT2860.
1150 * We thus issue multiple WRITE_2 commands instead.
1152 KASSERT((len & 1) == 0, ("run_write_region_1: Data too long.\n"));
1153 for (i = 0; i < len && error == 0; i += 2)
1154 error = run_write_2(sc, reg + i, buf[i] | buf[i + 1] << 8);
1157 usb_device_request_t req;
1159 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1160 req.bRequest = RT2870_WRITE_REGION_1;
1161 USETW(req.wValue, 0);
1162 USETW(req.wIndex, reg);
1163 USETW(req.wLength, len);
1164 return (run_do_request(sc, &req, buf));
1169 run_set_region_4(struct run_softc *sc, uint16_t reg, uint32_t val, int len)
1173 KASSERT((len & 3) == 0, ("run_set_region_4: Invalid data length.\n"));
1174 for (i = 0; i < len && error == 0; i += 4)
1175 error = run_write(sc, reg + i, val);
1179 /* Read 16-bit from eFUSE ROM (RT3070 only.) */
1181 run_efuse_read_2(struct run_softc *sc, uint16_t addr, uint16_t *val)
1187 if ((error = run_read(sc, RT3070_EFUSE_CTRL, &tmp)) != 0)
1192 * Read one 16-byte block into registers EFUSE_DATA[0-3]:
1198 tmp &= ~(RT3070_EFSROM_MODE_MASK | RT3070_EFSROM_AIN_MASK);
1199 tmp |= (addr & ~0xf) << RT3070_EFSROM_AIN_SHIFT | RT3070_EFSROM_KICK;
1200 run_write(sc, RT3070_EFUSE_CTRL, tmp);
1201 for (ntries = 0; ntries < 100; ntries++) {
1202 if ((error = run_read(sc, RT3070_EFUSE_CTRL, &tmp)) != 0)
1204 if (!(tmp & RT3070_EFSROM_KICK))
1211 if ((tmp & RT3070_EFUSE_AOUT_MASK) == RT3070_EFUSE_AOUT_MASK) {
1212 *val = 0xffff; /* address not found */
1215 /* determine to which 32-bit register our 16-bit word belongs */
1216 reg = RT3070_EFUSE_DATA3 - (addr & 0xc);
1217 if ((error = run_read(sc, reg, &tmp)) != 0)
1220 *val = (addr & 2) ? tmp >> 16 : tmp & 0xffff;
1225 run_eeprom_read_2(struct run_softc *sc, uint16_t addr, uint16_t *val)
1227 usb_device_request_t req;
1232 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1233 req.bRequest = RT2870_EEPROM_READ;
1234 USETW(req.wValue, 0);
1235 USETW(req.wIndex, addr);
1236 USETW(req.wLength, sizeof tmp);
1238 error = usbd_do_request(sc->sc_udev, &sc->sc_mtx, &req, &tmp);
1240 *val = le16toh(tmp);
1247 run_srom_read(struct run_softc *sc, uint16_t addr, uint16_t *val)
1249 /* either eFUSE ROM or EEPROM */
1250 return sc->sc_srom_read(sc, addr, val);
1254 run_rt2870_rf_write(struct run_softc *sc, uint8_t reg, uint32_t val)
1259 for (ntries = 0; ntries < 10; ntries++) {
1260 if ((error = run_read(sc, RT2860_RF_CSR_CFG0, &tmp)) != 0)
1262 if (!(tmp & RT2860_RF_REG_CTRL))
1268 /* RF registers are 24-bit on the RT2860 */
1269 tmp = RT2860_RF_REG_CTRL | 24 << RT2860_RF_REG_WIDTH_SHIFT |
1270 (val & 0x3fffff) << 2 | (reg & 3);
1271 return (run_write(sc, RT2860_RF_CSR_CFG0, tmp));
1275 run_rt3070_rf_read(struct run_softc *sc, uint8_t reg, uint8_t *val)
1280 for (ntries = 0; ntries < 100; ntries++) {
1281 if ((error = run_read(sc, RT3070_RF_CSR_CFG, &tmp)) != 0)
1283 if (!(tmp & RT3070_RF_KICK))
1289 tmp = RT3070_RF_KICK | reg << 8;
1290 if ((error = run_write(sc, RT3070_RF_CSR_CFG, tmp)) != 0)
1293 for (ntries = 0; ntries < 100; ntries++) {
1294 if ((error = run_read(sc, RT3070_RF_CSR_CFG, &tmp)) != 0)
1296 if (!(tmp & RT3070_RF_KICK))
1307 run_rt3070_rf_write(struct run_softc *sc, uint8_t reg, uint8_t val)
1312 for (ntries = 0; ntries < 10; ntries++) {
1313 if ((error = run_read(sc, RT3070_RF_CSR_CFG, &tmp)) != 0)
1315 if (!(tmp & RT3070_RF_KICK))
1321 tmp = RT3070_RF_WRITE | RT3070_RF_KICK | reg << 8 | val;
1322 return (run_write(sc, RT3070_RF_CSR_CFG, tmp));
1326 run_bbp_read(struct run_softc *sc, uint8_t reg, uint8_t *val)
1331 for (ntries = 0; ntries < 10; ntries++) {
1332 if ((error = run_read(sc, RT2860_BBP_CSR_CFG, &tmp)) != 0)
1334 if (!(tmp & RT2860_BBP_CSR_KICK))
1340 tmp = RT2860_BBP_CSR_READ | RT2860_BBP_CSR_KICK | reg << 8;
1341 if ((error = run_write(sc, RT2860_BBP_CSR_CFG, tmp)) != 0)
1344 for (ntries = 0; ntries < 10; ntries++) {
1345 if ((error = run_read(sc, RT2860_BBP_CSR_CFG, &tmp)) != 0)
1347 if (!(tmp & RT2860_BBP_CSR_KICK))
1358 run_bbp_write(struct run_softc *sc, uint8_t reg, uint8_t val)
1363 for (ntries = 0; ntries < 10; ntries++) {
1364 if ((error = run_read(sc, RT2860_BBP_CSR_CFG, &tmp)) != 0)
1366 if (!(tmp & RT2860_BBP_CSR_KICK))
1372 tmp = RT2860_BBP_CSR_KICK | reg << 8 | val;
1373 return (run_write(sc, RT2860_BBP_CSR_CFG, tmp));
1377 * Send a command to the 8051 microcontroller unit.
1380 run_mcu_cmd(struct run_softc *sc, uint8_t cmd, uint16_t arg)
1385 for (ntries = 0; ntries < 100; ntries++) {
1386 if ((error = run_read(sc, RT2860_H2M_MAILBOX, &tmp)) != 0)
1388 if (!(tmp & RT2860_H2M_BUSY))
1394 tmp = RT2860_H2M_BUSY | RT2860_TOKEN_NO_INTR << 16 | arg;
1395 if ((error = run_write(sc, RT2860_H2M_MAILBOX, tmp)) == 0)
1396 error = run_write(sc, RT2860_HOST_CMD, cmd);
1401 * Add `delta' (signed) to each 4-bit sub-word of a 32-bit word.
1402 * Used to adjust per-rate Tx power registers.
1404 static __inline uint32_t
1405 b4inc(uint32_t b32, int8_t delta)
1409 for (i = 0; i < 8; i++) {
1416 b32 = b32 >> 4 | b4 << 28;
1425 case RT2860_RF_2820: return "RT2820";
1426 case RT2860_RF_2850: return "RT2850";
1427 case RT2860_RF_2720: return "RT2720";
1428 case RT2860_RF_2750: return "RT2750";
1429 case RT3070_RF_3020: return "RT3020";
1430 case RT3070_RF_2020: return "RT2020";
1431 case RT3070_RF_3021: return "RT3021";
1432 case RT3070_RF_3022: return "RT3022";
1433 case RT3070_RF_3052: return "RT3052";
1439 run_read_eeprom(struct run_softc *sc)
1441 int8_t delta_2ghz, delta_5ghz;
1446 /* check whether the ROM is eFUSE ROM or EEPROM */
1447 sc->sc_srom_read = run_eeprom_read_2;
1448 if (sc->mac_ver >= 0x3070) {
1449 run_read(sc, RT3070_EFUSE_CTRL, &tmp);
1450 DPRINTF("EFUSE_CTRL=0x%08x\n", tmp);
1451 if (tmp & RT3070_SEL_EFUSE)
1452 sc->sc_srom_read = run_efuse_read_2;
1455 /* read ROM version */
1456 run_srom_read(sc, RT2860_EEPROM_VERSION, &val);
1457 DPRINTF("EEPROM rev=%d, FAE=%d\n", val & 0xff, val >> 8);
1459 /* read MAC address */
1460 run_srom_read(sc, RT2860_EEPROM_MAC01, &val);
1461 sc->sc_bssid[0] = val & 0xff;
1462 sc->sc_bssid[1] = val >> 8;
1463 run_srom_read(sc, RT2860_EEPROM_MAC23, &val);
1464 sc->sc_bssid[2] = val & 0xff;
1465 sc->sc_bssid[3] = val >> 8;
1466 run_srom_read(sc, RT2860_EEPROM_MAC45, &val);
1467 sc->sc_bssid[4] = val & 0xff;
1468 sc->sc_bssid[5] = val >> 8;
1470 /* read vender BBP settings */
1471 for (i = 0; i < 10; i++) {
1472 run_srom_read(sc, RT2860_EEPROM_BBP_BASE + i, &val);
1473 sc->bbp[i].val = val & 0xff;
1474 sc->bbp[i].reg = val >> 8;
1475 DPRINTF("BBP%d=0x%02x\n", sc->bbp[i].reg, sc->bbp[i].val);
1477 if (sc->mac_ver >= 0x3071) {
1478 /* read vendor RF settings */
1479 for (i = 0; i < 10; i++) {
1480 run_srom_read(sc, RT3071_EEPROM_RF_BASE + i, &val);
1481 sc->rf[i].val = val & 0xff;
1482 sc->rf[i].reg = val >> 8;
1483 DPRINTF("RF%d=0x%02x\n", sc->rf[i].reg,
1488 /* read RF frequency offset from EEPROM */
1489 run_srom_read(sc, RT2860_EEPROM_FREQ_LEDS, &val);
1490 sc->freq = ((val & 0xff) != 0xff) ? val & 0xff : 0;
1491 DPRINTF("EEPROM freq offset %d\n", sc->freq & 0xff);
1493 if (val >> 8 != 0xff) {
1494 /* read LEDs operating mode */
1495 sc->leds = val >> 8;
1496 run_srom_read(sc, RT2860_EEPROM_LED1, &sc->led[0]);
1497 run_srom_read(sc, RT2860_EEPROM_LED2, &sc->led[1]);
1498 run_srom_read(sc, RT2860_EEPROM_LED3, &sc->led[2]);
1500 /* broken EEPROM, use default settings */
1502 sc->led[0] = 0x5555;
1503 sc->led[1] = 0x2221;
1504 sc->led[2] = 0x5627; /* differs from RT2860 */
1506 DPRINTF("EEPROM LED mode=0x%02x, LEDs=0x%04x/0x%04x/0x%04x\n",
1507 sc->leds, sc->led[0], sc->led[1], sc->led[2]);
1509 /* read RF information */
1510 run_srom_read(sc, RT2860_EEPROM_ANTENNA, &val);
1511 if (val == 0xffff) {
1512 DPRINTF("invalid EEPROM antenna info, using default\n");
1513 if (sc->mac_ver == 0x3572) {
1514 /* default to RF3052 2T2R */
1515 sc->rf_rev = RT3070_RF_3052;
1518 } else if (sc->mac_ver >= 0x3070) {
1519 /* default to RF3020 1T1R */
1520 sc->rf_rev = RT3070_RF_3020;
1524 /* default to RF2820 1T2R */
1525 sc->rf_rev = RT2860_RF_2820;
1530 sc->rf_rev = (val >> 8) & 0xf;
1531 sc->ntxchains = (val >> 4) & 0xf;
1532 sc->nrxchains = val & 0xf;
1534 DPRINTF("EEPROM RF rev=0x%02x chains=%dT%dR\n",
1535 sc->rf_rev, sc->ntxchains, sc->nrxchains);
1537 /* check if RF supports automatic Tx access gain control */
1538 run_srom_read(sc, RT2860_EEPROM_CONFIG, &val);
1539 DPRINTF("EEPROM CFG 0x%04x\n", val);
1540 /* check if driver should patch the DAC issue */
1541 if ((val >> 8) != 0xff)
1542 sc->patch_dac = (val >> 15) & 1;
1543 if ((val & 0xff) != 0xff) {
1544 sc->ext_5ghz_lna = (val >> 3) & 1;
1545 sc->ext_2ghz_lna = (val >> 2) & 1;
1546 /* check if RF supports automatic Tx access gain control */
1547 sc->calib_2ghz = sc->calib_5ghz = (val >> 1) & 1;
1548 /* check if we have a hardware radio switch */
1549 sc->rfswitch = val & 1;
1552 /* read power settings for 2GHz channels */
1553 for (i = 0; i < 14; i += 2) {
1554 run_srom_read(sc, RT2860_EEPROM_PWR2GHZ_BASE1 + i / 2, &val);
1555 sc->txpow1[i + 0] = (int8_t)(val & 0xff);
1556 sc->txpow1[i + 1] = (int8_t)(val >> 8);
1558 run_srom_read(sc, RT2860_EEPROM_PWR2GHZ_BASE2 + i / 2, &val);
1559 sc->txpow2[i + 0] = (int8_t)(val & 0xff);
1560 sc->txpow2[i + 1] = (int8_t)(val >> 8);
1562 /* fix broken Tx power entries */
1563 for (i = 0; i < 14; i++) {
1564 if (sc->txpow1[i] < 0 || sc->txpow1[i] > 31)
1566 if (sc->txpow2[i] < 0 || sc->txpow2[i] > 31)
1568 DPRINTF("chan %d: power1=%d, power2=%d\n",
1569 rt2860_rf2850[i].chan, sc->txpow1[i], sc->txpow2[i]);
1571 /* read power settings for 5GHz channels */
1572 for (i = 0; i < 40; i += 2) {
1573 run_srom_read(sc, RT2860_EEPROM_PWR5GHZ_BASE1 + i / 2, &val);
1574 sc->txpow1[i + 14] = (int8_t)(val & 0xff);
1575 sc->txpow1[i + 15] = (int8_t)(val >> 8);
1577 run_srom_read(sc, RT2860_EEPROM_PWR5GHZ_BASE2 + i / 2, &val);
1578 sc->txpow2[i + 14] = (int8_t)(val & 0xff);
1579 sc->txpow2[i + 15] = (int8_t)(val >> 8);
1581 /* fix broken Tx power entries */
1582 for (i = 0; i < 40; i++) {
1583 if (sc->txpow1[14 + i] < -7 || sc->txpow1[14 + i] > 15)
1584 sc->txpow1[14 + i] = 5;
1585 if (sc->txpow2[14 + i] < -7 || sc->txpow2[14 + i] > 15)
1586 sc->txpow2[14 + i] = 5;
1587 DPRINTF("chan %d: power1=%d, power2=%d\n",
1588 rt2860_rf2850[14 + i].chan, sc->txpow1[14 + i],
1589 sc->txpow2[14 + i]);
1592 /* read Tx power compensation for each Tx rate */
1593 run_srom_read(sc, RT2860_EEPROM_DELTAPWR, &val);
1594 delta_2ghz = delta_5ghz = 0;
1595 if ((val & 0xff) != 0xff && (val & 0x80)) {
1596 delta_2ghz = val & 0xf;
1597 if (!(val & 0x40)) /* negative number */
1598 delta_2ghz = -delta_2ghz;
1601 if ((val & 0xff) != 0xff && (val & 0x80)) {
1602 delta_5ghz = val & 0xf;
1603 if (!(val & 0x40)) /* negative number */
1604 delta_5ghz = -delta_5ghz;
1606 DPRINTF("power compensation=%d (2GHz), %d (5GHz)\n",
1607 delta_2ghz, delta_5ghz);
1609 for (ridx = 0; ridx < 5; ridx++) {
1612 run_srom_read(sc, RT2860_EEPROM_RPWR + ridx * 2, &val);
1614 run_srom_read(sc, RT2860_EEPROM_RPWR + ridx * 2 + 1, &val);
1615 reg |= (uint32_t)val << 16;
1617 sc->txpow20mhz[ridx] = reg;
1618 sc->txpow40mhz_2ghz[ridx] = b4inc(reg, delta_2ghz);
1619 sc->txpow40mhz_5ghz[ridx] = b4inc(reg, delta_5ghz);
1621 DPRINTF("ridx %d: power 20MHz=0x%08x, 40MHz/2GHz=0x%08x, "
1622 "40MHz/5GHz=0x%08x\n", ridx, sc->txpow20mhz[ridx],
1623 sc->txpow40mhz_2ghz[ridx], sc->txpow40mhz_5ghz[ridx]);
1626 /* read RSSI offsets and LNA gains from EEPROM */
1627 run_srom_read(sc, RT2860_EEPROM_RSSI1_2GHZ, &val);
1628 sc->rssi_2ghz[0] = val & 0xff; /* Ant A */
1629 sc->rssi_2ghz[1] = val >> 8; /* Ant B */
1630 run_srom_read(sc, RT2860_EEPROM_RSSI2_2GHZ, &val);
1631 if (sc->mac_ver >= 0x3070) {
1633 * On RT3070 chips (limited to 2 Rx chains), this ROM
1634 * field contains the Tx mixer gain for the 2GHz band.
1636 if ((val & 0xff) != 0xff)
1637 sc->txmixgain_2ghz = val & 0x7;
1638 DPRINTF("tx mixer gain=%u (2GHz)\n", sc->txmixgain_2ghz);
1640 sc->rssi_2ghz[2] = val & 0xff; /* Ant C */
1641 sc->lna[2] = val >> 8; /* channel group 2 */
1643 run_srom_read(sc, RT2860_EEPROM_RSSI1_5GHZ, &val);
1644 sc->rssi_5ghz[0] = val & 0xff; /* Ant A */
1645 sc->rssi_5ghz[1] = val >> 8; /* Ant B */
1646 run_srom_read(sc, RT2860_EEPROM_RSSI2_5GHZ, &val);
1647 if (sc->mac_ver == 0x3572) {
1649 * On RT3572 chips (limited to 2 Rx chains), this ROM
1650 * field contains the Tx mixer gain for the 5GHz band.
1652 if ((val & 0xff) != 0xff)
1653 sc->txmixgain_5ghz = val & 0x7;
1654 DPRINTF("tx mixer gain=%u (5GHz)\n", sc->txmixgain_5ghz);
1656 sc->rssi_5ghz[2] = val & 0xff; /* Ant C */
1657 sc->lna[3] = val >> 8; /* channel group 3 */
1659 run_srom_read(sc, RT2860_EEPROM_LNA, &val);
1660 sc->lna[0] = val & 0xff; /* channel group 0 */
1661 sc->lna[1] = val >> 8; /* channel group 1 */
1663 /* fix broken 5GHz LNA entries */
1664 if (sc->lna[2] == 0 || sc->lna[2] == 0xff) {
1665 DPRINTF("invalid LNA for channel group %d\n", 2);
1666 sc->lna[2] = sc->lna[1];
1668 if (sc->lna[3] == 0 || sc->lna[3] == 0xff) {
1669 DPRINTF("invalid LNA for channel group %d\n", 3);
1670 sc->lna[3] = sc->lna[1];
1673 /* fix broken RSSI offset entries */
1674 for (ant = 0; ant < 3; ant++) {
1675 if (sc->rssi_2ghz[ant] < -10 || sc->rssi_2ghz[ant] > 10) {
1676 DPRINTF("invalid RSSI%d offset: %d (2GHz)\n",
1677 ant + 1, sc->rssi_2ghz[ant]);
1678 sc->rssi_2ghz[ant] = 0;
1680 if (sc->rssi_5ghz[ant] < -10 || sc->rssi_5ghz[ant] > 10) {
1681 DPRINTF("invalid RSSI%d offset: %d (5GHz)\n",
1682 ant + 1, sc->rssi_5ghz[ant]);
1683 sc->rssi_5ghz[ant] = 0;
1689 static struct ieee80211_node *
1690 run_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
1692 return malloc(sizeof (struct run_node), M_DEVBUF, M_NOWAIT | M_ZERO);
1696 run_media_change(struct ifnet *ifp)
1698 struct ieee80211vap *vap = ifp->if_softc;
1699 struct ieee80211com *ic = vap->iv_ic;
1700 const struct ieee80211_txparam *tp;
1701 struct run_softc *sc = ic->ic_ifp->if_softc;
1707 error = ieee80211_media_change(ifp);
1708 if (error != ENETRESET) {
1713 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1714 if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) {
1715 struct ieee80211_node *ni;
1716 struct run_node *rn;
1718 rate = ic->ic_sup_rates[ic->ic_curmode].
1719 rs_rates[tp->ucastrate] & IEEE80211_RATE_VAL;
1720 for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
1721 if (rt2860_rates[ridx].rate == rate)
1723 ni = ieee80211_ref_node(vap->iv_bss);
1724 rn = (struct run_node *)ni;
1725 rn->fix_ridx = ridx;
1726 DPRINTF("rate=%d, fix_ridx=%d\n", rate, rn->fix_ridx);
1727 ieee80211_free_node(ni);
1731 if ((ifp->if_flags & IFF_UP) &&
1732 (ifp->if_drv_flags & IFF_DRV_RUNNING)){
1733 run_init_locked(sc);
1743 run_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1745 const struct ieee80211_txparam *tp;
1746 struct ieee80211com *ic = vap->iv_ic;
1747 struct run_softc *sc = ic->ic_ifp->if_softc;
1748 struct run_vap *rvp = RUN_VAP(vap);
1749 enum ieee80211_state ostate;
1753 uint8_t restart_ratectl = 0;
1754 uint8_t bid = 1 << rvp->rvp_id;
1756 ostate = vap->iv_state;
1757 DPRINTF("%s -> %s\n",
1758 ieee80211_state_name[ostate],
1759 ieee80211_state_name[nstate]);
1761 IEEE80211_UNLOCK(ic);
1764 ratectl = sc->ratectl_run; /* remember current state */
1765 sc->ratectl_run = RUN_RATECTL_OFF;
1766 usb_callout_stop(&sc->ratectl_ch);
1768 if (ostate == IEEE80211_S_RUN) {
1769 /* turn link LED off */
1770 run_set_leds(sc, RT2860_LED_RADIO);
1774 case IEEE80211_S_INIT:
1775 restart_ratectl = 1;
1777 if (ostate != IEEE80211_S_RUN)
1781 sc->runbmap &= ~bid;
1783 /* abort TSF synchronization if there is no vap running */
1784 if (--sc->running == 0) {
1785 run_read(sc, RT2860_BCN_TIME_CFG, &tmp);
1786 run_write(sc, RT2860_BCN_TIME_CFG,
1787 tmp & ~(RT2860_BCN_TX_EN | RT2860_TSF_TIMER_EN |
1788 RT2860_TBTT_TIMER_EN));
1792 case IEEE80211_S_RUN:
1793 if (!(sc->runbmap & bid)) {
1795 restart_ratectl = 1;
1799 m_freem(rvp->beacon_mbuf);
1800 rvp->beacon_mbuf = NULL;
1802 switch (vap->iv_opmode) {
1803 case IEEE80211_M_HOSTAP:
1804 case IEEE80211_M_MBSS:
1805 sc->ap_running |= bid;
1806 ic->ic_opmode = vap->iv_opmode;
1807 run_update_beacon_cb(vap);
1809 case IEEE80211_M_IBSS:
1810 sc->adhoc_running |= bid;
1811 if (!sc->ap_running)
1812 ic->ic_opmode = vap->iv_opmode;
1813 run_update_beacon_cb(vap);
1815 case IEEE80211_M_STA:
1816 sc->sta_running |= bid;
1817 if (!sc->ap_running && !sc->adhoc_running)
1818 ic->ic_opmode = vap->iv_opmode;
1820 /* read statistic counters (clear on read) */
1821 run_read_region_1(sc, RT2860_TX_STA_CNT0,
1822 (uint8_t *)sta, sizeof sta);
1826 ic->ic_opmode = vap->iv_opmode;
1830 if (vap->iv_opmode != IEEE80211_M_MONITOR) {
1831 struct ieee80211_node *ni;
1833 run_updateslot(ic->ic_ifp);
1835 run_set_txpreamble(sc);
1836 run_set_basicrates(sc);
1837 ni = ieee80211_ref_node(vap->iv_bss);
1838 IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid);
1839 run_set_bssid(sc, ni->ni_bssid);
1840 ieee80211_free_node(ni);
1841 run_enable_tsf_sync(sc);
1843 /* enable automatic rate adaptation */
1844 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1845 if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
1849 /* turn link LED on */
1850 run_set_leds(sc, RT2860_LED_RADIO |
1851 (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan) ?
1852 RT2860_LED_LINK_2GHZ : RT2860_LED_LINK_5GHZ));
1856 DPRINTFN(6, "undefined case\n");
1860 /* restart amrr for running VAPs */
1861 if ((sc->ratectl_run = ratectl) && restart_ratectl)
1862 usb_callout_reset(&sc->ratectl_ch, hz, run_ratectl_to, sc);
1867 return(rvp->newstate(vap, nstate, arg));
1872 run_wme_update_cb(void *arg)
1874 struct ieee80211com *ic = arg;
1875 struct run_softc *sc = ic->ic_ifp->if_softc;
1876 struct ieee80211_wme_state *wmesp = &ic->ic_wme;
1879 RUN_LOCK_ASSERT(sc, MA_OWNED);
1881 /* update MAC TX configuration registers */
1882 for (aci = 0; aci < WME_NUM_AC; aci++) {
1883 error = run_write(sc, RT2860_EDCA_AC_CFG(aci),
1884 wmesp->wme_params[aci].wmep_logcwmax << 16 |
1885 wmesp->wme_params[aci].wmep_logcwmin << 12 |
1886 wmesp->wme_params[aci].wmep_aifsn << 8 |
1887 wmesp->wme_params[aci].wmep_txopLimit);
1888 if (error) goto err;
1891 /* update SCH/DMA registers too */
1892 error = run_write(sc, RT2860_WMM_AIFSN_CFG,
1893 wmesp->wme_params[WME_AC_VO].wmep_aifsn << 12 |
1894 wmesp->wme_params[WME_AC_VI].wmep_aifsn << 8 |
1895 wmesp->wme_params[WME_AC_BK].wmep_aifsn << 4 |
1896 wmesp->wme_params[WME_AC_BE].wmep_aifsn);
1897 if (error) goto err;
1898 error = run_write(sc, RT2860_WMM_CWMIN_CFG,
1899 wmesp->wme_params[WME_AC_VO].wmep_logcwmin << 12 |
1900 wmesp->wme_params[WME_AC_VI].wmep_logcwmin << 8 |
1901 wmesp->wme_params[WME_AC_BK].wmep_logcwmin << 4 |
1902 wmesp->wme_params[WME_AC_BE].wmep_logcwmin);
1903 if (error) goto err;
1904 error = run_write(sc, RT2860_WMM_CWMAX_CFG,
1905 wmesp->wme_params[WME_AC_VO].wmep_logcwmax << 12 |
1906 wmesp->wme_params[WME_AC_VI].wmep_logcwmax << 8 |
1907 wmesp->wme_params[WME_AC_BK].wmep_logcwmax << 4 |
1908 wmesp->wme_params[WME_AC_BE].wmep_logcwmax);
1909 if (error) goto err;
1910 error = run_write(sc, RT2860_WMM_TXOP0_CFG,
1911 wmesp->wme_params[WME_AC_BK].wmep_txopLimit << 16 |
1912 wmesp->wme_params[WME_AC_BE].wmep_txopLimit);
1913 if (error) goto err;
1914 error = run_write(sc, RT2860_WMM_TXOP1_CFG,
1915 wmesp->wme_params[WME_AC_VO].wmep_txopLimit << 16 |
1916 wmesp->wme_params[WME_AC_VI].wmep_txopLimit);
1920 DPRINTF("WME update failed\n");
1926 run_wme_update(struct ieee80211com *ic)
1928 struct run_softc *sc = ic->ic_ifp->if_softc;
1930 /* sometime called wothout lock */
1931 if (mtx_owned(&ic->ic_comlock.mtx)) {
1932 uint32_t i = RUN_CMDQ_GET(&sc->cmdq_store);
1933 DPRINTF("cmdq_store=%d\n", i);
1934 sc->cmdq[i].func = run_wme_update_cb;
1935 sc->cmdq[i].arg0 = ic;
1936 ieee80211_runtask(ic, &sc->cmdq_task);
1941 run_wme_update_cb(ic);
1944 /* return whatever, upper layer desn't care anyway */
1949 run_key_update_begin(struct ieee80211vap *vap)
1952 * To avoid out-of-order events, both run_key_set() and
1953 * _delete() are deferred and handled by run_cmdq_cb().
1954 * So, there is nothing we need to do here.
1959 run_key_update_end(struct ieee80211vap *vap)
1965 run_key_set_cb(void *arg)
1967 struct run_cmdq *cmdq = arg;
1968 struct ieee80211vap *vap = cmdq->arg1;
1969 struct ieee80211_key *k = cmdq->k;
1970 struct ieee80211com *ic = vap->iv_ic;
1971 struct run_softc *sc = ic->ic_ifp->if_softc;
1972 struct ieee80211_node *ni;
1974 uint16_t base, associd;
1975 uint8_t mode, wcid, iv[8];
1977 RUN_LOCK_ASSERT(sc, MA_OWNED);
1979 if (vap->iv_opmode == IEEE80211_M_HOSTAP)
1980 ni = ieee80211_find_vap_node(&ic->ic_sta, vap, cmdq->mac);
1983 associd = (ni != NULL) ? ni->ni_associd : 0;
1985 /* map net80211 cipher to RT2860 security mode */
1986 switch (k->wk_cipher->ic_cipher) {
1987 case IEEE80211_CIPHER_WEP:
1988 if(k->wk_keylen < 8)
1989 mode = RT2860_MODE_WEP40;
1991 mode = RT2860_MODE_WEP104;
1993 case IEEE80211_CIPHER_TKIP:
1994 mode = RT2860_MODE_TKIP;
1996 case IEEE80211_CIPHER_AES_CCM:
1997 mode = RT2860_MODE_AES_CCMP;
2000 DPRINTF("undefined case\n");
2004 DPRINTFN(1, "associd=%x, keyix=%d, mode=%x, type=%s, tx=%s, rx=%s\n",
2005 associd, k->wk_keyix, mode,
2006 (k->wk_flags & IEEE80211_KEY_GROUP) ? "group" : "pairwise",
2007 (k->wk_flags & IEEE80211_KEY_XMIT) ? "on" : "off",
2008 (k->wk_flags & IEEE80211_KEY_RECV) ? "on" : "off");
2010 if (k->wk_flags & IEEE80211_KEY_GROUP) {
2011 wcid = 0; /* NB: update WCID0 for group keys */
2012 base = RT2860_SKEY(RUN_VAP(vap)->rvp_id, k->wk_keyix);
2014 wcid = RUN_AID2WCID(associd);
2015 base = RT2860_PKEY(wcid);
2018 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP) {
2019 if(run_write_region_1(sc, base, k->wk_key, 16))
2021 if(run_write_region_1(sc, base + 16, &k->wk_key[16], 8)) /* wk_txmic */
2023 if(run_write_region_1(sc, base + 24, &k->wk_key[24], 8)) /* wk_rxmic */
2026 /* roundup len to 16-bit: XXX fix write_region_1() instead */
2027 if(run_write_region_1(sc, base, k->wk_key, (k->wk_keylen + 1) & ~1))
2031 if (!(k->wk_flags & IEEE80211_KEY_GROUP) ||
2032 (k->wk_flags & (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV))) {
2033 /* set initial packet number in IV+EIV */
2034 if (k->wk_cipher == IEEE80211_CIPHER_WEP) {
2035 memset(iv, 0, sizeof iv);
2036 iv[3] = vap->iv_def_txkey << 6;
2038 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP) {
2039 iv[0] = k->wk_keytsc >> 8;
2040 iv[1] = (iv[0] | 0x20) & 0x7f;
2041 iv[2] = k->wk_keytsc;
2043 iv[0] = k->wk_keytsc;
2044 iv[1] = k->wk_keytsc >> 8;
2047 iv[3] = k->wk_keyix << 6 | IEEE80211_WEP_EXTIV;
2048 iv[4] = k->wk_keytsc >> 16;
2049 iv[5] = k->wk_keytsc >> 24;
2050 iv[6] = k->wk_keytsc >> 32;
2051 iv[7] = k->wk_keytsc >> 40;
2053 if (run_write_region_1(sc, RT2860_IVEIV(wcid), iv, 8))
2057 if (k->wk_flags & IEEE80211_KEY_GROUP) {
2058 /* install group key */
2059 if (run_read(sc, RT2860_SKEY_MODE_0_7, &attr))
2061 attr &= ~(0xf << (k->wk_keyix * 4));
2062 attr |= mode << (k->wk_keyix * 4);
2063 if (run_write(sc, RT2860_SKEY_MODE_0_7, attr))
2066 /* install pairwise key */
2067 if (run_read(sc, RT2860_WCID_ATTR(wcid), &attr))
2069 attr = (attr & ~0xf) | (mode << 1) | RT2860_RX_PKEY_EN;
2070 if (run_write(sc, RT2860_WCID_ATTR(wcid), attr))
2074 /* TODO create a pass-thru key entry? */
2076 /* need wcid to delete the right key later */
2081 * Don't have to be deferred, but in order to keep order of
2082 * execution, i.e. with run_key_delete(), defer this and let
2083 * run_cmdq_cb() maintain the order.
2088 run_key_set(struct ieee80211vap *vap, struct ieee80211_key *k,
2089 const uint8_t mac[IEEE80211_ADDR_LEN])
2091 struct ieee80211com *ic = vap->iv_ic;
2092 struct run_softc *sc = ic->ic_ifp->if_softc;
2095 i = RUN_CMDQ_GET(&sc->cmdq_store);
2096 DPRINTF("cmdq_store=%d\n", i);
2097 sc->cmdq[i].func = run_key_set_cb;
2098 sc->cmdq[i].arg0 = NULL;
2099 sc->cmdq[i].arg1 = vap;
2101 IEEE80211_ADDR_COPY(sc->cmdq[i].mac, mac);
2102 ieee80211_runtask(ic, &sc->cmdq_task);
2105 * To make sure key will be set when hostapd
2106 * calls iv_key_set() before if_init().
2108 if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
2110 sc->cmdq_key_set = RUN_CMDQ_GO;
2118 * If wlan is destroyed without being brought down i.e. without
2119 * wlan down or wpa_cli terminate, this function is called after
2120 * vap is gone. Don't refer it.
2123 run_key_delete_cb(void *arg)
2125 struct run_cmdq *cmdq = arg;
2126 struct run_softc *sc = cmdq->arg1;
2127 struct ieee80211_key *k = &cmdq->key;
2131 RUN_LOCK_ASSERT(sc, MA_OWNED);
2133 if (k->wk_flags & IEEE80211_KEY_GROUP) {
2134 /* remove group key */
2135 DPRINTF("removing group key\n");
2136 run_read(sc, RT2860_SKEY_MODE_0_7, &attr);
2137 attr &= ~(0xf << (k->wk_keyix * 4));
2138 run_write(sc, RT2860_SKEY_MODE_0_7, attr);
2140 /* remove pairwise key */
2141 DPRINTF("removing key for wcid %x\n", k->wk_pad);
2142 /* matching wcid was written to wk_pad in run_key_set() */
2144 run_read(sc, RT2860_WCID_ATTR(wcid), &attr);
2146 run_write(sc, RT2860_WCID_ATTR(wcid), attr);
2147 run_set_region_4(sc, RT2860_WCID_ENTRY(wcid), 0, 8);
2157 run_key_delete(struct ieee80211vap *vap, struct ieee80211_key *k)
2159 struct ieee80211com *ic = vap->iv_ic;
2160 struct run_softc *sc = ic->ic_ifp->if_softc;
2161 struct ieee80211_key *k0;
2165 * When called back, key might be gone. So, make a copy
2166 * of some values need to delete keys before deferring.
2167 * But, because of LOR with node lock, cannot use lock here.
2168 * So, use atomic instead.
2170 i = RUN_CMDQ_GET(&sc->cmdq_store);
2171 DPRINTF("cmdq_store=%d\n", i);
2172 sc->cmdq[i].func = run_key_delete_cb;
2173 sc->cmdq[i].arg0 = NULL;
2174 sc->cmdq[i].arg1 = sc;
2175 k0 = &sc->cmdq[i].key;
2176 k0->wk_flags = k->wk_flags;
2177 k0->wk_keyix = k->wk_keyix;
2178 /* matching wcid was written to wk_pad in run_key_set() */
2179 k0->wk_pad = k->wk_pad;
2180 ieee80211_runtask(ic, &sc->cmdq_task);
2181 return (1); /* return fake success */
2186 run_ratectl_to(void *arg)
2188 struct run_softc *sc = arg;
2190 /* do it in a process context, so it can go sleep */
2191 ieee80211_runtask(sc->sc_ifp->if_l2com, &sc->ratectl_task);
2192 /* next timeout will be rescheduled in the callback task */
2197 run_ratectl_cb(void *arg, int pending)
2199 struct run_softc *sc = arg;
2200 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
2201 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2206 if (sc->rvp_cnt <= 1 && vap->iv_opmode == IEEE80211_M_STA)
2207 run_iter_func(sc, vap->iv_bss);
2210 * run_reset_livelock() doesn't do anything with AMRR,
2211 * but Ralink wants us to call it every 1 sec. So, we
2212 * piggyback here rather than creating another callout.
2213 * Livelock may occur only in HOSTAP or IBSS mode
2214 * (when h/w is sending beacons).
2217 run_reset_livelock(sc);
2218 /* just in case, there are some stats to drain */
2221 ieee80211_iterate_nodes(&ic->ic_sta, run_iter_func, sc);
2224 if(sc->ratectl_run != RUN_RATECTL_OFF)
2225 usb_callout_reset(&sc->ratectl_ch, hz, run_ratectl_to, sc);
2229 run_drain_fifo(void *arg)
2231 struct run_softc *sc = arg;
2232 struct ifnet *ifp = sc->sc_ifp;
2234 uint16_t (*wstat)[3];
2235 uint8_t wcid, mcs, pid;
2238 RUN_LOCK_ASSERT(sc, MA_OWNED);
2241 /* drain Tx status FIFO (maxsize = 16) */
2242 run_read(sc, RT2860_TX_STAT_FIFO, &stat);
2243 DPRINTFN(4, "tx stat 0x%08x\n", stat);
2244 if (!(stat & RT2860_TXQ_VLD))
2247 wcid = (stat >> RT2860_TXQ_WCID_SHIFT) & 0xff;
2249 /* if no ACK was requested, no feedback is available */
2250 if (!(stat & RT2860_TXQ_ACKREQ) || wcid > RT2870_WCID_MAX ||
2255 * Even though each stat is Tx-complete-status like format,
2256 * the device can poll stats. Because there is no guarantee
2257 * that the referring node is still around when read the stats.
2258 * So that, if we use ieee80211_ratectl_tx_update(), we will
2259 * have hard time not to refer already freed node.
2261 * To eliminate such page faults, we poll stats in softc.
2262 * Then, update the rates later with ieee80211_ratectl_tx_update().
2264 wstat = &(sc->wcid_stats[wcid]);
2265 (*wstat)[RUN_TXCNT]++;
2266 if (stat & RT2860_TXQ_OK)
2267 (*wstat)[RUN_SUCCESS]++;
2271 * Check if there were retries, ie if the Tx success rate is
2272 * different from the requested rate. Note that it works only
2273 * because we do not allow rate fallback from OFDM to CCK.
2275 mcs = (stat >> RT2860_TXQ_MCS_SHIFT) & 0x7f;
2276 pid = (stat >> RT2860_TXQ_PID_SHIFT) & 0xf;
2277 if ((retry = pid -1 - mcs) > 0) {
2278 (*wstat)[RUN_TXCNT] += retry;
2279 (*wstat)[RUN_RETRY] += retry;
2282 DPRINTFN(3, "count=%d\n", sc->fifo_cnt);
2288 run_iter_func(void *arg, struct ieee80211_node *ni)
2290 struct run_softc *sc = arg;
2291 struct ieee80211vap *vap = ni->ni_vap;
2292 struct ieee80211com *ic = ni->ni_ic;
2293 struct ifnet *ifp = ic->ic_ifp;
2294 struct run_node *rn = (void *)ni;
2295 union run_stats sta[2];
2296 uint16_t (*wstat)[3];
2297 int txcnt, success, retrycnt, error;
2301 if (sc->rvp_cnt <= 1 && (vap->iv_opmode == IEEE80211_M_IBSS ||
2302 vap->iv_opmode == IEEE80211_M_STA)) {
2303 /* read statistic counters (clear on read) and update AMRR state */
2304 error = run_read_region_1(sc, RT2860_TX_STA_CNT0, (uint8_t *)sta,
2309 /* count failed TX as errors */
2310 ifp->if_oerrors += le16toh(sta[0].error.fail);
2312 retrycnt = le16toh(sta[1].tx.retry);
2313 success = le16toh(sta[1].tx.success);
2314 txcnt = retrycnt + success + le16toh(sta[0].error.fail);
2316 DPRINTFN(3, "retrycnt=%d success=%d failcnt=%d\n",
2317 retrycnt, success, le16toh(sta[0].error.fail));
2319 wstat = &(sc->wcid_stats[RUN_AID2WCID(ni->ni_associd)]);
2321 if (wstat == &(sc->wcid_stats[0]) ||
2322 wstat > &(sc->wcid_stats[RT2870_WCID_MAX]))
2325 txcnt = (*wstat)[RUN_TXCNT];
2326 success = (*wstat)[RUN_SUCCESS];
2327 retrycnt = (*wstat)[RUN_RETRY];
2328 DPRINTFN(3, "retrycnt=%d txcnt=%d success=%d\n",
2329 retrycnt, txcnt, success);
2331 memset(wstat, 0, sizeof(*wstat));
2334 ieee80211_ratectl_tx_update(vap, ni, &txcnt, &success, &retrycnt);
2335 rn->amrr_ridx = ieee80211_ratectl_rate(ni, NULL, 0);
2340 DPRINTFN(3, "ridx=%d\n", rn->amrr_ridx);
2344 run_newassoc_cb(void *arg)
2346 struct run_cmdq *cmdq = arg;
2347 struct ieee80211_node *ni = cmdq->arg1;
2348 struct run_softc *sc = ni->ni_vap->iv_ic->ic_ifp->if_softc;
2349 uint8_t wcid = cmdq->wcid;
2351 RUN_LOCK_ASSERT(sc, MA_OWNED);
2353 run_write_region_1(sc, RT2860_WCID_ENTRY(wcid),
2354 ni->ni_macaddr, IEEE80211_ADDR_LEN);
2356 memset(&(sc->wcid_stats[wcid]), 0, sizeof(sc->wcid_stats[wcid]));
2360 run_newassoc(struct ieee80211_node *ni, int isnew)
2362 struct run_node *rn = (void *)ni;
2363 struct ieee80211_rateset *rs = &ni->ni_rates;
2364 struct ieee80211vap *vap = ni->ni_vap;
2365 struct ieee80211com *ic = vap->iv_ic;
2366 struct run_softc *sc = ic->ic_ifp->if_softc;
2369 uint8_t wcid = RUN_AID2WCID(ni->ni_associd);
2372 if (wcid > RT2870_WCID_MAX) {
2373 device_printf(sc->sc_dev, "wcid=%d out of range\n", wcid);
2377 /* only interested in true associations */
2378 if (isnew && ni->ni_associd != 0) {
2381 * This function could is called though timeout function.
2384 uint32_t cnt = RUN_CMDQ_GET(&sc->cmdq_store);
2385 DPRINTF("cmdq_store=%d\n", cnt);
2386 sc->cmdq[cnt].func = run_newassoc_cb;
2387 sc->cmdq[cnt].arg0 = NULL;
2388 sc->cmdq[cnt].arg1 = ni;
2389 sc->cmdq[cnt].wcid = wcid;
2390 ieee80211_runtask(ic, &sc->cmdq_task);
2393 DPRINTF("new assoc isnew=%d associd=%x addr=%s\n",
2394 isnew, ni->ni_associd, ether_sprintf(ni->ni_macaddr));
2396 for (i = 0; i < rs->rs_nrates; i++) {
2397 rate = rs->rs_rates[i] & IEEE80211_RATE_VAL;
2398 /* convert 802.11 rate to hardware rate index */
2399 for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
2400 if (rt2860_rates[ridx].rate == rate)
2403 /* determine rate of control response frames */
2404 for (j = i; j >= 0; j--) {
2405 if ((rs->rs_rates[j] & IEEE80211_RATE_BASIC) &&
2406 rt2860_rates[rn->ridx[i]].phy ==
2407 rt2860_rates[rn->ridx[j]].phy)
2411 rn->ctl_ridx[i] = rn->ridx[j];
2413 /* no basic rate found, use mandatory one */
2414 rn->ctl_ridx[i] = rt2860_rates[ridx].ctl_ridx;
2416 DPRINTF("rate=0x%02x ridx=%d ctl_ridx=%d\n",
2417 rs->rs_rates[i], rn->ridx[i], rn->ctl_ridx[i]);
2419 rate = vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)].mgmtrate;
2420 for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
2421 if (rt2860_rates[ridx].rate == rate)
2423 rn->mgt_ridx = ridx;
2424 DPRINTF("rate=%d, mgmt_ridx=%d\n", rate, rn->mgt_ridx);
2426 usb_callout_reset(&sc->ratectl_ch, hz, run_ratectl_to, sc);
2430 * Return the Rx chain with the highest RSSI for a given frame.
2432 static __inline uint8_t
2433 run_maxrssi_chain(struct run_softc *sc, const struct rt2860_rxwi *rxwi)
2435 uint8_t rxchain = 0;
2437 if (sc->nrxchains > 1) {
2438 if (rxwi->rssi[1] > rxwi->rssi[rxchain])
2440 if (sc->nrxchains > 2)
2441 if (rxwi->rssi[2] > rxwi->rssi[rxchain])
2448 run_rx_frame(struct run_softc *sc, struct mbuf *m, uint32_t dmalen)
2450 struct ifnet *ifp = sc->sc_ifp;
2451 struct ieee80211com *ic = ifp->if_l2com;
2452 struct ieee80211_frame *wh;
2453 struct ieee80211_node *ni;
2454 struct rt2870_rxd *rxd;
2455 struct rt2860_rxwi *rxwi;
2461 rxwi = mtod(m, struct rt2860_rxwi *);
2462 len = le16toh(rxwi->len) & 0xfff;
2463 if (__predict_false(len > dmalen)) {
2466 DPRINTF("bad RXWI length %u > %u\n", len, dmalen);
2469 /* Rx descriptor is located at the end */
2470 rxd = (struct rt2870_rxd *)(mtod(m, caddr_t) + dmalen);
2471 flags = le32toh(rxd->flags);
2473 if (__predict_false(flags & (RT2860_RX_CRCERR | RT2860_RX_ICVERR))) {
2476 DPRINTF("%s error.\n", (flags & RT2860_RX_CRCERR)?"CRC":"ICV");
2480 m->m_data += sizeof(struct rt2860_rxwi);
2481 m->m_pkthdr.len = m->m_len -= sizeof(struct rt2860_rxwi);
2483 wh = mtod(m, struct ieee80211_frame *);
2485 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
2486 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
2487 m->m_flags |= M_WEP;
2490 if (flags & RT2860_RX_L2PAD) {
2491 DPRINTFN(8, "received RT2860_RX_L2PAD frame\n");
2495 ni = ieee80211_find_rxnode(ic,
2496 mtod(m, struct ieee80211_frame_min *));
2498 if (__predict_false(flags & RT2860_RX_MICERR)) {
2499 /* report MIC failures to net80211 for TKIP */
2501 ieee80211_notify_michael_failure(ni->ni_vap, wh, rxwi->keyidx);
2504 DPRINTF("MIC error. Someone is lying.\n");
2508 ant = run_maxrssi_chain(sc, rxwi);
2509 rssi = rxwi->rssi[ant];
2510 nf = run_rssi2dbm(sc, rssi, ant);
2512 m->m_pkthdr.rcvif = ifp;
2513 m->m_pkthdr.len = m->m_len = len;
2516 (void)ieee80211_input(ni, m, rssi, nf);
2517 ieee80211_free_node(ni);
2519 (void)ieee80211_input_all(ic, m, rssi, nf);
2522 if (__predict_false(ieee80211_radiotap_active(ic))) {
2523 struct run_rx_radiotap_header *tap = &sc->sc_rxtap;
2526 tap->wr_chan_freq = htole16(ic->ic_bsschan->ic_freq);
2527 tap->wr_chan_flags = htole16(ic->ic_bsschan->ic_flags);
2528 tap->wr_antsignal = rssi;
2529 tap->wr_antenna = ant;
2530 tap->wr_dbm_antsignal = run_rssi2dbm(sc, rssi, ant);
2531 tap->wr_rate = 2; /* in case it can't be found below */
2532 phy = le16toh(rxwi->phy);
2533 switch (phy & RT2860_PHY_MODE) {
2534 case RT2860_PHY_CCK:
2535 switch ((phy & RT2860_PHY_MCS) & ~RT2860_PHY_SHPRE) {
2536 case 0: tap->wr_rate = 2; break;
2537 case 1: tap->wr_rate = 4; break;
2538 case 2: tap->wr_rate = 11; break;
2539 case 3: tap->wr_rate = 22; break;
2541 if (phy & RT2860_PHY_SHPRE)
2542 tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
2544 case RT2860_PHY_OFDM:
2545 switch (phy & RT2860_PHY_MCS) {
2546 case 0: tap->wr_rate = 12; break;
2547 case 1: tap->wr_rate = 18; break;
2548 case 2: tap->wr_rate = 24; break;
2549 case 3: tap->wr_rate = 36; break;
2550 case 4: tap->wr_rate = 48; break;
2551 case 5: tap->wr_rate = 72; break;
2552 case 6: tap->wr_rate = 96; break;
2553 case 7: tap->wr_rate = 108; break;
2561 run_bulk_rx_callback(struct usb_xfer *xfer, usb_error_t error)
2563 struct run_softc *sc = usbd_xfer_softc(xfer);
2564 struct ifnet *ifp = sc->sc_ifp;
2565 struct mbuf *m = NULL;
2570 usbd_xfer_status(xfer, &xferlen, NULL, NULL, NULL);
2572 switch (USB_GET_STATE(xfer)) {
2573 case USB_ST_TRANSFERRED:
2575 DPRINTFN(15, "rx done, actlen=%d\n", xferlen);
2577 if (xferlen < sizeof (uint32_t) +
2578 sizeof (struct rt2860_rxwi) + sizeof (struct rt2870_rxd)) {
2579 DPRINTF("xfer too short %d\n", xferlen);
2589 if (sc->rx_m == NULL) {
2590 sc->rx_m = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR,
2591 MJUMPAGESIZE /* xfer can be bigger than MCLBYTES */);
2593 if (sc->rx_m == NULL) {
2594 DPRINTF("could not allocate mbuf - idle with stall\n");
2596 usbd_xfer_set_stall(xfer);
2597 usbd_xfer_set_frames(xfer, 0);
2600 * Directly loading a mbuf cluster into DMA to
2601 * save some data copying. This works because
2602 * there is only one cluster.
2604 usbd_xfer_set_frame_data(xfer, 0,
2605 mtod(sc->rx_m, caddr_t), RUN_MAX_RXSZ);
2606 usbd_xfer_set_frames(xfer, 1);
2608 usbd_transfer_submit(xfer);
2611 default: /* Error */
2612 if (error != USB_ERR_CANCELLED) {
2613 /* try to clear stall first */
2614 usbd_xfer_set_stall(xfer);
2616 if (error == USB_ERR_TIMEOUT)
2617 device_printf(sc->sc_dev, "device timeout\n");
2623 if (sc->rx_m != NULL) {
2633 /* inputting all the frames must be last */
2637 m->m_pkthdr.len = m->m_len = xferlen;
2639 /* HW can aggregate multiple 802.11 frames in a single USB xfer */
2641 dmalen = le32toh(*mtod(m, uint32_t *)) & 0xffff;
2643 if ((dmalen == 0) || ((dmalen & 3) != 0)) {
2644 DPRINTF("bad DMA length %u\n", dmalen);
2647 if ((dmalen + 8) > xferlen) {
2648 DPRINTF("bad DMA length %u > %d\n",
2649 dmalen + 8, xferlen);
2653 /* If it is the last one or a single frame, we won't copy. */
2654 if ((xferlen -= dmalen + 8) <= 8) {
2655 /* trim 32-bit DMA-len header */
2657 m->m_pkthdr.len = m->m_len -= 4;
2658 run_rx_frame(sc, m, dmalen);
2662 /* copy aggregated frames to another mbuf */
2663 m0 = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
2664 if (__predict_false(m0 == NULL)) {
2665 DPRINTF("could not allocate mbuf\n");
2669 m_copydata(m, 4 /* skip 32-bit DMA-len header */,
2670 dmalen + sizeof(struct rt2870_rxd), mtod(m0, caddr_t));
2671 m0->m_pkthdr.len = m0->m_len =
2672 dmalen + sizeof(struct rt2870_rxd);
2673 run_rx_frame(sc, m0, dmalen);
2675 /* update data ptr */
2676 m->m_data += dmalen + 8;
2677 m->m_pkthdr.len = m->m_len -= dmalen + 8;
2684 run_tx_free(struct run_endpoint_queue *pq,
2685 struct run_tx_data *data, int txerr)
2687 if (data->m != NULL) {
2688 if (data->m->m_flags & M_TXCB)
2689 ieee80211_process_callback(data->ni, data->m,
2690 txerr ? ETIMEDOUT : 0);
2694 if (data->ni == NULL) {
2695 DPRINTF("no node\n");
2697 ieee80211_free_node(data->ni);
2702 STAILQ_INSERT_TAIL(&pq->tx_fh, data, next);
2707 run_bulk_tx_callbackN(struct usb_xfer *xfer, usb_error_t error, unsigned int index)
2709 struct run_softc *sc = usbd_xfer_softc(xfer);
2710 struct ifnet *ifp = sc->sc_ifp;
2711 struct ieee80211com *ic = ifp->if_l2com;
2712 struct run_tx_data *data;
2713 struct ieee80211vap *vap = NULL;
2714 struct usb_page_cache *pc;
2715 struct run_endpoint_queue *pq = &sc->sc_epq[index];
2717 usb_frlength_t size;
2722 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
2724 switch (USB_GET_STATE(xfer)) {
2725 case USB_ST_TRANSFERRED:
2726 DPRINTFN(11, "transfer complete: %d "
2727 "bytes @ index %d\n", actlen, index);
2729 data = usbd_xfer_get_priv(xfer);
2731 run_tx_free(pq, data, 0);
2732 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2734 usbd_xfer_set_priv(xfer, NULL);
2741 data = STAILQ_FIRST(&pq->tx_qh);
2745 STAILQ_REMOVE_HEAD(&pq->tx_qh, next);
2748 if (m->m_pkthdr.len > RUN_MAX_TXSZ) {
2749 DPRINTF("data overflow, %u bytes\n",
2754 run_tx_free(pq, data, 1);
2759 pc = usbd_xfer_get_frame(xfer, 0);
2760 size = sizeof(data->desc);
2761 usbd_copy_in(pc, 0, &data->desc, size);
2762 usbd_m_copy_in(pc, size, m, 0, m->m_pkthdr.len);
2764 vap = data->ni->ni_vap;
2765 if (ieee80211_radiotap_active_vap(vap)) {
2766 struct run_tx_radiotap_header *tap = &sc->sc_txtap;
2767 struct rt2860_txwi *txwi =
2768 (struct rt2860_txwi *)(&data->desc + sizeof(struct rt2870_txd));
2771 tap->wt_rate = rt2860_rates[data->ridx].rate;
2772 tap->wt_chan_freq = htole16(vap->iv_bss->ni_chan->ic_freq);
2773 tap->wt_chan_flags = htole16(vap->iv_bss->ni_chan->ic_flags);
2774 tap->wt_hwqueue = index;
2775 if (le16toh(txwi->phy) & RT2860_PHY_SHPRE)
2776 tap->wt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
2778 ieee80211_radiotap_tx(vap, m);
2781 /* align end on a 4-bytes boundary */
2782 len = (size + IEEE80211_CRC_LEN + m->m_pkthdr.len + 3) & ~3;
2784 DPRINTFN(11, "sending frame len=%u xferlen=%u @ index %d\n",
2785 m->m_pkthdr.len, len, index);
2787 usbd_xfer_set_frame_len(xfer, 0, len);
2788 usbd_xfer_set_priv(xfer, data);
2790 usbd_transfer_submit(xfer);
2799 DPRINTF("USB transfer error, %s\n",
2800 usbd_errstr(error));
2802 data = usbd_xfer_get_priv(xfer);
2807 if(data->ni != NULL)
2808 vap = data->ni->ni_vap;
2809 run_tx_free(pq, data, error);
2810 usbd_xfer_set_priv(xfer, NULL);
2813 vap = TAILQ_FIRST(&ic->ic_vaps);
2815 if (error != USB_ERR_CANCELLED) {
2816 if (error == USB_ERR_TIMEOUT) {
2817 device_printf(sc->sc_dev, "device timeout\n");
2818 uint32_t i = RUN_CMDQ_GET(&sc->cmdq_store);
2819 DPRINTF("cmdq_store=%d\n", i);
2820 sc->cmdq[i].func = run_usb_timeout_cb;
2821 sc->cmdq[i].arg0 = vap;
2822 ieee80211_runtask(ic, &sc->cmdq_task);
2826 * Try to clear stall first, also if other
2827 * errors occur, hence clearing stall
2828 * introduces a 50 ms delay:
2830 usbd_xfer_set_stall(xfer);
2838 run_bulk_tx_callback0(struct usb_xfer *xfer, usb_error_t error)
2840 run_bulk_tx_callbackN(xfer, error, 0);
2844 run_bulk_tx_callback1(struct usb_xfer *xfer, usb_error_t error)
2846 run_bulk_tx_callbackN(xfer, error, 1);
2850 run_bulk_tx_callback2(struct usb_xfer *xfer, usb_error_t error)
2852 run_bulk_tx_callbackN(xfer, error, 2);
2856 run_bulk_tx_callback3(struct usb_xfer *xfer, usb_error_t error)
2858 run_bulk_tx_callbackN(xfer, error, 3);
2862 run_bulk_tx_callback4(struct usb_xfer *xfer, usb_error_t error)
2864 run_bulk_tx_callbackN(xfer, error, 4);
2868 run_bulk_tx_callback5(struct usb_xfer *xfer, usb_error_t error)
2870 run_bulk_tx_callbackN(xfer, error, 5);
2874 run_set_tx_desc(struct run_softc *sc, struct run_tx_data *data)
2876 struct mbuf *m = data->m;
2877 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
2878 struct ieee80211vap *vap = data->ni->ni_vap;
2879 struct ieee80211_frame *wh;
2880 struct rt2870_txd *txd;
2881 struct rt2860_txwi *txwi;
2884 uint8_t ridx = data->ridx;
2887 /* get MCS code from rate index */
2888 mcs = rt2860_rates[ridx].mcs;
2890 xferlen = sizeof(*txwi) + m->m_pkthdr.len;
2892 /* roundup to 32-bit alignment */
2893 xferlen = (xferlen + 3) & ~3;
2895 txd = (struct rt2870_txd *)&data->desc;
2896 txd->len = htole16(xferlen);
2898 wh = mtod(m, struct ieee80211_frame *);
2901 * Ether both are true or both are false, the header
2902 * are nicely aligned to 32-bit. So, no L2 padding.
2904 if(IEEE80211_HAS_ADDR4(wh) == IEEE80211_QOS_HAS_SEQ(wh))
2909 /* setup TX Wireless Information */
2910 txwi = (struct rt2860_txwi *)(txd + 1);
2911 txwi->len = htole16(m->m_pkthdr.len - pad);
2912 if (rt2860_rates[ridx].phy == IEEE80211_T_DS) {
2913 txwi->phy = htole16(RT2860_PHY_CCK);
2914 if (ridx != RT2860_RIDX_CCK1 &&
2915 (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
2916 mcs |= RT2860_PHY_SHPRE;
2918 txwi->phy = htole16(RT2860_PHY_OFDM);
2919 txwi->phy |= htole16(mcs);
2921 /* check if RTS/CTS or CTS-to-self protection is required */
2922 if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
2923 (m->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold ||
2924 ((ic->ic_flags & IEEE80211_F_USEPROT) &&
2925 rt2860_rates[ridx].phy == IEEE80211_T_OFDM)))
2926 txwi->txop |= RT2860_TX_TXOP_HT;
2928 txwi->txop |= RT2860_TX_TXOP_BACKOFF;
2930 if (vap->iv_opmode != IEEE80211_M_STA && !IEEE80211_QOS_HAS_SEQ(wh))
2931 txwi->xflags |= RT2860_TX_NSEQ;
2934 /* This function must be called locked */
2936 run_tx(struct run_softc *sc, struct mbuf *m, struct ieee80211_node *ni)
2938 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
2939 struct ieee80211vap *vap = ni->ni_vap;
2940 struct ieee80211_frame *wh;
2941 struct ieee80211_channel *chan;
2942 const struct ieee80211_txparam *tp;
2943 struct run_node *rn = (void *)ni;
2944 struct run_tx_data *data;
2945 struct rt2870_txd *txd;
2946 struct rt2860_txwi *txwi;
2958 RUN_LOCK_ASSERT(sc, MA_OWNED);
2960 wh = mtod(m, struct ieee80211_frame *);
2962 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
2965 * There are 7 bulk endpoints: 1 for RX
2966 * and 6 for TX (4 EDCAs + HCCA + Prio).
2967 * Update 03-14-2009: some devices like the Planex GW-US300MiniS
2968 * seem to have only 4 TX bulk endpoints (Fukaumi Naoki).
2970 if ((hasqos = IEEE80211_QOS_HAS_SEQ(wh))) {
2973 if(IEEE80211_HAS_ADDR4(wh))
2974 frm = ((struct ieee80211_qosframe_addr4 *)wh)->i_qos;
2976 frm =((struct ieee80211_qosframe *)wh)->i_qos;
2978 qos = le16toh(*(const uint16_t *)frm);
2979 tid = qos & IEEE80211_QOS_TID;
2980 qid = TID_TO_WME_AC(tid);
2986 qflags = (qid < 4) ? RT2860_TX_QSEL_EDCA : RT2860_TX_QSEL_HCCA;
2988 DPRINTFN(8, "qos %d\tqid %d\ttid %d\tqflags %x\n",
2989 qos, qid, tid, qflags);
2991 chan = (ni->ni_chan != IEEE80211_CHAN_ANYC)?ni->ni_chan:ic->ic_curchan;
2992 tp = &vap->iv_txparms[ieee80211_chan2mode(chan)];
2994 /* pickup a rate index */
2995 if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
2996 type != IEEE80211_FC0_TYPE_DATA) {
2997 ridx = (ic->ic_curmode == IEEE80211_MODE_11A) ?
2998 RT2860_RIDX_OFDM6 : RT2860_RIDX_CCK1;
2999 ctl_ridx = rt2860_rates[ridx].ctl_ridx;
3001 if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
3002 ridx = rn->fix_ridx;
3004 ridx = rn->amrr_ridx;
3005 ctl_ridx = rt2860_rates[ridx].ctl_ridx;
3008 if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
3009 (!hasqos || (qos & IEEE80211_QOS_ACKPOLICY) !=
3010 IEEE80211_QOS_ACKPOLICY_NOACK)) {
3011 xflags |= RT2860_TX_ACK;
3012 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
3013 dur = rt2860_rates[ctl_ridx].sp_ack_dur;
3015 dur = rt2860_rates[ctl_ridx].lp_ack_dur;
3016 *(uint16_t *)wh->i_dur = htole16(dur);
3019 /* reserve slots for mgmt packets, just in case */
3020 if (sc->sc_epq[qid].tx_nfree < 3) {
3021 DPRINTFN(10, "tx ring %d is full\n", qid);
3025 data = STAILQ_FIRST(&sc->sc_epq[qid].tx_fh);
3026 STAILQ_REMOVE_HEAD(&sc->sc_epq[qid].tx_fh, next);
3027 sc->sc_epq[qid].tx_nfree--;
3029 txd = (struct rt2870_txd *)&data->desc;
3030 txd->flags = qflags;
3031 txwi = (struct rt2860_txwi *)(txd + 1);
3032 txwi->xflags = xflags;
3033 txwi->wcid = IEEE80211_IS_MULTICAST(wh->i_addr1) ?
3034 0 : RUN_AID2WCID(ni->ni_associd);
3035 /* clear leftover garbage bits */
3043 run_set_tx_desc(sc, data);
3046 * The chip keeps track of 2 kind of Tx stats,
3047 * * TX_STAT_FIFO, for per WCID stats, and
3048 * * TX_STA_CNT0 for all-TX-in-one stats.
3050 * To use FIFO stats, we need to store MCS into the driver-private
3051 * PacketID field. So that, we can tell whose stats when we read them.
3052 * We add 1 to the MCS because setting the PacketID field to 0 means
3053 * that we don't want feedback in TX_STAT_FIFO.
3054 * And, that's what we want for STA mode, since TX_STA_CNT0 does the job.
3056 * FIFO stats doesn't count Tx with WCID 0xff, so we do this in run_tx().
3058 if (sc->rvp_cnt > 1 || vap->iv_opmode == IEEE80211_M_HOSTAP ||
3059 vap->iv_opmode == IEEE80211_M_MBSS) {
3060 uint16_t pid = (rt2860_rates[ridx].mcs + 1) & 0xf;
3061 txwi->len |= htole16(pid << RT2860_TX_PID_SHIFT);
3064 * Unlike PCI based devices, we don't get any interrupt from
3065 * USB devices, so we simulate FIFO-is-full interrupt here.
3066 * Ralink recomends to drain FIFO stats every 100 ms, but 16 slots
3067 * quickly get fulled. To prevent overflow, increment a counter on
3068 * every FIFO stat request, so we know how many slots are left.
3069 * We do this only in HOSTAP or multiple vap mode since FIFO stats
3070 * are used only in those modes.
3071 * We just drain stats. AMRR gets updated every 1 sec by
3072 * run_ratectl_cb() via callout.
3073 * Call it early. Otherwise overflow.
3075 if (sc->fifo_cnt++ == 10) {
3077 * With multiple vaps or if_bridge, if_start() is called
3078 * with a non-sleepable lock, tcpinp. So, need to defer.
3080 uint32_t i = RUN_CMDQ_GET(&sc->cmdq_store);
3081 DPRINTFN(6, "cmdq_store=%d\n", i);
3082 sc->cmdq[i].func = run_drain_fifo;
3083 sc->cmdq[i].arg0 = sc;
3084 ieee80211_runtask(ic, &sc->cmdq_task);
3088 STAILQ_INSERT_TAIL(&sc->sc_epq[qid].tx_qh, data, next);
3090 usbd_transfer_start(sc->sc_xfer[qid]);
3092 DPRINTFN(8, "sending data frame len=%d rate=%d qid=%d\n", m->m_pkthdr.len +
3093 (int)(sizeof (struct rt2870_txd) + sizeof (struct rt2860_rxwi)),
3094 rt2860_rates[ridx].rate, qid);
3100 run_tx_mgt(struct run_softc *sc, struct mbuf *m, struct ieee80211_node *ni)
3102 struct ifnet *ifp = sc->sc_ifp;
3103 struct ieee80211com *ic = ifp->if_l2com;
3104 struct run_node *rn = (void *)ni;
3105 struct run_tx_data *data;
3106 struct ieee80211_frame *wh;
3107 struct rt2870_txd *txd;
3108 struct rt2860_txwi *txwi;
3110 uint8_t ridx = rn->mgt_ridx;
3115 RUN_LOCK_ASSERT(sc, MA_OWNED);
3117 wh = mtod(m, struct ieee80211_frame *);
3119 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
3121 /* tell hardware to add timestamp for probe responses */
3123 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
3124 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
3125 wflags |= RT2860_TX_TS;
3126 else if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
3127 xflags |= RT2860_TX_ACK;
3129 dur = ieee80211_ack_duration(ic->ic_rt, rt2860_rates[ridx].rate,
3130 ic->ic_flags & IEEE80211_F_SHPREAMBLE);
3131 *(uint16_t *)wh->i_dur = htole16(dur);
3134 if (sc->sc_epq[0].tx_nfree == 0) {
3135 /* let caller free mbuf */
3136 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3139 data = STAILQ_FIRST(&sc->sc_epq[0].tx_fh);
3140 STAILQ_REMOVE_HEAD(&sc->sc_epq[0].tx_fh, next);
3141 sc->sc_epq[0].tx_nfree--;
3143 txd = (struct rt2870_txd *)&data->desc;
3144 txd->flags = RT2860_TX_QSEL_EDCA;
3145 txwi = (struct rt2860_txwi *)(txd + 1);
3147 txwi->flags = wflags;
3148 txwi->xflags = xflags;
3149 txwi->txop = 0; /* clear leftover garbage bits */
3155 run_set_tx_desc(sc, data);
3157 DPRINTFN(10, "sending mgt frame len=%d rate=%d\n", m->m_pkthdr.len +
3158 (int)(sizeof (struct rt2870_txd) + sizeof (struct rt2860_rxwi)),
3159 rt2860_rates[ridx].rate);
3161 STAILQ_INSERT_TAIL(&sc->sc_epq[0].tx_qh, data, next);
3163 usbd_transfer_start(sc->sc_xfer[0]);
3169 run_sendprot(struct run_softc *sc,
3170 const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate)
3172 struct ieee80211com *ic = ni->ni_ic;
3173 struct ieee80211_frame *wh;
3174 struct run_tx_data *data;
3175 struct rt2870_txd *txd;
3176 struct rt2860_txwi *txwi;
3188 RUN_LOCK_ASSERT(sc, MA_OWNED);
3190 KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY,
3191 ("protection %d", prot));
3193 wh = mtod(m, struct ieee80211_frame *);
3194 pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
3195 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
3197 protrate = ieee80211_ctl_rate(ic->ic_rt, rate);
3198 ackrate = ieee80211_ack_rate(ic->ic_rt, rate);
3200 isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0;
3201 dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort)
3202 + ieee80211_ack_duration(ic->ic_rt, rate, isshort);
3203 wflags = RT2860_TX_FRAG;
3205 /* check that there are free slots before allocating the mbuf */
3206 if (sc->sc_epq[0].tx_nfree == 0) {
3207 /* let caller free mbuf */
3208 sc->sc_ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3212 if (prot == IEEE80211_PROT_RTSCTS) {
3213 /* NB: CTS is the same size as an ACK */
3214 dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort);
3215 xflags |= RT2860_TX_ACK;
3216 mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur);
3218 mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur);
3220 if (mprot == NULL) {
3221 sc->sc_ifp->if_oerrors++;
3222 DPRINTF("could not allocate mbuf\n");
3226 data = STAILQ_FIRST(&sc->sc_epq[0].tx_fh);
3227 STAILQ_REMOVE_HEAD(&sc->sc_epq[0].tx_fh, next);
3228 sc->sc_epq[0].tx_nfree--;
3230 txd = (struct rt2870_txd *)&data->desc;
3231 txd->flags = RT2860_TX_QSEL_EDCA;
3232 txwi = (struct rt2860_txwi *)(txd + 1);
3234 txwi->flags = wflags;
3235 txwi->xflags = xflags;
3236 txwi->txop = 0; /* clear leftover garbage bits */
3239 data->ni = ieee80211_ref_node(ni);
3241 for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
3242 if (rt2860_rates[ridx].rate == protrate)
3246 run_set_tx_desc(sc, data);
3248 DPRINTFN(1, "sending prot len=%u rate=%u\n",
3249 m->m_pkthdr.len, rate);
3251 STAILQ_INSERT_TAIL(&sc->sc_epq[0].tx_qh, data, next);
3253 usbd_transfer_start(sc->sc_xfer[0]);
3259 run_tx_param(struct run_softc *sc, struct mbuf *m, struct ieee80211_node *ni,
3260 const struct ieee80211_bpf_params *params)
3262 struct ieee80211com *ic = ni->ni_ic;
3263 struct ieee80211_frame *wh;
3264 struct run_tx_data *data;
3265 struct rt2870_txd *txd;
3266 struct rt2860_txwi *txwi;
3270 uint8_t opflags = 0;
3274 RUN_LOCK_ASSERT(sc, MA_OWNED);
3276 KASSERT(params != NULL, ("no raw xmit params"));
3278 wh = mtod(m, struct ieee80211_frame *);
3279 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
3281 rate = params->ibp_rate0;
3282 if (!ieee80211_isratevalid(ic->ic_rt, rate)) {
3283 /* let caller free mbuf */
3287 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
3288 xflags |= RT2860_TX_ACK;
3289 if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) {
3290 error = run_sendprot(sc, m, ni,
3291 params->ibp_flags & IEEE80211_BPF_RTS ?
3292 IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY,
3295 /* let caller free mbuf */
3298 opflags |= /*XXX RT2573_TX_LONG_RETRY |*/ RT2860_TX_TXOP_SIFS;
3301 if (sc->sc_epq[0].tx_nfree == 0) {
3302 /* let caller free mbuf */
3303 sc->sc_ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3304 DPRINTF("sending raw frame, but tx ring is full\n");
3307 data = STAILQ_FIRST(&sc->sc_epq[0].tx_fh);
3308 STAILQ_REMOVE_HEAD(&sc->sc_epq[0].tx_fh, next);
3309 sc->sc_epq[0].tx_nfree--;
3311 txd = (struct rt2870_txd *)&data->desc;
3312 txd->flags = RT2860_TX_QSEL_EDCA;
3313 txwi = (struct rt2860_txwi *)(txd + 1);
3315 txwi->xflags = xflags;
3316 txwi->txop = opflags;
3317 txwi->flags = 0; /* clear leftover garbage bits */
3321 for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
3322 if (rt2860_rates[ridx].rate == rate)
3326 run_set_tx_desc(sc, data);
3328 DPRINTFN(10, "sending raw frame len=%u rate=%u\n",
3329 m->m_pkthdr.len, rate);
3331 STAILQ_INSERT_TAIL(&sc->sc_epq[0].tx_qh, data, next);
3333 usbd_transfer_start(sc->sc_xfer[0]);
3339 run_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
3340 const struct ieee80211_bpf_params *params)
3342 struct ifnet *ifp = ni->ni_ic->ic_ifp;
3343 struct run_softc *sc = ifp->if_softc;
3348 /* prevent management frames from being sent if we're not ready */
3349 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
3354 if (params == NULL) {
3356 if ((error = run_tx_mgt(sc, m, ni)) != 0) {
3358 DPRINTF("mgt tx failed\n");
3362 /* tx raw packet with param */
3363 if ((error = run_tx_param(sc, m, ni, params)) != 0) {
3365 DPRINTF("tx with param failed\n");
3378 ieee80211_free_node(ni);
3385 run_start(struct ifnet *ifp)
3387 struct run_softc *sc = ifp->if_softc;
3388 struct ieee80211_node *ni;
3393 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
3399 /* send data frames */
3400 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
3404 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
3405 if (run_tx(sc, m, ni) != 0) {
3406 IFQ_DRV_PREPEND(&ifp->if_snd, m);
3407 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3416 run_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
3418 struct run_softc *sc = ifp->if_softc;
3419 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
3420 struct ifreq *ifr = (struct ifreq *) data;
3427 if (ifp->if_flags & IFF_UP) {
3428 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)){
3430 run_init_locked(sc);
3432 run_update_promisc_locked(ifp);
3434 if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
3435 (ic->ic_nrunning == 0 || sc->rvp_cnt <= 1)) {
3441 ieee80211_start_all(ic);
3444 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
3447 error = ether_ioctl(ifp, cmd, data);
3458 run_set_agc(struct run_softc *sc, uint8_t agc)
3462 if (sc->mac_ver == 0x3572) {
3463 run_bbp_read(sc, 27, &bbp);
3465 run_bbp_write(sc, 27, bbp | 0 << 5); /* select Rx0 */
3466 run_bbp_write(sc, 66, agc);
3467 run_bbp_write(sc, 27, bbp | 1 << 5); /* select Rx1 */
3468 run_bbp_write(sc, 66, agc);
3470 run_bbp_write(sc, 66, agc);
3474 run_select_chan_group(struct run_softc *sc, int group)
3479 run_bbp_write(sc, 62, 0x37 - sc->lna[group]);
3480 run_bbp_write(sc, 63, 0x37 - sc->lna[group]);
3481 run_bbp_write(sc, 64, 0x37 - sc->lna[group]);
3482 run_bbp_write(sc, 86, 0x00);
3485 if (sc->ext_2ghz_lna) {
3486 run_bbp_write(sc, 82, 0x62);
3487 run_bbp_write(sc, 75, 0x46);
3489 run_bbp_write(sc, 82, 0x84);
3490 run_bbp_write(sc, 75, 0x50);
3493 if (sc->mac_ver == 0x3572)
3494 run_bbp_write(sc, 82, 0x94);
3496 run_bbp_write(sc, 82, 0xf2);
3497 if (sc->ext_5ghz_lna)
3498 run_bbp_write(sc, 75, 0x46);
3500 run_bbp_write(sc, 75, 0x50);
3503 run_read(sc, RT2860_TX_BAND_CFG, &tmp);
3504 tmp &= ~(RT2860_5G_BAND_SEL_N | RT2860_5G_BAND_SEL_P);
3505 tmp |= (group == 0) ? RT2860_5G_BAND_SEL_N : RT2860_5G_BAND_SEL_P;
3506 run_write(sc, RT2860_TX_BAND_CFG, tmp);
3508 /* enable appropriate Power Amplifiers and Low Noise Amplifiers */
3509 tmp = RT2860_RFTR_EN | RT2860_TRSW_EN | RT2860_LNA_PE0_EN;
3510 if (sc->nrxchains > 1)
3511 tmp |= RT2860_LNA_PE1_EN;
3512 if (group == 0) { /* 2GHz */
3513 tmp |= RT2860_PA_PE_G0_EN;
3514 if (sc->ntxchains > 1)
3515 tmp |= RT2860_PA_PE_G1_EN;
3517 tmp |= RT2860_PA_PE_A0_EN;
3518 if (sc->ntxchains > 1)
3519 tmp |= RT2860_PA_PE_A1_EN;
3521 if (sc->mac_ver == 0x3572) {
3522 run_rt3070_rf_write(sc, 8, 0x00);
3523 run_write(sc, RT2860_TX_PIN_CFG, tmp);
3524 run_rt3070_rf_write(sc, 8, 0x80);
3526 run_write(sc, RT2860_TX_PIN_CFG, tmp);
3528 /* set initial AGC value */
3529 if (group == 0) { /* 2GHz band */
3530 if (sc->mac_ver >= 0x3070)
3531 agc = 0x1c + sc->lna[0] * 2;
3533 agc = 0x2e + sc->lna[0];
3534 } else { /* 5GHz band */
3535 if (sc->mac_ver == 0x3572)
3536 agc = 0x22 + (sc->lna[group] * 5) / 3;
3538 agc = 0x32 + (sc->lna[group] * 5) / 3;
3540 run_set_agc(sc, agc);
3544 run_rt2870_set_chan(struct run_softc *sc, uint32_t chan)
3546 const struct rfprog *rfprog = rt2860_rf2850;
3547 uint32_t r2, r3, r4;
3548 int8_t txpow1, txpow2;
3551 /* find the settings for this channel (we know it exists) */
3552 for (i = 0; rfprog[i].chan != chan; i++);
3555 if (sc->ntxchains == 1)
3556 r2 |= 1 << 12; /* 1T: disable Tx chain 2 */
3557 if (sc->nrxchains == 1)
3558 r2 |= 1 << 15 | 1 << 4; /* 1R: disable Rx chains 2 & 3 */
3559 else if (sc->nrxchains == 2)
3560 r2 |= 1 << 4; /* 2R: disable Rx chain 3 */
3562 /* use Tx power values from EEPROM */
3563 txpow1 = sc->txpow1[i];
3564 txpow2 = sc->txpow2[i];
3567 txpow1 = txpow1 << 1 | 1;
3569 txpow1 = (7 + txpow1) << 1;
3571 txpow2 = txpow2 << 1 | 1;
3573 txpow2 = (7 + txpow2) << 1;
3575 r3 = rfprog[i].r3 | txpow1 << 7;
3576 r4 = rfprog[i].r4 | sc->freq << 13 | txpow2 << 4;
3578 run_rt2870_rf_write(sc, RT2860_RF1, rfprog[i].r1);
3579 run_rt2870_rf_write(sc, RT2860_RF2, r2);
3580 run_rt2870_rf_write(sc, RT2860_RF3, r3);
3581 run_rt2870_rf_write(sc, RT2860_RF4, r4);
3585 run_rt2870_rf_write(sc, RT2860_RF1, rfprog[i].r1);
3586 run_rt2870_rf_write(sc, RT2860_RF2, r2);
3587 run_rt2870_rf_write(sc, RT2860_RF3, r3 | 1);
3588 run_rt2870_rf_write(sc, RT2860_RF4, r4);
3592 run_rt2870_rf_write(sc, RT2860_RF1, rfprog[i].r1);
3593 run_rt2870_rf_write(sc, RT2860_RF2, r2);
3594 run_rt2870_rf_write(sc, RT2860_RF3, r3);
3595 run_rt2870_rf_write(sc, RT2860_RF4, r4);
3599 run_rt3070_set_chan(struct run_softc *sc, uint32_t chan)
3601 int8_t txpow1, txpow2;
3605 /* RT3070 is 2GHz only */
3606 KASSERT(chan >= 1 && chan <= 14, ("wrong channel selected\n"));
3608 /* find the settings for this channel (we know it exists) */
3609 for (i = 0; rt2860_rf2850[i].chan != chan; i++);
3611 /* use Tx power values from EEPROM */
3612 txpow1 = sc->txpow1[i];
3613 txpow2 = sc->txpow2[i];
3615 run_rt3070_rf_write(sc, 2, rt3070_freqs[i].n);
3616 run_rt3070_rf_write(sc, 3, rt3070_freqs[i].k);
3617 run_rt3070_rf_read(sc, 6, &rf);
3618 rf = (rf & ~0x03) | rt3070_freqs[i].r;
3619 run_rt3070_rf_write(sc, 6, rf);
3622 run_rt3070_rf_read(sc, 12, &rf);
3623 rf = (rf & ~0x1f) | txpow1;
3624 run_rt3070_rf_write(sc, 12, rf);
3627 run_rt3070_rf_read(sc, 13, &rf);
3628 rf = (rf & ~0x1f) | txpow2;
3629 run_rt3070_rf_write(sc, 13, rf);
3631 run_rt3070_rf_read(sc, 1, &rf);
3633 if (sc->ntxchains == 1)
3634 rf |= 1 << 7 | 1 << 5; /* 1T: disable Tx chains 2 & 3 */
3635 else if (sc->ntxchains == 2)
3636 rf |= 1 << 7; /* 2T: disable Tx chain 3 */
3637 if (sc->nrxchains == 1)
3638 rf |= 1 << 6 | 1 << 4; /* 1R: disable Rx chains 2 & 3 */
3639 else if (sc->nrxchains == 2)
3640 rf |= 1 << 6; /* 2R: disable Rx chain 3 */
3641 run_rt3070_rf_write(sc, 1, rf);
3644 run_rt3070_rf_read(sc, 23, &rf);
3645 rf = (rf & ~0x7f) | sc->freq;
3646 run_rt3070_rf_write(sc, 23, rf);
3648 /* program RF filter */
3649 run_rt3070_rf_read(sc, 24, &rf); /* Tx */
3650 rf = (rf & ~0x3f) | sc->rf24_20mhz;
3651 run_rt3070_rf_write(sc, 24, rf);
3652 run_rt3070_rf_read(sc, 31, &rf); /* Rx */
3653 rf = (rf & ~0x3f) | sc->rf24_20mhz;
3654 run_rt3070_rf_write(sc, 31, rf);
3656 /* enable RF tuning */
3657 run_rt3070_rf_read(sc, 7, &rf);
3658 run_rt3070_rf_write(sc, 7, rf | 0x01);
3662 run_rt3572_set_chan(struct run_softc *sc, u_int chan)
3664 int8_t txpow1, txpow2;
3669 /* find the settings for this channel (we know it exists) */
3670 for (i = 0; rt2860_rf2850[i].chan != chan; i++);
3672 /* use Tx power values from EEPROM */
3673 txpow1 = sc->txpow1[i];
3674 txpow2 = sc->txpow2[i];
3677 run_bbp_write(sc, 25, sc->bbp25);
3678 run_bbp_write(sc, 26, sc->bbp26);
3680 /* enable IQ phase correction */
3681 run_bbp_write(sc, 25, 0x09);
3682 run_bbp_write(sc, 26, 0xff);
3685 run_rt3070_rf_write(sc, 2, rt3070_freqs[i].n);
3686 run_rt3070_rf_write(sc, 3, rt3070_freqs[i].k);
3687 run_rt3070_rf_read(sc, 6, &rf);
3688 rf = (rf & ~0x0f) | rt3070_freqs[i].r;
3689 rf |= (chan <= 14) ? 0x08 : 0x04;
3690 run_rt3070_rf_write(sc, 6, rf);
3693 run_rt3070_rf_read(sc, 5, &rf);
3694 rf &= ~(0x08 | 0x04);
3695 rf |= (chan <= 14) ? 0x04 : 0x08;
3696 run_rt3070_rf_write(sc, 5, rf);
3698 /* set Tx power for chain 0 */
3702 rf = 0xe0 | (txpow1 & 0xc) << 1 | (txpow1 & 0x3);
3703 run_rt3070_rf_write(sc, 12, rf);
3705 /* set Tx power for chain 1 */
3709 rf = 0xe0 | (txpow2 & 0xc) << 1 | (txpow2 & 0x3);
3710 run_rt3070_rf_write(sc, 13, rf);
3712 /* set Tx/Rx streams */
3713 run_rt3070_rf_read(sc, 1, &rf);
3715 if (sc->ntxchains == 1)
3716 rf |= 1 << 7 | 1 << 5; /* 1T: disable Tx chains 2 & 3 */
3717 else if (sc->ntxchains == 2)
3718 rf |= 1 << 7; /* 2T: disable Tx chain 3 */
3719 if (sc->nrxchains == 1)
3720 rf |= 1 << 6 | 1 << 4; /* 1R: disable Rx chains 2 & 3 */
3721 else if (sc->nrxchains == 2)
3722 rf |= 1 << 6; /* 2R: disable Rx chain 3 */
3723 run_rt3070_rf_write(sc, 1, rf);
3726 run_rt3070_rf_read(sc, 23, &rf);
3727 rf = (rf & ~0x7f) | sc->freq;
3728 run_rt3070_rf_write(sc, 23, rf);
3730 /* program RF filter */
3731 rf = sc->rf24_20mhz;
3732 run_rt3070_rf_write(sc, 24, rf); /* Tx */
3733 run_rt3070_rf_write(sc, 31, rf); /* Rx */
3735 /* enable RF tuning */
3736 run_rt3070_rf_read(sc, 7, &rf);
3737 rf = (chan <= 14) ? 0xd8 : ((rf & ~0xc8) | 0x14);
3738 run_rt3070_rf_write(sc, 7, rf);
3741 rf = (chan <= 14) ? 0xc3 : 0xc0;
3742 run_rt3070_rf_write(sc, 9, rf);
3744 /* set loop filter 1 */
3745 run_rt3070_rf_write(sc, 10, 0xf1);
3746 /* set loop filter 2 */
3747 run_rt3070_rf_write(sc, 11, (chan <= 14) ? 0xb9 : 0x00);
3750 run_rt3070_rf_write(sc, 15, (chan <= 14) ? 0x53 : 0x43);
3753 rf = 0x48 | sc->txmixgain_2ghz;
3755 rf = 0x78 | sc->txmixgain_5ghz;
3756 run_rt3070_rf_write(sc, 16, rf);
3759 run_rt3070_rf_write(sc, 17, 0x23);
3763 else if (chan <= 64)
3765 else if (chan <= 128)
3769 run_rt3070_rf_write(sc, 19, rf);
3774 else if (chan <= 64)
3776 else if (chan <= 128)
3780 run_rt3070_rf_write(sc, 20, rf);
3785 else if (chan <= 64)
3789 run_rt3070_rf_write(sc, 25, rf);
3792 run_rt3070_rf_write(sc, 26, (chan <= 14) ? 0x85 : 0x87);
3794 run_rt3070_rf_write(sc, 27, (chan <= 14) ? 0x00 : 0x01);
3796 run_rt3070_rf_write(sc, 29, (chan <= 14) ? 0x9b : 0x9f);
3798 run_read(sc, RT2860_GPIO_CTRL, &tmp);
3802 run_write(sc, RT2860_GPIO_CTRL, tmp);
3804 /* enable RF tuning */
3805 run_rt3070_rf_read(sc, 7, &rf);
3806 run_rt3070_rf_write(sc, 7, rf | 0x01);
3812 run_set_rx_antenna(struct run_softc *sc, int aux)
3817 run_mcu_cmd(sc, RT2860_MCU_CMD_ANTSEL, 0);
3818 run_read(sc, RT2860_GPIO_CTRL, &tmp);
3819 run_write(sc, RT2860_GPIO_CTRL, (tmp & ~0x0808) | 0x08);
3821 run_mcu_cmd(sc, RT2860_MCU_CMD_ANTSEL, 1);
3822 run_read(sc, RT2860_GPIO_CTRL, &tmp);
3823 run_write(sc, RT2860_GPIO_CTRL, tmp & ~0x0808);
3828 run_set_chan(struct run_softc *sc, struct ieee80211_channel *c)
3830 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
3831 uint32_t chan, group;
3833 chan = ieee80211_chan2ieee(ic, c);
3834 if (chan == 0 || chan == IEEE80211_CHAN_ANY)
3837 if (sc->mac_ver == 0x3572)
3838 run_rt3572_set_chan(sc, chan);
3839 else if (sc->mac_ver >= 0x3070)
3840 run_rt3070_set_chan(sc, chan);
3842 run_rt2870_set_chan(sc, chan);
3844 /* determine channel group */
3847 else if (chan <= 64)
3849 else if (chan <= 128)
3854 /* XXX necessary only when group has changed! */
3855 run_select_chan_group(sc, group);
3863 run_set_channel(struct ieee80211com *ic)
3865 struct run_softc *sc = ic->ic_ifp->if_softc;
3868 run_set_chan(sc, ic->ic_curchan);
3875 run_scan_start(struct ieee80211com *ic)
3877 struct run_softc *sc = ic->ic_ifp->if_softc;
3882 /* abort TSF synchronization */
3883 run_read(sc, RT2860_BCN_TIME_CFG, &tmp);
3884 run_write(sc, RT2860_BCN_TIME_CFG,
3885 tmp & ~(RT2860_BCN_TX_EN | RT2860_TSF_TIMER_EN |
3886 RT2860_TBTT_TIMER_EN));
3887 run_set_bssid(sc, sc->sc_ifp->if_broadcastaddr);
3895 run_scan_end(struct ieee80211com *ic)
3897 struct run_softc *sc = ic->ic_ifp->if_softc;
3901 run_enable_tsf_sync(sc);
3902 /* XXX keep local copy */
3903 run_set_bssid(sc, sc->sc_bssid);
3911 * Could be called from ieee80211_node_timeout()
3912 * (non-sleepable thread)
3915 run_update_beacon(struct ieee80211vap *vap, int item)
3917 struct ieee80211com *ic = vap->iv_ic;
3918 struct run_softc *sc = ic->ic_ifp->if_softc;
3919 struct run_vap *rvp = RUN_VAP(vap);
3923 KASSERT(vap != NULL, ("no beacon"));
3926 case IEEE80211_BEACON_ERP:
3927 run_updateslot(ic->ic_ifp);
3929 case IEEE80211_BEACON_HTINFO:
3932 case IEEE80211_BEACON_TIM:
3939 setbit(rvp->bo.bo_flags, item);
3940 ieee80211_beacon_update(vap->iv_bss, &rvp->bo, rvp->beacon_mbuf, mcast);
3942 i = RUN_CMDQ_GET(&sc->cmdq_store);
3943 DPRINTF("cmdq_store=%d\n", i);
3944 sc->cmdq[i].func = run_update_beacon_cb;
3945 sc->cmdq[i].arg0 = vap;
3946 ieee80211_runtask(ic, &sc->cmdq_task);
3952 run_update_beacon_cb(void *arg)
3954 struct ieee80211vap *vap = arg;
3955 struct run_vap *rvp = RUN_VAP(vap);
3956 struct ieee80211com *ic = vap->iv_ic;
3957 struct run_softc *sc = ic->ic_ifp->if_softc;
3958 struct rt2860_txwi txwi;
3962 if (vap->iv_bss->ni_chan == IEEE80211_CHAN_ANYC)
3966 * No need to call ieee80211_beacon_update(), run_update_beacon()
3967 * is taking care of apropriate calls.
3969 if (rvp->beacon_mbuf == NULL) {
3970 rvp->beacon_mbuf = ieee80211_beacon_alloc(vap->iv_bss,
3972 if (rvp->beacon_mbuf == NULL)
3975 m = rvp->beacon_mbuf;
3977 memset(&txwi, 0, sizeof txwi);
3979 txwi.len = htole16(m->m_pkthdr.len);
3980 /* send beacons at the lowest available rate */
3981 ridx = (ic->ic_curmode == IEEE80211_MODE_11A) ?
3982 RT2860_RIDX_OFDM6 : RT2860_RIDX_CCK1;
3983 txwi.phy = htole16(rt2860_rates[ridx].mcs);
3984 if (rt2860_rates[ridx].phy == IEEE80211_T_OFDM)
3985 txwi.phy |= htole16(RT2860_PHY_OFDM);
3986 txwi.txop = RT2860_TX_TXOP_HT;
3987 txwi.flags = RT2860_TX_TS;
3988 txwi.xflags = RT2860_TX_NSEQ;
3990 run_write_region_1(sc, RT2860_BCN_BASE(rvp->rvp_id),
3991 (uint8_t *)&txwi, sizeof txwi);
3992 run_write_region_1(sc, RT2860_BCN_BASE(rvp->rvp_id) + sizeof txwi,
3993 mtod(m, uint8_t *), (m->m_pkthdr.len + 1) & ~1); /* roundup len */
3999 run_updateprot(struct ieee80211com *ic)
4001 struct run_softc *sc = ic->ic_ifp->if_softc;
4004 i = RUN_CMDQ_GET(&sc->cmdq_store);
4005 DPRINTF("cmdq_store=%d\n", i);
4006 sc->cmdq[i].func = run_updateprot_cb;
4007 sc->cmdq[i].arg0 = ic;
4008 ieee80211_runtask(ic, &sc->cmdq_task);
4012 run_updateprot_cb(void *arg)
4014 struct ieee80211com *ic = arg;
4015 struct run_softc *sc = ic->ic_ifp->if_softc;
4018 tmp = RT2860_RTSTH_EN | RT2860_PROT_NAV_SHORT | RT2860_TXOP_ALLOW_ALL;
4019 /* setup protection frame rate (MCS code) */
4020 tmp |= (ic->ic_curmode == IEEE80211_MODE_11A) ?
4021 rt2860_rates[RT2860_RIDX_OFDM6].mcs :
4022 rt2860_rates[RT2860_RIDX_CCK11].mcs;
4024 /* CCK frames don't require protection */
4025 run_write(sc, RT2860_CCK_PROT_CFG, tmp);
4026 if (ic->ic_flags & IEEE80211_F_USEPROT) {
4027 if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
4028 tmp |= RT2860_PROT_CTRL_RTS_CTS;
4029 else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
4030 tmp |= RT2860_PROT_CTRL_CTS;
4032 run_write(sc, RT2860_OFDM_PROT_CFG, tmp);
4036 run_usb_timeout_cb(void *arg)
4038 struct ieee80211vap *vap = arg;
4039 struct run_softc *sc = vap->iv_ic->ic_ifp->if_softc;
4041 RUN_LOCK_ASSERT(sc, MA_OWNED);
4043 if(vap->iv_state == IEEE80211_S_RUN &&
4044 vap->iv_opmode != IEEE80211_M_STA)
4045 run_reset_livelock(sc);
4046 else if (vap->iv_state == IEEE80211_S_SCAN) {
4047 DPRINTF("timeout caused by scan\n");
4049 ieee80211_cancel_scan(vap);
4051 DPRINTF("timeout by unknown cause\n");
4055 run_reset_livelock(struct run_softc *sc)
4059 RUN_LOCK_ASSERT(sc, MA_OWNED);
4062 * In IBSS or HostAP modes (when the hardware sends beacons), the MAC
4063 * can run into a livelock and start sending CTS-to-self frames like
4064 * crazy if protection is enabled. Reset MAC/BBP for a while
4066 run_read(sc, RT2860_DEBUG, &tmp);
4067 DPRINTFN(3, "debug reg %08x\n", tmp);
4068 if ((tmp & (1 << 29)) && (tmp & (1 << 7 | 1 << 5))) {
4069 DPRINTF("CTS-to-self livelock detected\n");
4070 run_write(sc, RT2860_MAC_SYS_CTRL, RT2860_MAC_SRST);
4072 run_write(sc, RT2860_MAC_SYS_CTRL,
4073 RT2860_MAC_RX_EN | RT2860_MAC_TX_EN);
4078 run_update_promisc_locked(struct ifnet *ifp)
4080 struct run_softc *sc = ifp->if_softc;
4083 run_read(sc, RT2860_RX_FILTR_CFG, &tmp);
4085 tmp |= RT2860_DROP_UC_NOME;
4086 if (ifp->if_flags & IFF_PROMISC)
4087 tmp &= ~RT2860_DROP_UC_NOME;
4089 run_write(sc, RT2860_RX_FILTR_CFG, tmp);
4091 DPRINTF("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
4092 "entering" : "leaving");
4096 run_update_promisc(struct ifnet *ifp)
4098 struct run_softc *sc = ifp->if_softc;
4100 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
4104 run_update_promisc_locked(ifp);
4109 run_enable_tsf_sync(struct run_softc *sc)
4111 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
4112 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
4115 DPRINTF("rvp_id=%d ic_opmode=%d\n", RUN_VAP(vap)->rvp_id, ic->ic_opmode);
4117 run_read(sc, RT2860_BCN_TIME_CFG, &tmp);
4119 tmp |= vap->iv_bss->ni_intval * 16;
4120 tmp |= RT2860_TSF_TIMER_EN | RT2860_TBTT_TIMER_EN;
4122 if (ic->ic_opmode == IEEE80211_M_STA) {
4124 * Local TSF is always updated with remote TSF on beacon
4127 tmp |= 1 << RT2860_TSF_SYNC_MODE_SHIFT;
4128 } else if (ic->ic_opmode == IEEE80211_M_IBSS) {
4129 tmp |= RT2860_BCN_TX_EN;
4131 * Local TSF is updated with remote TSF on beacon reception
4132 * only if the remote TSF is greater than local TSF.
4134 tmp |= 2 << RT2860_TSF_SYNC_MODE_SHIFT;
4135 } else if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
4136 ic->ic_opmode == IEEE80211_M_MBSS) {
4137 tmp |= RT2860_BCN_TX_EN;
4138 /* SYNC with nobody */
4139 tmp |= 3 << RT2860_TSF_SYNC_MODE_SHIFT;
4141 DPRINTF("Enabling TSF failed. undefined opmode\n");
4145 run_write(sc, RT2860_BCN_TIME_CFG, tmp);
4149 run_enable_mrr(struct run_softc *sc)
4151 #define CCK(mcs) (mcs)
4152 #define OFDM(mcs) (1 << 3 | (mcs))
4153 run_write(sc, RT2860_LG_FBK_CFG0,
4154 OFDM(6) << 28 | /* 54->48 */
4155 OFDM(5) << 24 | /* 48->36 */
4156 OFDM(4) << 20 | /* 36->24 */
4157 OFDM(3) << 16 | /* 24->18 */
4158 OFDM(2) << 12 | /* 18->12 */
4159 OFDM(1) << 8 | /* 12-> 9 */
4160 OFDM(0) << 4 | /* 9-> 6 */
4161 OFDM(0)); /* 6-> 6 */
4163 run_write(sc, RT2860_LG_FBK_CFG1,
4164 CCK(2) << 12 | /* 11->5.5 */
4165 CCK(1) << 8 | /* 5.5-> 2 */
4166 CCK(0) << 4 | /* 2-> 1 */
4167 CCK(0)); /* 1-> 1 */
4173 run_set_txpreamble(struct run_softc *sc)
4175 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
4178 run_read(sc, RT2860_AUTO_RSP_CFG, &tmp);
4179 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
4180 tmp |= RT2860_CCK_SHORT_EN;
4182 tmp &= ~RT2860_CCK_SHORT_EN;
4183 run_write(sc, RT2860_AUTO_RSP_CFG, tmp);
4187 run_set_basicrates(struct run_softc *sc)
4189 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
4191 /* set basic rates mask */
4192 if (ic->ic_curmode == IEEE80211_MODE_11B)
4193 run_write(sc, RT2860_LEGACY_BASIC_RATE, 0x003);
4194 else if (ic->ic_curmode == IEEE80211_MODE_11A)
4195 run_write(sc, RT2860_LEGACY_BASIC_RATE, 0x150);
4197 run_write(sc, RT2860_LEGACY_BASIC_RATE, 0x15f);
4201 run_set_leds(struct run_softc *sc, uint16_t which)
4203 (void)run_mcu_cmd(sc, RT2860_MCU_CMD_LEDS,
4204 which | (sc->leds & 0x7f));
4208 run_set_bssid(struct run_softc *sc, const uint8_t *bssid)
4210 run_write(sc, RT2860_MAC_BSSID_DW0,
4211 bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24);
4212 run_write(sc, RT2860_MAC_BSSID_DW1,
4213 bssid[4] | bssid[5] << 8);
4217 run_set_macaddr(struct run_softc *sc, const uint8_t *addr)
4219 run_write(sc, RT2860_MAC_ADDR_DW0,
4220 addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
4221 run_write(sc, RT2860_MAC_ADDR_DW1,
4222 addr[4] | addr[5] << 8 | 0xff << 16);
4226 run_updateslot(struct ifnet *ifp)
4228 struct run_softc *sc = ifp->if_softc;
4229 struct ieee80211com *ic = ifp->if_l2com;
4232 i = RUN_CMDQ_GET(&sc->cmdq_store);
4233 DPRINTF("cmdq_store=%d\n", i);
4234 sc->cmdq[i].func = run_updateslot_cb;
4235 sc->cmdq[i].arg0 = ifp;
4236 ieee80211_runtask(ic, &sc->cmdq_task);
4243 run_updateslot_cb(void *arg)
4245 struct ifnet *ifp = arg;
4246 struct run_softc *sc = ifp->if_softc;
4247 struct ieee80211com *ic = ifp->if_l2com;
4250 run_read(sc, RT2860_BKOFF_SLOT_CFG, &tmp);
4252 tmp |= (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
4253 run_write(sc, RT2860_BKOFF_SLOT_CFG, tmp);
4257 run_update_mcast(struct ifnet *ifp)
4259 /* h/w filter supports getting everything or nothing */
4260 ifp->if_flags |= IFF_ALLMULTI;
4264 run_rssi2dbm(struct run_softc *sc, uint8_t rssi, uint8_t rxchain)
4266 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
4267 struct ieee80211_channel *c = ic->ic_curchan;
4270 if (IEEE80211_IS_CHAN_5GHZ(c)) {
4271 uint32_t chan = ieee80211_chan2ieee(ic, c);
4272 delta = sc->rssi_5ghz[rxchain];
4274 /* determine channel group */
4276 delta -= sc->lna[1];
4277 else if (chan <= 128)
4278 delta -= sc->lna[2];
4280 delta -= sc->lna[3];
4282 delta = sc->rssi_2ghz[rxchain] - sc->lna[0];
4284 return (-12 - delta - rssi);
4288 run_bbp_init(struct run_softc *sc)
4290 int i, error, ntries;
4293 /* wait for BBP to wake up */
4294 for (ntries = 0; ntries < 20; ntries++) {
4295 if ((error = run_bbp_read(sc, 0, &bbp0)) != 0)
4297 if (bbp0 != 0 && bbp0 != 0xff)
4303 /* initialize BBP registers to default values */
4304 for (i = 0; i < nitems(rt2860_def_bbp); i++) {
4305 run_bbp_write(sc, rt2860_def_bbp[i].reg,
4306 rt2860_def_bbp[i].val);
4309 /* fix BBP84 for RT2860E */
4310 if (sc->mac_ver == 0x2860 && sc->mac_rev != 0x0101)
4311 run_bbp_write(sc, 84, 0x19);
4313 if (sc->mac_ver >= 0x3070) {
4314 run_bbp_write(sc, 79, 0x13);
4315 run_bbp_write(sc, 80, 0x05);
4316 run_bbp_write(sc, 81, 0x33);
4317 } else if (sc->mac_ver == 0x2860 && sc->mac_rev == 0x0100) {
4318 run_bbp_write(sc, 69, 0x16);
4319 run_bbp_write(sc, 73, 0x12);
4325 run_rt3070_rf_init(struct run_softc *sc)
4328 uint8_t rf, target, bbp4;
4331 run_rt3070_rf_read(sc, 30, &rf);
4332 /* toggle RF R30 bit 7 */
4333 run_rt3070_rf_write(sc, 30, rf | 0x80);
4335 run_rt3070_rf_write(sc, 30, rf & ~0x80);
4337 /* initialize RF registers to default value */
4338 if (sc->mac_ver == 0x3572) {
4339 for (i = 0; i < nitems(rt3572_def_rf); i++) {
4340 run_rt3070_rf_write(sc, rt3572_def_rf[i].reg,
4341 rt3572_def_rf[i].val);
4344 for (i = 0; i < nitems(rt3070_def_rf); i++) {
4345 run_rt3070_rf_write(sc, rt3070_def_rf[i].reg,
4346 rt3070_def_rf[i].val);
4350 if (sc->mac_ver == 0x3070) {
4351 /* change voltage from 1.2V to 1.35V for RT3070 */
4352 run_read(sc, RT3070_LDO_CFG0, &tmp);
4353 tmp = (tmp & ~0x0f000000) | 0x0d000000;
4354 run_write(sc, RT3070_LDO_CFG0, tmp);
4356 } else if (sc->mac_ver == 0x3071) {
4357 run_rt3070_rf_read(sc, 6, &rf);
4358 run_rt3070_rf_write(sc, 6, rf | 0x40);
4359 run_rt3070_rf_write(sc, 31, 0x14);
4361 run_read(sc, RT3070_LDO_CFG0, &tmp);
4363 if (sc->mac_rev < 0x0211)
4364 tmp |= 0x0d000000; /* 1.3V */
4366 tmp |= 0x01000000; /* 1.2V */
4367 run_write(sc, RT3070_LDO_CFG0, tmp);
4369 /* patch LNA_PE_G1 */
4370 run_read(sc, RT3070_GPIO_SWITCH, &tmp);
4371 run_write(sc, RT3070_GPIO_SWITCH, tmp & ~0x20);
4373 } else if (sc->mac_ver == 0x3572) {
4374 run_rt3070_rf_read(sc, 6, &rf);
4375 run_rt3070_rf_write(sc, 6, rf | 0x40);
4377 /* increase voltage from 1.2V to 1.35V */
4378 run_read(sc, RT3070_LDO_CFG0, &tmp);
4379 tmp = (tmp & ~0x1f000000) | 0x0d000000;
4380 run_write(sc, RT3070_LDO_CFG0, tmp);
4382 if (sc->mac_rev < 0x0211 || !sc->patch_dac) {
4383 run_delay(sc, 1); /* wait for 1msec */
4384 /* decrease voltage back to 1.2V */
4385 tmp = (tmp & ~0x1f000000) | 0x01000000;
4386 run_write(sc, RT3070_LDO_CFG0, tmp);
4390 /* select 20MHz bandwidth */
4391 run_rt3070_rf_read(sc, 31, &rf);
4392 run_rt3070_rf_write(sc, 31, rf & ~0x20);
4394 /* calibrate filter for 20MHz bandwidth */
4395 sc->rf24_20mhz = 0x1f; /* default value */
4396 target = (sc->mac_ver < 0x3071) ? 0x16 : 0x13;
4397 run_rt3070_filter_calib(sc, 0x07, target, &sc->rf24_20mhz);
4399 /* select 40MHz bandwidth */
4400 run_bbp_read(sc, 4, &bbp4);
4401 run_bbp_write(sc, 4, (bbp4 & ~0x08) | 0x10);
4402 run_rt3070_rf_read(sc, 31, &rf);
4403 run_rt3070_rf_write(sc, 31, rf | 0x20);
4405 /* calibrate filter for 40MHz bandwidth */
4406 sc->rf24_40mhz = 0x2f; /* default value */
4407 target = (sc->mac_ver < 0x3071) ? 0x19 : 0x15;
4408 run_rt3070_filter_calib(sc, 0x27, target, &sc->rf24_40mhz);
4410 /* go back to 20MHz bandwidth */
4411 run_bbp_read(sc, 4, &bbp4);
4412 run_bbp_write(sc, 4, bbp4 & ~0x18);
4414 if (sc->mac_ver == 0x3572) {
4415 /* save default BBP registers 25 and 26 values */
4416 run_bbp_read(sc, 25, &sc->bbp25);
4417 run_bbp_read(sc, 26, &sc->bbp26);
4418 } else if (sc->mac_rev < 0x0211)
4419 run_rt3070_rf_write(sc, 27, 0x03);
4421 run_read(sc, RT3070_OPT_14, &tmp);
4422 run_write(sc, RT3070_OPT_14, tmp | 1);
4424 if (sc->mac_ver == 0x3070 || sc->mac_ver == 0x3071) {
4425 run_rt3070_rf_read(sc, 17, &rf);
4426 rf &= ~RT3070_TX_LO1;
4427 if ((sc->mac_ver == 0x3070 ||
4428 (sc->mac_ver == 0x3071 && sc->mac_rev >= 0x0211)) &&
4430 rf |= 0x20; /* fix for long range Rx issue */
4431 if (sc->txmixgain_2ghz >= 1)
4432 rf = (rf & ~0x7) | sc->txmixgain_2ghz;
4433 run_rt3070_rf_write(sc, 17, rf);
4436 if (sc->mac_rev == 0x3071) {
4437 run_rt3070_rf_read(sc, 1, &rf);
4438 rf &= ~(RT3070_RX0_PD | RT3070_TX0_PD);
4439 rf |= RT3070_RF_BLOCK | RT3070_RX1_PD | RT3070_TX1_PD;
4440 run_rt3070_rf_write(sc, 1, rf);
4442 run_rt3070_rf_read(sc, 15, &rf);
4443 run_rt3070_rf_write(sc, 15, rf & ~RT3070_TX_LO2);
4445 run_rt3070_rf_read(sc, 20, &rf);
4446 run_rt3070_rf_write(sc, 20, rf & ~RT3070_RX_LO1);
4448 run_rt3070_rf_read(sc, 21, &rf);
4449 run_rt3070_rf_write(sc, 21, rf & ~RT3070_RX_LO2);
4452 if (sc->mac_ver == 0x3070 || sc->mac_ver == 0x3071) {
4453 /* fix Tx to Rx IQ glitch by raising RF voltage */
4454 run_rt3070_rf_read(sc, 27, &rf);
4456 if (sc->mac_rev < 0x0211)
4458 run_rt3070_rf_write(sc, 27, rf);
4464 run_rt3070_filter_calib(struct run_softc *sc, uint8_t init, uint8_t target,
4468 uint8_t bbp55_pb, bbp55_sb, delta;
4471 /* program filter */
4472 run_rt3070_rf_read(sc, 24, &rf24);
4473 rf24 = (rf24 & 0xc0) | init; /* initial filter value */
4474 run_rt3070_rf_write(sc, 24, rf24);
4476 /* enable baseband loopback mode */
4477 run_rt3070_rf_read(sc, 22, &rf22);
4478 run_rt3070_rf_write(sc, 22, rf22 | 0x01);
4480 /* set power and frequency of passband test tone */
4481 run_bbp_write(sc, 24, 0x00);
4482 for (ntries = 0; ntries < 100; ntries++) {
4483 /* transmit test tone */
4484 run_bbp_write(sc, 25, 0x90);
4486 /* read received power */
4487 run_bbp_read(sc, 55, &bbp55_pb);
4494 /* set power and frequency of stopband test tone */
4495 run_bbp_write(sc, 24, 0x06);
4496 for (ntries = 0; ntries < 100; ntries++) {
4497 /* transmit test tone */
4498 run_bbp_write(sc, 25, 0x90);
4500 /* read received power */
4501 run_bbp_read(sc, 55, &bbp55_sb);
4503 delta = bbp55_pb - bbp55_sb;
4507 /* reprogram filter */
4509 run_rt3070_rf_write(sc, 24, rf24);
4513 rf24--; /* backtrack */
4515 run_rt3070_rf_write(sc, 24, rf24);
4518 /* restore initial state */
4519 run_bbp_write(sc, 24, 0x00);
4521 /* disable baseband loopback mode */
4522 run_rt3070_rf_read(sc, 22, &rf22);
4523 run_rt3070_rf_write(sc, 22, rf22 & ~0x01);
4529 run_rt3070_rf_setup(struct run_softc *sc)
4534 if (sc->mac_ver == 0x3572) {
4535 /* enable DC filter */
4536 if (sc->mac_rev >= 0x0201)
4537 run_bbp_write(sc, 103, 0xc0);
4539 run_bbp_read(sc, 138, &bbp);
4540 if (sc->ntxchains == 1)
4541 bbp |= 0x20; /* turn off DAC1 */
4542 if (sc->nrxchains == 1)
4543 bbp &= ~0x02; /* turn off ADC1 */
4544 run_bbp_write(sc, 138, bbp);
4546 if (sc->mac_rev >= 0x0211) {
4547 /* improve power consumption */
4548 run_bbp_read(sc, 31, &bbp);
4549 run_bbp_write(sc, 31, bbp & ~0x03);
4552 run_rt3070_rf_read(sc, 16, &rf);
4553 rf = (rf & ~0x07) | sc->txmixgain_2ghz;
4554 run_rt3070_rf_write(sc, 16, rf);
4556 } else if (sc->mac_ver == 0x3071) {
4557 /* enable DC filter */
4558 if (sc->mac_rev >= 0x0201)
4559 run_bbp_write(sc, 103, 0xc0);
4561 run_bbp_read(sc, 138, &bbp);
4562 if (sc->ntxchains == 1)
4563 bbp |= 0x20; /* turn off DAC1 */
4564 if (sc->nrxchains == 1)
4565 bbp &= ~0x02; /* turn off ADC1 */
4566 run_bbp_write(sc, 138, bbp);
4568 if (sc->mac_rev >= 0x0211) {
4569 /* improve power consumption */
4570 run_bbp_read(sc, 31, &bbp);
4571 run_bbp_write(sc, 31, bbp & ~0x03);
4574 run_write(sc, RT2860_TX_SW_CFG1, 0);
4575 if (sc->mac_rev < 0x0211) {
4576 run_write(sc, RT2860_TX_SW_CFG2,
4577 sc->patch_dac ? 0x2c : 0x0f);
4579 run_write(sc, RT2860_TX_SW_CFG2, 0);
4581 } else if (sc->mac_ver == 0x3070) {
4582 if (sc->mac_rev >= 0x0201) {
4583 /* enable DC filter */
4584 run_bbp_write(sc, 103, 0xc0);
4586 /* improve power consumption */
4587 run_bbp_read(sc, 31, &bbp);
4588 run_bbp_write(sc, 31, bbp & ~0x03);
4591 if (sc->mac_rev < 0x0211) {
4592 run_write(sc, RT2860_TX_SW_CFG1, 0);
4593 run_write(sc, RT2860_TX_SW_CFG2, 0x2c);
4595 run_write(sc, RT2860_TX_SW_CFG2, 0);
4598 /* initialize RF registers from ROM for >=RT3071*/
4599 if (sc->mac_ver >= 0x3071) {
4600 for (i = 0; i < 10; i++) {
4601 if (sc->rf[i].reg == 0 || sc->rf[i].reg == 0xff)
4603 run_rt3070_rf_write(sc, sc->rf[i].reg, sc->rf[i].val);
4609 run_txrx_enable(struct run_softc *sc)
4611 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
4615 run_write(sc, RT2860_MAC_SYS_CTRL, RT2860_MAC_TX_EN);
4616 for (ntries = 0; ntries < 200; ntries++) {
4617 if ((error = run_read(sc, RT2860_WPDMA_GLO_CFG, &tmp)) != 0)
4619 if ((tmp & (RT2860_TX_DMA_BUSY | RT2860_RX_DMA_BUSY)) == 0)
4628 tmp |= RT2860_RX_DMA_EN | RT2860_TX_DMA_EN | RT2860_TX_WB_DDONE;
4629 run_write(sc, RT2860_WPDMA_GLO_CFG, tmp);
4631 /* enable Rx bulk aggregation (set timeout and limit) */
4632 tmp = RT2860_USB_TX_EN | RT2860_USB_RX_EN | RT2860_USB_RX_AGG_EN |
4633 RT2860_USB_RX_AGG_TO(128) | RT2860_USB_RX_AGG_LMT(2);
4634 run_write(sc, RT2860_USB_DMA_CFG, tmp);
4637 tmp = RT2860_DROP_CRC_ERR | RT2860_DROP_PHY_ERR;
4638 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
4639 tmp |= RT2860_DROP_UC_NOME | RT2860_DROP_DUPL |
4640 RT2860_DROP_CTS | RT2860_DROP_BA | RT2860_DROP_ACK |
4641 RT2860_DROP_VER_ERR | RT2860_DROP_CTRL_RSV |
4642 RT2860_DROP_CFACK | RT2860_DROP_CFEND;
4643 if (ic->ic_opmode == IEEE80211_M_STA)
4644 tmp |= RT2860_DROP_RTS | RT2860_DROP_PSPOLL;
4646 run_write(sc, RT2860_RX_FILTR_CFG, tmp);
4648 run_write(sc, RT2860_MAC_SYS_CTRL,
4649 RT2860_MAC_RX_EN | RT2860_MAC_TX_EN);
4655 run_init_locked(struct run_softc *sc)
4657 struct ifnet *ifp = sc->sc_ifp;
4658 struct ieee80211com *ic = ifp->if_l2com;
4665 if (ic->ic_nrunning > 1)
4670 for (ntries = 0; ntries < 100; ntries++) {
4671 if (run_read(sc, RT2860_ASIC_VER_ID, &tmp) != 0)
4673 if (tmp != 0 && tmp != 0xffffffff)
4680 for (i = 0; i != RUN_EP_QUEUES; i++)
4681 run_setup_tx_list(sc, &sc->sc_epq[i]);
4683 run_set_macaddr(sc, IF_LLADDR(ifp));
4685 for (ntries = 0; ntries < 100; ntries++) {
4686 if (run_read(sc, RT2860_WPDMA_GLO_CFG, &tmp) != 0)
4688 if ((tmp & (RT2860_TX_DMA_BUSY | RT2860_RX_DMA_BUSY)) == 0)
4692 if (ntries == 100) {
4693 device_printf(sc->sc_dev, "timeout waiting for DMA engine\n");
4697 tmp |= RT2860_TX_WB_DDONE;
4698 run_write(sc, RT2860_WPDMA_GLO_CFG, tmp);
4700 /* turn off PME_OEN to solve high-current issue */
4701 run_read(sc, RT2860_SYS_CTRL, &tmp);
4702 run_write(sc, RT2860_SYS_CTRL, tmp & ~RT2860_PME_OEN);
4704 run_write(sc, RT2860_MAC_SYS_CTRL,
4705 RT2860_BBP_HRST | RT2860_MAC_SRST);
4706 run_write(sc, RT2860_USB_DMA_CFG, 0);
4708 if (run_reset(sc) != 0) {
4709 device_printf(sc->sc_dev, "could not reset chipset\n");
4713 run_write(sc, RT2860_MAC_SYS_CTRL, 0);
4715 /* init Tx power for all Tx rates (from EEPROM) */
4716 for (ridx = 0; ridx < 5; ridx++) {
4717 if (sc->txpow20mhz[ridx] == 0xffffffff)
4719 run_write(sc, RT2860_TX_PWR_CFG(ridx), sc->txpow20mhz[ridx]);
4722 for (i = 0; i < nitems(rt2870_def_mac); i++)
4723 run_write(sc, rt2870_def_mac[i].reg, rt2870_def_mac[i].val);
4724 run_write(sc, RT2860_WMM_AIFSN_CFG, 0x00002273);
4725 run_write(sc, RT2860_WMM_CWMIN_CFG, 0x00002344);
4726 run_write(sc, RT2860_WMM_CWMAX_CFG, 0x000034aa);
4728 if (sc->mac_ver >= 0x3070) {
4729 /* set delay of PA_PE assertion to 1us (unit of 0.25us) */
4730 run_write(sc, RT2860_TX_SW_CFG0,
4731 4 << RT2860_DLY_PAPE_EN_SHIFT);
4734 /* wait while MAC is busy */
4735 for (ntries = 0; ntries < 100; ntries++) {
4736 if (run_read(sc, RT2860_MAC_STATUS_REG, &tmp) != 0)
4738 if (!(tmp & (RT2860_RX_STATUS_BUSY | RT2860_TX_STATUS_BUSY)))
4745 /* clear Host to MCU mailbox */
4746 run_write(sc, RT2860_H2M_BBPAGENT, 0);
4747 run_write(sc, RT2860_H2M_MAILBOX, 0);
4750 if (run_bbp_init(sc) != 0) {
4751 device_printf(sc->sc_dev, "could not initialize BBP\n");
4755 /* abort TSF synchronization */
4756 run_read(sc, RT2860_BCN_TIME_CFG, &tmp);
4757 tmp &= ~(RT2860_BCN_TX_EN | RT2860_TSF_TIMER_EN |
4758 RT2860_TBTT_TIMER_EN);
4759 run_write(sc, RT2860_BCN_TIME_CFG, tmp);
4761 /* clear RX WCID search table */
4762 run_set_region_4(sc, RT2860_WCID_ENTRY(0), 0, 512);
4763 /* clear WCID attribute table */
4764 run_set_region_4(sc, RT2860_WCID_ATTR(0), 0, 8 * 32);
4766 /* hostapd sets a key before init. So, don't clear it. */
4767 if (sc->cmdq_key_set != RUN_CMDQ_GO) {
4768 /* clear shared key table */
4769 run_set_region_4(sc, RT2860_SKEY(0, 0), 0, 8 * 32);
4770 /* clear shared key mode */
4771 run_set_region_4(sc, RT2860_SKEY_MODE_0_7, 0, 4);
4774 run_read(sc, RT2860_US_CYC_CNT, &tmp);
4775 tmp = (tmp & ~0xff) | 0x1e;
4776 run_write(sc, RT2860_US_CYC_CNT, tmp);
4778 if (sc->mac_rev != 0x0101)
4779 run_write(sc, RT2860_TXOP_CTRL_CFG, 0x0000583f);
4781 run_write(sc, RT2860_WMM_TXOP0_CFG, 0);
4782 run_write(sc, RT2860_WMM_TXOP1_CFG, 48 << 16 | 96);
4784 /* write vendor-specific BBP values (from EEPROM) */
4785 for (i = 0; i < 10; i++) {
4786 if (sc->bbp[i].reg == 0 || sc->bbp[i].reg == 0xff)
4788 run_bbp_write(sc, sc->bbp[i].reg, sc->bbp[i].val);
4791 /* select Main antenna for 1T1R devices */
4792 if (sc->rf_rev == RT3070_RF_3020)
4793 run_set_rx_antenna(sc, 0);
4795 /* send LEDs operating mode to microcontroller */
4796 (void)run_mcu_cmd(sc, RT2860_MCU_CMD_LED1, sc->led[0]);
4797 (void)run_mcu_cmd(sc, RT2860_MCU_CMD_LED2, sc->led[1]);
4798 (void)run_mcu_cmd(sc, RT2860_MCU_CMD_LED3, sc->led[2]);
4800 if (sc->mac_ver >= 0x3070)
4801 run_rt3070_rf_init(sc);
4803 /* disable non-existing Rx chains */
4804 run_bbp_read(sc, 3, &bbp3);
4805 bbp3 &= ~(1 << 3 | 1 << 4);
4806 if (sc->nrxchains == 2)
4808 else if (sc->nrxchains == 3)
4810 run_bbp_write(sc, 3, bbp3);
4812 /* disable non-existing Tx chains */
4813 run_bbp_read(sc, 1, &bbp1);
4814 if (sc->ntxchains == 1)
4815 bbp1 &= ~(1 << 3 | 1 << 4);
4816 run_bbp_write(sc, 1, bbp1);
4818 if (sc->mac_ver >= 0x3070)
4819 run_rt3070_rf_setup(sc);
4821 /* select default channel */
4822 run_set_chan(sc, ic->ic_curchan);
4824 /* setup initial protection mode */
4825 run_updateprot_cb(ic);
4827 /* turn radio LED on */
4828 run_set_leds(sc, RT2860_LED_RADIO);
4830 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
4831 ifp->if_drv_flags |= IFF_DRV_RUNNING;
4832 sc->cmdq_run = RUN_CMDQ_GO;
4834 for (i = 0; i != RUN_N_XFER; i++)
4835 usbd_xfer_set_stall(sc->sc_xfer[i]);
4837 usbd_transfer_start(sc->sc_xfer[RUN_BULK_RX]);
4839 if (run_txrx_enable(sc) != 0)
4851 struct run_softc *sc = arg;
4852 struct ifnet *ifp = sc->sc_ifp;
4853 struct ieee80211com *ic = ifp->if_l2com;
4856 run_init_locked(sc);
4859 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
4860 ieee80211_start_all(ic);
4866 struct run_softc *sc = (struct run_softc *)arg;
4867 struct ifnet *ifp = sc->sc_ifp;
4872 RUN_LOCK_ASSERT(sc, MA_OWNED);
4874 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
4875 run_set_leds(sc, 0); /* turn all LEDs off */
4877 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
4879 sc->ratectl_run = RUN_RATECTL_OFF;
4880 sc->cmdq_run = sc->cmdq_key_set;
4884 for(i = 0; i < RUN_N_XFER; i++)
4885 usbd_transfer_drain(sc->sc_xfer[i]);
4889 if (sc->rx_m != NULL) {
4895 run_read(sc, RT2860_MAC_SYS_CTRL, &tmp);
4896 tmp &= ~(RT2860_MAC_RX_EN | RT2860_MAC_TX_EN);
4897 run_write(sc, RT2860_MAC_SYS_CTRL, tmp);
4899 /* wait for pending Tx to complete */
4900 for (ntries = 0; ntries < 100; ntries++) {
4901 if (run_read(sc, RT2860_TXRXQ_PCNT, &tmp) != 0) {
4902 DPRINTF("Cannot read Tx queue count\n");
4905 if ((tmp & RT2860_TX2Q_PCNT_MASK) == 0) {
4906 DPRINTF("All Tx cleared\n");
4912 DPRINTF("There are still pending Tx\n");
4914 run_write(sc, RT2860_USB_DMA_CFG, 0);
4916 run_write(sc, RT2860_MAC_SYS_CTRL, RT2860_BBP_HRST | RT2860_MAC_SRST);
4917 run_write(sc, RT2860_MAC_SYS_CTRL, 0);
4919 for (i = 0; i != RUN_EP_QUEUES; i++)
4920 run_unsetup_tx_list(sc, &sc->sc_epq[i]);
4926 run_delay(struct run_softc *sc, unsigned int ms)
4928 usb_pause_mtx(mtx_owned(&sc->sc_mtx) ?
4929 &sc->sc_mtx : NULL, USB_MS_TO_TICKS(ms));
4932 static device_method_t run_methods[] = {
4933 /* Device interface */
4934 DEVMETHOD(device_probe, run_match),
4935 DEVMETHOD(device_attach, run_attach),
4936 DEVMETHOD(device_detach, run_detach),
4941 static driver_t run_driver = {
4944 sizeof(struct run_softc)
4947 static devclass_t run_devclass;
4949 DRIVER_MODULE(run, uhub, run_driver, run_devclass, NULL, 0);
4950 MODULE_DEPEND(run, wlan, 1, 1, 1);
4951 MODULE_DEPEND(run, usb, 1, 1, 1);
4952 MODULE_DEPEND(run, firmware, 1, 1, 1);
4953 MODULE_VERSION(run, 1);