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