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