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