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