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