Merge branch 'vendor/LIBARCHIVE'
[dragonfly.git] / sys / bus / u4b / wlan / if_urtwn.c
1 /*      $OpenBSD: if_urtwn.c,v 1.16 2011/02/10 17:26:40 jakemsr Exp $   */
2
3 /*-
4  * Copyright (c) 2010 Damien Bergamini <damien.bergamini@free.fr>
5  * Copyright (c) 2014 Kevin Lo <kevlo@FreeBSD.org>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  *
19  * $FreeBSD: head/sys/dev/usb/wlan/if_urtwn.c 268487 2014-07-10 09:42:34Z kevlo $
20  */
21
22 /*
23  * Driver for Realtek RTL8188CE-VAU/RTL8188CUS/RTL8188EU/RTL8188RU/RTL8192CU.
24  */
25
26 #include <sys/param.h>
27 #include <sys/sockio.h>
28 #include <sys/sysctl.h>
29 #include <sys/lock.h>
30 #include <sys/mbuf.h>
31 #include <sys/kernel.h>
32 #include <sys/socket.h>
33 #include <sys/systm.h>
34 #include <sys/malloc.h>
35 #include <sys/module.h>
36 #include <sys/bus.h>
37 #include <sys/endian.h>
38 #include <sys/linker.h>
39 #include <sys/firmware.h>
40 #include <net/ifq_var.h>
41
42 #include <sys/rman.h>
43
44 #include <net/bpf.h>
45 #include <net/if.h>
46 #include <net/if_var.h>
47 #include <net/if_arp.h>
48 #include <net/ethernet.h>
49 #include <net/if_dl.h>
50 #include <net/if_media.h>
51 #include <net/if_types.h>
52
53 #include <netinet/in.h>
54 #include <netinet/in_systm.h>
55 #include <netinet/in_var.h>
56 #include <netinet/if_ether.h>
57 #include <netinet/ip.h>
58
59 #include <netproto/802_11/ieee80211_var.h>
60 #include <netproto/802_11/ieee80211_regdomain.h>
61 #include <netproto/802_11/ieee80211_radiotap.h>
62 #include <netproto/802_11/ieee80211_ratectl.h>
63
64 #include <bus/u4b/usb.h>
65 #include <bus/u4b/usbdi.h>
66 #include "usbdevs.h"
67
68 #define USB_DEBUG_VAR urtwn_debug
69 #include <bus/u4b/usb_debug.h>
70
71 #include <bus/u4b/wlan/if_urtwnreg.h>
72
73 #ifdef USB_DEBUG
74 static int urtwn_debug = 0;
75
76 SYSCTL_NODE(_hw_usb, OID_AUTO, urtwn, CTLFLAG_RW, 0, "USB urtwn");
77 SYSCTL_INT(_hw_usb_urtwn, OID_AUTO, debug, CTLFLAG_RW, &urtwn_debug, 0,
78     "Debug level");
79 #endif
80
81 #define URTWN_RSSI(r)  (r) - 110
82 #define IEEE80211_HAS_ADDR4(wh) \
83         (((wh)->i_fc[1] & IEEE80211_FC1_DIR_MASK) == IEEE80211_FC1_DIR_DSTODS)
84
85 /* various supported device vendors/products */
86 static const STRUCT_USB_HOST_ID urtwn_devs[] = {
87 #define URTWN_DEV(v,p)  { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
88 #define URTWN_RTL8188E_DEV(v,p) \
89         { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, URTWN_RTL8188E) }
90 #define URTWN_RTL8188E  1
91         URTWN_DEV(ABOCOM,       RTL8188CU_1),
92         URTWN_DEV(ABOCOM,       RTL8188CU_2),
93         URTWN_DEV(ABOCOM,       RTL8192CU),
94         URTWN_DEV(ASUS,         RTL8192CU),
95         URTWN_DEV(ASUS,         USBN10NANO),
96         URTWN_DEV(AZUREWAVE,    RTL8188CE_1),
97         URTWN_DEV(AZUREWAVE,    RTL8188CE_2),
98         URTWN_DEV(AZUREWAVE,    RTL8188CU),
99         URTWN_DEV(BELKIN,       F7D2102),
100         URTWN_DEV(BELKIN,       RTL8188CU),
101         URTWN_DEV(BELKIN,       RTL8192CU),
102         URTWN_DEV(CHICONY,      RTL8188CUS_1),
103         URTWN_DEV(CHICONY,      RTL8188CUS_2),
104         URTWN_DEV(CHICONY,      RTL8188CUS_3),
105         URTWN_DEV(CHICONY,      RTL8188CUS_4),
106         URTWN_DEV(CHICONY,      RTL8188CUS_5),
107         URTWN_DEV(COREGA,       RTL8192CU),
108         URTWN_DEV(DLINK,        RTL8188CU),
109         URTWN_DEV(DLINK,        RTL8192CU_1),
110         URTWN_DEV(DLINK,        RTL8192CU_2),
111         URTWN_DEV(DLINK,        RTL8192CU_3),
112         URTWN_DEV(DLINK,        DWA131B),
113         URTWN_DEV(EDIMAX,       EW7811UN),
114         URTWN_DEV(EDIMAX,       RTL8192CU),
115         URTWN_DEV(FEIXUN,       RTL8188CU),
116         URTWN_DEV(FEIXUN,       RTL8192CU),
117         URTWN_DEV(GUILLEMOT,    HWNUP150),
118         URTWN_DEV(HAWKING,      RTL8192CU),
119         URTWN_DEV(HP3,          RTL8188CU),
120         URTWN_DEV(NETGEAR,      WNA1000M),
121         URTWN_DEV(NETGEAR,      RTL8192CU),
122         URTWN_DEV(NETGEAR4,     RTL8188CU),
123         URTWN_DEV(NOVATECH,     RTL8188CU),
124         URTWN_DEV(PLANEX2,      RTL8188CU_1),
125         URTWN_DEV(PLANEX2,      RTL8188CU_2),
126         URTWN_DEV(PLANEX2,      RTL8188CU_3),
127         URTWN_DEV(PLANEX2,      RTL8188CU_4),
128         URTWN_DEV(PLANEX2,      RTL8188CUS),
129         URTWN_DEV(PLANEX2,      RTL8192CU),
130         URTWN_DEV(REALTEK,      RTL8188CE_0),
131         URTWN_DEV(REALTEK,      RTL8188CE_1),
132         URTWN_DEV(REALTEK,      RTL8188CTV),
133         URTWN_DEV(REALTEK,      RTL8188CU_0),
134         URTWN_DEV(REALTEK,      RTL8188CU_1),
135         URTWN_DEV(REALTEK,      RTL8188CU_2),
136         URTWN_DEV(REALTEK,      RTL8188CU_COMBO),
137         URTWN_DEV(REALTEK,      RTL8188CUS),
138         URTWN_DEV(REALTEK,      RTL8188RU_1),
139         URTWN_DEV(REALTEK,      RTL8188RU_2),
140         URTWN_DEV(REALTEK,      RTL8191CU),
141         URTWN_DEV(REALTEK,      RTL8192CE),
142         URTWN_DEV(REALTEK,      RTL8192CU),
143         URTWN_DEV(SITECOMEU,    RTL8188CU_1),
144         URTWN_DEV(SITECOMEU,    RTL8188CU_2),
145         URTWN_DEV(SITECOMEU,    RTL8192CU),
146         URTWN_DEV(TRENDNET,     RTL8188CU),
147         URTWN_DEV(TRENDNET,     RTL8192CU),
148         URTWN_DEV(ZYXEL,        RTL8192CU),
149         /* URTWN_RTL8188E */
150         URTWN_RTL8188E_DEV(REALTEK,     RTL8188ETV),
151         URTWN_RTL8188E_DEV(REALTEK,     RTL8188EU),
152 #undef URTWN_RTL8188E_DEV
153 #undef URTWN_DEV
154 };
155
156 static device_probe_t   urtwn_match;
157 static device_attach_t  urtwn_attach;
158 static device_detach_t  urtwn_detach;
159
160 static usb_callback_t   urtwn_bulk_tx_callback;
161 static usb_callback_t   urtwn_bulk_rx_callback;
162
163 static usb_error_t      urtwn_do_request(struct urtwn_softc *sc,
164                             struct usb_device_request *req, void *data);
165 static struct ieee80211vap *urtwn_vap_create(struct ieee80211com *,
166                     const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
167                     const uint8_t [IEEE80211_ADDR_LEN],
168                     const uint8_t [IEEE80211_ADDR_LEN]);
169 static void             urtwn_vap_delete(struct ieee80211vap *);
170 static struct mbuf *    urtwn_rx_frame(struct urtwn_softc *, uint8_t *, int, 
171                             int *);
172 static struct mbuf *    urtwn_rxeof(struct usb_xfer *, struct urtwn_data *, 
173                             int *, int8_t *);
174 static void             urtwn_txeof(struct usb_xfer *, struct urtwn_data *);
175 static int              urtwn_alloc_list(struct urtwn_softc *, 
176                             struct urtwn_data[], int, int);
177 static int              urtwn_alloc_rx_list(struct urtwn_softc *);
178 static int              urtwn_alloc_tx_list(struct urtwn_softc *);
179 static void             urtwn_free_tx_list(struct urtwn_softc *);
180 static void             urtwn_free_rx_list(struct urtwn_softc *);
181 static void             urtwn_free_list(struct urtwn_softc *,
182                             struct urtwn_data data[], int);
183 static struct urtwn_data *      _urtwn_getbuf(struct urtwn_softc *);
184 static struct urtwn_data *      urtwn_getbuf(struct urtwn_softc *);
185 static int              urtwn_write_region_1(struct urtwn_softc *, uint16_t, 
186                             uint8_t *, int);
187 static void             urtwn_write_1(struct urtwn_softc *, uint16_t, uint8_t);
188 static void             urtwn_write_2(struct urtwn_softc *, uint16_t, uint16_t);
189 static void             urtwn_write_4(struct urtwn_softc *, uint16_t, uint32_t);
190 static int              urtwn_read_region_1(struct urtwn_softc *, uint16_t, 
191                             uint8_t *, int);
192 static uint8_t          urtwn_read_1(struct urtwn_softc *, uint16_t);
193 static uint16_t         urtwn_read_2(struct urtwn_softc *, uint16_t);
194 static uint32_t         urtwn_read_4(struct urtwn_softc *, uint16_t);
195 static int              urtwn_fw_cmd(struct urtwn_softc *, uint8_t, 
196                             const void *, int);
197 static void             urtwn_r92c_rf_write(struct urtwn_softc *, int,
198                             uint8_t, uint32_t);
199 static void             urtwn_r88e_rf_write(struct urtwn_softc *, int, 
200                             uint8_t, uint32_t);
201 static uint32_t         urtwn_rf_read(struct urtwn_softc *, int, uint8_t);
202 static int              urtwn_llt_write(struct urtwn_softc *, uint32_t, 
203                             uint32_t);
204 static uint8_t          urtwn_efuse_read_1(struct urtwn_softc *, uint16_t);
205 static void             urtwn_efuse_read(struct urtwn_softc *);
206 static void             urtwn_efuse_switch_power(struct urtwn_softc *);
207 static int              urtwn_read_chipid(struct urtwn_softc *);
208 static void             urtwn_read_rom(struct urtwn_softc *);
209 static void             urtwn_r88e_read_rom(struct urtwn_softc *);
210 static int              urtwn_ra_init(struct urtwn_softc *);
211 static void             urtwn_tsf_sync_enable(struct urtwn_softc *);
212 static void             urtwn_set_led(struct urtwn_softc *, int, int);
213 static int              urtwn_newstate(struct ieee80211vap *, 
214                             enum ieee80211_state, int);
215 static void             urtwn_watchdog(void *);
216 static void             urtwn_update_avgrssi(struct urtwn_softc *, int, int8_t);
217 static int8_t           urtwn_get_rssi(struct urtwn_softc *, int, void *);
218 static int8_t           urtwn_r88e_get_rssi(struct urtwn_softc *, int, void *);
219 static int              urtwn_tx_start(struct urtwn_softc *,
220                             struct ieee80211_node *, struct mbuf *,
221                             struct urtwn_data *);
222 static void             urtwn_start(struct ifnet *, struct ifaltq_subque *);
223 static void             urtwn_start_locked(struct ifnet *);
224 static int              urtwn_ioctl(struct ifnet *, u_long, caddr_t,
225                             struct ucred *);
226 static int              urtwn_r92c_power_on(struct urtwn_softc *);
227 static int              urtwn_r88e_power_on(struct urtwn_softc *);
228 static int              urtwn_llt_init(struct urtwn_softc *);
229 static void             urtwn_fw_reset(struct urtwn_softc *);
230 static void             urtwn_r88e_fw_reset(struct urtwn_softc *);
231 static int              urtwn_fw_loadpage(struct urtwn_softc *, int, 
232                             const uint8_t *, int);
233 static int              urtwn_load_firmware(struct urtwn_softc *);
234 static int              urtwn_r92c_dma_init(struct urtwn_softc *);
235 static int              urtwn_r88e_dma_init(struct urtwn_softc *);
236 static void             urtwn_mac_init(struct urtwn_softc *);
237 static void             urtwn_bb_init(struct urtwn_softc *);
238 static void             urtwn_rf_init(struct urtwn_softc *);
239 static void             urtwn_cam_init(struct urtwn_softc *);
240 static void             urtwn_pa_bias_init(struct urtwn_softc *);
241 static void             urtwn_rxfilter_init(struct urtwn_softc *);
242 static void             urtwn_edca_init(struct urtwn_softc *);
243 static void             urtwn_write_txpower(struct urtwn_softc *, int, 
244                             uint16_t[]);
245 static void             urtwn_get_txpower(struct urtwn_softc *, int,
246                             struct ieee80211_channel *,
247                             struct ieee80211_channel *, uint16_t[]);
248 static void             urtwn_r88e_get_txpower(struct urtwn_softc *, int,
249                             struct ieee80211_channel *,
250                             struct ieee80211_channel *, uint16_t[]);
251 static void             urtwn_set_txpower(struct urtwn_softc *,
252                             struct ieee80211_channel *,
253                             struct ieee80211_channel *);
254 static void             urtwn_scan_start(struct ieee80211com *);
255 static void             urtwn_scan_end(struct ieee80211com *);
256 static void             urtwn_set_channel(struct ieee80211com *);
257 static void             urtwn_set_chan(struct urtwn_softc *,
258                             struct ieee80211_channel *,
259                             struct ieee80211_channel *);
260 static void             urtwn_update_mcast(struct ieee80211com *);
261 static void             urtwn_iq_calib(struct urtwn_softc *);
262 static void             urtwn_lc_calib(struct urtwn_softc *);
263 static void             urtwn_init(void *);
264 static void             urtwn_init_locked(void *);
265 static void             urtwn_stop(struct ifnet *);
266 static void             urtwn_stop_locked(struct ifnet *);
267 static void             urtwn_abort_xfers(struct urtwn_softc *);
268 static int              urtwn_raw_xmit(struct ieee80211_node *, struct mbuf *,
269                             const struct ieee80211_bpf_params *);
270 static void             urtwn_ms_delay(struct urtwn_softc *);
271
272 /* Aliases. */
273 #define urtwn_bb_write  urtwn_write_4
274 #define urtwn_bb_read   urtwn_read_4
275
276 static const struct usb_config urtwn_config[URTWN_N_TRANSFER] = {
277         [URTWN_BULK_RX] = {
278                 .type = UE_BULK,
279                 .endpoint = UE_ADDR_ANY,
280                 .direction = UE_DIR_IN,
281                 .bufsize = URTWN_RXBUFSZ,
282                 .flags = {
283                         .pipe_bof = 1,
284                         .short_xfer_ok = 1
285                 },
286                 .callback = urtwn_bulk_rx_callback,
287         },
288         [URTWN_BULK_TX_BE] = {
289                 .type = UE_BULK,
290                 .endpoint = 0x03,
291                 .direction = UE_DIR_OUT,
292                 .bufsize = URTWN_TXBUFSZ,
293                 .flags = {
294                         .ext_buffer = 1,
295                         .pipe_bof = 1,
296                         .force_short_xfer = 1
297                 },
298                 .callback = urtwn_bulk_tx_callback,
299                 .timeout = URTWN_TX_TIMEOUT,    /* ms */
300         },
301         [URTWN_BULK_TX_BK] = {
302                 .type = UE_BULK,
303                 .endpoint = 0x03,
304                 .direction = UE_DIR_OUT,
305                 .bufsize = URTWN_TXBUFSZ,
306                 .flags = {
307                         .ext_buffer = 1,
308                         .pipe_bof = 1,
309                         .force_short_xfer = 1,
310                 },
311                 .callback = urtwn_bulk_tx_callback,
312                 .timeout = URTWN_TX_TIMEOUT,    /* ms */
313         },
314         [URTWN_BULK_TX_VI] = {
315                 .type = UE_BULK,
316                 .endpoint = 0x02,
317                 .direction = UE_DIR_OUT,
318                 .bufsize = URTWN_TXBUFSZ,
319                 .flags = {
320                         .ext_buffer = 1,
321                         .pipe_bof = 1,
322                         .force_short_xfer = 1
323                 },
324                 .callback = urtwn_bulk_tx_callback,
325                 .timeout = URTWN_TX_TIMEOUT,    /* ms */
326         },
327         [URTWN_BULK_TX_VO] = {
328                 .type = UE_BULK,
329                 .endpoint = 0x02,
330                 .direction = UE_DIR_OUT,
331                 .bufsize = URTWN_TXBUFSZ,
332                 .flags = {
333                         .ext_buffer = 1,
334                         .pipe_bof = 1,
335                         .force_short_xfer = 1
336                 },
337                 .callback = urtwn_bulk_tx_callback,
338                 .timeout = URTWN_TX_TIMEOUT,    /* ms */
339         },
340 };
341
342 static int
343 urtwn_match(device_t self)
344 {
345         struct usb_attach_arg *uaa = device_get_ivars(self);
346
347         if (uaa->usb_mode != USB_MODE_HOST)
348                 return (ENXIO);
349         if (uaa->info.bConfigIndex != URTWN_CONFIG_INDEX)
350                 return (ENXIO);
351         if (uaa->info.bIfaceIndex != URTWN_IFACE_INDEX)
352                 return (ENXIO);
353
354         return (usbd_lookup_id_by_uaa(urtwn_devs, sizeof(urtwn_devs), uaa));
355 }
356
357 static int
358 urtwn_attach(device_t self)
359 {
360         struct usb_attach_arg *uaa = device_get_ivars(self);
361         struct urtwn_softc *sc = device_get_softc(self);
362         struct ifnet *ifp;
363         struct ieee80211com *ic;
364         uint8_t iface_index, bands;
365         int error;
366
367         wlan_serialize_enter();
368         device_set_usb_desc(self);
369         sc->sc_udev = uaa->device;
370         sc->sc_dev = self;
371         if (USB_GET_DRIVER_INFO(uaa) == URTWN_RTL8188E)
372                 sc->chip |= URTWN_CHIP_88E;
373
374         lockinit(&sc->sc_lock, device_get_nameunit(self), 0, LK_CANRECURSE);
375         callout_init(&sc->sc_watchdog_ch);
376
377         iface_index = URTWN_IFACE_INDEX;
378         error = usbd_transfer_setup(uaa->device, &iface_index, sc->sc_xfer,
379             urtwn_config, URTWN_N_TRANSFER, sc, &sc->sc_lock);
380         if (error) {
381                 device_printf(self, "could not allocate USB transfers, "
382                     "err=%s\n", usbd_errstr(error));
383                 goto detach;
384         }
385
386         URTWN_LOCK(sc);
387
388         error = urtwn_read_chipid(sc);
389         if (error) {
390                 device_printf(sc->sc_dev, "unsupported test chip\n");
391                 URTWN_UNLOCK(sc);
392                 goto detach;
393         }
394
395         /* Determine number of Tx/Rx chains. */
396         if (sc->chip & URTWN_CHIP_92C) {
397                 sc->ntxchains = (sc->chip & URTWN_CHIP_92C_1T2R) ? 1 : 2;
398                 sc->nrxchains = 2;
399         } else {
400                 sc->ntxchains = 1;
401                 sc->nrxchains = 1;
402         }
403
404         if (sc->chip & URTWN_CHIP_88E)
405                 urtwn_r88e_read_rom(sc);
406         else
407                 urtwn_read_rom(sc);
408
409         device_printf(sc->sc_dev, "MAC/BB RTL%s, RF 6052 %dT%dR\n",
410             (sc->chip & URTWN_CHIP_92C) ? "8192CU" :
411             (sc->chip & URTWN_CHIP_88E) ? "8188EU" :
412             (sc->board_type == R92C_BOARD_TYPE_HIGHPA) ? "8188RU" :
413             (sc->board_type == R92C_BOARD_TYPE_MINICARD) ? "8188CE-VAU" :
414             "8188CUS", sc->ntxchains, sc->nrxchains);
415
416         URTWN_UNLOCK(sc);
417
418         ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
419         if (ifp == NULL) {
420                 device_printf(sc->sc_dev, "can not if_alloc()\n");
421                 goto detach;
422         }
423         ic = ifp->if_l2com;
424
425         ifp->if_softc = sc;
426         if_initname(ifp, "urtwn", device_get_unit(sc->sc_dev));
427         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
428         ifp->if_init = urtwn_init;
429         ifp->if_ioctl = urtwn_ioctl;
430         ifp->if_start = urtwn_start;
431         ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
432 #if 0 /* XXX swildner: see c3d4131842e47b168d93a0650d58d425ebeef789 */
433         IFQ_SET_READY(&ifp->if_snd);
434 #endif
435
436         ic->ic_ifp = ifp;
437         ic->ic_softc = sc;
438         ic->ic_name = device_get_nameunit(sc->sc_dev);
439         ic->ic_phytype = IEEE80211_T_OFDM;      /* not only, but not used */
440         ic->ic_opmode = IEEE80211_M_STA;        /* default to BSS mode */
441
442         /* set device capabilities */
443         ic->ic_caps =
444                   IEEE80211_C_STA               /* station mode */
445                 | IEEE80211_C_MONITOR           /* monitor mode */
446                 | IEEE80211_C_SHPREAMBLE        /* short preamble supported */
447                 | IEEE80211_C_SHSLOT            /* short slot time supported */
448                 | IEEE80211_C_BGSCAN            /* capable of bg scanning */
449                 | IEEE80211_C_WPA               /* 802.11i */
450                 ;
451
452         bands = 0;
453         setbit(&bands, IEEE80211_MODE_11B);
454         setbit(&bands, IEEE80211_MODE_11G);
455         ieee80211_init_channels(ic, NULL, &bands);
456
457         ieee80211_ifattach(ic, sc->sc_bssid);
458         ic->ic_raw_xmit = urtwn_raw_xmit;
459         ic->ic_scan_start = urtwn_scan_start;
460         ic->ic_scan_end = urtwn_scan_end;
461         ic->ic_set_channel = urtwn_set_channel;
462
463         ic->ic_vap_create = urtwn_vap_create;
464         ic->ic_vap_delete = urtwn_vap_delete;
465         ic->ic_update_mcast = urtwn_update_mcast;
466
467         ieee80211_radiotap_attach(ic, &sc->sc_txtap.wt_ihdr, 
468             sizeof(sc->sc_txtap), URTWN_TX_RADIOTAP_PRESENT,
469             &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
470             URTWN_RX_RADIOTAP_PRESENT);
471
472         if (bootverbose)
473                 ieee80211_announce(ic);
474
475         wlan_serialize_exit();
476         return (0);
477
478 detach:
479         wlan_serialize_exit();
480         urtwn_detach(self);
481         return (ENXIO);                 /* failure */
482 }
483
484 static int
485 urtwn_detach(device_t self)
486 {
487         struct urtwn_softc *sc = device_get_softc(self);
488         struct ifnet *ifp = sc->sc_ifp;
489         struct ieee80211com *ic = ifp->if_l2com;
490         unsigned int x;
491
492         wlan_serialize_enter();
493
494         /* Prevent further ioctls. */
495         URTWN_LOCK(sc);
496         sc->sc_flags |= URTWN_DETACHED;
497         URTWN_UNLOCK(sc);
498
499         urtwn_stop(ifp);
500
501         callout_stop_sync(&sc->sc_watchdog_ch);
502
503         /* Prevent further allocations from RX/TX data lists. */
504         URTWN_LOCK(sc);
505         STAILQ_INIT(&sc->sc_tx_active);
506         STAILQ_INIT(&sc->sc_tx_inactive);
507         STAILQ_INIT(&sc->sc_tx_pending);
508
509         STAILQ_INIT(&sc->sc_rx_active);
510         STAILQ_INIT(&sc->sc_rx_inactive);
511         URTWN_UNLOCK(sc);
512
513         /* drain USB transfers */
514         for (x = 0; x != URTWN_N_TRANSFER; x++)
515                 usbd_transfer_drain(sc->sc_xfer[x]);
516
517         /* Free data buffers. */
518         URTWN_LOCK(sc);
519         urtwn_free_tx_list(sc);
520         urtwn_free_rx_list(sc);
521         URTWN_UNLOCK(sc);
522
523         /* stop all USB transfers */
524         usbd_transfer_unsetup(sc->sc_xfer, URTWN_N_TRANSFER);
525         ieee80211_ifdetach(ic);
526
527         if_free(ifp);
528         lockuninit(&sc->sc_lock);
529
530         wlan_serialize_exit();
531         return (0);
532 }
533
534 static void
535 urtwn_free_tx_list(struct urtwn_softc *sc)
536 {
537         urtwn_free_list(sc, sc->sc_tx, URTWN_TX_LIST_COUNT);
538 }
539
540 static void
541 urtwn_free_rx_list(struct urtwn_softc *sc)
542 {
543         urtwn_free_list(sc, sc->sc_rx, URTWN_RX_LIST_COUNT);
544 }
545
546 static void
547 urtwn_free_list(struct urtwn_softc *sc, struct urtwn_data data[], int ndata)
548 {
549         int i;
550
551         for (i = 0; i < ndata; i++) {
552                 struct urtwn_data *dp = &data[i];
553
554                 if (dp->buf != NULL) {
555                         kfree(dp->buf, M_USBDEV);
556                         dp->buf = NULL;
557                 }
558                 if (dp->ni != NULL) {
559                         ieee80211_free_node(dp->ni);
560                         dp->ni = NULL;
561                 }
562         }
563 }
564
565 static usb_error_t
566 urtwn_do_request(struct urtwn_softc *sc, struct usb_device_request *req,
567     void *data)
568 {
569         usb_error_t err;
570         int ntries = 10;
571
572         URTWN_ASSERT_LOCKED(sc);
573
574         while (ntries--) {
575                 err = usbd_do_request_flags(sc->sc_udev, &sc->sc_lock,
576                     req, data, 0, NULL, 250 /* ms */);
577                 if (err == 0)
578                         break;
579
580                 DPRINTFN(1, "Control request failed, %s (retrying)\n",
581                     usbd_errstr(err));
582                 usb_pause_mtx(&sc->sc_lock, hz / 100);
583         }
584         return (err);
585 }
586
587 static struct ieee80211vap *
588 urtwn_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
589     enum ieee80211_opmode opmode, int flags,
590     const uint8_t bssid[IEEE80211_ADDR_LEN],
591     const uint8_t mac[IEEE80211_ADDR_LEN])
592 {
593         struct urtwn_vap *uvp;
594         struct ieee80211vap *vap;
595
596         if (!TAILQ_EMPTY(&ic->ic_vaps))         /* only one at a time */
597                 return (NULL);
598
599         uvp = (struct urtwn_vap *) kmalloc(sizeof(struct urtwn_vap),
600             M_80211_VAP, M_INTWAIT | M_ZERO);
601         vap = &uvp->vap;
602         /* enable s/w bmiss handling for sta mode */
603
604         if (ieee80211_vap_setup(ic, vap, name, unit, opmode, 
605             flags | IEEE80211_CLONE_NOBEACONS, bssid, mac) != 0) {
606                 /* out of memory */
607                 kfree(uvp, M_80211_VAP);
608                 return (NULL);
609         }
610
611         /* override state transition machine */
612         uvp->newstate = vap->iv_newstate;
613         vap->iv_newstate = urtwn_newstate;
614
615         /* complete setup */
616         ieee80211_vap_attach(vap, ieee80211_media_change,
617             ieee80211_media_status);
618         ic->ic_opmode = opmode;
619         return (vap);
620 }
621
622 static void
623 urtwn_vap_delete(struct ieee80211vap *vap)
624 {
625         struct urtwn_vap *uvp = URTWN_VAP(vap);
626
627         ieee80211_vap_detach(vap);
628         kfree(uvp, M_80211_VAP);
629 }
630
631 static struct mbuf *
632 urtwn_rx_frame(struct urtwn_softc *sc, uint8_t *buf, int pktlen, int *rssi_p)
633 {
634         struct ifnet *ifp = sc->sc_ifp;
635         struct ieee80211com *ic = ifp->if_l2com;
636         struct ieee80211_frame *wh;
637         struct mbuf *m;
638         struct r92c_rx_stat *stat;
639         uint32_t rxdw0, rxdw3;
640         uint8_t rate;
641         int8_t rssi = 0;
642         int infosz;
643
644         /*
645          * don't pass packets to the ieee80211 framework if the driver isn't
646          * RUNNING.
647          */
648         if (!(ifp->if_flags & IFF_RUNNING))
649                 return (NULL);
650
651         stat = (struct r92c_rx_stat *)buf;
652         rxdw0 = le32toh(stat->rxdw0);
653         rxdw3 = le32toh(stat->rxdw3);
654
655         if (rxdw0 & (R92C_RXDW0_CRCERR | R92C_RXDW0_ICVERR)) {
656                 /*
657                  * This should not happen since we setup our Rx filter
658                  * to not receive these frames.
659                  */
660                 IFNET_STAT_INC(ifp, ierrors, 1);
661                 return (NULL);
662         }
663
664         rate = MS(rxdw3, R92C_RXDW3_RATE);
665         infosz = MS(rxdw0, R92C_RXDW0_INFOSZ) * 8;
666
667         /* Get RSSI from PHY status descriptor if present. */
668         if (infosz != 0 && (rxdw0 & R92C_RXDW0_PHYST)) {
669                 if (sc->chip & URTWN_CHIP_88E) 
670                         rssi = urtwn_r88e_get_rssi(sc, rate, &stat[1]);
671                 else
672                         rssi = urtwn_get_rssi(sc, rate, &stat[1]);
673                 /* Update our average RSSI. */
674                 urtwn_update_avgrssi(sc, rate, rssi);
675                 /*
676                  * Convert the RSSI to a range that will be accepted
677                  * by net80211.
678                  */
679                 rssi = URTWN_RSSI(rssi);
680         }
681
682         m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
683         if (m == NULL) {
684                 device_printf(sc->sc_dev, "could not create RX mbuf\n");
685                 return (NULL);
686         }
687
688         /* Finalize mbuf. */
689         m->m_pkthdr.rcvif = ifp;
690         wh = (struct ieee80211_frame *)((uint8_t *)&stat[1] + infosz);
691         memcpy(mtod(m, uint8_t *), wh, pktlen);
692         m->m_pkthdr.len = m->m_len = pktlen;
693
694         if (ieee80211_radiotap_active(ic)) {
695                 struct urtwn_rx_radiotap_header *tap = &sc->sc_rxtap;
696
697                 tap->wr_flags = 0;
698                 /* Map HW rate index to 802.11 rate. */
699                 if (!(rxdw3 & R92C_RXDW3_HT)) {
700                         switch (rate) {
701                         /* CCK. */
702                         case  0: tap->wr_rate =   2; break;
703                         case  1: tap->wr_rate =   4; break;
704                         case  2: tap->wr_rate =  11; break;
705                         case  3: tap->wr_rate =  22; break;
706                         /* OFDM. */
707                         case  4: tap->wr_rate =  12; break;
708                         case  5: tap->wr_rate =  18; break;
709                         case  6: tap->wr_rate =  24; break;
710                         case  7: tap->wr_rate =  36; break;
711                         case  8: tap->wr_rate =  48; break;
712                         case  9: tap->wr_rate =  72; break;
713                         case 10: tap->wr_rate =  96; break;
714                         case 11: tap->wr_rate = 108; break;
715                         }
716                 } else if (rate >= 12) {        /* MCS0~15. */
717                         /* Bit 7 set means HT MCS instead of rate. */
718                         tap->wr_rate = 0x80 | (rate - 12);
719                 }
720                 tap->wr_dbm_antsignal = rssi;
721                 tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
722                 tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
723         }
724
725         *rssi_p = rssi;
726
727         return (m);
728 }
729
730 static struct mbuf *
731 urtwn_rxeof(struct usb_xfer *xfer, struct urtwn_data *data, int *rssi,
732     int8_t *nf)
733 {
734         struct urtwn_softc *sc = data->sc;
735         struct ifnet *ifp = sc->sc_ifp;
736         struct r92c_rx_stat *stat;
737         struct mbuf *m, *m0 = NULL, *prevm = NULL;
738         uint32_t rxdw0;
739         uint8_t *buf;
740         int len, totlen, pktlen, infosz, npkts;
741
742         usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
743
744         if (len < sizeof(*stat)) {
745                 IFNET_STAT_INC(ifp, ierrors, 1);
746                 return (NULL);
747         }
748
749         buf = data->buf;
750         /* Get the number of encapsulated frames. */
751         stat = (struct r92c_rx_stat *)buf;
752         npkts = MS(le32toh(stat->rxdw2), R92C_RXDW2_PKTCNT);
753         DPRINTFN(6, "Rx %d frames in one chunk\n", npkts);
754
755         /* Process all of them. */
756         while (npkts-- > 0) {
757                 if (len < sizeof(*stat))
758                         break;
759                 stat = (struct r92c_rx_stat *)buf;
760                 rxdw0 = le32toh(stat->rxdw0);
761
762                 pktlen = MS(rxdw0, R92C_RXDW0_PKTLEN);
763                 if (pktlen == 0)
764                         break;
765
766                 infosz = MS(rxdw0, R92C_RXDW0_INFOSZ) * 8;
767
768                 /* Make sure everything fits in xfer. */
769                 totlen = sizeof(*stat) + infosz + pktlen;
770                 if (totlen > len)
771                         break;
772
773                 m = urtwn_rx_frame(sc, buf, pktlen, rssi);
774                 if (m0 == NULL)
775                         m0 = m;
776                 if (prevm == NULL)
777                         prevm = m;
778                 else {
779                         prevm->m_next = m;
780                         prevm = m;
781                 }
782
783                 /* Next chunk is 128-byte aligned. */
784                 totlen = (totlen + 127) & ~127;
785                 buf += totlen;
786                 len -= totlen;
787         }
788
789         return (m0);
790 }
791
792 static void
793 urtwn_bulk_rx_callback(struct usb_xfer *xfer, usb_error_t error)
794 {
795         struct urtwn_softc *sc = usbd_xfer_softc(xfer);
796         struct ifnet *ifp = sc->sc_ifp;
797         struct ieee80211com *ic = ifp->if_l2com;
798         struct ieee80211_frame *wh;
799         struct ieee80211_node *ni;
800         struct mbuf *m = NULL, *next;
801         struct urtwn_data *data;
802         int8_t nf;
803         int rssi = 1;
804
805         URTWN_ASSERT_LOCKED(sc);
806
807         switch (USB_GET_STATE(xfer)) {
808         case USB_ST_TRANSFERRED:
809                 data = STAILQ_FIRST(&sc->sc_rx_active);
810                 if (data == NULL)
811                         goto tr_setup;
812                 STAILQ_REMOVE_HEAD(&sc->sc_rx_active, next);
813                 m = urtwn_rxeof(xfer, data, &rssi, &nf);
814                 STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next);
815                 /* FALLTHROUGH */
816         case USB_ST_SETUP:
817 tr_setup:
818                 data = STAILQ_FIRST(&sc->sc_rx_inactive);
819                 if (data == NULL) {
820                         KASSERT(m == NULL, ("mbuf isn't NULL"));
821                         return;
822                 }
823                 STAILQ_REMOVE_HEAD(&sc->sc_rx_inactive, next);
824                 STAILQ_INSERT_TAIL(&sc->sc_rx_active, data, next);
825                 usbd_xfer_set_frame_data(xfer, 0, data->buf,
826                     usbd_xfer_max_len(xfer));
827                 usbd_transfer_submit(xfer);
828
829                 /*
830                  * To avoid LOR we should unlock our private mutex here to call
831                  * ieee80211_input() because here is at the end of a USB
832                  * callback and safe to unlock.
833                  */
834                 URTWN_UNLOCK(sc);
835                 while (m != NULL) {
836                         next = m->m_next;
837                         m->m_next = NULL;
838                         wh = mtod(m, struct ieee80211_frame *);
839                         ni = ieee80211_find_rxnode(ic,
840                             (struct ieee80211_frame_min *)wh);
841                         nf = URTWN_NOISE_FLOOR;
842                         if (ni != NULL) {
843                                 (void)ieee80211_input(ni, m, rssi, nf);
844                                 ieee80211_free_node(ni);
845                         } else
846                                 (void)ieee80211_input_all(ic, m, rssi, nf);
847                         m = next;
848                 }
849                 URTWN_LOCK(sc);
850                 break;
851         default:
852                 /* needs it to the inactive queue due to a error. */
853                 data = STAILQ_FIRST(&sc->sc_rx_active);
854                 if (data != NULL) {
855                         STAILQ_REMOVE_HEAD(&sc->sc_rx_active, next);
856                         STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next);
857                 }
858                 if (error != USB_ERR_CANCELLED) {
859                         usbd_xfer_set_stall(xfer);
860                         IFNET_STAT_INC(ifp, ierrors, 1);
861                         goto tr_setup;
862                 }
863                 break;
864         }
865 }
866
867 static void
868 urtwn_txeof(struct usb_xfer *xfer, struct urtwn_data *data)
869 {
870         struct urtwn_softc *sc = usbd_xfer_softc(xfer);
871         struct ifnet *ifp = sc->sc_ifp;
872         struct mbuf *m;
873
874         URTWN_ASSERT_LOCKED(sc);
875
876         /*
877          * Do any tx complete callback.  Note this must be done before releasing
878          * the node reference.
879          */
880         if (data->m) {
881                 m = data->m;
882                 if (m->m_flags & M_TXCB) {
883                         /* XXX status? */
884                         ieee80211_process_callback(data->ni, m, 0);
885                 }
886                 m_freem(m);
887                 data->m = NULL;
888         }
889         if (data->ni) {
890                 ieee80211_free_node(data->ni);
891                 data->ni = NULL;
892         }
893         sc->sc_txtimer = 0;
894         IFNET_STAT_INC(ifp, opackets, 1);
895         ifq_clr_oactive(&ifp->if_snd);
896 }
897
898 static void
899 urtwn_bulk_tx_callback(struct usb_xfer *xfer, usb_error_t error)
900 {
901         struct urtwn_softc *sc = usbd_xfer_softc(xfer);
902         struct ifnet *ifp = sc->sc_ifp;
903         struct urtwn_data *data;
904
905         URTWN_ASSERT_LOCKED(sc);
906
907         switch (USB_GET_STATE(xfer)){
908         case USB_ST_TRANSFERRED:
909                 data = STAILQ_FIRST(&sc->sc_tx_active);
910                 if (data == NULL)
911                         goto tr_setup;
912                 STAILQ_REMOVE_HEAD(&sc->sc_tx_active, next);
913                 urtwn_txeof(xfer, data);
914                 STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data, next);
915                 /* FALLTHROUGH */
916         case USB_ST_SETUP:
917 tr_setup:
918                 data = STAILQ_FIRST(&sc->sc_tx_pending);
919                 if (data == NULL) {
920                         DPRINTF("%s: empty pending queue\n", __func__);
921                         return;
922                 }
923                 STAILQ_REMOVE_HEAD(&sc->sc_tx_pending, next);
924                 STAILQ_INSERT_TAIL(&sc->sc_tx_active, data, next);
925                 usbd_xfer_set_frame_data(xfer, 0, data->buf, data->buflen);
926                 usbd_transfer_submit(xfer);
927
928                 urtwn_start_locked(ifp);
929                 break;
930         default:
931                 data = STAILQ_FIRST(&sc->sc_tx_active);
932                 if (data == NULL)
933                         goto tr_setup;
934                 if (data->ni != NULL) {
935                         ieee80211_free_node(data->ni);
936                         data->ni = NULL;
937                         IFNET_STAT_INC(ifp, oerrors, 1);
938                 }
939                 if (error != USB_ERR_CANCELLED) {
940                         usbd_xfer_set_stall(xfer);
941                         goto tr_setup;
942                 }
943                 break;
944         }
945 }
946
947 static struct urtwn_data *
948 _urtwn_getbuf(struct urtwn_softc *sc)
949 {
950         struct urtwn_data *bf;
951
952         bf = STAILQ_FIRST(&sc->sc_tx_inactive);
953         if (bf != NULL)
954                 STAILQ_REMOVE_HEAD(&sc->sc_tx_inactive, next);
955         else
956                 bf = NULL;
957         if (bf == NULL)
958                 DPRINTF("%s: %s\n", __func__, "out of xmit buffers");
959         return (bf);
960 }
961
962 static struct urtwn_data *
963 urtwn_getbuf(struct urtwn_softc *sc)
964 {
965         struct urtwn_data *bf;
966
967         URTWN_ASSERT_LOCKED(sc);
968
969         bf = _urtwn_getbuf(sc);
970         if (bf == NULL) {
971                 struct ifnet *ifp = sc->sc_ifp;
972                 DPRINTF("%s: stop queue\n", __func__);
973                 ifq_set_oactive(&ifp->if_snd);
974         }
975         return (bf);
976 }
977
978 static int
979 urtwn_write_region_1(struct urtwn_softc *sc, uint16_t addr, uint8_t *buf,
980     int len)
981 {
982         usb_device_request_t req;
983
984         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
985         req.bRequest = R92C_REQ_REGS;
986         USETW(req.wValue, addr);
987         USETW(req.wIndex, 0);
988         USETW(req.wLength, len);
989         return (urtwn_do_request(sc, &req, buf));
990 }
991
992 static void
993 urtwn_write_1(struct urtwn_softc *sc, uint16_t addr, uint8_t val)
994 {
995         urtwn_write_region_1(sc, addr, &val, 1);
996 }
997
998 static void
999 urtwn_write_2(struct urtwn_softc *sc, uint16_t addr, uint16_t val)
1000 {
1001         val = htole16(val);
1002         urtwn_write_region_1(sc, addr, (uint8_t *)&val, 2);
1003 }
1004
1005 static void
1006 urtwn_write_4(struct urtwn_softc *sc, uint16_t addr, uint32_t val)
1007 {
1008         val = htole32(val);
1009         urtwn_write_region_1(sc, addr, (uint8_t *)&val, 4);
1010 }
1011
1012 static int
1013 urtwn_read_region_1(struct urtwn_softc *sc, uint16_t addr, uint8_t *buf,
1014     int len)
1015 {
1016         usb_device_request_t req;
1017
1018         req.bmRequestType = UT_READ_VENDOR_DEVICE;
1019         req.bRequest = R92C_REQ_REGS;
1020         USETW(req.wValue, addr);
1021         USETW(req.wIndex, 0);
1022         USETW(req.wLength, len);
1023         return (urtwn_do_request(sc, &req, buf));
1024 }
1025
1026 static uint8_t
1027 urtwn_read_1(struct urtwn_softc *sc, uint16_t addr)
1028 {
1029         uint8_t val;
1030
1031         if (urtwn_read_region_1(sc, addr, &val, 1) != 0)
1032                 return (0xff);
1033         return (val);
1034 }
1035
1036 static uint16_t
1037 urtwn_read_2(struct urtwn_softc *sc, uint16_t addr)
1038 {
1039         uint16_t val;
1040
1041         if (urtwn_read_region_1(sc, addr, (uint8_t *)&val, 2) != 0)
1042                 return (0xffff);
1043         return (le16toh(val));
1044 }
1045
1046 static uint32_t
1047 urtwn_read_4(struct urtwn_softc *sc, uint16_t addr)
1048 {
1049         uint32_t val;
1050
1051         if (urtwn_read_region_1(sc, addr, (uint8_t *)&val, 4) != 0)
1052                 return (0xffffffff);
1053         return (le32toh(val));
1054 }
1055
1056 static int
1057 urtwn_fw_cmd(struct urtwn_softc *sc, uint8_t id, const void *buf, int len)
1058 {
1059         struct r92c_fw_cmd cmd;
1060         int ntries;
1061
1062         /* Wait for current FW box to be empty. */
1063         for (ntries = 0; ntries < 100; ntries++) {
1064                 if (!(urtwn_read_1(sc, R92C_HMETFR) & (1 << sc->fwcur)))
1065                         break;
1066                 urtwn_ms_delay(sc);
1067         }
1068         if (ntries == 100) {
1069                 device_printf(sc->sc_dev,
1070                     "could not send firmware command\n");
1071                 return (ETIMEDOUT);
1072         }
1073         memset(&cmd, 0, sizeof(cmd));
1074         cmd.id = id;
1075         if (len > 3)
1076                 cmd.id |= R92C_CMD_FLAG_EXT;
1077         KASSERT(len <= sizeof(cmd.msg), ("urtwn_fw_cmd\n"));
1078         memcpy(cmd.msg, buf, len);
1079
1080         /* Write the first word last since that will trigger the FW. */
1081         urtwn_write_region_1(sc, R92C_HMEBOX_EXT(sc->fwcur),
1082             (uint8_t *)&cmd + 4, 2);
1083         urtwn_write_region_1(sc, R92C_HMEBOX(sc->fwcur),
1084             (uint8_t *)&cmd + 0, 4);
1085
1086         sc->fwcur = (sc->fwcur + 1) % R92C_H2C_NBOX;
1087         return (0);
1088 }
1089
1090 static __inline void
1091 urtwn_rf_write(struct urtwn_softc *sc, int chain, uint8_t addr, uint32_t val)
1092 {
1093
1094         sc->sc_rf_write(sc, chain, addr, val);
1095 }
1096
1097 static void
1098 urtwn_r92c_rf_write(struct urtwn_softc *sc, int chain, uint8_t addr,
1099     uint32_t val)
1100 {
1101         urtwn_bb_write(sc, R92C_LSSI_PARAM(chain),
1102             SM(R92C_LSSI_PARAM_ADDR, addr) |
1103             SM(R92C_LSSI_PARAM_DATA, val));
1104 }
1105
1106 static void
1107 urtwn_r88e_rf_write(struct urtwn_softc *sc, int chain, uint8_t addr,
1108 uint32_t val)
1109 {
1110         urtwn_bb_write(sc, R92C_LSSI_PARAM(chain),
1111             SM(R88E_LSSI_PARAM_ADDR, addr) |
1112             SM(R92C_LSSI_PARAM_DATA, val));
1113 }
1114
1115 static uint32_t
1116 urtwn_rf_read(struct urtwn_softc *sc, int chain, uint8_t addr)
1117 {
1118         uint32_t reg[R92C_MAX_CHAINS], val;
1119
1120         reg[0] = urtwn_bb_read(sc, R92C_HSSI_PARAM2(0));
1121         if (chain != 0)
1122                 reg[chain] = urtwn_bb_read(sc, R92C_HSSI_PARAM2(chain));
1123
1124         urtwn_bb_write(sc, R92C_HSSI_PARAM2(0),
1125             reg[0] & ~R92C_HSSI_PARAM2_READ_EDGE);
1126         urtwn_ms_delay(sc);
1127
1128         urtwn_bb_write(sc, R92C_HSSI_PARAM2(chain),
1129             RW(reg[chain], R92C_HSSI_PARAM2_READ_ADDR, addr) |
1130             R92C_HSSI_PARAM2_READ_EDGE);
1131         urtwn_ms_delay(sc);
1132
1133         urtwn_bb_write(sc, R92C_HSSI_PARAM2(0),
1134             reg[0] | R92C_HSSI_PARAM2_READ_EDGE);
1135         urtwn_ms_delay(sc);
1136
1137         if (urtwn_bb_read(sc, R92C_HSSI_PARAM1(chain)) & R92C_HSSI_PARAM1_PI)
1138                 val = urtwn_bb_read(sc, R92C_HSPI_READBACK(chain));
1139         else
1140                 val = urtwn_bb_read(sc, R92C_LSSI_READBACK(chain));
1141         return (MS(val, R92C_LSSI_READBACK_DATA));
1142 }
1143
1144 static int
1145 urtwn_llt_write(struct urtwn_softc *sc, uint32_t addr, uint32_t data)
1146 {
1147         int ntries;
1148
1149         urtwn_write_4(sc, R92C_LLT_INIT,
1150             SM(R92C_LLT_INIT_OP, R92C_LLT_INIT_OP_WRITE) |
1151             SM(R92C_LLT_INIT_ADDR, addr) |
1152             SM(R92C_LLT_INIT_DATA, data));
1153         /* Wait for write operation to complete. */
1154         for (ntries = 0; ntries < 20; ntries++) {
1155                 if (MS(urtwn_read_4(sc, R92C_LLT_INIT), R92C_LLT_INIT_OP) ==
1156                     R92C_LLT_INIT_OP_NO_ACTIVE)
1157                         return (0);
1158                 urtwn_ms_delay(sc);
1159         }
1160         return (ETIMEDOUT);
1161 }
1162
1163 static uint8_t
1164 urtwn_efuse_read_1(struct urtwn_softc *sc, uint16_t addr)
1165 {
1166         uint32_t reg;
1167         int ntries;
1168
1169         reg = urtwn_read_4(sc, R92C_EFUSE_CTRL);
1170         reg = RW(reg, R92C_EFUSE_CTRL_ADDR, addr);
1171         reg &= ~R92C_EFUSE_CTRL_VALID;
1172         urtwn_write_4(sc, R92C_EFUSE_CTRL, reg);
1173         /* Wait for read operation to complete. */
1174         for (ntries = 0; ntries < 100; ntries++) {
1175                 reg = urtwn_read_4(sc, R92C_EFUSE_CTRL);
1176                 if (reg & R92C_EFUSE_CTRL_VALID)
1177                         return (MS(reg, R92C_EFUSE_CTRL_DATA));
1178                 urtwn_ms_delay(sc);
1179         }
1180         device_printf(sc->sc_dev, 
1181             "could not read efuse byte at address 0x%x\n", addr);
1182         return (0xff);
1183 }
1184
1185 static void
1186 urtwn_efuse_read(struct urtwn_softc *sc)
1187 {
1188         uint8_t *rom = (uint8_t *)&sc->rom;
1189         uint16_t addr = 0;
1190         uint32_t reg;
1191         uint8_t off, msk;
1192         int i;
1193
1194         urtwn_efuse_switch_power(sc);
1195
1196         memset(&sc->rom, 0xff, sizeof(sc->rom));
1197         while (addr < 512) {
1198                 reg = urtwn_efuse_read_1(sc, addr);
1199                 if (reg == 0xff)
1200                         break;
1201                 addr++;
1202                 off = reg >> 4;
1203                 msk = reg & 0xf;
1204                 for (i = 0; i < 4; i++) {
1205                         if (msk & (1 << i))
1206                                 continue;
1207                         rom[off * 8 + i * 2 + 0] =
1208                             urtwn_efuse_read_1(sc, addr);
1209                         addr++;
1210                         rom[off * 8 + i * 2 + 1] =
1211                             urtwn_efuse_read_1(sc, addr);
1212                         addr++;
1213                 }
1214         }
1215 #ifdef URTWN_DEBUG
1216         if (urtwn_debug >= 2) {
1217                 /* Dump ROM content. */
1218                 printf("\n");
1219                 for (i = 0; i < sizeof(sc->rom); i++)
1220                         printf("%02x:", rom[i]);
1221                 printf("\n");
1222         }
1223 #endif
1224 }
1225 static void
1226 urtwn_efuse_switch_power(struct urtwn_softc *sc)
1227 {
1228         uint32_t reg;
1229
1230         reg = urtwn_read_2(sc, R92C_SYS_ISO_CTRL);
1231         if (!(reg & R92C_SYS_ISO_CTRL_PWC_EV12V)) {
1232                 urtwn_write_2(sc, R92C_SYS_ISO_CTRL,
1233                     reg | R92C_SYS_ISO_CTRL_PWC_EV12V);
1234         }
1235         reg = urtwn_read_2(sc, R92C_SYS_FUNC_EN);
1236         if (!(reg & R92C_SYS_FUNC_EN_ELDR)) {
1237                 urtwn_write_2(sc, R92C_SYS_FUNC_EN,
1238                     reg | R92C_SYS_FUNC_EN_ELDR);
1239         }
1240         reg = urtwn_read_2(sc, R92C_SYS_CLKR);
1241         if ((reg & (R92C_SYS_CLKR_LOADER_EN | R92C_SYS_CLKR_ANA8M)) !=
1242             (R92C_SYS_CLKR_LOADER_EN | R92C_SYS_CLKR_ANA8M)) {
1243                 urtwn_write_2(sc, R92C_SYS_CLKR,
1244                     reg | R92C_SYS_CLKR_LOADER_EN | R92C_SYS_CLKR_ANA8M);
1245         }
1246 }
1247
1248 static int
1249 urtwn_read_chipid(struct urtwn_softc *sc)
1250 {
1251         uint32_t reg;
1252
1253         if (sc->chip & URTWN_CHIP_88E)
1254                 return (0);
1255
1256         reg = urtwn_read_4(sc, R92C_SYS_CFG);
1257         if (reg & R92C_SYS_CFG_TRP_VAUX_EN)
1258                 return (EIO);
1259
1260         if (reg & R92C_SYS_CFG_TYPE_92C) {
1261                 sc->chip |= URTWN_CHIP_92C;
1262                 /* Check if it is a castrated 8192C. */
1263                 if (MS(urtwn_read_4(sc, R92C_HPON_FSM),
1264                     R92C_HPON_FSM_CHIP_BONDING_ID) ==
1265                     R92C_HPON_FSM_CHIP_BONDING_ID_92C_1T2R)
1266                         sc->chip |= URTWN_CHIP_92C_1T2R;
1267         }
1268         if (reg & R92C_SYS_CFG_VENDOR_UMC) {
1269                 sc->chip |= URTWN_CHIP_UMC;
1270                 if (MS(reg, R92C_SYS_CFG_CHIP_VER_RTL) == 0)
1271                         sc->chip |= URTWN_CHIP_UMC_A_CUT;
1272         }
1273         return (0);
1274 }
1275
1276 static void
1277 urtwn_read_rom(struct urtwn_softc *sc)
1278 {
1279         struct r92c_rom *rom = &sc->rom;
1280
1281         /* Read full ROM image. */
1282         urtwn_efuse_read(sc);
1283
1284         /* XXX Weird but this is what the vendor driver does. */
1285         sc->pa_setting = urtwn_efuse_read_1(sc, 0x1fa);
1286         DPRINTF("PA setting=0x%x\n", sc->pa_setting);
1287
1288         sc->board_type = MS(rom->rf_opt1, R92C_ROM_RF1_BOARD_TYPE);
1289
1290         sc->regulatory = MS(rom->rf_opt1, R92C_ROM_RF1_REGULATORY);
1291         DPRINTF("regulatory type=%d\n", sc->regulatory);
1292         IEEE80211_ADDR_COPY(sc->sc_bssid, rom->macaddr);
1293
1294         sc->sc_rf_write = urtwn_r92c_rf_write;
1295         sc->sc_power_on = urtwn_r92c_power_on;
1296         sc->sc_dma_init = urtwn_r92c_dma_init;
1297 }
1298
1299 static void
1300 urtwn_r88e_read_rom(struct urtwn_softc *sc)
1301 {
1302         uint8_t *rom = sc->r88e_rom;
1303         uint16_t addr = 0;
1304         uint32_t reg;
1305         uint8_t off, msk, tmp;
1306         int i;
1307
1308         off = 0;
1309         urtwn_efuse_switch_power(sc);
1310
1311         /* Read full ROM image. */
1312         memset(&sc->r88e_rom, 0xff, sizeof(sc->r88e_rom));
1313         while (addr < 1024) {
1314                 reg = urtwn_efuse_read_1(sc, addr);
1315                 if (reg == 0xff)
1316                         break;
1317                 addr++;
1318                 if ((reg & 0x1f) == 0x0f) {
1319                         tmp = (reg & 0xe0) >> 5;
1320                         reg = urtwn_efuse_read_1(sc, addr);
1321                         if ((reg & 0x0f) != 0x0f)
1322                                 off = ((reg & 0xf0) >> 1) | tmp;
1323                         addr++;
1324                 } else
1325                         off = reg >> 4;
1326                 msk = reg & 0xf;
1327                 for (i = 0; i < 4; i++) {
1328                         if (msk & (1 << i))
1329                                 continue;
1330                         rom[off * 8 + i * 2 + 0] =
1331                             urtwn_efuse_read_1(sc, addr);
1332                         addr++;
1333                         rom[off * 8 + i * 2 + 1] =
1334                             urtwn_efuse_read_1(sc, addr);
1335                         addr++;
1336                 }
1337         }
1338
1339         addr = 0x10;
1340         for (i = 0; i < 6; i++)
1341                 sc->cck_tx_pwr[i] = sc->r88e_rom[addr++];
1342         for (i = 0; i < 5; i++)
1343                 sc->ht40_tx_pwr[i] = sc->r88e_rom[addr++];
1344         sc->bw20_tx_pwr_diff = (sc->r88e_rom[addr] & 0xf0) >> 4;
1345         if (sc->bw20_tx_pwr_diff & 0x08)
1346                 sc->bw20_tx_pwr_diff |= 0xf0;
1347         sc->ofdm_tx_pwr_diff = (sc->r88e_rom[addr] & 0xf);
1348         if (sc->ofdm_tx_pwr_diff & 0x08)
1349                 sc->ofdm_tx_pwr_diff |= 0xf0;
1350         sc->regulatory = MS(sc->r88e_rom[0xc1], R92C_ROM_RF1_REGULATORY);
1351         IEEE80211_ADDR_COPY(sc->sc_bssid, &sc->r88e_rom[0xd7]);
1352
1353         sc->sc_rf_write = urtwn_r88e_rf_write;
1354         sc->sc_power_on = urtwn_r88e_power_on;
1355         sc->sc_dma_init = urtwn_r88e_dma_init;
1356 }
1357
1358 /*
1359  * Initialize rate adaptation in firmware.
1360  */
1361 static int
1362 urtwn_ra_init(struct urtwn_softc *sc)
1363 {
1364         static const uint8_t map[] =
1365             { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 };
1366         struct ieee80211com *ic = sc->sc_ifp->if_l2com;
1367         struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1368         struct ieee80211_node *ni;
1369         struct ieee80211_rateset *rs;
1370         struct r92c_fw_cmd_macid_cfg cmd;
1371         uint32_t rates, basicrates;
1372         uint8_t mode;
1373         int maxrate, maxbasicrate, error, i, j;
1374
1375         ni = ieee80211_ref_node(vap->iv_bss);
1376         rs = &ni->ni_rates;
1377
1378         /* Get normal and basic rates mask. */
1379         rates = basicrates = 0;
1380         maxrate = maxbasicrate = 0;
1381         for (i = 0; i < rs->rs_nrates; i++) {
1382                 /* Convert 802.11 rate to HW rate index. */
1383                 for (j = 0; j < NELEM(map); j++)
1384                         if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == map[j])
1385                                 break;
1386                 if (j == NELEM(map))    /* Unknown rate, skip. */
1387                         continue;
1388                 rates |= 1 << j;
1389                 if (j > maxrate)
1390                         maxrate = j;
1391                 if (rs->rs_rates[i] & IEEE80211_RATE_BASIC) {
1392                         basicrates |= 1 << j;
1393                         if (j > maxbasicrate)
1394                                 maxbasicrate = j;
1395                 }
1396         }
1397         if (ic->ic_curmode == IEEE80211_MODE_11B)
1398                 mode = R92C_RAID_11B;
1399         else
1400                 mode = R92C_RAID_11BG;
1401         DPRINTF("mode=0x%x rates=0x%08x, basicrates=0x%08x\n",
1402             mode, rates, basicrates);
1403
1404         /* Set rates mask for group addressed frames. */
1405         cmd.macid = URTWN_MACID_BC | URTWN_MACID_VALID;
1406         cmd.mask = htole32(mode << 28 | basicrates);
1407         error = urtwn_fw_cmd(sc, R92C_CMD_MACID_CONFIG, &cmd, sizeof(cmd));
1408         if (error != 0) {
1409                 ieee80211_free_node(ni);
1410                 device_printf(sc->sc_dev,
1411                     "could not add broadcast station\n");
1412                 return (error);
1413         }
1414         /* Set initial MRR rate. */
1415         DPRINTF("maxbasicrate=%d\n", maxbasicrate);
1416         urtwn_write_1(sc, R92C_INIDATA_RATE_SEL(URTWN_MACID_BC),
1417             maxbasicrate);
1418
1419         /* Set rates mask for unicast frames. */
1420         cmd.macid = URTWN_MACID_BSS | URTWN_MACID_VALID;
1421         cmd.mask = htole32(mode << 28 | rates);
1422         error = urtwn_fw_cmd(sc, R92C_CMD_MACID_CONFIG, &cmd, sizeof(cmd));
1423         if (error != 0) {
1424                 ieee80211_free_node(ni);
1425                 device_printf(sc->sc_dev, "could not add BSS station\n");
1426                 return (error);
1427         }
1428         /* Set initial MRR rate. */
1429         DPRINTF("maxrate=%d\n", maxrate);
1430         urtwn_write_1(sc, R92C_INIDATA_RATE_SEL(URTWN_MACID_BSS),
1431             maxrate);
1432
1433         /* Indicate highest supported rate. */
1434         ni->ni_txrate = rs->rs_rates[rs->rs_nrates - 1];
1435         ieee80211_free_node(ni);
1436
1437         return (0);
1438 }
1439
1440 void
1441 urtwn_tsf_sync_enable(struct urtwn_softc *sc)
1442 {
1443         struct ifnet *ifp = sc->sc_ifp;
1444         struct ieee80211com *ic = ifp->if_l2com;
1445         struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1446         struct ieee80211_node *ni = vap->iv_bss;
1447
1448         uint64_t tsf;
1449
1450         /* Enable TSF synchronization. */
1451         urtwn_write_1(sc, R92C_BCN_CTRL,
1452             urtwn_read_1(sc, R92C_BCN_CTRL) & ~R92C_BCN_CTRL_DIS_TSF_UDT0);
1453
1454         urtwn_write_1(sc, R92C_BCN_CTRL,
1455             urtwn_read_1(sc, R92C_BCN_CTRL) & ~R92C_BCN_CTRL_EN_BCN);
1456
1457         /* Set initial TSF. */
1458         memcpy(&tsf, ni->ni_tstamp.data, 8);
1459         tsf = le64toh(tsf);
1460         tsf = tsf - (tsf % (vap->iv_bss->ni_intval * IEEE80211_DUR_TU));
1461         tsf -= IEEE80211_DUR_TU;
1462         urtwn_write_4(sc, R92C_TSFTR + 0, tsf);
1463         urtwn_write_4(sc, R92C_TSFTR + 4, tsf >> 32);
1464
1465         urtwn_write_1(sc, R92C_BCN_CTRL,
1466             urtwn_read_1(sc, R92C_BCN_CTRL) | R92C_BCN_CTRL_EN_BCN);
1467 }
1468
1469 static void
1470 urtwn_set_led(struct urtwn_softc *sc, int led, int on)
1471 {
1472         uint8_t reg;
1473
1474         if (led == URTWN_LED_LINK) {
1475                 if (sc->chip & URTWN_CHIP_88E) {
1476                         reg = urtwn_read_1(sc, R92C_LEDCFG2) & 0xf0;
1477                         urtwn_write_1(sc, R92C_LEDCFG2, reg | 0x60);
1478                         if (!on) {
1479                                 reg = urtwn_read_1(sc, R92C_LEDCFG2) & 0x90;
1480                                 urtwn_write_1(sc, R92C_LEDCFG2,
1481                                     reg | R92C_LEDCFG0_DIS);
1482                                 urtwn_write_1(sc, R92C_MAC_PINMUX_CFG,
1483                                     urtwn_read_1(sc, R92C_MAC_PINMUX_CFG) &
1484                                     0xfe);
1485                         }
1486                 } else {
1487                         reg = urtwn_read_1(sc, R92C_LEDCFG0) & 0x70;
1488                         if (!on)
1489                                 reg |= R92C_LEDCFG0_DIS;
1490                         urtwn_write_1(sc, R92C_LEDCFG0, reg);
1491                 }
1492                 sc->ledlink = on;       /* Save LED state. */
1493         }
1494 }
1495
1496 static int
1497 urtwn_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1498 {
1499         struct urtwn_vap *uvp = URTWN_VAP(vap);
1500         struct ieee80211com *ic = vap->iv_ic;
1501         struct urtwn_softc *sc = ic->ic_softc;
1502         struct ieee80211_node *ni;
1503         enum ieee80211_state ostate;
1504         uint32_t reg;
1505
1506         ostate = vap->iv_state;
1507         DPRINTF("%s -> %s\n", ieee80211_state_name[ostate],
1508             ieee80211_state_name[nstate]);
1509
1510         URTWN_LOCK(sc);
1511         callout_stop(&sc->sc_watchdog_ch);
1512
1513         if (ostate == IEEE80211_S_RUN) {
1514                 /* Turn link LED off. */
1515                 urtwn_set_led(sc, URTWN_LED_LINK, 0);
1516
1517                 /* Set media status to 'No Link'. */
1518                 reg = urtwn_read_4(sc, R92C_CR);
1519                 reg = RW(reg, R92C_CR_NETTYPE, R92C_CR_NETTYPE_NOLINK);
1520                 urtwn_write_4(sc, R92C_CR, reg);
1521
1522                 /* Stop Rx of data frames. */
1523                 urtwn_write_2(sc, R92C_RXFLTMAP2, 0);
1524
1525                 /* Rest TSF. */
1526                 urtwn_write_1(sc, R92C_DUAL_TSF_RST, 0x03);
1527
1528                 /* Disable TSF synchronization. */
1529                 urtwn_write_1(sc, R92C_BCN_CTRL,
1530                     urtwn_read_1(sc, R92C_BCN_CTRL) |
1531                     R92C_BCN_CTRL_DIS_TSF_UDT0);
1532
1533                 /* Reset EDCA parameters. */
1534                 urtwn_write_4(sc, R92C_EDCA_VO_PARAM, 0x002f3217);
1535                 urtwn_write_4(sc, R92C_EDCA_VI_PARAM, 0x005e4317);
1536                 urtwn_write_4(sc, R92C_EDCA_BE_PARAM, 0x00105320);
1537                 urtwn_write_4(sc, R92C_EDCA_BK_PARAM, 0x0000a444);
1538         }
1539
1540         switch (nstate) {
1541         case IEEE80211_S_INIT:
1542                 /* Turn link LED off. */
1543                 urtwn_set_led(sc, URTWN_LED_LINK, 0);
1544                 break;
1545         case IEEE80211_S_SCAN:
1546                 if (ostate != IEEE80211_S_SCAN) {
1547                         /* Allow Rx from any BSSID. */
1548                         urtwn_write_4(sc, R92C_RCR,
1549                             urtwn_read_4(sc, R92C_RCR) &
1550                             ~(R92C_RCR_CBSSID_DATA | R92C_RCR_CBSSID_BCN));
1551
1552                         /* Set gain for scanning. */
1553                         reg = urtwn_bb_read(sc, R92C_OFDM0_AGCCORE1(0));
1554                         reg = RW(reg, R92C_OFDM0_AGCCORE1_GAIN, 0x20);
1555                         urtwn_bb_write(sc, R92C_OFDM0_AGCCORE1(0), reg);
1556
1557                         if (!(sc->chip & URTWN_CHIP_88E)) {
1558                                 reg = urtwn_bb_read(sc, R92C_OFDM0_AGCCORE1(1));
1559                                 reg = RW(reg, R92C_OFDM0_AGCCORE1_GAIN, 0x20);
1560                                 urtwn_bb_write(sc, R92C_OFDM0_AGCCORE1(1), reg);
1561                         }
1562                 }
1563                 /* Make link LED blink during scan. */
1564                 urtwn_set_led(sc, URTWN_LED_LINK, !sc->ledlink);
1565
1566                 /* Pause AC Tx queues. */
1567                 urtwn_write_1(sc, R92C_TXPAUSE,
1568                     urtwn_read_1(sc, R92C_TXPAUSE) | 0x0f);
1569
1570                 urtwn_set_chan(sc, ic->ic_curchan, NULL);
1571                 break;
1572         case IEEE80211_S_AUTH:
1573                 /* Set initial gain under link. */
1574                 reg = urtwn_bb_read(sc, R92C_OFDM0_AGCCORE1(0));
1575                 reg = RW(reg, R92C_OFDM0_AGCCORE1_GAIN, 0x32);
1576                 urtwn_bb_write(sc, R92C_OFDM0_AGCCORE1(0), reg);
1577
1578                 if (!(sc->chip & URTWN_CHIP_88E)) {
1579                         reg = urtwn_bb_read(sc, R92C_OFDM0_AGCCORE1(1));
1580                         reg = RW(reg, R92C_OFDM0_AGCCORE1_GAIN, 0x32);
1581                         urtwn_bb_write(sc, R92C_OFDM0_AGCCORE1(1), reg);
1582                 }
1583                 urtwn_set_chan(sc, ic->ic_curchan, NULL);
1584                 break;
1585         case IEEE80211_S_RUN:
1586                 if (vap->iv_opmode == IEEE80211_M_MONITOR) {
1587                         /* Enable Rx of data frames. */
1588                         urtwn_write_2(sc, R92C_RXFLTMAP2, 0xffff);
1589
1590                         /* Turn link LED on. */
1591                         urtwn_set_led(sc, URTWN_LED_LINK, 1);
1592                         break;
1593                 }
1594
1595                 ni = ieee80211_ref_node(vap->iv_bss);
1596                 /* Set media status to 'Associated'. */
1597                 reg = urtwn_read_4(sc, R92C_CR);
1598                 reg = RW(reg, R92C_CR_NETTYPE, R92C_CR_NETTYPE_INFRA);
1599                 urtwn_write_4(sc, R92C_CR, reg);
1600
1601                 /* Set BSSID. */
1602                 urtwn_write_4(sc, R92C_BSSID + 0, LE_READ_4(&ni->ni_bssid[0]));
1603                 urtwn_write_4(sc, R92C_BSSID + 4, LE_READ_2(&ni->ni_bssid[4]));
1604
1605                 if (ic->ic_curmode == IEEE80211_MODE_11B)
1606                         urtwn_write_1(sc, R92C_INIRTS_RATE_SEL, 0);
1607                 else    /* 802.11b/g */
1608                         urtwn_write_1(sc, R92C_INIRTS_RATE_SEL, 3);
1609
1610                 /* Enable Rx of data frames. */
1611                 urtwn_write_2(sc, R92C_RXFLTMAP2, 0xffff);
1612
1613                 /* Flush all AC queues. */
1614                 urtwn_write_1(sc, R92C_TXPAUSE, 0);
1615
1616                 /* Set beacon interval. */
1617                 urtwn_write_2(sc, R92C_BCN_INTERVAL, ni->ni_intval);
1618
1619                 /* Allow Rx from our BSSID only. */
1620                 urtwn_write_4(sc, R92C_RCR,
1621                     urtwn_read_4(sc, R92C_RCR) |
1622                     R92C_RCR_CBSSID_DATA | R92C_RCR_CBSSID_BCN);
1623
1624                 /* Enable TSF synchronization. */
1625                 urtwn_tsf_sync_enable(sc);
1626
1627                 urtwn_write_1(sc, R92C_SIFS_CCK + 1, 10);
1628                 urtwn_write_1(sc, R92C_SIFS_OFDM + 1, 10);
1629                 urtwn_write_1(sc, R92C_SPEC_SIFS + 1, 10);
1630                 urtwn_write_1(sc, R92C_MAC_SPEC_SIFS + 1, 10);
1631                 urtwn_write_1(sc, R92C_R2T_SIFS + 1, 10);
1632                 urtwn_write_1(sc, R92C_T2T_SIFS + 1, 10);
1633
1634                 /* Intialize rate adaptation. */
1635                 if (sc->chip & URTWN_CHIP_88E)
1636                         ni->ni_txrate =
1637                             ni->ni_rates.rs_rates[ni->ni_rates.rs_nrates-1];
1638                 else 
1639                         urtwn_ra_init(sc);
1640                 /* Turn link LED on. */
1641                 urtwn_set_led(sc, URTWN_LED_LINK, 1);
1642
1643                 sc->avg_pwdb = -1;      /* Reset average RSSI. */
1644                 /* Reset temperature calibration state machine. */
1645                 sc->thcal_state = 0;
1646                 sc->thcal_lctemp = 0;
1647                 ieee80211_free_node(ni);
1648                 break;
1649         default:
1650                 break;
1651         }
1652         URTWN_UNLOCK(sc);
1653         return(uvp->newstate(vap, nstate, arg));
1654 }
1655
1656 static void
1657 urtwn_watchdog(void *arg)
1658 {
1659         struct urtwn_softc *sc = arg;
1660         struct ifnet *ifp = sc->sc_ifp;
1661
1662         if (sc->sc_txtimer > 0) {
1663                 if (--sc->sc_txtimer == 0) {
1664                         device_printf(sc->sc_dev, "device timeout\n");
1665                         IFNET_STAT_INC(ifp, oerrors, 1);
1666                         return;
1667                 }
1668                 callout_reset(&sc->sc_watchdog_ch, hz, urtwn_watchdog, sc);
1669         }
1670 }
1671
1672 static void
1673 urtwn_update_avgrssi(struct urtwn_softc *sc, int rate, int8_t rssi)
1674 {
1675         int pwdb;
1676
1677         /* Convert antenna signal to percentage. */
1678         if (rssi <= -100 || rssi >= 20)
1679                 pwdb = 0;
1680         else if (rssi >= 0)
1681                 pwdb = 100;
1682         else
1683                 pwdb = 100 + rssi;
1684         if (!(sc->chip & URTWN_CHIP_88E)) {
1685                 if (rate <= 3) {
1686                         /* CCK gain is smaller than OFDM/MCS gain. */
1687                         pwdb += 6;
1688                         if (pwdb > 100)
1689                                 pwdb = 100;
1690                         if (pwdb <= 14)
1691                                 pwdb -= 4;
1692                         else if (pwdb <= 26)
1693                                 pwdb -= 8;
1694                         else if (pwdb <= 34)
1695                                 pwdb -= 6;
1696                         else if (pwdb <= 42)
1697                                 pwdb -= 2;
1698                 }
1699         }
1700         if (sc->avg_pwdb == -1) /* Init. */
1701                 sc->avg_pwdb = pwdb;
1702         else if (sc->avg_pwdb < pwdb)
1703                 sc->avg_pwdb = ((sc->avg_pwdb * 19 + pwdb) / 20) + 1;
1704         else
1705                 sc->avg_pwdb = ((sc->avg_pwdb * 19 + pwdb) / 20);
1706         DPRINTFN(4, "PWDB=%d EMA=%d\n", pwdb, sc->avg_pwdb);
1707 }
1708
1709 static int8_t
1710 urtwn_get_rssi(struct urtwn_softc *sc, int rate, void *physt)
1711 {
1712         static const int8_t cckoff[] = { 16, -12, -26, -46 };
1713         struct r92c_rx_phystat *phy;
1714         struct r92c_rx_cck *cck;
1715         uint8_t rpt;
1716         int8_t rssi;
1717
1718         if (rate <= 3) {
1719                 cck = (struct r92c_rx_cck *)physt;
1720                 if (sc->sc_flags & URTWN_FLAG_CCK_HIPWR) {
1721                         rpt = (cck->agc_rpt >> 5) & 0x3;
1722                         rssi = (cck->agc_rpt & 0x1f) << 1;
1723                 } else {
1724                         rpt = (cck->agc_rpt >> 6) & 0x3;
1725                         rssi = cck->agc_rpt & 0x3e;
1726                 }
1727                 rssi = cckoff[rpt] - rssi;
1728         } else {        /* OFDM/HT. */
1729                 phy = (struct r92c_rx_phystat *)physt;
1730                 rssi = ((le32toh(phy->phydw1) >> 1) & 0x7f) - 110;
1731         }
1732         return (rssi);
1733 }
1734
1735 static int8_t
1736 urtwn_r88e_get_rssi(struct urtwn_softc *sc, int rate, void *physt)
1737 {
1738         struct r92c_rx_phystat *phy;
1739         struct r88e_rx_cck *cck;
1740         uint8_t cck_agc_rpt, lna_idx, vga_idx;
1741         int8_t rssi;
1742
1743         rssi = 0;
1744         if (rate <= 3) {
1745                 cck = (struct r88e_rx_cck *)physt;
1746                 cck_agc_rpt = cck->agc_rpt;
1747                 lna_idx = (cck_agc_rpt & 0xe0) >> 5;
1748                 vga_idx = cck_agc_rpt & 0x1f; 
1749                 switch (lna_idx) {
1750                 case 7:
1751                         if (vga_idx <= 27)
1752                                 rssi = -100 + 2* (27 - vga_idx);
1753                         else
1754                                 rssi = -100;
1755                         break;
1756                 case 6:
1757                         rssi = -48 + 2 * (2 - vga_idx);
1758                         break;
1759                 case 5:
1760                         rssi = -42 + 2 * (7 - vga_idx);
1761                         break;
1762                 case 4:
1763                         rssi = -36 + 2 * (7 - vga_idx);
1764                         break;
1765                 case 3:
1766                         rssi = -24 + 2 * (7 - vga_idx);
1767                         break;
1768                 case 2:
1769                         rssi = -12 + 2 * (5 - vga_idx);
1770                         break;
1771                 case 1:
1772                         rssi = 8 - (2 * vga_idx);
1773                         break;
1774                 case 0:
1775                         rssi = 14 - (2 * vga_idx);
1776                         break;
1777                 }
1778                 rssi += 6;
1779         } else {        /* OFDM/HT. */
1780                 phy = (struct r92c_rx_phystat *)physt;
1781                 rssi = ((le32toh(phy->phydw1) >> 1) & 0x7f) - 110;
1782         }
1783         return (rssi);
1784 }
1785
1786
1787 static int
1788 urtwn_tx_start(struct urtwn_softc *sc, struct ieee80211_node *ni, 
1789     struct mbuf *m0, struct urtwn_data *data)
1790 {
1791         struct ifnet *ifp = sc->sc_ifp;
1792         struct ieee80211_frame *wh;
1793         struct ieee80211_key *k;
1794         struct ieee80211com *ic = ifp->if_l2com;
1795         struct ieee80211vap *vap = ni->ni_vap;
1796         struct usb_xfer *xfer;
1797         struct r92c_tx_desc *txd;
1798         uint8_t raid, type;
1799         uint16_t sum;
1800         int i, hasqos, xferlen;
1801         struct usb_xfer *urtwn_pipes[4] = {
1802                 sc->sc_xfer[URTWN_BULK_TX_BE],
1803                 sc->sc_xfer[URTWN_BULK_TX_BK],
1804                 sc->sc_xfer[URTWN_BULK_TX_VI],
1805                 sc->sc_xfer[URTWN_BULK_TX_VO]
1806         };
1807
1808         URTWN_ASSERT_LOCKED(sc);
1809
1810         /*
1811          * Software crypto.
1812          */
1813         wh = mtod(m0, struct ieee80211_frame *);
1814         type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1815
1816         if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1817                 k = ieee80211_crypto_encap(ni, m0);
1818                 if (k == NULL) {
1819                         device_printf(sc->sc_dev,
1820                             "ieee80211_crypto_encap returns NULL.\n");
1821                         /* XXX we don't expect the fragmented frames */
1822                         m_freem(m0);
1823                         return (ENOBUFS);
1824                 }
1825
1826                 /* in case packet header moved, reset pointer */
1827                 wh = mtod(m0, struct ieee80211_frame *);
1828         }
1829
1830         switch (type) {
1831         case IEEE80211_FC0_TYPE_CTL:
1832         case IEEE80211_FC0_TYPE_MGT:
1833                 xfer = sc->sc_xfer[URTWN_BULK_TX_VO];
1834                 break;
1835         default:
1836                 KASSERT(M_WME_GETAC(m0) < 4,
1837                     ("unsupported WME pipe %d", M_WME_GETAC(m0)));
1838                 xfer = urtwn_pipes[M_WME_GETAC(m0)];
1839                 break;
1840         }
1841
1842         hasqos = 0;
1843
1844         /* Fill Tx descriptor. */
1845         txd = (struct r92c_tx_desc *)data->buf;
1846         memset(txd, 0, sizeof(*txd));
1847
1848         txd->txdw0 |= htole32(
1849             SM(R92C_TXDW0_PKTLEN, m0->m_pkthdr.len) |
1850             SM(R92C_TXDW0_OFFSET, sizeof(*txd)) |
1851             R92C_TXDW0_OWN | R92C_TXDW0_FSG | R92C_TXDW0_LSG);
1852         if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1853                 txd->txdw0 |= htole32(R92C_TXDW0_BMCAST);
1854         if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
1855             type == IEEE80211_FC0_TYPE_DATA) {
1856                 if (ic->ic_curmode == IEEE80211_MODE_11B)
1857                         raid = R92C_RAID_11B;
1858                 else
1859                         raid = R92C_RAID_11BG;
1860                 if (sc->chip & URTWN_CHIP_88E) {
1861                         txd->txdw1 |= htole32(
1862                             SM(R88E_TXDW1_MACID, URTWN_MACID_BSS) |
1863                             SM(R92C_TXDW1_QSEL, R92C_TXDW1_QSEL_BE) |
1864                             SM(R92C_TXDW1_RAID, raid));
1865                         txd->txdw2 |= htole32(R88E_TXDW2_AGGBK);
1866                 } else {
1867                         txd->txdw1 |= htole32(
1868                             SM(R92C_TXDW1_MACID, URTWN_MACID_BSS) |
1869                             SM(R92C_TXDW1_QSEL, R92C_TXDW1_QSEL_BE) |
1870                             SM(R92C_TXDW1_RAID, raid) | R92C_TXDW1_AGGBK);
1871                 }
1872                 if (ic->ic_flags & IEEE80211_F_USEPROT) {
1873                         if (ic->ic_protmode == IEEE80211_PROT_CTSONLY) {
1874                                 txd->txdw4 |= htole32(R92C_TXDW4_CTS2SELF |
1875                                     R92C_TXDW4_HWRTSEN);
1876                         } else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS) {
1877                                 txd->txdw4 |= htole32(R92C_TXDW4_RTSEN |
1878                                     R92C_TXDW4_HWRTSEN);
1879                         }
1880                 }
1881                 /* Send RTS at OFDM24. */
1882                 txd->txdw4 |= htole32(SM(R92C_TXDW4_RTSRATE, 8));
1883                 txd->txdw5 |= htole32(0x0001ff00);
1884                 /* Send data at OFDM54. */
1885                 if (sc->chip & URTWN_CHIP_88E)
1886                         txd->txdw5 |= htole32(0x13 & 0x3f);
1887                 else
1888                         txd->txdw5 |= htole32(SM(R92C_TXDW5_DATARATE, 11));
1889         } else {
1890                 txd->txdw1 |= htole32(
1891                     SM(R92C_TXDW1_MACID, 0) |
1892                     SM(R92C_TXDW1_QSEL, R92C_TXDW1_QSEL_MGNT) |
1893                     SM(R92C_TXDW1_RAID, R92C_RAID_11B));
1894
1895                 /* Force CCK1. */
1896                 txd->txdw4 |= htole32(R92C_TXDW4_DRVRATE);
1897                 txd->txdw5 |= htole32(SM(R92C_TXDW5_DATARATE, 0));
1898         }
1899         /* Set sequence number (already little endian). */
1900         txd->txdseq |= *(uint16_t *)wh->i_seq;
1901
1902         if (!hasqos) {
1903                 /* Use HW sequence numbering for non-QoS frames. */
1904                 txd->txdw4  |= htole32(R92C_TXDW4_HWSEQ);
1905                 txd->txdseq |= htole16(0x8000);
1906         } else
1907                 txd->txdw4 |= htole32(R92C_TXDW4_QOS);
1908
1909         /* Compute Tx descriptor checksum. */
1910         sum = 0;
1911         for (i = 0; i < sizeof(*txd) / 2; i++)
1912                 sum ^= ((uint16_t *)txd)[i];
1913         txd->txdsum = sum;      /* NB: already little endian. */
1914
1915         if (ieee80211_radiotap_active_vap(vap)) {
1916                 struct urtwn_tx_radiotap_header *tap = &sc->sc_txtap;
1917
1918                 tap->wt_flags = 0;
1919                 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1920                 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1921                 ieee80211_radiotap_tx(vap, m0);
1922         }
1923
1924         xferlen = sizeof(*txd) + m0->m_pkthdr.len;
1925         m_copydata(m0, 0, m0->m_pkthdr.len, (caddr_t)&txd[1]);
1926
1927         data->buflen = xferlen;
1928         data->ni = ni;
1929         data->m = m0;
1930
1931         STAILQ_INSERT_TAIL(&sc->sc_tx_pending, data, next);
1932         usbd_transfer_start(xfer);
1933         return (0);
1934 }
1935
1936 static void
1937 urtwn_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
1938 {
1939         if ((ifp->if_flags & IFF_RUNNING) == 0)
1940                 return;
1941
1942         ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
1943         urtwn_start_locked(ifp);
1944 }
1945
1946 static void
1947 urtwn_start_locked(struct ifnet *ifp)
1948 {
1949         struct urtwn_softc *sc = ifp->if_softc;
1950         struct ieee80211_node *ni;
1951         struct mbuf *m;
1952         struct urtwn_data *bf;
1953
1954         URTWN_LOCK(sc);
1955         for (;;) {
1956                 m = ifq_dequeue(&ifp->if_snd);
1957                 if (m == NULL)
1958                         break;
1959                 bf = urtwn_getbuf(sc);
1960                 if (bf == NULL) {
1961                         ifq_prepend(&ifp->if_snd, m);
1962                         break;
1963                 }
1964                 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
1965                 m->m_pkthdr.rcvif = NULL;
1966
1967                 if (urtwn_tx_start(sc, ni, m, bf) != 0) {
1968                         IFNET_STAT_INC(ifp, oerrors, 1);
1969                         STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, bf, next);
1970                         ieee80211_free_node(ni);
1971                         break;
1972                 }
1973
1974                 sc->sc_txtimer = 5;
1975                 callout_reset(&sc->sc_watchdog_ch, hz, urtwn_watchdog, sc);
1976         }
1977         URTWN_UNLOCK(sc);
1978 }
1979
1980 static int
1981 urtwn_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *ucred)
1982 {
1983         struct ieee80211com *ic = ifp->if_l2com;
1984         struct urtwn_softc *sc = ic->ic_softc;
1985         struct ifreq *ifr = (struct ifreq *) data;
1986         int error = 0, startall = 0;
1987
1988         URTWN_LOCK(sc);
1989         error = (sc->sc_flags & URTWN_DETACHED) ? ENXIO : 0;
1990         URTWN_UNLOCK(sc);
1991         if (error != 0)
1992                 return (error);
1993
1994         switch (cmd) {
1995         case SIOCSIFFLAGS:
1996                 if (ifp->if_flags & IFF_UP) {
1997                         if ((ifp->if_flags & IFF_RUNNING) == 0) {
1998                                 urtwn_init(sc);
1999                                 startall = 1;
2000                         }
2001                 } else {
2002                         if (ifp->if_flags & IFF_RUNNING)
2003                                 urtwn_stop(ifp);
2004                 }
2005                 if (startall)
2006                         ieee80211_start_all(ic);
2007                 break;
2008         case SIOCGIFMEDIA:
2009                 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
2010                 break;
2011         case SIOCGIFADDR:
2012                 error = ether_ioctl(ifp, cmd, data);
2013                 break;
2014         default:
2015                 error = EINVAL;
2016                 break;
2017         }
2018         return (error);
2019 }
2020
2021 static int
2022 urtwn_alloc_list(struct urtwn_softc *sc, struct urtwn_data data[],
2023     int ndata, int maxsz)
2024 {
2025         int i;
2026
2027         for (i = 0; i < ndata; i++) {
2028                 struct urtwn_data *dp = &data[i];
2029                 dp->sc = sc;
2030                 dp->m = NULL;
2031                 dp->buf = kmalloc(maxsz, M_USBDEV, M_WAITOK);
2032                 dp->ni = NULL;
2033         }
2034
2035         return (0);
2036 }
2037
2038 static int
2039 urtwn_alloc_rx_list(struct urtwn_softc *sc)
2040 {
2041         int error, i;
2042
2043         error = urtwn_alloc_list(sc, sc->sc_rx, URTWN_RX_LIST_COUNT,
2044             URTWN_RXBUFSZ);
2045         if (error != 0)
2046                 return (error);
2047
2048         STAILQ_INIT(&sc->sc_rx_active);
2049         STAILQ_INIT(&sc->sc_rx_inactive);
2050
2051         for (i = 0; i < URTWN_RX_LIST_COUNT; i++)
2052                 STAILQ_INSERT_HEAD(&sc->sc_rx_inactive, &sc->sc_rx[i], next);
2053
2054         return (0);
2055 }
2056
2057 static int
2058 urtwn_alloc_tx_list(struct urtwn_softc *sc)
2059 {
2060         int error, i;
2061
2062         error = urtwn_alloc_list(sc, sc->sc_tx, URTWN_TX_LIST_COUNT,
2063             URTWN_TXBUFSZ);
2064         if (error != 0)
2065                 return (error);
2066
2067         STAILQ_INIT(&sc->sc_tx_active);
2068         STAILQ_INIT(&sc->sc_tx_inactive);
2069         STAILQ_INIT(&sc->sc_tx_pending);
2070
2071         for (i = 0; i < URTWN_TX_LIST_COUNT; i++)
2072                 STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, &sc->sc_tx[i], next);
2073
2074         return (0);
2075 }
2076
2077 static __inline int
2078 urtwn_power_on(struct urtwn_softc *sc)
2079 {
2080
2081         return sc->sc_power_on(sc);
2082 }
2083
2084 static int
2085 urtwn_r92c_power_on(struct urtwn_softc *sc)
2086 {
2087         uint32_t reg;
2088         int ntries;
2089
2090         /* Wait for autoload done bit. */
2091         for (ntries = 0; ntries < 1000; ntries++) {
2092                 if (urtwn_read_1(sc, R92C_APS_FSMCO) & R92C_APS_FSMCO_PFM_ALDN)
2093                         break;
2094                 urtwn_ms_delay(sc);
2095         }
2096         if (ntries == 1000) {
2097                 device_printf(sc->sc_dev,
2098                     "timeout waiting for chip autoload\n");
2099                 return (ETIMEDOUT);
2100         }
2101
2102         /* Unlock ISO/CLK/Power control register. */
2103         urtwn_write_1(sc, R92C_RSV_CTRL, 0);
2104         /* Move SPS into PWM mode. */
2105         urtwn_write_1(sc, R92C_SPS0_CTRL, 0x2b);
2106         urtwn_ms_delay(sc);
2107
2108         reg = urtwn_read_1(sc, R92C_LDOV12D_CTRL);
2109         if (!(reg & R92C_LDOV12D_CTRL_LDV12_EN)) {
2110                 urtwn_write_1(sc, R92C_LDOV12D_CTRL,
2111                     reg | R92C_LDOV12D_CTRL_LDV12_EN);
2112                 urtwn_ms_delay(sc);
2113                 urtwn_write_1(sc, R92C_SYS_ISO_CTRL,
2114                     urtwn_read_1(sc, R92C_SYS_ISO_CTRL) &
2115                     ~R92C_SYS_ISO_CTRL_MD2PP);
2116         }
2117
2118         /* Auto enable WLAN. */
2119         urtwn_write_2(sc, R92C_APS_FSMCO,
2120             urtwn_read_2(sc, R92C_APS_FSMCO) | R92C_APS_FSMCO_APFM_ONMAC);
2121         for (ntries = 0; ntries < 1000; ntries++) {
2122                 if (!(urtwn_read_2(sc, R92C_APS_FSMCO) &
2123                     R92C_APS_FSMCO_APFM_ONMAC))
2124                         break;
2125                 urtwn_ms_delay(sc);
2126         }
2127         if (ntries == 1000) {
2128                 device_printf(sc->sc_dev,
2129                     "timeout waiting for MAC auto ON\n");
2130                 return (ETIMEDOUT);
2131         }
2132
2133         /* Enable radio, GPIO and LED functions. */
2134         urtwn_write_2(sc, R92C_APS_FSMCO,
2135             R92C_APS_FSMCO_AFSM_HSUS |
2136             R92C_APS_FSMCO_PDN_EN |
2137             R92C_APS_FSMCO_PFM_ALDN);
2138         /* Release RF digital isolation. */
2139         urtwn_write_2(sc, R92C_SYS_ISO_CTRL,
2140             urtwn_read_2(sc, R92C_SYS_ISO_CTRL) & ~R92C_SYS_ISO_CTRL_DIOR);
2141
2142         /* Initialize MAC. */
2143         urtwn_write_1(sc, R92C_APSD_CTRL,
2144             urtwn_read_1(sc, R92C_APSD_CTRL) & ~R92C_APSD_CTRL_OFF);
2145         for (ntries = 0; ntries < 200; ntries++) {
2146                 if (!(urtwn_read_1(sc, R92C_APSD_CTRL) &
2147                     R92C_APSD_CTRL_OFF_STATUS))
2148                         break;
2149                 urtwn_ms_delay(sc);
2150         }
2151         if (ntries == 200) {
2152                 device_printf(sc->sc_dev,
2153                     "timeout waiting for MAC initialization\n");
2154                 return (ETIMEDOUT);
2155         }
2156
2157         /* Enable MAC DMA/WMAC/SCHEDULE/SEC blocks. */
2158         reg = urtwn_read_2(sc, R92C_CR);
2159         reg |= R92C_CR_HCI_TXDMA_EN | R92C_CR_HCI_RXDMA_EN |
2160             R92C_CR_TXDMA_EN | R92C_CR_RXDMA_EN | R92C_CR_PROTOCOL_EN |
2161             R92C_CR_SCHEDULE_EN | R92C_CR_MACTXEN | R92C_CR_MACRXEN |
2162             R92C_CR_ENSEC;
2163         urtwn_write_2(sc, R92C_CR, reg);
2164
2165         urtwn_write_1(sc, 0xfe10, 0x19);
2166         return (0);
2167 }
2168
2169 static int
2170 urtwn_r88e_power_on(struct urtwn_softc *sc)
2171 {
2172         uint8_t val;
2173         uint32_t reg;
2174         int ntries;
2175
2176         /* Wait for power ready bit. */
2177         for (ntries = 0; ntries < 5000; ntries++) {
2178                 val = urtwn_read_1(sc, 0x6) & 0x2;
2179                 if (val == 0x2)
2180                         break;
2181                 urtwn_ms_delay(sc);
2182         }
2183         if (ntries == 5000) {
2184                 device_printf(sc->sc_dev,
2185                     "timeout waiting for chip power up\n");
2186                 return (ETIMEDOUT);
2187         }
2188
2189         /* Reset BB. */
2190         urtwn_write_1(sc, R92C_SYS_FUNC_EN,
2191             urtwn_read_1(sc, R92C_SYS_FUNC_EN) & ~(R92C_SYS_FUNC_EN_BBRSTB |
2192             R92C_SYS_FUNC_EN_BB_GLB_RST));
2193
2194         urtwn_write_1(sc, 0x26, urtwn_read_1(sc, 0x26) | 0x80);
2195
2196         /* Disable HWPDN. */
2197         urtwn_write_1(sc, 0x5, urtwn_read_1(sc, 0x5) & ~0x80);
2198
2199         /* Disable WL suspend. */
2200         urtwn_write_1(sc, 0x5, urtwn_read_1(sc, 0x5) & ~0x18);
2201
2202         urtwn_write_1(sc, 0x5, urtwn_read_1(sc, 0x5) | 0x1);
2203         for (ntries = 0; ntries < 5000; ntries++) {
2204                 if (!(urtwn_read_1(sc, 0x5) & 0x1))
2205                         break;
2206                 urtwn_ms_delay(sc);
2207         }
2208         if (ntries == 5000)
2209                 return (ETIMEDOUT);
2210
2211         /* Enable LDO normal mode. */
2212         urtwn_write_1(sc, 0x23, urtwn_read_1(sc, 0x23) & ~0x10);
2213
2214         /* Enable MAC DMA/WMAC/SCHEDULE/SEC blocks. */
2215         urtwn_write_2(sc, R92C_CR, 0);
2216         reg = urtwn_read_2(sc, R92C_CR);
2217         reg |= R92C_CR_HCI_TXDMA_EN | R92C_CR_HCI_RXDMA_EN |
2218             R92C_CR_TXDMA_EN | R92C_CR_RXDMA_EN | R92C_CR_PROTOCOL_EN |
2219             R92C_CR_SCHEDULE_EN | R92C_CR_ENSEC | R92C_CR_CALTMR_EN;
2220         urtwn_write_2(sc, R92C_CR, reg);
2221
2222         return (0);
2223 }
2224
2225 static int
2226 urtwn_llt_init(struct urtwn_softc *sc)
2227 {
2228         int i, error, page_count, pktbuf_count;
2229
2230         page_count = (sc->chip & URTWN_CHIP_88E) ?
2231             R88E_TX_PAGE_COUNT : R92C_TX_PAGE_COUNT;
2232         pktbuf_count = (sc->chip & URTWN_CHIP_88E) ?
2233             R88E_TXPKTBUF_COUNT : R92C_TXPKTBUF_COUNT;
2234
2235         /* Reserve pages [0; page_count]. */
2236         for (i = 0; i < page_count; i++) {
2237                 if ((error = urtwn_llt_write(sc, i, i + 1)) != 0)
2238                         return (error);
2239         }
2240         /* NB: 0xff indicates end-of-list. */
2241         if ((error = urtwn_llt_write(sc, i, 0xff)) != 0)
2242                 return (error);
2243         /*
2244          * Use pages [page_count + 1; pktbuf_count - 1]
2245          * as ring buffer.
2246          */
2247         for (++i; i < pktbuf_count - 1; i++) {
2248                 if ((error = urtwn_llt_write(sc, i, i + 1)) != 0)
2249                         return (error);
2250         }
2251         /* Make the last page point to the beginning of the ring buffer. */
2252         error = urtwn_llt_write(sc, i, page_count + 1);
2253         return (error);
2254 }
2255
2256 static void
2257 urtwn_fw_reset(struct urtwn_softc *sc)
2258 {
2259         uint16_t reg;
2260         int ntries;
2261
2262         /* Tell 8051 to reset itself. */
2263         urtwn_write_1(sc, R92C_HMETFR + 3, 0x20);
2264
2265         /* Wait until 8051 resets by itself. */
2266         for (ntries = 0; ntries < 100; ntries++) {
2267                 reg = urtwn_read_2(sc, R92C_SYS_FUNC_EN);
2268                 if (!(reg & R92C_SYS_FUNC_EN_CPUEN))
2269                         return;
2270                 urtwn_ms_delay(sc);
2271         }
2272         /* Force 8051 reset. */
2273         urtwn_write_2(sc, R92C_SYS_FUNC_EN, reg & ~R92C_SYS_FUNC_EN_CPUEN);
2274 }
2275
2276 static void
2277 urtwn_r88e_fw_reset(struct urtwn_softc *sc)
2278 {
2279         uint16_t reg;
2280
2281         reg = urtwn_read_2(sc, R92C_SYS_FUNC_EN);
2282         urtwn_write_2(sc, R92C_SYS_FUNC_EN, reg & ~R92C_SYS_FUNC_EN_CPUEN);
2283         urtwn_write_2(sc, R92C_SYS_FUNC_EN, reg | R92C_SYS_FUNC_EN_CPUEN);
2284 }
2285
2286 static int
2287 urtwn_fw_loadpage(struct urtwn_softc *sc, int page, const uint8_t *buf, int len)
2288 {
2289         uint32_t reg;
2290         int off, mlen, error = 0;
2291
2292         reg = urtwn_read_4(sc, R92C_MCUFWDL);
2293         reg = RW(reg, R92C_MCUFWDL_PAGE, page);
2294         urtwn_write_4(sc, R92C_MCUFWDL, reg);
2295
2296         off = R92C_FW_START_ADDR;
2297         while (len > 0) {
2298                 if (len > 196)
2299                         mlen = 196;
2300                 else if (len > 4)
2301                         mlen = 4;
2302                 else
2303                         mlen = 1;
2304                 /* XXX fix this deconst */
2305                 error = urtwn_write_region_1(sc, off, 
2306                     __DECONST(uint8_t *, buf), mlen);
2307                 if (error != 0)
2308                         break;
2309                 off += mlen;
2310                 buf += mlen;
2311                 len -= mlen;
2312         }
2313         return (error);
2314 }
2315
2316 static int
2317 urtwn_load_firmware(struct urtwn_softc *sc)
2318 {
2319         const struct firmware *fw;
2320         const struct r92c_fw_hdr *hdr;
2321         const char *imagename;
2322         const u_char *ptr;
2323         size_t len;
2324         uint32_t reg;
2325         int mlen, ntries, page, error;
2326
2327         URTWN_UNLOCK(sc);
2328         /* Read firmware image from the filesystem. */
2329         if (sc->chip & URTWN_CHIP_88E)
2330                 imagename = "urtwn-rtl8188eufw";
2331         else if ((sc->chip & (URTWN_CHIP_UMC_A_CUT | URTWN_CHIP_92C)) ==
2332                     URTWN_CHIP_UMC_A_CUT)
2333                 imagename = "urtwn-rtl8192cfwU";
2334         else
2335                 imagename = "urtwn-rtl8192cfwT";
2336
2337         fw = firmware_get(imagename);
2338         URTWN_LOCK(sc);
2339         if (fw == NULL) {
2340                 device_printf(sc->sc_dev,
2341                     "failed loadfirmware of file %s\n", imagename);
2342                 return (ENOENT);
2343         }
2344
2345         len = fw->datasize;
2346
2347         if (len < sizeof(*hdr)) {
2348                 device_printf(sc->sc_dev, "firmware too short\n");
2349                 error = EINVAL;
2350                 goto fail;
2351         }
2352         ptr = fw->data;
2353         hdr = (const struct r92c_fw_hdr *)ptr;
2354         /* Check if there is a valid FW header and skip it. */
2355         if ((le16toh(hdr->signature) >> 4) == 0x88c ||
2356             (le16toh(hdr->signature) >> 4) == 0x88e ||
2357             (le16toh(hdr->signature) >> 4) == 0x92c) {
2358                 DPRINTF("FW V%d.%d %02d-%02d %02d:%02d\n",
2359                     le16toh(hdr->version), le16toh(hdr->subversion),
2360                     hdr->month, hdr->date, hdr->hour, hdr->minute);
2361                 ptr += sizeof(*hdr);
2362                 len -= sizeof(*hdr);
2363         }
2364
2365         if (urtwn_read_1(sc, R92C_MCUFWDL) & R92C_MCUFWDL_RAM_DL_SEL) {
2366                 if (sc->chip & URTWN_CHIP_88E)
2367                         urtwn_r88e_fw_reset(sc);
2368                 else
2369                         urtwn_fw_reset(sc);
2370                 urtwn_write_1(sc, R92C_MCUFWDL, 0);
2371         }
2372
2373         if (!(sc->chip & URTWN_CHIP_88E)) {
2374                 urtwn_write_2(sc, R92C_SYS_FUNC_EN,
2375                     urtwn_read_2(sc, R92C_SYS_FUNC_EN) |
2376                     R92C_SYS_FUNC_EN_CPUEN);
2377         }
2378         urtwn_write_1(sc, R92C_MCUFWDL,
2379             urtwn_read_1(sc, R92C_MCUFWDL) | R92C_MCUFWDL_EN);
2380         urtwn_write_1(sc, R92C_MCUFWDL + 2,
2381             urtwn_read_1(sc, R92C_MCUFWDL + 2) & ~0x08);
2382
2383         /* Reset the FWDL checksum. */
2384         urtwn_write_1(sc, R92C_MCUFWDL,
2385             urtwn_read_1(sc, R92C_MCUFWDL) | R92C_MCUFWDL_CHKSUM_RPT);
2386
2387         for (page = 0; len > 0; page++) {
2388                 mlen = min(len, R92C_FW_PAGE_SIZE);
2389                 error = urtwn_fw_loadpage(sc, page, ptr, mlen);
2390                 if (error != 0) {
2391                         device_printf(sc->sc_dev,
2392                             "could not load firmware page\n");
2393                         goto fail;
2394                 }
2395                 ptr += mlen;
2396                 len -= mlen;
2397         }
2398         urtwn_write_1(sc, R92C_MCUFWDL,
2399             urtwn_read_1(sc, R92C_MCUFWDL) & ~R92C_MCUFWDL_EN);
2400         urtwn_write_1(sc, R92C_MCUFWDL + 1, 0);
2401
2402         /* Wait for checksum report. */
2403         for (ntries = 0; ntries < 1000; ntries++) {
2404                 if (urtwn_read_4(sc, R92C_MCUFWDL) & R92C_MCUFWDL_CHKSUM_RPT)
2405                         break;
2406                 urtwn_ms_delay(sc);
2407         }
2408         if (ntries == 1000) {
2409                 device_printf(sc->sc_dev,
2410                     "timeout waiting for checksum report\n");
2411                 error = ETIMEDOUT;
2412                 goto fail;
2413         }
2414
2415         reg = urtwn_read_4(sc, R92C_MCUFWDL);
2416         reg = (reg & ~R92C_MCUFWDL_WINTINI_RDY) | R92C_MCUFWDL_RDY;
2417         urtwn_write_4(sc, R92C_MCUFWDL, reg);
2418         if (sc->chip & URTWN_CHIP_88E)
2419                 urtwn_r88e_fw_reset(sc);
2420         /* Wait for firmware readiness. */
2421         for (ntries = 0; ntries < 1000; ntries++) {
2422                 if (urtwn_read_4(sc, R92C_MCUFWDL) & R92C_MCUFWDL_WINTINI_RDY)
2423                         break;
2424                 urtwn_ms_delay(sc);
2425         }
2426         if (ntries == 1000) {
2427                 device_printf(sc->sc_dev,
2428                     "timeout waiting for firmware readiness\n");
2429                 error = ETIMEDOUT;
2430                 goto fail;
2431         }
2432 fail:
2433         firmware_put(fw, FIRMWARE_UNLOAD);
2434         return (error);
2435 }
2436
2437 static __inline int
2438 urtwn_dma_init(struct urtwn_softc *sc)
2439 {
2440         return sc->sc_dma_init(sc);
2441 }
2442
2443 static int
2444 urtwn_r92c_dma_init(struct urtwn_softc *sc)
2445 {
2446         int hashq, hasnq, haslq, nqueues, nqpages, nrempages;
2447         uint32_t reg;
2448         int error;
2449
2450         /* Initialize LLT table. */
2451         error = urtwn_llt_init(sc);
2452         if (error != 0)
2453                 return (error);
2454
2455         /* Get Tx queues to USB endpoints mapping. */
2456         hashq = hasnq = haslq = 0;
2457         reg = urtwn_read_2(sc, R92C_USB_EP + 1);
2458         DPRINTFN(2, "USB endpoints mapping 0x%x\n", reg);
2459         if (MS(reg, R92C_USB_EP_HQ) != 0)
2460                 hashq = 1;
2461         if (MS(reg, R92C_USB_EP_NQ) != 0)
2462                 hasnq = 1;
2463         if (MS(reg, R92C_USB_EP_LQ) != 0)
2464                 haslq = 1;
2465         nqueues = hashq + hasnq + haslq;
2466         if (nqueues == 0)
2467                 return (EIO);
2468         /* Get the number of pages for each queue. */
2469         nqpages = (R92C_TX_PAGE_COUNT - R92C_PUBQ_NPAGES) / nqueues;
2470         /* The remaining pages are assigned to the high priority queue. */
2471         nrempages = (R92C_TX_PAGE_COUNT - R92C_PUBQ_NPAGES) % nqueues;
2472
2473         /* Set number of pages for normal priority queue. */
2474         urtwn_write_1(sc, R92C_RQPN_NPQ, hasnq ? nqpages : 0);
2475         urtwn_write_4(sc, R92C_RQPN,
2476             /* Set number of pages for public queue. */
2477             SM(R92C_RQPN_PUBQ, R92C_PUBQ_NPAGES) |
2478             /* Set number of pages for high priority queue. */
2479             SM(R92C_RQPN_HPQ, hashq ? nqpages + nrempages : 0) |
2480             /* Set number of pages for low priority queue. */
2481             SM(R92C_RQPN_LPQ, haslq ? nqpages : 0) |
2482             /* Load values. */
2483             R92C_RQPN_LD);
2484
2485         urtwn_write_1(sc, R92C_TXPKTBUF_BCNQ_BDNY, R92C_TX_PAGE_BOUNDARY);
2486         urtwn_write_1(sc, R92C_TXPKTBUF_MGQ_BDNY, R92C_TX_PAGE_BOUNDARY);
2487         urtwn_write_1(sc, R92C_TXPKTBUF_WMAC_LBK_BF_HD, R92C_TX_PAGE_BOUNDARY);
2488         urtwn_write_1(sc, R92C_TRXFF_BNDY, R92C_TX_PAGE_BOUNDARY);
2489         urtwn_write_1(sc, R92C_TDECTRL + 1, R92C_TX_PAGE_BOUNDARY);
2490
2491         /* Set queue to USB pipe mapping. */
2492         reg = urtwn_read_2(sc, R92C_TRXDMA_CTRL);
2493         reg &= ~R92C_TRXDMA_CTRL_QMAP_M;
2494         if (nqueues == 1) {
2495                 if (hashq)
2496                         reg |= R92C_TRXDMA_CTRL_QMAP_HQ;
2497                 else if (hasnq)
2498                         reg |= R92C_TRXDMA_CTRL_QMAP_NQ;
2499                 else
2500                         reg |= R92C_TRXDMA_CTRL_QMAP_LQ;
2501         } else if (nqueues == 2) {
2502                 /* All 2-endpoints configs have a high priority queue. */
2503                 if (!hashq)
2504                         return (EIO);
2505                 if (hasnq)
2506                         reg |= R92C_TRXDMA_CTRL_QMAP_HQ_NQ;
2507                 else
2508                         reg |= R92C_TRXDMA_CTRL_QMAP_HQ_LQ;
2509         } else
2510                 reg |= R92C_TRXDMA_CTRL_QMAP_3EP;
2511         urtwn_write_2(sc, R92C_TRXDMA_CTRL, reg);
2512
2513         /* Set Tx/Rx transfer page boundary. */
2514         urtwn_write_2(sc, R92C_TRXFF_BNDY + 2, 0x27ff);
2515
2516         /* Set Tx/Rx transfer page size. */
2517         urtwn_write_1(sc, R92C_PBP,
2518             SM(R92C_PBP_PSRX, R92C_PBP_128) |
2519             SM(R92C_PBP_PSTX, R92C_PBP_128));
2520         return (0);
2521 }
2522
2523 static int
2524 urtwn_r88e_dma_init(struct urtwn_softc *sc)
2525 {
2526         struct usb_interface *iface;
2527         uint32_t reg;
2528         int nqueues;
2529         int error;
2530
2531         /* Initialize LLT table. */
2532         error = urtwn_llt_init(sc);
2533         if (error != 0)
2534                 return (error);
2535
2536         /* Get Tx queues to USB endpoints mapping. */
2537         iface = usbd_get_iface(sc->sc_udev, 0);
2538         nqueues = iface->idesc->bNumEndpoints - 1;
2539         if (nqueues == 0)
2540                 return (EIO);
2541
2542         /* Set number of pages for normal priority queue. */
2543         urtwn_write_2(sc, R92C_RQPN_NPQ, 0);
2544         urtwn_write_2(sc, R92C_RQPN_NPQ, 0x000d);
2545         urtwn_write_4(sc, R92C_RQPN, 0x808e000d);
2546
2547         urtwn_write_1(sc, R92C_TXPKTBUF_BCNQ_BDNY, R88E_TX_PAGE_BOUNDARY);
2548         urtwn_write_1(sc, R92C_TXPKTBUF_MGQ_BDNY, R88E_TX_PAGE_BOUNDARY);
2549         urtwn_write_1(sc, R92C_TXPKTBUF_WMAC_LBK_BF_HD, R88E_TX_PAGE_BOUNDARY);
2550         urtwn_write_1(sc, R92C_TRXFF_BNDY, R88E_TX_PAGE_BOUNDARY);
2551         urtwn_write_1(sc, R92C_TDECTRL + 1, R88E_TX_PAGE_BOUNDARY);
2552
2553         /* Set queue to USB pipe mapping. */
2554         reg = urtwn_read_2(sc, R92C_TRXDMA_CTRL);
2555         reg &= ~R92C_TRXDMA_CTRL_QMAP_M;
2556         if (nqueues == 1)
2557                 reg |= R92C_TRXDMA_CTRL_QMAP_LQ;
2558         else if (nqueues == 2)
2559                 reg |= R92C_TRXDMA_CTRL_QMAP_HQ_NQ;
2560         else
2561                 reg |= R92C_TRXDMA_CTRL_QMAP_3EP;
2562         urtwn_write_2(sc, R92C_TRXDMA_CTRL, reg);
2563
2564         /* Set Tx/Rx transfer page boundary. */
2565         urtwn_write_2(sc, R92C_TRXFF_BNDY + 2, 0x23ff);
2566
2567         /* Set Tx/Rx transfer page size. */
2568         urtwn_write_1(sc, R92C_PBP,
2569             SM(R92C_PBP_PSRX, R92C_PBP_128) |
2570             SM(R92C_PBP_PSTX, R92C_PBP_128));
2571
2572         return (0);
2573 }
2574
2575 static void
2576 urtwn_mac_init(struct urtwn_softc *sc)
2577 {
2578         int i;
2579
2580         /* Write MAC initialization values. */
2581         if (sc->chip & URTWN_CHIP_88E) {
2582                 for (i = 0; i < NELEM(rtl8188eu_mac); i++) {
2583                         urtwn_write_1(sc, rtl8188eu_mac[i].reg,
2584                             rtl8188eu_mac[i].val);
2585                 }
2586                 urtwn_write_1(sc, R92C_MAX_AGGR_NUM, 0x07);
2587         } else {
2588                 for (i = 0; i < NELEM(rtl8192cu_mac); i++)
2589                         urtwn_write_1(sc, rtl8192cu_mac[i].reg,
2590                             rtl8192cu_mac[i].val);
2591         }
2592 }
2593
2594 static void
2595 urtwn_bb_init(struct urtwn_softc *sc)
2596 {
2597         const struct urtwn_bb_prog *prog;
2598         uint32_t reg;
2599         uint8_t crystalcap;
2600         int i;
2601
2602         /* Enable BB and RF. */
2603         urtwn_write_2(sc, R92C_SYS_FUNC_EN,
2604             urtwn_read_2(sc, R92C_SYS_FUNC_EN) |
2605             R92C_SYS_FUNC_EN_BBRSTB | R92C_SYS_FUNC_EN_BB_GLB_RST |
2606             R92C_SYS_FUNC_EN_DIO_RF);
2607
2608         if (!(sc->chip & URTWN_CHIP_88E))
2609                 urtwn_write_2(sc, R92C_AFE_PLL_CTRL, 0xdb83);
2610
2611         urtwn_write_1(sc, R92C_RF_CTRL,
2612             R92C_RF_CTRL_EN | R92C_RF_CTRL_RSTB | R92C_RF_CTRL_SDMRSTB);
2613         urtwn_write_1(sc, R92C_SYS_FUNC_EN,
2614             R92C_SYS_FUNC_EN_USBA | R92C_SYS_FUNC_EN_USBD |
2615             R92C_SYS_FUNC_EN_BB_GLB_RST | R92C_SYS_FUNC_EN_BBRSTB);
2616
2617         if (!(sc->chip & URTWN_CHIP_88E)) {
2618                 urtwn_write_1(sc, R92C_LDOHCI12_CTRL, 0x0f);
2619                 urtwn_write_1(sc, 0x15, 0xe9);
2620                 urtwn_write_1(sc, R92C_AFE_XTAL_CTRL + 1, 0x80);
2621         }
2622
2623         /* Select BB programming based on board type. */
2624         if (sc->chip & URTWN_CHIP_88E)
2625                 prog = &rtl8188eu_bb_prog;
2626         else if (!(sc->chip & URTWN_CHIP_92C)) {
2627                 if (sc->board_type == R92C_BOARD_TYPE_MINICARD)
2628                         prog = &rtl8188ce_bb_prog;
2629                 else if (sc->board_type == R92C_BOARD_TYPE_HIGHPA)
2630                         prog = &rtl8188ru_bb_prog;
2631                 else
2632                         prog = &rtl8188cu_bb_prog;
2633         } else {
2634                 if (sc->board_type == R92C_BOARD_TYPE_MINICARD)
2635                         prog = &rtl8192ce_bb_prog;
2636                 else
2637                         prog = &rtl8192cu_bb_prog;
2638         }
2639         /* Write BB initialization values. */
2640         for (i = 0; i < prog->count; i++) {
2641                 urtwn_bb_write(sc, prog->regs[i], prog->vals[i]);
2642                 urtwn_ms_delay(sc);
2643         }
2644
2645         if (sc->chip & URTWN_CHIP_92C_1T2R) {
2646                 /* 8192C 1T only configuration. */
2647                 reg = urtwn_bb_read(sc, R92C_FPGA0_TXINFO);
2648                 reg = (reg & ~0x00000003) | 0x2;
2649                 urtwn_bb_write(sc, R92C_FPGA0_TXINFO, reg);
2650
2651                 reg = urtwn_bb_read(sc, R92C_FPGA1_TXINFO);
2652                 reg = (reg & ~0x00300033) | 0x00200022;
2653                 urtwn_bb_write(sc, R92C_FPGA1_TXINFO, reg);
2654
2655                 reg = urtwn_bb_read(sc, R92C_CCK0_AFESETTING);
2656                 reg = (reg & ~0xff000000) | 0x45 << 24;
2657                 urtwn_bb_write(sc, R92C_CCK0_AFESETTING, reg);
2658
2659                 reg = urtwn_bb_read(sc, R92C_OFDM0_TRXPATHENA);
2660                 reg = (reg & ~0x000000ff) | 0x23;
2661                 urtwn_bb_write(sc, R92C_OFDM0_TRXPATHENA, reg);
2662
2663                 reg = urtwn_bb_read(sc, R92C_OFDM0_AGCPARAM1);
2664                 reg = (reg & ~0x00000030) | 1 << 4;
2665                 urtwn_bb_write(sc, R92C_OFDM0_AGCPARAM1, reg);
2666
2667                 reg = urtwn_bb_read(sc, 0xe74);
2668                 reg = (reg & ~0x0c000000) | 2 << 26;
2669                 urtwn_bb_write(sc, 0xe74, reg);
2670                 reg = urtwn_bb_read(sc, 0xe78);
2671                 reg = (reg & ~0x0c000000) | 2 << 26;
2672                 urtwn_bb_write(sc, 0xe78, reg);
2673                 reg = urtwn_bb_read(sc, 0xe7c);
2674                 reg = (reg & ~0x0c000000) | 2 << 26;
2675                 urtwn_bb_write(sc, 0xe7c, reg);
2676                 reg = urtwn_bb_read(sc, 0xe80);
2677                 reg = (reg & ~0x0c000000) | 2 << 26;
2678                 urtwn_bb_write(sc, 0xe80, reg);
2679                 reg = urtwn_bb_read(sc, 0xe88);
2680                 reg = (reg & ~0x0c000000) | 2 << 26;
2681                 urtwn_bb_write(sc, 0xe88, reg);
2682         }
2683
2684         /* Write AGC values. */
2685         for (i = 0; i < prog->agccount; i++) {
2686                 urtwn_bb_write(sc, R92C_OFDM0_AGCRSSITABLE,
2687                     prog->agcvals[i]);
2688                 urtwn_ms_delay(sc);
2689         }
2690
2691         if (sc->chip & URTWN_CHIP_88E) {
2692                 urtwn_bb_write(sc, R92C_OFDM0_AGCCORE1(0), 0x69553422);
2693                 urtwn_ms_delay(sc);
2694                 urtwn_bb_write(sc, R92C_OFDM0_AGCCORE1(0), 0x69553420);
2695                 urtwn_ms_delay(sc);
2696
2697                 crystalcap = sc->r88e_rom[0xb9];
2698                 if (crystalcap == 0xff)
2699                         crystalcap = 0x20;
2700                 crystalcap &= 0x3f;
2701                 reg = urtwn_bb_read(sc, R92C_AFE_XTAL_CTRL);
2702                 urtwn_bb_write(sc, R92C_AFE_XTAL_CTRL,
2703                     RW(reg, R92C_AFE_XTAL_CTRL_ADDR,
2704                     crystalcap | crystalcap << 6));
2705         } else {
2706                 if (urtwn_bb_read(sc, R92C_HSSI_PARAM2(0)) &
2707                     R92C_HSSI_PARAM2_CCK_HIPWR)
2708                         sc->sc_flags |= URTWN_FLAG_CCK_HIPWR;
2709         }
2710 }
2711
2712 void
2713 urtwn_rf_init(struct urtwn_softc *sc)
2714 {
2715         const struct urtwn_rf_prog *prog;
2716         uint32_t reg, type;
2717         int i, j, idx, off;
2718
2719         /* Select RF programming based on board type. */
2720         if (sc->chip & URTWN_CHIP_88E)
2721                 prog = rtl8188eu_rf_prog;
2722         else if (!(sc->chip & URTWN_CHIP_92C)) {
2723                 if (sc->board_type == R92C_BOARD_TYPE_MINICARD)
2724                         prog = rtl8188ce_rf_prog;
2725                 else if (sc->board_type == R92C_BOARD_TYPE_HIGHPA)
2726                         prog = rtl8188ru_rf_prog;
2727                 else
2728                         prog = rtl8188cu_rf_prog;
2729         } else
2730                 prog = rtl8192ce_rf_prog;
2731
2732         for (i = 0; i < sc->nrxchains; i++) {
2733                 /* Save RF_ENV control type. */
2734                 idx = i / 2;
2735                 off = (i % 2) * 16;
2736                 reg = urtwn_bb_read(sc, R92C_FPGA0_RFIFACESW(idx));
2737                 type = (reg >> off) & 0x10;
2738
2739                 /* Set RF_ENV enable. */
2740                 reg = urtwn_bb_read(sc, R92C_FPGA0_RFIFACEOE(i));
2741                 reg |= 0x100000;
2742                 urtwn_bb_write(sc, R92C_FPGA0_RFIFACEOE(i), reg);
2743                 urtwn_ms_delay(sc);
2744                 /* Set RF_ENV output high. */
2745                 reg = urtwn_bb_read(sc, R92C_FPGA0_RFIFACEOE(i));
2746                 reg |= 0x10;
2747                 urtwn_bb_write(sc, R92C_FPGA0_RFIFACEOE(i), reg);
2748                 urtwn_ms_delay(sc);
2749                 /* Set address and data lengths of RF registers. */
2750                 reg = urtwn_bb_read(sc, R92C_HSSI_PARAM2(i));
2751                 reg &= ~R92C_HSSI_PARAM2_ADDR_LENGTH;
2752                 urtwn_bb_write(sc, R92C_HSSI_PARAM2(i), reg);
2753                 urtwn_ms_delay(sc);
2754                 reg = urtwn_bb_read(sc, R92C_HSSI_PARAM2(i));
2755                 reg &= ~R92C_HSSI_PARAM2_DATA_LENGTH;
2756                 urtwn_bb_write(sc, R92C_HSSI_PARAM2(i), reg);
2757                 urtwn_ms_delay(sc);
2758
2759                 /* Write RF initialization values for this chain. */
2760                 for (j = 0; j < prog[i].count; j++) {
2761                         if (prog[i].regs[j] >= 0xf9 &&
2762                             prog[i].regs[j] <= 0xfe) {
2763                                 /*
2764                                  * These are fake RF registers offsets that
2765                                  * indicate a delay is required.
2766                                  */
2767                                 usb_pause_mtx(&sc->sc_lock, hz / 20);
2768                                 continue;
2769                         }
2770                         urtwn_rf_write(sc, i, prog[i].regs[j],
2771                             prog[i].vals[j]);
2772                         urtwn_ms_delay(sc);
2773                 }
2774
2775                 /* Restore RF_ENV control type. */
2776                 reg = urtwn_bb_read(sc, R92C_FPGA0_RFIFACESW(idx));
2777                 reg &= ~(0x10 << off) | (type << off);
2778                 urtwn_bb_write(sc, R92C_FPGA0_RFIFACESW(idx), reg);
2779
2780                 /* Cache RF register CHNLBW. */
2781                 sc->rf_chnlbw[i] = urtwn_rf_read(sc, i, R92C_RF_CHNLBW);
2782         }
2783
2784         if ((sc->chip & (URTWN_CHIP_UMC_A_CUT | URTWN_CHIP_92C)) ==
2785             URTWN_CHIP_UMC_A_CUT) {
2786                 urtwn_rf_write(sc, 0, R92C_RF_RX_G1, 0x30255);
2787                 urtwn_rf_write(sc, 0, R92C_RF_RX_G2, 0x50a00);
2788         }
2789 }
2790
2791 static void
2792 urtwn_cam_init(struct urtwn_softc *sc)
2793 {
2794         /* Invalidate all CAM entries. */
2795         urtwn_write_4(sc, R92C_CAMCMD,
2796             R92C_CAMCMD_POLLING | R92C_CAMCMD_CLR);
2797 }
2798
2799 static void
2800 urtwn_pa_bias_init(struct urtwn_softc *sc)
2801 {
2802         uint8_t reg;
2803         int i;
2804
2805         for (i = 0; i < sc->nrxchains; i++) {
2806                 if (sc->pa_setting & (1 << i))
2807                         continue;
2808                 urtwn_rf_write(sc, i, R92C_RF_IPA, 0x0f406);
2809                 urtwn_rf_write(sc, i, R92C_RF_IPA, 0x4f406);
2810                 urtwn_rf_write(sc, i, R92C_RF_IPA, 0x8f406);
2811                 urtwn_rf_write(sc, i, R92C_RF_IPA, 0xcf406);
2812         }
2813         if (!(sc->pa_setting & 0x10)) {
2814                 reg = urtwn_read_1(sc, 0x16);
2815                 reg = (reg & ~0xf0) | 0x90;
2816                 urtwn_write_1(sc, 0x16, reg);
2817         }
2818 }
2819
2820 static void
2821 urtwn_rxfilter_init(struct urtwn_softc *sc)
2822 {
2823         /* Initialize Rx filter. */
2824         /* TODO: use better filter for monitor mode. */
2825         urtwn_write_4(sc, R92C_RCR,
2826             R92C_RCR_AAP | R92C_RCR_APM | R92C_RCR_AM | R92C_RCR_AB |
2827             R92C_RCR_APP_ICV | R92C_RCR_AMF | R92C_RCR_HTC_LOC_CTRL |
2828             R92C_RCR_APP_MIC | R92C_RCR_APP_PHYSTS);
2829         /* Accept all multicast frames. */
2830         urtwn_write_4(sc, R92C_MAR + 0, 0xffffffff);
2831         urtwn_write_4(sc, R92C_MAR + 4, 0xffffffff);
2832         /* Accept all management frames. */
2833         urtwn_write_2(sc, R92C_RXFLTMAP0, 0xffff);
2834         /* Reject all control frames. */
2835         urtwn_write_2(sc, R92C_RXFLTMAP1, 0x0000);
2836         /* Accept all data frames. */
2837         urtwn_write_2(sc, R92C_RXFLTMAP2, 0xffff);
2838 }
2839
2840 static void
2841 urtwn_edca_init(struct urtwn_softc *sc)
2842 {
2843         urtwn_write_2(sc, R92C_SPEC_SIFS, 0x100a);
2844         urtwn_write_2(sc, R92C_MAC_SPEC_SIFS, 0x100a);
2845         urtwn_write_2(sc, R92C_SIFS_CCK, 0x100a);
2846         urtwn_write_2(sc, R92C_SIFS_OFDM, 0x100a);
2847         urtwn_write_4(sc, R92C_EDCA_BE_PARAM, 0x005ea42b);
2848         urtwn_write_4(sc, R92C_EDCA_BK_PARAM, 0x0000a44f);
2849         urtwn_write_4(sc, R92C_EDCA_VI_PARAM, 0x005ea324);
2850         urtwn_write_4(sc, R92C_EDCA_VO_PARAM, 0x002fa226);
2851 }
2852
2853 void
2854 urtwn_write_txpower(struct urtwn_softc *sc, int chain,
2855     uint16_t power[URTWN_RIDX_COUNT])
2856 {
2857         uint32_t reg;
2858
2859         /* Write per-CCK rate Tx power. */
2860         if (chain == 0) {
2861                 reg = urtwn_bb_read(sc, R92C_TXAGC_A_CCK1_MCS32);
2862                 reg = RW(reg, R92C_TXAGC_A_CCK1,  power[0]);
2863                 urtwn_bb_write(sc, R92C_TXAGC_A_CCK1_MCS32, reg);
2864                 reg = urtwn_bb_read(sc, R92C_TXAGC_B_CCK11_A_CCK2_11);
2865                 reg = RW(reg, R92C_TXAGC_A_CCK2,  power[1]);
2866                 reg = RW(reg, R92C_TXAGC_A_CCK55, power[2]);
2867                 reg = RW(reg, R92C_TXAGC_A_CCK11, power[3]);
2868                 urtwn_bb_write(sc, R92C_TXAGC_B_CCK11_A_CCK2_11, reg);
2869         } else {
2870                 reg = urtwn_bb_read(sc, R92C_TXAGC_B_CCK1_55_MCS32);
2871                 reg = RW(reg, R92C_TXAGC_B_CCK1,  power[0]);
2872                 reg = RW(reg, R92C_TXAGC_B_CCK2,  power[1]);
2873                 reg = RW(reg, R92C_TXAGC_B_CCK55, power[2]);
2874                 urtwn_bb_write(sc, R92C_TXAGC_B_CCK1_55_MCS32, reg);
2875                 reg = urtwn_bb_read(sc, R92C_TXAGC_B_CCK11_A_CCK2_11);
2876                 reg = RW(reg, R92C_TXAGC_B_CCK11, power[3]);
2877                 urtwn_bb_write(sc, R92C_TXAGC_B_CCK11_A_CCK2_11, reg);
2878         }
2879         /* Write per-OFDM rate Tx power. */
2880         urtwn_bb_write(sc, R92C_TXAGC_RATE18_06(chain),
2881             SM(R92C_TXAGC_RATE06, power[ 4]) |
2882             SM(R92C_TXAGC_RATE09, power[ 5]) |
2883             SM(R92C_TXAGC_RATE12, power[ 6]) |
2884             SM(R92C_TXAGC_RATE18, power[ 7]));
2885         urtwn_bb_write(sc, R92C_TXAGC_RATE54_24(chain),
2886             SM(R92C_TXAGC_RATE24, power[ 8]) |
2887             SM(R92C_TXAGC_RATE36, power[ 9]) |
2888             SM(R92C_TXAGC_RATE48, power[10]) |
2889             SM(R92C_TXAGC_RATE54, power[11]));
2890         /* Write per-MCS Tx power. */
2891         urtwn_bb_write(sc, R92C_TXAGC_MCS03_MCS00(chain),
2892             SM(R92C_TXAGC_MCS00,  power[12]) |
2893             SM(R92C_TXAGC_MCS01,  power[13]) |
2894             SM(R92C_TXAGC_MCS02,  power[14]) |
2895             SM(R92C_TXAGC_MCS03,  power[15]));
2896         urtwn_bb_write(sc, R92C_TXAGC_MCS07_MCS04(chain),
2897             SM(R92C_TXAGC_MCS04,  power[16]) |
2898             SM(R92C_TXAGC_MCS05,  power[17]) |
2899             SM(R92C_TXAGC_MCS06,  power[18]) |
2900             SM(R92C_TXAGC_MCS07,  power[19]));
2901         urtwn_bb_write(sc, R92C_TXAGC_MCS11_MCS08(chain),
2902             SM(R92C_TXAGC_MCS08,  power[20]) |
2903             SM(R92C_TXAGC_MCS09,  power[21]) |
2904             SM(R92C_TXAGC_MCS10,  power[22]) |
2905             SM(R92C_TXAGC_MCS11,  power[23]));
2906         urtwn_bb_write(sc, R92C_TXAGC_MCS15_MCS12(chain),
2907             SM(R92C_TXAGC_MCS12,  power[24]) |
2908             SM(R92C_TXAGC_MCS13,  power[25]) |
2909             SM(R92C_TXAGC_MCS14,  power[26]) |
2910             SM(R92C_TXAGC_MCS15,  power[27]));
2911 }
2912
2913 void
2914 urtwn_get_txpower(struct urtwn_softc *sc, int chain,
2915     struct ieee80211_channel *c, struct ieee80211_channel *extc,
2916     uint16_t power[URTWN_RIDX_COUNT])
2917 {
2918         struct ieee80211com *ic = sc->sc_ifp->if_l2com;
2919         struct r92c_rom *rom = &sc->rom;
2920         uint16_t cckpow, ofdmpow, htpow, diff, max;
2921         const struct urtwn_txpwr *base;
2922         int ridx, chan, group;
2923
2924         /* Determine channel group. */
2925         chan = ieee80211_chan2ieee(ic, c);      /* XXX center freq! */
2926         if (chan <= 3)
2927                 group = 0;
2928         else if (chan <= 9)
2929                 group = 1;
2930         else
2931                 group = 2;
2932
2933         /* Get original Tx power based on board type and RF chain. */
2934         if (!(sc->chip & URTWN_CHIP_92C)) {
2935                 if (sc->board_type == R92C_BOARD_TYPE_HIGHPA)
2936                         base = &rtl8188ru_txagc[chain];
2937                 else
2938                         base = &rtl8192cu_txagc[chain];
2939         } else
2940                 base = &rtl8192cu_txagc[chain];
2941
2942         memset(power, 0, URTWN_RIDX_COUNT * sizeof(power[0]));
2943         if (sc->regulatory == 0) {
2944                 for (ridx = 0; ridx <= 3; ridx++)
2945                         power[ridx] = base->pwr[0][ridx];
2946         }
2947         for (ridx = 4; ridx < URTWN_RIDX_COUNT; ridx++) {
2948                 if (sc->regulatory == 3) {
2949                         power[ridx] = base->pwr[0][ridx];
2950                         /* Apply vendor limits. */
2951                         if (extc != NULL)
2952                                 max = rom->ht40_max_pwr[group];
2953                         else
2954                                 max = rom->ht20_max_pwr[group];
2955                         max = (max >> (chain * 4)) & 0xf;
2956                         if (power[ridx] > max)
2957                                 power[ridx] = max;
2958                 } else if (sc->regulatory == 1) {
2959                         if (extc == NULL)
2960                                 power[ridx] = base->pwr[group][ridx];
2961                 } else if (sc->regulatory != 2)
2962                         power[ridx] = base->pwr[0][ridx];
2963         }
2964
2965         /* Compute per-CCK rate Tx power. */
2966         cckpow = rom->cck_tx_pwr[chain][group];
2967         for (ridx = 0; ridx <= 3; ridx++) {
2968                 power[ridx] += cckpow;
2969                 if (power[ridx] > R92C_MAX_TX_PWR)
2970                         power[ridx] = R92C_MAX_TX_PWR;
2971         }
2972
2973         htpow = rom->ht40_1s_tx_pwr[chain][group];
2974         if (sc->ntxchains > 1) {
2975                 /* Apply reduction for 2 spatial streams. */
2976                 diff = rom->ht40_2s_tx_pwr_diff[group];
2977                 diff = (diff >> (chain * 4)) & 0xf;
2978                 htpow = (htpow > diff) ? htpow - diff : 0;
2979         }
2980
2981         /* Compute per-OFDM rate Tx power. */
2982         diff = rom->ofdm_tx_pwr_diff[group];
2983         diff = (diff >> (chain * 4)) & 0xf;
2984         ofdmpow = htpow + diff; /* HT->OFDM correction. */
2985         for (ridx = 4; ridx <= 11; ridx++) {
2986                 power[ridx] += ofdmpow;
2987                 if (power[ridx] > R92C_MAX_TX_PWR)
2988                         power[ridx] = R92C_MAX_TX_PWR;
2989         }
2990
2991         /* Compute per-MCS Tx power. */
2992         if (extc == NULL) {
2993                 diff = rom->ht20_tx_pwr_diff[group];
2994                 diff = (diff >> (chain * 4)) & 0xf;
2995                 htpow += diff;  /* HT40->HT20 correction. */
2996         }
2997         for (ridx = 12; ridx <= 27; ridx++) {
2998                 power[ridx] += htpow;
2999                 if (power[ridx] > R92C_MAX_TX_PWR)
3000                         power[ridx] = R92C_MAX_TX_PWR;
3001         }
3002 #ifdef URTWN_DEBUG
3003         if (urtwn_debug >= 4) {
3004                 /* Dump per-rate Tx power values. */
3005                 printf("Tx power for chain %d:\n", chain);
3006                 for (ridx = 0; ridx < URTWN_RIDX_COUNT; ridx++)
3007                         printf("Rate %d = %u\n", ridx, power[ridx]);
3008         }
3009 #endif
3010 }
3011
3012 void
3013 urtwn_r88e_get_txpower(struct urtwn_softc *sc, int chain,
3014     struct ieee80211_channel *c, struct ieee80211_channel *extc,
3015     uint16_t power[URTWN_RIDX_COUNT])
3016 {
3017         struct ieee80211com *ic = sc->sc_ifp->if_l2com;
3018         uint16_t cckpow, ofdmpow, bw20pow, htpow;
3019         const struct urtwn_r88e_txpwr *base;
3020         int ridx, chan, group;
3021
3022         /* Determine channel group. */
3023         chan = ieee80211_chan2ieee(ic, c);      /* XXX center freq! */
3024         if (chan <= 2)
3025                 group = 0;
3026         else if (chan <= 5)
3027                 group = 1;
3028         else if (chan <= 8)
3029                 group = 2;
3030         else if (chan <= 11)
3031                 group = 3;
3032         else if (chan <= 13)
3033                 group = 4;
3034         else
3035                 group = 5;
3036
3037         /* Get original Tx power based on board type and RF chain. */
3038         base = &rtl8188eu_txagc[chain];
3039
3040         memset(power, 0, URTWN_RIDX_COUNT * sizeof(power[0]));
3041         if (sc->regulatory == 0) {
3042                 for (ridx = 0; ridx <= 3; ridx++)
3043                         power[ridx] = base->pwr[0][ridx];
3044         }
3045         for (ridx = 4; ridx < URTWN_RIDX_COUNT; ridx++) {
3046                 if (sc->regulatory == 3)
3047                         power[ridx] = base->pwr[0][ridx];
3048                 else if (sc->regulatory == 1) {
3049                         if (extc == NULL)
3050                                 power[ridx] = base->pwr[group][ridx];
3051                 } else if (sc->regulatory != 2)
3052                         power[ridx] = base->pwr[0][ridx];
3053         }
3054
3055         /* Compute per-CCK rate Tx power. */
3056         cckpow = sc->cck_tx_pwr[group];
3057         for (ridx = 0; ridx <= 3; ridx++) {
3058                 power[ridx] += cckpow;
3059                 if (power[ridx] > R92C_MAX_TX_PWR)
3060                         power[ridx] = R92C_MAX_TX_PWR;
3061         }
3062
3063         htpow = sc->ht40_tx_pwr[group];
3064
3065         /* Compute per-OFDM rate Tx power. */
3066         ofdmpow = htpow + sc->ofdm_tx_pwr_diff;
3067         for (ridx = 4; ridx <= 11; ridx++) {
3068                 power[ridx] += ofdmpow;
3069                 if (power[ridx] > R92C_MAX_TX_PWR)
3070                         power[ridx] = R92C_MAX_TX_PWR;
3071         }
3072
3073         bw20pow = htpow + sc->bw20_tx_pwr_diff;
3074         for (ridx = 12; ridx <= 27; ridx++) {
3075                 power[ridx] += bw20pow;
3076                 if (power[ridx] > R92C_MAX_TX_PWR)
3077                         power[ridx] = R92C_MAX_TX_PWR;
3078         }
3079 }
3080
3081 void
3082 urtwn_set_txpower(struct urtwn_softc *sc, struct ieee80211_channel *c,
3083     struct ieee80211_channel *extc)
3084 {
3085         uint16_t power[URTWN_RIDX_COUNT];
3086         int i;
3087
3088         for (i = 0; i < sc->ntxchains; i++) {
3089                 /* Compute per-rate Tx power values. */
3090                 if (sc->chip & URTWN_CHIP_88E)
3091                         urtwn_r88e_get_txpower(sc, i, c, extc, power);
3092                 else
3093                         urtwn_get_txpower(sc, i, c, extc, power);
3094                 /* Write per-rate Tx power values to hardware. */
3095                 urtwn_write_txpower(sc, i, power);
3096         }
3097 }
3098
3099 static void
3100 urtwn_scan_start(struct ieee80211com *ic)
3101 {
3102         /* XXX do nothing?  */
3103 }
3104
3105 static void
3106 urtwn_scan_end(struct ieee80211com *ic)
3107 {
3108         /* XXX do nothing?  */
3109 }
3110
3111 static void
3112 urtwn_set_channel(struct ieee80211com *ic)
3113 {
3114         struct urtwn_softc *sc = ic->ic_softc;
3115
3116         URTWN_LOCK(sc);
3117         urtwn_set_chan(sc, ic->ic_curchan, NULL);
3118         URTWN_UNLOCK(sc);
3119 }
3120
3121 static void
3122 urtwn_update_mcast(struct ieee80211com *ic)
3123 {
3124         /* XXX do nothing?  */
3125 }
3126
3127 static void
3128 urtwn_set_chan(struct urtwn_softc *sc, struct ieee80211_channel *c,
3129     struct ieee80211_channel *extc)
3130 {
3131         struct ieee80211com *ic = sc->sc_ifp->if_l2com;
3132         uint32_t reg;
3133         u_int chan;
3134         int i;
3135
3136         chan = ieee80211_chan2ieee(ic, c);      /* XXX center freq! */
3137         if (chan == 0 || chan == IEEE80211_CHAN_ANY) {
3138                 device_printf(sc->sc_dev,
3139                     "%s: invalid channel %x\n", __func__, chan);
3140                 return;
3141         }
3142
3143         /* Set Tx power for this new channel. */
3144         urtwn_set_txpower(sc, c, extc);
3145
3146         for (i = 0; i < sc->nrxchains; i++) {
3147                 urtwn_rf_write(sc, i, R92C_RF_CHNLBW,
3148                     RW(sc->rf_chnlbw[i], R92C_RF_CHNLBW_CHNL, chan));
3149         }
3150 #ifndef IEEE80211_NO_HT
3151         if (extc != NULL) {
3152                 /* Is secondary channel below or above primary? */
3153                 int prichlo = c->ic_freq < extc->ic_freq;
3154
3155                 urtwn_write_1(sc, R92C_BWOPMODE,
3156                     urtwn_read_1(sc, R92C_BWOPMODE) & ~R92C_BWOPMODE_20MHZ);
3157
3158                 reg = urtwn_read_1(sc, R92C_RRSR + 2);
3159                 reg = (reg & ~0x6f) | (prichlo ? 1 : 2) << 5;
3160                 urtwn_write_1(sc, R92C_RRSR + 2, reg);
3161
3162                 urtwn_bb_write(sc, R92C_FPGA0_RFMOD,
3163                     urtwn_bb_read(sc, R92C_FPGA0_RFMOD) | R92C_RFMOD_40MHZ);
3164                 urtwn_bb_write(sc, R92C_FPGA1_RFMOD,
3165                     urtwn_bb_read(sc, R92C_FPGA1_RFMOD) | R92C_RFMOD_40MHZ);
3166
3167                 /* Set CCK side band. */
3168                 reg = urtwn_bb_read(sc, R92C_CCK0_SYSTEM);
3169                 reg = (reg & ~0x00000010) | (prichlo ? 0 : 1) << 4;
3170                 urtwn_bb_write(sc, R92C_CCK0_SYSTEM, reg);
3171
3172                 reg = urtwn_bb_read(sc, R92C_OFDM1_LSTF);
3173                 reg = (reg & ~0x00000c00) | (prichlo ? 1 : 2) << 10;
3174                 urtwn_bb_write(sc, R92C_OFDM1_LSTF, reg);
3175
3176                 urtwn_bb_write(sc, R92C_FPGA0_ANAPARAM2,
3177                     urtwn_bb_read(sc, R92C_FPGA0_ANAPARAM2) &
3178                     ~R92C_FPGA0_ANAPARAM2_CBW20);
3179
3180                 reg = urtwn_bb_read(sc, 0x818);
3181                 reg = (reg & ~0x0c000000) | (prichlo ? 2 : 1) << 26;
3182                 urtwn_bb_write(sc, 0x818, reg);
3183
3184                 /* Select 40MHz bandwidth. */
3185                 urtwn_rf_write(sc, 0, R92C_RF_CHNLBW,
3186                     (sc->rf_chnlbw[0] & ~0xfff) | chan);
3187         } else
3188 #endif
3189         {
3190                 urtwn_write_1(sc, R92C_BWOPMODE,
3191                     urtwn_read_1(sc, R92C_BWOPMODE) | R92C_BWOPMODE_20MHZ);
3192
3193                 urtwn_bb_write(sc, R92C_FPGA0_RFMOD,
3194                     urtwn_bb_read(sc, R92C_FPGA0_RFMOD) & ~R92C_RFMOD_40MHZ);
3195                 urtwn_bb_write(sc, R92C_FPGA1_RFMOD,
3196                     urtwn_bb_read(sc, R92C_FPGA1_RFMOD) & ~R92C_RFMOD_40MHZ);
3197
3198                 if (!(sc->chip & URTWN_CHIP_88E)) {
3199                         urtwn_bb_write(sc, R92C_FPGA0_ANAPARAM2,
3200                             urtwn_bb_read(sc, R92C_FPGA0_ANAPARAM2) |
3201                             R92C_FPGA0_ANAPARAM2_CBW20);
3202                 }
3203
3204                 /* Select 20MHz bandwidth. */
3205                 urtwn_rf_write(sc, 0, R92C_RF_CHNLBW,
3206                     (sc->rf_chnlbw[0] & ~0xfff) | chan | 
3207                     ((sc->chip & URTWN_CHIP_88E) ? R88E_RF_CHNLBW_BW20 :
3208                     R92C_RF_CHNLBW_BW20));
3209         }
3210 }
3211
3212 static void
3213 urtwn_iq_calib(struct urtwn_softc *sc)
3214 {
3215         /* TODO */
3216 }
3217
3218 static void
3219 urtwn_lc_calib(struct urtwn_softc *sc)
3220 {
3221         uint32_t rf_ac[2];
3222         uint8_t txmode;
3223         int i;
3224
3225         txmode = urtwn_read_1(sc, R92C_OFDM1_LSTF + 3);
3226         if ((txmode & 0x70) != 0) {
3227                 /* Disable all continuous Tx. */
3228                 urtwn_write_1(sc, R92C_OFDM1_LSTF + 3, txmode & ~0x70);
3229
3230                 /* Set RF mode to standby mode. */
3231                 for (i = 0; i < sc->nrxchains; i++) {
3232                         rf_ac[i] = urtwn_rf_read(sc, i, R92C_RF_AC);
3233                         urtwn_rf_write(sc, i, R92C_RF_AC,
3234                             RW(rf_ac[i], R92C_RF_AC_MODE,
3235                                 R92C_RF_AC_MODE_STANDBY));
3236                 }
3237         } else {
3238                 /* Block all Tx queues. */
3239                 urtwn_write_1(sc, R92C_TXPAUSE, 0xff);
3240         }
3241         /* Start calibration. */
3242         urtwn_rf_write(sc, 0, R92C_RF_CHNLBW,
3243             urtwn_rf_read(sc, 0, R92C_RF_CHNLBW) | R92C_RF_CHNLBW_LCSTART);
3244
3245         /* Give calibration the time to complete. */
3246         usb_pause_mtx(&sc->sc_lock, hz / 10);           /* 100ms */
3247
3248         /* Restore configuration. */
3249         if ((txmode & 0x70) != 0) {
3250                 /* Restore Tx mode. */
3251                 urtwn_write_1(sc, R92C_OFDM1_LSTF + 3, txmode);
3252                 /* Restore RF mode. */
3253                 for (i = 0; i < sc->nrxchains; i++)
3254                         urtwn_rf_write(sc, i, R92C_RF_AC, rf_ac[i]);
3255         } else {
3256                 /* Unblock all Tx queues. */
3257                 urtwn_write_1(sc, R92C_TXPAUSE, 0x00);
3258         }
3259 }
3260
3261 static void
3262 urtwn_init_locked(void *arg)
3263 {
3264         struct urtwn_softc *sc = arg;
3265         struct ifnet *ifp = sc->sc_ifp;
3266         uint32_t reg;
3267         int error;
3268
3269         URTWN_ASSERT_LOCKED(sc);
3270
3271         if (ifp->if_flags & IFF_RUNNING)
3272                 urtwn_stop_locked(ifp);
3273
3274         /* Init firmware commands ring. */
3275         sc->fwcur = 0;
3276
3277         /* Allocate Tx/Rx buffers. */
3278         error = urtwn_alloc_rx_list(sc);
3279         if (error != 0)
3280                 goto fail;
3281
3282         error = urtwn_alloc_tx_list(sc);
3283         if (error != 0)
3284                 goto fail;
3285
3286         /* Power on adapter. */
3287         error = urtwn_power_on(sc);
3288         if (error != 0)
3289                 goto fail;
3290
3291         /* Initialize DMA. */
3292         error = urtwn_dma_init(sc);
3293         if (error != 0)
3294                 goto fail;
3295
3296         /* Set info size in Rx descriptors (in 64-bit words). */
3297         urtwn_write_1(sc, R92C_RX_DRVINFO_SZ, 4);
3298
3299         /* Init interrupts. */
3300         if (sc->chip & URTWN_CHIP_88E) {
3301                 urtwn_write_4(sc, R88E_HISR, 0xffffffff);
3302                 urtwn_write_4(sc, R88E_HIMR, R88E_HIMR_CPWM | R88E_HIMR_CPWM2 |
3303                     R88E_HIMR_TBDER | R88E_HIMR_PSTIMEOUT);
3304                 urtwn_write_4(sc, R88E_HIMRE, R88E_HIMRE_RXFOVW |
3305                     R88E_HIMRE_TXFOVW | R88E_HIMRE_RXERR | R88E_HIMRE_TXERR);
3306                 urtwn_write_1(sc, R92C_USB_SPECIAL_OPTION,
3307                     urtwn_read_1(sc, R92C_USB_SPECIAL_OPTION) |
3308                     R92C_USB_SPECIAL_OPTION_INT_BULK_SEL);
3309         } else {
3310                 urtwn_write_4(sc, R92C_HISR, 0xffffffff);
3311                 urtwn_write_4(sc, R92C_HIMR, 0xffffffff);
3312         }
3313
3314         /* Set MAC address. */
3315         urtwn_write_region_1(sc, R92C_MACID, IF_LLADDR(ifp),
3316             IEEE80211_ADDR_LEN);
3317
3318         /* Set initial network type. */
3319         reg = urtwn_read_4(sc, R92C_CR);
3320         reg = RW(reg, R92C_CR_NETTYPE, R92C_CR_NETTYPE_INFRA);
3321         urtwn_write_4(sc, R92C_CR, reg);
3322
3323         urtwn_rxfilter_init(sc);
3324
3325         reg = urtwn_read_4(sc, R92C_RRSR);
3326         reg = RW(reg, R92C_RRSR_RATE_BITMAP, R92C_RRSR_RATE_CCK_ONLY_1M);
3327         urtwn_write_4(sc, R92C_RRSR, reg);
3328
3329         /* Set short/long retry limits. */
3330         urtwn_write_2(sc, R92C_RL,
3331             SM(R92C_RL_SRL, 0x30) | SM(R92C_RL_LRL, 0x30));
3332
3333         /* Initialize EDCA parameters. */
3334         urtwn_edca_init(sc);
3335
3336         /* Setup rate fallback. */
3337         if (!(sc->chip & URTWN_CHIP_88E)) {
3338                 urtwn_write_4(sc, R92C_DARFRC + 0, 0x00000000);
3339                 urtwn_write_4(sc, R92C_DARFRC + 4, 0x10080404);
3340                 urtwn_write_4(sc, R92C_RARFRC + 0, 0x04030201);
3341                 urtwn_write_4(sc, R92C_RARFRC + 4, 0x08070605);
3342         }
3343
3344         urtwn_write_1(sc, R92C_FWHW_TXQ_CTRL,
3345             urtwn_read_1(sc, R92C_FWHW_TXQ_CTRL) |
3346             R92C_FWHW_TXQ_CTRL_AMPDU_RTY_NEW);
3347         /* Set ACK timeout. */
3348         urtwn_write_1(sc, R92C_ACKTO, 0x40);
3349
3350         /* Setup USB aggregation. */
3351         reg = urtwn_read_4(sc, R92C_TDECTRL);
3352         reg = RW(reg, R92C_TDECTRL_BLK_DESC_NUM, 6);
3353         urtwn_write_4(sc, R92C_TDECTRL, reg);
3354         urtwn_write_1(sc, R92C_TRXDMA_CTRL,
3355             urtwn_read_1(sc, R92C_TRXDMA_CTRL) |
3356             R92C_TRXDMA_CTRL_RXDMA_AGG_EN);
3357         urtwn_write_1(sc, R92C_USB_SPECIAL_OPTION,
3358             urtwn_read_1(sc, R92C_USB_SPECIAL_OPTION) |
3359             R92C_USB_SPECIAL_OPTION_AGG_EN);
3360         urtwn_write_1(sc, R92C_RXDMA_AGG_PG_TH, 48);
3361         if (sc->chip & URTWN_CHIP_88E)
3362                 urtwn_write_1(sc, R92C_RXDMA_AGG_PG_TH + 1, 4);
3363         else
3364                 urtwn_write_1(sc, R92C_USB_DMA_AGG_TO, 4);
3365         urtwn_write_1(sc, R92C_USB_AGG_TH, 8);
3366         urtwn_write_1(sc, R92C_USB_AGG_TO, 6);
3367
3368         /* Initialize beacon parameters. */
3369         urtwn_write_2(sc, R92C_BCN_CTRL, 0x1010);
3370         urtwn_write_2(sc, R92C_TBTT_PROHIBIT, 0x6404);
3371         urtwn_write_1(sc, R92C_DRVERLYINT, 0x05);
3372         urtwn_write_1(sc, R92C_BCNDMATIM, 0x02);
3373         urtwn_write_2(sc, R92C_BCNTCFG, 0x660f);
3374
3375         if (!(sc->chip & URTWN_CHIP_88E)) {
3376                 /* Setup AMPDU aggregation. */
3377                 urtwn_write_4(sc, R92C_AGGLEN_LMT, 0x99997631); /* MCS7~0 */
3378                 urtwn_write_1(sc, R92C_AGGR_BREAK_TIME, 0x16);
3379                 urtwn_write_2(sc, R92C_MAX_AGGR_NUM, 0x0708);
3380
3381                 urtwn_write_1(sc, R92C_BCN_MAX_ERR, 0xff);
3382         }
3383
3384         /* Load 8051 microcode. */
3385         error = urtwn_load_firmware(sc);
3386         if (error != 0)
3387                 goto fail;
3388
3389         /* Initialize MAC/BB/RF blocks. */
3390         urtwn_mac_init(sc);
3391         urtwn_bb_init(sc);
3392         urtwn_rf_init(sc);
3393
3394         if (sc->chip & URTWN_CHIP_88E) {
3395                 urtwn_write_2(sc, R92C_CR,
3396                     urtwn_read_2(sc, R92C_CR) | R92C_CR_MACTXEN |
3397                     R92C_CR_MACRXEN);
3398         }
3399
3400         /* Turn CCK and OFDM blocks on. */
3401         reg = urtwn_bb_read(sc, R92C_FPGA0_RFMOD);
3402         reg |= R92C_RFMOD_CCK_EN;
3403         urtwn_bb_write(sc, R92C_FPGA0_RFMOD, reg);
3404         reg = urtwn_bb_read(sc, R92C_FPGA0_RFMOD);
3405         reg |= R92C_RFMOD_OFDM_EN;
3406         urtwn_bb_write(sc, R92C_FPGA0_RFMOD, reg);
3407
3408         /* Clear per-station keys table. */
3409         urtwn_cam_init(sc);
3410
3411         /* Enable hardware sequence numbering. */
3412         urtwn_write_1(sc, R92C_HWSEQ_CTRL, 0xff);
3413
3414         /* Perform LO and IQ calibrations. */
3415         urtwn_iq_calib(sc);
3416         /* Perform LC calibration. */
3417         urtwn_lc_calib(sc);
3418
3419         /* Fix USB interference issue. */
3420         if (!(sc->chip & URTWN_CHIP_88E)) {
3421                 urtwn_write_1(sc, 0xfe40, 0xe0);
3422                 urtwn_write_1(sc, 0xfe41, 0x8d);
3423                 urtwn_write_1(sc, 0xfe42, 0x80);
3424
3425                 urtwn_pa_bias_init(sc);
3426         }
3427
3428         /* Initialize GPIO setting. */
3429         urtwn_write_1(sc, R92C_GPIO_MUXCFG,
3430             urtwn_read_1(sc, R92C_GPIO_MUXCFG) & ~R92C_GPIO_MUXCFG_ENBT);
3431
3432         /* Fix for lower temperature. */
3433         if (!(sc->chip & URTWN_CHIP_88E))
3434                 urtwn_write_1(sc, 0x15, 0xe9);
3435
3436         usbd_transfer_start(sc->sc_xfer[URTWN_BULK_RX]);
3437
3438         ifq_clr_oactive(&ifp->if_snd);
3439         ifp->if_flags |= IFF_RUNNING;
3440
3441         callout_reset(&sc->sc_watchdog_ch, hz, urtwn_watchdog, sc);
3442 fail:
3443         return;
3444 }
3445
3446 static void
3447 urtwn_init(void *arg)
3448 {
3449         struct urtwn_softc *sc = arg;
3450
3451         URTWN_LOCK(sc);
3452         urtwn_init_locked(arg);
3453         URTWN_UNLOCK(sc);
3454 }
3455
3456 static void
3457 urtwn_stop_locked(struct ifnet *ifp)
3458 {
3459         struct urtwn_softc *sc = ifp->if_softc;
3460
3461         URTWN_ASSERT_LOCKED(sc);
3462
3463         ifp->if_flags &= ~IFF_RUNNING;
3464         ifq_clr_oactive(&ifp->if_snd);
3465
3466         callout_stop(&sc->sc_watchdog_ch);
3467         urtwn_abort_xfers(sc);
3468 }
3469
3470 static void
3471 urtwn_stop(struct ifnet *ifp)
3472 {
3473         struct urtwn_softc *sc = ifp->if_softc;
3474
3475         URTWN_LOCK(sc);
3476         urtwn_stop_locked(ifp);
3477         URTWN_UNLOCK(sc);
3478 }
3479
3480 static void
3481 urtwn_abort_xfers(struct urtwn_softc *sc)
3482 {
3483         int i;
3484
3485         URTWN_ASSERT_LOCKED(sc);
3486
3487         /* abort any pending transfers */
3488         for (i = 0; i < URTWN_N_TRANSFER; i++)
3489                 usbd_transfer_stop(sc->sc_xfer[i]);
3490 }
3491
3492 static int
3493 urtwn_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
3494     const struct ieee80211_bpf_params *params)
3495 {
3496         struct ieee80211com *ic = ni->ni_ic;
3497         struct ifnet *ifp = ic->ic_ifp;
3498         struct urtwn_softc *sc = ic->ic_softc;
3499         struct urtwn_data *bf;
3500
3501         /* prevent management frames from being sent if we're not ready */
3502         if (!(ifp->if_flags & IFF_RUNNING)) {
3503                 m_freem(m);
3504                 ieee80211_free_node(ni);
3505                 return (ENETDOWN);
3506         }
3507         URTWN_LOCK(sc);
3508         bf = urtwn_getbuf(sc);
3509         if (bf == NULL) {
3510                 ieee80211_free_node(ni);
3511                 m_freem(m);
3512                 URTWN_UNLOCK(sc);
3513                 return (ENOBUFS);
3514         }
3515
3516         IFNET_STAT_INC(ifp, opackets, 1);
3517         if (urtwn_tx_start(sc, ni, m, bf) != 0) {
3518                 ieee80211_free_node(ni);
3519                 IFNET_STAT_INC(ifp, oerrors, 1);
3520                 STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, bf, next);
3521                 URTWN_UNLOCK(sc);
3522                 return (EIO);
3523         }
3524         URTWN_UNLOCK(sc);
3525
3526         sc->sc_txtimer = 5;
3527         return (0);
3528 }
3529
3530 static void
3531 urtwn_ms_delay(struct urtwn_softc *sc)
3532 {
3533         usb_pause_ls(&sc->sc_lock, &wlan_global_serializer, hz / 100);
3534 }
3535
3536 static device_method_t urtwn_methods[] = {
3537         /* Device interface */
3538         DEVMETHOD(device_probe,         urtwn_match),
3539         DEVMETHOD(device_attach,        urtwn_attach),
3540         DEVMETHOD(device_detach,        urtwn_detach),
3541
3542         DEVMETHOD_END
3543 };
3544
3545 static driver_t urtwn_driver = {
3546         "urtwn",
3547         urtwn_methods,
3548         sizeof(struct urtwn_softc)
3549 };
3550
3551 static devclass_t urtwn_devclass;
3552
3553 DRIVER_MODULE(urtwn, uhub, urtwn_driver, urtwn_devclass, NULL, NULL);
3554 MODULE_DEPEND(urtwn, usb, 1, 1, 1);
3555 MODULE_DEPEND(urtwn, wlan, 1, 1, 1);
3556 MODULE_DEPEND(urtwn, firmware, 1, 1, 1);
3557 MODULE_VERSION(urtwn, 1);