if_rum.c revision 292176
1/*	$FreeBSD: head/sys/dev/usb/wlan/if_rum.c 292176 2015-12-13 22:08:27Z avos $	*/
2
3/*-
4 * Copyright (c) 2005-2007 Damien Bergamini <damien.bergamini@free.fr>
5 * Copyright (c) 2006 Niall O'Higgins <niallo@openbsd.org>
6 * Copyright (c) 2007-2008 Hans Petter Selasky <hselasky@FreeBSD.org>
7 * Copyright (c) 2015 Andriy Voskoboinyk <avos@FreeBSD.org>
8 *
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 */
21
22#include <sys/cdefs.h>
23__FBSDID("$FreeBSD: head/sys/dev/usb/wlan/if_rum.c 292176 2015-12-13 22:08:27Z avos $");
24
25/*-
26 * Ralink Technology RT2501USB/RT2601USB chipset driver
27 * http://www.ralinktech.com.tw/
28 */
29
30#include <sys/param.h>
31#include <sys/sockio.h>
32#include <sys/sysctl.h>
33#include <sys/lock.h>
34#include <sys/mutex.h>
35#include <sys/mbuf.h>
36#include <sys/kernel.h>
37#include <sys/socket.h>
38#include <sys/systm.h>
39#include <sys/malloc.h>
40#include <sys/module.h>
41#include <sys/bus.h>
42#include <sys/endian.h>
43#include <sys/kdb.h>
44
45#include <machine/bus.h>
46#include <machine/resource.h>
47#include <sys/rman.h>
48
49#include <net/bpf.h>
50#include <net/if.h>
51#include <net/if_var.h>
52#include <net/if_arp.h>
53#include <net/ethernet.h>
54#include <net/if_dl.h>
55#include <net/if_media.h>
56#include <net/if_types.h>
57
58#ifdef INET
59#include <netinet/in.h>
60#include <netinet/in_systm.h>
61#include <netinet/in_var.h>
62#include <netinet/if_ether.h>
63#include <netinet/ip.h>
64#endif
65
66#include <net80211/ieee80211_var.h>
67#include <net80211/ieee80211_regdomain.h>
68#include <net80211/ieee80211_radiotap.h>
69#include <net80211/ieee80211_ratectl.h>
70
71#include <dev/usb/usb.h>
72#include <dev/usb/usbdi.h>
73#include "usbdevs.h"
74
75#define	USB_DEBUG_VAR rum_debug
76#include <dev/usb/usb_debug.h>
77
78#include <dev/usb/wlan/if_rumreg.h>
79#include <dev/usb/wlan/if_rumvar.h>
80#include <dev/usb/wlan/if_rumfw.h>
81
82#ifdef USB_DEBUG
83static int rum_debug = 0;
84
85static SYSCTL_NODE(_hw_usb, OID_AUTO, rum, CTLFLAG_RW, 0, "USB rum");
86SYSCTL_INT(_hw_usb_rum, OID_AUTO, debug, CTLFLAG_RWTUN, &rum_debug, 0,
87    "Debug level");
88#endif
89
90static const STRUCT_USB_HOST_ID rum_devs[] = {
91#define	RUM_DEV(v,p)  { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
92    RUM_DEV(ABOCOM, HWU54DM),
93    RUM_DEV(ABOCOM, RT2573_2),
94    RUM_DEV(ABOCOM, RT2573_3),
95    RUM_DEV(ABOCOM, RT2573_4),
96    RUM_DEV(ABOCOM, WUG2700),
97    RUM_DEV(AMIT, CGWLUSB2GO),
98    RUM_DEV(ASUS, RT2573_1),
99    RUM_DEV(ASUS, RT2573_2),
100    RUM_DEV(BELKIN, F5D7050A),
101    RUM_DEV(BELKIN, F5D9050V3),
102    RUM_DEV(CISCOLINKSYS, WUSB54GC),
103    RUM_DEV(CISCOLINKSYS, WUSB54GR),
104    RUM_DEV(CONCEPTRONIC2, C54RU2),
105    RUM_DEV(COREGA, CGWLUSB2GL),
106    RUM_DEV(COREGA, CGWLUSB2GPX),
107    RUM_DEV(DICKSMITH, CWD854F),
108    RUM_DEV(DICKSMITH, RT2573),
109    RUM_DEV(EDIMAX, EW7318USG),
110    RUM_DEV(DLINK2, DWLG122C1),
111    RUM_DEV(DLINK2, WUA1340),
112    RUM_DEV(DLINK2, DWA111),
113    RUM_DEV(DLINK2, DWA110),
114    RUM_DEV(GIGABYTE, GNWB01GS),
115    RUM_DEV(GIGABYTE, GNWI05GS),
116    RUM_DEV(GIGASET, RT2573),
117    RUM_DEV(GOODWAY, RT2573),
118    RUM_DEV(GUILLEMOT, HWGUSB254LB),
119    RUM_DEV(GUILLEMOT, HWGUSB254V2AP),
120    RUM_DEV(HUAWEI3COM, WUB320G),
121    RUM_DEV(MELCO, G54HP),
122    RUM_DEV(MELCO, SG54HP),
123    RUM_DEV(MELCO, SG54HG),
124    RUM_DEV(MELCO, WLIUCG),
125    RUM_DEV(MELCO, WLRUCG),
126    RUM_DEV(MELCO, WLRUCGAOSS),
127    RUM_DEV(MSI, RT2573_1),
128    RUM_DEV(MSI, RT2573_2),
129    RUM_DEV(MSI, RT2573_3),
130    RUM_DEV(MSI, RT2573_4),
131    RUM_DEV(NOVATECH, RT2573),
132    RUM_DEV(PLANEX2, GWUS54HP),
133    RUM_DEV(PLANEX2, GWUS54MINI2),
134    RUM_DEV(PLANEX2, GWUSMM),
135    RUM_DEV(QCOM, RT2573),
136    RUM_DEV(QCOM, RT2573_2),
137    RUM_DEV(QCOM, RT2573_3),
138    RUM_DEV(RALINK, RT2573),
139    RUM_DEV(RALINK, RT2573_2),
140    RUM_DEV(RALINK, RT2671),
141    RUM_DEV(SITECOMEU, WL113R2),
142    RUM_DEV(SITECOMEU, WL172),
143    RUM_DEV(SPARKLAN, RT2573),
144    RUM_DEV(SURECOM, RT2573),
145#undef RUM_DEV
146};
147
148static device_probe_t rum_match;
149static device_attach_t rum_attach;
150static device_detach_t rum_detach;
151
152static usb_callback_t rum_bulk_read_callback;
153static usb_callback_t rum_bulk_write_callback;
154
155static usb_error_t	rum_do_request(struct rum_softc *sc,
156			    struct usb_device_request *req, void *data);
157static struct ieee80211vap *rum_vap_create(struct ieee80211com *,
158			    const char [IFNAMSIZ], int, enum ieee80211_opmode,
159			    int, const uint8_t [IEEE80211_ADDR_LEN],
160			    const uint8_t [IEEE80211_ADDR_LEN]);
161static void		rum_vap_delete(struct ieee80211vap *);
162static void		rum_cmdq_cb(void *, int);
163static int		rum_cmd_sleepable(struct rum_softc *, const void *,
164			    size_t, uint8_t, CMD_FUNC_PROTO);
165static void		rum_tx_free(struct rum_tx_data *, int);
166static void		rum_setup_tx_list(struct rum_softc *);
167static void		rum_unsetup_tx_list(struct rum_softc *);
168static int		rum_newstate(struct ieee80211vap *,
169			    enum ieee80211_state, int);
170static uint8_t		rum_crypto_mode(struct rum_softc *, u_int, int);
171static void		rum_setup_tx_desc(struct rum_softc *,
172			    struct rum_tx_desc *, struct ieee80211_key *,
173			    uint32_t, uint8_t, uint8_t, int, int, int);
174static uint32_t		rum_tx_crypto_flags(struct rum_softc *,
175			    struct ieee80211_node *,
176			    const struct ieee80211_key *);
177static int		rum_tx_mgt(struct rum_softc *, struct mbuf *,
178			    struct ieee80211_node *);
179static int		rum_tx_raw(struct rum_softc *, struct mbuf *,
180			    struct ieee80211_node *,
181			    const struct ieee80211_bpf_params *);
182static int		rum_tx_data(struct rum_softc *, struct mbuf *,
183			    struct ieee80211_node *);
184static int		rum_transmit(struct ieee80211com *, struct mbuf *);
185static void		rum_start(struct rum_softc *);
186static void		rum_parent(struct ieee80211com *);
187static void		rum_eeprom_read(struct rum_softc *, uint16_t, void *,
188			    int);
189static uint32_t		rum_read(struct rum_softc *, uint16_t);
190static void		rum_read_multi(struct rum_softc *, uint16_t, void *,
191			    int);
192static usb_error_t	rum_write(struct rum_softc *, uint16_t, uint32_t);
193static usb_error_t	rum_write_multi(struct rum_softc *, uint16_t, void *,
194			    size_t);
195static usb_error_t	rum_setbits(struct rum_softc *, uint16_t, uint32_t);
196static usb_error_t	rum_clrbits(struct rum_softc *, uint16_t, uint32_t);
197static usb_error_t	rum_modbits(struct rum_softc *, uint16_t, uint32_t,
198			    uint32_t);
199static int		rum_bbp_busy(struct rum_softc *);
200static void		rum_bbp_write(struct rum_softc *, uint8_t, uint8_t);
201static uint8_t		rum_bbp_read(struct rum_softc *, uint8_t);
202static void		rum_rf_write(struct rum_softc *, uint8_t, uint32_t);
203static void		rum_select_antenna(struct rum_softc *);
204static void		rum_enable_mrr(struct rum_softc *);
205static void		rum_set_txpreamble(struct rum_softc *);
206static void		rum_set_basicrates(struct rum_softc *);
207static void		rum_select_band(struct rum_softc *,
208			    struct ieee80211_channel *);
209static void		rum_set_chan(struct rum_softc *,
210			    struct ieee80211_channel *);
211static void		rum_set_maxretry(struct rum_softc *,
212			    struct ieee80211vap *);
213static int		rum_enable_tsf_sync(struct rum_softc *);
214static void		rum_enable_tsf(struct rum_softc *);
215static void		rum_abort_tsf_sync(struct rum_softc *);
216static void		rum_get_tsf(struct rum_softc *, uint64_t *);
217static void		rum_update_slot_cb(struct rum_softc *,
218			    union sec_param *, uint8_t);
219static void		rum_update_slot(struct ieee80211com *);
220static int		rum_wme_update(struct ieee80211com *);
221static void		rum_set_bssid(struct rum_softc *, const uint8_t *);
222static void		rum_set_macaddr(struct rum_softc *, const uint8_t *);
223static void		rum_update_mcast(struct ieee80211com *);
224static void		rum_update_promisc(struct ieee80211com *);
225static void		rum_setpromisc(struct rum_softc *);
226static const char	*rum_get_rf(int);
227static void		rum_read_eeprom(struct rum_softc *);
228static int		rum_bbp_wakeup(struct rum_softc *);
229static int		rum_bbp_init(struct rum_softc *);
230static void		rum_clr_shkey_regs(struct rum_softc *);
231static int		rum_init(struct rum_softc *);
232static void		rum_stop(struct rum_softc *);
233static void		rum_load_microcode(struct rum_softc *, const uint8_t *,
234			    size_t);
235static int		rum_set_beacon(struct rum_softc *,
236			    struct ieee80211vap *);
237static int		rum_alloc_beacon(struct rum_softc *,
238			    struct ieee80211vap *);
239static void		rum_update_beacon_cb(struct rum_softc *,
240			    union sec_param *, uint8_t);
241static void		rum_update_beacon(struct ieee80211vap *, int);
242static int		rum_common_key_set(struct rum_softc *,
243			    struct ieee80211_key *, uint16_t);
244static void		rum_group_key_set_cb(struct rum_softc *,
245			    union sec_param *, uint8_t);
246static void		rum_group_key_del_cb(struct rum_softc *,
247			    union sec_param *, uint8_t);
248static void		rum_pair_key_set_cb(struct rum_softc *,
249			    union sec_param *, uint8_t);
250static void		rum_pair_key_del_cb(struct rum_softc *,
251			    union sec_param *, uint8_t);
252static int		rum_key_alloc(struct ieee80211vap *,
253			    struct ieee80211_key *, ieee80211_keyix *,
254			    ieee80211_keyix *);
255static int		rum_key_set(struct ieee80211vap *,
256			    const struct ieee80211_key *);
257static int		rum_key_delete(struct ieee80211vap *,
258			    const struct ieee80211_key *);
259static int		rum_raw_xmit(struct ieee80211_node *, struct mbuf *,
260			    const struct ieee80211_bpf_params *);
261static void		rum_scan_start(struct ieee80211com *);
262static void		rum_scan_end(struct ieee80211com *);
263static void		rum_set_channel(struct ieee80211com *);
264static int		rum_get_rssi(struct rum_softc *, uint8_t);
265static void		rum_ratectl_start(struct rum_softc *,
266			    struct ieee80211_node *);
267static void		rum_ratectl_timeout(void *);
268static void		rum_ratectl_task(void *, int);
269static int		rum_pause(struct rum_softc *, int);
270
271static const struct {
272	uint32_t	reg;
273	uint32_t	val;
274} rum_def_mac[] = {
275	{ RT2573_TXRX_CSR0,  0x025fb032 },
276	{ RT2573_TXRX_CSR1,  0x9eaa9eaf },
277	{ RT2573_TXRX_CSR2,  0x8a8b8c8d },
278	{ RT2573_TXRX_CSR3,  0x00858687 },
279	{ RT2573_TXRX_CSR7,  0x2e31353b },
280	{ RT2573_TXRX_CSR8,  0x2a2a2a2c },
281	{ RT2573_TXRX_CSR15, 0x0000000f },
282	{ RT2573_MAC_CSR6,   0x00000fff },
283	{ RT2573_MAC_CSR8,   0x016c030a },
284	{ RT2573_MAC_CSR10,  0x00000718 },
285	{ RT2573_MAC_CSR12,  0x00000004 },
286	{ RT2573_MAC_CSR13,  0x00007f00 },
287	{ RT2573_SEC_CSR2,   0x00000000 },
288	{ RT2573_SEC_CSR3,   0x00000000 },
289	{ RT2573_SEC_CSR4,   0x00000000 },
290	{ RT2573_PHY_CSR1,   0x000023b0 },
291	{ RT2573_PHY_CSR5,   0x00040a06 },
292	{ RT2573_PHY_CSR6,   0x00080606 },
293	{ RT2573_PHY_CSR7,   0x00000408 },
294	{ RT2573_AIFSN_CSR,  0x00002273 },
295	{ RT2573_CWMIN_CSR,  0x00002344 },
296	{ RT2573_CWMAX_CSR,  0x000034aa }
297};
298
299static const struct {
300	uint8_t	reg;
301	uint8_t	val;
302} rum_def_bbp[] = {
303	{   3, 0x80 },
304	{  15, 0x30 },
305	{  17, 0x20 },
306	{  21, 0xc8 },
307	{  22, 0x38 },
308	{  23, 0x06 },
309	{  24, 0xfe },
310	{  25, 0x0a },
311	{  26, 0x0d },
312	{  32, 0x0b },
313	{  34, 0x12 },
314	{  37, 0x07 },
315	{  39, 0xf8 },
316	{  41, 0x60 },
317	{  53, 0x10 },
318	{  54, 0x18 },
319	{  60, 0x10 },
320	{  61, 0x04 },
321	{  62, 0x04 },
322	{  75, 0xfe },
323	{  86, 0xfe },
324	{  88, 0xfe },
325	{  90, 0x0f },
326	{  99, 0x00 },
327	{ 102, 0x16 },
328	{ 107, 0x04 }
329};
330
331static const struct rfprog {
332	uint8_t		chan;
333	uint32_t	r1, r2, r3, r4;
334}  rum_rf5226[] = {
335	{   1, 0x00b03, 0x001e1, 0x1a014, 0x30282 },
336	{   2, 0x00b03, 0x001e1, 0x1a014, 0x30287 },
337	{   3, 0x00b03, 0x001e2, 0x1a014, 0x30282 },
338	{   4, 0x00b03, 0x001e2, 0x1a014, 0x30287 },
339	{   5, 0x00b03, 0x001e3, 0x1a014, 0x30282 },
340	{   6, 0x00b03, 0x001e3, 0x1a014, 0x30287 },
341	{   7, 0x00b03, 0x001e4, 0x1a014, 0x30282 },
342	{   8, 0x00b03, 0x001e4, 0x1a014, 0x30287 },
343	{   9, 0x00b03, 0x001e5, 0x1a014, 0x30282 },
344	{  10, 0x00b03, 0x001e5, 0x1a014, 0x30287 },
345	{  11, 0x00b03, 0x001e6, 0x1a014, 0x30282 },
346	{  12, 0x00b03, 0x001e6, 0x1a014, 0x30287 },
347	{  13, 0x00b03, 0x001e7, 0x1a014, 0x30282 },
348	{  14, 0x00b03, 0x001e8, 0x1a014, 0x30284 },
349
350	{  34, 0x00b03, 0x20266, 0x36014, 0x30282 },
351	{  38, 0x00b03, 0x20267, 0x36014, 0x30284 },
352	{  42, 0x00b03, 0x20268, 0x36014, 0x30286 },
353	{  46, 0x00b03, 0x20269, 0x36014, 0x30288 },
354
355	{  36, 0x00b03, 0x00266, 0x26014, 0x30288 },
356	{  40, 0x00b03, 0x00268, 0x26014, 0x30280 },
357	{  44, 0x00b03, 0x00269, 0x26014, 0x30282 },
358	{  48, 0x00b03, 0x0026a, 0x26014, 0x30284 },
359	{  52, 0x00b03, 0x0026b, 0x26014, 0x30286 },
360	{  56, 0x00b03, 0x0026c, 0x26014, 0x30288 },
361	{  60, 0x00b03, 0x0026e, 0x26014, 0x30280 },
362	{  64, 0x00b03, 0x0026f, 0x26014, 0x30282 },
363
364	{ 100, 0x00b03, 0x0028a, 0x2e014, 0x30280 },
365	{ 104, 0x00b03, 0x0028b, 0x2e014, 0x30282 },
366	{ 108, 0x00b03, 0x0028c, 0x2e014, 0x30284 },
367	{ 112, 0x00b03, 0x0028d, 0x2e014, 0x30286 },
368	{ 116, 0x00b03, 0x0028e, 0x2e014, 0x30288 },
369	{ 120, 0x00b03, 0x002a0, 0x2e014, 0x30280 },
370	{ 124, 0x00b03, 0x002a1, 0x2e014, 0x30282 },
371	{ 128, 0x00b03, 0x002a2, 0x2e014, 0x30284 },
372	{ 132, 0x00b03, 0x002a3, 0x2e014, 0x30286 },
373	{ 136, 0x00b03, 0x002a4, 0x2e014, 0x30288 },
374	{ 140, 0x00b03, 0x002a6, 0x2e014, 0x30280 },
375
376	{ 149, 0x00b03, 0x002a8, 0x2e014, 0x30287 },
377	{ 153, 0x00b03, 0x002a9, 0x2e014, 0x30289 },
378	{ 157, 0x00b03, 0x002ab, 0x2e014, 0x30281 },
379	{ 161, 0x00b03, 0x002ac, 0x2e014, 0x30283 },
380	{ 165, 0x00b03, 0x002ad, 0x2e014, 0x30285 }
381}, rum_rf5225[] = {
382	{   1, 0x00b33, 0x011e1, 0x1a014, 0x30282 },
383	{   2, 0x00b33, 0x011e1, 0x1a014, 0x30287 },
384	{   3, 0x00b33, 0x011e2, 0x1a014, 0x30282 },
385	{   4, 0x00b33, 0x011e2, 0x1a014, 0x30287 },
386	{   5, 0x00b33, 0x011e3, 0x1a014, 0x30282 },
387	{   6, 0x00b33, 0x011e3, 0x1a014, 0x30287 },
388	{   7, 0x00b33, 0x011e4, 0x1a014, 0x30282 },
389	{   8, 0x00b33, 0x011e4, 0x1a014, 0x30287 },
390	{   9, 0x00b33, 0x011e5, 0x1a014, 0x30282 },
391	{  10, 0x00b33, 0x011e5, 0x1a014, 0x30287 },
392	{  11, 0x00b33, 0x011e6, 0x1a014, 0x30282 },
393	{  12, 0x00b33, 0x011e6, 0x1a014, 0x30287 },
394	{  13, 0x00b33, 0x011e7, 0x1a014, 0x30282 },
395	{  14, 0x00b33, 0x011e8, 0x1a014, 0x30284 },
396
397	{  34, 0x00b33, 0x01266, 0x26014, 0x30282 },
398	{  38, 0x00b33, 0x01267, 0x26014, 0x30284 },
399	{  42, 0x00b33, 0x01268, 0x26014, 0x30286 },
400	{  46, 0x00b33, 0x01269, 0x26014, 0x30288 },
401
402	{  36, 0x00b33, 0x01266, 0x26014, 0x30288 },
403	{  40, 0x00b33, 0x01268, 0x26014, 0x30280 },
404	{  44, 0x00b33, 0x01269, 0x26014, 0x30282 },
405	{  48, 0x00b33, 0x0126a, 0x26014, 0x30284 },
406	{  52, 0x00b33, 0x0126b, 0x26014, 0x30286 },
407	{  56, 0x00b33, 0x0126c, 0x26014, 0x30288 },
408	{  60, 0x00b33, 0x0126e, 0x26014, 0x30280 },
409	{  64, 0x00b33, 0x0126f, 0x26014, 0x30282 },
410
411	{ 100, 0x00b33, 0x0128a, 0x2e014, 0x30280 },
412	{ 104, 0x00b33, 0x0128b, 0x2e014, 0x30282 },
413	{ 108, 0x00b33, 0x0128c, 0x2e014, 0x30284 },
414	{ 112, 0x00b33, 0x0128d, 0x2e014, 0x30286 },
415	{ 116, 0x00b33, 0x0128e, 0x2e014, 0x30288 },
416	{ 120, 0x00b33, 0x012a0, 0x2e014, 0x30280 },
417	{ 124, 0x00b33, 0x012a1, 0x2e014, 0x30282 },
418	{ 128, 0x00b33, 0x012a2, 0x2e014, 0x30284 },
419	{ 132, 0x00b33, 0x012a3, 0x2e014, 0x30286 },
420	{ 136, 0x00b33, 0x012a4, 0x2e014, 0x30288 },
421	{ 140, 0x00b33, 0x012a6, 0x2e014, 0x30280 },
422
423	{ 149, 0x00b33, 0x012a8, 0x2e014, 0x30287 },
424	{ 153, 0x00b33, 0x012a9, 0x2e014, 0x30289 },
425	{ 157, 0x00b33, 0x012ab, 0x2e014, 0x30281 },
426	{ 161, 0x00b33, 0x012ac, 0x2e014, 0x30283 },
427	{ 165, 0x00b33, 0x012ad, 0x2e014, 0x30285 }
428};
429
430static const struct usb_config rum_config[RUM_N_TRANSFER] = {
431	[RUM_BULK_WR] = {
432		.type = UE_BULK,
433		.endpoint = UE_ADDR_ANY,
434		.direction = UE_DIR_OUT,
435		.bufsize = (MCLBYTES + RT2573_TX_DESC_SIZE + 8),
436		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
437		.callback = rum_bulk_write_callback,
438		.timeout = 5000,	/* ms */
439	},
440	[RUM_BULK_RD] = {
441		.type = UE_BULK,
442		.endpoint = UE_ADDR_ANY,
443		.direction = UE_DIR_IN,
444		.bufsize = (MCLBYTES + RT2573_RX_DESC_SIZE),
445		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
446		.callback = rum_bulk_read_callback,
447	},
448};
449
450static int
451rum_match(device_t self)
452{
453	struct usb_attach_arg *uaa = device_get_ivars(self);
454
455	if (uaa->usb_mode != USB_MODE_HOST)
456		return (ENXIO);
457	if (uaa->info.bConfigIndex != 0)
458		return (ENXIO);
459	if (uaa->info.bIfaceIndex != RT2573_IFACE_INDEX)
460		return (ENXIO);
461
462	return (usbd_lookup_id_by_uaa(rum_devs, sizeof(rum_devs), uaa));
463}
464
465static int
466rum_attach(device_t self)
467{
468	struct usb_attach_arg *uaa = device_get_ivars(self);
469	struct rum_softc *sc = device_get_softc(self);
470	struct ieee80211com *ic = &sc->sc_ic;
471	uint8_t iface_index, bands;
472	uint32_t tmp;
473	int error, ntries;
474
475	device_set_usb_desc(self);
476	sc->sc_udev = uaa->device;
477	sc->sc_dev = self;
478
479	RUM_LOCK_INIT(sc);
480	RUM_CMDQ_LOCK_INIT(sc);
481	mbufq_init(&sc->sc_snd, ifqmaxlen);
482
483	iface_index = RT2573_IFACE_INDEX;
484	error = usbd_transfer_setup(uaa->device, &iface_index,
485	    sc->sc_xfer, rum_config, RUM_N_TRANSFER, sc, &sc->sc_mtx);
486	if (error) {
487		device_printf(self, "could not allocate USB transfers, "
488		    "err=%s\n", usbd_errstr(error));
489		goto detach;
490	}
491
492	RUM_LOCK(sc);
493	/* retrieve RT2573 rev. no */
494	for (ntries = 0; ntries < 100; ntries++) {
495		if ((tmp = rum_read(sc, RT2573_MAC_CSR0)) != 0)
496			break;
497		if (rum_pause(sc, hz / 100))
498			break;
499	}
500	if (ntries == 100) {
501		device_printf(sc->sc_dev, "timeout waiting for chip to settle\n");
502		RUM_UNLOCK(sc);
503		goto detach;
504	}
505
506	/* retrieve MAC address and various other things from EEPROM */
507	rum_read_eeprom(sc);
508
509	device_printf(sc->sc_dev, "MAC/BBP RT2573 (rev 0x%05x), RF %s\n",
510	    tmp, rum_get_rf(sc->rf_rev));
511
512	rum_load_microcode(sc, rt2573_ucode, sizeof(rt2573_ucode));
513	RUM_UNLOCK(sc);
514
515	ic->ic_softc = sc;
516	ic->ic_name = device_get_nameunit(self);
517	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
518
519	/* set device capabilities */
520	ic->ic_caps =
521	      IEEE80211_C_STA		/* station mode supported */
522	    | IEEE80211_C_IBSS		/* IBSS mode supported */
523	    | IEEE80211_C_MONITOR	/* monitor mode supported */
524	    | IEEE80211_C_HOSTAP	/* HostAp mode supported */
525	    | IEEE80211_C_AHDEMO	/* adhoc demo mode */
526	    | IEEE80211_C_TXPMGT	/* tx power management */
527	    | IEEE80211_C_SHPREAMBLE	/* short preamble supported */
528	    | IEEE80211_C_SHSLOT	/* short slot time supported */
529	    | IEEE80211_C_BGSCAN	/* bg scanning supported */
530	    | IEEE80211_C_WPA		/* 802.11i */
531	    | IEEE80211_C_WME		/* 802.11e */
532	    ;
533
534	ic->ic_cryptocaps =
535	    IEEE80211_CRYPTO_WEP |
536	    IEEE80211_CRYPTO_AES_CCM |
537	    IEEE80211_CRYPTO_TKIPMIC |
538	    IEEE80211_CRYPTO_TKIP;
539
540	bands = 0;
541	setbit(&bands, IEEE80211_MODE_11B);
542	setbit(&bands, IEEE80211_MODE_11G);
543	if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_5226)
544		setbit(&bands, IEEE80211_MODE_11A);
545	ieee80211_init_channels(ic, NULL, &bands);
546
547	ieee80211_ifattach(ic);
548	ic->ic_update_promisc = rum_update_promisc;
549	ic->ic_raw_xmit = rum_raw_xmit;
550	ic->ic_scan_start = rum_scan_start;
551	ic->ic_scan_end = rum_scan_end;
552	ic->ic_set_channel = rum_set_channel;
553	ic->ic_transmit = rum_transmit;
554	ic->ic_parent = rum_parent;
555	ic->ic_vap_create = rum_vap_create;
556	ic->ic_vap_delete = rum_vap_delete;
557	ic->ic_updateslot = rum_update_slot;
558	ic->ic_wme.wme_update = rum_wme_update;
559	ic->ic_update_mcast = rum_update_mcast;
560
561	ieee80211_radiotap_attach(ic,
562	    &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
563		RT2573_TX_RADIOTAP_PRESENT,
564	    &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
565		RT2573_RX_RADIOTAP_PRESENT);
566
567	TASK_INIT(&sc->cmdq_task, 0, rum_cmdq_cb, sc);
568
569	if (bootverbose)
570		ieee80211_announce(ic);
571
572	return (0);
573
574detach:
575	rum_detach(self);
576	return (ENXIO);			/* failure */
577}
578
579static int
580rum_detach(device_t self)
581{
582	struct rum_softc *sc = device_get_softc(self);
583	struct ieee80211com *ic = &sc->sc_ic;
584
585	/* Prevent further ioctls */
586	RUM_LOCK(sc);
587	sc->sc_detached = 1;
588	RUM_UNLOCK(sc);
589
590	/* stop all USB transfers */
591	usbd_transfer_unsetup(sc->sc_xfer, RUM_N_TRANSFER);
592
593	/* free TX list, if any */
594	RUM_LOCK(sc);
595	rum_unsetup_tx_list(sc);
596	RUM_UNLOCK(sc);
597
598	if (ic->ic_softc == sc) {
599		ieee80211_draintask(ic, &sc->cmdq_task);
600		ieee80211_ifdetach(ic);
601	}
602
603	mbufq_drain(&sc->sc_snd);
604	RUM_CMDQ_LOCK_DESTROY(sc);
605	RUM_LOCK_DESTROY(sc);
606
607	return (0);
608}
609
610static usb_error_t
611rum_do_request(struct rum_softc *sc,
612    struct usb_device_request *req, void *data)
613{
614	usb_error_t err;
615	int ntries = 10;
616
617	while (ntries--) {
618		err = usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
619		    req, data, 0, NULL, 250 /* ms */);
620		if (err == 0)
621			break;
622
623		DPRINTFN(1, "Control request failed, %s (retrying)\n",
624		    usbd_errstr(err));
625		if (rum_pause(sc, hz / 100))
626			break;
627	}
628	return (err);
629}
630
631static struct ieee80211vap *
632rum_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
633    enum ieee80211_opmode opmode, int flags,
634    const uint8_t bssid[IEEE80211_ADDR_LEN],
635    const uint8_t mac[IEEE80211_ADDR_LEN])
636{
637	struct rum_softc *sc = ic->ic_softc;
638	struct rum_vap *rvp;
639	struct ieee80211vap *vap;
640
641	if (!TAILQ_EMPTY(&ic->ic_vaps))		/* only one at a time */
642		return NULL;
643	rvp = malloc(sizeof(struct rum_vap), M_80211_VAP, M_WAITOK | M_ZERO);
644	vap = &rvp->vap;
645	/* enable s/w bmiss handling for sta mode */
646
647	if (ieee80211_vap_setup(ic, vap, name, unit, opmode,
648	    flags | IEEE80211_CLONE_NOBEACONS, bssid) != 0) {
649		/* out of memory */
650		free(rvp, M_80211_VAP);
651		return (NULL);
652	}
653
654	/* override state transition machine */
655	rvp->newstate = vap->iv_newstate;
656	vap->iv_newstate = rum_newstate;
657	vap->iv_key_alloc = rum_key_alloc;
658	vap->iv_key_set = rum_key_set;
659	vap->iv_key_delete = rum_key_delete;
660	vap->iv_update_beacon = rum_update_beacon;
661	vap->iv_max_aid = RT2573_ADDR_MAX;
662
663	usb_callout_init_mtx(&rvp->ratectl_ch, &sc->sc_mtx, 0);
664	TASK_INIT(&rvp->ratectl_task, 0, rum_ratectl_task, rvp);
665	ieee80211_ratectl_init(vap);
666	ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */);
667	/* complete setup */
668	ieee80211_vap_attach(vap, ieee80211_media_change,
669	    ieee80211_media_status, mac);
670	ic->ic_opmode = opmode;
671	return vap;
672}
673
674static void
675rum_vap_delete(struct ieee80211vap *vap)
676{
677	struct rum_vap *rvp = RUM_VAP(vap);
678	struct ieee80211com *ic = vap->iv_ic;
679
680	m_freem(rvp->bcn_mbuf);
681	usb_callout_drain(&rvp->ratectl_ch);
682	ieee80211_draintask(ic, &rvp->ratectl_task);
683	ieee80211_ratectl_deinit(vap);
684	ieee80211_vap_detach(vap);
685	free(rvp, M_80211_VAP);
686}
687
688static void
689rum_cmdq_cb(void *arg, int pending)
690{
691	struct rum_softc *sc = arg;
692	struct rum_cmdq *rc;
693
694	RUM_CMDQ_LOCK(sc);
695	while (sc->cmdq[sc->cmdq_first].func != NULL) {
696		rc = &sc->cmdq[sc->cmdq_first];
697		RUM_CMDQ_UNLOCK(sc);
698
699		RUM_LOCK(sc);
700		rc->func(sc, &rc->data, rc->rvp_id);
701		RUM_UNLOCK(sc);
702
703		RUM_CMDQ_LOCK(sc);
704		memset(rc, 0, sizeof (*rc));
705		sc->cmdq_first = (sc->cmdq_first + 1) % RUM_CMDQ_SIZE;
706	}
707	RUM_CMDQ_UNLOCK(sc);
708}
709
710static int
711rum_cmd_sleepable(struct rum_softc *sc, const void *ptr, size_t len,
712    uint8_t rvp_id, CMD_FUNC_PROTO)
713{
714	struct ieee80211com *ic = &sc->sc_ic;
715
716	KASSERT(len <= sizeof(union sec_param), ("buffer overflow"));
717
718	RUM_CMDQ_LOCK(sc);
719	if (sc->cmdq[sc->cmdq_last].func != NULL) {
720		device_printf(sc->sc_dev, "%s: cmdq overflow\n", __func__);
721		RUM_CMDQ_UNLOCK(sc);
722
723		return EAGAIN;
724	}
725
726	if (ptr != NULL)
727		memcpy(&sc->cmdq[sc->cmdq_last].data, ptr, len);
728	sc->cmdq[sc->cmdq_last].rvp_id = rvp_id;
729	sc->cmdq[sc->cmdq_last].func = func;
730	sc->cmdq_last = (sc->cmdq_last + 1) % RUM_CMDQ_SIZE;
731	RUM_CMDQ_UNLOCK(sc);
732
733	ieee80211_runtask(ic, &sc->cmdq_task);
734
735	return 0;
736}
737
738static void
739rum_tx_free(struct rum_tx_data *data, int txerr)
740{
741	struct rum_softc *sc = data->sc;
742
743	if (data->m != NULL) {
744		ieee80211_tx_complete(data->ni, data->m, txerr);
745		data->m = NULL;
746		data->ni = NULL;
747	}
748	STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
749	sc->tx_nfree++;
750}
751
752static void
753rum_setup_tx_list(struct rum_softc *sc)
754{
755	struct rum_tx_data *data;
756	int i;
757
758	sc->tx_nfree = 0;
759	STAILQ_INIT(&sc->tx_q);
760	STAILQ_INIT(&sc->tx_free);
761
762	for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
763		data = &sc->tx_data[i];
764
765		data->sc = sc;
766		STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
767		sc->tx_nfree++;
768	}
769}
770
771static void
772rum_unsetup_tx_list(struct rum_softc *sc)
773{
774	struct rum_tx_data *data;
775	int i;
776
777	/* make sure any subsequent use of the queues will fail */
778	sc->tx_nfree = 0;
779	STAILQ_INIT(&sc->tx_q);
780	STAILQ_INIT(&sc->tx_free);
781
782	/* free up all node references and mbufs */
783	for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
784		data = &sc->tx_data[i];
785
786		if (data->m != NULL) {
787			m_freem(data->m);
788			data->m = NULL;
789		}
790		if (data->ni != NULL) {
791			ieee80211_free_node(data->ni);
792			data->ni = NULL;
793		}
794	}
795}
796
797static int
798rum_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
799{
800	struct rum_vap *rvp = RUM_VAP(vap);
801	struct ieee80211com *ic = vap->iv_ic;
802	struct rum_softc *sc = ic->ic_softc;
803	const struct ieee80211_txparam *tp;
804	enum ieee80211_state ostate;
805	struct ieee80211_node *ni;
806	int ret;
807
808	ostate = vap->iv_state;
809	DPRINTF("%s -> %s\n",
810		ieee80211_state_name[ostate],
811		ieee80211_state_name[nstate]);
812
813	IEEE80211_UNLOCK(ic);
814	RUM_LOCK(sc);
815	usb_callout_stop(&rvp->ratectl_ch);
816
817	switch (nstate) {
818	case IEEE80211_S_INIT:
819		if (ostate == IEEE80211_S_RUN)
820			rum_abort_tsf_sync(sc);
821
822		break;
823
824	case IEEE80211_S_RUN:
825		ni = ieee80211_ref_node(vap->iv_bss);
826
827		if (vap->iv_opmode != IEEE80211_M_MONITOR) {
828			if (ic->ic_bsschan == IEEE80211_CHAN_ANYC ||
829			    ni->ni_chan == IEEE80211_CHAN_ANYC) {
830				ret = EINVAL;
831				goto run_fail;
832			}
833			rum_update_slot_cb(sc, NULL, 0);
834			rum_enable_mrr(sc);
835			rum_set_txpreamble(sc);
836			rum_set_basicrates(sc);
837			rum_set_maxretry(sc, vap);
838			IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid);
839			rum_set_bssid(sc, sc->sc_bssid);
840		}
841
842		if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
843		    vap->iv_opmode == IEEE80211_M_IBSS) {
844			if ((ret = rum_alloc_beacon(sc, vap)) != 0)
845				goto run_fail;
846		}
847
848		if (vap->iv_opmode != IEEE80211_M_MONITOR &&
849		    vap->iv_opmode != IEEE80211_M_AHDEMO) {
850			if ((ret = rum_enable_tsf_sync(sc)) != 0)
851				goto run_fail;
852		} else
853			rum_enable_tsf(sc);
854
855		/* enable automatic rate adaptation */
856		tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
857		if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
858			rum_ratectl_start(sc, ni);
859		ieee80211_free_node(ni);
860		break;
861	default:
862		break;
863	}
864	RUM_UNLOCK(sc);
865	IEEE80211_LOCK(ic);
866	return (rvp->newstate(vap, nstate, arg));
867
868run_fail:
869	RUM_UNLOCK(sc);
870	IEEE80211_LOCK(ic);
871	ieee80211_free_node(ni);
872	return ret;
873}
874
875static void
876rum_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
877{
878	struct rum_softc *sc = usbd_xfer_softc(xfer);
879	struct ieee80211vap *vap;
880	struct rum_tx_data *data;
881	struct mbuf *m;
882	struct usb_page_cache *pc;
883	unsigned int len;
884	int actlen, sumlen;
885
886	usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
887
888	switch (USB_GET_STATE(xfer)) {
889	case USB_ST_TRANSFERRED:
890		DPRINTFN(11, "transfer complete, %d bytes\n", actlen);
891
892		/* free resources */
893		data = usbd_xfer_get_priv(xfer);
894		rum_tx_free(data, 0);
895		usbd_xfer_set_priv(xfer, NULL);
896
897		/* FALLTHROUGH */
898	case USB_ST_SETUP:
899tr_setup:
900		data = STAILQ_FIRST(&sc->tx_q);
901		if (data) {
902			STAILQ_REMOVE_HEAD(&sc->tx_q, next);
903			m = data->m;
904
905			if (m->m_pkthdr.len > (int)(MCLBYTES + RT2573_TX_DESC_SIZE)) {
906				DPRINTFN(0, "data overflow, %u bytes\n",
907				    m->m_pkthdr.len);
908				m->m_pkthdr.len = (MCLBYTES + RT2573_TX_DESC_SIZE);
909			}
910			pc = usbd_xfer_get_frame(xfer, 0);
911			usbd_copy_in(pc, 0, &data->desc, RT2573_TX_DESC_SIZE);
912			usbd_m_copy_in(pc, RT2573_TX_DESC_SIZE, m, 0,
913			    m->m_pkthdr.len);
914
915			vap = data->ni->ni_vap;
916			if (ieee80211_radiotap_active_vap(vap)) {
917				struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
918
919				tap->wt_flags = 0;
920				tap->wt_rate = data->rate;
921				rum_get_tsf(sc, &tap->wt_tsf);
922				tap->wt_antenna = sc->tx_ant;
923
924				ieee80211_radiotap_tx(vap, m);
925			}
926
927			/* align end on a 4-bytes boundary */
928			len = (RT2573_TX_DESC_SIZE + m->m_pkthdr.len + 3) & ~3;
929			if ((len % 64) == 0)
930				len += 4;
931
932			DPRINTFN(11, "sending frame len=%u xferlen=%u\n",
933			    m->m_pkthdr.len, len);
934
935			usbd_xfer_set_frame_len(xfer, 0, len);
936			usbd_xfer_set_priv(xfer, data);
937
938			usbd_transfer_submit(xfer);
939		}
940		rum_start(sc);
941		break;
942
943	default:			/* Error */
944		DPRINTFN(11, "transfer error, %s\n",
945		    usbd_errstr(error));
946
947		counter_u64_add(sc->sc_ic.ic_oerrors, 1);
948		data = usbd_xfer_get_priv(xfer);
949		if (data != NULL) {
950			rum_tx_free(data, error);
951			usbd_xfer_set_priv(xfer, NULL);
952		}
953
954		if (error != USB_ERR_CANCELLED) {
955			if (error == USB_ERR_TIMEOUT)
956				device_printf(sc->sc_dev, "device timeout\n");
957
958			/*
959			 * Try to clear stall first, also if other
960			 * errors occur, hence clearing stall
961			 * introduces a 50 ms delay:
962			 */
963			usbd_xfer_set_stall(xfer);
964			goto tr_setup;
965		}
966		break;
967	}
968}
969
970static void
971rum_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
972{
973	struct rum_softc *sc = usbd_xfer_softc(xfer);
974	struct ieee80211com *ic = &sc->sc_ic;
975	struct ieee80211_frame_min *wh;
976	struct ieee80211_node *ni;
977	struct mbuf *m = NULL;
978	struct usb_page_cache *pc;
979	uint32_t flags;
980	uint8_t rssi = 0;
981	int len;
982
983	usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
984
985	switch (USB_GET_STATE(xfer)) {
986	case USB_ST_TRANSFERRED:
987
988		DPRINTFN(15, "rx done, actlen=%d\n", len);
989
990		if (len < (int)(RT2573_RX_DESC_SIZE + IEEE80211_MIN_LEN)) {
991			DPRINTF("%s: xfer too short %d\n",
992			    device_get_nameunit(sc->sc_dev), len);
993			counter_u64_add(ic->ic_ierrors, 1);
994			goto tr_setup;
995		}
996
997		len -= RT2573_RX_DESC_SIZE;
998		pc = usbd_xfer_get_frame(xfer, 0);
999		usbd_copy_out(pc, 0, &sc->sc_rx_desc, RT2573_RX_DESC_SIZE);
1000
1001		rssi = rum_get_rssi(sc, sc->sc_rx_desc.rssi);
1002		flags = le32toh(sc->sc_rx_desc.flags);
1003		if (flags & RT2573_RX_CRC_ERROR) {
1004			/*
1005		         * This should not happen since we did not
1006		         * request to receive those frames when we
1007		         * filled RUM_TXRX_CSR2:
1008		         */
1009			DPRINTFN(5, "PHY or CRC error\n");
1010			counter_u64_add(ic->ic_ierrors, 1);
1011			goto tr_setup;
1012		}
1013		if ((flags & RT2573_RX_DEC_MASK) != RT2573_RX_DEC_OK) {
1014			switch (flags & RT2573_RX_DEC_MASK) {
1015			case RT2573_RX_IV_ERROR:
1016				DPRINTFN(5, "IV/EIV error\n");
1017				break;
1018			case RT2573_RX_MIC_ERROR:
1019				DPRINTFN(5, "MIC error\n");
1020				break;
1021			case RT2573_RX_KEY_ERROR:
1022				DPRINTFN(5, "Key error\n");
1023				break;
1024			}
1025			counter_u64_add(ic->ic_ierrors, 1);
1026			goto tr_setup;
1027		}
1028
1029		m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1030		if (m == NULL) {
1031			DPRINTF("could not allocate mbuf\n");
1032			counter_u64_add(ic->ic_ierrors, 1);
1033			goto tr_setup;
1034		}
1035		usbd_copy_out(pc, RT2573_RX_DESC_SIZE,
1036		    mtod(m, uint8_t *), len);
1037
1038		wh = mtod(m, struct ieee80211_frame_min *);
1039
1040		if ((wh->i_fc[1] & IEEE80211_FC1_PROTECTED) &&
1041		    (flags & RT2573_RX_CIP_MASK) !=
1042		     RT2573_RX_CIP_MODE(RT2573_MODE_NOSEC)) {
1043			wh->i_fc[1] &= ~IEEE80211_FC1_PROTECTED;
1044			m->m_flags |= M_WEP;
1045		}
1046
1047		/* finalize mbuf */
1048		m->m_pkthdr.len = m->m_len = (flags >> 16) & 0xfff;
1049
1050		if (ieee80211_radiotap_active(ic)) {
1051			struct rum_rx_radiotap_header *tap = &sc->sc_rxtap;
1052
1053			tap->wr_flags = 0;
1054			tap->wr_rate = ieee80211_plcp2rate(sc->sc_rx_desc.rate,
1055			    (flags & RT2573_RX_OFDM) ?
1056			    IEEE80211_T_OFDM : IEEE80211_T_CCK);
1057			rum_get_tsf(sc, &tap->wr_tsf);
1058			tap->wr_antsignal = RT2573_NOISE_FLOOR + rssi;
1059			tap->wr_antnoise = RT2573_NOISE_FLOOR;
1060			tap->wr_antenna = sc->rx_ant;
1061		}
1062		/* FALLTHROUGH */
1063	case USB_ST_SETUP:
1064tr_setup:
1065		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1066		usbd_transfer_submit(xfer);
1067
1068		/*
1069		 * At the end of a USB callback it is always safe to unlock
1070		 * the private mutex of a device! That is why we do the
1071		 * "ieee80211_input" here, and not some lines up!
1072		 */
1073		RUM_UNLOCK(sc);
1074		if (m) {
1075			if (m->m_len >= sizeof(struct ieee80211_frame_min))
1076				ni = ieee80211_find_rxnode(ic, wh);
1077			else
1078				ni = NULL;
1079
1080			if (ni != NULL) {
1081				(void) ieee80211_input(ni, m, rssi,
1082				    RT2573_NOISE_FLOOR);
1083				ieee80211_free_node(ni);
1084			} else
1085				(void) ieee80211_input_all(ic, m, rssi,
1086				    RT2573_NOISE_FLOOR);
1087		}
1088		RUM_LOCK(sc);
1089		rum_start(sc);
1090		return;
1091
1092	default:			/* Error */
1093		if (error != USB_ERR_CANCELLED) {
1094			/* try to clear stall first */
1095			usbd_xfer_set_stall(xfer);
1096			goto tr_setup;
1097		}
1098		return;
1099	}
1100}
1101
1102static uint8_t
1103rum_plcp_signal(int rate)
1104{
1105	switch (rate) {
1106	/* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1107	case 12:	return 0xb;
1108	case 18:	return 0xf;
1109	case 24:	return 0xa;
1110	case 36:	return 0xe;
1111	case 48:	return 0x9;
1112	case 72:	return 0xd;
1113	case 96:	return 0x8;
1114	case 108:	return 0xc;
1115
1116	/* CCK rates (NB: not IEEE std, device-specific) */
1117	case 2:		return 0x0;
1118	case 4:		return 0x1;
1119	case 11:	return 0x2;
1120	case 22:	return 0x3;
1121	}
1122	return 0xff;		/* XXX unsupported/unknown rate */
1123}
1124
1125/*
1126 * Map net80211 cipher to RT2573 security mode.
1127 */
1128static uint8_t
1129rum_crypto_mode(struct rum_softc *sc, u_int cipher, int keylen)
1130{
1131	switch (cipher) {
1132	case IEEE80211_CIPHER_WEP:
1133		return (keylen < 8 ? RT2573_MODE_WEP40 : RT2573_MODE_WEP104);
1134	case IEEE80211_CIPHER_TKIP:
1135		return RT2573_MODE_TKIP;
1136	case IEEE80211_CIPHER_AES_CCM:
1137		return RT2573_MODE_AES_CCMP;
1138	default:
1139		device_printf(sc->sc_dev, "unknown cipher %d\n", cipher);
1140		return 0;
1141	}
1142}
1143
1144static void
1145rum_setup_tx_desc(struct rum_softc *sc, struct rum_tx_desc *desc,
1146    struct ieee80211_key *k, uint32_t flags, uint8_t xflags, uint8_t qid,
1147    int hdrlen, int len, int rate)
1148{
1149	struct ieee80211com *ic = &sc->sc_ic;
1150	struct wmeParams *wmep = &sc->wme_params[qid];
1151	uint16_t plcp_length;
1152	int remainder;
1153
1154	flags |= RT2573_TX_VALID;
1155	flags |= len << 16;
1156
1157	if (k != NULL && !(k->wk_flags & IEEE80211_KEY_SWCRYPT)) {
1158		const struct ieee80211_cipher *cip = k->wk_cipher;
1159
1160		len += cip->ic_header + cip->ic_trailer + cip->ic_miclen;
1161
1162		desc->eiv = 0;		/* for WEP */
1163		cip->ic_setiv(k, (uint8_t *)&desc->iv);
1164	}
1165
1166	/* setup PLCP fields */
1167	desc->plcp_signal  = rum_plcp_signal(rate);
1168	desc->plcp_service = 4;
1169
1170	len += IEEE80211_CRC_LEN;
1171	if (ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) {
1172		flags |= RT2573_TX_OFDM;
1173
1174		plcp_length = len & 0xfff;
1175		desc->plcp_length_hi = plcp_length >> 6;
1176		desc->plcp_length_lo = plcp_length & 0x3f;
1177	} else {
1178		if (rate == 0)
1179			rate = 2;	/* avoid division by zero */
1180		plcp_length = (16 * len + rate - 1) / rate;
1181		if (rate == 22) {
1182			remainder = (16 * len) % 22;
1183			if (remainder != 0 && remainder < 7)
1184				desc->plcp_service |= RT2573_PLCP_LENGEXT;
1185		}
1186		desc->plcp_length_hi = plcp_length >> 8;
1187		desc->plcp_length_lo = plcp_length & 0xff;
1188
1189		if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1190			desc->plcp_signal |= 0x08;
1191	}
1192
1193	desc->flags = htole32(flags);
1194	desc->hdrlen = hdrlen;
1195	desc->xflags = xflags;
1196
1197	desc->wme = htole16(RT2573_QID(qid) |
1198	    RT2573_AIFSN(wmep->wmep_aifsn) |
1199	    RT2573_LOGCWMIN(wmep->wmep_logcwmin) |
1200	    RT2573_LOGCWMAX(wmep->wmep_logcwmax));
1201}
1202
1203static int
1204rum_sendprot(struct rum_softc *sc,
1205    const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate)
1206{
1207	struct ieee80211com *ic = ni->ni_ic;
1208	const struct ieee80211_frame *wh;
1209	struct rum_tx_data *data;
1210	struct mbuf *mprot;
1211	int protrate, pktlen, flags, isshort;
1212	uint16_t dur;
1213
1214	RUM_LOCK_ASSERT(sc);
1215	KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY,
1216	    ("protection %d", prot));
1217
1218	wh = mtod(m, const struct ieee80211_frame *);
1219	pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
1220
1221	protrate = ieee80211_ctl_rate(ic->ic_rt, rate);
1222
1223	isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0;
1224	dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort)
1225	    + ieee80211_ack_duration(ic->ic_rt, rate, isshort);
1226	flags = 0;
1227	if (prot == IEEE80211_PROT_RTSCTS) {
1228		/* NB: CTS is the same size as an ACK */
1229		dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort);
1230		flags |= RT2573_TX_NEED_ACK;
1231		mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur);
1232	} else {
1233		mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur);
1234	}
1235	if (mprot == NULL) {
1236		/* XXX stat + msg */
1237		return (ENOBUFS);
1238	}
1239	data = STAILQ_FIRST(&sc->tx_free);
1240	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1241	sc->tx_nfree--;
1242
1243	data->m = mprot;
1244	data->ni = ieee80211_ref_node(ni);
1245	data->rate = protrate;
1246	rum_setup_tx_desc(sc, &data->desc, NULL, flags, 0, 0, 0,
1247	    mprot->m_pkthdr.len, protrate);
1248
1249	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1250	usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1251
1252	return 0;
1253}
1254
1255static uint32_t
1256rum_tx_crypto_flags(struct rum_softc *sc, struct ieee80211_node *ni,
1257    const struct ieee80211_key *k)
1258{
1259	struct ieee80211vap *vap = ni->ni_vap;
1260	u_int cipher;
1261	uint32_t flags = 0;
1262	uint8_t mode, pos;
1263
1264	if (!(k->wk_flags & IEEE80211_KEY_SWCRYPT)) {
1265		cipher = k->wk_cipher->ic_cipher;
1266		pos = k->wk_keyix;
1267		mode = rum_crypto_mode(sc, cipher, k->wk_keylen);
1268		if (mode == 0)
1269			return 0;
1270
1271		flags |= RT2573_TX_CIP_MODE(mode);
1272
1273		/* Do not trust GROUP flag */
1274		if (!(k >= &vap->iv_nw_keys[0] &&
1275		      k < &vap->iv_nw_keys[IEEE80211_WEP_NKID]))
1276			flags |= RT2573_TX_KEY_PAIR;
1277		else
1278			pos += 0 * RT2573_SKEY_MAX;	/* vap id */
1279
1280		flags |= RT2573_TX_KEY_ID(pos);
1281
1282		if (cipher == IEEE80211_CIPHER_TKIP)
1283			flags |= RT2573_TX_TKIPMIC;
1284	}
1285
1286	return flags;
1287}
1288
1289static int
1290rum_tx_mgt(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1291{
1292	struct ieee80211vap *vap = ni->ni_vap;
1293	struct ieee80211com *ic = &sc->sc_ic;
1294	struct rum_tx_data *data;
1295	struct ieee80211_frame *wh;
1296	const struct ieee80211_txparam *tp;
1297	struct ieee80211_key *k = NULL;
1298	uint32_t flags = 0;
1299	uint16_t dur;
1300	uint8_t ac, type, xflags = 0;
1301	int hdrlen;
1302
1303	RUM_LOCK_ASSERT(sc);
1304
1305	data = STAILQ_FIRST(&sc->tx_free);
1306	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1307	sc->tx_nfree--;
1308
1309	wh = mtod(m0, struct ieee80211_frame *);
1310	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1311	hdrlen = ieee80211_anyhdrsize(wh);
1312	ac = M_WME_GETAC(m0);
1313
1314	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1315		k = ieee80211_crypto_get_txkey(ni, m0);
1316		if (k == NULL)
1317			return (ENOENT);
1318
1319		if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) &&
1320		    !k->wk_cipher->ic_encap(k, m0))
1321			return (ENOBUFS);
1322
1323		wh = mtod(m0, struct ieee80211_frame *);
1324	}
1325
1326	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1327
1328	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1329		flags |= RT2573_TX_NEED_ACK;
1330
1331		dur = ieee80211_ack_duration(ic->ic_rt, tp->mgmtrate,
1332		    ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1333		USETW(wh->i_dur, dur);
1334
1335		/* tell hardware to add timestamp for probe responses */
1336		if (type == IEEE80211_FC0_TYPE_MGT &&
1337		    (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
1338		    IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1339			flags |= RT2573_TX_TIMESTAMP;
1340	}
1341
1342	if (type != IEEE80211_FC0_TYPE_CTL && !IEEE80211_QOS_HAS_SEQ(wh))
1343		xflags |= RT2573_TX_HWSEQ;
1344
1345	if (k != NULL)
1346		flags |= rum_tx_crypto_flags(sc, ni, k);
1347
1348	data->m = m0;
1349	data->ni = ni;
1350	data->rate = tp->mgmtrate;
1351
1352	rum_setup_tx_desc(sc, &data->desc, k, flags, xflags, ac, hdrlen,
1353	    m0->m_pkthdr.len, tp->mgmtrate);
1354
1355	DPRINTFN(10, "sending mgt frame len=%d rate=%d\n",
1356	    m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, tp->mgmtrate);
1357
1358	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1359	usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1360
1361	return (0);
1362}
1363
1364static int
1365rum_tx_raw(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
1366    const struct ieee80211_bpf_params *params)
1367{
1368	struct ieee80211com *ic = ni->ni_ic;
1369	struct ieee80211_frame *wh;
1370	struct rum_tx_data *data;
1371	uint32_t flags;
1372	uint8_t ac, type, xflags = 0;
1373	int rate, error;
1374
1375	RUM_LOCK_ASSERT(sc);
1376
1377	wh = mtod(m0, struct ieee80211_frame *);
1378	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1379
1380	ac = params->ibp_pri & 3;
1381
1382	rate = params->ibp_rate0;
1383	if (!ieee80211_isratevalid(ic->ic_rt, rate))
1384		return (EINVAL);
1385
1386	flags = 0;
1387	if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
1388		flags |= RT2573_TX_NEED_ACK;
1389	if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) {
1390		error = rum_sendprot(sc, m0, ni,
1391		    params->ibp_flags & IEEE80211_BPF_RTS ?
1392			 IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY,
1393		    rate);
1394		if (error || sc->tx_nfree == 0)
1395			return (ENOBUFS);
1396
1397		flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
1398	}
1399
1400	if (type != IEEE80211_FC0_TYPE_CTL && !IEEE80211_QOS_HAS_SEQ(wh))
1401		xflags |= RT2573_TX_HWSEQ;
1402
1403	data = STAILQ_FIRST(&sc->tx_free);
1404	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1405	sc->tx_nfree--;
1406
1407	data->m = m0;
1408	data->ni = ni;
1409	data->rate = rate;
1410
1411	/* XXX need to setup descriptor ourself */
1412	rum_setup_tx_desc(sc, &data->desc, NULL, flags, xflags, ac, 0,
1413	    m0->m_pkthdr.len, rate);
1414
1415	DPRINTFN(10, "sending raw frame len=%u rate=%u\n",
1416	    m0->m_pkthdr.len, rate);
1417
1418	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1419	usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1420
1421	return 0;
1422}
1423
1424static int
1425rum_tx_data(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1426{
1427	struct ieee80211vap *vap = ni->ni_vap;
1428	struct ieee80211com *ic = &sc->sc_ic;
1429	struct rum_tx_data *data;
1430	struct ieee80211_frame *wh;
1431	const struct ieee80211_txparam *tp;
1432	struct ieee80211_key *k = NULL;
1433	uint32_t flags = 0;
1434	uint16_t dur;
1435	uint8_t ac, type, qos, xflags = 0;
1436	int error, hdrlen, rate;
1437
1438	RUM_LOCK_ASSERT(sc);
1439
1440	wh = mtod(m0, struct ieee80211_frame *);
1441	type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
1442	hdrlen = ieee80211_anyhdrsize(wh);
1443
1444	if (IEEE80211_QOS_HAS_SEQ(wh))
1445		qos = ((const struct ieee80211_qosframe *)wh)->i_qos[0];
1446	else
1447		qos = 0;
1448	ac = M_WME_GETAC(m0);
1449
1450	tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
1451	if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1452		rate = tp->mcastrate;
1453	else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
1454		rate = tp->ucastrate;
1455	else
1456		rate = ni->ni_txrate;
1457
1458	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1459		k = ieee80211_crypto_get_txkey(ni, m0);
1460		if (k == NULL) {
1461			m_freem(m0);
1462			return (ENOENT);
1463		}
1464		if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) &&
1465		    !k->wk_cipher->ic_encap(k, m0)) {
1466			m_freem(m0);
1467			return (ENOBUFS);
1468		}
1469
1470		/* packet header may have moved, reset our local pointer */
1471		wh = mtod(m0, struct ieee80211_frame *);
1472	}
1473
1474	if (type != IEEE80211_FC0_TYPE_CTL && !IEEE80211_QOS_HAS_SEQ(wh))
1475		xflags |= RT2573_TX_HWSEQ;
1476
1477	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1478		int prot = IEEE80211_PROT_NONE;
1479		if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold)
1480			prot = IEEE80211_PROT_RTSCTS;
1481		else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
1482		    ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM)
1483			prot = ic->ic_protmode;
1484		if (prot != IEEE80211_PROT_NONE) {
1485			error = rum_sendprot(sc, m0, ni, prot, rate);
1486			if (error || sc->tx_nfree == 0) {
1487				m_freem(m0);
1488				return ENOBUFS;
1489			}
1490			flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
1491		}
1492	}
1493
1494	if (k != NULL)
1495		flags |= rum_tx_crypto_flags(sc, ni, k);
1496
1497	data = STAILQ_FIRST(&sc->tx_free);
1498	STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1499	sc->tx_nfree--;
1500
1501	data->m = m0;
1502	data->ni = ni;
1503	data->rate = rate;
1504
1505	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1506		/* Unicast frame, check if an ACK is expected. */
1507		if (!qos || (qos & IEEE80211_QOS_ACKPOLICY) !=
1508		    IEEE80211_QOS_ACKPOLICY_NOACK)
1509			flags |= RT2573_TX_NEED_ACK;
1510
1511		dur = ieee80211_ack_duration(ic->ic_rt, rate,
1512		    ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1513		USETW(wh->i_dur, dur);
1514	}
1515
1516	rum_setup_tx_desc(sc, &data->desc, k, flags, xflags, ac, hdrlen,
1517	    m0->m_pkthdr.len, rate);
1518
1519	DPRINTFN(10, "sending frame len=%d rate=%d\n",
1520	    m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, rate);
1521
1522	STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1523	usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1524
1525	return 0;
1526}
1527
1528static int
1529rum_transmit(struct ieee80211com *ic, struct mbuf *m)
1530{
1531	struct rum_softc *sc = ic->ic_softc;
1532	int error;
1533
1534	RUM_LOCK(sc);
1535	if (!sc->sc_running) {
1536		RUM_UNLOCK(sc);
1537		return (ENXIO);
1538	}
1539	error = mbufq_enqueue(&sc->sc_snd, m);
1540	if (error) {
1541		RUM_UNLOCK(sc);
1542		return (error);
1543	}
1544	rum_start(sc);
1545	RUM_UNLOCK(sc);
1546
1547	return (0);
1548}
1549
1550static void
1551rum_start(struct rum_softc *sc)
1552{
1553	struct ieee80211_node *ni;
1554	struct mbuf *m;
1555
1556	RUM_LOCK_ASSERT(sc);
1557
1558	if (!sc->sc_running)
1559		return;
1560
1561	while (sc->tx_nfree >= RUM_TX_MINFREE &&
1562	    (m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
1563		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1564		if (rum_tx_data(sc, m, ni) != 0) {
1565			if_inc_counter(ni->ni_vap->iv_ifp,
1566			    IFCOUNTER_OERRORS, 1);
1567			ieee80211_free_node(ni);
1568			break;
1569		}
1570	}
1571}
1572
1573static void
1574rum_parent(struct ieee80211com *ic)
1575{
1576	struct rum_softc *sc = ic->ic_softc;
1577	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1578
1579	RUM_LOCK(sc);
1580	if (sc->sc_detached) {
1581		RUM_UNLOCK(sc);
1582		return;
1583	}
1584	RUM_UNLOCK(sc);
1585
1586	if (ic->ic_nrunning > 0) {
1587		if (rum_init(sc) == 0)
1588			ieee80211_start_all(ic);
1589		else
1590			ieee80211_stop(vap);
1591	} else
1592		rum_stop(sc);
1593}
1594
1595static void
1596rum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len)
1597{
1598	struct usb_device_request req;
1599	usb_error_t error;
1600
1601	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1602	req.bRequest = RT2573_READ_EEPROM;
1603	USETW(req.wValue, 0);
1604	USETW(req.wIndex, addr);
1605	USETW(req.wLength, len);
1606
1607	error = rum_do_request(sc, &req, buf);
1608	if (error != 0) {
1609		device_printf(sc->sc_dev, "could not read EEPROM: %s\n",
1610		    usbd_errstr(error));
1611	}
1612}
1613
1614static uint32_t
1615rum_read(struct rum_softc *sc, uint16_t reg)
1616{
1617	uint32_t val;
1618
1619	rum_read_multi(sc, reg, &val, sizeof val);
1620
1621	return le32toh(val);
1622}
1623
1624static void
1625rum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len)
1626{
1627	struct usb_device_request req;
1628	usb_error_t error;
1629
1630	req.bmRequestType = UT_READ_VENDOR_DEVICE;
1631	req.bRequest = RT2573_READ_MULTI_MAC;
1632	USETW(req.wValue, 0);
1633	USETW(req.wIndex, reg);
1634	USETW(req.wLength, len);
1635
1636	error = rum_do_request(sc, &req, buf);
1637	if (error != 0) {
1638		device_printf(sc->sc_dev,
1639		    "could not multi read MAC register: %s\n",
1640		    usbd_errstr(error));
1641	}
1642}
1643
1644static usb_error_t
1645rum_write(struct rum_softc *sc, uint16_t reg, uint32_t val)
1646{
1647	uint32_t tmp = htole32(val);
1648
1649	return (rum_write_multi(sc, reg, &tmp, sizeof tmp));
1650}
1651
1652static usb_error_t
1653rum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len)
1654{
1655	struct usb_device_request req;
1656	usb_error_t error;
1657	size_t offset;
1658
1659	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1660	req.bRequest = RT2573_WRITE_MULTI_MAC;
1661	USETW(req.wValue, 0);
1662
1663	/* write at most 64 bytes at a time */
1664	for (offset = 0; offset < len; offset += 64) {
1665		USETW(req.wIndex, reg + offset);
1666		USETW(req.wLength, MIN(len - offset, 64));
1667
1668		error = rum_do_request(sc, &req, (char *)buf + offset);
1669		if (error != 0) {
1670			device_printf(sc->sc_dev,
1671			    "could not multi write MAC register: %s\n",
1672			    usbd_errstr(error));
1673			return (error);
1674		}
1675	}
1676
1677	return (USB_ERR_NORMAL_COMPLETION);
1678}
1679
1680static usb_error_t
1681rum_setbits(struct rum_softc *sc, uint16_t reg, uint32_t mask)
1682{
1683	return (rum_write(sc, reg, rum_read(sc, reg) | mask));
1684}
1685
1686static usb_error_t
1687rum_clrbits(struct rum_softc *sc, uint16_t reg, uint32_t mask)
1688{
1689	return (rum_write(sc, reg, rum_read(sc, reg) & ~mask));
1690}
1691
1692static usb_error_t
1693rum_modbits(struct rum_softc *sc, uint16_t reg, uint32_t set, uint32_t unset)
1694{
1695	return (rum_write(sc, reg, (rum_read(sc, reg) & ~unset) | set));
1696}
1697
1698static int
1699rum_bbp_busy(struct rum_softc *sc)
1700{
1701	int ntries;
1702
1703	for (ntries = 0; ntries < 100; ntries++) {
1704		if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1705			break;
1706		if (rum_pause(sc, hz / 100))
1707			break;
1708	}
1709	if (ntries == 100)
1710		return (ETIMEDOUT);
1711
1712	return (0);
1713}
1714
1715static void
1716rum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val)
1717{
1718	uint32_t tmp;
1719
1720	DPRINTFN(2, "reg=0x%08x\n", reg);
1721
1722	if (rum_bbp_busy(sc) != 0) {
1723		device_printf(sc->sc_dev, "could not write to BBP\n");
1724		return;
1725	}
1726
1727	tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val;
1728	rum_write(sc, RT2573_PHY_CSR3, tmp);
1729}
1730
1731static uint8_t
1732rum_bbp_read(struct rum_softc *sc, uint8_t reg)
1733{
1734	uint32_t val;
1735	int ntries;
1736
1737	DPRINTFN(2, "reg=0x%08x\n", reg);
1738
1739	if (rum_bbp_busy(sc) != 0) {
1740		device_printf(sc->sc_dev, "could not read BBP\n");
1741		return 0;
1742	}
1743
1744	val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8;
1745	rum_write(sc, RT2573_PHY_CSR3, val);
1746
1747	for (ntries = 0; ntries < 100; ntries++) {
1748		val = rum_read(sc, RT2573_PHY_CSR3);
1749		if (!(val & RT2573_BBP_BUSY))
1750			return val & 0xff;
1751		if (rum_pause(sc, hz / 100))
1752			break;
1753	}
1754
1755	device_printf(sc->sc_dev, "could not read BBP\n");
1756	return 0;
1757}
1758
1759static void
1760rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val)
1761{
1762	uint32_t tmp;
1763	int ntries;
1764
1765	for (ntries = 0; ntries < 100; ntries++) {
1766		if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY))
1767			break;
1768		if (rum_pause(sc, hz / 100))
1769			break;
1770	}
1771	if (ntries == 100) {
1772		device_printf(sc->sc_dev, "could not write to RF\n");
1773		return;
1774	}
1775
1776	tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 |
1777	    (reg & 3);
1778	rum_write(sc, RT2573_PHY_CSR4, tmp);
1779
1780	/* remember last written value in sc */
1781	sc->rf_regs[reg] = val;
1782
1783	DPRINTFN(15, "RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff);
1784}
1785
1786static void
1787rum_select_antenna(struct rum_softc *sc)
1788{
1789	uint8_t bbp4, bbp77;
1790	uint32_t tmp;
1791
1792	bbp4  = rum_bbp_read(sc, 4);
1793	bbp77 = rum_bbp_read(sc, 77);
1794
1795	/* TBD */
1796
1797	/* make sure Rx is disabled before switching antenna */
1798	tmp = rum_read(sc, RT2573_TXRX_CSR0);
1799	rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
1800
1801	rum_bbp_write(sc,  4, bbp4);
1802	rum_bbp_write(sc, 77, bbp77);
1803
1804	rum_write(sc, RT2573_TXRX_CSR0, tmp);
1805}
1806
1807/*
1808 * Enable multi-rate retries for frames sent at OFDM rates.
1809 * In 802.11b/g mode, allow fallback to CCK rates.
1810 */
1811static void
1812rum_enable_mrr(struct rum_softc *sc)
1813{
1814	struct ieee80211com *ic = &sc->sc_ic;
1815
1816	if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) {
1817		rum_setbits(sc, RT2573_TXRX_CSR4,
1818		    RT2573_MRR_ENABLED | RT2573_MRR_CCK_FALLBACK);
1819	} else {
1820		rum_modbits(sc, RT2573_TXRX_CSR4,
1821		    RT2573_MRR_ENABLED, RT2573_MRR_CCK_FALLBACK);
1822	}
1823}
1824
1825static void
1826rum_set_txpreamble(struct rum_softc *sc)
1827{
1828	struct ieee80211com *ic = &sc->sc_ic;
1829
1830	if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
1831		rum_setbits(sc, RT2573_TXRX_CSR4, RT2573_SHORT_PREAMBLE);
1832	else
1833		rum_clrbits(sc, RT2573_TXRX_CSR4, RT2573_SHORT_PREAMBLE);
1834}
1835
1836static void
1837rum_set_basicrates(struct rum_softc *sc)
1838{
1839	struct ieee80211com *ic = &sc->sc_ic;
1840
1841	/* update basic rate set */
1842	if (ic->ic_curmode == IEEE80211_MODE_11B) {
1843		/* 11b basic rates: 1, 2Mbps */
1844		rum_write(sc, RT2573_TXRX_CSR5, 0x3);
1845	} else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) {
1846		/* 11a basic rates: 6, 12, 24Mbps */
1847		rum_write(sc, RT2573_TXRX_CSR5, 0x150);
1848	} else {
1849		/* 11b/g basic rates: 1, 2, 5.5, 11Mbps */
1850		rum_write(sc, RT2573_TXRX_CSR5, 0xf);
1851	}
1852}
1853
1854/*
1855 * Reprogram MAC/BBP to switch to a new band.  Values taken from the reference
1856 * driver.
1857 */
1858static void
1859rum_select_band(struct rum_softc *sc, struct ieee80211_channel *c)
1860{
1861	uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104;
1862
1863	/* update all BBP registers that depend on the band */
1864	bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c;
1865	bbp35 = 0x50; bbp97 = 0x48; bbp98  = 0x48;
1866	if (IEEE80211_IS_CHAN_5GHZ(c)) {
1867		bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c;
1868		bbp35 += 0x10; bbp97 += 0x10; bbp98  += 0x10;
1869	}
1870	if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1871	    (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1872		bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10;
1873	}
1874
1875	sc->bbp17 = bbp17;
1876	rum_bbp_write(sc,  17, bbp17);
1877	rum_bbp_write(sc,  96, bbp96);
1878	rum_bbp_write(sc, 104, bbp104);
1879
1880	if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1881	    (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1882		rum_bbp_write(sc, 75, 0x80);
1883		rum_bbp_write(sc, 86, 0x80);
1884		rum_bbp_write(sc, 88, 0x80);
1885	}
1886
1887	rum_bbp_write(sc, 35, bbp35);
1888	rum_bbp_write(sc, 97, bbp97);
1889	rum_bbp_write(sc, 98, bbp98);
1890
1891	if (IEEE80211_IS_CHAN_2GHZ(c)) {
1892		rum_modbits(sc, RT2573_PHY_CSR0, RT2573_PA_PE_2GHZ,
1893		    RT2573_PA_PE_5GHZ);
1894	} else {
1895		rum_modbits(sc, RT2573_PHY_CSR0, RT2573_PA_PE_5GHZ,
1896		    RT2573_PA_PE_2GHZ);
1897	}
1898}
1899
1900static void
1901rum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c)
1902{
1903	struct ieee80211com *ic = &sc->sc_ic;
1904	const struct rfprog *rfprog;
1905	uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT;
1906	int8_t power;
1907	int i, chan;
1908
1909	chan = ieee80211_chan2ieee(ic, c);
1910	if (chan == 0 || chan == IEEE80211_CHAN_ANY)
1911		return;
1912
1913	/* select the appropriate RF settings based on what EEPROM says */
1914	rfprog = (sc->rf_rev == RT2573_RF_5225 ||
1915		  sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226;
1916
1917	/* find the settings for this channel (we know it exists) */
1918	for (i = 0; rfprog[i].chan != chan; i++);
1919
1920	power = sc->txpow[i];
1921	if (power < 0) {
1922		bbp94 += power;
1923		power = 0;
1924	} else if (power > 31) {
1925		bbp94 += power - 31;
1926		power = 31;
1927	}
1928
1929	/*
1930	 * If we are switching from the 2GHz band to the 5GHz band or
1931	 * vice-versa, BBP registers need to be reprogrammed.
1932	 */
1933	if (c->ic_flags != ic->ic_curchan->ic_flags) {
1934		rum_select_band(sc, c);
1935		rum_select_antenna(sc);
1936	}
1937	ic->ic_curchan = c;
1938
1939	rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1940	rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1941	rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1942	rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1943
1944	rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1945	rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1946	rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7 | 1);
1947	rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1948
1949	rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1950	rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1951	rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1952	rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1953
1954	rum_pause(sc, hz / 100);
1955
1956	/* enable smart mode for MIMO-capable RFs */
1957	bbp3 = rum_bbp_read(sc, 3);
1958
1959	bbp3 &= ~RT2573_SMART_MODE;
1960	if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527)
1961		bbp3 |= RT2573_SMART_MODE;
1962
1963	rum_bbp_write(sc, 3, bbp3);
1964
1965	if (bbp94 != RT2573_BBPR94_DEFAULT)
1966		rum_bbp_write(sc, 94, bbp94);
1967
1968	/* give the chip some extra time to do the switchover */
1969	rum_pause(sc, hz / 100);
1970}
1971
1972static void
1973rum_set_maxretry(struct rum_softc *sc, struct ieee80211vap *vap)
1974{
1975	const struct ieee80211_txparam *tp;
1976	struct ieee80211_node *ni = vap->iv_bss;
1977	struct rum_vap *rvp = RUM_VAP(vap);
1978
1979	tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
1980	rvp->maxretry = tp->maxretry < 0xf ? tp->maxretry : 0xf;
1981
1982	rum_modbits(sc, RT2573_TXRX_CSR4, RT2573_SHORT_RETRY(rvp->maxretry) |
1983	    RT2573_LONG_RETRY(rvp->maxretry),
1984	    RT2573_SHORT_RETRY_MASK | RT2573_LONG_RETRY_MASK);
1985}
1986
1987/*
1988 * Enable TSF synchronization and tell h/w to start sending beacons for IBSS
1989 * and HostAP operating modes.
1990 */
1991static int
1992rum_enable_tsf_sync(struct rum_softc *sc)
1993{
1994	struct ieee80211com *ic = &sc->sc_ic;
1995	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1996	uint32_t tmp;
1997
1998	if (vap->iv_opmode != IEEE80211_M_STA) {
1999		/*
2000		 * Change default 16ms TBTT adjustment to 8ms.
2001		 * Must be done before enabling beacon generation.
2002		 */
2003		if (rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8) != 0)
2004			return EIO;
2005	}
2006
2007	tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000;
2008
2009	/* set beacon interval (in 1/16ms unit) */
2010	tmp |= vap->iv_bss->ni_intval * 16;
2011	tmp |= RT2573_TSF_TIMER_EN | RT2573_TBTT_TIMER_EN;
2012
2013	switch (vap->iv_opmode) {
2014	case IEEE80211_M_STA:
2015		/*
2016		 * Local TSF is always updated with remote TSF on beacon
2017		 * reception.
2018		 */
2019		tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_STA);
2020		break;
2021	case IEEE80211_M_IBSS:
2022		/*
2023		 * Local TSF is updated with remote TSF on beacon reception
2024		 * only if the remote TSF is greater than local TSF.
2025		 */
2026		tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_IBSS);
2027		tmp |= RT2573_BCN_TX_EN;
2028		break;
2029	case IEEE80211_M_HOSTAP:
2030		/* SYNC with nobody */
2031		tmp |= RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_HOSTAP);
2032		tmp |= RT2573_BCN_TX_EN;
2033		break;
2034	default:
2035		device_printf(sc->sc_dev,
2036		    "Enabling TSF failed. undefined opmode %d\n",
2037		    vap->iv_opmode);
2038		return EINVAL;
2039	}
2040
2041	if (rum_write(sc, RT2573_TXRX_CSR9, tmp) != 0)
2042		return EIO;
2043
2044	return 0;
2045}
2046
2047static void
2048rum_enable_tsf(struct rum_softc *sc)
2049{
2050	rum_modbits(sc, RT2573_TXRX_CSR9, RT2573_TSF_TIMER_EN |
2051	    RT2573_TSF_SYNC_MODE(RT2573_TSF_SYNC_MODE_DIS), 0x00ffffff);
2052}
2053
2054static void
2055rum_abort_tsf_sync(struct rum_softc *sc)
2056{
2057	rum_clrbits(sc, RT2573_TXRX_CSR9, 0x00ffffff);
2058}
2059
2060static void
2061rum_get_tsf(struct rum_softc *sc, uint64_t *buf)
2062{
2063	rum_read_multi(sc, RT2573_TXRX_CSR12, buf, sizeof (*buf));
2064}
2065
2066static void
2067rum_update_slot_cb(struct rum_softc *sc, union sec_param *data, uint8_t rvp_id)
2068{
2069	struct ieee80211com *ic = &sc->sc_ic;
2070	uint8_t slottime;
2071
2072	slottime = IEEE80211_GET_SLOTTIME(ic);
2073
2074	rum_modbits(sc, RT2573_MAC_CSR9, slottime, 0xff);
2075
2076	DPRINTF("setting slot time to %uus\n", slottime);
2077}
2078
2079static void
2080rum_update_slot(struct ieee80211com *ic)
2081{
2082	rum_cmd_sleepable(ic->ic_softc, NULL, 0, 0, rum_update_slot_cb);
2083}
2084
2085static int
2086rum_wme_update(struct ieee80211com *ic)
2087{
2088	const struct wmeParams *chanp =
2089	    ic->ic_wme.wme_chanParams.cap_wmeParams;
2090	struct rum_softc *sc = ic->ic_softc;
2091	int error = 0;
2092
2093	RUM_LOCK(sc);
2094	error = rum_write(sc, RT2573_AIFSN_CSR,
2095	    chanp[WME_AC_VO].wmep_aifsn  << 12 |
2096	    chanp[WME_AC_VI].wmep_aifsn  <<  8 |
2097	    chanp[WME_AC_BK].wmep_aifsn  <<  4 |
2098	    chanp[WME_AC_BE].wmep_aifsn);
2099	if (error)
2100		goto print_err;
2101	error = rum_write(sc, RT2573_CWMIN_CSR,
2102	    chanp[WME_AC_VO].wmep_logcwmin << 12 |
2103	    chanp[WME_AC_VI].wmep_logcwmin <<  8 |
2104	    chanp[WME_AC_BK].wmep_logcwmin <<  4 |
2105	    chanp[WME_AC_BE].wmep_logcwmin);
2106	if (error)
2107		goto print_err;
2108	error = rum_write(sc, RT2573_CWMAX_CSR,
2109	    chanp[WME_AC_VO].wmep_logcwmax << 12 |
2110	    chanp[WME_AC_VI].wmep_logcwmax <<  8 |
2111	    chanp[WME_AC_BK].wmep_logcwmax <<  4 |
2112	    chanp[WME_AC_BE].wmep_logcwmax);
2113	if (error)
2114		goto print_err;
2115	error = rum_write(sc, RT2573_TXOP01_CSR,
2116	    chanp[WME_AC_BK].wmep_txopLimit << 16 |
2117	    chanp[WME_AC_BE].wmep_txopLimit);
2118	if (error)
2119		goto print_err;
2120	error = rum_write(sc, RT2573_TXOP23_CSR,
2121	    chanp[WME_AC_VO].wmep_txopLimit << 16 |
2122	    chanp[WME_AC_VI].wmep_txopLimit);
2123	if (error)
2124		goto print_err;
2125
2126	memcpy(sc->wme_params, chanp, sizeof(*chanp) * WME_NUM_AC);
2127
2128print_err:
2129	RUM_UNLOCK(sc);
2130	if (error != 0) {
2131		device_printf(sc->sc_dev, "%s: WME update failed, error %d\n",
2132		    __func__, error);
2133	}
2134
2135	return (error);
2136}
2137
2138static void
2139rum_set_bssid(struct rum_softc *sc, const uint8_t *bssid)
2140{
2141
2142	rum_write(sc, RT2573_MAC_CSR4,
2143	    bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24);
2144	rum_write(sc, RT2573_MAC_CSR5,
2145	    bssid[4] | bssid[5] << 8 | RT2573_NUM_BSSID_MSK(1));
2146}
2147
2148static void
2149rum_set_macaddr(struct rum_softc *sc, const uint8_t *addr)
2150{
2151
2152	rum_write(sc, RT2573_MAC_CSR2,
2153	    addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24);
2154	rum_write(sc, RT2573_MAC_CSR3,
2155	    addr[4] | addr[5] << 8 | 0xff << 16);
2156}
2157
2158static void
2159rum_setpromisc(struct rum_softc *sc)
2160{
2161	struct ieee80211com *ic = &sc->sc_ic;
2162
2163	if (ic->ic_promisc == 0)
2164		rum_setbits(sc, RT2573_TXRX_CSR0, RT2573_DROP_NOT_TO_ME);
2165	else
2166		rum_clrbits(sc, RT2573_TXRX_CSR0, RT2573_DROP_NOT_TO_ME);
2167
2168	DPRINTF("%s promiscuous mode\n", ic->ic_promisc > 0 ?
2169	    "entering" : "leaving");
2170}
2171
2172static void
2173rum_update_promisc(struct ieee80211com *ic)
2174{
2175	struct rum_softc *sc = ic->ic_softc;
2176
2177	RUM_LOCK(sc);
2178	if (sc->sc_running)
2179		rum_setpromisc(sc);
2180	RUM_UNLOCK(sc);
2181}
2182
2183static void
2184rum_update_mcast(struct ieee80211com *ic)
2185{
2186	/* Ignore. */
2187}
2188
2189static const char *
2190rum_get_rf(int rev)
2191{
2192	switch (rev) {
2193	case RT2573_RF_2527:	return "RT2527 (MIMO XR)";
2194	case RT2573_RF_2528:	return "RT2528";
2195	case RT2573_RF_5225:	return "RT5225 (MIMO XR)";
2196	case RT2573_RF_5226:	return "RT5226";
2197	default:		return "unknown";
2198	}
2199}
2200
2201static void
2202rum_read_eeprom(struct rum_softc *sc)
2203{
2204	uint16_t val;
2205#ifdef RUM_DEBUG
2206	int i;
2207#endif
2208
2209	/* read MAC address */
2210	rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, sc->sc_ic.ic_macaddr, 6);
2211
2212	rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2);
2213	val = le16toh(val);
2214	sc->rf_rev =   (val >> 11) & 0x1f;
2215	sc->hw_radio = (val >> 10) & 0x1;
2216	sc->rx_ant =   (val >> 4)  & 0x3;
2217	sc->tx_ant =   (val >> 2)  & 0x3;
2218	sc->nb_ant =   val & 0x3;
2219
2220	DPRINTF("RF revision=%d\n", sc->rf_rev);
2221
2222	rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2);
2223	val = le16toh(val);
2224	sc->ext_5ghz_lna = (val >> 6) & 0x1;
2225	sc->ext_2ghz_lna = (val >> 4) & 0x1;
2226
2227	DPRINTF("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n",
2228	    sc->ext_2ghz_lna, sc->ext_5ghz_lna);
2229
2230	rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2);
2231	val = le16toh(val);
2232	if ((val & 0xff) != 0xff)
2233		sc->rssi_2ghz_corr = (int8_t)(val & 0xff);	/* signed */
2234
2235	/* Only [-10, 10] is valid */
2236	if (sc->rssi_2ghz_corr < -10 || sc->rssi_2ghz_corr > 10)
2237		sc->rssi_2ghz_corr = 0;
2238
2239	rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2);
2240	val = le16toh(val);
2241	if ((val & 0xff) != 0xff)
2242		sc->rssi_5ghz_corr = (int8_t)(val & 0xff);	/* signed */
2243
2244	/* Only [-10, 10] is valid */
2245	if (sc->rssi_5ghz_corr < -10 || sc->rssi_5ghz_corr > 10)
2246		sc->rssi_5ghz_corr = 0;
2247
2248	if (sc->ext_2ghz_lna)
2249		sc->rssi_2ghz_corr -= 14;
2250	if (sc->ext_5ghz_lna)
2251		sc->rssi_5ghz_corr -= 14;
2252
2253	DPRINTF("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n",
2254	    sc->rssi_2ghz_corr, sc->rssi_5ghz_corr);
2255
2256	rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2);
2257	val = le16toh(val);
2258	if ((val & 0xff) != 0xff)
2259		sc->rffreq = val & 0xff;
2260
2261	DPRINTF("RF freq=%d\n", sc->rffreq);
2262
2263	/* read Tx power for all a/b/g channels */
2264	rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14);
2265	/* XXX default Tx power for 802.11a channels */
2266	memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14);
2267#ifdef RUM_DEBUG
2268	for (i = 0; i < 14; i++)
2269		DPRINTF("Channel=%d Tx power=%d\n", i + 1,  sc->txpow[i]);
2270#endif
2271
2272	/* read default values for BBP registers */
2273	rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
2274#ifdef RUM_DEBUG
2275	for (i = 0; i < 14; i++) {
2276		if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
2277			continue;
2278		DPRINTF("BBP R%d=%02x\n", sc->bbp_prom[i].reg,
2279		    sc->bbp_prom[i].val);
2280	}
2281#endif
2282}
2283
2284static int
2285rum_bbp_wakeup(struct rum_softc *sc)
2286{
2287	unsigned int ntries;
2288
2289	for (ntries = 0; ntries < 100; ntries++) {
2290		if (rum_read(sc, RT2573_MAC_CSR12) & 8)
2291			break;
2292		rum_write(sc, RT2573_MAC_CSR12, 4);	/* force wakeup */
2293		if (rum_pause(sc, hz / 100))
2294			break;
2295	}
2296	if (ntries == 100) {
2297		device_printf(sc->sc_dev,
2298		    "timeout waiting for BBP/RF to wakeup\n");
2299		return (ETIMEDOUT);
2300	}
2301
2302	return (0);
2303}
2304
2305static int
2306rum_bbp_init(struct rum_softc *sc)
2307{
2308	int i, ntries;
2309
2310	/* wait for BBP to be ready */
2311	for (ntries = 0; ntries < 100; ntries++) {
2312		const uint8_t val = rum_bbp_read(sc, 0);
2313		if (val != 0 && val != 0xff)
2314			break;
2315		if (rum_pause(sc, hz / 100))
2316			break;
2317	}
2318	if (ntries == 100) {
2319		device_printf(sc->sc_dev, "timeout waiting for BBP\n");
2320		return EIO;
2321	}
2322
2323	/* initialize BBP registers to default values */
2324	for (i = 0; i < nitems(rum_def_bbp); i++)
2325		rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val);
2326
2327	/* write vendor-specific BBP values (from EEPROM) */
2328	for (i = 0; i < 16; i++) {
2329		if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
2330			continue;
2331		rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
2332	}
2333
2334	return 0;
2335}
2336
2337static void
2338rum_clr_shkey_regs(struct rum_softc *sc)
2339{
2340	rum_write(sc, RT2573_SEC_CSR0, 0);
2341	rum_write(sc, RT2573_SEC_CSR1, 0);
2342	rum_write(sc, RT2573_SEC_CSR5, 0);
2343}
2344
2345static int
2346rum_init(struct rum_softc *sc)
2347{
2348	struct ieee80211com *ic = &sc->sc_ic;
2349	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2350	uint32_t tmp;
2351	int i, ret;
2352
2353	RUM_LOCK(sc);
2354	if (sc->sc_running) {
2355		ret = 0;
2356		goto end;
2357	}
2358
2359	/* initialize MAC registers to default values */
2360	for (i = 0; i < nitems(rum_def_mac); i++)
2361		rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val);
2362
2363	/* reset some WME parameters to default values */
2364	sc->wme_params[0].wmep_aifsn = 2;
2365	sc->wme_params[0].wmep_logcwmin = 4;
2366	sc->wme_params[0].wmep_logcwmax = 10;
2367
2368	/* set host ready */
2369	rum_write(sc, RT2573_MAC_CSR1, RT2573_RESET_ASIC | RT2573_RESET_BBP);
2370	rum_write(sc, RT2573_MAC_CSR1, 0);
2371
2372	/* wait for BBP/RF to wakeup */
2373	if ((ret = rum_bbp_wakeup(sc)) != 0)
2374		goto end;
2375
2376	if ((ret = rum_bbp_init(sc)) != 0)
2377		goto end;
2378
2379	/* select default channel */
2380	rum_select_band(sc, ic->ic_curchan);
2381	rum_select_antenna(sc);
2382	rum_set_chan(sc, ic->ic_curchan);
2383
2384	/* clear STA registers */
2385	rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
2386
2387	/* clear security registers (if required) */
2388	if (sc->sc_clr_shkeys == 0) {
2389		rum_clr_shkey_regs(sc);
2390		sc->sc_clr_shkeys = 1;
2391	}
2392
2393	rum_set_macaddr(sc, vap ? vap->iv_myaddr : ic->ic_macaddr);
2394
2395	/* initialize ASIC */
2396	rum_write(sc, RT2573_MAC_CSR1, RT2573_HOST_READY);
2397
2398	/*
2399	 * Allocate Tx and Rx xfer queues.
2400	 */
2401	rum_setup_tx_list(sc);
2402
2403	/* update Rx filter */
2404	tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff;
2405
2406	tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR;
2407	if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2408		tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR |
2409		       RT2573_DROP_ACKCTS;
2410		if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2411			tmp |= RT2573_DROP_TODS;
2412		if (ic->ic_promisc == 0)
2413			tmp |= RT2573_DROP_NOT_TO_ME;
2414	}
2415	rum_write(sc, RT2573_TXRX_CSR0, tmp);
2416
2417	sc->sc_running = 1;
2418	usbd_xfer_set_stall(sc->sc_xfer[RUM_BULK_WR]);
2419	usbd_transfer_start(sc->sc_xfer[RUM_BULK_RD]);
2420
2421end:	RUM_UNLOCK(sc);
2422
2423	if (ret != 0)
2424		rum_stop(sc);
2425
2426	return ret;
2427}
2428
2429static void
2430rum_stop(struct rum_softc *sc)
2431{
2432
2433	RUM_LOCK(sc);
2434	if (!sc->sc_running) {
2435		RUM_UNLOCK(sc);
2436		return;
2437	}
2438	sc->sc_running = 0;
2439	RUM_UNLOCK(sc);
2440
2441	/*
2442	 * Drain the USB transfers, if not already drained:
2443	 */
2444	usbd_transfer_drain(sc->sc_xfer[RUM_BULK_WR]);
2445	usbd_transfer_drain(sc->sc_xfer[RUM_BULK_RD]);
2446
2447	RUM_LOCK(sc);
2448	rum_unsetup_tx_list(sc);
2449
2450	/* disable Rx */
2451	rum_setbits(sc, RT2573_TXRX_CSR0, RT2573_DISABLE_RX);
2452
2453	/* reset ASIC */
2454	rum_write(sc, RT2573_MAC_CSR1, RT2573_RESET_ASIC | RT2573_RESET_BBP);
2455	rum_write(sc, RT2573_MAC_CSR1, 0);
2456	RUM_UNLOCK(sc);
2457}
2458
2459static void
2460rum_load_microcode(struct rum_softc *sc, const uint8_t *ucode, size_t size)
2461{
2462	struct usb_device_request req;
2463	uint16_t reg = RT2573_MCU_CODE_BASE;
2464	usb_error_t err;
2465
2466	/* copy firmware image into NIC */
2467	for (; size >= 4; reg += 4, ucode += 4, size -= 4) {
2468		err = rum_write(sc, reg, UGETDW(ucode));
2469		if (err) {
2470			/* firmware already loaded ? */
2471			device_printf(sc->sc_dev, "Firmware load "
2472			    "failure! (ignored)\n");
2473			break;
2474		}
2475	}
2476
2477	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2478	req.bRequest = RT2573_MCU_CNTL;
2479	USETW(req.wValue, RT2573_MCU_RUN);
2480	USETW(req.wIndex, 0);
2481	USETW(req.wLength, 0);
2482
2483	err = rum_do_request(sc, &req, NULL);
2484	if (err != 0) {
2485		device_printf(sc->sc_dev, "could not run firmware: %s\n",
2486		    usbd_errstr(err));
2487	}
2488
2489	/* give the chip some time to boot */
2490	rum_pause(sc, hz / 8);
2491}
2492
2493static int
2494rum_set_beacon(struct rum_softc *sc, struct ieee80211vap *vap)
2495{
2496	struct ieee80211com *ic = vap->iv_ic;
2497	struct rum_vap *rvp = RUM_VAP(vap);
2498	struct mbuf *m = rvp->bcn_mbuf;
2499	const struct ieee80211_txparam *tp;
2500	struct rum_tx_desc desc;
2501
2502	RUM_LOCK_ASSERT(sc);
2503
2504	if (m == NULL)
2505		return EINVAL;
2506	if (ic->ic_bsschan == IEEE80211_CHAN_ANYC)
2507		return EINVAL;
2508
2509	tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)];
2510	rum_setup_tx_desc(sc, &desc, NULL, RT2573_TX_TIMESTAMP,
2511	    RT2573_TX_HWSEQ, 0, 0, m->m_pkthdr.len, tp->mgmtrate);
2512
2513	/* copy the Tx descriptor into NIC memory */
2514	if (rum_write_multi(sc, RT2573_HW_BCN_BASE(0), (uint8_t *)&desc,
2515	    RT2573_TX_DESC_SIZE) != 0)
2516		return EIO;
2517
2518	/* copy beacon header and payload into NIC memory */
2519	if (rum_write_multi(sc, RT2573_HW_BCN_BASE(0) + RT2573_TX_DESC_SIZE,
2520	    mtod(m, uint8_t *), m->m_pkthdr.len) != 0)
2521		return EIO;
2522
2523	return 0;
2524}
2525
2526static int
2527rum_alloc_beacon(struct rum_softc *sc, struct ieee80211vap *vap)
2528{
2529	struct rum_vap *rvp = RUM_VAP(vap);
2530	struct ieee80211_node *ni = vap->iv_bss;
2531	struct mbuf *m;
2532
2533	if (ni->ni_chan == IEEE80211_CHAN_ANYC)
2534		return EINVAL;
2535
2536	m = ieee80211_beacon_alloc(ni);
2537	if (m == NULL)
2538		return ENOMEM;
2539
2540	if (rvp->bcn_mbuf != NULL)
2541		m_freem(rvp->bcn_mbuf);
2542
2543	rvp->bcn_mbuf = m;
2544
2545	return (rum_set_beacon(sc, vap));
2546}
2547
2548static void
2549rum_update_beacon_cb(struct rum_softc *sc, union sec_param *data,
2550    uint8_t rvp_id)
2551{
2552	struct ieee80211vap *vap = data->vap;
2553
2554	rum_set_beacon(sc, vap);
2555}
2556
2557static void
2558rum_update_beacon(struct ieee80211vap *vap, int item)
2559{
2560	struct ieee80211com *ic = vap->iv_ic;
2561	struct rum_softc *sc = ic->ic_softc;
2562	struct rum_vap *rvp = RUM_VAP(vap);
2563	struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
2564	struct ieee80211_node *ni = vap->iv_bss;
2565	struct mbuf *m = rvp->bcn_mbuf;
2566	int mcast = 0;
2567
2568	RUM_LOCK(sc);
2569	if (m == NULL) {
2570		m = ieee80211_beacon_alloc(ni);
2571		if (m == NULL) {
2572			device_printf(sc->sc_dev,
2573			    "%s: could not allocate beacon frame\n", __func__);
2574			RUM_UNLOCK(sc);
2575			return;
2576		}
2577		rvp->bcn_mbuf = m;
2578	}
2579
2580	switch (item) {
2581	case IEEE80211_BEACON_ERP:
2582		rum_update_slot(ic);
2583		break;
2584	case IEEE80211_BEACON_TIM:
2585		mcast = 1;	/*TODO*/
2586		break;
2587	default:
2588		break;
2589	}
2590	RUM_UNLOCK(sc);
2591
2592	setbit(bo->bo_flags, item);
2593	ieee80211_beacon_update(ni, m, mcast);
2594
2595	rum_cmd_sleepable(sc, &vap, sizeof(vap), 0, rum_update_beacon_cb);
2596}
2597
2598static int
2599rum_common_key_set(struct rum_softc *sc, struct ieee80211_key *k,
2600    uint16_t base)
2601{
2602
2603	if (rum_write_multi(sc, base, k->wk_key, k->wk_keylen))
2604		return EIO;
2605
2606	if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP) {
2607		if (rum_write_multi(sc, base + IEEE80211_KEYBUF_SIZE,
2608		    k->wk_txmic, 8))
2609			return EIO;
2610		if (rum_write_multi(sc, base + IEEE80211_KEYBUF_SIZE + 8,
2611		    k->wk_rxmic, 8))
2612			return EIO;
2613	}
2614
2615	return 0;
2616}
2617
2618static void
2619rum_group_key_set_cb(struct rum_softc *sc, union sec_param *data,
2620    uint8_t rvp_id)
2621{
2622	struct ieee80211_key *k = &data->key;
2623	uint8_t mode;
2624
2625	if (sc->sc_clr_shkeys == 0) {
2626		rum_clr_shkey_regs(sc);
2627		sc->sc_clr_shkeys = 1;
2628	}
2629
2630	mode = rum_crypto_mode(sc, k->wk_cipher->ic_cipher, k->wk_keylen);
2631	if (mode == 0)
2632		goto print_err;
2633
2634	DPRINTFN(1, "setting group key %d for vap %d, mode %d "
2635	    "(tx %s, rx %s)\n", k->wk_keyix, rvp_id, mode,
2636	    (k->wk_flags & IEEE80211_KEY_XMIT) ? "on" : "off",
2637	    (k->wk_flags & IEEE80211_KEY_RECV) ? "on" : "off");
2638
2639	/* Install the key. */
2640	if (rum_common_key_set(sc, k, RT2573_SKEY(rvp_id, k->wk_keyix)) != 0)
2641		goto print_err;
2642
2643	/* Set cipher mode. */
2644	if (rum_modbits(sc, rvp_id < 2 ? RT2573_SEC_CSR1 : RT2573_SEC_CSR5,
2645	      mode << (rvp_id % 2 + k->wk_keyix) * RT2573_SKEY_MAX,
2646	      RT2573_MODE_MASK << (rvp_id % 2 + k->wk_keyix) * RT2573_SKEY_MAX)
2647	    != 0)
2648		goto print_err;
2649
2650	/* Mark this key as valid. */
2651	if (rum_setbits(sc, RT2573_SEC_CSR0,
2652	      1 << (rvp_id * RT2573_SKEY_MAX + k->wk_keyix)) != 0)
2653		goto print_err;
2654
2655	return;
2656
2657print_err:
2658	device_printf(sc->sc_dev, "%s: cannot set group key %d for vap %d\n",
2659	    __func__, k->wk_keyix, rvp_id);
2660}
2661
2662static void
2663rum_group_key_del_cb(struct rum_softc *sc, union sec_param *data,
2664    uint8_t rvp_id)
2665{
2666	struct ieee80211_key *k = &data->key;
2667
2668	DPRINTF("%s: removing group key %d for vap %d\n", __func__,
2669	    k->wk_keyix, rvp_id);
2670	rum_clrbits(sc,
2671	    rvp_id < 2 ? RT2573_SEC_CSR1 : RT2573_SEC_CSR5,
2672	    RT2573_MODE_MASK << (rvp_id % 2 + k->wk_keyix) * RT2573_SKEY_MAX);
2673	rum_clrbits(sc, RT2573_SEC_CSR0,
2674	    rvp_id * RT2573_SKEY_MAX + k->wk_keyix);
2675}
2676
2677static void
2678rum_pair_key_set_cb(struct rum_softc *sc, union sec_param *data,
2679    uint8_t rvp_id)
2680{
2681	struct ieee80211_key *k = &data->key;
2682	uint8_t buf[IEEE80211_ADDR_LEN + 1];
2683	uint8_t mode;
2684
2685	mode = rum_crypto_mode(sc, k->wk_cipher->ic_cipher, k->wk_keylen);
2686	if (mode == 0)
2687		goto print_err;
2688
2689	DPRINTFN(1, "setting pairwise key %d for vap %d, mode %d "
2690	    "(tx %s, rx %s)\n", k->wk_keyix, rvp_id, mode,
2691	    (k->wk_flags & IEEE80211_KEY_XMIT) ? "on" : "off",
2692	    (k->wk_flags & IEEE80211_KEY_RECV) ? "on" : "off");
2693
2694	/* Install the key. */
2695	if (rum_common_key_set(sc, k, RT2573_PKEY(k->wk_keyix)) != 0)
2696		goto print_err;
2697
2698	IEEE80211_ADDR_COPY(buf, k->wk_macaddr);
2699	buf[IEEE80211_ADDR_LEN] = mode;
2700
2701	/* Set transmitter address and cipher mode. */
2702	if (rum_write_multi(sc, RT2573_ADDR_ENTRY(k->wk_keyix),
2703	      buf, sizeof buf) != 0)
2704		goto print_err;
2705
2706	/* Enable key table lookup for this vap. */
2707	if (sc->vap_key_count[rvp_id]++ == 0)
2708		if (rum_setbits(sc, RT2573_SEC_CSR4, 1 << rvp_id) != 0)
2709			goto print_err;
2710
2711	/* Mark this key as valid. */
2712	if (rum_setbits(sc,
2713	      k->wk_keyix < 32 ? RT2573_SEC_CSR2 : RT2573_SEC_CSR3,
2714	      1 << (k->wk_keyix % 32)) != 0)
2715		goto print_err;
2716
2717	return;
2718
2719print_err:
2720	device_printf(sc->sc_dev,
2721	    "%s: cannot set pairwise key %d, vap %d\n", __func__, k->wk_keyix,
2722	    rvp_id);
2723}
2724
2725static void
2726rum_pair_key_del_cb(struct rum_softc *sc, union sec_param *data,
2727    uint8_t rvp_id)
2728{
2729	struct ieee80211_key *k = &data->key;
2730
2731	DPRINTF("%s: removing key %d\n", __func__, k->wk_keyix);
2732	rum_clrbits(sc, (k->wk_keyix < 32) ? RT2573_SEC_CSR2 : RT2573_SEC_CSR3,
2733	    1 << (k->wk_keyix % 32));
2734	sc->keys_bmap &= ~(1 << k->wk_keyix);
2735	if (--sc->vap_key_count[rvp_id] == 0)
2736		rum_clrbits(sc, RT2573_SEC_CSR4, 1 << rvp_id);
2737}
2738
2739static int
2740rum_key_alloc(struct ieee80211vap *vap, struct ieee80211_key *k,
2741    ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
2742{
2743	struct rum_softc *sc = vap->iv_ic->ic_softc;
2744	uint8_t i;
2745
2746	if (!(&vap->iv_nw_keys[0] <= k &&
2747	     k < &vap->iv_nw_keys[IEEE80211_WEP_NKID])) {
2748		if (!(k->wk_flags & IEEE80211_KEY_SWCRYPT)) {
2749			RUM_LOCK(sc);
2750			for (i = 0; i < RT2573_ADDR_MAX; i++) {
2751				if ((sc->keys_bmap & (1 << i)) == 0) {
2752					sc->keys_bmap |= 1 << i;
2753					*keyix = i;
2754					break;
2755				}
2756			}
2757			RUM_UNLOCK(sc);
2758			if (i == RT2573_ADDR_MAX) {
2759				device_printf(sc->sc_dev,
2760				    "%s: no free space in the key table\n",
2761				    __func__);
2762				return 0;
2763			}
2764		} else
2765			*keyix = 0;
2766	} else {
2767		*keyix = k - vap->iv_nw_keys;
2768	}
2769	*rxkeyix = *keyix;
2770	return 1;
2771}
2772
2773static int
2774rum_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
2775{
2776	struct rum_softc *sc = vap->iv_ic->ic_softc;
2777	int group;
2778
2779	if (k->wk_flags & IEEE80211_KEY_SWCRYPT) {
2780		/* Not for us. */
2781		return 1;
2782	}
2783
2784	group = k >= &vap->iv_nw_keys[0] && k < &vap->iv_nw_keys[IEEE80211_WEP_NKID];
2785
2786	return !rum_cmd_sleepable(sc, k, sizeof(*k), 0,
2787		   group ? rum_group_key_set_cb : rum_pair_key_set_cb);
2788}
2789
2790static int
2791rum_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
2792{
2793	struct rum_softc *sc = vap->iv_ic->ic_softc;
2794	int group;
2795
2796	if (k->wk_flags & IEEE80211_KEY_SWCRYPT) {
2797		/* Not for us. */
2798		return 1;
2799	}
2800
2801	group = k >= &vap->iv_nw_keys[0] && k < &vap->iv_nw_keys[IEEE80211_WEP_NKID];
2802
2803	return !rum_cmd_sleepable(sc, k, sizeof(*k), 0,
2804		   group ? rum_group_key_del_cb : rum_pair_key_del_cb);
2805}
2806
2807static int
2808rum_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2809    const struct ieee80211_bpf_params *params)
2810{
2811	struct rum_softc *sc = ni->ni_ic->ic_softc;
2812	int ret;
2813
2814	RUM_LOCK(sc);
2815	/* prevent management frames from being sent if we're not ready */
2816	if (!sc->sc_running) {
2817		ret = ENETDOWN;
2818		goto bad;
2819	}
2820	if (sc->tx_nfree < RUM_TX_MINFREE) {
2821		ret = EIO;
2822		goto bad;
2823	}
2824
2825	if (params == NULL) {
2826		/*
2827		 * Legacy path; interpret frame contents to decide
2828		 * precisely how to send the frame.
2829		 */
2830		if ((ret = rum_tx_mgt(sc, m, ni)) != 0)
2831			goto bad;
2832	} else {
2833		/*
2834		 * Caller supplied explicit parameters to use in
2835		 * sending the frame.
2836		 */
2837		if ((ret = rum_tx_raw(sc, m, ni, params)) != 0)
2838			goto bad;
2839	}
2840	RUM_UNLOCK(sc);
2841
2842	return 0;
2843bad:
2844	RUM_UNLOCK(sc);
2845	m_freem(m);
2846	return ret;
2847}
2848
2849static void
2850rum_ratectl_start(struct rum_softc *sc, struct ieee80211_node *ni)
2851{
2852	struct ieee80211vap *vap = ni->ni_vap;
2853	struct rum_vap *rvp = RUM_VAP(vap);
2854
2855	/* clear statistic registers (STA_CSR0 to STA_CSR5) */
2856	rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
2857
2858	usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp);
2859}
2860
2861static void
2862rum_ratectl_timeout(void *arg)
2863{
2864	struct rum_vap *rvp = arg;
2865	struct ieee80211vap *vap = &rvp->vap;
2866	struct ieee80211com *ic = vap->iv_ic;
2867
2868	ieee80211_runtask(ic, &rvp->ratectl_task);
2869}
2870
2871static void
2872rum_ratectl_task(void *arg, int pending)
2873{
2874	struct rum_vap *rvp = arg;
2875	struct ieee80211vap *vap = &rvp->vap;
2876	struct rum_softc *sc = vap->iv_ic->ic_softc;
2877	struct ieee80211_node *ni;
2878	int ok[3], fail;
2879	int sum, success, retrycnt;
2880
2881	RUM_LOCK(sc);
2882	/* read and clear statistic registers (STA_CSR0 to STA_CSR5) */
2883	rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof(sc->sta));
2884
2885	ok[0] = (le32toh(sc->sta[4]) & 0xffff);	/* TX ok w/o retry */
2886	ok[1] = (le32toh(sc->sta[4]) >> 16);	/* TX ok w/ one retry */
2887	ok[2] = (le32toh(sc->sta[5]) & 0xffff);	/* TX ok w/ multiple retries */
2888	fail =  (le32toh(sc->sta[5]) >> 16);	/* TX retry-fail count */
2889
2890	success = ok[0] + ok[1] + ok[2];
2891	sum = success + fail;
2892	/* XXX at least */
2893	retrycnt = ok[1] + ok[2] * 2 + fail * (rvp->maxretry + 1);
2894
2895	if (sum != 0) {
2896		ni = ieee80211_ref_node(vap->iv_bss);
2897		ieee80211_ratectl_tx_update(vap, ni, &sum, &ok, &retrycnt);
2898		(void) ieee80211_ratectl_rate(ni, NULL, 0);
2899		ieee80211_free_node(ni);
2900	}
2901
2902	/* count TX retry-fail as Tx errors */
2903	if_inc_counter(vap->iv_ifp, IFCOUNTER_OERRORS, fail);
2904
2905	usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp);
2906	RUM_UNLOCK(sc);
2907}
2908
2909static void
2910rum_scan_start(struct ieee80211com *ic)
2911{
2912	struct rum_softc *sc = ic->ic_softc;
2913
2914	RUM_LOCK(sc);
2915	rum_abort_tsf_sync(sc);
2916	rum_set_bssid(sc, ieee80211broadcastaddr);
2917	RUM_UNLOCK(sc);
2918
2919}
2920
2921static void
2922rum_scan_end(struct ieee80211com *ic)
2923{
2924	struct rum_softc *sc = ic->ic_softc;
2925
2926	RUM_LOCK(sc);
2927	if (ic->ic_opmode != IEEE80211_M_AHDEMO)
2928		rum_enable_tsf_sync(sc);
2929	else
2930		rum_enable_tsf(sc);
2931	rum_set_bssid(sc, sc->sc_bssid);
2932	RUM_UNLOCK(sc);
2933
2934}
2935
2936static void
2937rum_set_channel(struct ieee80211com *ic)
2938{
2939	struct rum_softc *sc = ic->ic_softc;
2940
2941	RUM_LOCK(sc);
2942	rum_set_chan(sc, ic->ic_curchan);
2943	RUM_UNLOCK(sc);
2944}
2945
2946static int
2947rum_get_rssi(struct rum_softc *sc, uint8_t raw)
2948{
2949	struct ieee80211com *ic = &sc->sc_ic;
2950	int lna, agc, rssi;
2951
2952	lna = (raw >> 5) & 0x3;
2953	agc = raw & 0x1f;
2954
2955	if (lna == 0) {
2956		/*
2957		 * No RSSI mapping
2958		 *
2959		 * NB: Since RSSI is relative to noise floor, -1 is
2960		 *     adequate for caller to know error happened.
2961		 */
2962		return -1;
2963	}
2964
2965	rssi = (2 * agc) - RT2573_NOISE_FLOOR;
2966
2967	if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) {
2968		rssi += sc->rssi_2ghz_corr;
2969
2970		if (lna == 1)
2971			rssi -= 64;
2972		else if (lna == 2)
2973			rssi -= 74;
2974		else if (lna == 3)
2975			rssi -= 90;
2976	} else {
2977		rssi += sc->rssi_5ghz_corr;
2978
2979		if (!sc->ext_5ghz_lna && lna != 1)
2980			rssi += 4;
2981
2982		if (lna == 1)
2983			rssi -= 64;
2984		else if (lna == 2)
2985			rssi -= 86;
2986		else if (lna == 3)
2987			rssi -= 100;
2988	}
2989	return rssi;
2990}
2991
2992static int
2993rum_pause(struct rum_softc *sc, int timeout)
2994{
2995
2996	usb_pause_mtx(&sc->sc_mtx, timeout);
2997	return (0);
2998}
2999
3000static device_method_t rum_methods[] = {
3001	/* Device interface */
3002	DEVMETHOD(device_probe,		rum_match),
3003	DEVMETHOD(device_attach,	rum_attach),
3004	DEVMETHOD(device_detach,	rum_detach),
3005	DEVMETHOD_END
3006};
3007
3008static driver_t rum_driver = {
3009	.name = "rum",
3010	.methods = rum_methods,
3011	.size = sizeof(struct rum_softc),
3012};
3013
3014static devclass_t rum_devclass;
3015
3016DRIVER_MODULE(rum, uhub, rum_driver, rum_devclass, NULL, 0);
3017MODULE_DEPEND(rum, wlan, 1, 1, 1);
3018MODULE_DEPEND(rum, usb, 1, 1, 1);
3019MODULE_VERSION(rum, 1);
3020USB_PNP_HOST_INFO(rum_devs);
3021