]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/dev/usb/wlan/if_run.c
MFC r227751, r238766, r241553, r242748, r244047, r245947, r246021, r247255,
[FreeBSD/stable/8.git] / sys / dev / usb / wlan / if_run.c
1 /*-
2  * Copyright (c) 2008,2010 Damien Bergamini <damien.bergamini@free.fr>
3  * ported to FreeBSD by Akinori Furukoshi <moonlightakkiy@yahoo.ca>
4  * USB Consulting, Hans Petter Selasky <hselasky@freebsd.org>
5  *
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.
9  *
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.
17  */
18
19 #include <sys/cdefs.h>
20 __FBSDID("$FreeBSD$");
21
22 /*-
23  * Ralink Technology RT2700U/RT2800U/RT3000U chipset driver.
24  * http://www.ralinktech.com/
25  */
26
27 #include <sys/param.h>
28 #include <sys/sockio.h>
29 #include <sys/sysctl.h>
30 #include <sys/lock.h>
31 #include <sys/mutex.h>
32 #include <sys/mbuf.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>
38 #include <sys/bus.h>
39 #include <sys/endian.h>
40 #include <sys/linker.h>
41 #include <sys/firmware.h>
42 #include <sys/kdb.h>
43
44 #include <machine/bus.h>
45 #include <machine/resource.h>
46 #include <sys/rman.h>
47
48 #include <net/bpf.h>
49 #include <net/if.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>
55
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>
61
62 #include <net80211/ieee80211_var.h>
63 #include <net80211/ieee80211_regdomain.h>
64 #include <net80211/ieee80211_radiotap.h>
65 #include <net80211/ieee80211_ratectl.h>
66
67 #include <dev/usb/usb.h>
68 #include <dev/usb/usbdi.h>
69 #include "usbdevs.h"
70
71 #define USB_DEBUG_VAR run_debug
72 #include <dev/usb/usb_debug.h>
73
74 #include "if_runreg.h"
75 #include "if_runvar.h"
76
77 #define N(_a) ((int)(sizeof((_a)) / sizeof((_a)[0])))
78
79 #ifdef  USB_DEBUG
80 #define RUN_DEBUG
81 #endif
82
83 #ifdef  RUN_DEBUG
84 int run_debug = 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,
87     "run debug level");
88 #endif
89
90 #define IEEE80211_HAS_ADDR4(wh) \
91         (((wh)->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS)
92
93 /*
94  * Because of LOR in run_key_delete(), use atomic instead.
95  * '& RUN_CMDQ_MASQ' is to loop cmdq[].
96  */
97 #define RUN_CMDQ_GET(c) (atomic_fetchadd_32((c), 1) & RUN_CMDQ_MASQ)
98
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,             F5D8055V2),
148     RUN_DEV(BELKIN,             F6D4050V1),
149     RUN_DEV(BELKIN,             RT2870_1),
150     RUN_DEV(BELKIN,             RT2870_2),
151     RUN_DEV(CISCOLINKSYS,       AE1000),
152     RUN_DEV(CISCOLINKSYS2,      RT3070),
153     RUN_DEV(CISCOLINKSYS3,      RT3070),
154     RUN_DEV(CONCEPTRONIC2,      RT2870_1),
155     RUN_DEV(CONCEPTRONIC2,      RT2870_2),
156     RUN_DEV(CONCEPTRONIC2,      RT2870_3),
157     RUN_DEV(CONCEPTRONIC2,      RT2870_4),
158     RUN_DEV(CONCEPTRONIC2,      RT2870_5),
159     RUN_DEV(CONCEPTRONIC2,      RT2870_6),
160     RUN_DEV(CONCEPTRONIC2,      RT2870_7),
161     RUN_DEV(CONCEPTRONIC2,      RT2870_8),
162     RUN_DEV(CONCEPTRONIC2,      RT3070_1),
163     RUN_DEV(CONCEPTRONIC2,      RT3070_2),
164     RUN_DEV(CONCEPTRONIC2,      VIGORN61),
165     RUN_DEV(COREGA,             CGWLUSB300GNM),
166     RUN_DEV(COREGA,             RT2870_1),
167     RUN_DEV(COREGA,             RT2870_2),
168     RUN_DEV(COREGA,             RT2870_3),
169     RUN_DEV(COREGA,             RT3070),
170     RUN_DEV(CYBERTAN,           RT2870),
171     RUN_DEV(DLINK,              RT2870),
172     RUN_DEV(DLINK,              RT3072),
173     RUN_DEV(DLINK,              DWA127),
174     RUN_DEV(DLINK2,             DWA130),
175     RUN_DEV(DLINK2,             RT2870_1),
176     RUN_DEV(DLINK2,             RT2870_2),
177     RUN_DEV(DLINK2,             RT3070_1),
178     RUN_DEV(DLINK2,             RT3070_2),
179     RUN_DEV(DLINK2,             RT3070_3),
180     RUN_DEV(DLINK2,             RT3070_4),
181     RUN_DEV(DLINK2,             RT3070_5),
182     RUN_DEV(DLINK2,             RT3072),
183     RUN_DEV(DLINK2,             RT3072_1),
184     RUN_DEV(EDIMAX,             EW7717),
185     RUN_DEV(EDIMAX,             EW7718),
186     RUN_DEV(EDIMAX,             RT2870_1),
187     RUN_DEV(ENCORE,             RT3070_1),
188     RUN_DEV(ENCORE,             RT3070_2),
189     RUN_DEV(ENCORE,             RT3070_3),
190     RUN_DEV(GIGABYTE,           GNWB31N),
191     RUN_DEV(GIGABYTE,           GNWB32L),
192     RUN_DEV(GIGABYTE,           RT2870_1),
193     RUN_DEV(GIGASET,            RT3070_1),
194     RUN_DEV(GIGASET,            RT3070_2),
195     RUN_DEV(GUILLEMOT,          HWNU300),
196     RUN_DEV(HAWKING,            HWUN2),
197     RUN_DEV(HAWKING,            RT2870_1),
198     RUN_DEV(HAWKING,            RT2870_2),
199     RUN_DEV(HAWKING,            RT3070),
200     RUN_DEV(IODATA,             RT3072_1),
201     RUN_DEV(IODATA,             RT3072_2),
202     RUN_DEV(IODATA,             RT3072_3),
203     RUN_DEV(IODATA,             RT3072_4),
204     RUN_DEV(LINKSYS4,           RT3070),
205     RUN_DEV(LINKSYS4,           WUSB100),
206     RUN_DEV(LINKSYS4,           WUSB54GCV3),
207     RUN_DEV(LINKSYS4,           WUSB600N),
208     RUN_DEV(LINKSYS4,           WUSB600NV2),
209     RUN_DEV(LOGITEC,            RT2870_1),
210     RUN_DEV(LOGITEC,            RT2870_2),
211     RUN_DEV(LOGITEC,            RT2870_3),
212     RUN_DEV(LOGITEC,            LANW300NU2),
213     RUN_DEV(LOGITEC,            LANW300NU2S),
214     RUN_DEV(MELCO,              RT2870_1),
215     RUN_DEV(MELCO,              RT2870_2),
216     RUN_DEV(MELCO,              WLIUCAG300N),
217     RUN_DEV(MELCO,              WLIUCG300N),
218     RUN_DEV(MELCO,              WLIUCG301N),
219     RUN_DEV(MELCO,              WLIUCGN),
220     RUN_DEV(MELCO,              WLIUCGNM),
221     RUN_DEV(MOTOROLA4,          RT2770),
222     RUN_DEV(MOTOROLA4,          RT3070),
223     RUN_DEV(MSI,                RT3070_1),
224     RUN_DEV(MSI,                RT3070_2),
225     RUN_DEV(MSI,                RT3070_3),
226     RUN_DEV(MSI,                RT3070_4),
227     RUN_DEV(MSI,                RT3070_5),
228     RUN_DEV(MSI,                RT3070_6),
229     RUN_DEV(MSI,                RT3070_7),
230     RUN_DEV(MSI,                RT3070_8),
231     RUN_DEV(MSI,                RT3070_9),
232     RUN_DEV(MSI,                RT3070_10),
233     RUN_DEV(MSI,                RT3070_11),
234     RUN_DEV(OVISLINK,           RT3072),
235     RUN_DEV(PARA,               RT3070),
236     RUN_DEV(PEGATRON,           RT2870),
237     RUN_DEV(PEGATRON,           RT3070),
238     RUN_DEV(PEGATRON,           RT3070_2),
239     RUN_DEV(PEGATRON,           RT3070_3),
240     RUN_DEV(PHILIPS,            RT2870),
241     RUN_DEV(PLANEX2,            GWUS300MINIS),
242     RUN_DEV(PLANEX2,            GWUSMICRON),
243     RUN_DEV(PLANEX2,            RT2870),
244     RUN_DEV(PLANEX2,            RT3070),
245     RUN_DEV(QCOM,               RT2870),
246     RUN_DEV(QUANTA,             RT3070),
247     RUN_DEV(RALINK,             RT2070),
248     RUN_DEV(RALINK,             RT2770),
249     RUN_DEV(RALINK,             RT2870),
250     RUN_DEV(RALINK,             RT3070),
251     RUN_DEV(RALINK,             RT3071),
252     RUN_DEV(RALINK,             RT3072),
253     RUN_DEV(RALINK,             RT3370),
254     RUN_DEV(RALINK,             RT3572),
255     RUN_DEV(RALINK,             RT8070),
256     RUN_DEV(SAMSUNG,            WIS09ABGN),
257     RUN_DEV(SAMSUNG2,           RT2870_1),
258     RUN_DEV(SENAO,              RT2870_1),
259     RUN_DEV(SENAO,              RT2870_2),
260     RUN_DEV(SENAO,              RT2870_3),
261     RUN_DEV(SENAO,              RT2870_4),
262     RUN_DEV(SENAO,              RT3070),
263     RUN_DEV(SENAO,              RT3071),
264     RUN_DEV(SENAO,              RT3072_1),
265     RUN_DEV(SENAO,              RT3072_2),
266     RUN_DEV(SENAO,              RT3072_3),
267     RUN_DEV(SENAO,              RT3072_4),
268     RUN_DEV(SENAO,              RT3072_5),
269     RUN_DEV(SITECOMEU,          RT2770),
270     RUN_DEV(SITECOMEU,          RT2870_1),
271     RUN_DEV(SITECOMEU,          RT2870_2),
272     RUN_DEV(SITECOMEU,          RT2870_3),
273     RUN_DEV(SITECOMEU,          RT2870_4),
274     RUN_DEV(SITECOMEU,          RT3070),
275     RUN_DEV(SITECOMEU,          RT3070_2),
276     RUN_DEV(SITECOMEU,          RT3070_3),
277     RUN_DEV(SITECOMEU,          RT3070_4),
278     RUN_DEV(SITECOMEU,          RT3071),
279     RUN_DEV(SITECOMEU,          RT3072_1),
280     RUN_DEV(SITECOMEU,          RT3072_2),
281     RUN_DEV(SITECOMEU,          RT3072_3),
282     RUN_DEV(SITECOMEU,          RT3072_4),
283     RUN_DEV(SITECOMEU,          RT3072_5),
284     RUN_DEV(SITECOMEU,          RT3072_6),
285     RUN_DEV(SITECOMEU,          WL608),
286     RUN_DEV(SPARKLAN,           RT2870_1),
287     RUN_DEV(SPARKLAN,           RT3070),
288     RUN_DEV(SWEEX2,             LW153),
289     RUN_DEV(SWEEX2,             LW303),
290     RUN_DEV(SWEEX2,             LW313),
291     RUN_DEV(TOSHIBA,            RT3070),
292     RUN_DEV(UMEDIA,             RT2870_1),
293     RUN_DEV(ZCOM,               RT2870_1),
294     RUN_DEV(ZCOM,               RT2870_2),
295     RUN_DEV(ZINWELL,            RT2870_1),
296     RUN_DEV(ZINWELL,            RT2870_2),
297     RUN_DEV(ZINWELL,            RT3070),
298     RUN_DEV(ZINWELL,            RT3072_1),
299     RUN_DEV(ZINWELL,            RT3072_2),
300     RUN_DEV(ZYXEL,              RT2870_1),
301     RUN_DEV(ZYXEL,              RT2870_2),
302 #undef RUN_DEV
303 };
304
305 static device_probe_t   run_match;
306 static device_attach_t  run_attach;
307 static device_detach_t  run_detach;
308
309 static usb_callback_t   run_bulk_rx_callback;
310 static usb_callback_t   run_bulk_tx_callback0;
311 static usb_callback_t   run_bulk_tx_callback1;
312 static usb_callback_t   run_bulk_tx_callback2;
313 static usb_callback_t   run_bulk_tx_callback3;
314 static usb_callback_t   run_bulk_tx_callback4;
315 static usb_callback_t   run_bulk_tx_callback5;
316
317 static void     run_bulk_tx_callbackN(struct usb_xfer *xfer,
318                     usb_error_t error, unsigned int index);
319 static struct ieee80211vap *run_vap_create(struct ieee80211com *,
320                     const char name[IFNAMSIZ], int unit, int opmode, int flags,
321                     const uint8_t bssid[IEEE80211_ADDR_LEN], const uint8_t
322                     mac[IEEE80211_ADDR_LEN]);
323 static void     run_vap_delete(struct ieee80211vap *);
324 static void     run_cmdq_cb(void *, int);
325 static void     run_setup_tx_list(struct run_softc *,
326                     struct run_endpoint_queue *);
327 static void     run_unsetup_tx_list(struct run_softc *,
328                     struct run_endpoint_queue *);
329 static int      run_load_microcode(struct run_softc *);
330 static int      run_reset(struct run_softc *);
331 static usb_error_t run_do_request(struct run_softc *,
332                     struct usb_device_request *, void *);
333 static int      run_read(struct run_softc *, uint16_t, uint32_t *);
334 static int      run_read_region_1(struct run_softc *, uint16_t, uint8_t *, int);
335 static int      run_write_2(struct run_softc *, uint16_t, uint16_t);
336 static int      run_write(struct run_softc *, uint16_t, uint32_t);
337 static int      run_write_region_1(struct run_softc *, uint16_t,
338                     const uint8_t *, int);
339 static int      run_set_region_4(struct run_softc *, uint16_t, uint32_t, int);
340 static int      run_efuse_read_2(struct run_softc *, uint16_t, uint16_t *);
341 static int      run_eeprom_read_2(struct run_softc *, uint16_t, uint16_t *);
342 static int      run_rt2870_rf_write(struct run_softc *, uint8_t, uint32_t);
343 static int      run_rt3070_rf_read(struct run_softc *, uint8_t, uint8_t *);
344 static int      run_rt3070_rf_write(struct run_softc *, uint8_t, uint8_t);
345 static int      run_bbp_read(struct run_softc *, uint8_t, uint8_t *);
346 static int      run_bbp_write(struct run_softc *, uint8_t, uint8_t);
347 static int      run_mcu_cmd(struct run_softc *, uint8_t, uint16_t);
348 static const char *run_get_rf(int);
349 static int      run_read_eeprom(struct run_softc *);
350 static struct ieee80211_node *run_node_alloc(struct ieee80211vap *,
351                             const uint8_t mac[IEEE80211_ADDR_LEN]);
352 static int      run_media_change(struct ifnet *);
353 static int      run_newstate(struct ieee80211vap *, enum ieee80211_state, int);
354 static int      run_wme_update(struct ieee80211com *);
355 static void     run_wme_update_cb(void *);
356 static void     run_key_update_begin(struct ieee80211vap *);
357 static void     run_key_update_end(struct ieee80211vap *);
358 static void     run_key_set_cb(void *);
359 static int      run_key_set(struct ieee80211vap *, struct ieee80211_key *,
360                             const uint8_t mac[IEEE80211_ADDR_LEN]);
361 static void     run_key_delete_cb(void *);
362 static int      run_key_delete(struct ieee80211vap *, struct ieee80211_key *);
363 static void     run_ratectl_to(void *);
364 static void     run_ratectl_cb(void *, int);
365 static void     run_drain_fifo(void *);
366 static void     run_iter_func(void *, struct ieee80211_node *);
367 static void     run_newassoc_cb(void *);
368 static void     run_newassoc(struct ieee80211_node *, int);
369 static void     run_rx_frame(struct run_softc *, struct mbuf *, uint32_t);
370 static void     run_tx_free(struct run_endpoint_queue *pq,
371                     struct run_tx_data *, int);
372 static void     run_set_tx_desc(struct run_softc *, struct run_tx_data *);
373 static int      run_tx(struct run_softc *, struct mbuf *,
374                     struct ieee80211_node *);
375 static int      run_tx_mgt(struct run_softc *, struct mbuf *,
376                     struct ieee80211_node *);
377 static int      run_sendprot(struct run_softc *, const struct mbuf *,
378                     struct ieee80211_node *, int, int);
379 static int      run_tx_param(struct run_softc *, struct mbuf *,
380                     struct ieee80211_node *,
381                     const struct ieee80211_bpf_params *);
382 static int      run_raw_xmit(struct ieee80211_node *, struct mbuf *,
383                     const struct ieee80211_bpf_params *);
384 static void     run_start(struct ifnet *);
385 static int      run_ioctl(struct ifnet *, u_long, caddr_t);
386 static void     run_set_agc(struct run_softc *, uint8_t);
387 static void     run_select_chan_group(struct run_softc *, int);
388 static void     run_set_rx_antenna(struct run_softc *, int);
389 static void     run_rt2870_set_chan(struct run_softc *, u_int);
390 static void     run_rt3070_set_chan(struct run_softc *, u_int);
391 static void     run_rt3572_set_chan(struct run_softc *, u_int);
392 static int      run_set_chan(struct run_softc *, struct ieee80211_channel *);
393 static void     run_set_channel(struct ieee80211com *);
394 static void     run_scan_start(struct ieee80211com *);
395 static void     run_scan_end(struct ieee80211com *);
396 static void     run_update_beacon(struct ieee80211vap *, int);
397 static void     run_update_beacon_cb(void *);
398 static void     run_updateprot(struct ieee80211com *);
399 static void     run_usb_timeout_cb(void *);
400 static void     run_reset_livelock(struct run_softc *);
401 static void     run_enable_tsf_sync(struct run_softc *);
402 static void     run_enable_mrr(struct run_softc *);
403 static void     run_set_txpreamble(struct run_softc *);
404 static void     run_set_basicrates(struct run_softc *);
405 static void     run_set_leds(struct run_softc *, uint16_t);
406 static void     run_set_bssid(struct run_softc *, const uint8_t *);
407 static void     run_set_macaddr(struct run_softc *, const uint8_t *);
408 static void     run_updateslot(struct ifnet *);
409 static void     run_update_mcast(struct ifnet *);
410 static int8_t   run_rssi2dbm(struct run_softc *, uint8_t, uint8_t);
411 static void     run_update_promisc_locked(struct ifnet *);
412 static void     run_update_promisc(struct ifnet *);
413 static int      run_bbp_init(struct run_softc *);
414 static int      run_rt3070_rf_init(struct run_softc *);
415 static int      run_rt3070_filter_calib(struct run_softc *, uint8_t, uint8_t,
416                     uint8_t *);
417 static void     run_rt3070_rf_setup(struct run_softc *);
418 static int      run_txrx_enable(struct run_softc *);
419 static void     run_init(void *);
420 static void     run_init_locked(struct run_softc *);
421 static void     run_stop(void *);
422 static void     run_delay(struct run_softc *, unsigned int);
423
424 static const struct {
425         uint16_t        reg;
426         uint32_t        val;
427 } rt2870_def_mac[] = {
428         RT2870_DEF_MAC
429 };
430
431 static const struct {
432         uint8_t reg;
433         uint8_t val;
434 } rt2860_def_bbp[] = {
435         RT2860_DEF_BBP
436 };
437
438 static const struct rfprog {
439         uint8_t         chan;
440         uint32_t        r1, r2, r3, r4;
441 } rt2860_rf2850[] = {
442         RT2860_RF2850
443 };
444
445 struct {
446         uint8_t n, r, k;
447 } rt3070_freqs[] = {
448         RT3070_RF3052
449 };
450
451 static const struct {
452         uint8_t reg;
453         uint8_t val;
454 } rt3070_def_rf[] = {
455         RT3070_DEF_RF
456 },rt3572_def_rf[] = {
457         RT3572_DEF_RF
458 };
459
460 static const struct usb_config run_config[RUN_N_XFER] = {
461     [RUN_BULK_TX_BE] = {
462         .type = UE_BULK,
463         .endpoint = UE_ADDR_ANY,
464         .ep_index = 0,
465         .direction = UE_DIR_OUT,
466         .bufsize = RUN_MAX_TXSZ,
467         .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
468         .callback = run_bulk_tx_callback0,
469         .timeout = 5000,        /* ms */
470     },
471     [RUN_BULK_TX_BK] = {
472         .type = UE_BULK,
473         .endpoint = UE_ADDR_ANY,
474         .direction = UE_DIR_OUT,
475         .ep_index = 1,
476         .bufsize = RUN_MAX_TXSZ,
477         .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
478         .callback = run_bulk_tx_callback1,
479         .timeout = 5000,        /* ms */
480     },
481     [RUN_BULK_TX_VI] = {
482         .type = UE_BULK,
483         .endpoint = UE_ADDR_ANY,
484         .direction = UE_DIR_OUT,
485         .ep_index = 2,
486         .bufsize = RUN_MAX_TXSZ,
487         .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
488         .callback = run_bulk_tx_callback2,
489         .timeout = 5000,        /* ms */
490     },
491     [RUN_BULK_TX_VO] = {
492         .type = UE_BULK,
493         .endpoint = UE_ADDR_ANY,
494         .direction = UE_DIR_OUT,
495         .ep_index = 3,
496         .bufsize = RUN_MAX_TXSZ,
497         .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
498         .callback = run_bulk_tx_callback3,
499         .timeout = 5000,        /* ms */
500     },
501     [RUN_BULK_TX_HCCA] = {
502         .type = UE_BULK,
503         .endpoint = UE_ADDR_ANY,
504         .direction = UE_DIR_OUT,
505         .ep_index = 4,
506         .bufsize = RUN_MAX_TXSZ,
507         .flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
508         .callback = run_bulk_tx_callback4,
509         .timeout = 5000,        /* ms */
510     },
511     [RUN_BULK_TX_PRIO] = {
512         .type = UE_BULK,
513         .endpoint = UE_ADDR_ANY,
514         .direction = UE_DIR_OUT,
515         .ep_index = 5,
516         .bufsize = RUN_MAX_TXSZ,
517         .flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
518         .callback = run_bulk_tx_callback5,
519         .timeout = 5000,        /* ms */
520     },
521     [RUN_BULK_RX] = {
522         .type = UE_BULK,
523         .endpoint = UE_ADDR_ANY,
524         .direction = UE_DIR_IN,
525         .bufsize = RUN_MAX_RXSZ,
526         .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
527         .callback = run_bulk_rx_callback,
528     }
529 };
530
531 int
532 run_match(device_t self)
533 {
534         struct usb_attach_arg *uaa = device_get_ivars(self);
535
536         if (uaa->usb_mode != USB_MODE_HOST)
537                 return (ENXIO);
538         if (uaa->info.bConfigIndex != 0)
539                 return (ENXIO);
540         if (uaa->info.bIfaceIndex != RT2860_IFACE_INDEX)
541                 return (ENXIO);
542
543         return (usbd_lookup_id_by_uaa(run_devs, sizeof(run_devs), uaa));
544 }
545
546 static int
547 run_attach(device_t self)
548 {
549         struct run_softc *sc = device_get_softc(self);
550         struct usb_attach_arg *uaa = device_get_ivars(self);
551         struct ieee80211com *ic;
552         struct ifnet *ifp;
553         uint32_t ver;
554         int i, ntries, error;
555         uint8_t iface_index, bands;
556
557         device_set_usb_desc(self);
558         sc->sc_udev = uaa->device;
559         sc->sc_dev = self;
560
561         mtx_init(&sc->sc_mtx, device_get_nameunit(sc->sc_dev),
562             MTX_NETWORK_LOCK, MTX_DEF);
563
564         iface_index = RT2860_IFACE_INDEX;
565
566         error = usbd_transfer_setup(uaa->device, &iface_index,
567             sc->sc_xfer, run_config, RUN_N_XFER, sc, &sc->sc_mtx);
568         if (error) {
569                 device_printf(self, "could not allocate USB transfers, "
570                     "err=%s\n", usbd_errstr(error));
571                 goto detach;
572         }
573
574         RUN_LOCK(sc);
575
576         /* wait for the chip to settle */
577         for (ntries = 0; ntries < 100; ntries++) {
578                 if (run_read(sc, RT2860_ASIC_VER_ID, &ver) != 0) {
579                         RUN_UNLOCK(sc);
580                         goto detach;
581                 }
582                 if (ver != 0 && ver != 0xffffffff)
583                         break;
584                 run_delay(sc, 10);
585         }
586         if (ntries == 100) {
587                 device_printf(sc->sc_dev,
588                     "timeout waiting for NIC to initialize\n");
589                 RUN_UNLOCK(sc);
590                 goto detach;
591         }
592         sc->mac_ver = ver >> 16;
593         sc->mac_rev = ver & 0xffff;
594
595         /* retrieve RF rev. no and various other things from EEPROM */
596         run_read_eeprom(sc);
597
598         device_printf(sc->sc_dev,
599             "MAC/BBP RT%04X (rev 0x%04X), RF %s (MIMO %dT%dR), address %s\n",
600             sc->mac_ver, sc->mac_rev, run_get_rf(sc->rf_rev),
601             sc->ntxchains, sc->nrxchains, ether_sprintf(sc->sc_bssid));
602
603         RUN_UNLOCK(sc);
604
605         ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
606         if(ifp == NULL){
607                 device_printf(sc->sc_dev, "can not if_alloc()\n");
608                 goto detach;
609         }
610         ic = ifp->if_l2com;
611
612         ifp->if_softc = sc;
613         if_initname(ifp, "run", device_get_unit(sc->sc_dev));
614         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
615         ifp->if_init = run_init;
616         ifp->if_ioctl = run_ioctl;
617         ifp->if_start = run_start;
618         IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
619         ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
620         IFQ_SET_READY(&ifp->if_snd);
621
622         ic->ic_ifp = ifp;
623         ic->ic_phytype = IEEE80211_T_OFDM;      /* not only, but not used */
624         ic->ic_opmode = IEEE80211_M_STA;        /* default to BSS mode */
625
626         /* set device capabilities */
627         ic->ic_caps =
628             IEEE80211_C_STA |           /* station mode supported */
629             IEEE80211_C_MONITOR |       /* monitor mode supported */
630             IEEE80211_C_IBSS |
631             IEEE80211_C_HOSTAP |
632             IEEE80211_C_WDS |           /* 4-address traffic works */
633             IEEE80211_C_MBSS |
634             IEEE80211_C_SHPREAMBLE |    /* short preamble supported */
635             IEEE80211_C_SHSLOT |        /* short slot time supported */
636             IEEE80211_C_WME |           /* WME */
637             IEEE80211_C_WPA;            /* WPA1|WPA2(RSN) */
638
639         ic->ic_cryptocaps =
640             IEEE80211_CRYPTO_WEP |
641             IEEE80211_CRYPTO_AES_CCM |
642             IEEE80211_CRYPTO_TKIPMIC |
643             IEEE80211_CRYPTO_TKIP;
644
645         ic->ic_flags |= IEEE80211_F_DATAPAD;
646         ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS;
647
648         bands = 0;
649         setbit(&bands, IEEE80211_MODE_11B);
650         setbit(&bands, IEEE80211_MODE_11G);
651         ieee80211_init_channels(ic, NULL, &bands);
652
653         /*
654          * Do this by own because h/w supports
655          * more channels than ieee80211_init_channels()
656          */
657         if (sc->rf_rev == RT2860_RF_2750 ||
658             sc->rf_rev == RT2860_RF_2850 ||
659             sc->rf_rev == RT3070_RF_3052) {
660                 /* set supported .11a rates */
661                 for (i = 14; i < N(rt2860_rf2850); i++) {
662                         uint8_t chan = rt2860_rf2850[i].chan;
663                         ic->ic_channels[ic->ic_nchans].ic_freq =
664                             ieee80211_ieee2mhz(chan, IEEE80211_CHAN_A);
665                         ic->ic_channels[ic->ic_nchans].ic_ieee = chan;
666                         ic->ic_channels[ic->ic_nchans].ic_flags = IEEE80211_CHAN_A;
667                         ic->ic_channels[ic->ic_nchans].ic_extieee = 0;
668                         ic->ic_nchans++;
669                 }
670         }
671
672         ieee80211_ifattach(ic, sc->sc_bssid);
673
674         ic->ic_scan_start = run_scan_start;
675         ic->ic_scan_end = run_scan_end;
676         ic->ic_set_channel = run_set_channel;
677         ic->ic_node_alloc = run_node_alloc;
678         ic->ic_newassoc = run_newassoc;
679         //ic->ic_updateslot = run_updateslot;
680         ic->ic_update_mcast = run_update_mcast;
681         ic->ic_wme.wme_update = run_wme_update;
682         ic->ic_raw_xmit = run_raw_xmit;
683         ic->ic_update_promisc = run_update_promisc;
684
685         ic->ic_vap_create = run_vap_create;
686         ic->ic_vap_delete = run_vap_delete;
687
688         ieee80211_radiotap_attach(ic,
689             &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
690                 RUN_TX_RADIOTAP_PRESENT,
691             &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
692                 RUN_RX_RADIOTAP_PRESENT);
693
694         TASK_INIT(&sc->cmdq_task, 0, run_cmdq_cb, sc);
695         TASK_INIT(&sc->ratectl_task, 0, run_ratectl_cb, sc);
696         callout_init((struct callout *)&sc->ratectl_ch, 1);
697
698         if (bootverbose)
699                 ieee80211_announce(ic);
700
701         return (0);
702
703 detach:
704         run_detach(self);
705         return (ENXIO);
706 }
707
708 static int
709 run_detach(device_t self)
710 {
711         struct run_softc *sc = device_get_softc(self);
712         struct ifnet *ifp = sc->sc_ifp;
713         struct ieee80211com *ic;
714         int i;
715
716         /* stop all USB transfers */
717         usbd_transfer_unsetup(sc->sc_xfer, RUN_N_XFER);
718
719         RUN_LOCK(sc);
720
721         sc->ratectl_run = RUN_RATECTL_OFF;
722         sc->cmdq_run = sc->cmdq_key_set = RUN_CMDQ_ABORT;
723
724         /* free TX list, if any */
725         for (i = 0; i != RUN_EP_QUEUES; i++)
726                 run_unsetup_tx_list(sc, &sc->sc_epq[i]);
727         RUN_UNLOCK(sc);
728
729         if (ifp) {
730                 ic = ifp->if_l2com;
731                 /* drain tasks */
732                 usb_callout_drain(&sc->ratectl_ch);
733                 ieee80211_draintask(ic, &sc->cmdq_task);
734                 ieee80211_draintask(ic, &sc->ratectl_task);
735                 ieee80211_ifdetach(ic);
736                 if_free(ifp);
737         }
738
739         mtx_destroy(&sc->sc_mtx);
740
741         return (0);
742 }
743
744 static struct ieee80211vap *
745 run_vap_create(struct ieee80211com *ic,
746     const char name[IFNAMSIZ], int unit, int opmode, int flags,
747     const uint8_t bssid[IEEE80211_ADDR_LEN],
748     const uint8_t mac[IEEE80211_ADDR_LEN])
749 {
750         struct ifnet *ifp = ic->ic_ifp;
751         struct run_softc *sc = ifp->if_softc;
752         struct run_vap *rvp;
753         struct ieee80211vap *vap;
754         int i;
755
756         if (sc->rvp_cnt >= RUN_VAP_MAX) {
757                 if_printf(ifp, "number of VAPs maxed out\n");
758                 return (NULL);
759         }
760
761         switch (opmode) {
762         case IEEE80211_M_STA:
763                 /* enable s/w bmiss handling for sta mode */
764                 flags |= IEEE80211_CLONE_NOBEACONS; 
765                 /* fall though */
766         case IEEE80211_M_IBSS:
767         case IEEE80211_M_MONITOR:
768         case IEEE80211_M_HOSTAP:
769         case IEEE80211_M_MBSS:
770                 /* other than WDS vaps, only one at a time */
771                 if (!TAILQ_EMPTY(&ic->ic_vaps))
772                         return (NULL);
773                 break;
774         case IEEE80211_M_WDS:
775                 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next){
776                         if(vap->iv_opmode != IEEE80211_M_HOSTAP)
777                                 continue;
778                         /* WDS vap's always share the local mac address. */
779                         flags &= ~IEEE80211_CLONE_BSSID;
780                         break;
781                 }
782                 if (vap == NULL) {
783                         if_printf(ifp, "wds only supported in ap mode\n");
784                         return (NULL);
785                 }
786                 break;
787         default:
788                 if_printf(ifp, "unknown opmode %d\n", opmode);
789                 return (NULL);
790         }
791
792         rvp = (struct run_vap *) malloc(sizeof(struct run_vap),
793             M_80211_VAP, M_NOWAIT | M_ZERO);
794         if (rvp == NULL)
795                 return (NULL);
796         vap = &rvp->vap;
797         ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac);
798
799         vap->iv_key_update_begin = run_key_update_begin;
800         vap->iv_key_update_end = run_key_update_end;
801         vap->iv_update_beacon = run_update_beacon;
802         vap->iv_max_aid = RT2870_WCID_MAX;
803         /*
804          * To delete the right key from h/w, we need wcid.
805          * Luckily, there is unused space in ieee80211_key{}, wk_pad,
806          * and matching wcid will be written into there. So, cast
807          * some spells to remove 'const' from ieee80211_key{}
808          */
809         vap->iv_key_delete = (void *)run_key_delete;
810         vap->iv_key_set = (void *)run_key_set;
811
812         /* override state transition machine */
813         rvp->newstate = vap->iv_newstate;
814         vap->iv_newstate = run_newstate;
815
816         ieee80211_ratectl_init(vap);
817         ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */);
818
819         /* complete setup */
820         ieee80211_vap_attach(vap, run_media_change, ieee80211_media_status);
821
822         /* make sure id is always unique */
823         for (i = 0; i < RUN_VAP_MAX; i++) {
824                 if((sc->rvp_bmap & 1 << i) == 0){
825                         sc->rvp_bmap |= 1 << i;
826                         rvp->rvp_id = i;
827                         break;
828                 }
829         }
830         if (sc->rvp_cnt++ == 0)
831                 ic->ic_opmode = opmode;
832
833         if (opmode == IEEE80211_M_HOSTAP)
834                 sc->cmdq_run = RUN_CMDQ_GO;
835
836         DPRINTF("rvp_id=%d bmap=%x rvp_cnt=%d\n",
837             rvp->rvp_id, sc->rvp_bmap, sc->rvp_cnt);
838
839         return (vap);
840 }
841
842 static void
843 run_vap_delete(struct ieee80211vap *vap)
844 {
845         struct run_vap *rvp = RUN_VAP(vap);
846         struct ifnet *ifp;
847         struct ieee80211com *ic;
848         struct run_softc *sc;
849         uint8_t rvp_id;
850
851         if (vap == NULL)
852                 return;
853
854         ic = vap->iv_ic;
855         ifp = ic->ic_ifp;
856
857         sc = ifp->if_softc;
858
859         RUN_LOCK(sc);
860
861         rvp_id = rvp->rvp_id;
862         sc->ratectl_run &= ~(1 << rvp_id);
863         sc->rvp_bmap &= ~(1 << rvp_id);
864         run_set_region_4(sc, RT2860_SKEY(rvp_id, 0), 0, 128);
865         run_set_region_4(sc, RT2860_BCN_BASE(rvp_id), 0, 512);
866         --sc->rvp_cnt;
867
868         DPRINTF("vap=%p rvp_id=%d bmap=%x rvp_cnt=%d\n",
869             vap, rvp_id, sc->rvp_bmap, sc->rvp_cnt);
870
871         RUN_UNLOCK(sc);
872
873         ieee80211_ratectl_deinit(vap);
874         ieee80211_vap_detach(vap);
875         free(rvp, M_80211_VAP);
876 }
877
878 /*
879  * There are numbers of functions need to be called in context thread.
880  * Rather than creating taskqueue event for each of those functions,
881  * here is all-for-one taskqueue callback function. This function
882  * gurantees deferred functions are executed in the same order they
883  * were enqueued.
884  * '& RUN_CMDQ_MASQ' is to loop cmdq[].
885  */
886 static void
887 run_cmdq_cb(void *arg, int pending)
888 {
889         struct run_softc *sc = arg;
890         uint8_t i;
891
892         /* call cmdq[].func locked */
893         RUN_LOCK(sc);
894         for (i = sc->cmdq_exec; sc->cmdq[i].func && pending;
895             i = sc->cmdq_exec, pending--) {
896                 DPRINTFN(6, "cmdq_exec=%d pending=%d\n", i, pending);
897                 if (sc->cmdq_run == RUN_CMDQ_GO) {
898                         /*
899                          * If arg0 is NULL, callback func needs more
900                          * than one arg. So, pass ptr to cmdq struct.
901                          */
902                         if (sc->cmdq[i].arg0)
903                                 sc->cmdq[i].func(sc->cmdq[i].arg0);
904                         else
905                                 sc->cmdq[i].func(&sc->cmdq[i]);
906                 }
907                 sc->cmdq[i].arg0 = NULL;
908                 sc->cmdq[i].func = NULL;
909                 sc->cmdq_exec++;
910                 sc->cmdq_exec &= RUN_CMDQ_MASQ;
911         }
912         RUN_UNLOCK(sc);
913 }
914
915 static void
916 run_setup_tx_list(struct run_softc *sc, struct run_endpoint_queue *pq)
917 {
918         struct run_tx_data *data;
919
920         memset(pq, 0, sizeof(*pq));
921
922         STAILQ_INIT(&pq->tx_qh);
923         STAILQ_INIT(&pq->tx_fh);
924
925         for (data = &pq->tx_data[0];
926             data < &pq->tx_data[RUN_TX_RING_COUNT]; data++) {
927                 data->sc = sc;
928                 STAILQ_INSERT_TAIL(&pq->tx_fh, data, next);
929         }
930         pq->tx_nfree = RUN_TX_RING_COUNT;
931 }
932
933 static void
934 run_unsetup_tx_list(struct run_softc *sc, struct run_endpoint_queue *pq)
935 {
936         struct run_tx_data *data;
937
938         /* make sure any subsequent use of the queues will fail */
939         pq->tx_nfree = 0;
940         STAILQ_INIT(&pq->tx_fh);
941         STAILQ_INIT(&pq->tx_qh);
942
943         /* free up all node references and mbufs */
944         for (data = &pq->tx_data[0];
945             data < &pq->tx_data[RUN_TX_RING_COUNT]; data++) {
946                 if (data->m != NULL) {
947                         m_freem(data->m);
948                         data->m = NULL;
949                 }
950                 if (data->ni != NULL) {
951                         ieee80211_free_node(data->ni);
952                         data->ni = NULL;
953                 }
954         }
955 }
956
957 int
958 run_load_microcode(struct run_softc *sc)
959 {
960         usb_device_request_t req;
961         const struct firmware *fw;
962         const u_char *base;
963         uint32_t tmp;
964         int ntries, error;
965         const uint64_t *temp;
966         uint64_t bytes;
967
968         RUN_UNLOCK(sc);
969         fw = firmware_get("runfw");
970         RUN_LOCK(sc);
971         if (fw == NULL) {
972                 device_printf(sc->sc_dev,
973                     "failed loadfirmware of file %s\n", "runfw");
974                 return ENOENT;
975         }
976
977         if (fw->datasize != 8192) {
978                 device_printf(sc->sc_dev,
979                     "invalid firmware size (should be 8KB)\n");
980                 error = EINVAL;
981                 goto fail;
982         }
983
984         /*
985          * RT3071/RT3072 use a different firmware
986          * run-rt2870 (8KB) contains both,
987          * first half (4KB) is for rt2870,
988          * last half is for rt3071.
989          */
990         base = fw->data;
991         if ((sc->mac_ver) != 0x2860 &&
992             (sc->mac_ver) != 0x2872 &&
993             (sc->mac_ver) != 0x3070) { 
994                 base += 4096;
995         }
996
997         /* cheap sanity check */
998         temp = fw->data;
999         bytes = *temp;
1000         if (bytes != be64toh(0xffffff0210280210)) {
1001                 device_printf(sc->sc_dev, "firmware checksum failed\n");
1002                 error = EINVAL;
1003                 goto fail;
1004         }
1005
1006         run_read(sc, RT2860_ASIC_VER_ID, &tmp);
1007         /* write microcode image */
1008         run_write_region_1(sc, RT2870_FW_BASE, base, 4096);
1009         run_write(sc, RT2860_H2M_MAILBOX_CID, 0xffffffff);
1010         run_write(sc, RT2860_H2M_MAILBOX_STATUS, 0xffffffff);
1011
1012         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1013         req.bRequest = RT2870_RESET;
1014         USETW(req.wValue, 8);
1015         USETW(req.wIndex, 0);
1016         USETW(req.wLength, 0);
1017         if ((error = usbd_do_request(sc->sc_udev, &sc->sc_mtx, &req, NULL)) != 0) {
1018                 device_printf(sc->sc_dev, "firmware reset failed\n");
1019                 goto fail;
1020         }
1021
1022         run_delay(sc, 10);
1023
1024         run_write(sc, RT2860_H2M_MAILBOX, 0);
1025         if ((error = run_mcu_cmd(sc, RT2860_MCU_CMD_RFRESET, 0)) != 0)
1026                 goto fail;
1027
1028         /* wait until microcontroller is ready */
1029         for (ntries = 0; ntries < 1000; ntries++) {
1030                 if ((error = run_read(sc, RT2860_SYS_CTRL, &tmp)) != 0) {
1031                         goto fail;
1032                 }
1033                 if (tmp & RT2860_MCU_READY)
1034                         break;
1035                 run_delay(sc, 10);
1036         }
1037         if (ntries == 1000) {
1038                 device_printf(sc->sc_dev,
1039                     "timeout waiting for MCU to initialize\n");
1040                 error = ETIMEDOUT;
1041                 goto fail;
1042         }
1043         device_printf(sc->sc_dev, "firmware %s ver. %u.%u loaded\n",
1044             (base == fw->data) ? "RT2870" : "RT3071",
1045             *(base + 4092), *(base + 4093));
1046
1047 fail:
1048         firmware_put(fw, FIRMWARE_UNLOAD);
1049         return (error);
1050 }
1051
1052 int
1053 run_reset(struct run_softc *sc)
1054 {
1055         usb_device_request_t req;
1056
1057         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1058         req.bRequest = RT2870_RESET;
1059         USETW(req.wValue, 1);
1060         USETW(req.wIndex, 0);
1061         USETW(req.wLength, 0);
1062         return (usbd_do_request(sc->sc_udev, &sc->sc_mtx, &req, NULL));
1063 }
1064
1065 static usb_error_t
1066 run_do_request(struct run_softc *sc,
1067     struct usb_device_request *req, void *data)
1068 {
1069         usb_error_t err;
1070         int ntries = 10;
1071
1072         RUN_LOCK_ASSERT(sc, MA_OWNED);
1073
1074         while (ntries--) {
1075                 err = usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
1076                     req, data, 0, NULL, 250 /* ms */);
1077                 if (err == 0)
1078                         break;
1079                 DPRINTFN(1, "Control request failed, %s (retrying)\n",
1080                     usbd_errstr(err));
1081                 run_delay(sc, 10);
1082         }
1083         return (err);
1084 }
1085
1086 static int
1087 run_read(struct run_softc *sc, uint16_t reg, uint32_t *val)
1088 {
1089         uint32_t tmp;
1090         int error;
1091
1092         error = run_read_region_1(sc, reg, (uint8_t *)&tmp, sizeof tmp);
1093         if (error == 0)
1094                 *val = le32toh(tmp);
1095         else
1096                 *val = 0xffffffff;
1097         return (error);
1098 }
1099
1100 static int
1101 run_read_region_1(struct run_softc *sc, uint16_t reg, uint8_t *buf, int len)
1102 {
1103         usb_device_request_t req;
1104
1105         req.bmRequestType = UT_READ_VENDOR_DEVICE;
1106         req.bRequest = RT2870_READ_REGION_1;
1107         USETW(req.wValue, 0);
1108         USETW(req.wIndex, reg);
1109         USETW(req.wLength, len);
1110
1111         return (run_do_request(sc, &req, buf));
1112 }
1113
1114 static int
1115 run_write_2(struct run_softc *sc, uint16_t reg, uint16_t val)
1116 {
1117         usb_device_request_t req;
1118
1119         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1120         req.bRequest = RT2870_WRITE_2;
1121         USETW(req.wValue, val);
1122         USETW(req.wIndex, reg);
1123         USETW(req.wLength, 0);
1124
1125         return (run_do_request(sc, &req, NULL));
1126 }
1127
1128 static int
1129 run_write(struct run_softc *sc, uint16_t reg, uint32_t val)
1130 {
1131         int error;
1132
1133         if ((error = run_write_2(sc, reg, val & 0xffff)) == 0)
1134                 error = run_write_2(sc, reg + 2, val >> 16);
1135         return (error);
1136 }
1137
1138 static int
1139 run_write_region_1(struct run_softc *sc, uint16_t reg, const uint8_t *buf,
1140     int len)
1141 {
1142 #if 1
1143         int i, error = 0;
1144         /*
1145          * NB: the WRITE_REGION_1 command is not stable on RT2860.
1146          * We thus issue multiple WRITE_2 commands instead.
1147          */
1148         KASSERT((len & 1) == 0, ("run_write_region_1: Data too long.\n"));
1149         for (i = 0; i < len && error == 0; i += 2)
1150                 error = run_write_2(sc, reg + i, buf[i] | buf[i + 1] << 8);
1151         return (error);
1152 #else
1153         usb_device_request_t req;
1154
1155         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1156         req.bRequest = RT2870_WRITE_REGION_1;
1157         USETW(req.wValue, 0);
1158         USETW(req.wIndex, reg);
1159         USETW(req.wLength, len);
1160         return (run_do_request(sc, &req, buf));
1161 #endif
1162 }
1163
1164 static int
1165 run_set_region_4(struct run_softc *sc, uint16_t reg, uint32_t val, int len)
1166 {
1167         int i, error = 0;
1168
1169         KASSERT((len & 3) == 0, ("run_set_region_4: Invalid data length.\n"));
1170         for (i = 0; i < len && error == 0; i += 4)
1171                 error = run_write(sc, reg + i, val);
1172         return (error);
1173 }
1174
1175 /* Read 16-bit from eFUSE ROM (RT3070 only.) */
1176 static int
1177 run_efuse_read_2(struct run_softc *sc, uint16_t addr, uint16_t *val)
1178 {
1179         uint32_t tmp;
1180         uint16_t reg;
1181         int error, ntries;
1182
1183         if ((error = run_read(sc, RT3070_EFUSE_CTRL, &tmp)) != 0)
1184                 return (error);
1185
1186         addr *= 2;
1187         /*-
1188          * Read one 16-byte block into registers EFUSE_DATA[0-3]:
1189          * DATA0: F E D C
1190          * DATA1: B A 9 8
1191          * DATA2: 7 6 5 4
1192          * DATA3: 3 2 1 0
1193          */
1194         tmp &= ~(RT3070_EFSROM_MODE_MASK | RT3070_EFSROM_AIN_MASK);
1195         tmp |= (addr & ~0xf) << RT3070_EFSROM_AIN_SHIFT | RT3070_EFSROM_KICK;
1196         run_write(sc, RT3070_EFUSE_CTRL, tmp);
1197         for (ntries = 0; ntries < 100; ntries++) {
1198                 if ((error = run_read(sc, RT3070_EFUSE_CTRL, &tmp)) != 0)
1199                         return (error);
1200                 if (!(tmp & RT3070_EFSROM_KICK))
1201                         break;
1202                 run_delay(sc, 2);
1203         }
1204         if (ntries == 100)
1205                 return (ETIMEDOUT);
1206
1207         if ((tmp & RT3070_EFUSE_AOUT_MASK) == RT3070_EFUSE_AOUT_MASK) {
1208                 *val = 0xffff;  /* address not found */
1209                 return (0);
1210         }
1211         /* determine to which 32-bit register our 16-bit word belongs */
1212         reg = RT3070_EFUSE_DATA3 - (addr & 0xc);
1213         if ((error = run_read(sc, reg, &tmp)) != 0)
1214                 return (error);
1215
1216         *val = (addr & 2) ? tmp >> 16 : tmp & 0xffff;
1217         return (0);
1218 }
1219
1220 static int
1221 run_eeprom_read_2(struct run_softc *sc, uint16_t addr, uint16_t *val)
1222 {
1223         usb_device_request_t req;
1224         uint16_t tmp;
1225         int error;
1226
1227         addr *= 2;
1228         req.bmRequestType = UT_READ_VENDOR_DEVICE;
1229         req.bRequest = RT2870_EEPROM_READ;
1230         USETW(req.wValue, 0);
1231         USETW(req.wIndex, addr);
1232         USETW(req.wLength, sizeof tmp);
1233
1234         error = usbd_do_request(sc->sc_udev, &sc->sc_mtx, &req, &tmp);
1235         if (error == 0)
1236                 *val = le16toh(tmp);
1237         else
1238                 *val = 0xffff;
1239         return (error);
1240 }
1241
1242 static __inline int
1243 run_srom_read(struct run_softc *sc, uint16_t addr, uint16_t *val)
1244 {
1245         /* either eFUSE ROM or EEPROM */
1246         return sc->sc_srom_read(sc, addr, val);
1247 }
1248
1249 static int
1250 run_rt2870_rf_write(struct run_softc *sc, uint8_t reg, uint32_t val)
1251 {
1252         uint32_t tmp;
1253         int error, ntries;
1254
1255         for (ntries = 0; ntries < 10; ntries++) {
1256                 if ((error = run_read(sc, RT2860_RF_CSR_CFG0, &tmp)) != 0)
1257                         return (error);
1258                 if (!(tmp & RT2860_RF_REG_CTRL))
1259                         break;
1260         }
1261         if (ntries == 10)
1262                 return (ETIMEDOUT);
1263
1264         /* RF registers are 24-bit on the RT2860 */
1265         tmp = RT2860_RF_REG_CTRL | 24 << RT2860_RF_REG_WIDTH_SHIFT |
1266             (val & 0x3fffff) << 2 | (reg & 3);
1267         return (run_write(sc, RT2860_RF_CSR_CFG0, tmp));
1268 }
1269
1270 static int
1271 run_rt3070_rf_read(struct run_softc *sc, uint8_t reg, uint8_t *val)
1272 {
1273         uint32_t tmp;
1274         int error, ntries;
1275
1276         for (ntries = 0; ntries < 100; ntries++) {
1277                 if ((error = run_read(sc, RT3070_RF_CSR_CFG, &tmp)) != 0)
1278                         return (error);
1279                 if (!(tmp & RT3070_RF_KICK))
1280                         break;
1281         }
1282         if (ntries == 100)
1283                 return (ETIMEDOUT);
1284
1285         tmp = RT3070_RF_KICK | reg << 8;
1286         if ((error = run_write(sc, RT3070_RF_CSR_CFG, tmp)) != 0)
1287                 return (error);
1288
1289         for (ntries = 0; ntries < 100; ntries++) {
1290                 if ((error = run_read(sc, RT3070_RF_CSR_CFG, &tmp)) != 0)
1291                         return (error);
1292                 if (!(tmp & RT3070_RF_KICK))
1293                         break;
1294         }
1295         if (ntries == 100)
1296                 return (ETIMEDOUT);
1297
1298         *val = tmp & 0xff;
1299         return (0);
1300 }
1301
1302 static int
1303 run_rt3070_rf_write(struct run_softc *sc, uint8_t reg, uint8_t val)
1304 {
1305         uint32_t tmp;
1306         int error, ntries;
1307
1308         for (ntries = 0; ntries < 10; ntries++) {
1309                 if ((error = run_read(sc, RT3070_RF_CSR_CFG, &tmp)) != 0)
1310                         return (error);
1311                 if (!(tmp & RT3070_RF_KICK))
1312                         break;
1313         }
1314         if (ntries == 10)
1315                 return (ETIMEDOUT);
1316
1317         tmp = RT3070_RF_WRITE | RT3070_RF_KICK | reg << 8 | val;
1318         return (run_write(sc, RT3070_RF_CSR_CFG, tmp));
1319 }
1320
1321 static int
1322 run_bbp_read(struct run_softc *sc, uint8_t reg, uint8_t *val)
1323 {
1324         uint32_t tmp;
1325         int ntries, error;
1326
1327         for (ntries = 0; ntries < 10; ntries++) {
1328                 if ((error = run_read(sc, RT2860_BBP_CSR_CFG, &tmp)) != 0)
1329                         return (error);
1330                 if (!(tmp & RT2860_BBP_CSR_KICK))
1331                         break;
1332         }
1333         if (ntries == 10)
1334                 return (ETIMEDOUT);
1335
1336         tmp = RT2860_BBP_CSR_READ | RT2860_BBP_CSR_KICK | reg << 8;
1337         if ((error = run_write(sc, RT2860_BBP_CSR_CFG, tmp)) != 0)
1338                 return (error);
1339
1340         for (ntries = 0; ntries < 10; ntries++) {
1341                 if ((error = run_read(sc, RT2860_BBP_CSR_CFG, &tmp)) != 0)
1342                         return (error);
1343                 if (!(tmp & RT2860_BBP_CSR_KICK))
1344                         break;
1345         }
1346         if (ntries == 10)
1347                 return (ETIMEDOUT);
1348
1349         *val = tmp & 0xff;
1350         return (0);
1351 }
1352
1353 static int
1354 run_bbp_write(struct run_softc *sc, uint8_t reg, uint8_t val)
1355 {
1356         uint32_t tmp;
1357         int ntries, error;
1358
1359         for (ntries = 0; ntries < 10; ntries++) {
1360                 if ((error = run_read(sc, RT2860_BBP_CSR_CFG, &tmp)) != 0)
1361                         return (error);
1362                 if (!(tmp & RT2860_BBP_CSR_KICK))
1363                         break;
1364         }
1365         if (ntries == 10)
1366                 return (ETIMEDOUT);
1367
1368         tmp = RT2860_BBP_CSR_KICK | reg << 8 | val;
1369         return (run_write(sc, RT2860_BBP_CSR_CFG, tmp));
1370 }
1371
1372 /*
1373  * Send a command to the 8051 microcontroller unit.
1374  */
1375 static int
1376 run_mcu_cmd(struct run_softc *sc, uint8_t cmd, uint16_t arg)
1377 {
1378         uint32_t tmp;
1379         int error, ntries;
1380
1381         for (ntries = 0; ntries < 100; ntries++) {
1382                 if ((error = run_read(sc, RT2860_H2M_MAILBOX, &tmp)) != 0)
1383                         return error;
1384                 if (!(tmp & RT2860_H2M_BUSY))
1385                         break;
1386         }
1387         if (ntries == 100)
1388                 return ETIMEDOUT;
1389
1390         tmp = RT2860_H2M_BUSY | RT2860_TOKEN_NO_INTR << 16 | arg;
1391         if ((error = run_write(sc, RT2860_H2M_MAILBOX, tmp)) == 0)
1392                 error = run_write(sc, RT2860_HOST_CMD, cmd);
1393         return (error);
1394 }
1395
1396 /*
1397  * Add `delta' (signed) to each 4-bit sub-word of a 32-bit word.
1398  * Used to adjust per-rate Tx power registers.
1399  */
1400 static __inline uint32_t
1401 b4inc(uint32_t b32, int8_t delta)
1402 {
1403         int8_t i, b4;
1404
1405         for (i = 0; i < 8; i++) {
1406                 b4 = b32 & 0xf;
1407                 b4 += delta;
1408                 if (b4 < 0)
1409                         b4 = 0;
1410                 else if (b4 > 0xf)
1411                         b4 = 0xf;
1412                 b32 = b32 >> 4 | b4 << 28;
1413         }
1414         return (b32);
1415 }
1416
1417 static const char *
1418 run_get_rf(int rev)
1419 {
1420         switch (rev) {
1421         case RT2860_RF_2820:    return "RT2820";
1422         case RT2860_RF_2850:    return "RT2850";
1423         case RT2860_RF_2720:    return "RT2720";
1424         case RT2860_RF_2750:    return "RT2750";
1425         case RT3070_RF_3020:    return "RT3020";
1426         case RT3070_RF_2020:    return "RT2020";
1427         case RT3070_RF_3021:    return "RT3021";
1428         case RT3070_RF_3022:    return "RT3022";
1429         case RT3070_RF_3052:    return "RT3052";
1430         }
1431         return ("unknown");
1432 }
1433
1434 int
1435 run_read_eeprom(struct run_softc *sc)
1436 {
1437         int8_t delta_2ghz, delta_5ghz;
1438         uint32_t tmp;
1439         uint16_t val;
1440         int ridx, ant, i;
1441
1442         /* check whether the ROM is eFUSE ROM or EEPROM */
1443         sc->sc_srom_read = run_eeprom_read_2;
1444         if (sc->mac_ver >= 0x3070) {
1445                 run_read(sc, RT3070_EFUSE_CTRL, &tmp);
1446                 DPRINTF("EFUSE_CTRL=0x%08x\n", tmp);
1447                 if (tmp & RT3070_SEL_EFUSE)
1448                         sc->sc_srom_read = run_efuse_read_2;
1449         }
1450
1451         /* read ROM version */
1452         run_srom_read(sc, RT2860_EEPROM_VERSION, &val);
1453         DPRINTF("EEPROM rev=%d, FAE=%d\n", val & 0xff, val >> 8);
1454
1455         /* read MAC address */
1456         run_srom_read(sc, RT2860_EEPROM_MAC01, &val);
1457         sc->sc_bssid[0] = val & 0xff;
1458         sc->sc_bssid[1] = val >> 8;
1459         run_srom_read(sc, RT2860_EEPROM_MAC23, &val);
1460         sc->sc_bssid[2] = val & 0xff;
1461         sc->sc_bssid[3] = val >> 8;
1462         run_srom_read(sc, RT2860_EEPROM_MAC45, &val);
1463         sc->sc_bssid[4] = val & 0xff;
1464         sc->sc_bssid[5] = val >> 8;
1465
1466         /* read vender BBP settings */
1467         for (i = 0; i < 10; i++) {
1468                 run_srom_read(sc, RT2860_EEPROM_BBP_BASE + i, &val);
1469                 sc->bbp[i].val = val & 0xff;
1470                 sc->bbp[i].reg = val >> 8;
1471                 DPRINTF("BBP%d=0x%02x\n", sc->bbp[i].reg, sc->bbp[i].val);
1472         }
1473         if (sc->mac_ver >= 0x3071) {
1474                 /* read vendor RF settings */
1475                 for (i = 0; i < 10; i++) {
1476                         run_srom_read(sc, RT3071_EEPROM_RF_BASE + i, &val);
1477                         sc->rf[i].val = val & 0xff;
1478                         sc->rf[i].reg = val >> 8;
1479                         DPRINTF("RF%d=0x%02x\n", sc->rf[i].reg,
1480                             sc->rf[i].val);
1481                 }
1482         }
1483
1484         /* read RF frequency offset from EEPROM */
1485         run_srom_read(sc, RT2860_EEPROM_FREQ_LEDS, &val);
1486         sc->freq = ((val & 0xff) != 0xff) ? val & 0xff : 0;
1487         DPRINTF("EEPROM freq offset %d\n", sc->freq & 0xff);
1488
1489         if (val >> 8 != 0xff) {
1490                 /* read LEDs operating mode */
1491                 sc->leds = val >> 8;
1492                 run_srom_read(sc, RT2860_EEPROM_LED1, &sc->led[0]);
1493                 run_srom_read(sc, RT2860_EEPROM_LED2, &sc->led[1]);
1494                 run_srom_read(sc, RT2860_EEPROM_LED3, &sc->led[2]);
1495         } else {
1496                 /* broken EEPROM, use default settings */
1497                 sc->leds = 0x01;
1498                 sc->led[0] = 0x5555;
1499                 sc->led[1] = 0x2221;
1500                 sc->led[2] = 0x5627;    /* differs from RT2860 */
1501         }
1502         DPRINTF("EEPROM LED mode=0x%02x, LEDs=0x%04x/0x%04x/0x%04x\n",
1503             sc->leds, sc->led[0], sc->led[1], sc->led[2]);
1504
1505         /* read RF information */
1506         run_srom_read(sc, RT2860_EEPROM_ANTENNA, &val);
1507         if (val == 0xffff) {
1508                 DPRINTF("invalid EEPROM antenna info, using default\n");
1509                 if (sc->mac_ver == 0x3572) {
1510                         /* default to RF3052 2T2R */
1511                         sc->rf_rev = RT3070_RF_3052;
1512                         sc->ntxchains = 2;
1513                         sc->nrxchains = 2;
1514                 } else if (sc->mac_ver >= 0x3070) {
1515                         /* default to RF3020 1T1R */
1516                         sc->rf_rev = RT3070_RF_3020;
1517                         sc->ntxchains = 1;
1518                         sc->nrxchains = 1;
1519                 } else {
1520                         /* default to RF2820 1T2R */
1521                         sc->rf_rev = RT2860_RF_2820;
1522                         sc->ntxchains = 1;
1523                         sc->nrxchains = 2;
1524                 }
1525         } else {
1526                 sc->rf_rev = (val >> 8) & 0xf;
1527                 sc->ntxchains = (val >> 4) & 0xf;
1528                 sc->nrxchains = val & 0xf;
1529         }
1530         DPRINTF("EEPROM RF rev=0x%02x chains=%dT%dR\n",
1531             sc->rf_rev, sc->ntxchains, sc->nrxchains);
1532
1533         /* check if RF supports automatic Tx access gain control */
1534         run_srom_read(sc, RT2860_EEPROM_CONFIG, &val);
1535         DPRINTF("EEPROM CFG 0x%04x\n", val);
1536         /* check if driver should patch the DAC issue */
1537         if ((val >> 8) != 0xff)
1538                 sc->patch_dac = (val >> 15) & 1;
1539         if ((val & 0xff) != 0xff) {
1540                 sc->ext_5ghz_lna = (val >> 3) & 1;
1541                 sc->ext_2ghz_lna = (val >> 2) & 1;
1542                 /* check if RF supports automatic Tx access gain control */
1543                 sc->calib_2ghz = sc->calib_5ghz = (val >> 1) & 1;
1544                 /* check if we have a hardware radio switch */
1545                 sc->rfswitch = val & 1;
1546         }
1547
1548         /* read power settings for 2GHz channels */
1549         for (i = 0; i < 14; i += 2) {
1550                 run_srom_read(sc, RT2860_EEPROM_PWR2GHZ_BASE1 + i / 2, &val);
1551                 sc->txpow1[i + 0] = (int8_t)(val & 0xff);
1552                 sc->txpow1[i + 1] = (int8_t)(val >> 8);
1553
1554                 run_srom_read(sc, RT2860_EEPROM_PWR2GHZ_BASE2 + i / 2, &val);
1555                 sc->txpow2[i + 0] = (int8_t)(val & 0xff);
1556                 sc->txpow2[i + 1] = (int8_t)(val >> 8);
1557         }
1558         /* fix broken Tx power entries */
1559         for (i = 0; i < 14; i++) {
1560                 if (sc->txpow1[i] < 0 || sc->txpow1[i] > 31)
1561                         sc->txpow1[i] = 5;
1562                 if (sc->txpow2[i] < 0 || sc->txpow2[i] > 31)
1563                         sc->txpow2[i] = 5;
1564                 DPRINTF("chan %d: power1=%d, power2=%d\n",
1565                     rt2860_rf2850[i].chan, sc->txpow1[i], sc->txpow2[i]);
1566         }
1567         /* read power settings for 5GHz channels */
1568         for (i = 0; i < 40; i += 2) {
1569                 run_srom_read(sc, RT2860_EEPROM_PWR5GHZ_BASE1 + i / 2, &val);
1570                 sc->txpow1[i + 14] = (int8_t)(val & 0xff);
1571                 sc->txpow1[i + 15] = (int8_t)(val >> 8);
1572
1573                 run_srom_read(sc, RT2860_EEPROM_PWR5GHZ_BASE2 + i / 2, &val);
1574                 sc->txpow2[i + 14] = (int8_t)(val & 0xff);
1575                 sc->txpow2[i + 15] = (int8_t)(val >> 8);
1576         }
1577         /* fix broken Tx power entries */
1578         for (i = 0; i < 40; i++) {
1579                 if (sc->txpow1[14 + i] < -7 || sc->txpow1[14 + i] > 15)
1580                         sc->txpow1[14 + i] = 5;
1581                 if (sc->txpow2[14 + i] < -7 || sc->txpow2[14 + i] > 15)
1582                         sc->txpow2[14 + i] = 5;
1583                 DPRINTF("chan %d: power1=%d, power2=%d\n",
1584                     rt2860_rf2850[14 + i].chan, sc->txpow1[14 + i],
1585                     sc->txpow2[14 + i]);
1586         }
1587
1588         /* read Tx power compensation for each Tx rate */
1589         run_srom_read(sc, RT2860_EEPROM_DELTAPWR, &val);
1590         delta_2ghz = delta_5ghz = 0;
1591         if ((val & 0xff) != 0xff && (val & 0x80)) {
1592                 delta_2ghz = val & 0xf;
1593                 if (!(val & 0x40))      /* negative number */
1594                         delta_2ghz = -delta_2ghz;
1595         }
1596         val >>= 8;
1597         if ((val & 0xff) != 0xff && (val & 0x80)) {
1598                 delta_5ghz = val & 0xf;
1599                 if (!(val & 0x40))      /* negative number */
1600                         delta_5ghz = -delta_5ghz;
1601         }
1602         DPRINTF("power compensation=%d (2GHz), %d (5GHz)\n",
1603             delta_2ghz, delta_5ghz);
1604
1605         for (ridx = 0; ridx < 5; ridx++) {
1606                 uint32_t reg;
1607
1608                 run_srom_read(sc, RT2860_EEPROM_RPWR + ridx * 2, &val);
1609                 reg = val;
1610                 run_srom_read(sc, RT2860_EEPROM_RPWR + ridx * 2 + 1, &val);
1611                 reg |= (uint32_t)val << 16;
1612
1613                 sc->txpow20mhz[ridx] = reg;
1614                 sc->txpow40mhz_2ghz[ridx] = b4inc(reg, delta_2ghz);
1615                 sc->txpow40mhz_5ghz[ridx] = b4inc(reg, delta_5ghz);
1616
1617                 DPRINTF("ridx %d: power 20MHz=0x%08x, 40MHz/2GHz=0x%08x, "
1618                     "40MHz/5GHz=0x%08x\n", ridx, sc->txpow20mhz[ridx],
1619                     sc->txpow40mhz_2ghz[ridx], sc->txpow40mhz_5ghz[ridx]);
1620         }
1621
1622         /* read RSSI offsets and LNA gains from EEPROM */
1623         run_srom_read(sc, RT2860_EEPROM_RSSI1_2GHZ, &val);
1624         sc->rssi_2ghz[0] = val & 0xff;  /* Ant A */
1625         sc->rssi_2ghz[1] = val >> 8;    /* Ant B */
1626         run_srom_read(sc, RT2860_EEPROM_RSSI2_2GHZ, &val);
1627         if (sc->mac_ver >= 0x3070) {
1628                 /*
1629                  * On RT3070 chips (limited to 2 Rx chains), this ROM
1630                  * field contains the Tx mixer gain for the 2GHz band.
1631                  */
1632                 if ((val & 0xff) != 0xff)
1633                         sc->txmixgain_2ghz = val & 0x7;
1634                 DPRINTF("tx mixer gain=%u (2GHz)\n", sc->txmixgain_2ghz);
1635         } else
1636                 sc->rssi_2ghz[2] = val & 0xff;  /* Ant C */
1637         sc->lna[2] = val >> 8;          /* channel group 2 */
1638
1639         run_srom_read(sc, RT2860_EEPROM_RSSI1_5GHZ, &val);
1640         sc->rssi_5ghz[0] = val & 0xff;  /* Ant A */
1641         sc->rssi_5ghz[1] = val >> 8;    /* Ant B */
1642         run_srom_read(sc, RT2860_EEPROM_RSSI2_5GHZ, &val);
1643         if (sc->mac_ver == 0x3572) {
1644                 /*
1645                  * On RT3572 chips (limited to 2 Rx chains), this ROM
1646                  * field contains the Tx mixer gain for the 5GHz band.
1647                  */
1648                 if ((val & 0xff) != 0xff)
1649                         sc->txmixgain_5ghz = val & 0x7;
1650                 DPRINTF("tx mixer gain=%u (5GHz)\n", sc->txmixgain_5ghz);
1651         } else
1652                 sc->rssi_5ghz[2] = val & 0xff;  /* Ant C */
1653         sc->lna[3] = val >> 8;          /* channel group 3 */
1654
1655         run_srom_read(sc, RT2860_EEPROM_LNA, &val);
1656         sc->lna[0] = val & 0xff;        /* channel group 0 */
1657         sc->lna[1] = val >> 8;          /* channel group 1 */
1658
1659         /* fix broken 5GHz LNA entries */
1660         if (sc->lna[2] == 0 || sc->lna[2] == 0xff) {
1661                 DPRINTF("invalid LNA for channel group %d\n", 2);
1662                 sc->lna[2] = sc->lna[1];
1663         }
1664         if (sc->lna[3] == 0 || sc->lna[3] == 0xff) {
1665                 DPRINTF("invalid LNA for channel group %d\n", 3);
1666                 sc->lna[3] = sc->lna[1];
1667         }
1668
1669         /* fix broken RSSI offset entries */
1670         for (ant = 0; ant < 3; ant++) {
1671                 if (sc->rssi_2ghz[ant] < -10 || sc->rssi_2ghz[ant] > 10) {
1672                         DPRINTF("invalid RSSI%d offset: %d (2GHz)\n",
1673                             ant + 1, sc->rssi_2ghz[ant]);
1674                         sc->rssi_2ghz[ant] = 0;
1675                 }
1676                 if (sc->rssi_5ghz[ant] < -10 || sc->rssi_5ghz[ant] > 10) {
1677                         DPRINTF("invalid RSSI%d offset: %d (5GHz)\n",
1678                             ant + 1, sc->rssi_5ghz[ant]);
1679                         sc->rssi_5ghz[ant] = 0;
1680                 }
1681         }
1682         return (0);
1683 }
1684
1685 struct ieee80211_node *
1686 run_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
1687 {
1688         return malloc(sizeof (struct run_node), M_DEVBUF, M_NOWAIT | M_ZERO);
1689 }
1690
1691 static int
1692 run_media_change(struct ifnet *ifp)
1693 {
1694         struct ieee80211vap *vap = ifp->if_softc;
1695         struct ieee80211com *ic = vap->iv_ic;
1696         const struct ieee80211_txparam *tp;
1697         struct run_softc *sc = ic->ic_ifp->if_softc;
1698         uint8_t rate, ridx;
1699         int error;
1700
1701         RUN_LOCK(sc);
1702
1703         error = ieee80211_media_change(ifp);
1704         if (error != ENETRESET) {
1705                 RUN_UNLOCK(sc);
1706                 return (error);
1707         }
1708
1709         tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1710         if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) {
1711                 struct ieee80211_node *ni;
1712                 struct run_node *rn;
1713
1714                 rate = ic->ic_sup_rates[ic->ic_curmode].
1715                     rs_rates[tp->ucastrate] & IEEE80211_RATE_VAL;
1716                 for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
1717                         if (rt2860_rates[ridx].rate == rate)
1718                                 break;
1719                 ni = ieee80211_ref_node(vap->iv_bss);
1720                 rn = (struct run_node *)ni;
1721                 rn->fix_ridx = ridx;
1722                 DPRINTF("rate=%d, fix_ridx=%d\n", rate, rn->fix_ridx);
1723                 ieee80211_free_node(ni);
1724         }
1725
1726 #if 0
1727         if ((ifp->if_flags & IFF_UP) &&
1728             (ifp->if_drv_flags &  IFF_DRV_RUNNING)){
1729                 run_init_locked(sc);
1730         }
1731 #endif
1732
1733         RUN_UNLOCK(sc);
1734
1735         return (0);
1736 }
1737
1738 static int
1739 run_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1740 {
1741         const struct ieee80211_txparam *tp;
1742         struct ieee80211com *ic = vap->iv_ic;
1743         struct run_softc *sc = ic->ic_ifp->if_softc;
1744         struct run_vap *rvp = RUN_VAP(vap);
1745         enum ieee80211_state ostate;
1746         uint32_t sta[3];
1747         uint32_t tmp;
1748         uint8_t ratectl;
1749         uint8_t restart_ratectl = 0;
1750         uint8_t bid = 1 << rvp->rvp_id;
1751
1752         ostate = vap->iv_state;
1753         DPRINTF("%s -> %s\n",
1754                 ieee80211_state_name[ostate],
1755                 ieee80211_state_name[nstate]);
1756
1757         IEEE80211_UNLOCK(ic);
1758         RUN_LOCK(sc);
1759
1760         ratectl = sc->ratectl_run; /* remember current state */
1761         sc->ratectl_run = RUN_RATECTL_OFF;
1762         usb_callout_stop(&sc->ratectl_ch);
1763
1764         if (ostate == IEEE80211_S_RUN) {
1765                 /* turn link LED off */
1766                 run_set_leds(sc, RT2860_LED_RADIO);
1767         }
1768
1769         switch (nstate) {
1770         case IEEE80211_S_INIT:
1771                 restart_ratectl = 1;
1772
1773                 if (ostate != IEEE80211_S_RUN)
1774                         break;
1775
1776                 ratectl &= ~bid;
1777                 sc->runbmap &= ~bid;
1778
1779                 /* abort TSF synchronization if there is no vap running */
1780                 if (--sc->running == 0) {
1781                         run_read(sc, RT2860_BCN_TIME_CFG, &tmp);
1782                         run_write(sc, RT2860_BCN_TIME_CFG,
1783                             tmp & ~(RT2860_BCN_TX_EN | RT2860_TSF_TIMER_EN |
1784                             RT2860_TBTT_TIMER_EN));
1785                 }
1786                 break;
1787
1788
1789         case IEEE80211_S_RUN:
1790                 if (!(sc->runbmap & bid)) {
1791                         if(sc->running++)
1792                                 restart_ratectl = 1;
1793                         sc->runbmap |= bid;
1794                 }
1795
1796                 switch (vap->iv_opmode) {
1797                 case IEEE80211_M_HOSTAP:
1798                 case IEEE80211_M_MBSS:
1799                         sc->ap_running |= bid;
1800                         ic->ic_opmode = vap->iv_opmode;
1801                         run_update_beacon_cb(vap);
1802                         break;
1803                 case IEEE80211_M_IBSS:
1804                         sc->adhoc_running |= bid;
1805                         if (!sc->ap_running)
1806                                 ic->ic_opmode = vap->iv_opmode;
1807                         run_update_beacon_cb(vap);
1808                         break;
1809                 case IEEE80211_M_STA:
1810                         sc->sta_running |= bid;
1811                         if (!sc->ap_running && !sc->adhoc_running)
1812                                 ic->ic_opmode = vap->iv_opmode;
1813
1814                         /* read statistic counters (clear on read) */
1815                         run_read_region_1(sc, RT2860_TX_STA_CNT0,
1816                             (uint8_t *)sta, sizeof sta);
1817
1818                         break;
1819                 default:
1820                         ic->ic_opmode = vap->iv_opmode;
1821                         break;
1822                 }
1823
1824                 if (vap->iv_opmode != IEEE80211_M_MONITOR) {
1825                         struct ieee80211_node *ni;
1826
1827                         run_updateslot(ic->ic_ifp);
1828                         run_enable_mrr(sc);
1829                         run_set_txpreamble(sc);
1830                         run_set_basicrates(sc);
1831                         ni = ieee80211_ref_node(vap->iv_bss);
1832                         IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid);
1833                         run_set_bssid(sc, ni->ni_bssid);
1834                         ieee80211_free_node(ni);
1835                         run_enable_tsf_sync(sc);
1836
1837                         /* enable automatic rate adaptation */
1838                         tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1839                         if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
1840                                 ratectl |= bid;
1841                 }
1842
1843                 /* turn link LED on */
1844                 run_set_leds(sc, RT2860_LED_RADIO |
1845                     (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan) ?
1846                      RT2860_LED_LINK_2GHZ : RT2860_LED_LINK_5GHZ));
1847
1848                 break;
1849         default:
1850                 DPRINTFN(6, "undefined case\n");
1851                 break;
1852         }
1853
1854         /* restart amrr for running VAPs */
1855         if ((sc->ratectl_run = ratectl) && restart_ratectl)
1856                 usb_callout_reset(&sc->ratectl_ch, hz, run_ratectl_to, sc);
1857
1858         RUN_UNLOCK(sc);
1859         IEEE80211_LOCK(ic);
1860
1861         return(rvp->newstate(vap, nstate, arg));
1862 }
1863
1864 /* ARGSUSED */
1865 static void
1866 run_wme_update_cb(void *arg)
1867 {
1868         struct ieee80211com *ic = arg;
1869         struct run_softc *sc = ic->ic_ifp->if_softc;
1870         struct ieee80211_wme_state *wmesp = &ic->ic_wme;
1871         int aci, error = 0;
1872
1873         RUN_LOCK_ASSERT(sc, MA_OWNED);
1874
1875         /* update MAC TX configuration registers */
1876         for (aci = 0; aci < WME_NUM_AC; aci++) {
1877                 error = run_write(sc, RT2860_EDCA_AC_CFG(aci),
1878                     wmesp->wme_params[aci].wmep_logcwmax << 16 |
1879                     wmesp->wme_params[aci].wmep_logcwmin << 12 |
1880                     wmesp->wme_params[aci].wmep_aifsn  <<  8 |
1881                     wmesp->wme_params[aci].wmep_txopLimit);
1882                 if (error) goto err;
1883         }
1884
1885         /* update SCH/DMA registers too */
1886         error = run_write(sc, RT2860_WMM_AIFSN_CFG,
1887             wmesp->wme_params[WME_AC_VO].wmep_aifsn  << 12 |
1888             wmesp->wme_params[WME_AC_VI].wmep_aifsn  <<  8 |
1889             wmesp->wme_params[WME_AC_BK].wmep_aifsn  <<  4 |
1890             wmesp->wme_params[WME_AC_BE].wmep_aifsn);
1891         if (error) goto err;
1892         error = run_write(sc, RT2860_WMM_CWMIN_CFG,
1893             wmesp->wme_params[WME_AC_VO].wmep_logcwmin << 12 |
1894             wmesp->wme_params[WME_AC_VI].wmep_logcwmin <<  8 |
1895             wmesp->wme_params[WME_AC_BK].wmep_logcwmin <<  4 |
1896             wmesp->wme_params[WME_AC_BE].wmep_logcwmin);
1897         if (error) goto err;
1898         error = run_write(sc, RT2860_WMM_CWMAX_CFG,
1899             wmesp->wme_params[WME_AC_VO].wmep_logcwmax << 12 |
1900             wmesp->wme_params[WME_AC_VI].wmep_logcwmax <<  8 |
1901             wmesp->wme_params[WME_AC_BK].wmep_logcwmax <<  4 |
1902             wmesp->wme_params[WME_AC_BE].wmep_logcwmax);
1903         if (error) goto err;
1904         error = run_write(sc, RT2860_WMM_TXOP0_CFG,
1905             wmesp->wme_params[WME_AC_BK].wmep_txopLimit << 16 |
1906             wmesp->wme_params[WME_AC_BE].wmep_txopLimit);
1907         if (error) goto err;
1908         error = run_write(sc, RT2860_WMM_TXOP1_CFG,
1909             wmesp->wme_params[WME_AC_VO].wmep_txopLimit << 16 |
1910             wmesp->wme_params[WME_AC_VI].wmep_txopLimit);
1911
1912 err:
1913         if (error)
1914                 DPRINTF("WME update failed\n");
1915
1916         return;
1917 }
1918
1919 static int
1920 run_wme_update(struct ieee80211com *ic)
1921 {
1922         struct run_softc *sc = ic->ic_ifp->if_softc;
1923
1924         /* sometime called wothout lock */
1925         if (mtx_owned(&ic->ic_comlock.mtx)) {
1926                 uint32_t i = RUN_CMDQ_GET(&sc->cmdq_store);
1927                 DPRINTF("cmdq_store=%d\n", i);
1928                 sc->cmdq[i].func = run_wme_update_cb;
1929                 sc->cmdq[i].arg0 = ic;
1930                 ieee80211_runtask(ic, &sc->cmdq_task);
1931                 return (0);
1932         }
1933
1934         RUN_LOCK(sc);
1935         run_wme_update_cb(ic);
1936         RUN_UNLOCK(sc);
1937
1938         /* return whatever, upper layer desn't care anyway */
1939         return (0);
1940 }
1941
1942 static void
1943 run_key_update_begin(struct ieee80211vap *vap)
1944 {
1945         /*
1946          * To avoid out-of-order events, both run_key_set() and
1947          * _delete() are deferred and handled by run_cmdq_cb().
1948          * So, there is nothing we need to do here.
1949          */
1950 }
1951
1952 static void
1953 run_key_update_end(struct ieee80211vap *vap)
1954 {
1955         /* null */
1956 }
1957
1958 static void
1959 run_key_set_cb(void *arg)
1960 {
1961         struct run_cmdq *cmdq = arg;
1962         struct ieee80211vap *vap = cmdq->arg1;
1963         struct ieee80211_key *k = cmdq->k;
1964         struct ieee80211com *ic = vap->iv_ic;
1965         struct run_softc *sc = ic->ic_ifp->if_softc;
1966         struct ieee80211_node *ni;
1967         uint32_t attr;
1968         uint16_t base, associd;
1969         uint8_t mode, wcid, iv[8];
1970
1971         RUN_LOCK_ASSERT(sc, MA_OWNED);
1972
1973         if (vap->iv_opmode == IEEE80211_M_HOSTAP)
1974                 ni = ieee80211_find_vap_node(&ic->ic_sta, vap, cmdq->mac);
1975         else
1976                 ni = vap->iv_bss;
1977         associd = (ni != NULL) ? ni->ni_associd : 0;
1978
1979         /* map net80211 cipher to RT2860 security mode */
1980         switch (k->wk_cipher->ic_cipher) {
1981         case IEEE80211_CIPHER_WEP:
1982                 if(k->wk_keylen < 8)
1983                         mode = RT2860_MODE_WEP40;
1984                 else
1985                         mode = RT2860_MODE_WEP104;
1986                 break;
1987         case IEEE80211_CIPHER_TKIP:
1988                 mode = RT2860_MODE_TKIP;
1989                 break;
1990         case IEEE80211_CIPHER_AES_CCM:
1991                 mode = RT2860_MODE_AES_CCMP;
1992                 break;
1993         default:
1994                 DPRINTF("undefined case\n");
1995                 return;
1996         }
1997
1998         DPRINTFN(1, "associd=%x, keyix=%d, mode=%x, type=%s, tx=%s, rx=%s\n",
1999             associd, k->wk_keyix, mode,
2000             (k->wk_flags & IEEE80211_KEY_GROUP) ? "group" : "pairwise",
2001             (k->wk_flags & IEEE80211_KEY_XMIT) ? "on" : "off",
2002             (k->wk_flags & IEEE80211_KEY_RECV) ? "on" : "off");
2003
2004         if (k->wk_flags & IEEE80211_KEY_GROUP) {
2005                 wcid = 0;       /* NB: update WCID0 for group keys */
2006                 base = RT2860_SKEY(RUN_VAP(vap)->rvp_id, k->wk_keyix);
2007         } else {
2008                 wcid = RUN_AID2WCID(associd);
2009                 base = RT2860_PKEY(wcid);
2010         }
2011
2012         if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP) {
2013                 if(run_write_region_1(sc, base, k->wk_key, 16))
2014                         return;
2015                 if(run_write_region_1(sc, base + 16, &k->wk_key[16], 8))        /* wk_txmic */
2016                         return;
2017                 if(run_write_region_1(sc, base + 24, &k->wk_key[24], 8))        /* wk_rxmic */
2018                         return;
2019         } else {
2020                 /* roundup len to 16-bit: XXX fix write_region_1() instead */
2021                 if(run_write_region_1(sc, base, k->wk_key, (k->wk_keylen + 1) & ~1))
2022                         return;
2023         }
2024
2025         if (!(k->wk_flags & IEEE80211_KEY_GROUP) ||
2026             (k->wk_flags & (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV))) {
2027                 /* set initial packet number in IV+EIV */
2028                 if (k->wk_cipher == IEEE80211_CIPHER_WEP) {
2029                         memset(iv, 0, sizeof iv);
2030                         iv[3] = vap->iv_def_txkey << 6;
2031                 } else {
2032                         if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP) {
2033                                 iv[0] = k->wk_keytsc >> 8;
2034                                 iv[1] = (iv[0] | 0x20) & 0x7f;
2035                                 iv[2] = k->wk_keytsc;
2036                         } else /* CCMP */ {
2037                                 iv[0] = k->wk_keytsc;
2038                                 iv[1] = k->wk_keytsc >> 8;
2039                                 iv[2] = 0;
2040                         }
2041                         iv[3] = k->wk_keyix << 6 | IEEE80211_WEP_EXTIV;
2042                         iv[4] = k->wk_keytsc >> 16;
2043                         iv[5] = k->wk_keytsc >> 24;
2044                         iv[6] = k->wk_keytsc >> 32;
2045                         iv[7] = k->wk_keytsc >> 40;
2046                 }
2047                 if (run_write_region_1(sc, RT2860_IVEIV(wcid), iv, 8))
2048                         return;
2049         }
2050
2051         if (k->wk_flags & IEEE80211_KEY_GROUP) {
2052                 /* install group key */
2053                 if (run_read(sc, RT2860_SKEY_MODE_0_7, &attr))
2054                         return;
2055                 attr &= ~(0xf << (k->wk_keyix * 4));
2056                 attr |= mode << (k->wk_keyix * 4);
2057                 if (run_write(sc, RT2860_SKEY_MODE_0_7, attr))
2058                         return;
2059         } else {
2060                 /* install pairwise key */
2061                 if (run_read(sc, RT2860_WCID_ATTR(wcid), &attr))
2062                         return;
2063                 attr = (attr & ~0xf) | (mode << 1) | RT2860_RX_PKEY_EN;
2064                 if (run_write(sc, RT2860_WCID_ATTR(wcid), attr))
2065                         return;
2066         }
2067
2068         /* TODO create a pass-thru key entry? */
2069
2070         /* need wcid to delete the right key later */
2071         k->wk_pad = wcid;
2072 }
2073
2074 /*
2075  * Don't have to be deferred, but in order to keep order of
2076  * execution, i.e. with run_key_delete(), defer this and let
2077  * run_cmdq_cb() maintain the order.
2078  *
2079  * return 0 on error
2080  */
2081 static int
2082 run_key_set(struct ieee80211vap *vap, struct ieee80211_key *k,
2083                 const uint8_t mac[IEEE80211_ADDR_LEN])
2084 {
2085         struct ieee80211com *ic = vap->iv_ic;
2086         struct run_softc *sc = ic->ic_ifp->if_softc;
2087         uint32_t i;
2088
2089         i = RUN_CMDQ_GET(&sc->cmdq_store);
2090         DPRINTF("cmdq_store=%d\n", i);
2091         sc->cmdq[i].func = run_key_set_cb;
2092         sc->cmdq[i].arg0 = NULL;
2093         sc->cmdq[i].arg1 = vap;
2094         sc->cmdq[i].k = k;
2095         IEEE80211_ADDR_COPY(sc->cmdq[i].mac, mac);
2096         ieee80211_runtask(ic, &sc->cmdq_task);
2097
2098         /*
2099          * To make sure key will be set when hostapd
2100          * calls iv_key_set() before if_init().
2101          */
2102         if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
2103                 RUN_LOCK(sc);
2104                 sc->cmdq_key_set = RUN_CMDQ_GO;
2105                 RUN_UNLOCK(sc);
2106         }
2107
2108         return (1);
2109 }
2110
2111 /*
2112  * If wlan is destroyed without being brought down i.e. without
2113  * wlan down or wpa_cli terminate, this function is called after
2114  * vap is gone. Don't refer it.
2115  */
2116 static void
2117 run_key_delete_cb(void *arg)
2118 {
2119         struct run_cmdq *cmdq = arg;
2120         struct run_softc *sc = cmdq->arg1;
2121         struct ieee80211_key *k = &cmdq->key;
2122         uint32_t attr;
2123         uint8_t wcid;
2124
2125         RUN_LOCK_ASSERT(sc, MA_OWNED);
2126
2127         if (k->wk_flags & IEEE80211_KEY_GROUP) {
2128                 /* remove group key */
2129                 DPRINTF("removing group key\n");
2130                 run_read(sc, RT2860_SKEY_MODE_0_7, &attr);
2131                 attr &= ~(0xf << (k->wk_keyix * 4));
2132                 run_write(sc, RT2860_SKEY_MODE_0_7, attr);
2133         } else {
2134                 /* remove pairwise key */
2135                 DPRINTF("removing key for wcid %x\n", k->wk_pad);
2136                 /* matching wcid was written to wk_pad in run_key_set() */
2137                 wcid = k->wk_pad;
2138                 run_read(sc, RT2860_WCID_ATTR(wcid), &attr);
2139                 attr &= ~0xf;
2140                 run_write(sc, RT2860_WCID_ATTR(wcid), attr);
2141                 run_set_region_4(sc, RT2860_WCID_ENTRY(wcid), 0, 8);
2142         }
2143
2144         k->wk_pad = 0;
2145 }
2146
2147 /*
2148  * return 0 on error
2149  */
2150 static int
2151 run_key_delete(struct ieee80211vap *vap, struct ieee80211_key *k)
2152 {
2153         struct ieee80211com *ic = vap->iv_ic;
2154         struct run_softc *sc = ic->ic_ifp->if_softc;
2155         struct ieee80211_key *k0;
2156         uint32_t i;
2157
2158         /*
2159          * When called back, key might be gone. So, make a copy
2160          * of some values need to delete keys before deferring.
2161          * But, because of LOR with node lock, cannot use lock here.
2162          * So, use atomic instead.
2163          */
2164         i = RUN_CMDQ_GET(&sc->cmdq_store);
2165         DPRINTF("cmdq_store=%d\n", i);
2166         sc->cmdq[i].func = run_key_delete_cb;
2167         sc->cmdq[i].arg0 = NULL;
2168         sc->cmdq[i].arg1 = sc;
2169         k0 = &sc->cmdq[i].key;
2170         k0->wk_flags = k->wk_flags;
2171         k0->wk_keyix = k->wk_keyix;
2172         /* matching wcid was written to wk_pad in run_key_set() */
2173         k0->wk_pad = k->wk_pad;
2174         ieee80211_runtask(ic, &sc->cmdq_task);
2175         return (1);     /* return fake success */
2176
2177 }
2178
2179 static void
2180 run_ratectl_to(void *arg)
2181 {
2182         struct run_softc *sc = arg;
2183
2184         /* do it in a process context, so it can go sleep */
2185         ieee80211_runtask(sc->sc_ifp->if_l2com, &sc->ratectl_task);
2186         /* next timeout will be rescheduled in the callback task */
2187 }
2188
2189 /* ARGSUSED */
2190 static void
2191 run_ratectl_cb(void *arg, int pending)
2192 {
2193         struct run_softc *sc = arg;
2194         struct ieee80211com *ic = sc->sc_ifp->if_l2com;
2195         struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2196
2197         if (vap == NULL)
2198                 return;
2199
2200         if (sc->rvp_cnt <= 1 && vap->iv_opmode == IEEE80211_M_STA)
2201                 run_iter_func(sc, vap->iv_bss);
2202         else {
2203                 /*
2204                  * run_reset_livelock() doesn't do anything with AMRR,
2205                  * but Ralink wants us to call it every 1 sec. So, we
2206                  * piggyback here rather than creating another callout.
2207                  * Livelock may occur only in HOSTAP or IBSS mode
2208                  * (when h/w is sending beacons).
2209                  */
2210                 RUN_LOCK(sc);
2211                 run_reset_livelock(sc);
2212                 /* just in case, there are some stats to drain */
2213                 run_drain_fifo(sc);
2214                 RUN_UNLOCK(sc);
2215                 ieee80211_iterate_nodes(&ic->ic_sta, run_iter_func, sc);
2216         }
2217
2218         if(sc->ratectl_run != RUN_RATECTL_OFF)
2219                 usb_callout_reset(&sc->ratectl_ch, hz, run_ratectl_to, sc);
2220 }
2221
2222 static void
2223 run_drain_fifo(void *arg)
2224 {
2225         struct run_softc *sc = arg;
2226         struct ifnet *ifp = sc->sc_ifp;
2227         struct ieee80211_node *ni = sc->sc_ni[0];       /* make compiler happy */
2228         uint32_t stat;
2229         int retrycnt = 0;
2230         uint8_t wcid, mcs, pid;
2231
2232         RUN_LOCK_ASSERT(sc, MA_OWNED);
2233
2234         for (;;) {
2235                 /* drain Tx status FIFO (maxsize = 16) */
2236                 run_read(sc, RT2860_TX_STAT_FIFO, &stat);
2237                 DPRINTFN(4, "tx stat 0x%08x\n", stat);
2238                 if (!(stat & RT2860_TXQ_VLD))
2239                         break;
2240
2241                 wcid = (stat >> RT2860_TXQ_WCID_SHIFT) & 0xff;
2242
2243                 /* if no ACK was requested, no feedback is available */
2244                 if (!(stat & RT2860_TXQ_ACKREQ) || wcid > RT2870_WCID_MAX ||
2245                     wcid == 0)
2246                         continue;
2247
2248                 ni = sc->sc_ni[wcid];
2249                 if (ni->ni_rctls == NULL)
2250                         continue;
2251
2252                 /* update per-STA AMRR stats */
2253                 if (stat & RT2860_TXQ_OK) {
2254                         /*
2255                          * Check if there were retries, ie if the Tx
2256                          * success rate is different from the requested
2257                          * rate. Note that it works only because we do
2258                          * not allow rate fallback from OFDM to CCK.
2259                          */
2260                         mcs = (stat >> RT2860_TXQ_MCS_SHIFT) & 0x7f;
2261                         pid = (stat >> RT2860_TXQ_PID_SHIFT) & 0xf;
2262                         if (mcs + 1 != pid)
2263                                 retrycnt = 1;
2264                         ieee80211_ratectl_tx_complete(ni->ni_vap, ni,
2265                             IEEE80211_RATECTL_TX_SUCCESS,
2266                             &retrycnt, NULL);
2267                 } else {
2268                         retrycnt = 1;
2269                         ieee80211_ratectl_tx_complete(ni->ni_vap, ni,
2270                             IEEE80211_RATECTL_TX_FAILURE,
2271                             &retrycnt, NULL);
2272                         ifp->if_oerrors++;
2273                 }
2274         }
2275         DPRINTFN(3, "count=%d\n", sc->fifo_cnt);
2276
2277         sc->fifo_cnt = 0;
2278 }
2279
2280 static void
2281 run_iter_func(void *arg, struct ieee80211_node *ni)
2282 {
2283         struct run_softc *sc = arg;
2284         struct ieee80211vap *vap = ni->ni_vap;
2285         struct ieee80211com *ic = ni->ni_ic;
2286         struct ifnet *ifp = ic->ic_ifp;
2287         struct run_node *rn = (void *)ni;
2288         uint32_t sta[3];
2289         int txcnt = 0, success = 0, retrycnt = 0;
2290         int error;
2291
2292         if (sc->rvp_cnt <= 1 && (vap->iv_opmode == IEEE80211_M_IBSS ||
2293             vap->iv_opmode == IEEE80211_M_STA)) {
2294                 RUN_LOCK(sc);
2295
2296                 /* read statistic counters (clear on read) and update AMRR state */
2297                 error = run_read_region_1(sc, RT2860_TX_STA_CNT0, (uint8_t *)sta,
2298                     sizeof sta);
2299                 if (error != 0)
2300                         return;
2301
2302                 DPRINTFN(3, "retrycnt=%d txcnt=%d failcnt=%d\n",
2303                     le32toh(sta[1]) >> 16, le32toh(sta[1]) & 0xffff,
2304                     le32toh(sta[0]) & 0xffff);
2305
2306                 /* count failed TX as errors */
2307                 ifp->if_oerrors += le32toh(sta[0]) & 0xffff;
2308
2309                 retrycnt =
2310                     (le32toh(sta[0]) & 0xffff) +        /* failed TX count */
2311                     (le32toh(sta[1]) >> 16);            /* TX retransmission count */
2312
2313                 txcnt =
2314                     retrycnt +
2315                     (le32toh(sta[1]) & 0xffff);         /* successful TX count */
2316
2317                 success =
2318                     (le32toh(sta[1]) >> 16) +
2319                     (le32toh(sta[1]) & 0xffff);
2320
2321                 ieee80211_ratectl_tx_update(vap, ni, &txcnt, &success,
2322                     &retrycnt);
2323
2324                 RUN_UNLOCK(sc);
2325         }
2326
2327         rn->amrr_ridx = ieee80211_ratectl_rate(ni, NULL, 0);
2328         DPRINTFN(3, "ridx=%d\n", rn->amrr_ridx);
2329 }
2330
2331 static void
2332 run_newassoc_cb(void *arg)
2333 {
2334         struct run_cmdq *cmdq = arg;
2335         struct ieee80211_node *ni = cmdq->arg1;
2336         struct run_softc *sc = ni->ni_vap->iv_ic->ic_ifp->if_softc;
2337         uint8_t wcid = cmdq->wcid;
2338
2339         RUN_LOCK_ASSERT(sc, MA_OWNED);
2340
2341         run_write_region_1(sc, RT2860_WCID_ENTRY(wcid),
2342             ni->ni_macaddr, IEEE80211_ADDR_LEN);
2343 }
2344
2345 static void
2346 run_newassoc(struct ieee80211_node *ni, int isnew)
2347 {
2348         struct run_node *rn = (void *)ni;
2349         struct ieee80211_rateset *rs = &ni->ni_rates;
2350         struct ieee80211vap *vap = ni->ni_vap;
2351         struct ieee80211com *ic = vap->iv_ic;
2352         struct run_softc *sc = ic->ic_ifp->if_softc;
2353         uint8_t rate;
2354         uint8_t ridx;
2355         uint8_t wcid = RUN_AID2WCID(ni->ni_associd);
2356         int i, j;
2357
2358         if (wcid > RT2870_WCID_MAX) {
2359                 device_printf(sc->sc_dev, "wcid=%d out of range\n", wcid);
2360                 return;
2361         }
2362
2363         /* only interested in true associations */
2364         if (isnew && ni->ni_associd != 0) {
2365
2366                 /*
2367                  * This function could is called though timeout function.
2368                  * Need to defer.
2369                  */
2370                 uint32_t cnt = RUN_CMDQ_GET(&sc->cmdq_store);
2371                 DPRINTF("cmdq_store=%d\n", cnt);
2372                 sc->cmdq[cnt].func = run_newassoc_cb;
2373                 sc->cmdq[cnt].arg0 = NULL;
2374                 sc->cmdq[cnt].arg1 = ni;
2375                 sc->cmdq[cnt].wcid = wcid;
2376                 ieee80211_runtask(ic, &sc->cmdq_task);
2377         }
2378
2379         DPRINTF("new assoc isnew=%d associd=%x addr=%s\n",
2380             isnew, ni->ni_associd, ether_sprintf(ni->ni_macaddr));
2381
2382         sc->sc_ni[wcid] = ni;
2383
2384         for (i = 0; i < rs->rs_nrates; i++) {
2385                 rate = rs->rs_rates[i] & IEEE80211_RATE_VAL;
2386                 /* convert 802.11 rate to hardware rate index */
2387                 for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
2388                         if (rt2860_rates[ridx].rate == rate)
2389                                 break;
2390                 rn->ridx[i] = ridx;
2391                 /* determine rate of control response frames */
2392                 for (j = i; j >= 0; j--) {
2393                         if ((rs->rs_rates[j] & IEEE80211_RATE_BASIC) &&
2394                             rt2860_rates[rn->ridx[i]].phy ==
2395                             rt2860_rates[rn->ridx[j]].phy)
2396                                 break;
2397                 }
2398                 if (j >= 0) {
2399                         rn->ctl_ridx[i] = rn->ridx[j];
2400                 } else {
2401                         /* no basic rate found, use mandatory one */
2402                         rn->ctl_ridx[i] = rt2860_rates[ridx].ctl_ridx;
2403                 }
2404                 DPRINTF("rate=0x%02x ridx=%d ctl_ridx=%d\n",
2405                     rs->rs_rates[i], rn->ridx[i], rn->ctl_ridx[i]);
2406         }
2407         rate = vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)].mgmtrate;
2408         for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
2409                 if (rt2860_rates[ridx].rate == rate)
2410                         break;
2411         rn->mgt_ridx = ridx;
2412         DPRINTF("rate=%d, mgmt_ridx=%d\n", rate, rn->mgt_ridx);
2413
2414         usb_callout_reset(&sc->ratectl_ch, hz, run_ratectl_to, sc);
2415 }
2416
2417 /*
2418  * Return the Rx chain with the highest RSSI for a given frame.
2419  */
2420 static __inline uint8_t
2421 run_maxrssi_chain(struct run_softc *sc, const struct rt2860_rxwi *rxwi)
2422 {
2423         uint8_t rxchain = 0;
2424
2425         if (sc->nrxchains > 1) {
2426                 if (rxwi->rssi[1] > rxwi->rssi[rxchain])
2427                         rxchain = 1;
2428                 if (sc->nrxchains > 2)
2429                         if (rxwi->rssi[2] > rxwi->rssi[rxchain])
2430                                 rxchain = 2;
2431         }
2432         return (rxchain);
2433 }
2434
2435 static void
2436 run_rx_frame(struct run_softc *sc, struct mbuf *m, uint32_t dmalen)
2437 {
2438         struct ifnet *ifp = sc->sc_ifp;
2439         struct ieee80211com *ic = ifp->if_l2com;
2440         struct ieee80211_frame *wh;
2441         struct ieee80211_node *ni;
2442         struct rt2870_rxd *rxd;
2443         struct rt2860_rxwi *rxwi;
2444         uint32_t flags;
2445         uint16_t len, phy;
2446         uint8_t ant, rssi;
2447         int8_t nf;
2448
2449         rxwi = mtod(m, struct rt2860_rxwi *);
2450         len = le16toh(rxwi->len) & 0xfff;
2451         if (__predict_false(len > dmalen)) {
2452                 m_freem(m);
2453                 ifp->if_ierrors++;
2454                 DPRINTF("bad RXWI length %u > %u\n", len, dmalen);
2455                 return;
2456         }
2457         /* Rx descriptor is located at the end */
2458         rxd = (struct rt2870_rxd *)(mtod(m, caddr_t) + dmalen);
2459         flags = le32toh(rxd->flags);
2460
2461         if (__predict_false(flags & (RT2860_RX_CRCERR | RT2860_RX_ICVERR))) {
2462                 m_freem(m);
2463                 ifp->if_ierrors++;
2464                 DPRINTF("%s error.\n", (flags & RT2860_RX_CRCERR)?"CRC":"ICV");
2465                 return;
2466         }
2467
2468         m->m_data += sizeof(struct rt2860_rxwi);
2469         m->m_pkthdr.len = m->m_len -= sizeof(struct rt2860_rxwi);
2470
2471         wh = mtod(m, struct ieee80211_frame *);
2472
2473         if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
2474                 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
2475                 m->m_flags |= M_WEP;
2476         }
2477
2478         if (flags & RT2860_RX_L2PAD) {
2479                 DPRINTFN(8, "received RT2860_RX_L2PAD frame\n");
2480                 len += 2;
2481         }
2482
2483         ni = ieee80211_find_rxnode(ic,
2484             mtod(m, struct ieee80211_frame_min *));
2485
2486         if (__predict_false(flags & RT2860_RX_MICERR)) {
2487                 /* report MIC failures to net80211 for TKIP */
2488                 if (ni != NULL)
2489                         ieee80211_notify_michael_failure(ni->ni_vap, wh, rxwi->keyidx);
2490                 m_freem(m);
2491                 ifp->if_ierrors++;
2492                 DPRINTF("MIC error. Someone is lying.\n");
2493                 return;
2494         }
2495
2496         ant = run_maxrssi_chain(sc, rxwi);
2497         rssi = rxwi->rssi[ant];
2498         nf = run_rssi2dbm(sc, rssi, ant);
2499
2500         m->m_pkthdr.rcvif = ifp;
2501         m->m_pkthdr.len = m->m_len = len;
2502
2503         if (ni != NULL) {
2504                 (void)ieee80211_input(ni, m, rssi, nf);
2505                 ieee80211_free_node(ni);
2506         } else {
2507                 (void)ieee80211_input_all(ic, m, rssi, nf);
2508         }
2509
2510         if (__predict_false(ieee80211_radiotap_active(ic))) {
2511                 struct run_rx_radiotap_header *tap = &sc->sc_rxtap;
2512
2513                 tap->wr_flags = 0;
2514                 tap->wr_chan_freq = htole16(ic->ic_bsschan->ic_freq);
2515                 tap->wr_chan_flags = htole16(ic->ic_bsschan->ic_flags);
2516                 tap->wr_antsignal = rssi;
2517                 tap->wr_antenna = ant;
2518                 tap->wr_dbm_antsignal = run_rssi2dbm(sc, rssi, ant);
2519                 tap->wr_rate = 2;       /* in case it can't be found below */
2520                 phy = le16toh(rxwi->phy);
2521                 switch (phy & RT2860_PHY_MODE) {
2522                 case RT2860_PHY_CCK:
2523                         switch ((phy & RT2860_PHY_MCS) & ~RT2860_PHY_SHPRE) {
2524                         case 0: tap->wr_rate =   2; break;
2525                         case 1: tap->wr_rate =   4; break;
2526                         case 2: tap->wr_rate =  11; break;
2527                         case 3: tap->wr_rate =  22; break;
2528                         }
2529                         if (phy & RT2860_PHY_SHPRE)
2530                                 tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
2531                         break;
2532                 case RT2860_PHY_OFDM:
2533                         switch (phy & RT2860_PHY_MCS) {
2534                         case 0: tap->wr_rate =  12; break;
2535                         case 1: tap->wr_rate =  18; break;
2536                         case 2: tap->wr_rate =  24; break;
2537                         case 3: tap->wr_rate =  36; break;
2538                         case 4: tap->wr_rate =  48; break;
2539                         case 5: tap->wr_rate =  72; break;
2540                         case 6: tap->wr_rate =  96; break;
2541                         case 7: tap->wr_rate = 108; break;
2542                         }
2543                         break;
2544                 }
2545         }
2546 }
2547
2548 static void
2549 run_bulk_rx_callback(struct usb_xfer *xfer, usb_error_t error)
2550 {
2551         struct run_softc *sc = usbd_xfer_softc(xfer);
2552         struct ifnet *ifp = sc->sc_ifp;
2553         struct mbuf *m = NULL;
2554         struct mbuf *m0;
2555         uint32_t dmalen;
2556         int xferlen;
2557
2558         usbd_xfer_status(xfer, &xferlen, NULL, NULL, NULL);
2559
2560         switch (USB_GET_STATE(xfer)) {
2561         case USB_ST_TRANSFERRED:
2562
2563                 DPRINTFN(15, "rx done, actlen=%d\n", xferlen);
2564
2565                 if (xferlen < (int)(sizeof(uint32_t) +
2566                     sizeof(struct rt2860_rxwi) + sizeof(struct rt2870_rxd))) {
2567                         DPRINTF("xfer too short %d\n", xferlen);
2568                         goto tr_setup;
2569                 }
2570
2571                 m = sc->rx_m;
2572                 sc->rx_m = NULL;
2573
2574                 /* FALLTHROUGH */
2575         case USB_ST_SETUP:
2576 tr_setup:
2577                 if (sc->rx_m == NULL) {
2578                         sc->rx_m = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR,
2579                             MJUMPAGESIZE /* xfer can be bigger than MCLBYTES */);
2580                 }
2581                 if (sc->rx_m == NULL) {
2582                         DPRINTF("could not allocate mbuf - idle with stall\n");
2583                         ifp->if_ierrors++;
2584                         usbd_xfer_set_stall(xfer);
2585                         usbd_xfer_set_frames(xfer, 0);
2586                 } else {
2587                         /*
2588                          * Directly loading a mbuf cluster into DMA to
2589                          * save some data copying. This works because
2590                          * there is only one cluster.
2591                          */
2592                         usbd_xfer_set_frame_data(xfer, 0, 
2593                             mtod(sc->rx_m, caddr_t), RUN_MAX_RXSZ);
2594                         usbd_xfer_set_frames(xfer, 1);
2595                 }
2596                 usbd_transfer_submit(xfer);
2597                 break;
2598
2599         default:        /* Error */
2600                 if (error != USB_ERR_CANCELLED) {
2601                         /* try to clear stall first */
2602                         usbd_xfer_set_stall(xfer);
2603
2604                         if (error == USB_ERR_TIMEOUT)
2605                                 device_printf(sc->sc_dev, "device timeout\n");
2606
2607                         ifp->if_ierrors++;
2608
2609                         goto tr_setup;
2610                 }
2611                 if (sc->rx_m != NULL) {
2612                         m_freem(sc->rx_m);
2613                         sc->rx_m = NULL;
2614                 }
2615                 break;
2616         }
2617
2618         if (m == NULL)
2619                 return;
2620
2621         /* inputting all the frames must be last */
2622
2623         RUN_UNLOCK(sc);
2624
2625         m->m_pkthdr.len = m->m_len = xferlen;
2626
2627         /* HW can aggregate multiple 802.11 frames in a single USB xfer */
2628         for(;;) {
2629                 dmalen = le32toh(*mtod(m, uint32_t *)) & 0xffff;
2630
2631                 if ((dmalen >= (uint32_t)-8) || (dmalen == 0) ||
2632                     ((dmalen & 3) != 0)) {
2633                         DPRINTF("bad DMA length %u\n", dmalen);
2634                         break;
2635                 }
2636                 if ((dmalen + 8) > (uint32_t)xferlen) {
2637                         DPRINTF("bad DMA length %u > %d\n",
2638                         dmalen + 8, xferlen);
2639                         break;
2640                 }
2641
2642                 /* If it is the last one or a single frame, we won't copy. */
2643                 if ((xferlen -= dmalen + 8) <= 8) {
2644                         /* trim 32-bit DMA-len header */
2645                         m->m_data += 4;
2646                         m->m_pkthdr.len = m->m_len -= 4;
2647                         run_rx_frame(sc, m, dmalen);
2648                         break;
2649                 }
2650
2651                 /* copy aggregated frames to another mbuf */
2652                 m0 = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
2653                 if (__predict_false(m0 == NULL)) {
2654                         DPRINTF("could not allocate mbuf\n");
2655                         ifp->if_ierrors++;
2656                         break;
2657                 }
2658                 m_copydata(m, 4 /* skip 32-bit DMA-len header */,
2659                     dmalen + sizeof(struct rt2870_rxd), mtod(m0, caddr_t));
2660                 m0->m_pkthdr.len = m0->m_len =
2661                     dmalen + sizeof(struct rt2870_rxd);
2662                 run_rx_frame(sc, m0, dmalen);
2663
2664                 /* update data ptr */
2665                 m->m_data += dmalen + 8;
2666                 m->m_pkthdr.len = m->m_len -= dmalen + 8;
2667         }
2668
2669         RUN_LOCK(sc);
2670 }
2671
2672 static void
2673 run_tx_free(struct run_endpoint_queue *pq,
2674     struct run_tx_data *data, int txerr)
2675 {
2676         if (data->m != NULL) {
2677                 if (data->m->m_flags & M_TXCB)
2678                         ieee80211_process_callback(data->ni, data->m,
2679                             txerr ? ETIMEDOUT : 0);
2680                 m_freem(data->m);
2681                 data->m = NULL;
2682
2683                 if (data->ni == NULL) {
2684                         DPRINTF("no node\n");
2685                 } else {
2686                         ieee80211_free_node(data->ni);
2687                         data->ni = NULL;
2688                 }
2689         }
2690
2691         STAILQ_INSERT_TAIL(&pq->tx_fh, data, next);
2692         pq->tx_nfree++;
2693 }
2694
2695 static void
2696 run_bulk_tx_callbackN(struct usb_xfer *xfer, usb_error_t error, unsigned int index)
2697 {
2698         struct run_softc *sc = usbd_xfer_softc(xfer);
2699         struct ifnet *ifp = sc->sc_ifp;
2700         struct ieee80211com *ic = ifp->if_l2com;
2701         struct run_tx_data *data;
2702         struct ieee80211vap *vap = NULL;
2703         struct usb_page_cache *pc;
2704         struct run_endpoint_queue *pq = &sc->sc_epq[index];
2705         struct mbuf *m;
2706         usb_frlength_t size;
2707         unsigned int len;
2708         int actlen;
2709         int sumlen;
2710
2711         usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
2712
2713         switch (USB_GET_STATE(xfer)) {
2714         case USB_ST_TRANSFERRED:
2715                 DPRINTFN(11, "transfer complete: %d "
2716                     "bytes @ index %d\n", actlen, index);
2717
2718                 data = usbd_xfer_get_priv(xfer);
2719
2720                 run_tx_free(pq, data, 0);
2721                 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2722
2723                 usbd_xfer_set_priv(xfer, NULL);
2724
2725                 ifp->if_opackets++;
2726
2727                 /* FALLTHROUGH */
2728         case USB_ST_SETUP:
2729 tr_setup:
2730                 data = STAILQ_FIRST(&pq->tx_qh);
2731                 if (data == NULL)
2732                         break;
2733
2734                 STAILQ_REMOVE_HEAD(&pq->tx_qh, next);
2735
2736                 m = data->m;
2737                 if (m->m_pkthdr.len > RUN_MAX_TXSZ) {
2738                         DPRINTF("data overflow, %u bytes\n",
2739                             m->m_pkthdr.len);
2740
2741                         ifp->if_oerrors++;
2742
2743                         run_tx_free(pq, data, 1);
2744
2745                         goto tr_setup;
2746                 }
2747
2748                 pc = usbd_xfer_get_frame(xfer, 0);
2749                 size = sizeof(data->desc);
2750                 usbd_copy_in(pc, 0, &data->desc, size);
2751                 usbd_m_copy_in(pc, size, m, 0, m->m_pkthdr.len);
2752
2753                 vap = data->ni->ni_vap;
2754                 if (ieee80211_radiotap_active_vap(vap)) {
2755                         struct run_tx_radiotap_header *tap = &sc->sc_txtap;
2756                         struct rt2860_txwi *txwi =
2757                             (struct rt2860_txwi *)(&data->desc + sizeof(struct rt2870_txd));
2758
2759                         tap->wt_flags = 0;
2760                         tap->wt_rate = rt2860_rates[data->ridx].rate;
2761                         tap->wt_chan_freq = htole16(vap->iv_bss->ni_chan->ic_freq);
2762                         tap->wt_chan_flags = htole16(vap->iv_bss->ni_chan->ic_flags);
2763                         tap->wt_hwqueue = index;
2764                         if (le16toh(txwi->phy) & RT2860_PHY_SHPRE)
2765                                 tap->wt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
2766
2767                         ieee80211_radiotap_tx(vap, m);
2768                 }
2769
2770                 /* align end on a 4-bytes boundary */
2771                 len = (size + IEEE80211_CRC_LEN + m->m_pkthdr.len + 3) & ~3;
2772
2773                 DPRINTFN(11, "sending frame len=%u xferlen=%u @ index %d\n",
2774                         m->m_pkthdr.len, len, index);
2775
2776                 usbd_xfer_set_frame_len(xfer, 0, len);
2777                 usbd_xfer_set_priv(xfer, data);
2778
2779                 usbd_transfer_submit(xfer);
2780
2781                 RUN_UNLOCK(sc);
2782                 run_start(ifp);
2783                 RUN_LOCK(sc);
2784
2785                 break;
2786
2787         default:
2788                 DPRINTF("USB transfer error, %s\n",
2789                     usbd_errstr(error));
2790
2791                 data = usbd_xfer_get_priv(xfer);
2792
2793                 ifp->if_oerrors++;
2794
2795                 if (data != NULL) {
2796                         if(data->ni != NULL)
2797                                 vap = data->ni->ni_vap;
2798                         run_tx_free(pq, data, error);
2799                         usbd_xfer_set_priv(xfer, NULL);
2800                 }
2801                 if (vap == NULL)
2802                         vap = TAILQ_FIRST(&ic->ic_vaps);
2803
2804                 if (error != USB_ERR_CANCELLED) {
2805                         if (error == USB_ERR_TIMEOUT) {
2806                                 device_printf(sc->sc_dev, "device timeout\n");
2807                                 uint32_t i = RUN_CMDQ_GET(&sc->cmdq_store);
2808                                 DPRINTF("cmdq_store=%d\n", i);
2809                                 sc->cmdq[i].func = run_usb_timeout_cb;
2810                                 sc->cmdq[i].arg0 = vap;
2811                                 ieee80211_runtask(ic, &sc->cmdq_task);
2812                         }
2813
2814                         /*
2815                          * Try to clear stall first, also if other
2816                          * errors occur, hence clearing stall
2817                          * introduces a 50 ms delay:
2818                          */
2819                         usbd_xfer_set_stall(xfer);
2820                         goto tr_setup;
2821                 }
2822                 break;
2823         }
2824 }
2825
2826 static void
2827 run_bulk_tx_callback0(struct usb_xfer *xfer, usb_error_t error)
2828 {
2829         run_bulk_tx_callbackN(xfer, error, 0);
2830 }
2831
2832 static void
2833 run_bulk_tx_callback1(struct usb_xfer *xfer, usb_error_t error)
2834 {
2835         run_bulk_tx_callbackN(xfer, error, 1);
2836 }
2837
2838 static void
2839 run_bulk_tx_callback2(struct usb_xfer *xfer, usb_error_t error)
2840 {
2841         run_bulk_tx_callbackN(xfer, error, 2);
2842 }
2843
2844 static void
2845 run_bulk_tx_callback3(struct usb_xfer *xfer, usb_error_t error)
2846 {
2847         run_bulk_tx_callbackN(xfer, error, 3);
2848 }
2849
2850 static void
2851 run_bulk_tx_callback4(struct usb_xfer *xfer, usb_error_t error)
2852 {
2853         run_bulk_tx_callbackN(xfer, error, 4);
2854 }
2855
2856 static void
2857 run_bulk_tx_callback5(struct usb_xfer *xfer, usb_error_t error)
2858 {
2859         run_bulk_tx_callbackN(xfer, error, 5);
2860 }
2861
2862 static void
2863 run_set_tx_desc(struct run_softc *sc, struct run_tx_data *data)
2864 {
2865         struct mbuf *m = data->m;
2866         struct ieee80211com *ic = sc->sc_ifp->if_l2com;
2867         struct ieee80211vap *vap = data->ni->ni_vap;
2868         struct ieee80211_frame *wh;
2869         struct rt2870_txd *txd;
2870         struct rt2860_txwi *txwi;
2871         uint16_t xferlen;
2872         uint16_t mcs;
2873         uint8_t ridx = data->ridx;
2874         uint8_t pad;
2875
2876         /* get MCS code from rate index */
2877         mcs = rt2860_rates[ridx].mcs;
2878
2879         xferlen = sizeof(*txwi) + m->m_pkthdr.len;
2880
2881         /* roundup to 32-bit alignment */
2882         xferlen = (xferlen + 3) & ~3;
2883
2884         txd = (struct rt2870_txd *)&data->desc;
2885         txd->len = htole16(xferlen);
2886
2887         wh = mtod(m, struct ieee80211_frame *);
2888
2889         /*
2890          * Ether both are true or both are false, the header
2891          * are nicely aligned to 32-bit. So, no L2 padding.
2892          */
2893         if(IEEE80211_HAS_ADDR4(wh) == IEEE80211_QOS_HAS_SEQ(wh))
2894                 pad = 0;
2895         else
2896                 pad = 2;
2897
2898         /* setup TX Wireless Information */
2899         txwi = (struct rt2860_txwi *)(txd + 1);
2900         txwi->len = htole16(m->m_pkthdr.len - pad);
2901         if (rt2860_rates[ridx].phy == IEEE80211_T_DS) {
2902                 txwi->phy = htole16(RT2860_PHY_CCK);
2903                 if (ridx != RT2860_RIDX_CCK1 &&
2904                     (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
2905                         mcs |= RT2860_PHY_SHPRE;
2906         } else
2907                 txwi->phy = htole16(RT2860_PHY_OFDM);
2908         txwi->phy |= htole16(mcs);
2909
2910         /* check if RTS/CTS or CTS-to-self protection is required */
2911         if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
2912             (m->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold ||
2913              ((ic->ic_flags & IEEE80211_F_USEPROT) &&
2914               rt2860_rates[ridx].phy == IEEE80211_T_OFDM)))
2915                 txwi->txop |= RT2860_TX_TXOP_HT;
2916         else
2917                 txwi->txop |= RT2860_TX_TXOP_BACKOFF;
2918
2919         if (vap->iv_opmode != IEEE80211_M_STA && !IEEE80211_QOS_HAS_SEQ(wh))
2920                 txwi->xflags |= RT2860_TX_NSEQ;
2921 }
2922
2923 /* This function must be called locked */
2924 static int
2925 run_tx(struct run_softc *sc, struct mbuf *m, struct ieee80211_node *ni)
2926 {
2927         struct ieee80211com *ic = sc->sc_ifp->if_l2com;
2928         struct ieee80211vap *vap = ni->ni_vap;
2929         struct ieee80211_frame *wh;
2930         struct ieee80211_channel *chan;
2931         const struct ieee80211_txparam *tp;
2932         struct run_node *rn = (void *)ni;
2933         struct run_tx_data *data;
2934         struct rt2870_txd *txd;
2935         struct rt2860_txwi *txwi;
2936         uint16_t qos;
2937         uint16_t dur;
2938         uint16_t qid;
2939         uint8_t type;
2940         uint8_t tid;
2941         uint8_t ridx;
2942         uint8_t ctl_ridx;
2943         uint8_t qflags;
2944         uint8_t xflags = 0;
2945         int hasqos;
2946
2947         RUN_LOCK_ASSERT(sc, MA_OWNED);
2948
2949         wh = mtod(m, struct ieee80211_frame *);
2950
2951         type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
2952
2953         /*
2954          * There are 7 bulk endpoints: 1 for RX
2955          * and 6 for TX (4 EDCAs + HCCA + Prio).
2956          * Update 03-14-2009:  some devices like the Planex GW-US300MiniS
2957          * seem to have only 4 TX bulk endpoints (Fukaumi Naoki).
2958          */
2959         if ((hasqos = IEEE80211_QOS_HAS_SEQ(wh))) {
2960                 uint8_t *frm;
2961
2962                 if(IEEE80211_HAS_ADDR4(wh))
2963                         frm = ((struct ieee80211_qosframe_addr4 *)wh)->i_qos;
2964                 else
2965                         frm =((struct ieee80211_qosframe *)wh)->i_qos;
2966
2967                 qos = le16toh(*(const uint16_t *)frm);
2968                 tid = qos & IEEE80211_QOS_TID;
2969                 qid = TID_TO_WME_AC(tid);
2970         } else {
2971                 qos = 0;
2972                 tid = 0;
2973                 qid = WME_AC_BE;
2974         }
2975         qflags = (qid < 4) ? RT2860_TX_QSEL_EDCA : RT2860_TX_QSEL_HCCA;
2976
2977         DPRINTFN(8, "qos %d\tqid %d\ttid %d\tqflags %x\n",
2978             qos, qid, tid, qflags);
2979
2980         chan = (ni->ni_chan != IEEE80211_CHAN_ANYC)?ni->ni_chan:ic->ic_curchan;
2981         tp = &vap->iv_txparms[ieee80211_chan2mode(chan)];
2982
2983         /* pickup a rate index */
2984         if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
2985             type != IEEE80211_FC0_TYPE_DATA) {
2986                 ridx = (ic->ic_curmode == IEEE80211_MODE_11A) ?
2987                     RT2860_RIDX_OFDM6 : RT2860_RIDX_CCK1;
2988                 ctl_ridx = rt2860_rates[ridx].ctl_ridx;
2989         } else {
2990                 if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
2991                         ridx = rn->fix_ridx;
2992                 else
2993                         ridx = rn->amrr_ridx;
2994                 ctl_ridx = rt2860_rates[ridx].ctl_ridx;
2995         }
2996
2997         if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
2998             (!hasqos || (qos & IEEE80211_QOS_ACKPOLICY) !=
2999              IEEE80211_QOS_ACKPOLICY_NOACK)) {
3000                 xflags |= RT2860_TX_ACK;
3001                 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
3002                         dur = rt2860_rates[ctl_ridx].sp_ack_dur;
3003                 else
3004                         dur = rt2860_rates[ctl_ridx].lp_ack_dur;
3005                 *(uint16_t *)wh->i_dur = htole16(dur);
3006         }
3007
3008         /* reserve slots for mgmt packets, just in case */
3009         if (sc->sc_epq[qid].tx_nfree < 3) {
3010                 DPRINTFN(10, "tx ring %d is full\n", qid);
3011                 return (-1);
3012         }
3013
3014         data = STAILQ_FIRST(&sc->sc_epq[qid].tx_fh);
3015         STAILQ_REMOVE_HEAD(&sc->sc_epq[qid].tx_fh, next);
3016         sc->sc_epq[qid].tx_nfree--;
3017
3018         txd = (struct rt2870_txd *)&data->desc;
3019         txd->flags = qflags;
3020         txwi = (struct rt2860_txwi *)(txd + 1);
3021         txwi->xflags = xflags;
3022         txwi->wcid = IEEE80211_IS_MULTICAST(wh->i_addr1) ?
3023             0 : RUN_AID2WCID(ni->ni_associd);
3024         /* clear leftover garbage bits */
3025         txwi->flags = 0;
3026         txwi->txop = 0;
3027
3028         data->m = m;
3029         data->ni = ni;
3030         data->ridx = ridx;
3031
3032         run_set_tx_desc(sc, data);
3033
3034         /*
3035          * The chip keeps track of 2 kind of Tx stats,
3036          *  * TX_STAT_FIFO, for per WCID stats, and
3037          *  * TX_STA_CNT0 for all-TX-in-one stats.
3038          *
3039          * To use FIFO stats, we need to store MCS into the driver-private
3040          * PacketID field. So that, we can tell whose stats when we read them.
3041          * We add 1 to the MCS because setting the PacketID field to 0 means
3042          * that we don't want feedback in TX_STAT_FIFO.
3043          * And, that's what we want for STA mode, since TX_STA_CNT0 does the job.
3044          *
3045          * FIFO stats doesn't count Tx with WCID 0xff, so we do this in run_tx().
3046          */
3047         if (sc->rvp_cnt > 1 || vap->iv_opmode == IEEE80211_M_HOSTAP ||
3048             vap->iv_opmode == IEEE80211_M_MBSS) {
3049                 uint16_t pid = (rt2860_rates[ridx].mcs + 1) & 0xf;
3050                 txwi->len |= htole16(pid << RT2860_TX_PID_SHIFT);
3051
3052                 /*
3053                  * Unlike PCI based devices, we don't get any interrupt from
3054                  * USB devices, so we simulate FIFO-is-full interrupt here.
3055                  * Ralink recomends to drain FIFO stats every 100 ms, but 16 slots
3056                  * quickly get fulled. To prevent overflow, increment a counter on
3057                  * every FIFO stat request, so we know how many slots are left.
3058                  * We do this only in HOSTAP or multiple vap mode since FIFO stats
3059                  * are used only in those modes.
3060                  * We just drain stats. AMRR gets updated every 1 sec by
3061                  * run_ratectl_cb() via callout.
3062                  * Call it early. Otherwise overflow.
3063                  */
3064                 if (sc->fifo_cnt++ == 10) {
3065                         /*
3066                          * With multiple vaps or if_bridge, if_start() is called
3067                          * with a non-sleepable lock, tcpinp. So, need to defer.
3068                          */
3069                         uint32_t i = RUN_CMDQ_GET(&sc->cmdq_store);
3070                         DPRINTFN(6, "cmdq_store=%d\n", i);
3071                         sc->cmdq[i].func = run_drain_fifo;
3072                         sc->cmdq[i].arg0 = sc;
3073                         ieee80211_runtask(ic, &sc->cmdq_task);
3074                 }
3075         }
3076
3077         STAILQ_INSERT_TAIL(&sc->sc_epq[qid].tx_qh, data, next);
3078
3079         usbd_transfer_start(sc->sc_xfer[qid]);
3080
3081         DPRINTFN(8, "sending data frame len=%d rate=%d qid=%d\n", m->m_pkthdr.len +
3082             (int)(sizeof (struct rt2870_txd) + sizeof (struct rt2860_rxwi)),
3083             rt2860_rates[ridx].rate, qid);
3084
3085         return (0);
3086 }
3087
3088 static int
3089 run_tx_mgt(struct run_softc *sc, struct mbuf *m, struct ieee80211_node *ni)
3090 {
3091         struct ifnet *ifp = sc->sc_ifp;
3092         struct ieee80211com *ic = ifp->if_l2com;
3093         struct run_node *rn = (void *)ni;
3094         struct run_tx_data *data;
3095         struct ieee80211_frame *wh;
3096         struct rt2870_txd *txd;
3097         struct rt2860_txwi *txwi;
3098         uint16_t dur;
3099         uint8_t ridx = rn->mgt_ridx;
3100         uint8_t type;
3101         uint8_t xflags = 0;
3102         uint8_t wflags = 0;
3103
3104         RUN_LOCK_ASSERT(sc, MA_OWNED);
3105
3106         wh = mtod(m, struct ieee80211_frame *);
3107
3108         type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
3109
3110         /* tell hardware to add timestamp for probe responses */
3111         if ((wh->i_fc[0] &
3112             (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
3113             (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
3114                 wflags |= RT2860_TX_TS;
3115         else if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
3116                 xflags |= RT2860_TX_ACK;
3117
3118                 dur = ieee80211_ack_duration(ic->ic_rt, rt2860_rates[ridx].rate, 
3119                     ic->ic_flags & IEEE80211_F_SHPREAMBLE);
3120                 *(uint16_t *)wh->i_dur = htole16(dur);
3121         }
3122
3123         if (sc->sc_epq[0].tx_nfree == 0) {
3124                 /* let caller free mbuf */
3125                 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3126                 return (EIO);
3127         }
3128         data = STAILQ_FIRST(&sc->sc_epq[0].tx_fh);
3129         STAILQ_REMOVE_HEAD(&sc->sc_epq[0].tx_fh, next);
3130         sc->sc_epq[0].tx_nfree--;
3131
3132         txd = (struct rt2870_txd *)&data->desc;
3133         txd->flags = RT2860_TX_QSEL_EDCA;
3134         txwi = (struct rt2860_txwi *)(txd + 1);
3135         txwi->wcid = 0xff;
3136         txwi->flags = wflags;
3137         txwi->xflags = xflags;
3138         txwi->txop = 0; /* clear leftover garbage bits */
3139
3140         data->m = m;
3141         data->ni = ni;
3142         data->ridx = ridx;
3143
3144         run_set_tx_desc(sc, data);
3145
3146         DPRINTFN(10, "sending mgt frame len=%d rate=%d\n", m->m_pkthdr.len +
3147             (int)(sizeof (struct rt2870_txd) + sizeof (struct rt2860_rxwi)),
3148             rt2860_rates[ridx].rate);
3149
3150         STAILQ_INSERT_TAIL(&sc->sc_epq[0].tx_qh, data, next);
3151
3152         usbd_transfer_start(sc->sc_xfer[0]);
3153
3154         return (0);
3155 }
3156
3157 static int
3158 run_sendprot(struct run_softc *sc,
3159     const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate)
3160 {
3161         struct ieee80211com *ic = ni->ni_ic;
3162         struct ieee80211_frame *wh;
3163         struct run_tx_data *data;
3164         struct rt2870_txd *txd;
3165         struct rt2860_txwi *txwi;
3166         struct mbuf *mprot;
3167         int ridx;
3168         int protrate;
3169         int ackrate;
3170         int pktlen;
3171         int isshort;
3172         uint16_t dur;
3173         uint8_t type;
3174         uint8_t wflags = 0;
3175         uint8_t xflags = 0;
3176
3177         RUN_LOCK_ASSERT(sc, MA_OWNED);
3178
3179         KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY,
3180             ("protection %d", prot));
3181
3182         wh = mtod(m, struct ieee80211_frame *);
3183         pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
3184         type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
3185
3186         protrate = ieee80211_ctl_rate(ic->ic_rt, rate);
3187         ackrate = ieee80211_ack_rate(ic->ic_rt, rate);
3188
3189         isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0;
3190         dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort)
3191             + ieee80211_ack_duration(ic->ic_rt, rate, isshort);
3192         wflags = RT2860_TX_FRAG;
3193
3194         /* check that there are free slots before allocating the mbuf */
3195         if (sc->sc_epq[0].tx_nfree == 0) {
3196                 /* let caller free mbuf */
3197                 sc->sc_ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3198                 return (ENOBUFS);
3199         }
3200
3201         if (prot == IEEE80211_PROT_RTSCTS) {
3202                 /* NB: CTS is the same size as an ACK */
3203                 dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort);
3204                 xflags |= RT2860_TX_ACK;
3205                 mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur);
3206         } else {
3207                 mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur);
3208         }
3209         if (mprot == NULL) {
3210                 sc->sc_ifp->if_oerrors++;
3211                 DPRINTF("could not allocate mbuf\n");
3212                 return (ENOBUFS);
3213         }
3214
3215         data = STAILQ_FIRST(&sc->sc_epq[0].tx_fh);
3216         STAILQ_REMOVE_HEAD(&sc->sc_epq[0].tx_fh, next);
3217         sc->sc_epq[0].tx_nfree--;
3218
3219         txd = (struct rt2870_txd *)&data->desc;
3220         txd->flags = RT2860_TX_QSEL_EDCA;
3221         txwi = (struct rt2860_txwi *)(txd + 1);
3222         txwi->wcid = 0xff;
3223         txwi->flags = wflags;
3224         txwi->xflags = xflags;
3225         txwi->txop = 0; /* clear leftover garbage bits */
3226
3227         data->m = mprot;
3228         data->ni = ieee80211_ref_node(ni);
3229
3230         for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
3231                 if (rt2860_rates[ridx].rate == protrate)
3232                         break;
3233         data->ridx = ridx;
3234
3235         run_set_tx_desc(sc, data);
3236
3237         DPRINTFN(1, "sending prot len=%u rate=%u\n",
3238             m->m_pkthdr.len, rate);
3239
3240         STAILQ_INSERT_TAIL(&sc->sc_epq[0].tx_qh, data, next);
3241
3242         usbd_transfer_start(sc->sc_xfer[0]);
3243
3244         return (0);
3245 }
3246
3247 static int
3248 run_tx_param(struct run_softc *sc, struct mbuf *m, struct ieee80211_node *ni,
3249     const struct ieee80211_bpf_params *params)
3250 {
3251         struct ieee80211com *ic = ni->ni_ic;
3252         struct ieee80211_frame *wh;
3253         struct run_tx_data *data;
3254         struct rt2870_txd *txd;
3255         struct rt2860_txwi *txwi;
3256         uint8_t type;
3257         uint8_t ridx;
3258         uint8_t rate;
3259         uint8_t opflags = 0;
3260         uint8_t xflags = 0;
3261         int error;
3262
3263         RUN_LOCK_ASSERT(sc, MA_OWNED);
3264
3265         KASSERT(params != NULL, ("no raw xmit params"));
3266
3267         wh = mtod(m, struct ieee80211_frame *);
3268         type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
3269
3270         rate = params->ibp_rate0;
3271         if (!ieee80211_isratevalid(ic->ic_rt, rate)) {
3272                 /* let caller free mbuf */
3273                 return (EINVAL);
3274         }
3275
3276         if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
3277                 xflags |= RT2860_TX_ACK;
3278         if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) {
3279                 error = run_sendprot(sc, m, ni,
3280                     params->ibp_flags & IEEE80211_BPF_RTS ?
3281                         IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY,
3282                     rate);
3283                 if (error) {
3284                         /* let caller free mbuf */
3285                         return error;
3286                 }
3287                 opflags |= /*XXX RT2573_TX_LONG_RETRY |*/ RT2860_TX_TXOP_SIFS;
3288         }
3289
3290         if (sc->sc_epq[0].tx_nfree == 0) {
3291                 /* let caller free mbuf */
3292                 sc->sc_ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3293                 DPRINTF("sending raw frame, but tx ring is full\n");
3294                 return (EIO);
3295         }
3296         data = STAILQ_FIRST(&sc->sc_epq[0].tx_fh);
3297         STAILQ_REMOVE_HEAD(&sc->sc_epq[0].tx_fh, next);
3298         sc->sc_epq[0].tx_nfree--;
3299
3300         txd = (struct rt2870_txd *)&data->desc;
3301         txd->flags = RT2860_TX_QSEL_EDCA;
3302         txwi = (struct rt2860_txwi *)(txd + 1);
3303         txwi->wcid = 0xff;
3304         txwi->xflags = xflags;
3305         txwi->txop = opflags;
3306         txwi->flags = 0;        /* clear leftover garbage bits */
3307
3308         data->m = m;
3309         data->ni = ni;
3310         for (ridx = 0; ridx < RT2860_RIDX_MAX; ridx++)
3311                 if (rt2860_rates[ridx].rate == rate)
3312                         break;
3313         data->ridx = ridx;
3314
3315         run_set_tx_desc(sc, data);
3316
3317         DPRINTFN(10, "sending raw frame len=%u rate=%u\n",
3318             m->m_pkthdr.len, rate);
3319
3320         STAILQ_INSERT_TAIL(&sc->sc_epq[0].tx_qh, data, next);
3321
3322         usbd_transfer_start(sc->sc_xfer[0]);
3323
3324         return (0);
3325 }
3326
3327 static int
3328 run_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
3329     const struct ieee80211_bpf_params *params)
3330 {
3331         struct ifnet *ifp = ni->ni_ic->ic_ifp;
3332         struct run_softc *sc = ifp->if_softc;
3333         int error = 0;
3334  
3335         RUN_LOCK(sc);
3336
3337         /* prevent management frames from being sent if we're not ready */
3338         if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
3339                 error =  ENETDOWN;
3340                 goto done;
3341         }
3342
3343         if (params == NULL) {
3344                 /* tx mgt packet */
3345                 if ((error = run_tx_mgt(sc, m, ni)) != 0) {
3346                         ifp->if_oerrors++;
3347                         DPRINTF("mgt tx failed\n");
3348                         goto done;
3349                 }
3350         } else {
3351                 /* tx raw packet with param */
3352                 if ((error = run_tx_param(sc, m, ni, params)) != 0) {
3353                         ifp->if_oerrors++;
3354                         DPRINTF("tx with param failed\n");
3355                         goto done;
3356                 }
3357         }
3358
3359         ifp->if_opackets++;
3360
3361 done:
3362         RUN_UNLOCK(sc);
3363
3364         if (error != 0) {
3365                 if(m != NULL)
3366                         m_freem(m);
3367                 ieee80211_free_node(ni);
3368         }
3369
3370         return (error);
3371 }
3372
3373 static void
3374 run_start(struct ifnet *ifp)
3375 {
3376         struct run_softc *sc = ifp->if_softc;
3377         struct ieee80211_node *ni;
3378         struct mbuf *m;
3379
3380         RUN_LOCK(sc);
3381
3382         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
3383                 RUN_UNLOCK(sc);
3384                 return;
3385         }
3386
3387         for (;;) {
3388                 /* send data frames */
3389                 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
3390                 if (m == NULL)
3391                         break;
3392
3393                 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
3394                 if (run_tx(sc, m, ni) != 0) {
3395                         IFQ_DRV_PREPEND(&ifp->if_snd, m);
3396                         ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3397                         break;
3398                 }
3399         }
3400
3401         RUN_UNLOCK(sc);
3402 }
3403
3404 static int
3405 run_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
3406 {
3407         struct run_softc *sc = ifp->if_softc;
3408         struct ieee80211com *ic = sc->sc_ifp->if_l2com;
3409         struct ifreq *ifr = (struct ifreq *) data;
3410         int startall = 0;
3411         int error = 0;
3412
3413         switch (cmd) {
3414         case SIOCSIFFLAGS:
3415                 RUN_LOCK(sc);
3416                 if (ifp->if_flags & IFF_UP) {
3417                         if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)){
3418                                 startall = 1;
3419                                 run_init_locked(sc);
3420                         } else
3421                                 run_update_promisc_locked(ifp);
3422                 } else {
3423                         if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
3424                             (ic->ic_nrunning == 0 || sc->rvp_cnt <= 1)) {
3425                                         run_stop(sc);
3426                         }
3427                 }
3428                 RUN_UNLOCK(sc);
3429                 if (startall)
3430                         ieee80211_start_all(ic);
3431                 break;
3432         case SIOCGIFMEDIA:
3433                 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
3434                 break;
3435         case SIOCGIFADDR:
3436                 error = ether_ioctl(ifp, cmd, data);
3437                 break;
3438         default:
3439                 error = EINVAL;
3440                 break;
3441         }
3442
3443         return (error);
3444 }
3445
3446 static void
3447 run_set_agc(struct run_softc *sc, uint8_t agc)
3448 {
3449         uint8_t bbp;
3450
3451         if (sc->mac_ver == 0x3572) {
3452                 run_bbp_read(sc, 27, &bbp);
3453                 bbp &= ~(0x3 << 5);
3454                 run_bbp_write(sc, 27, bbp | 0 << 5);    /* select Rx0 */
3455                 run_bbp_write(sc, 66, agc);
3456                 run_bbp_write(sc, 27, bbp | 1 << 5);    /* select Rx1 */
3457                 run_bbp_write(sc, 66, agc);
3458         } else
3459                 run_bbp_write(sc, 66, agc);
3460 }
3461
3462 static void
3463 run_select_chan_group(struct run_softc *sc, int group)
3464 {
3465         uint32_t tmp;
3466         uint8_t agc;
3467
3468         run_bbp_write(sc, 62, 0x37 - sc->lna[group]);
3469         run_bbp_write(sc, 63, 0x37 - sc->lna[group]);
3470         run_bbp_write(sc, 64, 0x37 - sc->lna[group]);
3471         run_bbp_write(sc, 86, 0x00);
3472
3473         if (group == 0) {
3474                 if (sc->ext_2ghz_lna) {
3475                         run_bbp_write(sc, 82, 0x62);
3476                         run_bbp_write(sc, 75, 0x46);
3477                 } else {
3478                         run_bbp_write(sc, 82, 0x84);
3479                         run_bbp_write(sc, 75, 0x50);
3480                 }
3481         } else {
3482                 if (sc->mac_ver == 0x3572)
3483                         run_bbp_write(sc, 82, 0x94);
3484                 else
3485                         run_bbp_write(sc, 82, 0xf2);
3486                 if (sc->ext_5ghz_lna)
3487                         run_bbp_write(sc, 75, 0x46);
3488                 else 
3489                         run_bbp_write(sc, 75, 0x50);
3490         }
3491
3492         run_read(sc, RT2860_TX_BAND_CFG, &tmp);
3493         tmp &= ~(RT2860_5G_BAND_SEL_N | RT2860_5G_BAND_SEL_P);
3494         tmp |= (group == 0) ? RT2860_5G_BAND_SEL_N : RT2860_5G_BAND_SEL_P;
3495         run_write(sc, RT2860_TX_BAND_CFG, tmp);
3496
3497         /* enable appropriate Power Amplifiers and Low Noise Amplifiers */
3498         tmp = RT2860_RFTR_EN | RT2860_TRSW_EN | RT2860_LNA_PE0_EN;
3499         if (sc->nrxchains > 1)
3500                 tmp |= RT2860_LNA_PE1_EN;
3501         if (group == 0) {       /* 2GHz */
3502                 tmp |= RT2860_PA_PE_G0_EN;
3503                 if (sc->ntxchains > 1)
3504                         tmp |= RT2860_PA_PE_G1_EN;
3505         } else {                /* 5GHz */
3506                 tmp |= RT2860_PA_PE_A0_EN;
3507                 if (sc->ntxchains > 1)
3508                         tmp |= RT2860_PA_PE_A1_EN;
3509         }
3510         if (sc->mac_ver == 0x3572) {
3511                 run_rt3070_rf_write(sc, 8, 0x00);
3512                 run_write(sc, RT2860_TX_PIN_CFG, tmp);
3513                 run_rt3070_rf_write(sc, 8, 0x80);
3514         } else
3515                 run_write(sc, RT2860_TX_PIN_CFG, tmp);
3516
3517         /* set initial AGC value */
3518         if (group == 0) {       /* 2GHz band */
3519                 if (sc->mac_ver >= 0x3070)
3520                         agc = 0x1c + sc->lna[0] * 2;
3521                 else
3522                         agc = 0x2e + sc->lna[0];
3523         } else {                /* 5GHz band */
3524                 if (sc->mac_ver == 0x3572)
3525                         agc = 0x22 + (sc->lna[group] * 5) / 3;
3526                 else
3527                         agc = 0x32 + (sc->lna[group] * 5) / 3;
3528         }
3529         run_set_agc(sc, agc);
3530 }
3531
3532 static void
3533 run_rt2870_set_chan(struct run_softc *sc, uint32_t chan)
3534 {
3535         const struct rfprog *rfprog = rt2860_rf2850;
3536         uint32_t r2, r3, r4;
3537         int8_t txpow1, txpow2;
3538         int i;
3539
3540         /* find the settings for this channel (we know it exists) */
3541         for (i = 0; rfprog[i].chan != chan; i++);
3542
3543         r2 = rfprog[i].r2;
3544         if (sc->ntxchains == 1)
3545                 r2 |= 1 << 12;          /* 1T: disable Tx chain 2 */
3546         if (sc->nrxchains == 1)
3547                 r2 |= 1 << 15 | 1 << 4; /* 1R: disable Rx chains 2 & 3 */
3548         else if (sc->nrxchains == 2)
3549                 r2 |= 1 << 4;           /* 2R: disable Rx chain 3 */
3550
3551         /* use Tx power values from EEPROM */
3552         txpow1 = sc->txpow1[i];
3553         txpow2 = sc->txpow2[i];
3554         if (chan > 14) {
3555                 if (txpow1 >= 0)
3556                         txpow1 = txpow1 << 1 | 1;
3557                 else
3558                         txpow1 = (7 + txpow1) << 1;
3559                 if (txpow2 >= 0)
3560                         txpow2 = txpow2 << 1 | 1;
3561                 else
3562                         txpow2 = (7 + txpow2) << 1;
3563         }
3564         r3 = rfprog[i].r3 | txpow1 << 7;
3565         r4 = rfprog[i].r4 | sc->freq << 13 | txpow2 << 4;
3566
3567         run_rt2870_rf_write(sc, RT2860_RF1, rfprog[i].r1);
3568         run_rt2870_rf_write(sc, RT2860_RF2, r2);
3569         run_rt2870_rf_write(sc, RT2860_RF3, r3);
3570         run_rt2870_rf_write(sc, RT2860_RF4, r4);
3571
3572         run_delay(sc, 10);
3573
3574         run_rt2870_rf_write(sc, RT2860_RF1, rfprog[i].r1);
3575         run_rt2870_rf_write(sc, RT2860_RF2, r2);
3576         run_rt2870_rf_write(sc, RT2860_RF3, r3 | 1);
3577         run_rt2870_rf_write(sc, RT2860_RF4, r4);
3578
3579         run_delay(sc, 10);
3580
3581         run_rt2870_rf_write(sc, RT2860_RF1, rfprog[i].r1);
3582         run_rt2870_rf_write(sc, RT2860_RF2, r2);
3583         run_rt2870_rf_write(sc, RT2860_RF3, r3);
3584         run_rt2870_rf_write(sc, RT2860_RF4, r4);
3585 }
3586
3587 static void
3588 run_rt3070_set_chan(struct run_softc *sc, uint32_t chan)
3589 {
3590         int8_t txpow1, txpow2;
3591         uint8_t rf;
3592         int i;
3593
3594         /* RT3070 is 2GHz only */
3595         KASSERT(chan >= 1 && chan <= 14, ("wrong channel selected\n"));
3596
3597         /* find the settings for this channel (we know it exists) */
3598         for (i = 0; rt2860_rf2850[i].chan != chan; i++);
3599
3600         /* use Tx power values from EEPROM */
3601         txpow1 = sc->txpow1[i];
3602         txpow2 = sc->txpow2[i];
3603
3604         run_rt3070_rf_write(sc, 2, rt3070_freqs[i].n);
3605         run_rt3070_rf_write(sc, 3, rt3070_freqs[i].k);
3606         run_rt3070_rf_read(sc, 6, &rf);
3607         rf = (rf & ~0x03) | rt3070_freqs[i].r;
3608         run_rt3070_rf_write(sc, 6, rf);
3609
3610         /* set Tx0 power */
3611         run_rt3070_rf_read(sc, 12, &rf);
3612         rf = (rf & ~0x1f) | txpow1;
3613         run_rt3070_rf_write(sc, 12, rf);
3614
3615         /* set Tx1 power */
3616         run_rt3070_rf_read(sc, 13, &rf);
3617         rf = (rf & ~0x1f) | txpow2;
3618         run_rt3070_rf_write(sc, 13, rf);
3619
3620         run_rt3070_rf_read(sc, 1, &rf);
3621         rf &= ~0xfc;
3622         if (sc->ntxchains == 1)
3623                 rf |= 1 << 7 | 1 << 5;  /* 1T: disable Tx chains 2 & 3 */
3624         else if (sc->ntxchains == 2)
3625                 rf |= 1 << 7;           /* 2T: disable Tx chain 3 */
3626         if (sc->nrxchains == 1)
3627                 rf |= 1 << 6 | 1 << 4;  /* 1R: disable Rx chains 2 & 3 */
3628         else if (sc->nrxchains == 2)
3629                 rf |= 1 << 6;           /* 2R: disable Rx chain 3 */
3630         run_rt3070_rf_write(sc, 1, rf);
3631
3632         /* set RF offset */
3633         run_rt3070_rf_read(sc, 23, &rf);
3634         rf = (rf & ~0x7f) | sc->freq;
3635         run_rt3070_rf_write(sc, 23, rf);
3636
3637         /* program RF filter */
3638         run_rt3070_rf_read(sc, 24, &rf);        /* Tx */
3639         rf = (rf & ~0x3f) | sc->rf24_20mhz;
3640         run_rt3070_rf_write(sc, 24, rf);
3641         run_rt3070_rf_read(sc, 31, &rf);        /* Rx */
3642         rf = (rf & ~0x3f) | sc->rf24_20mhz;
3643         run_rt3070_rf_write(sc, 31, rf);
3644
3645         /* enable RF tuning */
3646         run_rt3070_rf_read(sc, 7, &rf);
3647         run_rt3070_rf_write(sc, 7, rf | 0x01);
3648 }
3649
3650 static void
3651 run_rt3572_set_chan(struct run_softc *sc, u_int chan)
3652 {
3653         int8_t txpow1, txpow2;
3654         uint32_t tmp;
3655         uint8_t rf;
3656         int i;
3657
3658         /* find the settings for this channel (we know it exists) */
3659         for (i = 0; rt2860_rf2850[i].chan != chan; i++);
3660
3661         /* use Tx power values from EEPROM */
3662         txpow1 = sc->txpow1[i];
3663         txpow2 = sc->txpow2[i];
3664
3665         if (chan <= 14) {
3666                 run_bbp_write(sc, 25, sc->bbp25);
3667                 run_bbp_write(sc, 26, sc->bbp26);
3668         } else {
3669                 /* enable IQ phase correction */
3670                 run_bbp_write(sc, 25, 0x09);
3671                 run_bbp_write(sc, 26, 0xff);
3672         }
3673
3674         run_rt3070_rf_write(sc, 2, rt3070_freqs[i].n);
3675         run_rt3070_rf_write(sc, 3, rt3070_freqs[i].k);
3676         run_rt3070_rf_read(sc, 6, &rf);
3677         rf  = (rf & ~0x0f) | rt3070_freqs[i].r;
3678         rf |= (chan <= 14) ? 0x08 : 0x04;
3679         run_rt3070_rf_write(sc, 6, rf);
3680
3681         /* set PLL mode */
3682         run_rt3070_rf_read(sc, 5, &rf);
3683         rf &= ~(0x08 | 0x04);
3684         rf |= (chan <= 14) ? 0x04 : 0x08;
3685         run_rt3070_rf_write(sc, 5, rf);
3686
3687         /* set Tx power for chain 0 */
3688         if (chan <= 14)
3689                 rf = 0x60 | txpow1;
3690         else
3691                 rf = 0xe0 | (txpow1 & 0xc) << 1 | (txpow1 & 0x3);
3692         run_rt3070_rf_write(sc, 12, rf);
3693
3694         /* set Tx power for chain 1 */
3695         if (chan <= 14)
3696                 rf = 0x60 | txpow2;
3697         else
3698                 rf = 0xe0 | (txpow2 & 0xc) << 1 | (txpow2 & 0x3);
3699         run_rt3070_rf_write(sc, 13, rf);
3700
3701         /* set Tx/Rx streams */
3702         run_rt3070_rf_read(sc, 1, &rf);
3703         rf &= ~0xfc;
3704         if (sc->ntxchains == 1)
3705                 rf |= 1 << 7 | 1 << 5;  /* 1T: disable Tx chains 2 & 3 */
3706         else if (sc->ntxchains == 2)
3707                 rf |= 1 << 7;           /* 2T: disable Tx chain 3 */
3708         if (sc->nrxchains == 1)
3709                 rf |= 1 << 6 | 1 << 4;  /* 1R: disable Rx chains 2 & 3 */
3710         else if (sc->nrxchains == 2)
3711                 rf |= 1 << 6;           /* 2R: disable Rx chain 3 */
3712         run_rt3070_rf_write(sc, 1, rf);
3713
3714         /* set RF offset */
3715         run_rt3070_rf_read(sc, 23, &rf);
3716         rf = (rf & ~0x7f) | sc->freq;
3717         run_rt3070_rf_write(sc, 23, rf);
3718
3719         /* program RF filter */
3720         rf = sc->rf24_20mhz;
3721         run_rt3070_rf_write(sc, 24, rf);        /* Tx */
3722         run_rt3070_rf_write(sc, 31, rf);        /* Rx */
3723
3724         /* enable RF tuning */
3725         run_rt3070_rf_read(sc, 7, &rf);
3726         rf = (chan <= 14) ? 0xd8 : ((rf & ~0xc8) | 0x14);
3727         run_rt3070_rf_write(sc, 7, rf);
3728
3729         /* TSSI */
3730         rf = (chan <= 14) ? 0xc3 : 0xc0;
3731         run_rt3070_rf_write(sc, 9, rf);
3732
3733         /* set loop filter 1 */
3734         run_rt3070_rf_write(sc, 10, 0xf1);
3735         /* set loop filter 2 */
3736         run_rt3070_rf_write(sc, 11, (chan <= 14) ? 0xb9 : 0x00);
3737
3738         /* set tx_mx2_ic */
3739         run_rt3070_rf_write(sc, 15, (chan <= 14) ? 0x53 : 0x43);
3740         /* set tx_mx1_ic */
3741         if (chan <= 14)
3742                 rf = 0x48 | sc->txmixgain_2ghz;
3743         else
3744                 rf = 0x78 | sc->txmixgain_5ghz;
3745         run_rt3070_rf_write(sc, 16, rf);
3746
3747         /* set tx_lo1 */
3748         run_rt3070_rf_write(sc, 17, 0x23);
3749         /* set tx_lo2 */
3750         if (chan <= 14)
3751                 rf = 0x93;
3752         else if (chan <= 64)
3753                 rf = 0xb7;
3754         else if (chan <= 128)
3755                 rf = 0x74;
3756         else
3757                 rf = 0x72;
3758         run_rt3070_rf_write(sc, 19, rf);
3759
3760         /* set rx_lo1 */
3761         if (chan <= 14)
3762                 rf = 0xb3;
3763         else if (chan <= 64)
3764                 rf = 0xf6;
3765         else if (chan <= 128)
3766                 rf = 0xf4;
3767         else
3768                 rf = 0xf3;
3769         run_rt3070_rf_write(sc, 20, rf);
3770
3771         /* set pfd_delay */
3772         if (chan <= 14)
3773                 rf = 0x15;
3774         else if (chan <= 64)
3775                 rf = 0x3d;
3776         else
3777                 rf = 0x01;
3778         run_rt3070_rf_write(sc, 25, rf);
3779
3780         /* set rx_lo2 */
3781         run_rt3070_rf_write(sc, 26, (chan <= 14) ? 0x85 : 0x87);
3782         /* set ldo_rf_vc */
3783         run_rt3070_rf_write(sc, 27, (chan <= 14) ? 0x00 : 0x01);
3784         /* set drv_cc */
3785         run_rt3070_rf_write(sc, 29, (chan <= 14) ? 0x9b : 0x9f);
3786
3787         run_read(sc, RT2860_GPIO_CTRL, &tmp);
3788         tmp &= ~0x8080;
3789         if (chan <= 14)
3790                 tmp |= 0x80;
3791         run_write(sc, RT2860_GPIO_CTRL, tmp);
3792
3793         /* enable RF tuning */
3794         run_rt3070_rf_read(sc, 7, &rf);
3795         run_rt3070_rf_write(sc, 7, rf | 0x01);
3796
3797         run_delay(sc, 2);
3798 }
3799
3800 static void
3801 run_set_rx_antenna(struct run_softc *sc, int aux)
3802 {
3803         uint32_t tmp;
3804
3805         if (aux) {
3806                 run_mcu_cmd(sc, RT2860_MCU_CMD_ANTSEL, 0);
3807                 run_read(sc, RT2860_GPIO_CTRL, &tmp);
3808                 run_write(sc, RT2860_GPIO_CTRL, (tmp & ~0x0808) | 0x08);
3809         } else {
3810                 run_mcu_cmd(sc, RT2860_MCU_CMD_ANTSEL, 1);
3811                 run_read(sc, RT2860_GPIO_CTRL, &tmp);
3812                 run_write(sc, RT2860_GPIO_CTRL, tmp & ~0x0808);
3813         }
3814 }
3815
3816 static int
3817 run_set_chan(struct run_softc *sc, struct ieee80211_channel *c)
3818 {
3819         struct ieee80211com *ic = sc->sc_ifp->if_l2com;
3820         uint32_t chan, group;
3821
3822         chan = ieee80211_chan2ieee(ic, c);
3823         if (chan == 0 || chan == IEEE80211_CHAN_ANY)
3824                 return (EINVAL);
3825
3826         if (sc->mac_ver == 0x3572)
3827                 run_rt3572_set_chan(sc, chan);
3828         else if (sc->mac_ver >= 0x3070)
3829                 run_rt3070_set_chan(sc, chan);
3830         else
3831                 run_rt2870_set_chan(sc, chan);
3832
3833         /* determine channel group */
3834         if (chan <= 14)
3835                 group = 0;
3836         else if (chan <= 64)
3837                 group = 1;
3838         else if (chan <= 128)
3839                 group = 2;
3840         else
3841                 group = 3;
3842
3843         /* XXX necessary only when group has changed! */
3844         run_select_chan_group(sc, group);
3845
3846         run_delay(sc, 10);
3847
3848         return (0);
3849 }
3850
3851 static void
3852 run_set_channel(struct ieee80211com *ic)
3853 {
3854         struct run_softc *sc = ic->ic_ifp->if_softc;
3855
3856         RUN_LOCK(sc);
3857         run_set_chan(sc, ic->ic_curchan);
3858         RUN_UNLOCK(sc);
3859
3860         return;
3861 }
3862
3863 static void
3864 run_scan_start(struct ieee80211com *ic)
3865 {
3866         struct run_softc *sc = ic->ic_ifp->if_softc;
3867         uint32_t tmp;
3868
3869         RUN_LOCK(sc);
3870
3871         /* abort TSF synchronization */
3872         run_read(sc, RT2860_BCN_TIME_CFG, &tmp);
3873         run_write(sc, RT2860_BCN_TIME_CFG,
3874             tmp & ~(RT2860_BCN_TX_EN | RT2860_TSF_TIMER_EN |
3875             RT2860_TBTT_TIMER_EN));
3876         run_set_bssid(sc, sc->sc_ifp->if_broadcastaddr);
3877
3878         RUN_UNLOCK(sc);
3879
3880         return;
3881 }
3882
3883 static void
3884 run_scan_end(struct ieee80211com *ic)
3885 {
3886         struct run_softc *sc = ic->ic_ifp->if_softc;
3887
3888         RUN_LOCK(sc);
3889
3890         run_enable_tsf_sync(sc);
3891         /* XXX keep local copy */
3892         run_set_bssid(sc, sc->sc_bssid);
3893
3894         RUN_UNLOCK(sc);
3895
3896         return;
3897 }
3898
3899 /*
3900  * Could be called from ieee80211_node_timeout()
3901  * (non-sleepable thread)
3902  */
3903 static void
3904 run_update_beacon(struct ieee80211vap *vap, int item)
3905 {
3906         struct ieee80211com *ic = vap->iv_ic;
3907         struct run_softc *sc = ic->ic_ifp->if_softc;
3908         uint32_t i;
3909
3910         i = RUN_CMDQ_GET(&sc->cmdq_store);
3911         DPRINTF("cmdq_store=%d\n", i);
3912         sc->cmdq[i].func = run_update_beacon_cb;
3913         sc->cmdq[i].arg0 = vap;
3914         ieee80211_runtask(ic, &sc->cmdq_task);
3915
3916         return;
3917 }
3918
3919 static void
3920 run_update_beacon_cb(void *arg)
3921 {
3922         struct ieee80211vap *vap = arg;
3923         struct ieee80211com *ic = vap->iv_ic;
3924         struct run_softc *sc = ic->ic_ifp->if_softc;
3925         struct rt2860_txwi txwi;
3926         struct mbuf *m;
3927         uint8_t ridx;
3928
3929         if (vap->iv_bss->ni_chan == IEEE80211_CHAN_ANYC)
3930                 return;
3931
3932         if ((m = ieee80211_beacon_alloc(vap->iv_bss, &RUN_VAP(vap)->bo)) == NULL)
3933                 return;
3934
3935         memset(&txwi, 0, sizeof txwi);
3936         txwi.wcid = 0xff;
3937         txwi.len = htole16(m->m_pkthdr.len);
3938         /* send beacons at the lowest available rate */
3939         ridx = (ic->ic_curmode == IEEE80211_MODE_11A) ?
3940             RT2860_RIDX_OFDM6 : RT2860_RIDX_CCK1;
3941         txwi.phy = htole16(rt2860_rates[ridx].mcs);
3942         if (rt2860_rates[ridx].phy == IEEE80211_T_OFDM)
3943                 txwi.phy |= htole16(RT2860_PHY_OFDM);
3944         txwi.txop = RT2860_TX_TXOP_HT;
3945         txwi.flags = RT2860_TX_TS;
3946         txwi.xflags = RT2860_TX_NSEQ;
3947
3948         run_write_region_1(sc, RT2860_BCN_BASE(RUN_VAP(vap)->rvp_id),
3949             (uint8_t *)&txwi, sizeof txwi);
3950         run_write_region_1(sc, RT2860_BCN_BASE(RUN_VAP(vap)->rvp_id) + sizeof txwi,
3951             mtod(m, uint8_t *), (m->m_pkthdr.len + 1) & ~1);    /* roundup len */
3952
3953         m_freem(m);
3954
3955         return;
3956 }
3957
3958 static void
3959 run_updateprot(struct ieee80211com *ic)
3960 {
3961         struct run_softc *sc = ic->ic_ifp->if_softc;
3962         uint32_t tmp;
3963
3964         tmp = RT2860_RTSTH_EN | RT2860_PROT_NAV_SHORT | RT2860_TXOP_ALLOW_ALL;
3965         /* setup protection frame rate (MCS code) */
3966         tmp |= (ic->ic_curmode == IEEE80211_MODE_11A) ?
3967             rt2860_rates[RT2860_RIDX_OFDM6].mcs :
3968             rt2860_rates[RT2860_RIDX_CCK11].mcs;
3969
3970         /* CCK frames don't require protection */
3971         run_write(sc, RT2860_CCK_PROT_CFG, tmp);
3972         if (ic->ic_flags & IEEE80211_F_USEPROT) {
3973                 if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
3974                         tmp |= RT2860_PROT_CTRL_RTS_CTS;
3975                 else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
3976                         tmp |= RT2860_PROT_CTRL_CTS;
3977         }
3978         run_write(sc, RT2860_OFDM_PROT_CFG, tmp);
3979 }
3980
3981 static void
3982 run_usb_timeout_cb(void *arg)
3983 {
3984         struct ieee80211vap *vap = arg;
3985         struct run_softc *sc = vap->iv_ic->ic_ifp->if_softc;
3986
3987         RUN_LOCK_ASSERT(sc, MA_OWNED);
3988
3989         if(vap->iv_state == IEEE80211_S_RUN &&
3990             vap->iv_opmode != IEEE80211_M_STA)
3991                 run_reset_livelock(sc);
3992         else if (vap->iv_state == IEEE80211_S_SCAN) {
3993                 DPRINTF("timeout caused by scan\n");
3994                 /* cancel bgscan */
3995                 ieee80211_cancel_scan(vap);
3996         } else
3997                 DPRINTF("timeout by unknown cause\n");
3998 }
3999
4000 static void
4001 run_reset_livelock(struct run_softc *sc)
4002 {
4003         uint32_t tmp;
4004
4005         RUN_LOCK_ASSERT(sc, MA_OWNED);
4006
4007         /*
4008          * In IBSS or HostAP modes (when the hardware sends beacons), the MAC
4009          * can run into a livelock and start sending CTS-to-self frames like
4010          * crazy if protection is enabled.  Reset MAC/BBP for a while
4011          */
4012         run_read(sc, RT2860_DEBUG, &tmp);
4013         DPRINTFN(3, "debug reg %08x\n", tmp);
4014         if ((tmp & (1 << 29)) && (tmp & (1 << 7 | 1 << 5))) {
4015                 DPRINTF("CTS-to-self livelock detected\n");
4016                 run_write(sc, RT2860_MAC_SYS_CTRL, RT2860_MAC_SRST);
4017                 run_delay(sc, 1);
4018                 run_write(sc, RT2860_MAC_SYS_CTRL,
4019                     RT2860_MAC_RX_EN | RT2860_MAC_TX_EN);
4020         }
4021 }
4022
4023 static void
4024 run_update_promisc_locked(struct ifnet *ifp)
4025 {
4026         struct run_softc *sc = ifp->if_softc;
4027         uint32_t tmp;
4028
4029         run_read(sc, RT2860_RX_FILTR_CFG, &tmp);
4030
4031         tmp |= RT2860_DROP_UC_NOME;
4032         if (ifp->if_flags & IFF_PROMISC)
4033                 tmp &= ~RT2860_DROP_UC_NOME;
4034
4035         run_write(sc, RT2860_RX_FILTR_CFG, tmp);
4036
4037         DPRINTF("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
4038             "entering" : "leaving");
4039 }
4040
4041 static void
4042 run_update_promisc(struct ifnet *ifp)
4043 {
4044         struct run_softc *sc = ifp->if_softc;
4045
4046         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
4047                 return;
4048
4049         RUN_LOCK(sc);
4050         run_update_promisc_locked(ifp);
4051         RUN_UNLOCK(sc);
4052 }
4053
4054 static void
4055 run_enable_tsf_sync(struct run_softc *sc)
4056 {
4057         struct ieee80211com *ic = sc->sc_ifp->if_l2com;
4058         struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
4059         uint32_t tmp;
4060
4061         DPRINTF("rvp_id=%d ic_opmode=%d\n", RUN_VAP(vap)->rvp_id, ic->ic_opmode);
4062
4063         run_read(sc, RT2860_BCN_TIME_CFG, &tmp);
4064         tmp &= ~0x1fffff;
4065         tmp |= vap->iv_bss->ni_intval * 16;
4066         tmp |= RT2860_TSF_TIMER_EN | RT2860_TBTT_TIMER_EN;
4067
4068         if (ic->ic_opmode == IEEE80211_M_STA) {
4069                 /*
4070                  * Local TSF is always updated with remote TSF on beacon
4071                  * reception.
4072                  */
4073                 tmp |= 1 << RT2860_TSF_SYNC_MODE_SHIFT;
4074         } else if (ic->ic_opmode == IEEE80211_M_IBSS) {
4075                 tmp |= RT2860_BCN_TX_EN;
4076                 /*
4077                  * Local TSF is updated with remote TSF on beacon reception
4078                  * only if the remote TSF is greater than local TSF.
4079                  */
4080                 tmp |= 2 << RT2860_TSF_SYNC_MODE_SHIFT;
4081         } else if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
4082                     ic->ic_opmode == IEEE80211_M_MBSS) {
4083                 tmp |= RT2860_BCN_TX_EN;
4084                 /* SYNC with nobody */
4085                 tmp |= 3 << RT2860_TSF_SYNC_MODE_SHIFT;
4086         } else {
4087                 DPRINTF("Enabling TSF failed. undefined opmode\n");
4088                 return;
4089         }
4090
4091         run_write(sc, RT2860_BCN_TIME_CFG, tmp);
4092 }
4093
4094 static void
4095 run_enable_mrr(struct run_softc *sc)
4096 {
4097 #define CCK(mcs)        (mcs)
4098 #define OFDM(mcs)       (1 << 3 | (mcs))
4099         run_write(sc, RT2860_LG_FBK_CFG0,
4100             OFDM(6) << 28 |     /* 54->48 */
4101             OFDM(5) << 24 |     /* 48->36 */
4102             OFDM(4) << 20 |     /* 36->24 */
4103             OFDM(3) << 16 |     /* 24->18 */
4104             OFDM(2) << 12 |     /* 18->12 */
4105             OFDM(1) <<  8 |     /* 12-> 9 */
4106             OFDM(0) <<  4 |     /*  9-> 6 */
4107             OFDM(0));           /*  6-> 6 */
4108
4109         run_write(sc, RT2860_LG_FBK_CFG1,
4110             CCK(2) << 12 |      /* 11->5.5 */
4111             CCK(1) <<  8 |      /* 5.5-> 2 */
4112             CCK(0) <<  4 |      /*   2-> 1 */
4113             CCK(0));            /*   1-> 1 */
4114 #undef OFDM
4115 #undef CCK
4116 }
4117
4118 static void
4119 run_set_txpreamble(struct run_softc *sc)
4120 {
4121         struct ieee80211com *ic = sc->sc_ifp->if_l2com;
4122         uint32_t tmp;
4123
4124         run_read(sc, RT2860_AUTO_RSP_CFG, &tmp);
4125         if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
4126                 tmp |= RT2860_CCK_SHORT_EN;
4127         else
4128                 tmp &= ~RT2860_CCK_SHORT_EN;
4129         run_write(sc, RT2860_AUTO_RSP_CFG, tmp);
4130 }
4131
4132 static void
4133 run_set_basicrates(struct run_softc *sc)
4134 {
4135         struct ieee80211com *ic = sc->sc_ifp->if_l2com;
4136
4137         /* set basic rates mask */
4138         if (ic->ic_curmode == IEEE80211_MODE_11B)
4139                 run_write(sc, RT2860_LEGACY_BASIC_RATE, 0x003);
4140         else if (ic->ic_curmode == IEEE80211_MODE_11A)
4141                 run_write(sc, RT2860_LEGACY_BASIC_RATE, 0x150);
4142         else    /* 11g */
4143                 run_write(sc, RT2860_LEGACY_BASIC_RATE, 0x15f);
4144 }
4145
4146 static void
4147 run_set_leds(struct run_softc *sc, uint16_t which)
4148 {
4149         (void)run_mcu_cmd(sc, RT2860_MCU_CMD_LEDS,
4150             which | (sc->leds & 0x7f));
4151 }
4152
4153 static void
4154 run_set_bssid(struct run_softc *sc, const uint8_t *bssid)
4155 {
4156         run_write(sc, RT2860_MAC_BSSID_DW0,
4157             bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24);
4158         run_write(sc, RT2860_MAC_BSSID_DW1,
4159             bssid[4] | bssid[5] << 8);
4160 }
4161
4162 static void
4163 run_set_macaddr(struct run_softc *sc, const uint8_t *addr)
4164 {
4165         run_write(sc, RT2860_MAC_ADDR_DW0,
4166             addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
4167         run_write(sc, RT2860_MAC_ADDR_DW1,
4168             addr[4] | addr[5] << 8 | 0xff << 16);
4169 }
4170
4171 /* ARGSUSED */
4172 static void
4173 run_updateslot(struct ifnet *ifp)
4174 {
4175         struct run_softc *sc = ifp->if_softc;
4176         struct ieee80211com *ic = ifp->if_l2com;
4177         uint32_t tmp;
4178
4179         run_read(sc, RT2860_BKOFF_SLOT_CFG, &tmp);
4180         tmp &= ~0xff;
4181         tmp |= (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
4182         run_write(sc, RT2860_BKOFF_SLOT_CFG, tmp);
4183 }
4184
4185 static void
4186 run_update_mcast(struct ifnet *ifp)
4187 {
4188         /* h/w filter supports getting everything or nothing */
4189         ifp->if_flags |= IFF_ALLMULTI;
4190 }
4191
4192 static int8_t
4193 run_rssi2dbm(struct run_softc *sc, uint8_t rssi, uint8_t rxchain)
4194 {
4195         struct ieee80211com *ic = sc->sc_ifp->if_l2com;
4196         struct ieee80211_channel *c = ic->ic_curchan;
4197         int delta;
4198
4199         if (IEEE80211_IS_CHAN_5GHZ(c)) {
4200                 uint32_t chan = ieee80211_chan2ieee(ic, c);
4201                 delta = sc->rssi_5ghz[rxchain];
4202
4203                 /* determine channel group */
4204                 if (chan <= 64)
4205                         delta -= sc->lna[1];
4206                 else if (chan <= 128)
4207                         delta -= sc->lna[2];
4208                 else
4209                         delta -= sc->lna[3];
4210         } else
4211                 delta = sc->rssi_2ghz[rxchain] - sc->lna[0];
4212
4213         return (-12 - delta - rssi);
4214 }
4215
4216 static int
4217 run_bbp_init(struct run_softc *sc)
4218 {
4219         int i, error, ntries;
4220         uint8_t bbp0;
4221
4222         /* wait for BBP to wake up */
4223         for (ntries = 0; ntries < 20; ntries++) {
4224                 if ((error = run_bbp_read(sc, 0, &bbp0)) != 0)
4225                         return error;
4226                 if (bbp0 != 0 && bbp0 != 0xff)
4227                         break;
4228         }
4229         if (ntries == 20)
4230                 return (ETIMEDOUT);
4231
4232         /* initialize BBP registers to default values */
4233         for (i = 0; i < N(rt2860_def_bbp); i++) {
4234                 run_bbp_write(sc, rt2860_def_bbp[i].reg,
4235                     rt2860_def_bbp[i].val);
4236         }
4237
4238         /* fix BBP84 for RT2860E */
4239         if (sc->mac_ver == 0x2860 && sc->mac_rev != 0x0101)
4240                 run_bbp_write(sc, 84, 0x19);
4241
4242         if (sc->mac_ver >= 0x3070) {
4243                 run_bbp_write(sc, 79, 0x13);
4244                 run_bbp_write(sc, 80, 0x05);
4245                 run_bbp_write(sc, 81, 0x33);
4246         } else if (sc->mac_ver == 0x2860 && sc->mac_rev == 0x0100) {
4247                 run_bbp_write(sc, 69, 0x16);
4248                 run_bbp_write(sc, 73, 0x12);
4249         }
4250         return (0);
4251 }
4252
4253 static int
4254 run_rt3070_rf_init(struct run_softc *sc)
4255 {
4256         uint32_t tmp;
4257         uint8_t rf, target, bbp4;
4258         int i;
4259
4260         run_rt3070_rf_read(sc, 30, &rf);
4261         /* toggle RF R30 bit 7 */
4262         run_rt3070_rf_write(sc, 30, rf | 0x80);
4263         run_delay(sc, 10);
4264         run_rt3070_rf_write(sc, 30, rf & ~0x80);
4265
4266         /* initialize RF registers to default value */
4267         if (sc->mac_ver == 0x3572) {
4268                 for (i = 0; i < N(rt3572_def_rf); i++) {
4269                         run_rt3070_rf_write(sc, rt3572_def_rf[i].reg,
4270                             rt3572_def_rf[i].val);
4271                 }
4272         } else {
4273                 for (i = 0; i < N(rt3070_def_rf); i++) {
4274                         run_rt3070_rf_write(sc, rt3070_def_rf[i].reg,
4275                             rt3070_def_rf[i].val);
4276                 }
4277         }
4278
4279         if (sc->mac_ver == 0x3070) {
4280                 /* change voltage from 1.2V to 1.35V for RT3070 */
4281                 run_read(sc, RT3070_LDO_CFG0, &tmp);
4282                 tmp = (tmp & ~0x0f000000) | 0x0d000000;
4283                 run_write(sc, RT3070_LDO_CFG0, tmp);
4284
4285         } else if (sc->mac_ver == 0x3071) {
4286                 run_rt3070_rf_read(sc, 6, &rf);
4287                 run_rt3070_rf_write(sc, 6, rf | 0x40);
4288                 run_rt3070_rf_write(sc, 31, 0x14);
4289
4290                 run_read(sc, RT3070_LDO_CFG0, &tmp);
4291                 tmp &= ~0x1f000000;
4292                 if (sc->mac_rev < 0x0211)
4293                         tmp |= 0x0d000000;      /* 1.3V */
4294                 else
4295                         tmp |= 0x01000000;      /* 1.2V */
4296                 run_write(sc, RT3070_LDO_CFG0, tmp);
4297
4298                 /* patch LNA_PE_G1 */
4299                 run_read(sc, RT3070_GPIO_SWITCH, &tmp);
4300                 run_write(sc, RT3070_GPIO_SWITCH, tmp & ~0x20);
4301
4302         } else if (sc->mac_ver == 0x3572) {
4303                 run_rt3070_rf_read(sc, 6, &rf);
4304                 run_rt3070_rf_write(sc, 6, rf | 0x40);
4305
4306                 /* increase voltage from 1.2V to 1.35V */
4307                 run_read(sc, RT3070_LDO_CFG0, &tmp);
4308                 tmp = (tmp & ~0x1f000000) | 0x0d000000;
4309                 run_write(sc, RT3070_LDO_CFG0, tmp);
4310
4311                 if (sc->mac_rev < 0x0211 || !sc->patch_dac) {
4312                         run_delay(sc, 1);       /* wait for 1msec */
4313                         /* decrease voltage back to 1.2V */
4314                         tmp = (tmp & ~0x1f000000) | 0x01000000;
4315                         run_write(sc, RT3070_LDO_CFG0, tmp);
4316                 }
4317         }
4318
4319         /* select 20MHz bandwidth */
4320         run_rt3070_rf_read(sc, 31, &rf);
4321         run_rt3070_rf_write(sc, 31, rf & ~0x20);
4322
4323         /* calibrate filter for 20MHz bandwidth */
4324         sc->rf24_20mhz = 0x1f;  /* default value */
4325         target = (sc->mac_ver < 0x3071) ? 0x16 : 0x13;
4326         run_rt3070_filter_calib(sc, 0x07, target, &sc->rf24_20mhz);
4327
4328         /* select 40MHz bandwidth */
4329         run_bbp_read(sc, 4, &bbp4);
4330         run_bbp_write(sc, 4, (bbp4 & ~0x08) | 0x10);
4331         run_rt3070_rf_read(sc, 31, &rf);
4332         run_rt3070_rf_write(sc, 31, rf | 0x20);
4333
4334         /* calibrate filter for 40MHz bandwidth */
4335         sc->rf24_40mhz = 0x2f;  /* default value */
4336         target = (sc->mac_ver < 0x3071) ? 0x19 : 0x15;
4337         run_rt3070_filter_calib(sc, 0x27, target, &sc->rf24_40mhz);
4338
4339         /* go back to 20MHz bandwidth */
4340         run_bbp_read(sc, 4, &bbp4);
4341         run_bbp_write(sc, 4, bbp4 & ~0x18);
4342
4343         if (sc->mac_ver == 0x3572) {
4344                 /* save default BBP registers 25 and 26 values */
4345                 run_bbp_read(sc, 25, &sc->bbp25);
4346                 run_bbp_read(sc, 26, &sc->bbp26);
4347         } else if (sc->mac_rev < 0x0211)
4348                 run_rt3070_rf_write(sc, 27, 0x03);
4349
4350         run_read(sc, RT3070_OPT_14, &tmp);
4351         run_write(sc, RT3070_OPT_14, tmp | 1);
4352
4353         if (sc->mac_ver == 0x3070 || sc->mac_ver == 0x3071) {
4354                 run_rt3070_rf_read(sc, 17, &rf);
4355                 rf &= ~RT3070_TX_LO1;
4356                 if ((sc->mac_ver == 0x3070 ||
4357                      (sc->mac_ver == 0x3071 && sc->mac_rev >= 0x0211)) &&
4358                     !sc->ext_2ghz_lna)
4359                         rf |= 0x20;     /* fix for long range Rx issue */
4360                 if (sc->txmixgain_2ghz >= 1)
4361                         rf = (rf & ~0x7) | sc->txmixgain_2ghz;
4362                 run_rt3070_rf_write(sc, 17, rf);
4363         }
4364
4365         if (sc->mac_rev == 0x3071) {
4366                 run_rt3070_rf_read(sc, 1, &rf);
4367                 rf &= ~(RT3070_RX0_PD | RT3070_TX0_PD);
4368                 rf |= RT3070_RF_BLOCK | RT3070_RX1_PD | RT3070_TX1_PD;
4369                 run_rt3070_rf_write(sc, 1, rf);
4370
4371                 run_rt3070_rf_read(sc, 15, &rf);
4372                 run_rt3070_rf_write(sc, 15, rf & ~RT3070_TX_LO2);
4373
4374                 run_rt3070_rf_read(sc, 20, &rf);
4375                 run_rt3070_rf_write(sc, 20, rf & ~RT3070_RX_LO1);
4376
4377                 run_rt3070_rf_read(sc, 21, &rf);
4378                 run_rt3070_rf_write(sc, 21, rf & ~RT3070_RX_LO2);
4379         }
4380
4381         if (sc->mac_ver == 0x3070 || sc->mac_ver == 0x3071) {
4382                 /* fix Tx to Rx IQ glitch by raising RF voltage */
4383                 run_rt3070_rf_read(sc, 27, &rf);
4384                 rf &= ~0x77;
4385                 if (sc->mac_rev < 0x0211)
4386                         rf |= 0x03;
4387                 run_rt3070_rf_write(sc, 27, rf);
4388         }
4389         return (0);
4390 }
4391
4392 static int
4393 run_rt3070_filter_calib(struct run_softc *sc, uint8_t init, uint8_t target,
4394     uint8_t *val)
4395 {
4396         uint8_t rf22, rf24;
4397         uint8_t bbp55_pb, bbp55_sb, delta;
4398         int ntries;
4399
4400         /* program filter */
4401         run_rt3070_rf_read(sc, 24, &rf24);
4402         rf24 = (rf24 & 0xc0) | init;    /* initial filter value */
4403         run_rt3070_rf_write(sc, 24, rf24);
4404
4405         /* enable baseband loopback mode */
4406         run_rt3070_rf_read(sc, 22, &rf22);
4407         run_rt3070_rf_write(sc, 22, rf22 | 0x01);
4408
4409         /* set power and frequency of passband test tone */
4410         run_bbp_write(sc, 24, 0x00);
4411         for (ntries = 0; ntries < 100; ntries++) {
4412                 /* transmit test tone */
4413                 run_bbp_write(sc, 25, 0x90);
4414                 run_delay(sc, 10);
4415                 /* read received power */
4416                 run_bbp_read(sc, 55, &bbp55_pb);
4417                 if (bbp55_pb != 0)
4418                         break;
4419         }
4420         if (ntries == 100)
4421                 return ETIMEDOUT;
4422
4423         /* set power and frequency of stopband test tone */
4424         run_bbp_write(sc, 24, 0x06);
4425         for (ntries = 0; ntries < 100; ntries++) {
4426                 /* transmit test tone */
4427                 run_bbp_write(sc, 25, 0x90);
4428                 run_delay(sc, 10);
4429                 /* read received power */
4430                 run_bbp_read(sc, 55, &bbp55_sb);
4431
4432                 delta = bbp55_pb - bbp55_sb;
4433                 if (delta > target)
4434                         break;
4435
4436                 /* reprogram filter */
4437                 rf24++;
4438                 run_rt3070_rf_write(sc, 24, rf24);
4439         }
4440         if (ntries < 100) {
4441                 if (rf24 != init)
4442                         rf24--; /* backtrack */
4443                 *val = rf24;
4444                 run_rt3070_rf_write(sc, 24, rf24);
4445         }
4446
4447         /* restore initial state */
4448         run_bbp_write(sc, 24, 0x00);
4449
4450         /* disable baseband loopback mode */
4451         run_rt3070_rf_read(sc, 22, &rf22);
4452         run_rt3070_rf_write(sc, 22, rf22 & ~0x01);
4453
4454         return (0);
4455 }
4456
4457 static void
4458 run_rt3070_rf_setup(struct run_softc *sc)
4459 {
4460         uint8_t bbp, rf;
4461         int i;
4462
4463         if (sc->mac_ver == 0x3572) {
4464                 /* enable DC filter */
4465                 if (sc->mac_rev >= 0x0201)
4466                         run_bbp_write(sc, 103, 0xc0);
4467
4468                 run_bbp_read(sc, 138, &bbp);
4469                 if (sc->ntxchains == 1)
4470                         bbp |= 0x20;    /* turn off DAC1 */
4471                 if (sc->nrxchains == 1)
4472                         bbp &= ~0x02;   /* turn off ADC1 */
4473                 run_bbp_write(sc, 138, bbp);
4474
4475                 if (sc->mac_rev >= 0x0211) {
4476                         /* improve power consumption */
4477                         run_bbp_read(sc, 31, &bbp);
4478                         run_bbp_write(sc, 31, bbp & ~0x03);
4479                 }
4480
4481                 run_rt3070_rf_read(sc, 16, &rf);
4482                 rf = (rf & ~0x07) | sc->txmixgain_2ghz;
4483                 run_rt3070_rf_write(sc, 16, rf);
4484
4485         } else if (sc->mac_ver == 0x3071) {
4486                 /* enable DC filter */
4487                 if (sc->mac_rev >= 0x0201)
4488                         run_bbp_write(sc, 103, 0xc0);
4489
4490                 run_bbp_read(sc, 138, &bbp);
4491                 if (sc->ntxchains == 1)
4492                         bbp |= 0x20;    /* turn off DAC1 */
4493                 if (sc->nrxchains == 1)
4494                         bbp &= ~0x02;   /* turn off ADC1 */
4495                 run_bbp_write(sc, 138, bbp);
4496
4497                 if (sc->mac_rev >= 0x0211) {
4498                         /* improve power consumption */
4499                         run_bbp_read(sc, 31, &bbp);
4500                         run_bbp_write(sc, 31, bbp & ~0x03);
4501                 }
4502
4503                 run_write(sc, RT2860_TX_SW_CFG1, 0);
4504                 if (sc->mac_rev < 0x0211) {
4505                         run_write(sc, RT2860_TX_SW_CFG2,
4506                             sc->patch_dac ? 0x2c : 0x0f);
4507                 } else
4508                         run_write(sc, RT2860_TX_SW_CFG2, 0);
4509
4510         } else if (sc->mac_ver == 0x3070) {
4511                 if (sc->mac_rev >= 0x0201) {
4512                         /* enable DC filter */
4513                         run_bbp_write(sc, 103, 0xc0);
4514
4515                         /* improve power consumption */
4516                         run_bbp_read(sc, 31, &bbp);
4517                         run_bbp_write(sc, 31, bbp & ~0x03);
4518                 }
4519
4520                 if (sc->mac_rev < 0x0211) {
4521                         run_write(sc, RT2860_TX_SW_CFG1, 0);
4522                         run_write(sc, RT2860_TX_SW_CFG2, 0x2c);
4523                 } else
4524                         run_write(sc, RT2860_TX_SW_CFG2, 0);
4525         }
4526
4527         /* initialize RF registers from ROM for >=RT3071*/
4528         if (sc->mac_ver >= 0x3071) {
4529                 for (i = 0; i < 10; i++) {
4530                         if (sc->rf[i].reg == 0 || sc->rf[i].reg == 0xff)
4531                                 continue;
4532                         run_rt3070_rf_write(sc, sc->rf[i].reg, sc->rf[i].val);
4533                 }
4534         }
4535 }
4536
4537 static int
4538 run_txrx_enable(struct run_softc *sc)
4539 {
4540         struct ieee80211com *ic = sc->sc_ifp->if_l2com;
4541         uint32_t tmp;
4542         int error, ntries;
4543
4544         run_write(sc, RT2860_MAC_SYS_CTRL, RT2860_MAC_TX_EN);
4545         for (ntries = 0; ntries < 200; ntries++) {
4546                 if ((error = run_read(sc, RT2860_WPDMA_GLO_CFG, &tmp)) != 0)
4547                         return error;
4548                 if ((tmp & (RT2860_TX_DMA_BUSY | RT2860_RX_DMA_BUSY)) == 0)
4549                         break;
4550                 run_delay(sc, 50);
4551         }
4552         if (ntries == 200)
4553                 return ETIMEDOUT;
4554
4555         run_delay(sc, 50);
4556
4557         tmp |= RT2860_RX_DMA_EN | RT2860_TX_DMA_EN | RT2860_TX_WB_DDONE;
4558         run_write(sc, RT2860_WPDMA_GLO_CFG, tmp);
4559
4560         /* enable Rx bulk aggregation (set timeout and limit) */
4561         tmp = RT2860_USB_TX_EN | RT2860_USB_RX_EN | RT2860_USB_RX_AGG_EN |
4562             RT2860_USB_RX_AGG_TO(128) | RT2860_USB_RX_AGG_LMT(2);
4563         run_write(sc, RT2860_USB_DMA_CFG, tmp);
4564
4565         /* set Rx filter */
4566         tmp = RT2860_DROP_CRC_ERR | RT2860_DROP_PHY_ERR;
4567         if (ic->ic_opmode != IEEE80211_M_MONITOR) {
4568                 tmp |= RT2860_DROP_UC_NOME | RT2860_DROP_DUPL |
4569                     RT2860_DROP_CTS | RT2860_DROP_BA | RT2860_DROP_ACK |
4570                     RT2860_DROP_VER_ERR | RT2860_DROP_CTRL_RSV |
4571                     RT2860_DROP_CFACK | RT2860_DROP_CFEND;
4572                 if (ic->ic_opmode == IEEE80211_M_STA)
4573                         tmp |= RT2860_DROP_RTS | RT2860_DROP_PSPOLL;
4574         }
4575         run_write(sc, RT2860_RX_FILTR_CFG, tmp);
4576
4577         run_write(sc, RT2860_MAC_SYS_CTRL,
4578             RT2860_MAC_RX_EN | RT2860_MAC_TX_EN);
4579
4580         return (0);
4581 }
4582
4583 static void
4584 run_init_locked(struct run_softc *sc)
4585 {
4586         struct ifnet *ifp = sc->sc_ifp;
4587         struct ieee80211com *ic = ifp->if_l2com;
4588         uint32_t tmp;
4589         uint8_t bbp1, bbp3;
4590         int i;
4591         int ridx;
4592         int ntries;
4593
4594         if (ic->ic_nrunning > 1)
4595                 return;
4596
4597         run_stop(sc);
4598
4599         if (run_load_microcode(sc) != 0) {
4600                 device_printf(sc->sc_dev, "could not load 8051 microcode\n");
4601                 goto fail;
4602         }
4603
4604         for (ntries = 0; ntries < 100; ntries++) {
4605                 if (run_read(sc, RT2860_ASIC_VER_ID, &tmp) != 0)
4606                         goto fail;
4607                 if (tmp != 0 && tmp != 0xffffffff)
4608                         break;
4609                 run_delay(sc, 10);
4610         }
4611         if (ntries == 100)
4612                 goto fail;
4613
4614         for (i = 0; i != RUN_EP_QUEUES; i++)
4615                 run_setup_tx_list(sc, &sc->sc_epq[i]);
4616
4617         run_set_macaddr(sc, IF_LLADDR(ifp));
4618
4619         for (ntries = 0; ntries < 100; ntries++) {
4620                 if (run_read(sc, RT2860_WPDMA_GLO_CFG, &tmp) != 0)
4621                         goto fail;
4622                 if ((tmp & (RT2860_TX_DMA_BUSY | RT2860_RX_DMA_BUSY)) == 0)
4623                         break;
4624                 run_delay(sc, 10);
4625         }
4626         if (ntries == 100) {
4627                 device_printf(sc->sc_dev, "timeout waiting for DMA engine\n");
4628                 goto fail;
4629         }
4630         tmp &= 0xff0;
4631         tmp |= RT2860_TX_WB_DDONE;
4632         run_write(sc, RT2860_WPDMA_GLO_CFG, tmp);
4633
4634         /* turn off PME_OEN to solve high-current issue */
4635         run_read(sc, RT2860_SYS_CTRL, &tmp);
4636         run_write(sc, RT2860_SYS_CTRL, tmp & ~RT2860_PME_OEN);
4637
4638         run_write(sc, RT2860_MAC_SYS_CTRL,
4639             RT2860_BBP_HRST | RT2860_MAC_SRST);
4640         run_write(sc, RT2860_USB_DMA_CFG, 0);
4641
4642         if (run_reset(sc) != 0) {
4643                 device_printf(sc->sc_dev, "could not reset chipset\n");
4644                 goto fail;
4645         }
4646
4647         run_write(sc, RT2860_MAC_SYS_CTRL, 0);
4648
4649         /* init Tx power for all Tx rates (from EEPROM) */
4650         for (ridx = 0; ridx < 5; ridx++) {
4651                 if (sc->txpow20mhz[ridx] == 0xffffffff)
4652                         continue;
4653                 run_write(sc, RT2860_TX_PWR_CFG(ridx), sc->txpow20mhz[ridx]);
4654         }
4655
4656         for (i = 0; i < N(rt2870_def_mac); i++)
4657                 run_write(sc, rt2870_def_mac[i].reg, rt2870_def_mac[i].val);
4658         run_write(sc, RT2860_WMM_AIFSN_CFG, 0x00002273);
4659         run_write(sc, RT2860_WMM_CWMIN_CFG, 0x00002344);
4660         run_write(sc, RT2860_WMM_CWMAX_CFG, 0x000034aa);
4661
4662         if (sc->mac_ver >= 0x3070) {
4663                 /* set delay of PA_PE assertion to 1us (unit of 0.25us) */
4664                 run_write(sc, RT2860_TX_SW_CFG0,
4665                     4 << RT2860_DLY_PAPE_EN_SHIFT);
4666         }
4667
4668         /* wait while MAC is busy */
4669         for (ntries = 0; ntries < 100; ntries++) {
4670                 if (run_read(sc, RT2860_MAC_STATUS_REG, &tmp) != 0)
4671                         goto fail;
4672                 if (!(tmp & (RT2860_RX_STATUS_BUSY | RT2860_TX_STATUS_BUSY)))
4673                         break;
4674                 run_delay(sc, 10);
4675         }
4676         if (ntries == 100)
4677                 goto fail;
4678
4679         /* clear Host to MCU mailbox */
4680         run_write(sc, RT2860_H2M_BBPAGENT, 0);
4681         run_write(sc, RT2860_H2M_MAILBOX, 0);
4682         run_delay(sc, 10);
4683
4684         if (run_bbp_init(sc) != 0) {
4685                 device_printf(sc->sc_dev, "could not initialize BBP\n");
4686                 goto fail;
4687         }
4688
4689         /* abort TSF synchronization */
4690         run_read(sc, RT2860_BCN_TIME_CFG, &tmp);
4691         tmp &= ~(RT2860_BCN_TX_EN | RT2860_TSF_TIMER_EN |
4692             RT2860_TBTT_TIMER_EN);
4693         run_write(sc, RT2860_BCN_TIME_CFG, tmp);
4694
4695         /* clear RX WCID search table */
4696         run_set_region_4(sc, RT2860_WCID_ENTRY(0), 0, 512);
4697         /* clear WCID attribute table */
4698         run_set_region_4(sc, RT2860_WCID_ATTR(0), 0, 8 * 32);
4699
4700         /* hostapd sets a key before init. So, don't clear it. */
4701         if (sc->cmdq_key_set != RUN_CMDQ_GO) {
4702                 /* clear shared key table */
4703                 run_set_region_4(sc, RT2860_SKEY(0, 0), 0, 8 * 32);
4704                 /* clear shared key mode */
4705                 run_set_region_4(sc, RT2860_SKEY_MODE_0_7, 0, 4);
4706         }
4707
4708         run_read(sc, RT2860_US_CYC_CNT, &tmp);
4709         tmp = (tmp & ~0xff) | 0x1e;
4710         run_write(sc, RT2860_US_CYC_CNT, tmp);
4711
4712         if (sc->mac_rev != 0x0101)
4713                 run_write(sc, RT2860_TXOP_CTRL_CFG, 0x0000583f);
4714
4715         run_write(sc, RT2860_WMM_TXOP0_CFG, 0);
4716         run_write(sc, RT2860_WMM_TXOP1_CFG, 48 << 16 | 96);
4717
4718         /* write vendor-specific BBP values (from EEPROM) */
4719         for (i = 0; i < 10; i++) {
4720                 if (sc->bbp[i].reg == 0 || sc->bbp[i].reg == 0xff)
4721                         continue;
4722                 run_bbp_write(sc, sc->bbp[i].reg, sc->bbp[i].val);
4723         }
4724
4725         /* select Main antenna for 1T1R devices */
4726         if (sc->rf_rev == RT3070_RF_3020)
4727                 run_set_rx_antenna(sc, 0);
4728
4729         /* send LEDs operating mode to microcontroller */
4730         (void)run_mcu_cmd(sc, RT2860_MCU_CMD_LED1, sc->led[0]);
4731         (void)run_mcu_cmd(sc, RT2860_MCU_CMD_LED2, sc->led[1]);
4732         (void)run_mcu_cmd(sc, RT2860_MCU_CMD_LED3, sc->led[2]);
4733
4734         if (sc->mac_ver >= 0x3070)
4735                 run_rt3070_rf_init(sc);
4736
4737         /* disable non-existing Rx chains */
4738         run_bbp_read(sc, 3, &bbp3);
4739         bbp3 &= ~(1 << 3 | 1 << 4);
4740         if (sc->nrxchains == 2)
4741                 bbp3 |= 1 << 3;
4742         else if (sc->nrxchains == 3)
4743                 bbp3 |= 1 << 4;
4744         run_bbp_write(sc, 3, bbp3);
4745
4746         /* disable non-existing Tx chains */
4747         run_bbp_read(sc, 1, &bbp1);
4748         if (sc->ntxchains == 1)
4749                 bbp1 &= ~(1 << 3 | 1 << 4);
4750         run_bbp_write(sc, 1, bbp1);
4751
4752         if (sc->mac_ver >= 0x3070)
4753                 run_rt3070_rf_setup(sc);
4754
4755         /* select default channel */
4756         run_set_chan(sc, ic->ic_curchan);
4757
4758         /* setup initial protection mode */
4759         run_updateprot(ic);
4760
4761         /* turn radio LED on */
4762         run_set_leds(sc, RT2860_LED_RADIO);
4763
4764         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
4765         ifp->if_drv_flags |= IFF_DRV_RUNNING;
4766         sc->cmdq_run = RUN_CMDQ_GO;
4767
4768         for (i = 0; i != RUN_N_XFER; i++)
4769                 usbd_xfer_set_stall(sc->sc_xfer[i]);
4770
4771         usbd_transfer_start(sc->sc_xfer[RUN_BULK_RX]);
4772
4773         if (run_txrx_enable(sc) != 0)
4774                 goto fail;
4775
4776         return;
4777
4778 fail:
4779         run_stop(sc);
4780 }
4781
4782 static void
4783 run_init(void *arg)
4784 {
4785         struct run_softc *sc = arg;
4786         struct ifnet *ifp = sc->sc_ifp;
4787         struct ieee80211com *ic = ifp->if_l2com;
4788
4789         RUN_LOCK(sc);
4790         run_init_locked(sc);
4791         RUN_UNLOCK(sc);
4792
4793         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
4794                 ieee80211_start_all(ic);
4795 }
4796
4797 static void
4798 run_stop(void *arg)
4799 {
4800         struct run_softc *sc = (struct run_softc *)arg;
4801         struct ifnet *ifp = sc->sc_ifp;
4802         uint32_t tmp;
4803         int i;
4804         int ntries;
4805
4806         RUN_LOCK_ASSERT(sc, MA_OWNED);
4807
4808         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
4809                 run_set_leds(sc, 0);    /* turn all LEDs off */
4810
4811         ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
4812
4813         sc->ratectl_run = RUN_RATECTL_OFF;
4814         sc->cmdq_run = sc->cmdq_key_set;
4815
4816         RUN_UNLOCK(sc);
4817
4818         for(i = 0; i < RUN_N_XFER; i++)
4819                 usbd_transfer_drain(sc->sc_xfer[i]);
4820
4821         RUN_LOCK(sc);
4822
4823         if (sc->rx_m != NULL) {
4824                 m_free(sc->rx_m);
4825                 sc->rx_m = NULL;
4826         }
4827
4828         /* disable Tx/Rx */
4829         run_read(sc, RT2860_MAC_SYS_CTRL, &tmp);
4830         tmp &= ~(RT2860_MAC_RX_EN | RT2860_MAC_TX_EN);
4831         run_write(sc, RT2860_MAC_SYS_CTRL, tmp);
4832
4833         /* wait for pending Tx to complete */
4834         for (ntries = 0; ntries < 100; ntries++) {
4835                 if (run_read(sc, RT2860_TXRXQ_PCNT, &tmp) != 0) {
4836                         DPRINTF("Cannot read Tx queue count\n");
4837                         break;
4838                 }
4839                 if ((tmp & RT2860_TX2Q_PCNT_MASK) == 0) {
4840                         DPRINTF("All Tx cleared\n");
4841                         break;
4842                 }
4843                 run_delay(sc, 10);
4844         }
4845         if (ntries >= 100)
4846                 DPRINTF("There are still pending Tx\n");
4847         run_delay(sc, 10);
4848         run_write(sc, RT2860_USB_DMA_CFG, 0);
4849
4850         run_write(sc, RT2860_MAC_SYS_CTRL, RT2860_BBP_HRST | RT2860_MAC_SRST);
4851         run_write(sc, RT2860_MAC_SYS_CTRL, 0);
4852
4853         for (i = 0; i != RUN_EP_QUEUES; i++)
4854                 run_unsetup_tx_list(sc, &sc->sc_epq[i]);
4855
4856         return;
4857 }
4858
4859 static void
4860 run_delay(struct run_softc *sc, unsigned int ms)
4861 {
4862         usb_pause_mtx(mtx_owned(&sc->sc_mtx) ? 
4863             &sc->sc_mtx : NULL, USB_MS_TO_TICKS(ms));
4864 }
4865
4866 static device_method_t run_methods[] = {
4867         /* Device interface */
4868         DEVMETHOD(device_probe,         run_match),
4869         DEVMETHOD(device_attach,        run_attach),
4870         DEVMETHOD(device_detach,        run_detach),
4871
4872         { 0, 0 }
4873 };
4874
4875 static driver_t run_driver = {
4876         .name = "run",
4877         .methods = run_methods,
4878         .size = sizeof(struct run_softc)
4879 };
4880
4881 static devclass_t run_devclass;
4882
4883 DRIVER_MODULE(run, uhub, run_driver, run_devclass, NULL, 0);
4884 MODULE_DEPEND(run, wlan, 1, 1, 1);
4885 MODULE_DEPEND(run, usb, 1, 1, 1);
4886 MODULE_DEPEND(run, firmware, 1, 1, 1);
4887 MODULE_VERSION(run, 1);