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