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.
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
25 * Ralink Technology RT2501USB/RT2601USB chipset driver
26 * http://www.ralinktech.com.tw/
29 #include <sys/param.h>
30 #include <sys/sockio.h>
31 #include <sys/sysctl.h>
33 #include <sys/mutex.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>
41 #include <sys/endian.h>
44 #include <machine/bus.h>
45 #include <machine/resource.h>
50 #include <net/if_arp.h>
51 #include <net/ethernet.h>
52 #include <net/if_dl.h>
53 #include <net/if_media.h>
54 #include <net/if_types.h>
57 #include <netinet/in.h>
58 #include <netinet/in_systm.h>
59 #include <netinet/in_var.h>
60 #include <netinet/if_ether.h>
61 #include <netinet/ip.h>
64 #include <net80211/ieee80211_var.h>
65 #include <net80211/ieee80211_regdomain.h>
66 #include <net80211/ieee80211_radiotap.h>
67 #include <net80211/ieee80211_ratectl.h>
69 #include <dev/usb/usb.h>
70 #include <dev/usb/usbdi.h>
73 #define USB_DEBUG_VAR rum_debug
74 #include <dev/usb/usb_debug.h>
76 #include <dev/usb/wlan/if_rumreg.h>
77 #include <dev/usb/wlan/if_rumvar.h>
78 #include <dev/usb/wlan/if_rumfw.h>
81 static int rum_debug = 0;
83 static SYSCTL_NODE(_hw_usb, OID_AUTO, rum, CTLFLAG_RW, 0, "USB rum");
84 SYSCTL_INT(_hw_usb_rum, OID_AUTO, debug, CTLFLAG_RW, &rum_debug, 0,
88 #define N(a) ((int)(sizeof (a) / sizeof ((a)[0])))
90 static const STRUCT_USB_HOST_ID rum_devs[] = {
91 #define RUM_DEV(v,p) { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
92 RUM_DEV(ABOCOM, HWU54DM),
93 RUM_DEV(ABOCOM, RT2573_2),
94 RUM_DEV(ABOCOM, RT2573_3),
95 RUM_DEV(ABOCOM, RT2573_4),
96 RUM_DEV(ABOCOM, WUG2700),
97 RUM_DEV(AMIT, CGWLUSB2GO),
98 RUM_DEV(ASUS, RT2573_1),
99 RUM_DEV(ASUS, RT2573_2),
100 RUM_DEV(BELKIN, F5D7050A),
101 RUM_DEV(BELKIN, F5D9050V3),
102 RUM_DEV(CISCOLINKSYS, WUSB54GC),
103 RUM_DEV(CISCOLINKSYS, WUSB54GR),
104 RUM_DEV(CONCEPTRONIC2, C54RU2),
105 RUM_DEV(COREGA, CGWLUSB2GL),
106 RUM_DEV(COREGA, CGWLUSB2GPX),
107 RUM_DEV(DICKSMITH, CWD854F),
108 RUM_DEV(DICKSMITH, RT2573),
109 RUM_DEV(EDIMAX, EW7318USG),
110 RUM_DEV(DLINK2, DWLG122C1),
111 RUM_DEV(DLINK2, WUA1340),
112 RUM_DEV(DLINK2, DWA111),
113 RUM_DEV(DLINK2, DWA110),
114 RUM_DEV(GIGABYTE, GNWB01GS),
115 RUM_DEV(GIGABYTE, GNWI05GS),
116 RUM_DEV(GIGASET, RT2573),
117 RUM_DEV(GOODWAY, RT2573),
118 RUM_DEV(GUILLEMOT, HWGUSB254LB),
119 RUM_DEV(GUILLEMOT, HWGUSB254V2AP),
120 RUM_DEV(HUAWEI3COM, WUB320G),
121 RUM_DEV(MELCO, G54HP),
122 RUM_DEV(MELCO, SG54HP),
123 RUM_DEV(MELCO, 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),
147 static device_probe_t rum_match;
148 static device_attach_t rum_attach;
149 static device_detach_t rum_detach;
151 static usb_callback_t rum_bulk_read_callback;
152 static usb_callback_t rum_bulk_write_callback;
154 static usb_error_t rum_do_request(struct rum_softc *sc,
155 struct usb_device_request *req, void *data);
156 static 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]);
160 static void rum_vap_delete(struct ieee80211vap *);
161 static void rum_tx_free(struct rum_tx_data *, int);
162 static void rum_setup_tx_list(struct rum_softc *);
163 static void rum_unsetup_tx_list(struct rum_softc *);
164 static int rum_newstate(struct ieee80211vap *,
165 enum ieee80211_state, int);
166 static void rum_setup_tx_desc(struct rum_softc *,
167 struct rum_tx_desc *, uint32_t, uint16_t, int,
169 static int rum_tx_mgt(struct rum_softc *, struct mbuf *,
170 struct ieee80211_node *);
171 static int rum_tx_raw(struct rum_softc *, struct mbuf *,
172 struct ieee80211_node *,
173 const struct ieee80211_bpf_params *);
174 static int rum_tx_data(struct rum_softc *, struct mbuf *,
175 struct ieee80211_node *);
176 static void rum_start(struct ifnet *);
177 static int rum_ioctl(struct ifnet *, u_long, caddr_t);
178 static void rum_eeprom_read(struct rum_softc *, uint16_t, void *,
180 static uint32_t rum_read(struct rum_softc *, uint16_t);
181 static void rum_read_multi(struct rum_softc *, uint16_t, void *,
183 static usb_error_t rum_write(struct rum_softc *, uint16_t, uint32_t);
184 static usb_error_t rum_write_multi(struct rum_softc *, uint16_t, void *,
186 static void rum_bbp_write(struct rum_softc *, uint8_t, uint8_t);
187 static uint8_t rum_bbp_read(struct rum_softc *, uint8_t);
188 static void rum_rf_write(struct rum_softc *, uint8_t, uint32_t);
189 static void rum_select_antenna(struct rum_softc *);
190 static void rum_enable_mrr(struct rum_softc *);
191 static void rum_set_txpreamble(struct rum_softc *);
192 static void rum_set_basicrates(struct rum_softc *);
193 static void rum_select_band(struct rum_softc *,
194 struct ieee80211_channel *);
195 static void rum_set_chan(struct rum_softc *,
196 struct ieee80211_channel *);
197 static void rum_enable_tsf_sync(struct rum_softc *);
198 static void rum_enable_tsf(struct rum_softc *);
199 static void rum_update_slot(struct ifnet *);
200 static void rum_set_bssid(struct rum_softc *, const uint8_t *);
201 static void rum_set_macaddr(struct rum_softc *, const uint8_t *);
202 static void rum_update_mcast(struct ifnet *);
203 static void rum_update_promisc(struct ifnet *);
204 static void rum_setpromisc(struct rum_softc *);
205 static const char *rum_get_rf(int);
206 static void rum_read_eeprom(struct rum_softc *);
207 static int rum_bbp_init(struct rum_softc *);
208 static void rum_init_locked(struct rum_softc *);
209 static void rum_init(void *);
210 static void rum_stop(struct rum_softc *);
211 static void rum_load_microcode(struct rum_softc *, const uint8_t *,
213 static void rum_prepare_beacon(struct rum_softc *,
214 struct ieee80211vap *);
215 static int rum_raw_xmit(struct ieee80211_node *, struct mbuf *,
216 const struct ieee80211_bpf_params *);
217 static void rum_scan_start(struct ieee80211com *);
218 static void rum_scan_end(struct ieee80211com *);
219 static void rum_set_channel(struct ieee80211com *);
220 static int rum_get_rssi(struct rum_softc *, uint8_t);
221 static void rum_ratectl_start(struct rum_softc *,
222 struct ieee80211_node *);
223 static void rum_ratectl_timeout(void *);
224 static void rum_ratectl_task(void *, int);
225 static int rum_pause(struct rum_softc *, int);
227 static const struct {
231 { RT2573_TXRX_CSR0, 0x025fb032 },
232 { RT2573_TXRX_CSR1, 0x9eaa9eaf },
233 { RT2573_TXRX_CSR2, 0x8a8b8c8d },
234 { RT2573_TXRX_CSR3, 0x00858687 },
235 { RT2573_TXRX_CSR7, 0x2e31353b },
236 { RT2573_TXRX_CSR8, 0x2a2a2a2c },
237 { RT2573_TXRX_CSR15, 0x0000000f },
238 { RT2573_MAC_CSR6, 0x00000fff },
239 { RT2573_MAC_CSR8, 0x016c030a },
240 { RT2573_MAC_CSR10, 0x00000718 },
241 { RT2573_MAC_CSR12, 0x00000004 },
242 { RT2573_MAC_CSR13, 0x00007f00 },
243 { RT2573_SEC_CSR0, 0x00000000 },
244 { RT2573_SEC_CSR1, 0x00000000 },
245 { RT2573_SEC_CSR5, 0x00000000 },
246 { RT2573_PHY_CSR1, 0x000023b0 },
247 { RT2573_PHY_CSR5, 0x00040a06 },
248 { RT2573_PHY_CSR6, 0x00080606 },
249 { RT2573_PHY_CSR7, 0x00000408 },
250 { RT2573_AIFSN_CSR, 0x00002273 },
251 { RT2573_CWMIN_CSR, 0x00002344 },
252 { RT2573_CWMAX_CSR, 0x000034aa }
255 static const struct {
287 static const struct rfprog {
289 uint32_t r1, r2, r3, r4;
291 { 1, 0x00b03, 0x001e1, 0x1a014, 0x30282 },
292 { 2, 0x00b03, 0x001e1, 0x1a014, 0x30287 },
293 { 3, 0x00b03, 0x001e2, 0x1a014, 0x30282 },
294 { 4, 0x00b03, 0x001e2, 0x1a014, 0x30287 },
295 { 5, 0x00b03, 0x001e3, 0x1a014, 0x30282 },
296 { 6, 0x00b03, 0x001e3, 0x1a014, 0x30287 },
297 { 7, 0x00b03, 0x001e4, 0x1a014, 0x30282 },
298 { 8, 0x00b03, 0x001e4, 0x1a014, 0x30287 },
299 { 9, 0x00b03, 0x001e5, 0x1a014, 0x30282 },
300 { 10, 0x00b03, 0x001e5, 0x1a014, 0x30287 },
301 { 11, 0x00b03, 0x001e6, 0x1a014, 0x30282 },
302 { 12, 0x00b03, 0x001e6, 0x1a014, 0x30287 },
303 { 13, 0x00b03, 0x001e7, 0x1a014, 0x30282 },
304 { 14, 0x00b03, 0x001e8, 0x1a014, 0x30284 },
306 { 34, 0x00b03, 0x20266, 0x36014, 0x30282 },
307 { 38, 0x00b03, 0x20267, 0x36014, 0x30284 },
308 { 42, 0x00b03, 0x20268, 0x36014, 0x30286 },
309 { 46, 0x00b03, 0x20269, 0x36014, 0x30288 },
311 { 36, 0x00b03, 0x00266, 0x26014, 0x30288 },
312 { 40, 0x00b03, 0x00268, 0x26014, 0x30280 },
313 { 44, 0x00b03, 0x00269, 0x26014, 0x30282 },
314 { 48, 0x00b03, 0x0026a, 0x26014, 0x30284 },
315 { 52, 0x00b03, 0x0026b, 0x26014, 0x30286 },
316 { 56, 0x00b03, 0x0026c, 0x26014, 0x30288 },
317 { 60, 0x00b03, 0x0026e, 0x26014, 0x30280 },
318 { 64, 0x00b03, 0x0026f, 0x26014, 0x30282 },
320 { 100, 0x00b03, 0x0028a, 0x2e014, 0x30280 },
321 { 104, 0x00b03, 0x0028b, 0x2e014, 0x30282 },
322 { 108, 0x00b03, 0x0028c, 0x2e014, 0x30284 },
323 { 112, 0x00b03, 0x0028d, 0x2e014, 0x30286 },
324 { 116, 0x00b03, 0x0028e, 0x2e014, 0x30288 },
325 { 120, 0x00b03, 0x002a0, 0x2e014, 0x30280 },
326 { 124, 0x00b03, 0x002a1, 0x2e014, 0x30282 },
327 { 128, 0x00b03, 0x002a2, 0x2e014, 0x30284 },
328 { 132, 0x00b03, 0x002a3, 0x2e014, 0x30286 },
329 { 136, 0x00b03, 0x002a4, 0x2e014, 0x30288 },
330 { 140, 0x00b03, 0x002a6, 0x2e014, 0x30280 },
332 { 149, 0x00b03, 0x002a8, 0x2e014, 0x30287 },
333 { 153, 0x00b03, 0x002a9, 0x2e014, 0x30289 },
334 { 157, 0x00b03, 0x002ab, 0x2e014, 0x30281 },
335 { 161, 0x00b03, 0x002ac, 0x2e014, 0x30283 },
336 { 165, 0x00b03, 0x002ad, 0x2e014, 0x30285 }
338 { 1, 0x00b33, 0x011e1, 0x1a014, 0x30282 },
339 { 2, 0x00b33, 0x011e1, 0x1a014, 0x30287 },
340 { 3, 0x00b33, 0x011e2, 0x1a014, 0x30282 },
341 { 4, 0x00b33, 0x011e2, 0x1a014, 0x30287 },
342 { 5, 0x00b33, 0x011e3, 0x1a014, 0x30282 },
343 { 6, 0x00b33, 0x011e3, 0x1a014, 0x30287 },
344 { 7, 0x00b33, 0x011e4, 0x1a014, 0x30282 },
345 { 8, 0x00b33, 0x011e4, 0x1a014, 0x30287 },
346 { 9, 0x00b33, 0x011e5, 0x1a014, 0x30282 },
347 { 10, 0x00b33, 0x011e5, 0x1a014, 0x30287 },
348 { 11, 0x00b33, 0x011e6, 0x1a014, 0x30282 },
349 { 12, 0x00b33, 0x011e6, 0x1a014, 0x30287 },
350 { 13, 0x00b33, 0x011e7, 0x1a014, 0x30282 },
351 { 14, 0x00b33, 0x011e8, 0x1a014, 0x30284 },
353 { 34, 0x00b33, 0x01266, 0x26014, 0x30282 },
354 { 38, 0x00b33, 0x01267, 0x26014, 0x30284 },
355 { 42, 0x00b33, 0x01268, 0x26014, 0x30286 },
356 { 46, 0x00b33, 0x01269, 0x26014, 0x30288 },
358 { 36, 0x00b33, 0x01266, 0x26014, 0x30288 },
359 { 40, 0x00b33, 0x01268, 0x26014, 0x30280 },
360 { 44, 0x00b33, 0x01269, 0x26014, 0x30282 },
361 { 48, 0x00b33, 0x0126a, 0x26014, 0x30284 },
362 { 52, 0x00b33, 0x0126b, 0x26014, 0x30286 },
363 { 56, 0x00b33, 0x0126c, 0x26014, 0x30288 },
364 { 60, 0x00b33, 0x0126e, 0x26014, 0x30280 },
365 { 64, 0x00b33, 0x0126f, 0x26014, 0x30282 },
367 { 100, 0x00b33, 0x0128a, 0x2e014, 0x30280 },
368 { 104, 0x00b33, 0x0128b, 0x2e014, 0x30282 },
369 { 108, 0x00b33, 0x0128c, 0x2e014, 0x30284 },
370 { 112, 0x00b33, 0x0128d, 0x2e014, 0x30286 },
371 { 116, 0x00b33, 0x0128e, 0x2e014, 0x30288 },
372 { 120, 0x00b33, 0x012a0, 0x2e014, 0x30280 },
373 { 124, 0x00b33, 0x012a1, 0x2e014, 0x30282 },
374 { 128, 0x00b33, 0x012a2, 0x2e014, 0x30284 },
375 { 132, 0x00b33, 0x012a3, 0x2e014, 0x30286 },
376 { 136, 0x00b33, 0x012a4, 0x2e014, 0x30288 },
377 { 140, 0x00b33, 0x012a6, 0x2e014, 0x30280 },
379 { 149, 0x00b33, 0x012a8, 0x2e014, 0x30287 },
380 { 153, 0x00b33, 0x012a9, 0x2e014, 0x30289 },
381 { 157, 0x00b33, 0x012ab, 0x2e014, 0x30281 },
382 { 161, 0x00b33, 0x012ac, 0x2e014, 0x30283 },
383 { 165, 0x00b33, 0x012ad, 0x2e014, 0x30285 }
386 static const struct usb_config rum_config[RUM_N_TRANSFER] = {
389 .endpoint = UE_ADDR_ANY,
390 .direction = UE_DIR_OUT,
391 .bufsize = (MCLBYTES + RT2573_TX_DESC_SIZE + 8),
392 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
393 .callback = rum_bulk_write_callback,
394 .timeout = 5000, /* ms */
398 .endpoint = UE_ADDR_ANY,
399 .direction = UE_DIR_IN,
400 .bufsize = (MCLBYTES + RT2573_RX_DESC_SIZE),
401 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
402 .callback = rum_bulk_read_callback,
407 rum_match(device_t self)
409 struct usb_attach_arg *uaa = device_get_ivars(self);
411 if (uaa->usb_mode != USB_MODE_HOST)
413 if (uaa->info.bConfigIndex != 0)
415 if (uaa->info.bIfaceIndex != RT2573_IFACE_INDEX)
418 return (usbd_lookup_id_by_uaa(rum_devs, sizeof(rum_devs), uaa));
422 rum_attach(device_t self)
424 struct usb_attach_arg *uaa = device_get_ivars(self);
425 struct rum_softc *sc = device_get_softc(self);
426 struct ieee80211com *ic;
428 uint8_t iface_index, bands;
432 device_set_usb_desc(self);
433 sc->sc_udev = uaa->device;
436 mtx_init(&sc->sc_mtx, device_get_nameunit(self),
437 MTX_NETWORK_LOCK, MTX_DEF);
439 iface_index = RT2573_IFACE_INDEX;
440 error = usbd_transfer_setup(uaa->device, &iface_index,
441 sc->sc_xfer, rum_config, RUM_N_TRANSFER, sc, &sc->sc_mtx);
443 device_printf(self, "could not allocate USB transfers, "
444 "err=%s\n", usbd_errstr(error));
449 /* retrieve RT2573 rev. no */
450 for (ntries = 0; ntries < 100; ntries++) {
451 if ((tmp = rum_read(sc, RT2573_MAC_CSR0)) != 0)
453 if (rum_pause(sc, hz / 100))
457 device_printf(sc->sc_dev, "timeout waiting for chip to settle\n");
462 /* retrieve MAC address and various other things from EEPROM */
465 device_printf(sc->sc_dev, "MAC/BBP RT2573 (rev 0x%05x), RF %s\n",
466 tmp, rum_get_rf(sc->rf_rev));
468 rum_load_microcode(sc, rt2573_ucode, sizeof(rt2573_ucode));
471 ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
473 device_printf(sc->sc_dev, "can not if_alloc()\n");
479 if_initname(ifp, "rum", device_get_unit(sc->sc_dev));
480 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
481 ifp->if_init = rum_init;
482 ifp->if_ioctl = rum_ioctl;
483 ifp->if_start = rum_start;
484 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
485 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
486 IFQ_SET_READY(&ifp->if_snd);
489 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
491 /* set device capabilities */
493 IEEE80211_C_STA /* station mode supported */
494 | IEEE80211_C_IBSS /* IBSS mode supported */
495 | IEEE80211_C_MONITOR /* monitor mode supported */
496 | IEEE80211_C_HOSTAP /* HostAp mode supported */
497 | IEEE80211_C_TXPMGT /* tx power management */
498 | IEEE80211_C_SHPREAMBLE /* short preamble supported */
499 | IEEE80211_C_SHSLOT /* short slot time supported */
500 | IEEE80211_C_BGSCAN /* bg scanning supported */
501 | IEEE80211_C_WPA /* 802.11i */
505 setbit(&bands, IEEE80211_MODE_11B);
506 setbit(&bands, IEEE80211_MODE_11G);
507 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_5226)
508 setbit(&bands, IEEE80211_MODE_11A);
509 ieee80211_init_channels(ic, NULL, &bands);
511 ieee80211_ifattach(ic, sc->sc_bssid);
512 ic->ic_update_promisc = rum_update_promisc;
513 ic->ic_raw_xmit = rum_raw_xmit;
514 ic->ic_scan_start = rum_scan_start;
515 ic->ic_scan_end = rum_scan_end;
516 ic->ic_set_channel = rum_set_channel;
518 ic->ic_vap_create = rum_vap_create;
519 ic->ic_vap_delete = rum_vap_delete;
520 ic->ic_update_mcast = rum_update_mcast;
522 ieee80211_radiotap_attach(ic,
523 &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
524 RT2573_TX_RADIOTAP_PRESENT,
525 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
526 RT2573_RX_RADIOTAP_PRESENT);
529 ieee80211_announce(ic);
535 return (ENXIO); /* failure */
539 rum_detach(device_t self)
541 struct rum_softc *sc = device_get_softc(self);
542 struct ifnet *ifp = sc->sc_ifp;
543 struct ieee80211com *ic;
545 /* stop all USB transfers */
546 usbd_transfer_unsetup(sc->sc_xfer, RUM_N_TRANSFER);
548 /* free TX list, if any */
550 rum_unsetup_tx_list(sc);
555 ieee80211_ifdetach(ic);
558 mtx_destroy(&sc->sc_mtx);
564 rum_do_request(struct rum_softc *sc,
565 struct usb_device_request *req, void *data)
571 err = usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
572 req, data, 0, NULL, 250 /* ms */);
576 DPRINTFN(1, "Control request failed, %s (retrying)\n",
578 if (rum_pause(sc, hz / 100))
584 static struct ieee80211vap *
585 rum_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
586 enum ieee80211_opmode opmode, int flags,
587 const uint8_t bssid[IEEE80211_ADDR_LEN],
588 const uint8_t mac[IEEE80211_ADDR_LEN])
590 struct rum_softc *sc = ic->ic_ifp->if_softc;
592 struct ieee80211vap *vap;
594 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */
596 rvp = (struct rum_vap *) malloc(sizeof(struct rum_vap),
597 M_80211_VAP, M_NOWAIT | M_ZERO);
601 /* enable s/w bmiss handling for sta mode */
602 ieee80211_vap_setup(ic, vap, name, unit, opmode,
603 flags | IEEE80211_CLONE_NOBEACONS, bssid, mac);
605 /* override state transition machine */
606 rvp->newstate = vap->iv_newstate;
607 vap->iv_newstate = rum_newstate;
609 usb_callout_init_mtx(&rvp->ratectl_ch, &sc->sc_mtx, 0);
610 TASK_INIT(&rvp->ratectl_task, 0, rum_ratectl_task, rvp);
611 ieee80211_ratectl_init(vap);
612 ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */);
614 ieee80211_vap_attach(vap, ieee80211_media_change, ieee80211_media_status);
615 ic->ic_opmode = opmode;
620 rum_vap_delete(struct ieee80211vap *vap)
622 struct rum_vap *rvp = RUM_VAP(vap);
623 struct ieee80211com *ic = vap->iv_ic;
625 usb_callout_drain(&rvp->ratectl_ch);
626 ieee80211_draintask(ic, &rvp->ratectl_task);
627 ieee80211_ratectl_deinit(vap);
628 ieee80211_vap_detach(vap);
629 free(rvp, M_80211_VAP);
633 rum_tx_free(struct rum_tx_data *data, int txerr)
635 struct rum_softc *sc = data->sc;
637 if (data->m != NULL) {
638 if (data->m->m_flags & M_TXCB)
639 ieee80211_process_callback(data->ni, data->m,
640 txerr ? ETIMEDOUT : 0);
644 ieee80211_free_node(data->ni);
647 STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
652 rum_setup_tx_list(struct rum_softc *sc)
654 struct rum_tx_data *data;
658 STAILQ_INIT(&sc->tx_q);
659 STAILQ_INIT(&sc->tx_free);
661 for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
662 data = &sc->tx_data[i];
665 STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
671 rum_unsetup_tx_list(struct rum_softc *sc)
673 struct rum_tx_data *data;
676 /* make sure any subsequent use of the queues will fail */
678 STAILQ_INIT(&sc->tx_q);
679 STAILQ_INIT(&sc->tx_free);
681 /* free up all node references and mbufs */
682 for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
683 data = &sc->tx_data[i];
685 if (data->m != NULL) {
689 if (data->ni != NULL) {
690 ieee80211_free_node(data->ni);
697 rum_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
699 struct rum_vap *rvp = RUM_VAP(vap);
700 struct ieee80211com *ic = vap->iv_ic;
701 struct rum_softc *sc = ic->ic_ifp->if_softc;
702 const struct ieee80211_txparam *tp;
703 enum ieee80211_state ostate;
704 struct ieee80211_node *ni;
707 ostate = vap->iv_state;
708 DPRINTF("%s -> %s\n",
709 ieee80211_state_name[ostate],
710 ieee80211_state_name[nstate]);
712 IEEE80211_UNLOCK(ic);
714 usb_callout_stop(&rvp->ratectl_ch);
717 case IEEE80211_S_INIT:
718 if (ostate == IEEE80211_S_RUN) {
719 /* abort TSF synchronization */
720 tmp = rum_read(sc, RT2573_TXRX_CSR9);
721 rum_write(sc, RT2573_TXRX_CSR9, tmp & ~0x00ffffff);
725 case IEEE80211_S_RUN:
726 ni = ieee80211_ref_node(vap->iv_bss);
728 if (vap->iv_opmode != IEEE80211_M_MONITOR) {
729 if (ic->ic_bsschan == IEEE80211_CHAN_ANYC) {
732 ieee80211_free_node(ni);
735 rum_update_slot(ic->ic_ifp);
737 rum_set_txpreamble(sc);
738 rum_set_basicrates(sc);
739 IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid);
740 rum_set_bssid(sc, sc->sc_bssid);
743 if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
744 vap->iv_opmode == IEEE80211_M_IBSS)
745 rum_prepare_beacon(sc, vap);
747 if (vap->iv_opmode != IEEE80211_M_MONITOR)
748 rum_enable_tsf_sync(sc);
752 /* enable automatic rate adaptation */
753 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
754 if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
755 rum_ratectl_start(sc, ni);
756 ieee80211_free_node(ni);
763 return (rvp->newstate(vap, nstate, arg));
767 rum_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
769 struct rum_softc *sc = usbd_xfer_softc(xfer);
770 struct ifnet *ifp = sc->sc_ifp;
771 struct ieee80211vap *vap;
772 struct rum_tx_data *data;
774 struct usb_page_cache *pc;
778 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
780 switch (USB_GET_STATE(xfer)) {
781 case USB_ST_TRANSFERRED:
782 DPRINTFN(11, "transfer complete, %d bytes\n", actlen);
785 data = usbd_xfer_get_priv(xfer);
786 rum_tx_free(data, 0);
787 usbd_xfer_set_priv(xfer, NULL);
790 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
795 data = STAILQ_FIRST(&sc->tx_q);
797 STAILQ_REMOVE_HEAD(&sc->tx_q, next);
800 if (m->m_pkthdr.len > (int)(MCLBYTES + RT2573_TX_DESC_SIZE)) {
801 DPRINTFN(0, "data overflow, %u bytes\n",
803 m->m_pkthdr.len = (MCLBYTES + RT2573_TX_DESC_SIZE);
805 pc = usbd_xfer_get_frame(xfer, 0);
806 usbd_copy_in(pc, 0, &data->desc, RT2573_TX_DESC_SIZE);
807 usbd_m_copy_in(pc, RT2573_TX_DESC_SIZE, m, 0,
810 vap = data->ni->ni_vap;
811 if (ieee80211_radiotap_active_vap(vap)) {
812 struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
815 tap->wt_rate = data->rate;
816 tap->wt_antenna = sc->tx_ant;
818 ieee80211_radiotap_tx(vap, m);
821 /* align end on a 4-bytes boundary */
822 len = (RT2573_TX_DESC_SIZE + m->m_pkthdr.len + 3) & ~3;
826 DPRINTFN(11, "sending frame len=%u xferlen=%u\n",
827 m->m_pkthdr.len, len);
829 usbd_xfer_set_frame_len(xfer, 0, len);
830 usbd_xfer_set_priv(xfer, data);
832 usbd_transfer_submit(xfer);
840 DPRINTFN(11, "transfer error, %s\n",
844 data = usbd_xfer_get_priv(xfer);
846 rum_tx_free(data, error);
847 usbd_xfer_set_priv(xfer, NULL);
850 if (error != USB_ERR_CANCELLED) {
851 if (error == USB_ERR_TIMEOUT)
852 device_printf(sc->sc_dev, "device timeout\n");
855 * Try to clear stall first, also if other
856 * errors occur, hence clearing stall
857 * introduces a 50 ms delay:
859 usbd_xfer_set_stall(xfer);
867 rum_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
869 struct rum_softc *sc = usbd_xfer_softc(xfer);
870 struct ifnet *ifp = sc->sc_ifp;
871 struct ieee80211com *ic = ifp->if_l2com;
872 struct ieee80211_node *ni;
873 struct mbuf *m = NULL;
874 struct usb_page_cache *pc;
879 usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
881 switch (USB_GET_STATE(xfer)) {
882 case USB_ST_TRANSFERRED:
884 DPRINTFN(15, "rx done, actlen=%d\n", len);
886 if (len < (int)(RT2573_RX_DESC_SIZE + IEEE80211_MIN_LEN)) {
887 DPRINTF("%s: xfer too short %d\n",
888 device_get_nameunit(sc->sc_dev), len);
893 len -= RT2573_RX_DESC_SIZE;
894 pc = usbd_xfer_get_frame(xfer, 0);
895 usbd_copy_out(pc, 0, &sc->sc_rx_desc, RT2573_RX_DESC_SIZE);
897 rssi = rum_get_rssi(sc, sc->sc_rx_desc.rssi);
898 flags = le32toh(sc->sc_rx_desc.flags);
899 if (flags & RT2573_RX_CRC_ERROR) {
901 * This should not happen since we did not
902 * request to receive those frames when we
903 * filled RUM_TXRX_CSR2:
905 DPRINTFN(5, "PHY or CRC error\n");
910 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
912 DPRINTF("could not allocate mbuf\n");
916 usbd_copy_out(pc, RT2573_RX_DESC_SIZE,
917 mtod(m, uint8_t *), len);
920 m->m_pkthdr.rcvif = ifp;
921 m->m_pkthdr.len = m->m_len = (flags >> 16) & 0xfff;
923 if (ieee80211_radiotap_active(ic)) {
924 struct rum_rx_radiotap_header *tap = &sc->sc_rxtap;
928 tap->wr_rate = ieee80211_plcp2rate(sc->sc_rx_desc.rate,
929 (flags & RT2573_RX_OFDM) ?
930 IEEE80211_T_OFDM : IEEE80211_T_CCK);
931 tap->wr_antsignal = RT2573_NOISE_FLOOR + rssi;
932 tap->wr_antnoise = RT2573_NOISE_FLOOR;
933 tap->wr_antenna = sc->rx_ant;
938 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
939 usbd_transfer_submit(xfer);
942 * At the end of a USB callback it is always safe to unlock
943 * the private mutex of a device! That is why we do the
944 * "ieee80211_input" here, and not some lines up!
948 ni = ieee80211_find_rxnode(ic,
949 mtod(m, struct ieee80211_frame_min *));
951 (void) ieee80211_input(ni, m, rssi,
953 ieee80211_free_node(ni);
955 (void) ieee80211_input_all(ic, m, rssi,
958 if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0 &&
959 !IFQ_IS_EMPTY(&ifp->if_snd))
965 if (error != USB_ERR_CANCELLED) {
966 /* try to clear stall first */
967 usbd_xfer_set_stall(xfer);
975 rum_plcp_signal(int rate)
978 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
986 case 108: return 0xc;
988 /* CCK rates (NB: not IEEE std, device-specific) */
994 return 0xff; /* XXX unsupported/unknown rate */
998 rum_setup_tx_desc(struct rum_softc *sc, struct rum_tx_desc *desc,
999 uint32_t flags, uint16_t xflags, int len, int rate)
1001 struct ifnet *ifp = sc->sc_ifp;
1002 struct ieee80211com *ic = ifp->if_l2com;
1003 uint16_t plcp_length;
1006 desc->flags = htole32(flags);
1007 desc->flags |= htole32(RT2573_TX_VALID);
1008 desc->flags |= htole32(len << 16);
1010 desc->xflags = htole16(xflags);
1012 desc->wme = htole16(RT2573_QID(0) | RT2573_AIFSN(2) |
1013 RT2573_LOGCWMIN(4) | RT2573_LOGCWMAX(10));
1015 /* setup PLCP fields */
1016 desc->plcp_signal = rum_plcp_signal(rate);
1017 desc->plcp_service = 4;
1019 len += IEEE80211_CRC_LEN;
1020 if (ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) {
1021 desc->flags |= htole32(RT2573_TX_OFDM);
1023 plcp_length = len & 0xfff;
1024 desc->plcp_length_hi = plcp_length >> 6;
1025 desc->plcp_length_lo = plcp_length & 0x3f;
1027 plcp_length = (16 * len + rate - 1) / rate;
1029 remainder = (16 * len) % 22;
1030 if (remainder != 0 && remainder < 7)
1031 desc->plcp_service |= RT2573_PLCP_LENGEXT;
1033 desc->plcp_length_hi = plcp_length >> 8;
1034 desc->plcp_length_lo = plcp_length & 0xff;
1036 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1037 desc->plcp_signal |= 0x08;
1042 rum_sendprot(struct rum_softc *sc,
1043 const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate)
1045 struct ieee80211com *ic = ni->ni_ic;
1046 const struct ieee80211_frame *wh;
1047 struct rum_tx_data *data;
1049 int protrate, ackrate, pktlen, flags, isshort;
1052 RUM_LOCK_ASSERT(sc, MA_OWNED);
1053 KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY,
1054 ("protection %d", prot));
1056 wh = mtod(m, const struct ieee80211_frame *);
1057 pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
1059 protrate = ieee80211_ctl_rate(ic->ic_rt, rate);
1060 ackrate = ieee80211_ack_rate(ic->ic_rt, rate);
1062 isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0;
1063 dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort)
1064 + ieee80211_ack_duration(ic->ic_rt, rate, isshort);
1065 flags = RT2573_TX_MORE_FRAG;
1066 if (prot == IEEE80211_PROT_RTSCTS) {
1067 /* NB: CTS is the same size as an ACK */
1068 dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort);
1069 flags |= RT2573_TX_NEED_ACK;
1070 mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur);
1072 mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur);
1074 if (mprot == NULL) {
1075 /* XXX stat + msg */
1078 data = STAILQ_FIRST(&sc->tx_free);
1079 STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1083 data->ni = ieee80211_ref_node(ni);
1084 data->rate = protrate;
1085 rum_setup_tx_desc(sc, &data->desc, flags, 0, mprot->m_pkthdr.len, protrate);
1087 STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1088 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1094 rum_tx_mgt(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1096 struct ieee80211vap *vap = ni->ni_vap;
1097 struct ifnet *ifp = sc->sc_ifp;
1098 struct ieee80211com *ic = ifp->if_l2com;
1099 struct rum_tx_data *data;
1100 struct ieee80211_frame *wh;
1101 const struct ieee80211_txparam *tp;
1102 struct ieee80211_key *k;
1106 RUM_LOCK_ASSERT(sc, MA_OWNED);
1108 data = STAILQ_FIRST(&sc->tx_free);
1109 STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1112 wh = mtod(m0, struct ieee80211_frame *);
1113 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1114 k = ieee80211_crypto_encap(ni, m0);
1119 wh = mtod(m0, struct ieee80211_frame *);
1122 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1124 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1125 flags |= RT2573_TX_NEED_ACK;
1127 dur = ieee80211_ack_duration(ic->ic_rt, tp->mgmtrate,
1128 ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1129 *(uint16_t *)wh->i_dur = htole16(dur);
1131 /* tell hardware to add timestamp for probe responses */
1133 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
1134 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
1135 flags |= RT2573_TX_TIMESTAMP;
1140 data->rate = tp->mgmtrate;
1142 rum_setup_tx_desc(sc, &data->desc, flags, 0, m0->m_pkthdr.len, tp->mgmtrate);
1144 DPRINTFN(10, "sending mgt frame len=%d rate=%d\n",
1145 m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, tp->mgmtrate);
1147 STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1148 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1154 rum_tx_raw(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
1155 const struct ieee80211_bpf_params *params)
1157 struct ieee80211com *ic = ni->ni_ic;
1158 struct rum_tx_data *data;
1162 RUM_LOCK_ASSERT(sc, MA_OWNED);
1163 KASSERT(params != NULL, ("no raw xmit params"));
1165 rate = params->ibp_rate0;
1166 if (!ieee80211_isratevalid(ic->ic_rt, rate)) {
1171 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
1172 flags |= RT2573_TX_NEED_ACK;
1173 if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) {
1174 error = rum_sendprot(sc, m0, ni,
1175 params->ibp_flags & IEEE80211_BPF_RTS ?
1176 IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY,
1178 if (error || sc->tx_nfree == 0) {
1182 flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
1185 data = STAILQ_FIRST(&sc->tx_free);
1186 STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1193 /* XXX need to setup descriptor ourself */
1194 rum_setup_tx_desc(sc, &data->desc, flags, 0, m0->m_pkthdr.len, rate);
1196 DPRINTFN(10, "sending raw frame len=%u rate=%u\n",
1197 m0->m_pkthdr.len, rate);
1199 STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1200 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1206 rum_tx_data(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1208 struct ieee80211vap *vap = ni->ni_vap;
1209 struct ifnet *ifp = sc->sc_ifp;
1210 struct ieee80211com *ic = ifp->if_l2com;
1211 struct rum_tx_data *data;
1212 struct ieee80211_frame *wh;
1213 const struct ieee80211_txparam *tp;
1214 struct ieee80211_key *k;
1219 RUM_LOCK_ASSERT(sc, MA_OWNED);
1221 wh = mtod(m0, struct ieee80211_frame *);
1223 tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
1224 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1225 rate = tp->mcastrate;
1226 else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
1227 rate = tp->ucastrate;
1229 rate = ni->ni_txrate;
1231 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1232 k = ieee80211_crypto_encap(ni, m0);
1238 /* packet header may have moved, reset our local pointer */
1239 wh = mtod(m0, struct ieee80211_frame *);
1242 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1243 int prot = IEEE80211_PROT_NONE;
1244 if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold)
1245 prot = IEEE80211_PROT_RTSCTS;
1246 else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
1247 ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM)
1248 prot = ic->ic_protmode;
1249 if (prot != IEEE80211_PROT_NONE) {
1250 error = rum_sendprot(sc, m0, ni, prot, rate);
1251 if (error || sc->tx_nfree == 0) {
1255 flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
1259 data = STAILQ_FIRST(&sc->tx_free);
1260 STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1267 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1268 flags |= RT2573_TX_NEED_ACK;
1269 flags |= RT2573_TX_MORE_FRAG;
1271 dur = ieee80211_ack_duration(ic->ic_rt, rate,
1272 ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1273 *(uint16_t *)wh->i_dur = htole16(dur);
1276 rum_setup_tx_desc(sc, &data->desc, flags, 0, m0->m_pkthdr.len, rate);
1278 DPRINTFN(10, "sending frame len=%d rate=%d\n",
1279 m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, rate);
1281 STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1282 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1288 rum_start(struct ifnet *ifp)
1290 struct rum_softc *sc = ifp->if_softc;
1291 struct ieee80211_node *ni;
1295 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1300 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1303 if (sc->tx_nfree < RUM_TX_MINFREE) {
1304 IFQ_DRV_PREPEND(&ifp->if_snd, m);
1305 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1308 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1309 if (rum_tx_data(sc, m, ni) != 0) {
1310 ieee80211_free_node(ni);
1319 rum_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1321 struct rum_softc *sc = ifp->if_softc;
1322 struct ieee80211com *ic = ifp->if_l2com;
1323 struct ifreq *ifr = (struct ifreq *) data;
1324 int error = 0, startall = 0;
1329 if (ifp->if_flags & IFF_UP) {
1330 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1331 rum_init_locked(sc);
1336 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1341 ieee80211_start_all(ic);
1344 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
1347 error = ether_ioctl(ifp, cmd, data);
1357 rum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len)
1359 struct usb_device_request req;
1362 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1363 req.bRequest = RT2573_READ_EEPROM;
1364 USETW(req.wValue, 0);
1365 USETW(req.wIndex, addr);
1366 USETW(req.wLength, len);
1368 error = rum_do_request(sc, &req, buf);
1370 device_printf(sc->sc_dev, "could not read EEPROM: %s\n",
1371 usbd_errstr(error));
1376 rum_read(struct rum_softc *sc, uint16_t reg)
1380 rum_read_multi(sc, reg, &val, sizeof val);
1382 return le32toh(val);
1386 rum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len)
1388 struct usb_device_request req;
1391 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1392 req.bRequest = RT2573_READ_MULTI_MAC;
1393 USETW(req.wValue, 0);
1394 USETW(req.wIndex, reg);
1395 USETW(req.wLength, len);
1397 error = rum_do_request(sc, &req, buf);
1399 device_printf(sc->sc_dev,
1400 "could not multi read MAC register: %s\n",
1401 usbd_errstr(error));
1406 rum_write(struct rum_softc *sc, uint16_t reg, uint32_t val)
1408 uint32_t tmp = htole32(val);
1410 return (rum_write_multi(sc, reg, &tmp, sizeof tmp));
1414 rum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len)
1416 struct usb_device_request req;
1420 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1421 req.bRequest = RT2573_WRITE_MULTI_MAC;
1422 USETW(req.wValue, 0);
1424 /* write at most 64 bytes at a time */
1425 for (offset = 0; offset < len; offset += 64) {
1426 USETW(req.wIndex, reg + offset);
1427 USETW(req.wLength, MIN(len - offset, 64));
1429 error = rum_do_request(sc, &req, (char *)buf + offset);
1431 device_printf(sc->sc_dev,
1432 "could not multi write MAC register: %s\n",
1433 usbd_errstr(error));
1438 return (USB_ERR_NORMAL_COMPLETION);
1442 rum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val)
1447 DPRINTFN(2, "reg=0x%08x\n", reg);
1449 for (ntries = 0; ntries < 100; ntries++) {
1450 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1452 if (rum_pause(sc, hz / 100))
1455 if (ntries == 100) {
1456 device_printf(sc->sc_dev, "could not write to BBP\n");
1460 tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val;
1461 rum_write(sc, RT2573_PHY_CSR3, tmp);
1465 rum_bbp_read(struct rum_softc *sc, uint8_t reg)
1470 DPRINTFN(2, "reg=0x%08x\n", reg);
1472 for (ntries = 0; ntries < 100; ntries++) {
1473 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1475 if (rum_pause(sc, hz / 100))
1478 if (ntries == 100) {
1479 device_printf(sc->sc_dev, "could not read BBP\n");
1483 val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8;
1484 rum_write(sc, RT2573_PHY_CSR3, val);
1486 for (ntries = 0; ntries < 100; ntries++) {
1487 val = rum_read(sc, RT2573_PHY_CSR3);
1488 if (!(val & RT2573_BBP_BUSY))
1490 if (rum_pause(sc, hz / 100))
1494 device_printf(sc->sc_dev, "could not read BBP\n");
1499 rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val)
1504 for (ntries = 0; ntries < 100; ntries++) {
1505 if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY))
1507 if (rum_pause(sc, hz / 100))
1510 if (ntries == 100) {
1511 device_printf(sc->sc_dev, "could not write to RF\n");
1515 tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 |
1517 rum_write(sc, RT2573_PHY_CSR4, tmp);
1519 /* remember last written value in sc */
1520 sc->rf_regs[reg] = val;
1522 DPRINTFN(15, "RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff);
1526 rum_select_antenna(struct rum_softc *sc)
1528 uint8_t bbp4, bbp77;
1531 bbp4 = rum_bbp_read(sc, 4);
1532 bbp77 = rum_bbp_read(sc, 77);
1536 /* make sure Rx is disabled before switching antenna */
1537 tmp = rum_read(sc, RT2573_TXRX_CSR0);
1538 rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
1540 rum_bbp_write(sc, 4, bbp4);
1541 rum_bbp_write(sc, 77, bbp77);
1543 rum_write(sc, RT2573_TXRX_CSR0, tmp);
1547 * Enable multi-rate retries for frames sent at OFDM rates.
1548 * In 802.11b/g mode, allow fallback to CCK rates.
1551 rum_enable_mrr(struct rum_softc *sc)
1553 struct ifnet *ifp = sc->sc_ifp;
1554 struct ieee80211com *ic = ifp->if_l2com;
1557 tmp = rum_read(sc, RT2573_TXRX_CSR4);
1559 tmp &= ~RT2573_MRR_CCK_FALLBACK;
1560 if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan))
1561 tmp |= RT2573_MRR_CCK_FALLBACK;
1562 tmp |= RT2573_MRR_ENABLED;
1564 rum_write(sc, RT2573_TXRX_CSR4, tmp);
1568 rum_set_txpreamble(struct rum_softc *sc)
1570 struct ifnet *ifp = sc->sc_ifp;
1571 struct ieee80211com *ic = ifp->if_l2com;
1574 tmp = rum_read(sc, RT2573_TXRX_CSR4);
1576 tmp &= ~RT2573_SHORT_PREAMBLE;
1577 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
1578 tmp |= RT2573_SHORT_PREAMBLE;
1580 rum_write(sc, RT2573_TXRX_CSR4, tmp);
1584 rum_set_basicrates(struct rum_softc *sc)
1586 struct ifnet *ifp = sc->sc_ifp;
1587 struct ieee80211com *ic = ifp->if_l2com;
1589 /* update basic rate set */
1590 if (ic->ic_curmode == IEEE80211_MODE_11B) {
1591 /* 11b basic rates: 1, 2Mbps */
1592 rum_write(sc, RT2573_TXRX_CSR5, 0x3);
1593 } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) {
1594 /* 11a basic rates: 6, 12, 24Mbps */
1595 rum_write(sc, RT2573_TXRX_CSR5, 0x150);
1597 /* 11b/g basic rates: 1, 2, 5.5, 11Mbps */
1598 rum_write(sc, RT2573_TXRX_CSR5, 0xf);
1603 * Reprogram MAC/BBP to switch to a new band. Values taken from the reference
1607 rum_select_band(struct rum_softc *sc, struct ieee80211_channel *c)
1609 uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104;
1612 /* update all BBP registers that depend on the band */
1613 bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c;
1614 bbp35 = 0x50; bbp97 = 0x48; bbp98 = 0x48;
1615 if (IEEE80211_IS_CHAN_5GHZ(c)) {
1616 bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c;
1617 bbp35 += 0x10; bbp97 += 0x10; bbp98 += 0x10;
1619 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1620 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1621 bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10;
1625 rum_bbp_write(sc, 17, bbp17);
1626 rum_bbp_write(sc, 96, bbp96);
1627 rum_bbp_write(sc, 104, bbp104);
1629 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1630 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1631 rum_bbp_write(sc, 75, 0x80);
1632 rum_bbp_write(sc, 86, 0x80);
1633 rum_bbp_write(sc, 88, 0x80);
1636 rum_bbp_write(sc, 35, bbp35);
1637 rum_bbp_write(sc, 97, bbp97);
1638 rum_bbp_write(sc, 98, bbp98);
1640 tmp = rum_read(sc, RT2573_PHY_CSR0);
1641 tmp &= ~(RT2573_PA_PE_2GHZ | RT2573_PA_PE_5GHZ);
1642 if (IEEE80211_IS_CHAN_2GHZ(c))
1643 tmp |= RT2573_PA_PE_2GHZ;
1645 tmp |= RT2573_PA_PE_5GHZ;
1646 rum_write(sc, RT2573_PHY_CSR0, tmp);
1650 rum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c)
1652 struct ifnet *ifp = sc->sc_ifp;
1653 struct ieee80211com *ic = ifp->if_l2com;
1654 const struct rfprog *rfprog;
1655 uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT;
1659 chan = ieee80211_chan2ieee(ic, c);
1660 if (chan == 0 || chan == IEEE80211_CHAN_ANY)
1663 /* select the appropriate RF settings based on what EEPROM says */
1664 rfprog = (sc->rf_rev == RT2573_RF_5225 ||
1665 sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226;
1667 /* find the settings for this channel (we know it exists) */
1668 for (i = 0; rfprog[i].chan != chan; i++);
1670 power = sc->txpow[i];
1674 } else if (power > 31) {
1675 bbp94 += power - 31;
1680 * If we are switching from the 2GHz band to the 5GHz band or
1681 * vice-versa, BBP registers need to be reprogrammed.
1683 if (c->ic_flags != ic->ic_curchan->ic_flags) {
1684 rum_select_band(sc, c);
1685 rum_select_antenna(sc);
1689 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1690 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1691 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1692 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1694 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1695 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1696 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7 | 1);
1697 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1699 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1700 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1701 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1702 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1704 rum_pause(sc, hz / 100);
1706 /* enable smart mode for MIMO-capable RFs */
1707 bbp3 = rum_bbp_read(sc, 3);
1709 bbp3 &= ~RT2573_SMART_MODE;
1710 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527)
1711 bbp3 |= RT2573_SMART_MODE;
1713 rum_bbp_write(sc, 3, bbp3);
1715 if (bbp94 != RT2573_BBPR94_DEFAULT)
1716 rum_bbp_write(sc, 94, bbp94);
1718 /* give the chip some extra time to do the switchover */
1719 rum_pause(sc, hz / 100);
1723 * Enable TSF synchronization and tell h/w to start sending beacons for IBSS
1724 * and HostAP operating modes.
1727 rum_enable_tsf_sync(struct rum_softc *sc)
1729 struct ifnet *ifp = sc->sc_ifp;
1730 struct ieee80211com *ic = ifp->if_l2com;
1731 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1734 if (vap->iv_opmode != IEEE80211_M_STA) {
1736 * Change default 16ms TBTT adjustment to 8ms.
1737 * Must be done before enabling beacon generation.
1739 rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8);
1742 tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000;
1744 /* set beacon interval (in 1/16ms unit) */
1745 tmp |= vap->iv_bss->ni_intval * 16;
1747 tmp |= RT2573_TSF_TICKING | RT2573_ENABLE_TBTT;
1748 if (vap->iv_opmode == IEEE80211_M_STA)
1749 tmp |= RT2573_TSF_MODE(1);
1751 tmp |= RT2573_TSF_MODE(2) | RT2573_GENERATE_BEACON;
1753 rum_write(sc, RT2573_TXRX_CSR9, tmp);
1757 rum_enable_tsf(struct rum_softc *sc)
1759 rum_write(sc, RT2573_TXRX_CSR9,
1760 (rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000) |
1761 RT2573_TSF_TICKING | RT2573_TSF_MODE(2));
1765 rum_update_slot(struct ifnet *ifp)
1767 struct rum_softc *sc = ifp->if_softc;
1768 struct ieee80211com *ic = ifp->if_l2com;
1772 slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
1774 tmp = rum_read(sc, RT2573_MAC_CSR9);
1775 tmp = (tmp & ~0xff) | slottime;
1776 rum_write(sc, RT2573_MAC_CSR9, tmp);
1778 DPRINTF("setting slot time to %uus\n", slottime);
1782 rum_set_bssid(struct rum_softc *sc, const uint8_t *bssid)
1786 tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24;
1787 rum_write(sc, RT2573_MAC_CSR4, tmp);
1789 tmp = bssid[4] | bssid[5] << 8 | RT2573_ONE_BSSID << 16;
1790 rum_write(sc, RT2573_MAC_CSR5, tmp);
1794 rum_set_macaddr(struct rum_softc *sc, const uint8_t *addr)
1798 tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24;
1799 rum_write(sc, RT2573_MAC_CSR2, tmp);
1801 tmp = addr[4] | addr[5] << 8 | 0xff << 16;
1802 rum_write(sc, RT2573_MAC_CSR3, tmp);
1806 rum_setpromisc(struct rum_softc *sc)
1808 struct ifnet *ifp = sc->sc_ifp;
1811 tmp = rum_read(sc, RT2573_TXRX_CSR0);
1813 tmp &= ~RT2573_DROP_NOT_TO_ME;
1814 if (!(ifp->if_flags & IFF_PROMISC))
1815 tmp |= RT2573_DROP_NOT_TO_ME;
1817 rum_write(sc, RT2573_TXRX_CSR0, tmp);
1819 DPRINTF("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
1820 "entering" : "leaving");
1824 rum_update_promisc(struct ifnet *ifp)
1826 struct rum_softc *sc = ifp->if_softc;
1828 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1837 rum_update_mcast(struct ifnet *ifp)
1839 static int warning_printed;
1841 if (warning_printed == 0) {
1842 if_printf(ifp, "need to implement %s\n", __func__);
1843 warning_printed = 1;
1851 case RT2573_RF_2527: return "RT2527 (MIMO XR)";
1852 case RT2573_RF_2528: return "RT2528";
1853 case RT2573_RF_5225: return "RT5225 (MIMO XR)";
1854 case RT2573_RF_5226: return "RT5226";
1855 default: return "unknown";
1860 rum_read_eeprom(struct rum_softc *sc)
1867 /* read MAC address */
1868 rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, sc->sc_bssid, 6);
1870 rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2);
1872 sc->rf_rev = (val >> 11) & 0x1f;
1873 sc->hw_radio = (val >> 10) & 0x1;
1874 sc->rx_ant = (val >> 4) & 0x3;
1875 sc->tx_ant = (val >> 2) & 0x3;
1876 sc->nb_ant = val & 0x3;
1878 DPRINTF("RF revision=%d\n", sc->rf_rev);
1880 rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2);
1882 sc->ext_5ghz_lna = (val >> 6) & 0x1;
1883 sc->ext_2ghz_lna = (val >> 4) & 0x1;
1885 DPRINTF("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n",
1886 sc->ext_2ghz_lna, sc->ext_5ghz_lna);
1888 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2);
1890 if ((val & 0xff) != 0xff)
1891 sc->rssi_2ghz_corr = (int8_t)(val & 0xff); /* signed */
1893 /* Only [-10, 10] is valid */
1894 if (sc->rssi_2ghz_corr < -10 || sc->rssi_2ghz_corr > 10)
1895 sc->rssi_2ghz_corr = 0;
1897 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2);
1899 if ((val & 0xff) != 0xff)
1900 sc->rssi_5ghz_corr = (int8_t)(val & 0xff); /* signed */
1902 /* Only [-10, 10] is valid */
1903 if (sc->rssi_5ghz_corr < -10 || sc->rssi_5ghz_corr > 10)
1904 sc->rssi_5ghz_corr = 0;
1906 if (sc->ext_2ghz_lna)
1907 sc->rssi_2ghz_corr -= 14;
1908 if (sc->ext_5ghz_lna)
1909 sc->rssi_5ghz_corr -= 14;
1911 DPRINTF("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n",
1912 sc->rssi_2ghz_corr, sc->rssi_5ghz_corr);
1914 rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2);
1916 if ((val & 0xff) != 0xff)
1917 sc->rffreq = val & 0xff;
1919 DPRINTF("RF freq=%d\n", sc->rffreq);
1921 /* read Tx power for all a/b/g channels */
1922 rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14);
1923 /* XXX default Tx power for 802.11a channels */
1924 memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14);
1926 for (i = 0; i < 14; i++)
1927 DPRINTF("Channel=%d Tx power=%d\n", i + 1, sc->txpow[i]);
1930 /* read default values for BBP registers */
1931 rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
1933 for (i = 0; i < 14; i++) {
1934 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
1936 DPRINTF("BBP R%d=%02x\n", sc->bbp_prom[i].reg,
1937 sc->bbp_prom[i].val);
1943 rum_bbp_init(struct rum_softc *sc)
1947 /* wait for BBP to be ready */
1948 for (ntries = 0; ntries < 100; ntries++) {
1949 const uint8_t val = rum_bbp_read(sc, 0);
1950 if (val != 0 && val != 0xff)
1952 if (rum_pause(sc, hz / 100))
1955 if (ntries == 100) {
1956 device_printf(sc->sc_dev, "timeout waiting for BBP\n");
1960 /* initialize BBP registers to default values */
1961 for (i = 0; i < N(rum_def_bbp); i++)
1962 rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val);
1964 /* write vendor-specific BBP values (from EEPROM) */
1965 for (i = 0; i < 16; i++) {
1966 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
1968 rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
1975 rum_init_locked(struct rum_softc *sc)
1977 struct ifnet *ifp = sc->sc_ifp;
1978 struct ieee80211com *ic = ifp->if_l2com;
1983 RUM_LOCK_ASSERT(sc, MA_OWNED);
1987 /* initialize MAC registers to default values */
1988 for (i = 0; i < N(rum_def_mac); i++)
1989 rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val);
1991 /* set host ready */
1992 rum_write(sc, RT2573_MAC_CSR1, 3);
1993 rum_write(sc, RT2573_MAC_CSR1, 0);
1995 /* wait for BBP/RF to wakeup */
1996 for (ntries = 0; ntries < 100; ntries++) {
1997 if (rum_read(sc, RT2573_MAC_CSR12) & 8)
1999 rum_write(sc, RT2573_MAC_CSR12, 4); /* force wakeup */
2000 if (rum_pause(sc, hz / 100))
2003 if (ntries == 100) {
2004 device_printf(sc->sc_dev,
2005 "timeout waiting for BBP/RF to wakeup\n");
2009 if ((error = rum_bbp_init(sc)) != 0)
2012 /* select default channel */
2013 rum_select_band(sc, ic->ic_curchan);
2014 rum_select_antenna(sc);
2015 rum_set_chan(sc, ic->ic_curchan);
2017 /* clear STA registers */
2018 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
2020 rum_set_macaddr(sc, IF_LLADDR(ifp));
2022 /* initialize ASIC */
2023 rum_write(sc, RT2573_MAC_CSR1, 4);
2026 * Allocate Tx and Rx xfer queues.
2028 rum_setup_tx_list(sc);
2030 /* update Rx filter */
2031 tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff;
2033 tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR;
2034 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2035 tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR |
2037 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2038 tmp |= RT2573_DROP_TODS;
2039 if (!(ifp->if_flags & IFF_PROMISC))
2040 tmp |= RT2573_DROP_NOT_TO_ME;
2042 rum_write(sc, RT2573_TXRX_CSR0, tmp);
2044 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2045 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2046 usbd_xfer_set_stall(sc->sc_xfer[RUM_BULK_WR]);
2047 usbd_transfer_start(sc->sc_xfer[RUM_BULK_RD]);
2055 rum_init(void *priv)
2057 struct rum_softc *sc = priv;
2058 struct ifnet *ifp = sc->sc_ifp;
2059 struct ieee80211com *ic = ifp->if_l2com;
2062 rum_init_locked(sc);
2065 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2066 ieee80211_start_all(ic); /* start all vap's */
2070 rum_stop(struct rum_softc *sc)
2072 struct ifnet *ifp = sc->sc_ifp;
2075 RUM_LOCK_ASSERT(sc, MA_OWNED);
2077 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2082 * Drain the USB transfers, if not already drained:
2084 usbd_transfer_drain(sc->sc_xfer[RUM_BULK_WR]);
2085 usbd_transfer_drain(sc->sc_xfer[RUM_BULK_RD]);
2089 rum_unsetup_tx_list(sc);
2092 tmp = rum_read(sc, RT2573_TXRX_CSR0);
2093 rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
2096 rum_write(sc, RT2573_MAC_CSR1, 3);
2097 rum_write(sc, RT2573_MAC_CSR1, 0);
2101 rum_load_microcode(struct rum_softc *sc, const uint8_t *ucode, size_t size)
2103 struct usb_device_request req;
2104 uint16_t reg = RT2573_MCU_CODE_BASE;
2107 /* copy firmware image into NIC */
2108 for (; size >= 4; reg += 4, ucode += 4, size -= 4) {
2109 err = rum_write(sc, reg, UGETDW(ucode));
2111 /* firmware already loaded ? */
2112 device_printf(sc->sc_dev, "Firmware load "
2113 "failure! (ignored)\n");
2118 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2119 req.bRequest = RT2573_MCU_CNTL;
2120 USETW(req.wValue, RT2573_MCU_RUN);
2121 USETW(req.wIndex, 0);
2122 USETW(req.wLength, 0);
2124 err = rum_do_request(sc, &req, NULL);
2126 device_printf(sc->sc_dev, "could not run firmware: %s\n",
2130 /* give the chip some time to boot */
2131 rum_pause(sc, hz / 8);
2135 rum_prepare_beacon(struct rum_softc *sc, struct ieee80211vap *vap)
2137 struct ieee80211com *ic = vap->iv_ic;
2138 const struct ieee80211_txparam *tp;
2139 struct rum_tx_desc desc;
2142 if (vap->iv_bss->ni_chan == IEEE80211_CHAN_ANYC)
2144 if (ic->ic_bsschan == IEEE80211_CHAN_ANYC)
2147 m0 = ieee80211_beacon_alloc(vap->iv_bss, &RUM_VAP(vap)->bo);
2151 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)];
2152 rum_setup_tx_desc(sc, &desc, RT2573_TX_TIMESTAMP, RT2573_TX_HWSEQ,
2153 m0->m_pkthdr.len, tp->mgmtrate);
2155 /* copy the first 24 bytes of Tx descriptor into NIC memory */
2156 rum_write_multi(sc, RT2573_HW_BEACON_BASE0, (uint8_t *)&desc, 24);
2158 /* copy beacon header and payload into NIC memory */
2159 rum_write_multi(sc, RT2573_HW_BEACON_BASE0 + 24, mtod(m0, uint8_t *),
2166 rum_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2167 const struct ieee80211_bpf_params *params)
2169 struct ifnet *ifp = ni->ni_ic->ic_ifp;
2170 struct rum_softc *sc = ifp->if_softc;
2173 /* prevent management frames from being sent if we're not ready */
2174 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2177 ieee80211_free_node(ni);
2180 if (sc->tx_nfree < RUM_TX_MINFREE) {
2181 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2184 ieee80211_free_node(ni);
2190 if (params == NULL) {
2192 * Legacy path; interpret frame contents to decide
2193 * precisely how to send the frame.
2195 if (rum_tx_mgt(sc, m, ni) != 0)
2199 * Caller supplied explicit parameters to use in
2200 * sending the frame.
2202 if (rum_tx_raw(sc, m, ni, params) != 0)
2211 ieee80211_free_node(ni);
2216 rum_ratectl_start(struct rum_softc *sc, struct ieee80211_node *ni)
2218 struct ieee80211vap *vap = ni->ni_vap;
2219 struct rum_vap *rvp = RUM_VAP(vap);
2221 /* clear statistic registers (STA_CSR0 to STA_CSR5) */
2222 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
2224 usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp);
2228 rum_ratectl_timeout(void *arg)
2230 struct rum_vap *rvp = arg;
2231 struct ieee80211vap *vap = &rvp->vap;
2232 struct ieee80211com *ic = vap->iv_ic;
2234 ieee80211_runtask(ic, &rvp->ratectl_task);
2238 rum_ratectl_task(void *arg, int pending)
2240 struct rum_vap *rvp = arg;
2241 struct ieee80211vap *vap = &rvp->vap;
2242 struct ieee80211com *ic = vap->iv_ic;
2243 struct ifnet *ifp = ic->ic_ifp;
2244 struct rum_softc *sc = ifp->if_softc;
2245 struct ieee80211_node *ni;
2250 /* read and clear statistic registers (STA_CSR0 to STA_CSR10) */
2251 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof(sc->sta));
2253 ok = (le32toh(sc->sta[4]) >> 16) + /* TX ok w/o retry */
2254 (le32toh(sc->sta[5]) & 0xffff); /* TX ok w/ retry */
2255 fail = (le32toh(sc->sta[5]) >> 16); /* TX retry-fail count */
2257 retrycnt = (le32toh(sc->sta[5]) & 0xffff) + fail;
2259 ni = ieee80211_ref_node(vap->iv_bss);
2260 ieee80211_ratectl_tx_update(vap, ni, &sum, &ok, &retrycnt);
2261 (void) ieee80211_ratectl_rate(ni, NULL, 0);
2262 ieee80211_free_node(ni);
2264 ifp->if_oerrors += fail; /* count TX retry-fail as Tx errors */
2266 usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp);
2271 rum_scan_start(struct ieee80211com *ic)
2273 struct ifnet *ifp = ic->ic_ifp;
2274 struct rum_softc *sc = ifp->if_softc;
2278 /* abort TSF synchronization */
2279 tmp = rum_read(sc, RT2573_TXRX_CSR9);
2280 rum_write(sc, RT2573_TXRX_CSR9, tmp & ~0x00ffffff);
2281 rum_set_bssid(sc, ifp->if_broadcastaddr);
2287 rum_scan_end(struct ieee80211com *ic)
2289 struct rum_softc *sc = ic->ic_ifp->if_softc;
2292 rum_enable_tsf_sync(sc);
2293 rum_set_bssid(sc, sc->sc_bssid);
2299 rum_set_channel(struct ieee80211com *ic)
2301 struct rum_softc *sc = ic->ic_ifp->if_softc;
2304 rum_set_chan(sc, ic->ic_curchan);
2309 rum_get_rssi(struct rum_softc *sc, uint8_t raw)
2311 struct ifnet *ifp = sc->sc_ifp;
2312 struct ieee80211com *ic = ifp->if_l2com;
2315 lna = (raw >> 5) & 0x3;
2322 * NB: Since RSSI is relative to noise floor, -1 is
2323 * adequate for caller to know error happened.
2328 rssi = (2 * agc) - RT2573_NOISE_FLOOR;
2330 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) {
2331 rssi += sc->rssi_2ghz_corr;
2340 rssi += sc->rssi_5ghz_corr;
2342 if (!sc->ext_5ghz_lna && lna != 1)
2356 rum_pause(struct rum_softc *sc, int timeout)
2359 usb_pause_mtx(&sc->sc_mtx, timeout);
2363 static device_method_t rum_methods[] = {
2364 /* Device interface */
2365 DEVMETHOD(device_probe, rum_match),
2366 DEVMETHOD(device_attach, rum_attach),
2367 DEVMETHOD(device_detach, rum_detach),
2372 static driver_t rum_driver = {
2374 .methods = rum_methods,
2375 .size = sizeof(struct rum_softc),
2378 static devclass_t rum_devclass;
2380 DRIVER_MODULE(rum, uhub, rum_driver, rum_devclass, NULL, 0);
2381 MODULE_DEPEND(rum, wlan, 1, 1, 1);
2382 MODULE_DEPEND(rum, usb, 1, 1, 1);
2383 MODULE_VERSION(rum, 1);