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