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