1 /* $FreeBSD: src/sys/dev/usb/wlan/if_rum.c,v 1.51 2013/03/22 02:25:33 svnexp Exp $ */
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>
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.
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.
22 * Ralink Technology RT2501USB/RT2601USB chipset driver
23 * http://www.ralinktech.com.tw/
28 #include <sys/param.h>
29 #include <sys/sockio.h>
30 #include <sys/sysctl.h>
33 #include <sys/kernel.h>
34 #include <sys/socket.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
39 #include <sys/endian.h>
45 #include <net/if_arp.h>
46 #include <net/ethernet.h>
47 #include <net/if_dl.h>
48 #include <net/if_media.h>
49 #include <net/if_types.h>
50 #include <net/ifq_var.h>
53 #include <netinet/in.h>
54 #include <netinet/in_systm.h>
55 #include <netinet/in_var.h>
56 #include <netinet/if_ether.h>
57 #include <netinet/ip.h>
60 #include <netproto/802_11/ieee80211_var.h>
61 #include <netproto/802_11/ieee80211_regdomain.h>
62 #include <netproto/802_11/ieee80211_radiotap.h>
63 #include <netproto/802_11/ieee80211_ratectl.h>
65 #include <bus/u4b/usb.h>
66 #include <bus/u4b/usbdi.h>
67 #include <bus/u4b/usbdevs.h>
69 #define USB_DEBUG_VAR rum_debug
70 #include <bus/u4b/usb_debug.h>
72 #include <bus/u4b/wlan/if_rumreg.h>
73 #include <bus/u4b/wlan/if_rumvar.h>
74 #include <bus/u4b/wlan/if_rumfw.h>
77 static int rum_debug = 0;
79 static SYSCTL_NODE(_hw_usb, OID_AUTO, rum, CTLFLAG_RW, 0, "USB rum");
80 SYSCTL_INT(_hw_usb_rum, OID_AUTO, debug, CTLFLAG_RW, &rum_debug, 0,
84 #define N(a) ((int)(sizeof (a) / sizeof ((a)[0])))
86 static const STRUCT_USB_HOST_ID rum_devs[] = {
87 #define RUM_DEV(v,p) { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
88 RUM_DEV(ABOCOM, HWU54DM),
89 RUM_DEV(ABOCOM, RT2573_2),
90 RUM_DEV(ABOCOM, RT2573_3),
91 RUM_DEV(ABOCOM, RT2573_4),
92 RUM_DEV(ABOCOM, WUG2700),
93 RUM_DEV(AMIT, CGWLUSB2GO),
94 RUM_DEV(ASUS, RT2573_1),
95 RUM_DEV(ASUS, RT2573_2),
96 RUM_DEV(BELKIN, F5D7050A),
97 RUM_DEV(BELKIN, F5D9050V3),
98 RUM_DEV(CISCOLINKSYS, WUSB54GC),
99 RUM_DEV(CISCOLINKSYS, WUSB54GR),
100 RUM_DEV(CONCEPTRONIC2, C54RU2),
101 RUM_DEV(COREGA, CGWLUSB2GL),
102 RUM_DEV(COREGA, CGWLUSB2GPX),
103 RUM_DEV(DICKSMITH, CWD854F),
104 RUM_DEV(DICKSMITH, RT2573),
105 RUM_DEV(EDIMAX, EW7318USG),
106 RUM_DEV(DLINK2, DWLG122C1),
107 RUM_DEV(DLINK2, WUA1340),
108 RUM_DEV(DLINK2, DWA111),
109 RUM_DEV(DLINK2, DWA110),
110 RUM_DEV(GIGABYTE, GNWB01GS),
111 RUM_DEV(GIGABYTE, GNWI05GS),
112 RUM_DEV(GIGASET, RT2573),
113 RUM_DEV(GOODWAY, RT2573),
114 RUM_DEV(GUILLEMOT, HWGUSB254LB),
115 RUM_DEV(GUILLEMOT, HWGUSB254V2AP),
116 RUM_DEV(HUAWEI3COM, WUB320G),
117 RUM_DEV(MELCO, G54HP),
118 RUM_DEV(MELCO, SG54HP),
119 RUM_DEV(MELCO, SG54HG),
120 RUM_DEV(MELCO, WLIUCG),
121 RUM_DEV(MELCO, WLRUCG),
122 RUM_DEV(MELCO, WLRUCGAOSS),
123 RUM_DEV(MSI, RT2573_1),
124 RUM_DEV(MSI, RT2573_2),
125 RUM_DEV(MSI, RT2573_3),
126 RUM_DEV(MSI, RT2573_4),
127 RUM_DEV(NOVATECH, RT2573),
128 RUM_DEV(PLANEX2, GWUS54HP),
129 RUM_DEV(PLANEX2, GWUS54MINI2),
130 RUM_DEV(PLANEX2, GWUSMM),
131 RUM_DEV(QCOM, RT2573),
132 RUM_DEV(QCOM, RT2573_2),
133 RUM_DEV(QCOM, RT2573_3),
134 RUM_DEV(RALINK, RT2573),
135 RUM_DEV(RALINK, RT2573_2),
136 RUM_DEV(RALINK, RT2671),
137 RUM_DEV(SITECOMEU, WL113R2),
138 RUM_DEV(SITECOMEU, WL172),
139 RUM_DEV(SPARKLAN, RT2573),
140 RUM_DEV(SURECOM, RT2573),
144 static device_probe_t rum_match;
145 static device_attach_t rum_attach;
146 static device_detach_t rum_detach;
148 static usb_callback_t rum_bulk_read_callback;
149 static usb_callback_t rum_bulk_write_callback;
151 static usb_error_t rum_do_request(struct rum_softc *sc,
152 struct usb_device_request *req, void *data);
153 static struct ieee80211vap *rum_vap_create(struct ieee80211com *,
154 const char [IFNAMSIZ], int,
155 enum ieee80211_opmode,
156 int, const uint8_t [IEEE80211_ADDR_LEN],
157 const uint8_t [IEEE80211_ADDR_LEN]);
158 static void rum_vap_delete(struct ieee80211vap *);
159 static void rum_tx_free(struct rum_tx_data *, int);
160 static void rum_setup_tx_list(struct rum_softc *);
161 static void rum_unsetup_tx_list(struct rum_softc *);
162 static void rum_newassoc(struct ieee80211_node *, int);
163 static int rum_newstate(struct ieee80211vap *,
164 enum ieee80211_state, int);
165 static void rum_setup_tx_desc(struct rum_softc *,
166 struct rum_tx_desc *, uint32_t, uint16_t, int,
168 static int rum_tx_mgt(struct rum_softc *, struct mbuf *,
169 struct ieee80211_node *);
170 static int rum_tx_raw(struct rum_softc *, struct mbuf *,
171 struct ieee80211_node *,
172 const struct ieee80211_bpf_params *);
173 static int rum_tx_data(struct rum_softc *, struct mbuf *,
174 struct ieee80211_node *);
175 static void rum_start_locked(struct ifnet *);
176 static void rum_start(struct ifnet *, struct ifaltq_subque *);
177 static int rum_ioctl(struct ifnet *, u_long, caddr_t,
179 static void rum_eeprom_read(struct rum_softc *, uint16_t, void *,
181 static uint32_t rum_read(struct rum_softc *, uint16_t);
182 static void rum_read_multi(struct rum_softc *, uint16_t, void *,
184 static usb_error_t rum_write(struct rum_softc *, uint16_t, uint32_t);
185 static usb_error_t rum_write_multi(struct rum_softc *, uint16_t, void *,
187 static void rum_bbp_write(struct rum_softc *, uint8_t, uint8_t);
188 static uint8_t rum_bbp_read(struct rum_softc *, uint8_t);
189 static void rum_rf_write(struct rum_softc *, uint8_t, uint32_t);
190 static void rum_select_antenna(struct rum_softc *);
191 static void rum_enable_mrr(struct rum_softc *);
192 static void rum_set_txpreamble(struct rum_softc *);
193 static void rum_set_basicrates(struct rum_softc *);
194 static void rum_select_band(struct rum_softc *,
195 struct ieee80211_channel *);
196 static void rum_set_chan(struct rum_softc *,
197 struct ieee80211_channel *);
198 static void rum_enable_tsf_sync(struct rum_softc *);
199 static void rum_enable_tsf(struct rum_softc *);
200 static void rum_update_slot(struct ifnet *);
201 static void rum_set_bssid(struct rum_softc *, const uint8_t *);
202 static void rum_set_macaddr(struct rum_softc *, const uint8_t *);
203 static void rum_update_mcast(struct ifnet *);
204 static void rum_update_promisc(struct ifnet *);
205 static void rum_setpromisc(struct rum_softc *);
206 static const char *rum_get_rf(int);
207 static void rum_read_eeprom(struct rum_softc *);
208 static int rum_bbp_init(struct rum_softc *);
209 static void rum_init_locked(struct rum_softc *);
210 static void rum_init(void *);
211 static void rum_stop(struct rum_softc *);
212 static void rum_load_microcode(struct rum_softc *, const uint8_t *,
214 static void rum_prepare_beacon(struct rum_softc *,
215 struct ieee80211vap *);
216 static int rum_raw_xmit(struct ieee80211_node *, struct mbuf *,
217 const struct ieee80211_bpf_params *);
218 static void rum_scan_start(struct ieee80211com *);
219 static void rum_scan_end(struct ieee80211com *);
220 static void rum_set_channel(struct ieee80211com *);
221 static int rum_get_rssi(struct rum_softc *, uint8_t);
222 static void rum_ratectl_start(struct rum_softc *,
223 struct ieee80211_node *);
224 static void rum_ratectl_timeout(void *);
225 static void rum_ratectl_task(void *, int);
226 static int rum_pause(struct rum_softc *, int);
228 static const struct {
232 { RT2573_TXRX_CSR0, 0x025fb032 },
233 { RT2573_TXRX_CSR1, 0x9eaa9eaf },
234 { RT2573_TXRX_CSR2, 0x8a8b8c8d },
235 { RT2573_TXRX_CSR3, 0x00858687 },
236 { RT2573_TXRX_CSR7, 0x2e31353b },
237 { RT2573_TXRX_CSR8, 0x2a2a2a2c },
238 { RT2573_TXRX_CSR15, 0x0000000f },
239 { RT2573_MAC_CSR6, 0x00000fff },
240 { RT2573_MAC_CSR8, 0x016c030a },
241 { RT2573_MAC_CSR10, 0x00000718 },
242 { RT2573_MAC_CSR12, 0x00000004 },
243 { RT2573_MAC_CSR13, 0x00007f00 },
244 { RT2573_SEC_CSR0, 0x00000000 },
245 { RT2573_SEC_CSR1, 0x00000000 },
246 { RT2573_SEC_CSR5, 0x00000000 },
247 { RT2573_PHY_CSR1, 0x000023b0 },
248 { RT2573_PHY_CSR5, 0x00040a06 },
249 { RT2573_PHY_CSR6, 0x00080606 },
250 { RT2573_PHY_CSR7, 0x00000408 },
251 { RT2573_AIFSN_CSR, 0x00002273 },
252 { RT2573_CWMIN_CSR, 0x00002344 },
253 { RT2573_CWMAX_CSR, 0x000034aa }
256 static const struct {
288 static const struct rfprog {
290 uint32_t r1, r2, r3, r4;
292 { 1, 0x00b03, 0x001e1, 0x1a014, 0x30282 },
293 { 2, 0x00b03, 0x001e1, 0x1a014, 0x30287 },
294 { 3, 0x00b03, 0x001e2, 0x1a014, 0x30282 },
295 { 4, 0x00b03, 0x001e2, 0x1a014, 0x30287 },
296 { 5, 0x00b03, 0x001e3, 0x1a014, 0x30282 },
297 { 6, 0x00b03, 0x001e3, 0x1a014, 0x30287 },
298 { 7, 0x00b03, 0x001e4, 0x1a014, 0x30282 },
299 { 8, 0x00b03, 0x001e4, 0x1a014, 0x30287 },
300 { 9, 0x00b03, 0x001e5, 0x1a014, 0x30282 },
301 { 10, 0x00b03, 0x001e5, 0x1a014, 0x30287 },
302 { 11, 0x00b03, 0x001e6, 0x1a014, 0x30282 },
303 { 12, 0x00b03, 0x001e6, 0x1a014, 0x30287 },
304 { 13, 0x00b03, 0x001e7, 0x1a014, 0x30282 },
305 { 14, 0x00b03, 0x001e8, 0x1a014, 0x30284 },
307 { 34, 0x00b03, 0x20266, 0x36014, 0x30282 },
308 { 38, 0x00b03, 0x20267, 0x36014, 0x30284 },
309 { 42, 0x00b03, 0x20268, 0x36014, 0x30286 },
310 { 46, 0x00b03, 0x20269, 0x36014, 0x30288 },
312 { 36, 0x00b03, 0x00266, 0x26014, 0x30288 },
313 { 40, 0x00b03, 0x00268, 0x26014, 0x30280 },
314 { 44, 0x00b03, 0x00269, 0x26014, 0x30282 },
315 { 48, 0x00b03, 0x0026a, 0x26014, 0x30284 },
316 { 52, 0x00b03, 0x0026b, 0x26014, 0x30286 },
317 { 56, 0x00b03, 0x0026c, 0x26014, 0x30288 },
318 { 60, 0x00b03, 0x0026e, 0x26014, 0x30280 },
319 { 64, 0x00b03, 0x0026f, 0x26014, 0x30282 },
321 { 100, 0x00b03, 0x0028a, 0x2e014, 0x30280 },
322 { 104, 0x00b03, 0x0028b, 0x2e014, 0x30282 },
323 { 108, 0x00b03, 0x0028c, 0x2e014, 0x30284 },
324 { 112, 0x00b03, 0x0028d, 0x2e014, 0x30286 },
325 { 116, 0x00b03, 0x0028e, 0x2e014, 0x30288 },
326 { 120, 0x00b03, 0x002a0, 0x2e014, 0x30280 },
327 { 124, 0x00b03, 0x002a1, 0x2e014, 0x30282 },
328 { 128, 0x00b03, 0x002a2, 0x2e014, 0x30284 },
329 { 132, 0x00b03, 0x002a3, 0x2e014, 0x30286 },
330 { 136, 0x00b03, 0x002a4, 0x2e014, 0x30288 },
331 { 140, 0x00b03, 0x002a6, 0x2e014, 0x30280 },
333 { 149, 0x00b03, 0x002a8, 0x2e014, 0x30287 },
334 { 153, 0x00b03, 0x002a9, 0x2e014, 0x30289 },
335 { 157, 0x00b03, 0x002ab, 0x2e014, 0x30281 },
336 { 161, 0x00b03, 0x002ac, 0x2e014, 0x30283 },
337 { 165, 0x00b03, 0x002ad, 0x2e014, 0x30285 }
339 { 1, 0x00b33, 0x011e1, 0x1a014, 0x30282 },
340 { 2, 0x00b33, 0x011e1, 0x1a014, 0x30287 },
341 { 3, 0x00b33, 0x011e2, 0x1a014, 0x30282 },
342 { 4, 0x00b33, 0x011e2, 0x1a014, 0x30287 },
343 { 5, 0x00b33, 0x011e3, 0x1a014, 0x30282 },
344 { 6, 0x00b33, 0x011e3, 0x1a014, 0x30287 },
345 { 7, 0x00b33, 0x011e4, 0x1a014, 0x30282 },
346 { 8, 0x00b33, 0x011e4, 0x1a014, 0x30287 },
347 { 9, 0x00b33, 0x011e5, 0x1a014, 0x30282 },
348 { 10, 0x00b33, 0x011e5, 0x1a014, 0x30287 },
349 { 11, 0x00b33, 0x011e6, 0x1a014, 0x30282 },
350 { 12, 0x00b33, 0x011e6, 0x1a014, 0x30287 },
351 { 13, 0x00b33, 0x011e7, 0x1a014, 0x30282 },
352 { 14, 0x00b33, 0x011e8, 0x1a014, 0x30284 },
354 { 34, 0x00b33, 0x01266, 0x26014, 0x30282 },
355 { 38, 0x00b33, 0x01267, 0x26014, 0x30284 },
356 { 42, 0x00b33, 0x01268, 0x26014, 0x30286 },
357 { 46, 0x00b33, 0x01269, 0x26014, 0x30288 },
359 { 36, 0x00b33, 0x01266, 0x26014, 0x30288 },
360 { 40, 0x00b33, 0x01268, 0x26014, 0x30280 },
361 { 44, 0x00b33, 0x01269, 0x26014, 0x30282 },
362 { 48, 0x00b33, 0x0126a, 0x26014, 0x30284 },
363 { 52, 0x00b33, 0x0126b, 0x26014, 0x30286 },
364 { 56, 0x00b33, 0x0126c, 0x26014, 0x30288 },
365 { 60, 0x00b33, 0x0126e, 0x26014, 0x30280 },
366 { 64, 0x00b33, 0x0126f, 0x26014, 0x30282 },
368 { 100, 0x00b33, 0x0128a, 0x2e014, 0x30280 },
369 { 104, 0x00b33, 0x0128b, 0x2e014, 0x30282 },
370 { 108, 0x00b33, 0x0128c, 0x2e014, 0x30284 },
371 { 112, 0x00b33, 0x0128d, 0x2e014, 0x30286 },
372 { 116, 0x00b33, 0x0128e, 0x2e014, 0x30288 },
373 { 120, 0x00b33, 0x012a0, 0x2e014, 0x30280 },
374 { 124, 0x00b33, 0x012a1, 0x2e014, 0x30282 },
375 { 128, 0x00b33, 0x012a2, 0x2e014, 0x30284 },
376 { 132, 0x00b33, 0x012a3, 0x2e014, 0x30286 },
377 { 136, 0x00b33, 0x012a4, 0x2e014, 0x30288 },
378 { 140, 0x00b33, 0x012a6, 0x2e014, 0x30280 },
380 { 149, 0x00b33, 0x012a8, 0x2e014, 0x30287 },
381 { 153, 0x00b33, 0x012a9, 0x2e014, 0x30289 },
382 { 157, 0x00b33, 0x012ab, 0x2e014, 0x30281 },
383 { 161, 0x00b33, 0x012ac, 0x2e014, 0x30283 },
384 { 165, 0x00b33, 0x012ad, 0x2e014, 0x30285 }
387 static const struct usb_config rum_config[RUM_N_TRANSFER] = {
390 .endpoint = UE_ADDR_ANY,
391 .direction = UE_DIR_OUT,
392 .bufsize = (MCLBYTES + RT2573_TX_DESC_SIZE + 8),
393 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
394 .callback = rum_bulk_write_callback,
395 .timeout = 5000, /* ms */
399 .endpoint = UE_ADDR_ANY,
400 .direction = UE_DIR_IN,
401 .bufsize = (MCLBYTES + RT2573_RX_DESC_SIZE),
402 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
403 .callback = rum_bulk_read_callback,
408 rum_match(device_t self)
410 struct usb_attach_arg *uaa = device_get_ivars(self);
412 if (uaa->usb_mode != USB_MODE_HOST)
414 if (uaa->info.bConfigIndex != 0)
416 if (uaa->info.bIfaceIndex != RT2573_IFACE_INDEX)
419 return (usbd_lookup_id_by_uaa(rum_devs, sizeof(rum_devs), uaa));
423 rum_attach(device_t self)
425 struct usb_attach_arg *uaa = device_get_ivars(self);
426 struct rum_softc *sc = device_get_softc(self);
427 struct ieee80211com *ic;
429 uint8_t iface_index, bands;
433 wlan_serialize_enter();
434 device_set_usb_desc(self);
435 sc->sc_udev = uaa->device;
438 lockinit(&sc->sc_lock, device_get_nameunit(self), 0, LK_CANRECURSE);
440 iface_index = RT2573_IFACE_INDEX;
441 error = usbd_transfer_setup(uaa->device, &iface_index,
442 sc->sc_xfer, rum_config, RUM_N_TRANSFER, sc, &sc->sc_lock);
444 device_printf(self, "could not allocate USB transfers, "
445 "err=%s\n", usbd_errstr(error));
450 /* retrieve RT2573 rev. no */
451 for (ntries = 0; ntries < 100; ntries++) {
452 if ((tmp = rum_read(sc, RT2573_MAC_CSR0)) != 0)
454 if (rum_pause(sc, hz / 100))
458 device_printf(sc->sc_dev, "timeout waiting for chip to settle\n");
463 /* retrieve MAC address and various other things from EEPROM */
466 device_printf(sc->sc_dev, "MAC/BBP RT2573 (rev 0x%05x), RF %s\n",
467 tmp, rum_get_rf(sc->rf_rev));
469 rum_load_microcode(sc, rt2573_ucode, sizeof(rt2573_ucode));
472 ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
474 device_printf(sc->sc_dev, "can not if_alloc()\n");
480 if_initname(ifp, "rum", device_get_unit(sc->sc_dev));
481 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
482 ifp->if_init = rum_init;
483 ifp->if_ioctl = rum_ioctl;
484 ifp->if_start = rum_start;
485 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
486 #if 0 /* XXX swildner: see c3d4131842e47b168d93a0650d58d425ebeef789 */
487 ifq_set_ready(&ifp->if_snd);
491 ic->ic_opmode = IEEE80211_M_STA;
492 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
494 /* set device capabilities */
496 IEEE80211_C_STA /* station mode supported */
497 | IEEE80211_C_IBSS /* IBSS mode supported */
498 | IEEE80211_C_MONITOR /* monitor mode supported */
499 | IEEE80211_C_HOSTAP /* HostAp mode supported */
500 | IEEE80211_C_TXPMGT /* tx power management */
501 | IEEE80211_C_SHPREAMBLE /* short preamble supported */
502 | IEEE80211_C_SHSLOT /* short slot time supported */
503 | IEEE80211_C_BGSCAN /* bg scanning supported */
504 | IEEE80211_C_WPA /* 802.11i */
508 setbit(&bands, IEEE80211_MODE_11B);
509 setbit(&bands, IEEE80211_MODE_11G);
510 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_5226)
511 setbit(&bands, IEEE80211_MODE_11A);
512 ieee80211_init_channels(ic, NULL, &bands);
514 ieee80211_ifattach(ic, sc->sc_bssid);
515 ic->ic_newassoc = rum_newassoc;
516 ic->ic_update_promisc = rum_update_promisc;
517 ic->ic_raw_xmit = rum_raw_xmit;
518 ic->ic_scan_start = rum_scan_start;
519 ic->ic_scan_end = rum_scan_end;
520 ic->ic_set_channel = rum_set_channel;
522 ic->ic_vap_create = rum_vap_create;
523 ic->ic_vap_delete = rum_vap_delete;
524 ic->ic_update_mcast = rum_update_mcast;
526 ieee80211_radiotap_attach(ic,
527 &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
528 RT2573_TX_RADIOTAP_PRESENT,
529 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
530 RT2573_RX_RADIOTAP_PRESENT);
533 ieee80211_announce(ic);
535 wlan_serialize_exit();
539 wlan_serialize_exit();
541 return (ENXIO); /* failure */
545 rum_detach(device_t self)
547 struct rum_softc *sc = device_get_softc(self);
548 struct ifnet *ifp = sc->sc_ifp;
549 struct ieee80211com *ic;
551 wlan_serialize_enter();
552 /* Prevent further ioctls */
555 /* stop all USB transfers */
556 usbd_transfer_unsetup(sc->sc_xfer, RUM_N_TRANSFER);
558 /* free TX list, if any */
560 rum_unsetup_tx_list(sc);
565 ieee80211_ifdetach(ic);
568 lockuninit(&sc->sc_lock);
569 wlan_serialize_exit();
574 rum_do_request(struct rum_softc *sc,
575 struct usb_device_request *req, void *data)
581 err = usbd_do_request_flags(sc->sc_udev, &sc->sc_lock,
582 req, data, 0, NULL, 250 /* ms */);
586 DPRINTFN(1, "Control request failed, %s (retrying)\n",
588 if (rum_pause(sc, hz / 100))
594 static struct ieee80211vap *
595 rum_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
596 enum ieee80211_opmode opmode, int flags,
597 const uint8_t bssid[IEEE80211_ADDR_LEN],
598 const uint8_t mac[IEEE80211_ADDR_LEN])
600 struct rum_softc *sc = ic->ic_ifp->if_softc;
602 struct ieee80211vap *vap;
604 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */
606 rvp = (struct rum_vap *) kmalloc(sizeof(struct rum_vap),
607 M_80211_VAP, M_INTWAIT | M_ZERO);
609 /* enable s/w bmiss handling for sta mode */
610 ieee80211_vap_setup(ic, vap, name, unit, opmode,
611 flags | IEEE80211_CLONE_NOBEACONS, bssid, mac);
613 /* override state transition machine */
614 rvp->newstate = vap->iv_newstate;
615 vap->iv_newstate = rum_newstate;
617 usb_callout_init_mtx(&rvp->ratectl_ch, &sc->sc_lock, 0);
618 TASK_INIT(&rvp->ratectl_task, 0, rum_ratectl_task, rvp);
619 ieee80211_ratectl_init(vap);
620 ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */);
622 ieee80211_vap_attach(vap, ieee80211_media_change, ieee80211_media_status);
623 ic->ic_opmode = opmode;
628 rum_vap_delete(struct ieee80211vap *vap)
630 struct rum_vap *rvp = RUM_VAP(vap);
631 struct ieee80211com *ic = vap->iv_ic;
633 usb_callout_drain(&rvp->ratectl_ch);
634 ieee80211_draintask(ic, &rvp->ratectl_task);
635 ieee80211_ratectl_deinit(vap);
636 ieee80211_vap_detach(vap);
637 kfree(rvp, M_80211_VAP);
641 rum_tx_free(struct rum_tx_data *data, int txerr)
643 struct rum_softc *sc = data->sc;
645 if (data->m != NULL) {
646 if (data->m->m_flags & M_TXCB)
647 ieee80211_process_callback(data->ni, data->m,
648 txerr ? ETIMEDOUT : 0);
652 ieee80211_free_node(data->ni);
655 STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
660 rum_setup_tx_list(struct rum_softc *sc)
662 struct rum_tx_data *data;
666 STAILQ_INIT(&sc->tx_q);
667 STAILQ_INIT(&sc->tx_free);
669 for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
670 data = &sc->tx_data[i];
673 STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
679 rum_unsetup_tx_list(struct rum_softc *sc)
681 struct rum_tx_data *data;
684 /* make sure any subsequent use of the queues will fail */
686 STAILQ_INIT(&sc->tx_q);
687 STAILQ_INIT(&sc->tx_free);
689 /* free up all node references and mbufs */
690 for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
691 data = &sc->tx_data[i];
693 if (data->m != NULL) {
697 if (data->ni != NULL) {
698 ieee80211_free_node(data->ni);
705 rum_newassoc(struct ieee80211_node *ni, int isnew)
707 ieee80211_ratectl_node_deinit(ni);
708 ieee80211_ratectl_node_init(ni);
712 rum_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
714 struct rum_vap *rvp = RUM_VAP(vap);
715 struct ieee80211com *ic = vap->iv_ic;
716 struct rum_softc *sc = ic->ic_ifp->if_softc;
717 const struct ieee80211_txparam *tp;
718 enum ieee80211_state ostate;
719 struct ieee80211_node *ni;
722 ostate = vap->iv_state;
723 DPRINTF("%s -> %s\n",
724 ieee80211_state_name[ostate],
725 ieee80211_state_name[nstate]);
727 #if 0 /* XXX swildner: needed? */
728 IEEE80211_UNLOCK(ic);
731 usb_callout_stop(&rvp->ratectl_ch);
734 case IEEE80211_S_INIT:
735 if (ostate == IEEE80211_S_RUN) {
736 /* abort TSF synchronization */
737 tmp = rum_read(sc, RT2573_TXRX_CSR9);
738 rum_write(sc, RT2573_TXRX_CSR9, tmp & ~0x00ffffff);
742 case IEEE80211_S_RUN:
743 ni = ieee80211_ref_node(vap->iv_bss);
745 if (vap->iv_opmode != IEEE80211_M_MONITOR) {
746 if (ic->ic_bsschan == IEEE80211_CHAN_ANYC) {
748 #if 0 /* XXX swildner: needed? */
751 ieee80211_free_node(ni);
754 rum_update_slot(ic->ic_ifp);
756 rum_set_txpreamble(sc);
757 rum_set_basicrates(sc);
758 IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid);
759 rum_set_bssid(sc, sc->sc_bssid);
762 if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
763 vap->iv_opmode == IEEE80211_M_IBSS)
764 rum_prepare_beacon(sc, vap);
766 if (vap->iv_opmode != IEEE80211_M_MONITOR)
767 rum_enable_tsf_sync(sc);
771 /* enable automatic rate adaptation */
772 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
773 if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
774 rum_ratectl_start(sc, ni);
775 ieee80211_free_node(ni);
781 #if 0 /* XXX swildner: needed? */
784 return (rvp->newstate(vap, nstate, arg));
788 rum_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
790 struct rum_softc *sc = usbd_xfer_softc(xfer);
791 struct ifnet *ifp = sc->sc_ifp;
792 struct ieee80211vap *vap;
793 struct rum_tx_data *data;
795 struct usb_page_cache *pc;
799 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
801 switch (USB_GET_STATE(xfer)) {
802 case USB_ST_TRANSFERRED:
803 DPRINTFN(11, "transfer complete, %d bytes\n", actlen);
806 data = usbd_xfer_get_priv(xfer);
807 rum_tx_free(data, 0);
808 usbd_xfer_set_priv(xfer, NULL);
811 ifq_clr_oactive(&ifp->if_snd);
816 data = STAILQ_FIRST(&sc->tx_q);
818 STAILQ_REMOVE_HEAD(&sc->tx_q, next);
821 if (m->m_pkthdr.len > (int)(MCLBYTES + RT2573_TX_DESC_SIZE)) {
822 DPRINTFN(0, "data overflow, %u bytes\n",
824 m->m_pkthdr.len = (MCLBYTES + RT2573_TX_DESC_SIZE);
826 pc = usbd_xfer_get_frame(xfer, 0);
827 usbd_copy_in(pc, 0, &data->desc, RT2573_TX_DESC_SIZE);
828 usbd_m_copy_in(pc, RT2573_TX_DESC_SIZE, m, 0,
831 vap = data->ni->ni_vap;
832 if (ieee80211_radiotap_active_vap(vap)) {
833 struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
836 tap->wt_rate = data->rate;
837 tap->wt_antenna = sc->tx_ant;
839 ieee80211_radiotap_tx(vap, m);
842 /* align end on a 4-bytes boundary */
843 len = (RT2573_TX_DESC_SIZE + m->m_pkthdr.len + 3) & ~3;
847 DPRINTFN(11, "sending frame len=%u xferlen=%u\n",
848 m->m_pkthdr.len, len);
850 usbd_xfer_set_frame_len(xfer, 0, len);
851 usbd_xfer_set_priv(xfer, data);
853 usbd_transfer_submit(xfer);
855 rum_start_locked(ifp);
859 DPRINTFN(11, "transfer error, %s\n",
863 data = usbd_xfer_get_priv(xfer);
865 rum_tx_free(data, error);
866 usbd_xfer_set_priv(xfer, NULL);
869 if (error != USB_ERR_CANCELLED) {
870 if (error == USB_ERR_TIMEOUT)
871 device_printf(sc->sc_dev, "device timeout\n");
874 * Try to clear stall first, also if other
875 * errors occur, hence clearing stall
876 * introduces a 50 ms delay:
878 usbd_xfer_set_stall(xfer);
886 rum_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
888 struct rum_softc *sc = usbd_xfer_softc(xfer);
889 struct ifnet *ifp = sc->sc_ifp;
890 struct ieee80211com *ic = ifp->if_l2com;
891 struct ieee80211_node *ni;
892 struct mbuf *m = NULL;
893 struct usb_page_cache *pc;
898 usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
900 switch (USB_GET_STATE(xfer)) {
901 case USB_ST_TRANSFERRED:
903 DPRINTFN(15, "rx done, actlen=%d\n", len);
905 if (len < (int)(RT2573_RX_DESC_SIZE + IEEE80211_MIN_LEN)) {
906 DPRINTF("%s: xfer too short %d\n",
907 device_get_nameunit(sc->sc_dev), len);
912 len -= RT2573_RX_DESC_SIZE;
913 pc = usbd_xfer_get_frame(xfer, 0);
914 usbd_copy_out(pc, 0, &sc->sc_rx_desc, RT2573_RX_DESC_SIZE);
916 rssi = rum_get_rssi(sc, sc->sc_rx_desc.rssi);
917 flags = le32toh(sc->sc_rx_desc.flags);
918 if (flags & RT2573_RX_CRC_ERROR) {
920 * This should not happen since we did not
921 * request to receive those frames when we
922 * filled RUM_TXRX_CSR2:
924 DPRINTFN(5, "PHY or CRC error\n");
929 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
931 DPRINTF("could not allocate mbuf\n");
935 usbd_copy_out(pc, RT2573_RX_DESC_SIZE,
936 mtod(m, uint8_t *), len);
939 m->m_pkthdr.rcvif = ifp;
940 m->m_pkthdr.len = m->m_len = (flags >> 16) & 0xfff;
942 if (ieee80211_radiotap_active(ic)) {
943 struct rum_rx_radiotap_header *tap = &sc->sc_rxtap;
947 tap->wr_rate = ieee80211_plcp2rate(sc->sc_rx_desc.rate,
948 (flags & RT2573_RX_OFDM) ?
949 IEEE80211_T_OFDM : IEEE80211_T_CCK);
950 tap->wr_antsignal = RT2573_NOISE_FLOOR + rssi;
951 tap->wr_antnoise = RT2573_NOISE_FLOOR;
952 tap->wr_antenna = sc->rx_ant;
957 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
958 usbd_transfer_submit(xfer);
961 * At the end of a USB callback it is always safe to unlock
962 * the private mutex of a device! That is why we do the
963 * "ieee80211_input" here, and not some lines up!
967 ni = ieee80211_find_rxnode(ic,
968 mtod(m, struct ieee80211_frame_min *));
970 (void) ieee80211_input(ni, m, rssi,
972 ieee80211_free_node(ni);
974 (void) ieee80211_input_all(ic, m, rssi,
977 if (!ifq_is_oactive(&ifp->if_snd) &&
978 !ifq_is_empty(&ifp->if_snd))
979 rum_start_locked(ifp);
984 if (error != USB_ERR_CANCELLED) {
985 /* try to clear stall first */
986 usbd_xfer_set_stall(xfer);
994 rum_plcp_signal(int rate)
997 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1000 case 24: return 0xa;
1001 case 36: return 0xe;
1002 case 48: return 0x9;
1003 case 72: return 0xd;
1004 case 96: return 0x8;
1005 case 108: return 0xc;
1007 /* CCK rates (NB: not IEEE std, device-specific) */
1010 case 11: return 0x2;
1011 case 22: return 0x3;
1013 return 0xff; /* XXX unsupported/unknown rate */
1017 rum_setup_tx_desc(struct rum_softc *sc, struct rum_tx_desc *desc,
1018 uint32_t flags, uint16_t xflags, int len, int rate)
1020 struct ifnet *ifp = sc->sc_ifp;
1021 struct ieee80211com *ic = ifp->if_l2com;
1022 uint16_t plcp_length;
1025 desc->flags = htole32(flags);
1026 desc->flags |= htole32(RT2573_TX_VALID);
1027 desc->flags |= htole32(len << 16);
1029 desc->xflags = htole16(xflags);
1031 desc->wme = htole16(RT2573_QID(0) | RT2573_AIFSN(2) |
1032 RT2573_LOGCWMIN(4) | RT2573_LOGCWMAX(10));
1034 /* setup PLCP fields */
1035 desc->plcp_signal = rum_plcp_signal(rate);
1036 desc->plcp_service = 4;
1038 len += IEEE80211_CRC_LEN;
1039 if (ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) {
1040 desc->flags |= htole32(RT2573_TX_OFDM);
1042 plcp_length = len & 0xfff;
1043 desc->plcp_length_hi = plcp_length >> 6;
1044 desc->plcp_length_lo = plcp_length & 0x3f;
1046 plcp_length = (16 * len + rate - 1) / rate;
1048 remainder = (16 * len) % 22;
1049 if (remainder != 0 && remainder < 7)
1050 desc->plcp_service |= RT2573_PLCP_LENGEXT;
1052 desc->plcp_length_hi = plcp_length >> 8;
1053 desc->plcp_length_lo = plcp_length & 0xff;
1055 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1056 desc->plcp_signal |= 0x08;
1061 rum_sendprot(struct rum_softc *sc,
1062 const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate)
1064 struct ieee80211com *ic = ni->ni_ic;
1065 const struct ieee80211_frame *wh;
1066 struct rum_tx_data *data;
1068 int protrate, ackrate, pktlen, flags, isshort;
1071 RUM_LOCK_ASSERT(sc, MA_OWNED);
1072 KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY,
1073 ("protection %d", prot));
1075 wh = mtod(m, const struct ieee80211_frame *);
1076 pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
1078 protrate = ieee80211_ctl_rate(ic->ic_rt, rate);
1079 ackrate = ieee80211_ack_rate(ic->ic_rt, rate);
1081 isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0;
1082 dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort)
1083 + ieee80211_ack_duration(ic->ic_rt, rate, isshort);
1084 flags = RT2573_TX_MORE_FRAG;
1085 if (prot == IEEE80211_PROT_RTSCTS) {
1086 /* NB: CTS is the same size as an ACK */
1087 dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort);
1088 flags |= RT2573_TX_NEED_ACK;
1089 mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur);
1091 mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur);
1093 if (mprot == NULL) {
1094 /* XXX stat + msg */
1097 data = STAILQ_FIRST(&sc->tx_free);
1098 STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1102 data->ni = ieee80211_ref_node(ni);
1103 data->rate = protrate;
1104 rum_setup_tx_desc(sc, &data->desc, flags, 0, mprot->m_pkthdr.len, protrate);
1106 STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1107 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1113 rum_tx_mgt(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1115 struct ieee80211vap *vap = ni->ni_vap;
1116 struct ifnet *ifp = sc->sc_ifp;
1117 struct ieee80211com *ic = ifp->if_l2com;
1118 struct rum_tx_data *data;
1119 struct ieee80211_frame *wh;
1120 const struct ieee80211_txparam *tp;
1121 struct ieee80211_key *k;
1125 RUM_LOCK_ASSERT(sc, MA_OWNED);
1127 data = STAILQ_FIRST(&sc->tx_free);
1128 STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1131 wh = mtod(m0, struct ieee80211_frame *);
1132 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1133 k = ieee80211_crypto_encap(ni, m0);
1138 wh = mtod(m0, struct ieee80211_frame *);
1141 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1143 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1144 flags |= RT2573_TX_NEED_ACK;
1146 dur = ieee80211_ack_duration(ic->ic_rt, tp->mgmtrate,
1147 ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1148 *(uint16_t *)wh->i_dur = htole16(dur);
1150 /* tell hardware to add timestamp for probe responses */
1152 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
1153 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
1154 flags |= RT2573_TX_TIMESTAMP;
1159 data->rate = tp->mgmtrate;
1161 rum_setup_tx_desc(sc, &data->desc, flags, 0, m0->m_pkthdr.len, tp->mgmtrate);
1163 DPRINTFN(10, "sending mgt frame len=%d rate=%d\n",
1164 m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, tp->mgmtrate);
1166 STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1167 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1173 rum_tx_raw(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
1174 const struct ieee80211_bpf_params *params)
1176 struct ieee80211com *ic = ni->ni_ic;
1177 struct rum_tx_data *data;
1181 RUM_LOCK_ASSERT(sc, MA_OWNED);
1182 KASSERT(params != NULL, ("no raw xmit params"));
1184 rate = params->ibp_rate0;
1185 if (!ieee80211_isratevalid(ic->ic_rt, rate)) {
1190 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
1191 flags |= RT2573_TX_NEED_ACK;
1192 if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) {
1193 error = rum_sendprot(sc, m0, ni,
1194 params->ibp_flags & IEEE80211_BPF_RTS ?
1195 IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY,
1197 if (error || sc->tx_nfree == 0) {
1201 flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
1204 data = STAILQ_FIRST(&sc->tx_free);
1205 STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1212 /* XXX need to setup descriptor ourself */
1213 rum_setup_tx_desc(sc, &data->desc, flags, 0, m0->m_pkthdr.len, rate);
1215 DPRINTFN(10, "sending raw frame len=%u rate=%u\n",
1216 m0->m_pkthdr.len, rate);
1218 STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1219 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1225 rum_tx_data(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1227 struct ieee80211vap *vap = ni->ni_vap;
1228 struct ifnet *ifp = sc->sc_ifp;
1229 struct ieee80211com *ic = ifp->if_l2com;
1230 struct rum_tx_data *data;
1231 struct ieee80211_frame *wh;
1232 const struct ieee80211_txparam *tp;
1233 struct ieee80211_key *k;
1238 #if 0 /* XXX swildner: lock needed? */
1239 RUM_LOCK_ASSERT(sc, MA_OWNED);
1242 wh = mtod(m0, struct ieee80211_frame *);
1244 tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
1245 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1246 rate = tp->mcastrate;
1247 else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
1248 rate = tp->ucastrate;
1250 rate = ni->ni_txrate;
1252 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1253 k = ieee80211_crypto_encap(ni, m0);
1259 /* packet header may have moved, reset our local pointer */
1260 wh = mtod(m0, struct ieee80211_frame *);
1263 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1264 int prot = IEEE80211_PROT_NONE;
1265 if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold)
1266 prot = IEEE80211_PROT_RTSCTS;
1267 else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
1268 ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM)
1269 prot = ic->ic_protmode;
1270 if (prot != IEEE80211_PROT_NONE) {
1271 error = rum_sendprot(sc, m0, ni, prot, rate);
1272 if (error || sc->tx_nfree == 0) {
1276 flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
1280 data = STAILQ_FIRST(&sc->tx_free);
1281 STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1288 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1289 flags |= RT2573_TX_NEED_ACK;
1290 flags |= RT2573_TX_MORE_FRAG;
1292 dur = ieee80211_ack_duration(ic->ic_rt, rate,
1293 ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1294 *(uint16_t *)wh->i_dur = htole16(dur);
1297 rum_setup_tx_desc(sc, &data->desc, flags, 0, m0->m_pkthdr.len, rate);
1299 DPRINTFN(10, "sending frame len=%d rate=%d\n",
1300 m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, rate);
1302 STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1304 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1311 rum_start_locked(struct ifnet *ifp)
1313 struct rum_softc *sc = ifp->if_softc;
1314 struct ieee80211_node *ni;
1317 if ((ifp->if_flags & IFF_RUNNING) == 0) {
1321 m = ifq_dequeue(&ifp->if_snd);
1324 if (sc->tx_nfree < RUM_TX_MINFREE) {
1325 ifq_prepend(&ifp->if_snd, m);
1326 ifq_set_oactive(&ifp->if_snd);
1329 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1330 if (rum_tx_data(sc, m, ni) != 0) {
1331 ieee80211_free_node(ni);
1339 rum_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
1341 ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
1342 rum_start_locked(ifp);
1346 rum_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *ucred)
1348 struct rum_softc *sc = ifp->if_softc;
1349 struct ieee80211com *ic = ifp->if_l2com;
1350 struct ifreq *ifr = (struct ifreq *) data;
1355 error = sc->sc_detached ? ENXIO : 0;
1363 if (ifp->if_flags & IFF_UP) {
1364 if ((ifp->if_flags & IFF_RUNNING) == 0) {
1365 rum_init_locked(sc);
1370 if (ifp->if_flags & IFF_RUNNING)
1375 ieee80211_start_all(ic);
1378 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
1381 error = ether_ioctl(ifp, cmd, data);
1391 rum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len)
1393 struct usb_device_request req;
1396 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1397 req.bRequest = RT2573_READ_EEPROM;
1398 USETW(req.wValue, 0);
1399 USETW(req.wIndex, addr);
1400 USETW(req.wLength, len);
1402 error = rum_do_request(sc, &req, buf);
1404 device_printf(sc->sc_dev, "could not read EEPROM: %s\n",
1405 usbd_errstr(error));
1410 rum_read(struct rum_softc *sc, uint16_t reg)
1414 rum_read_multi(sc, reg, &val, sizeof val);
1416 return le32toh(val);
1420 rum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len)
1422 struct usb_device_request req;
1425 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1426 req.bRequest = RT2573_READ_MULTI_MAC;
1427 USETW(req.wValue, 0);
1428 USETW(req.wIndex, reg);
1429 USETW(req.wLength, len);
1431 error = rum_do_request(sc, &req, buf);
1433 device_printf(sc->sc_dev,
1434 "could not multi read MAC register: %s\n",
1435 usbd_errstr(error));
1440 rum_write(struct rum_softc *sc, uint16_t reg, uint32_t val)
1442 uint32_t tmp = htole32(val);
1444 return (rum_write_multi(sc, reg, &tmp, sizeof tmp));
1448 rum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len)
1450 struct usb_device_request req;
1454 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1455 req.bRequest = RT2573_WRITE_MULTI_MAC;
1456 USETW(req.wValue, 0);
1458 /* write at most 64 bytes at a time */
1459 for (offset = 0; offset < len; offset += 64) {
1460 USETW(req.wIndex, reg + offset);
1461 USETW(req.wLength, MIN(len - offset, 64));
1463 error = rum_do_request(sc, &req, (char *)buf + offset);
1465 device_printf(sc->sc_dev,
1466 "could not multi write MAC register: %s\n",
1467 usbd_errstr(error));
1472 return (USB_ERR_NORMAL_COMPLETION);
1476 rum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val)
1481 DPRINTFN(2, "reg=0x%08x\n", reg);
1483 for (ntries = 0; ntries < 100; ntries++) {
1484 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1486 if (rum_pause(sc, hz / 100))
1489 if (ntries == 100) {
1490 device_printf(sc->sc_dev, "could not write to BBP\n");
1494 tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val;
1495 rum_write(sc, RT2573_PHY_CSR3, tmp);
1499 rum_bbp_read(struct rum_softc *sc, uint8_t reg)
1504 DPRINTFN(2, "reg=0x%08x\n", reg);
1506 for (ntries = 0; ntries < 100; ntries++) {
1507 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1509 if (rum_pause(sc, hz / 100))
1512 if (ntries == 100) {
1513 device_printf(sc->sc_dev, "could not read BBP\n");
1517 val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8;
1518 rum_write(sc, RT2573_PHY_CSR3, val);
1520 for (ntries = 0; ntries < 100; ntries++) {
1521 val = rum_read(sc, RT2573_PHY_CSR3);
1522 if (!(val & RT2573_BBP_BUSY))
1524 if (rum_pause(sc, hz / 100))
1528 device_printf(sc->sc_dev, "could not read BBP\n");
1533 rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val)
1538 for (ntries = 0; ntries < 100; ntries++) {
1539 if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY))
1541 if (rum_pause(sc, hz / 100))
1544 if (ntries == 100) {
1545 device_printf(sc->sc_dev, "could not write to RF\n");
1549 tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 |
1551 rum_write(sc, RT2573_PHY_CSR4, tmp);
1553 /* remember last written value in sc */
1554 sc->rf_regs[reg] = val;
1556 DPRINTFN(15, "RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff);
1560 rum_select_antenna(struct rum_softc *sc)
1562 uint8_t bbp4, bbp77;
1565 bbp4 = rum_bbp_read(sc, 4);
1566 bbp77 = rum_bbp_read(sc, 77);
1570 /* make sure Rx is disabled before switching antenna */
1571 tmp = rum_read(sc, RT2573_TXRX_CSR0);
1572 rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
1574 rum_bbp_write(sc, 4, bbp4);
1575 rum_bbp_write(sc, 77, bbp77);
1577 rum_write(sc, RT2573_TXRX_CSR0, tmp);
1581 * Enable multi-rate retries for frames sent at OFDM rates.
1582 * In 802.11b/g mode, allow fallback to CCK rates.
1585 rum_enable_mrr(struct rum_softc *sc)
1587 struct ifnet *ifp = sc->sc_ifp;
1588 struct ieee80211com *ic = ifp->if_l2com;
1591 tmp = rum_read(sc, RT2573_TXRX_CSR4);
1593 tmp &= ~RT2573_MRR_CCK_FALLBACK;
1594 if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan))
1595 tmp |= RT2573_MRR_CCK_FALLBACK;
1596 tmp |= RT2573_MRR_ENABLED;
1598 rum_write(sc, RT2573_TXRX_CSR4, tmp);
1602 rum_set_txpreamble(struct rum_softc *sc)
1604 struct ifnet *ifp = sc->sc_ifp;
1605 struct ieee80211com *ic = ifp->if_l2com;
1608 tmp = rum_read(sc, RT2573_TXRX_CSR4);
1610 tmp &= ~RT2573_SHORT_PREAMBLE;
1611 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
1612 tmp |= RT2573_SHORT_PREAMBLE;
1614 rum_write(sc, RT2573_TXRX_CSR4, tmp);
1618 rum_set_basicrates(struct rum_softc *sc)
1620 struct ifnet *ifp = sc->sc_ifp;
1621 struct ieee80211com *ic = ifp->if_l2com;
1623 /* update basic rate set */
1624 if (ic->ic_curmode == IEEE80211_MODE_11B) {
1625 /* 11b basic rates: 1, 2Mbps */
1626 rum_write(sc, RT2573_TXRX_CSR5, 0x3);
1627 } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) {
1628 /* 11a basic rates: 6, 12, 24Mbps */
1629 rum_write(sc, RT2573_TXRX_CSR5, 0x150);
1631 /* 11b/g basic rates: 1, 2, 5.5, 11Mbps */
1632 rum_write(sc, RT2573_TXRX_CSR5, 0xf);
1637 * Reprogram MAC/BBP to switch to a new band. Values taken from the reference
1641 rum_select_band(struct rum_softc *sc, struct ieee80211_channel *c)
1643 uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104;
1646 /* update all BBP registers that depend on the band */
1647 bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c;
1648 bbp35 = 0x50; bbp97 = 0x48; bbp98 = 0x48;
1649 if (IEEE80211_IS_CHAN_5GHZ(c)) {
1650 bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c;
1651 bbp35 += 0x10; bbp97 += 0x10; bbp98 += 0x10;
1653 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1654 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1655 bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10;
1659 rum_bbp_write(sc, 17, bbp17);
1660 rum_bbp_write(sc, 96, bbp96);
1661 rum_bbp_write(sc, 104, bbp104);
1663 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1664 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1665 rum_bbp_write(sc, 75, 0x80);
1666 rum_bbp_write(sc, 86, 0x80);
1667 rum_bbp_write(sc, 88, 0x80);
1670 rum_bbp_write(sc, 35, bbp35);
1671 rum_bbp_write(sc, 97, bbp97);
1672 rum_bbp_write(sc, 98, bbp98);
1674 tmp = rum_read(sc, RT2573_PHY_CSR0);
1675 tmp &= ~(RT2573_PA_PE_2GHZ | RT2573_PA_PE_5GHZ);
1676 if (IEEE80211_IS_CHAN_2GHZ(c))
1677 tmp |= RT2573_PA_PE_2GHZ;
1679 tmp |= RT2573_PA_PE_5GHZ;
1680 rum_write(sc, RT2573_PHY_CSR0, tmp);
1684 rum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c)
1686 struct ifnet *ifp = sc->sc_ifp;
1687 struct ieee80211com *ic = ifp->if_l2com;
1688 const struct rfprog *rfprog;
1689 uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT;
1693 chan = ieee80211_chan2ieee(ic, c);
1694 if (chan == 0 || chan == IEEE80211_CHAN_ANY)
1697 /* select the appropriate RF settings based on what EEPROM says */
1698 rfprog = (sc->rf_rev == RT2573_RF_5225 ||
1699 sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226;
1701 /* find the settings for this channel (we know it exists) */
1702 for (i = 0; rfprog[i].chan != chan; i++);
1704 power = sc->txpow[i];
1708 } else if (power > 31) {
1709 bbp94 += power - 31;
1714 * If we are switching from the 2GHz band to the 5GHz band or
1715 * vice-versa, BBP registers need to be reprogrammed.
1717 if (c->ic_flags != ic->ic_curchan->ic_flags) {
1718 rum_select_band(sc, c);
1719 rum_select_antenna(sc);
1723 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1724 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1725 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1726 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1728 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1729 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1730 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7 | 1);
1731 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1733 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1734 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1735 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1736 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1738 rum_pause(sc, hz / 100);
1740 /* enable smart mode for MIMO-capable RFs */
1741 bbp3 = rum_bbp_read(sc, 3);
1743 bbp3 &= ~RT2573_SMART_MODE;
1744 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527)
1745 bbp3 |= RT2573_SMART_MODE;
1747 rum_bbp_write(sc, 3, bbp3);
1749 if (bbp94 != RT2573_BBPR94_DEFAULT)
1750 rum_bbp_write(sc, 94, bbp94);
1752 /* give the chip some extra time to do the switchover */
1753 rum_pause(sc, hz / 100);
1757 * Enable TSF synchronization and tell h/w to start sending beacons for IBSS
1758 * and HostAP operating modes.
1761 rum_enable_tsf_sync(struct rum_softc *sc)
1763 struct ifnet *ifp = sc->sc_ifp;
1764 struct ieee80211com *ic = ifp->if_l2com;
1765 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1768 if (vap->iv_opmode != IEEE80211_M_STA) {
1770 * Change default 16ms TBTT adjustment to 8ms.
1771 * Must be done before enabling beacon generation.
1773 rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8);
1776 tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000;
1778 /* set beacon interval (in 1/16ms unit) */
1779 tmp |= vap->iv_bss->ni_intval * 16;
1781 tmp |= RT2573_TSF_TICKING | RT2573_ENABLE_TBTT;
1782 if (vap->iv_opmode == IEEE80211_M_STA)
1783 tmp |= RT2573_TSF_MODE(1);
1785 tmp |= RT2573_TSF_MODE(2) | RT2573_GENERATE_BEACON;
1787 rum_write(sc, RT2573_TXRX_CSR9, tmp);
1791 rum_enable_tsf(struct rum_softc *sc)
1793 rum_write(sc, RT2573_TXRX_CSR9,
1794 (rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000) |
1795 RT2573_TSF_TICKING | RT2573_TSF_MODE(2));
1799 rum_update_slot(struct ifnet *ifp)
1801 struct rum_softc *sc = ifp->if_softc;
1802 struct ieee80211com *ic = ifp->if_l2com;
1806 slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
1808 tmp = rum_read(sc, RT2573_MAC_CSR9);
1809 tmp = (tmp & ~0xff) | slottime;
1810 rum_write(sc, RT2573_MAC_CSR9, tmp);
1812 DPRINTF("setting slot time to %uus\n", slottime);
1816 rum_set_bssid(struct rum_softc *sc, const uint8_t *bssid)
1820 tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24;
1821 rum_write(sc, RT2573_MAC_CSR4, tmp);
1823 tmp = bssid[4] | bssid[5] << 8 | RT2573_ONE_BSSID << 16;
1824 rum_write(sc, RT2573_MAC_CSR5, tmp);
1828 rum_set_macaddr(struct rum_softc *sc, const uint8_t *addr)
1832 tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24;
1833 rum_write(sc, RT2573_MAC_CSR2, tmp);
1835 tmp = addr[4] | addr[5] << 8 | 0xff << 16;
1836 rum_write(sc, RT2573_MAC_CSR3, tmp);
1840 rum_setpromisc(struct rum_softc *sc)
1842 struct ifnet *ifp = sc->sc_ifp;
1845 tmp = rum_read(sc, RT2573_TXRX_CSR0);
1847 tmp &= ~RT2573_DROP_NOT_TO_ME;
1848 if (!(ifp->if_flags & IFF_PROMISC))
1849 tmp |= RT2573_DROP_NOT_TO_ME;
1851 rum_write(sc, RT2573_TXRX_CSR0, tmp);
1853 DPRINTF("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
1854 "entering" : "leaving");
1858 rum_update_promisc(struct ifnet *ifp)
1860 struct rum_softc *sc = ifp->if_softc;
1862 if ((ifp->if_flags & IFF_RUNNING) == 0)
1871 rum_update_mcast(struct ifnet *ifp)
1873 static int warning_printed;
1875 if (warning_printed == 0) {
1876 if_printf(ifp, "need to implement %s\n", __func__);
1877 warning_printed = 1;
1885 case RT2573_RF_2527: return "RT2527 (MIMO XR)";
1886 case RT2573_RF_2528: return "RT2528";
1887 case RT2573_RF_5225: return "RT5225 (MIMO XR)";
1888 case RT2573_RF_5226: return "RT5226";
1889 default: return "unknown";
1894 rum_read_eeprom(struct rum_softc *sc)
1901 /* read MAC address */
1902 rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, sc->sc_bssid, 6);
1904 rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2);
1906 sc->rf_rev = (val >> 11) & 0x1f;
1907 sc->hw_radio = (val >> 10) & 0x1;
1908 sc->rx_ant = (val >> 4) & 0x3;
1909 sc->tx_ant = (val >> 2) & 0x3;
1910 sc->nb_ant = val & 0x3;
1912 DPRINTF("RF revision=%d\n", sc->rf_rev);
1914 rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2);
1916 sc->ext_5ghz_lna = (val >> 6) & 0x1;
1917 sc->ext_2ghz_lna = (val >> 4) & 0x1;
1919 DPRINTF("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n",
1920 sc->ext_2ghz_lna, sc->ext_5ghz_lna);
1922 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2);
1924 if ((val & 0xff) != 0xff)
1925 sc->rssi_2ghz_corr = (int8_t)(val & 0xff); /* signed */
1927 /* Only [-10, 10] is valid */
1928 if (sc->rssi_2ghz_corr < -10 || sc->rssi_2ghz_corr > 10)
1929 sc->rssi_2ghz_corr = 0;
1931 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2);
1933 if ((val & 0xff) != 0xff)
1934 sc->rssi_5ghz_corr = (int8_t)(val & 0xff); /* signed */
1936 /* Only [-10, 10] is valid */
1937 if (sc->rssi_5ghz_corr < -10 || sc->rssi_5ghz_corr > 10)
1938 sc->rssi_5ghz_corr = 0;
1940 if (sc->ext_2ghz_lna)
1941 sc->rssi_2ghz_corr -= 14;
1942 if (sc->ext_5ghz_lna)
1943 sc->rssi_5ghz_corr -= 14;
1945 DPRINTF("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n",
1946 sc->rssi_2ghz_corr, sc->rssi_5ghz_corr);
1948 rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2);
1950 if ((val & 0xff) != 0xff)
1951 sc->rffreq = val & 0xff;
1953 DPRINTF("RF freq=%d\n", sc->rffreq);
1955 /* read Tx power for all a/b/g channels */
1956 rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14);
1957 /* XXX default Tx power for 802.11a channels */
1958 memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14);
1960 for (i = 0; i < 14; i++)
1961 DPRINTF("Channel=%d Tx power=%d\n", i + 1, sc->txpow[i]);
1964 /* read default values for BBP registers */
1965 rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
1967 for (i = 0; i < 14; i++) {
1968 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
1970 DPRINTF("BBP R%d=%02x\n", sc->bbp_prom[i].reg,
1971 sc->bbp_prom[i].val);
1977 rum_bbp_init(struct rum_softc *sc)
1981 /* wait for BBP to be ready */
1982 for (ntries = 0; ntries < 100; ntries++) {
1983 const uint8_t val = rum_bbp_read(sc, 0);
1984 if (val != 0 && val != 0xff)
1986 if (rum_pause(sc, hz / 100))
1989 if (ntries == 100) {
1990 device_printf(sc->sc_dev, "timeout waiting for BBP\n");
1994 /* initialize BBP registers to default values */
1995 for (i = 0; i < N(rum_def_bbp); i++)
1996 rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val);
1998 /* write vendor-specific BBP values (from EEPROM) */
1999 for (i = 0; i < 16; i++) {
2000 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
2002 rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
2009 rum_init_locked(struct rum_softc *sc)
2011 struct ifnet *ifp = sc->sc_ifp;
2012 struct ieee80211com *ic = ifp->if_l2com;
2017 RUM_LOCK_ASSERT(sc, MA_OWNED);
2021 /* initialize MAC registers to default values */
2022 for (i = 0; i < N(rum_def_mac); i++)
2023 rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val);
2025 /* set host ready */
2026 rum_write(sc, RT2573_MAC_CSR1, 3);
2027 rum_write(sc, RT2573_MAC_CSR1, 0);
2029 /* wait for BBP/RF to wakeup */
2030 for (ntries = 0; ntries < 100; ntries++) {
2031 if (rum_read(sc, RT2573_MAC_CSR12) & 8)
2033 rum_write(sc, RT2573_MAC_CSR12, 4); /* force wakeup */
2034 if (rum_pause(sc, hz / 100))
2037 if (ntries == 100) {
2038 device_printf(sc->sc_dev,
2039 "timeout waiting for BBP/RF to wakeup\n");
2043 if ((error = rum_bbp_init(sc)) != 0)
2046 /* select default channel */
2047 rum_select_band(sc, ic->ic_curchan);
2048 rum_select_antenna(sc);
2049 rum_set_chan(sc, ic->ic_curchan);
2051 /* clear STA registers */
2052 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
2054 rum_set_macaddr(sc, IF_LLADDR(ifp));
2056 /* initialize ASIC */
2057 rum_write(sc, RT2573_MAC_CSR1, 4);
2060 * Allocate Tx and Rx xfer queues.
2062 rum_setup_tx_list(sc);
2064 /* update Rx filter */
2065 tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff;
2067 tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR;
2068 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2069 tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR |
2071 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2072 tmp |= RT2573_DROP_TODS;
2073 if (!(ifp->if_flags & IFF_PROMISC))
2074 tmp |= RT2573_DROP_NOT_TO_ME;
2076 rum_write(sc, RT2573_TXRX_CSR0, tmp);
2078 ifq_clr_oactive(&ifp->if_snd);
2079 ifp->if_flags |= IFF_RUNNING;
2080 usbd_xfer_set_stall(sc->sc_xfer[RUM_BULK_WR]);
2081 usbd_transfer_start(sc->sc_xfer[RUM_BULK_RD]);
2089 rum_init(void *priv)
2091 struct rum_softc *sc = priv;
2092 struct ifnet *ifp = sc->sc_ifp;
2093 struct ieee80211com *ic = ifp->if_l2com;
2096 rum_init_locked(sc);
2099 if (ifp->if_flags & IFF_RUNNING)
2100 ieee80211_start_all(ic); /* start all vap's */
2104 rum_stop(struct rum_softc *sc)
2106 struct ifnet *ifp = sc->sc_ifp;
2109 RUM_LOCK_ASSERT(sc, MA_OWNED);
2111 ifp->if_flags &= ~IFF_RUNNING;
2112 ifq_clr_oactive(&ifp->if_snd);
2117 * Drain the USB transfers, if not already drained:
2119 usbd_transfer_drain(sc->sc_xfer[RUM_BULK_WR]);
2120 usbd_transfer_drain(sc->sc_xfer[RUM_BULK_RD]);
2124 rum_unsetup_tx_list(sc);
2127 tmp = rum_read(sc, RT2573_TXRX_CSR0);
2128 rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
2131 rum_write(sc, RT2573_MAC_CSR1, 3);
2132 rum_write(sc, RT2573_MAC_CSR1, 0);
2136 rum_load_microcode(struct rum_softc *sc, const uint8_t *ucode, size_t size)
2138 struct usb_device_request req;
2139 uint16_t reg = RT2573_MCU_CODE_BASE;
2142 /* copy firmware image into NIC */
2143 for (; size >= 4; reg += 4, ucode += 4, size -= 4) {
2144 err = rum_write(sc, reg, UGETDW(ucode));
2146 /* firmware already loaded ? */
2147 device_printf(sc->sc_dev, "Firmware load "
2148 "failure! (ignored)\n");
2153 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2154 req.bRequest = RT2573_MCU_CNTL;
2155 USETW(req.wValue, RT2573_MCU_RUN);
2156 USETW(req.wIndex, 0);
2157 USETW(req.wLength, 0);
2159 err = rum_do_request(sc, &req, NULL);
2161 device_printf(sc->sc_dev, "could not run firmware: %s\n",
2165 /* give the chip some time to boot */
2166 rum_pause(sc, hz / 8);
2170 rum_prepare_beacon(struct rum_softc *sc, struct ieee80211vap *vap)
2172 struct ieee80211com *ic = vap->iv_ic;
2173 const struct ieee80211_txparam *tp;
2174 struct rum_tx_desc desc;
2177 if (vap->iv_bss->ni_chan == IEEE80211_CHAN_ANYC)
2179 if (ic->ic_bsschan == IEEE80211_CHAN_ANYC)
2182 m0 = ieee80211_beacon_alloc(vap->iv_bss, &RUM_VAP(vap)->bo);
2186 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)];
2187 rum_setup_tx_desc(sc, &desc, RT2573_TX_TIMESTAMP, RT2573_TX_HWSEQ,
2188 m0->m_pkthdr.len, tp->mgmtrate);
2190 /* copy the first 24 bytes of Tx descriptor into NIC memory */
2191 rum_write_multi(sc, RT2573_HW_BEACON_BASE0, (uint8_t *)&desc, 24);
2193 /* copy beacon header and payload into NIC memory */
2194 rum_write_multi(sc, RT2573_HW_BEACON_BASE0 + 24, mtod(m0, uint8_t *),
2201 rum_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2202 const struct ieee80211_bpf_params *params)
2204 struct ifnet *ifp = ni->ni_ic->ic_ifp;
2205 struct rum_softc *sc = ifp->if_softc;
2208 /* prevent management frames from being sent if we're not ready */
2209 if (!(ifp->if_flags & IFF_RUNNING)) {
2212 ieee80211_free_node(ni);
2215 if (sc->tx_nfree < RUM_TX_MINFREE) {
2216 ifq_set_oactive(&ifp->if_snd);
2219 ieee80211_free_node(ni);
2225 if (params == NULL) {
2227 * Legacy path; interpret frame contents to decide
2228 * precisely how to send the frame.
2230 if (rum_tx_mgt(sc, m, ni) != 0)
2234 * Caller supplied explicit parameters to use in
2235 * sending the frame.
2237 if (rum_tx_raw(sc, m, ni, params) != 0)
2246 ieee80211_free_node(ni);
2251 rum_ratectl_start(struct rum_softc *sc, struct ieee80211_node *ni)
2253 struct ieee80211vap *vap = ni->ni_vap;
2254 struct rum_vap *rvp = RUM_VAP(vap);
2256 /* clear statistic registers (STA_CSR0 to STA_CSR5) */
2257 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
2259 usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp);
2263 rum_ratectl_timeout(void *arg)
2265 struct rum_vap *rvp = arg;
2266 struct ieee80211vap *vap = &rvp->vap;
2267 struct ieee80211com *ic = vap->iv_ic;
2269 ieee80211_runtask(ic, &rvp->ratectl_task);
2273 rum_ratectl_task(void *arg, int pending)
2275 struct rum_vap *rvp = arg;
2276 struct ieee80211vap *vap = &rvp->vap;
2277 struct ieee80211com *ic = vap->iv_ic;
2278 struct ifnet *ifp = ic->ic_ifp;
2279 struct rum_softc *sc = ifp->if_softc;
2280 struct ieee80211_node *ni;
2285 /* read and clear statistic registers (STA_CSR0 to STA_CSR10) */
2286 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof(sc->sta));
2288 ok = (le32toh(sc->sta[4]) >> 16) + /* TX ok w/o retry */
2289 (le32toh(sc->sta[5]) & 0xffff); /* TX ok w/ retry */
2290 fail = (le32toh(sc->sta[5]) >> 16); /* TX retry-fail count */
2292 retrycnt = (le32toh(sc->sta[5]) & 0xffff) + fail;
2294 ni = ieee80211_ref_node(vap->iv_bss);
2295 ieee80211_ratectl_tx_update(vap, ni, &sum, &ok, &retrycnt);
2296 (void) ieee80211_ratectl_rate(ni, NULL, 0);
2297 ieee80211_free_node(ni);
2299 ifp->if_oerrors += fail; /* count TX retry-fail as Tx errors */
2301 usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp);
2306 rum_scan_start(struct ieee80211com *ic)
2308 struct ifnet *ifp = ic->ic_ifp;
2309 struct rum_softc *sc = ifp->if_softc;
2313 /* abort TSF synchronization */
2314 tmp = rum_read(sc, RT2573_TXRX_CSR9);
2315 rum_write(sc, RT2573_TXRX_CSR9, tmp & ~0x00ffffff);
2316 rum_set_bssid(sc, ifp->if_broadcastaddr);
2322 rum_scan_end(struct ieee80211com *ic)
2324 struct rum_softc *sc = ic->ic_ifp->if_softc;
2327 rum_enable_tsf_sync(sc);
2328 rum_set_bssid(sc, sc->sc_bssid);
2334 rum_set_channel(struct ieee80211com *ic)
2336 struct rum_softc *sc = ic->ic_ifp->if_softc;
2339 rum_set_chan(sc, ic->ic_curchan);
2344 rum_get_rssi(struct rum_softc *sc, uint8_t raw)
2346 struct ifnet *ifp = sc->sc_ifp;
2347 struct ieee80211com *ic = ifp->if_l2com;
2350 lna = (raw >> 5) & 0x3;
2357 * NB: Since RSSI is relative to noise floor, -1 is
2358 * adequate for caller to know error happened.
2363 rssi = (2 * agc) - RT2573_NOISE_FLOOR;
2365 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) {
2366 rssi += sc->rssi_2ghz_corr;
2375 rssi += sc->rssi_5ghz_corr;
2377 if (!sc->ext_5ghz_lna && lna != 1)
2391 rum_pause(struct rum_softc *sc, int timeout)
2393 zsleep(sc, &wlan_global_serializer, 0, "rumpause", timeout + 1);
2397 static device_method_t rum_methods[] = {
2398 /* Device interface */
2399 DEVMETHOD(device_probe, rum_match),
2400 DEVMETHOD(device_attach, rum_attach),
2401 DEVMETHOD(device_detach, rum_detach),
2405 static driver_t rum_driver = {
2407 .methods = rum_methods,
2408 .size = sizeof(struct rum_softc),
2411 static devclass_t rum_devclass;
2413 DRIVER_MODULE(rum, uhub, rum_driver, rum_devclass, NULL, 0);
2414 MODULE_DEPEND(rum, wlan, 1, 1, 1);
2415 MODULE_DEPEND(rum, usb, 1, 1, 1);
2416 MODULE_VERSION(rum, 1);