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 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 static const struct usb_device_id rum_devs[] = {
89 #define RUM_DEV(v,p) { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
90 RUM_DEV(ABOCOM, HWU54DM),
91 RUM_DEV(ABOCOM, RT2573_2),
92 RUM_DEV(ABOCOM, RT2573_3),
93 RUM_DEV(ABOCOM, RT2573_4),
94 RUM_DEV(ABOCOM, WUG2700),
95 RUM_DEV(AMIT, CGWLUSB2GO),
96 RUM_DEV(ASUS, RT2573_1),
97 RUM_DEV(ASUS, RT2573_2),
98 RUM_DEV(BELKIN, F5D7050A),
99 RUM_DEV(BELKIN, F5D9050V3),
100 RUM_DEV(CISCOLINKSYS, WUSB54GC),
101 RUM_DEV(CISCOLINKSYS, WUSB54GR),
102 RUM_DEV(CONCEPTRONIC2, C54RU2),
103 RUM_DEV(COREGA, CGWLUSB2GL),
104 RUM_DEV(COREGA, CGWLUSB2GPX),
105 RUM_DEV(DICKSMITH, CWD854F),
106 RUM_DEV(DICKSMITH, RT2573),
107 RUM_DEV(EDIMAX, EW7318USG),
108 RUM_DEV(DLINK2, DWLG122C1),
109 RUM_DEV(DLINK2, WUA1340),
110 RUM_DEV(DLINK2, DWA111),
111 RUM_DEV(DLINK2, DWA110),
112 RUM_DEV(GIGABYTE, GNWB01GS),
113 RUM_DEV(GIGABYTE, GNWI05GS),
114 RUM_DEV(GIGASET, RT2573),
115 RUM_DEV(GOODWAY, RT2573),
116 RUM_DEV(GUILLEMOT, HWGUSB254LB),
117 RUM_DEV(GUILLEMOT, HWGUSB254V2AP),
118 RUM_DEV(HUAWEI3COM, WUB320G),
119 RUM_DEV(MELCO, G54HP),
120 RUM_DEV(MELCO, SG54HP),
121 RUM_DEV(MSI, RT2573_1),
122 RUM_DEV(MSI, RT2573_2),
123 RUM_DEV(MSI, RT2573_3),
124 RUM_DEV(MSI, RT2573_4),
125 RUM_DEV(NOVATECH, RT2573),
126 RUM_DEV(PLANEX2, GWUS54HP),
127 RUM_DEV(PLANEX2, GWUS54MINI2),
128 RUM_DEV(PLANEX2, GWUSMM),
129 RUM_DEV(QCOM, RT2573),
130 RUM_DEV(QCOM, RT2573_2),
131 RUM_DEV(QCOM, RT2573_3),
132 RUM_DEV(RALINK, RT2573),
133 RUM_DEV(RALINK, RT2573_2),
134 RUM_DEV(RALINK, RT2671),
135 RUM_DEV(SITECOMEU, WL113R2),
136 RUM_DEV(SITECOMEU, WL172),
137 RUM_DEV(SPARKLAN, RT2573),
138 RUM_DEV(SURECOM, RT2573),
142 MODULE_DEPEND(rum, wlan, 1, 1, 1);
143 MODULE_DEPEND(rum, usb, 1, 1, 1);
145 static device_probe_t rum_match;
146 static device_attach_t rum_attach;
147 static device_detach_t rum_detach;
149 static usb_callback_t rum_bulk_read_callback;
150 static usb_callback_t rum_bulk_write_callback;
152 static usb_error_t rum_do_request(struct rum_softc *sc,
153 struct usb_device_request *req, void *data);
154 static struct ieee80211vap *rum_vap_create(struct ieee80211com *,
155 const char name[IFNAMSIZ], int unit, int opmode,
156 int flags, const uint8_t bssid[IEEE80211_ADDR_LEN],
157 const uint8_t mac[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 int rum_newstate(struct ieee80211vap *,
163 enum ieee80211_state, int);
164 static void rum_setup_tx_desc(struct rum_softc *,
165 struct rum_tx_desc *, uint32_t, uint16_t, int,
167 static int rum_tx_mgt(struct rum_softc *, struct mbuf *,
168 struct ieee80211_node *);
169 static int rum_tx_raw(struct rum_softc *, struct mbuf *,
170 struct ieee80211_node *,
171 const struct ieee80211_bpf_params *);
172 static int rum_tx_data(struct rum_softc *, struct mbuf *,
173 struct ieee80211_node *);
174 static void rum_start(struct ifnet *);
175 static int rum_ioctl(struct ifnet *, u_long, caddr_t);
176 static void rum_eeprom_read(struct rum_softc *, uint16_t, void *,
178 static uint32_t rum_read(struct rum_softc *, uint16_t);
179 static void rum_read_multi(struct rum_softc *, uint16_t, void *,
181 static usb_error_t rum_write(struct rum_softc *, uint16_t, uint32_t);
182 static usb_error_t rum_write_multi(struct rum_softc *, uint16_t, void *,
184 static void rum_bbp_write(struct rum_softc *, uint8_t, uint8_t);
185 static uint8_t rum_bbp_read(struct rum_softc *, uint8_t);
186 static void rum_rf_write(struct rum_softc *, uint8_t, uint32_t);
187 static void rum_select_antenna(struct rum_softc *);
188 static void rum_enable_mrr(struct rum_softc *);
189 static void rum_set_txpreamble(struct rum_softc *);
190 static void rum_set_basicrates(struct rum_softc *);
191 static void rum_select_band(struct rum_softc *,
192 struct ieee80211_channel *);
193 static void rum_set_chan(struct rum_softc *,
194 struct ieee80211_channel *);
195 static void rum_enable_tsf_sync(struct rum_softc *);
196 static void rum_enable_tsf(struct rum_softc *);
197 static void rum_update_slot(struct ifnet *);
198 static void rum_set_bssid(struct rum_softc *, const uint8_t *);
199 static void rum_set_macaddr(struct rum_softc *, const uint8_t *);
200 static void rum_update_promisc(struct ifnet *);
201 static void rum_setpromisc(struct rum_softc *);
202 static const char *rum_get_rf(int);
203 static void rum_read_eeprom(struct rum_softc *);
204 static int rum_bbp_init(struct rum_softc *);
205 static void rum_init_locked(struct rum_softc *);
206 static void rum_init(void *);
207 static void rum_stop(struct rum_softc *);
208 static void rum_load_microcode(struct rum_softc *, const uint8_t *,
210 static int rum_prepare_beacon(struct rum_softc *,
211 struct ieee80211vap *);
212 static int rum_raw_xmit(struct ieee80211_node *, struct mbuf *,
213 const struct ieee80211_bpf_params *);
214 static void rum_scan_start(struct ieee80211com *);
215 static void rum_scan_end(struct ieee80211com *);
216 static void rum_set_channel(struct ieee80211com *);
217 static int rum_get_rssi(struct rum_softc *, uint8_t);
218 static void rum_ratectl_start(struct rum_softc *,
219 struct ieee80211_node *);
220 static void rum_ratectl_timeout(void *);
221 static void rum_ratectl_task(void *, int);
222 static int rum_pause(struct rum_softc *, int);
224 static const struct {
228 { RT2573_TXRX_CSR0, 0x025fb032 },
229 { RT2573_TXRX_CSR1, 0x9eaa9eaf },
230 { RT2573_TXRX_CSR2, 0x8a8b8c8d },
231 { RT2573_TXRX_CSR3, 0x00858687 },
232 { RT2573_TXRX_CSR7, 0x2e31353b },
233 { RT2573_TXRX_CSR8, 0x2a2a2a2c },
234 { RT2573_TXRX_CSR15, 0x0000000f },
235 { RT2573_MAC_CSR6, 0x00000fff },
236 { RT2573_MAC_CSR8, 0x016c030a },
237 { RT2573_MAC_CSR10, 0x00000718 },
238 { RT2573_MAC_CSR12, 0x00000004 },
239 { RT2573_MAC_CSR13, 0x00007f00 },
240 { RT2573_SEC_CSR0, 0x00000000 },
241 { RT2573_SEC_CSR1, 0x00000000 },
242 { RT2573_SEC_CSR5, 0x00000000 },
243 { RT2573_PHY_CSR1, 0x000023b0 },
244 { RT2573_PHY_CSR5, 0x00040a06 },
245 { RT2573_PHY_CSR6, 0x00080606 },
246 { RT2573_PHY_CSR7, 0x00000408 },
247 { RT2573_AIFSN_CSR, 0x00002273 },
248 { RT2573_CWMIN_CSR, 0x00002344 },
249 { RT2573_CWMAX_CSR, 0x000034aa }
252 static const struct {
284 static const struct rfprog {
286 uint32_t r1, r2, r3, r4;
288 { 1, 0x00b03, 0x001e1, 0x1a014, 0x30282 },
289 { 2, 0x00b03, 0x001e1, 0x1a014, 0x30287 },
290 { 3, 0x00b03, 0x001e2, 0x1a014, 0x30282 },
291 { 4, 0x00b03, 0x001e2, 0x1a014, 0x30287 },
292 { 5, 0x00b03, 0x001e3, 0x1a014, 0x30282 },
293 { 6, 0x00b03, 0x001e3, 0x1a014, 0x30287 },
294 { 7, 0x00b03, 0x001e4, 0x1a014, 0x30282 },
295 { 8, 0x00b03, 0x001e4, 0x1a014, 0x30287 },
296 { 9, 0x00b03, 0x001e5, 0x1a014, 0x30282 },
297 { 10, 0x00b03, 0x001e5, 0x1a014, 0x30287 },
298 { 11, 0x00b03, 0x001e6, 0x1a014, 0x30282 },
299 { 12, 0x00b03, 0x001e6, 0x1a014, 0x30287 },
300 { 13, 0x00b03, 0x001e7, 0x1a014, 0x30282 },
301 { 14, 0x00b03, 0x001e8, 0x1a014, 0x30284 },
303 { 34, 0x00b03, 0x20266, 0x36014, 0x30282 },
304 { 38, 0x00b03, 0x20267, 0x36014, 0x30284 },
305 { 42, 0x00b03, 0x20268, 0x36014, 0x30286 },
306 { 46, 0x00b03, 0x20269, 0x36014, 0x30288 },
308 { 36, 0x00b03, 0x00266, 0x26014, 0x30288 },
309 { 40, 0x00b03, 0x00268, 0x26014, 0x30280 },
310 { 44, 0x00b03, 0x00269, 0x26014, 0x30282 },
311 { 48, 0x00b03, 0x0026a, 0x26014, 0x30284 },
312 { 52, 0x00b03, 0x0026b, 0x26014, 0x30286 },
313 { 56, 0x00b03, 0x0026c, 0x26014, 0x30288 },
314 { 60, 0x00b03, 0x0026e, 0x26014, 0x30280 },
315 { 64, 0x00b03, 0x0026f, 0x26014, 0x30282 },
317 { 100, 0x00b03, 0x0028a, 0x2e014, 0x30280 },
318 { 104, 0x00b03, 0x0028b, 0x2e014, 0x30282 },
319 { 108, 0x00b03, 0x0028c, 0x2e014, 0x30284 },
320 { 112, 0x00b03, 0x0028d, 0x2e014, 0x30286 },
321 { 116, 0x00b03, 0x0028e, 0x2e014, 0x30288 },
322 { 120, 0x00b03, 0x002a0, 0x2e014, 0x30280 },
323 { 124, 0x00b03, 0x002a1, 0x2e014, 0x30282 },
324 { 128, 0x00b03, 0x002a2, 0x2e014, 0x30284 },
325 { 132, 0x00b03, 0x002a3, 0x2e014, 0x30286 },
326 { 136, 0x00b03, 0x002a4, 0x2e014, 0x30288 },
327 { 140, 0x00b03, 0x002a6, 0x2e014, 0x30280 },
329 { 149, 0x00b03, 0x002a8, 0x2e014, 0x30287 },
330 { 153, 0x00b03, 0x002a9, 0x2e014, 0x30289 },
331 { 157, 0x00b03, 0x002ab, 0x2e014, 0x30281 },
332 { 161, 0x00b03, 0x002ac, 0x2e014, 0x30283 },
333 { 165, 0x00b03, 0x002ad, 0x2e014, 0x30285 }
335 { 1, 0x00b33, 0x011e1, 0x1a014, 0x30282 },
336 { 2, 0x00b33, 0x011e1, 0x1a014, 0x30287 },
337 { 3, 0x00b33, 0x011e2, 0x1a014, 0x30282 },
338 { 4, 0x00b33, 0x011e2, 0x1a014, 0x30287 },
339 { 5, 0x00b33, 0x011e3, 0x1a014, 0x30282 },
340 { 6, 0x00b33, 0x011e3, 0x1a014, 0x30287 },
341 { 7, 0x00b33, 0x011e4, 0x1a014, 0x30282 },
342 { 8, 0x00b33, 0x011e4, 0x1a014, 0x30287 },
343 { 9, 0x00b33, 0x011e5, 0x1a014, 0x30282 },
344 { 10, 0x00b33, 0x011e5, 0x1a014, 0x30287 },
345 { 11, 0x00b33, 0x011e6, 0x1a014, 0x30282 },
346 { 12, 0x00b33, 0x011e6, 0x1a014, 0x30287 },
347 { 13, 0x00b33, 0x011e7, 0x1a014, 0x30282 },
348 { 14, 0x00b33, 0x011e8, 0x1a014, 0x30284 },
350 { 34, 0x00b33, 0x01266, 0x26014, 0x30282 },
351 { 38, 0x00b33, 0x01267, 0x26014, 0x30284 },
352 { 42, 0x00b33, 0x01268, 0x26014, 0x30286 },
353 { 46, 0x00b33, 0x01269, 0x26014, 0x30288 },
355 { 36, 0x00b33, 0x01266, 0x26014, 0x30288 },
356 { 40, 0x00b33, 0x01268, 0x26014, 0x30280 },
357 { 44, 0x00b33, 0x01269, 0x26014, 0x30282 },
358 { 48, 0x00b33, 0x0126a, 0x26014, 0x30284 },
359 { 52, 0x00b33, 0x0126b, 0x26014, 0x30286 },
360 { 56, 0x00b33, 0x0126c, 0x26014, 0x30288 },
361 { 60, 0x00b33, 0x0126e, 0x26014, 0x30280 },
362 { 64, 0x00b33, 0x0126f, 0x26014, 0x30282 },
364 { 100, 0x00b33, 0x0128a, 0x2e014, 0x30280 },
365 { 104, 0x00b33, 0x0128b, 0x2e014, 0x30282 },
366 { 108, 0x00b33, 0x0128c, 0x2e014, 0x30284 },
367 { 112, 0x00b33, 0x0128d, 0x2e014, 0x30286 },
368 { 116, 0x00b33, 0x0128e, 0x2e014, 0x30288 },
369 { 120, 0x00b33, 0x012a0, 0x2e014, 0x30280 },
370 { 124, 0x00b33, 0x012a1, 0x2e014, 0x30282 },
371 { 128, 0x00b33, 0x012a2, 0x2e014, 0x30284 },
372 { 132, 0x00b33, 0x012a3, 0x2e014, 0x30286 },
373 { 136, 0x00b33, 0x012a4, 0x2e014, 0x30288 },
374 { 140, 0x00b33, 0x012a6, 0x2e014, 0x30280 },
376 { 149, 0x00b33, 0x012a8, 0x2e014, 0x30287 },
377 { 153, 0x00b33, 0x012a9, 0x2e014, 0x30289 },
378 { 157, 0x00b33, 0x012ab, 0x2e014, 0x30281 },
379 { 161, 0x00b33, 0x012ac, 0x2e014, 0x30283 },
380 { 165, 0x00b33, 0x012ad, 0x2e014, 0x30285 }
383 static const struct usb_config rum_config[RUM_N_TRANSFER] = {
386 .endpoint = UE_ADDR_ANY,
387 .direction = UE_DIR_OUT,
388 .bufsize = (MCLBYTES + RT2573_TX_DESC_SIZE + 8),
389 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
390 .callback = rum_bulk_write_callback,
391 .timeout = 5000, /* ms */
395 .endpoint = UE_ADDR_ANY,
396 .direction = UE_DIR_IN,
397 .bufsize = (MCLBYTES + RT2573_RX_DESC_SIZE),
398 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
399 .callback = rum_bulk_read_callback,
404 rum_match(device_t self)
406 struct usb_attach_arg *uaa = device_get_ivars(self);
408 if (uaa->usb_mode != USB_MODE_HOST)
410 if (uaa->info.bConfigIndex != 0)
412 if (uaa->info.bIfaceIndex != RT2573_IFACE_INDEX)
415 return (usbd_lookup_id_by_uaa(rum_devs, sizeof(rum_devs), uaa));
419 rum_attach(device_t self)
421 struct usb_attach_arg *uaa = device_get_ivars(self);
422 struct rum_softc *sc = device_get_softc(self);
423 struct ieee80211com *ic;
425 uint8_t iface_index, bands;
429 device_set_usb_desc(self);
430 sc->sc_udev = uaa->device;
433 mtx_init(&sc->sc_mtx, device_get_nameunit(self),
434 MTX_NETWORK_LOCK, MTX_DEF);
436 iface_index = RT2573_IFACE_INDEX;
437 error = usbd_transfer_setup(uaa->device, &iface_index,
438 sc->sc_xfer, rum_config, RUM_N_TRANSFER, sc, &sc->sc_mtx);
440 device_printf(self, "could not allocate USB transfers, "
441 "err=%s\n", usbd_errstr(error));
446 /* retrieve RT2573 rev. no */
447 for (ntries = 0; ntries < 100; ntries++) {
448 if ((tmp = rum_read(sc, RT2573_MAC_CSR0)) != 0)
450 if (rum_pause(sc, hz / 100))
454 device_printf(sc->sc_dev, "timeout waiting for chip to settle\n");
459 /* retrieve MAC address and various other things from EEPROM */
462 device_printf(sc->sc_dev, "MAC/BBP RT2573 (rev 0x%05x), RF %s\n",
463 tmp, rum_get_rf(sc->rf_rev));
465 rum_load_microcode(sc, rt2573_ucode, sizeof(rt2573_ucode));
468 ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
470 device_printf(sc->sc_dev, "can not if_alloc()\n");
476 if_initname(ifp, "rum", device_get_unit(sc->sc_dev));
477 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
478 ifp->if_init = rum_init;
479 ifp->if_ioctl = rum_ioctl;
480 ifp->if_start = rum_start;
481 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
482 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
483 IFQ_SET_READY(&ifp->if_snd);
486 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
488 /* set device capabilities */
490 IEEE80211_C_STA /* station mode supported */
491 | IEEE80211_C_IBSS /* IBSS mode supported */
492 | IEEE80211_C_MONITOR /* monitor mode supported */
493 | IEEE80211_C_HOSTAP /* HostAp mode supported */
494 | IEEE80211_C_TXPMGT /* tx power management */
495 | IEEE80211_C_SHPREAMBLE /* short preamble supported */
496 | IEEE80211_C_SHSLOT /* short slot time supported */
497 | IEEE80211_C_BGSCAN /* bg scanning supported */
498 | IEEE80211_C_WPA /* 802.11i */
502 setbit(&bands, IEEE80211_MODE_11B);
503 setbit(&bands, IEEE80211_MODE_11G);
504 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_5226)
505 setbit(&bands, IEEE80211_MODE_11A);
506 ieee80211_init_channels(ic, NULL, &bands);
508 ieee80211_ifattach(ic, sc->sc_bssid);
509 ic->ic_update_promisc = rum_update_promisc;
510 ic->ic_raw_xmit = rum_raw_xmit;
511 ic->ic_scan_start = rum_scan_start;
512 ic->ic_scan_end = rum_scan_end;
513 ic->ic_set_channel = rum_set_channel;
515 ic->ic_vap_create = rum_vap_create;
516 ic->ic_vap_delete = rum_vap_delete;
518 ieee80211_radiotap_attach(ic,
519 &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
520 RT2573_TX_RADIOTAP_PRESENT,
521 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
522 RT2573_RX_RADIOTAP_PRESENT);
525 ieee80211_announce(ic);
531 return (ENXIO); /* failure */
535 rum_detach(device_t self)
537 struct rum_softc *sc = device_get_softc(self);
538 struct ifnet *ifp = sc->sc_ifp;
539 struct ieee80211com *ic;
541 /* stop all USB transfers */
542 usbd_transfer_unsetup(sc->sc_xfer, RUM_N_TRANSFER);
544 /* free TX list, if any */
546 rum_unsetup_tx_list(sc);
551 ieee80211_ifdetach(ic);
554 mtx_destroy(&sc->sc_mtx);
560 rum_do_request(struct rum_softc *sc,
561 struct usb_device_request *req, void *data)
567 err = usbd_do_request_flags(sc->sc_udev, &sc->sc_mtx,
568 req, data, 0, NULL, 250 /* ms */);
572 DPRINTFN(1, "Control request failed, %s (retrying)\n",
574 if (rum_pause(sc, hz / 100))
580 static struct ieee80211vap *
581 rum_vap_create(struct ieee80211com *ic,
582 const char name[IFNAMSIZ], int unit, int opmode, int flags,
583 const uint8_t bssid[IEEE80211_ADDR_LEN],
584 const uint8_t mac[IEEE80211_ADDR_LEN])
586 struct rum_softc *sc = ic->ic_ifp->if_softc;
588 struct ieee80211vap *vap;
590 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */
592 rvp = (struct rum_vap *) malloc(sizeof(struct rum_vap),
593 M_80211_VAP, M_NOWAIT | M_ZERO);
597 /* enable s/w bmiss handling for sta mode */
598 ieee80211_vap_setup(ic, vap, name, unit, opmode,
599 flags | IEEE80211_CLONE_NOBEACONS, bssid, mac);
601 /* override state transition machine */
602 rvp->newstate = vap->iv_newstate;
603 vap->iv_newstate = rum_newstate;
605 usb_callout_init_mtx(&rvp->ratectl_ch, &sc->sc_mtx, 0);
606 TASK_INIT(&rvp->ratectl_task, 0, rum_ratectl_task, rvp);
607 ieee80211_ratectl_init(vap);
608 ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */);
610 ieee80211_vap_attach(vap, ieee80211_media_change, ieee80211_media_status);
611 ic->ic_opmode = opmode;
616 rum_vap_delete(struct ieee80211vap *vap)
618 struct rum_vap *rvp = RUM_VAP(vap);
619 struct ieee80211com *ic = vap->iv_ic;
621 usb_callout_drain(&rvp->ratectl_ch);
622 ieee80211_draintask(ic, &rvp->ratectl_task);
623 ieee80211_ratectl_deinit(vap);
624 ieee80211_vap_detach(vap);
625 free(rvp, M_80211_VAP);
629 rum_tx_free(struct rum_tx_data *data, int txerr)
631 struct rum_softc *sc = data->sc;
633 if (data->m != NULL) {
634 if (data->m->m_flags & M_TXCB)
635 ieee80211_process_callback(data->ni, data->m,
636 txerr ? ETIMEDOUT : 0);
640 ieee80211_free_node(data->ni);
643 STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
648 rum_setup_tx_list(struct rum_softc *sc)
650 struct rum_tx_data *data;
654 STAILQ_INIT(&sc->tx_q);
655 STAILQ_INIT(&sc->tx_free);
657 for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
658 data = &sc->tx_data[i];
661 STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
667 rum_unsetup_tx_list(struct rum_softc *sc)
669 struct rum_tx_data *data;
672 /* make sure any subsequent use of the queues will fail */
674 STAILQ_INIT(&sc->tx_q);
675 STAILQ_INIT(&sc->tx_free);
677 /* free up all node references and mbufs */
678 for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
679 data = &sc->tx_data[i];
681 if (data->m != NULL) {
685 if (data->ni != NULL) {
686 ieee80211_free_node(data->ni);
693 rum_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
695 struct rum_vap *rvp = RUM_VAP(vap);
696 struct ieee80211com *ic = vap->iv_ic;
697 struct rum_softc *sc = ic->ic_ifp->if_softc;
698 const struct ieee80211_txparam *tp;
699 enum ieee80211_state ostate;
700 struct ieee80211_node *ni;
703 ostate = vap->iv_state;
704 DPRINTF("%s -> %s\n",
705 ieee80211_state_name[ostate],
706 ieee80211_state_name[nstate]);
708 IEEE80211_UNLOCK(ic);
710 usb_callout_stop(&rvp->ratectl_ch);
713 case IEEE80211_S_INIT:
714 if (ostate == IEEE80211_S_RUN) {
715 /* abort TSF synchronization */
716 tmp = rum_read(sc, RT2573_TXRX_CSR9);
717 rum_write(sc, RT2573_TXRX_CSR9, tmp & ~0x00ffffff);
721 case IEEE80211_S_RUN:
724 if (vap->iv_opmode != IEEE80211_M_MONITOR) {
725 rum_update_slot(ic->ic_ifp);
727 rum_set_txpreamble(sc);
728 rum_set_basicrates(sc);
729 IEEE80211_ADDR_COPY(sc->sc_bssid, ni->ni_bssid);
730 rum_set_bssid(sc, sc->sc_bssid);
733 if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
734 vap->iv_opmode == IEEE80211_M_IBSS)
735 rum_prepare_beacon(sc, vap);
737 if (vap->iv_opmode != IEEE80211_M_MONITOR)
738 rum_enable_tsf_sync(sc);
742 /* enable automatic rate adaptation */
743 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
744 if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
745 rum_ratectl_start(sc, ni);
752 return (rvp->newstate(vap, nstate, arg));
756 rum_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
758 struct rum_softc *sc = usbd_xfer_softc(xfer);
759 struct ifnet *ifp = sc->sc_ifp;
760 struct ieee80211vap *vap;
761 struct rum_tx_data *data;
763 struct usb_page_cache *pc;
767 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
769 switch (USB_GET_STATE(xfer)) {
770 case USB_ST_TRANSFERRED:
771 DPRINTFN(11, "transfer complete, %d bytes\n", actlen);
774 data = usbd_xfer_get_priv(xfer);
775 rum_tx_free(data, 0);
776 usbd_xfer_set_priv(xfer, NULL);
779 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
784 data = STAILQ_FIRST(&sc->tx_q);
786 STAILQ_REMOVE_HEAD(&sc->tx_q, next);
789 if (m->m_pkthdr.len > (MCLBYTES + RT2573_TX_DESC_SIZE)) {
790 DPRINTFN(0, "data overflow, %u bytes\n",
792 m->m_pkthdr.len = (MCLBYTES + RT2573_TX_DESC_SIZE);
794 pc = usbd_xfer_get_frame(xfer, 0);
795 usbd_copy_in(pc, 0, &data->desc, RT2573_TX_DESC_SIZE);
796 usbd_m_copy_in(pc, RT2573_TX_DESC_SIZE, m, 0,
799 vap = data->ni->ni_vap;
800 if (ieee80211_radiotap_active_vap(vap)) {
801 struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
804 tap->wt_rate = data->rate;
805 tap->wt_antenna = sc->tx_ant;
807 ieee80211_radiotap_tx(vap, m);
810 /* align end on a 4-bytes boundary */
811 len = (RT2573_TX_DESC_SIZE + m->m_pkthdr.len + 3) & ~3;
815 DPRINTFN(11, "sending frame len=%u xferlen=%u\n",
816 m->m_pkthdr.len, len);
818 usbd_xfer_set_frame_len(xfer, 0, len);
819 usbd_xfer_set_priv(xfer, data);
821 usbd_transfer_submit(xfer);
829 DPRINTFN(11, "transfer error, %s\n",
833 data = usbd_xfer_get_priv(xfer);
835 rum_tx_free(data, error);
836 usbd_xfer_set_priv(xfer, NULL);
839 if (error != USB_ERR_CANCELLED) {
840 if (error == USB_ERR_TIMEOUT)
841 device_printf(sc->sc_dev, "device timeout\n");
844 * Try to clear stall first, also if other
845 * errors occur, hence clearing stall
846 * introduces a 50 ms delay:
848 usbd_xfer_set_stall(xfer);
856 rum_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
858 struct rum_softc *sc = usbd_xfer_softc(xfer);
859 struct ifnet *ifp = sc->sc_ifp;
860 struct ieee80211com *ic = ifp->if_l2com;
861 struct ieee80211_node *ni;
862 struct mbuf *m = NULL;
863 struct usb_page_cache *pc;
868 usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
870 switch (USB_GET_STATE(xfer)) {
871 case USB_ST_TRANSFERRED:
873 DPRINTFN(15, "rx done, actlen=%d\n", len);
875 if (len < RT2573_RX_DESC_SIZE + IEEE80211_MIN_LEN) {
876 DPRINTF("%s: xfer too short %d\n",
877 device_get_nameunit(sc->sc_dev), len);
882 len -= RT2573_RX_DESC_SIZE;
883 pc = usbd_xfer_get_frame(xfer, 0);
884 usbd_copy_out(pc, 0, &sc->sc_rx_desc, RT2573_RX_DESC_SIZE);
886 rssi = rum_get_rssi(sc, sc->sc_rx_desc.rssi);
887 flags = le32toh(sc->sc_rx_desc.flags);
888 if (flags & RT2573_RX_CRC_ERROR) {
890 * This should not happen since we did not
891 * request to receive those frames when we
892 * filled RUM_TXRX_CSR2:
894 DPRINTFN(5, "PHY or CRC error\n");
899 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
901 DPRINTF("could not allocate mbuf\n");
905 usbd_copy_out(pc, RT2573_RX_DESC_SIZE,
906 mtod(m, uint8_t *), len);
909 m->m_pkthdr.rcvif = ifp;
910 m->m_pkthdr.len = m->m_len = (flags >> 16) & 0xfff;
912 if (ieee80211_radiotap_active(ic)) {
913 struct rum_rx_radiotap_header *tap = &sc->sc_rxtap;
917 tap->wr_rate = ieee80211_plcp2rate(sc->sc_rx_desc.rate,
918 (flags & RT2573_RX_OFDM) ?
919 IEEE80211_T_OFDM : IEEE80211_T_CCK);
920 tap->wr_antsignal = RT2573_NOISE_FLOOR + rssi;
921 tap->wr_antnoise = RT2573_NOISE_FLOOR;
922 tap->wr_antenna = sc->rx_ant;
927 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
928 usbd_transfer_submit(xfer);
931 * At the end of a USB callback it is always safe to unlock
932 * the private mutex of a device! That is why we do the
933 * "ieee80211_input" here, and not some lines up!
937 ni = ieee80211_find_rxnode(ic,
938 mtod(m, struct ieee80211_frame_min *));
940 (void) ieee80211_input(ni, m, rssi,
942 ieee80211_free_node(ni);
944 (void) ieee80211_input_all(ic, m, rssi,
947 if ((ifp->if_drv_flags & IFF_DRV_OACTIVE) == 0 &&
948 !IFQ_IS_EMPTY(&ifp->if_snd))
954 if (error != USB_ERR_CANCELLED) {
955 /* try to clear stall first */
956 usbd_xfer_set_stall(xfer);
964 rum_plcp_signal(int rate)
967 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
975 case 108: return 0xc;
977 /* CCK rates (NB: not IEEE std, device-specific) */
983 return 0xff; /* XXX unsupported/unknown rate */
987 rum_setup_tx_desc(struct rum_softc *sc, struct rum_tx_desc *desc,
988 uint32_t flags, uint16_t xflags, int len, int rate)
990 struct ifnet *ifp = sc->sc_ifp;
991 struct ieee80211com *ic = ifp->if_l2com;
992 uint16_t plcp_length;
995 desc->flags = htole32(flags);
996 desc->flags |= htole32(RT2573_TX_VALID);
997 desc->flags |= htole32(len << 16);
999 desc->xflags = htole16(xflags);
1001 desc->wme = htole16(RT2573_QID(0) | RT2573_AIFSN(2) |
1002 RT2573_LOGCWMIN(4) | RT2573_LOGCWMAX(10));
1004 /* setup PLCP fields */
1005 desc->plcp_signal = rum_plcp_signal(rate);
1006 desc->plcp_service = 4;
1008 len += IEEE80211_CRC_LEN;
1009 if (ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) {
1010 desc->flags |= htole32(RT2573_TX_OFDM);
1012 plcp_length = len & 0xfff;
1013 desc->plcp_length_hi = plcp_length >> 6;
1014 desc->plcp_length_lo = plcp_length & 0x3f;
1016 plcp_length = (16 * len + rate - 1) / rate;
1018 remainder = (16 * len) % 22;
1019 if (remainder != 0 && remainder < 7)
1020 desc->plcp_service |= RT2573_PLCP_LENGEXT;
1022 desc->plcp_length_hi = plcp_length >> 8;
1023 desc->plcp_length_lo = plcp_length & 0xff;
1025 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1026 desc->plcp_signal |= 0x08;
1031 rum_sendprot(struct rum_softc *sc,
1032 const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate)
1034 struct ieee80211com *ic = ni->ni_ic;
1035 const struct ieee80211_frame *wh;
1036 struct rum_tx_data *data;
1038 int protrate, ackrate, pktlen, flags, isshort;
1041 RUM_LOCK_ASSERT(sc, MA_OWNED);
1042 KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY,
1043 ("protection %d", prot));
1045 wh = mtod(m, const struct ieee80211_frame *);
1046 pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
1048 protrate = ieee80211_ctl_rate(ic->ic_rt, rate);
1049 ackrate = ieee80211_ack_rate(ic->ic_rt, rate);
1051 isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0;
1052 dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort);
1053 + ieee80211_ack_duration(ic->ic_rt, rate, isshort);
1054 flags = RT2573_TX_MORE_FRAG;
1055 if (prot == IEEE80211_PROT_RTSCTS) {
1056 /* NB: CTS is the same size as an ACK */
1057 dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort);
1058 flags |= RT2573_TX_NEED_ACK;
1059 mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur);
1061 mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur);
1063 if (mprot == NULL) {
1064 /* XXX stat + msg */
1067 data = STAILQ_FIRST(&sc->tx_free);
1068 STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1072 data->ni = ieee80211_ref_node(ni);
1073 data->rate = protrate;
1074 rum_setup_tx_desc(sc, &data->desc, flags, 0, mprot->m_pkthdr.len, protrate);
1076 STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1077 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1083 rum_tx_mgt(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1085 struct ieee80211vap *vap = ni->ni_vap;
1086 struct ifnet *ifp = sc->sc_ifp;
1087 struct ieee80211com *ic = ifp->if_l2com;
1088 struct rum_tx_data *data;
1089 struct ieee80211_frame *wh;
1090 const struct ieee80211_txparam *tp;
1091 struct ieee80211_key *k;
1095 RUM_LOCK_ASSERT(sc, MA_OWNED);
1097 data = STAILQ_FIRST(&sc->tx_free);
1098 STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1101 wh = mtod(m0, struct ieee80211_frame *);
1102 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1103 k = ieee80211_crypto_encap(ni, m0);
1108 wh = mtod(m0, struct ieee80211_frame *);
1111 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)];
1113 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1114 flags |= RT2573_TX_NEED_ACK;
1116 dur = ieee80211_ack_duration(ic->ic_rt, tp->mgmtrate,
1117 ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1118 *(uint16_t *)wh->i_dur = htole16(dur);
1120 /* tell hardware to add timestamp for probe responses */
1122 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
1123 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
1124 flags |= RT2573_TX_TIMESTAMP;
1129 data->rate = tp->mgmtrate;
1131 rum_setup_tx_desc(sc, &data->desc, flags, 0, m0->m_pkthdr.len, tp->mgmtrate);
1133 DPRINTFN(10, "sending mgt frame len=%d rate=%d\n",
1134 m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, tp->mgmtrate);
1136 STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1137 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1143 rum_tx_raw(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
1144 const struct ieee80211_bpf_params *params)
1146 struct ieee80211com *ic = ni->ni_ic;
1147 struct rum_tx_data *data;
1151 RUM_LOCK_ASSERT(sc, MA_OWNED);
1152 KASSERT(params != NULL, ("no raw xmit params"));
1154 rate = params->ibp_rate0;
1155 if (!ieee80211_isratevalid(ic->ic_rt, rate)) {
1160 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
1161 flags |= RT2573_TX_NEED_ACK;
1162 if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) {
1163 error = rum_sendprot(sc, m0, ni,
1164 params->ibp_flags & IEEE80211_BPF_RTS ?
1165 IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY,
1167 if (error || sc->tx_nfree == 0) {
1171 flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
1174 data = STAILQ_FIRST(&sc->tx_free);
1175 STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1182 /* XXX need to setup descriptor ourself */
1183 rum_setup_tx_desc(sc, &data->desc, flags, 0, m0->m_pkthdr.len, rate);
1185 DPRINTFN(10, "sending raw frame len=%u rate=%u\n",
1186 m0->m_pkthdr.len, rate);
1188 STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1189 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1195 rum_tx_data(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1197 struct ieee80211vap *vap = ni->ni_vap;
1198 struct ifnet *ifp = sc->sc_ifp;
1199 struct ieee80211com *ic = ifp->if_l2com;
1200 struct rum_tx_data *data;
1201 struct ieee80211_frame *wh;
1202 const struct ieee80211_txparam *tp;
1203 struct ieee80211_key *k;
1208 RUM_LOCK_ASSERT(sc, MA_OWNED);
1210 wh = mtod(m0, struct ieee80211_frame *);
1212 tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
1213 if (IEEE80211_IS_MULTICAST(wh->i_addr1))
1214 rate = tp->mcastrate;
1215 else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
1216 rate = tp->ucastrate;
1218 rate = ni->ni_txrate;
1220 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1221 k = ieee80211_crypto_encap(ni, m0);
1227 /* packet header may have moved, reset our local pointer */
1228 wh = mtod(m0, struct ieee80211_frame *);
1231 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1232 int prot = IEEE80211_PROT_NONE;
1233 if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold)
1234 prot = IEEE80211_PROT_RTSCTS;
1235 else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
1236 ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM)
1237 prot = ic->ic_protmode;
1238 if (prot != IEEE80211_PROT_NONE) {
1239 error = rum_sendprot(sc, m0, ni, prot, rate);
1240 if (error || sc->tx_nfree == 0) {
1244 flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
1248 data = STAILQ_FIRST(&sc->tx_free);
1249 STAILQ_REMOVE_HEAD(&sc->tx_free, next);
1256 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1257 flags |= RT2573_TX_NEED_ACK;
1258 flags |= RT2573_TX_MORE_FRAG;
1260 dur = ieee80211_ack_duration(ic->ic_rt, rate,
1261 ic->ic_flags & IEEE80211_F_SHPREAMBLE);
1262 *(uint16_t *)wh->i_dur = htole16(dur);
1265 rum_setup_tx_desc(sc, &data->desc, flags, 0, m0->m_pkthdr.len, rate);
1267 DPRINTFN(10, "sending frame len=%d rate=%d\n",
1268 m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, rate);
1270 STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
1271 usbd_transfer_start(sc->sc_xfer[RUM_BULK_WR]);
1277 rum_start(struct ifnet *ifp)
1279 struct rum_softc *sc = ifp->if_softc;
1280 struct ieee80211_node *ni;
1284 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1289 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
1292 if (sc->tx_nfree < RUM_TX_MINFREE) {
1293 IFQ_DRV_PREPEND(&ifp->if_snd, m);
1294 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1297 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1298 if (rum_tx_data(sc, m, ni) != 0) {
1299 ieee80211_free_node(ni);
1308 rum_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1310 struct rum_softc *sc = ifp->if_softc;
1311 struct ieee80211com *ic = ifp->if_l2com;
1312 struct ifreq *ifr = (struct ifreq *) data;
1313 int error = 0, startall = 0;
1318 if (ifp->if_flags & IFF_UP) {
1319 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1320 rum_init_locked(sc);
1325 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1330 ieee80211_start_all(ic);
1333 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
1336 error = ether_ioctl(ifp, cmd, data);
1346 rum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len)
1348 struct usb_device_request req;
1351 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1352 req.bRequest = RT2573_READ_EEPROM;
1353 USETW(req.wValue, 0);
1354 USETW(req.wIndex, addr);
1355 USETW(req.wLength, len);
1357 error = rum_do_request(sc, &req, buf);
1359 device_printf(sc->sc_dev, "could not read EEPROM: %s\n",
1360 usbd_errstr(error));
1365 rum_read(struct rum_softc *sc, uint16_t reg)
1369 rum_read_multi(sc, reg, &val, sizeof val);
1371 return le32toh(val);
1375 rum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len)
1377 struct usb_device_request req;
1380 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1381 req.bRequest = RT2573_READ_MULTI_MAC;
1382 USETW(req.wValue, 0);
1383 USETW(req.wIndex, reg);
1384 USETW(req.wLength, len);
1386 error = rum_do_request(sc, &req, buf);
1388 device_printf(sc->sc_dev,
1389 "could not multi read MAC register: %s\n",
1390 usbd_errstr(error));
1395 rum_write(struct rum_softc *sc, uint16_t reg, uint32_t val)
1397 uint32_t tmp = htole32(val);
1399 return (rum_write_multi(sc, reg, &tmp, sizeof tmp));
1403 rum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len)
1405 struct usb_device_request req;
1408 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1409 req.bRequest = RT2573_WRITE_MULTI_MAC;
1410 USETW(req.wValue, 0);
1411 USETW(req.wIndex, reg);
1412 USETW(req.wLength, len);
1414 error = rum_do_request(sc, &req, buf);
1416 device_printf(sc->sc_dev,
1417 "could not multi write MAC register: %s\n",
1418 usbd_errstr(error));
1424 rum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val)
1429 DPRINTFN(2, "reg=0x%08x\n", reg);
1431 for (ntries = 0; ntries < 100; ntries++) {
1432 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1434 if (rum_pause(sc, hz / 100))
1437 if (ntries == 100) {
1438 device_printf(sc->sc_dev, "could not write to BBP\n");
1442 tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val;
1443 rum_write(sc, RT2573_PHY_CSR3, tmp);
1447 rum_bbp_read(struct rum_softc *sc, uint8_t reg)
1452 DPRINTFN(2, "reg=0x%08x\n", reg);
1454 for (ntries = 0; ntries < 100; ntries++) {
1455 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1457 if (rum_pause(sc, hz / 100))
1460 if (ntries == 100) {
1461 device_printf(sc->sc_dev, "could not read BBP\n");
1465 val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8;
1466 rum_write(sc, RT2573_PHY_CSR3, val);
1468 for (ntries = 0; ntries < 100; ntries++) {
1469 val = rum_read(sc, RT2573_PHY_CSR3);
1470 if (!(val & RT2573_BBP_BUSY))
1472 if (rum_pause(sc, hz / 100))
1476 device_printf(sc->sc_dev, "could not read BBP\n");
1481 rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val)
1486 for (ntries = 0; ntries < 100; ntries++) {
1487 if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY))
1489 if (rum_pause(sc, hz / 100))
1492 if (ntries == 100) {
1493 device_printf(sc->sc_dev, "could not write to RF\n");
1497 tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 |
1499 rum_write(sc, RT2573_PHY_CSR4, tmp);
1501 /* remember last written value in sc */
1502 sc->rf_regs[reg] = val;
1504 DPRINTFN(15, "RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff);
1508 rum_select_antenna(struct rum_softc *sc)
1510 uint8_t bbp4, bbp77;
1513 bbp4 = rum_bbp_read(sc, 4);
1514 bbp77 = rum_bbp_read(sc, 77);
1518 /* make sure Rx is disabled before switching antenna */
1519 tmp = rum_read(sc, RT2573_TXRX_CSR0);
1520 rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
1522 rum_bbp_write(sc, 4, bbp4);
1523 rum_bbp_write(sc, 77, bbp77);
1525 rum_write(sc, RT2573_TXRX_CSR0, tmp);
1529 * Enable multi-rate retries for frames sent at OFDM rates.
1530 * In 802.11b/g mode, allow fallback to CCK rates.
1533 rum_enable_mrr(struct rum_softc *sc)
1535 struct ifnet *ifp = sc->sc_ifp;
1536 struct ieee80211com *ic = ifp->if_l2com;
1539 tmp = rum_read(sc, RT2573_TXRX_CSR4);
1541 tmp &= ~RT2573_MRR_CCK_FALLBACK;
1542 if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan))
1543 tmp |= RT2573_MRR_CCK_FALLBACK;
1544 tmp |= RT2573_MRR_ENABLED;
1546 rum_write(sc, RT2573_TXRX_CSR4, tmp);
1550 rum_set_txpreamble(struct rum_softc *sc)
1552 struct ifnet *ifp = sc->sc_ifp;
1553 struct ieee80211com *ic = ifp->if_l2com;
1556 tmp = rum_read(sc, RT2573_TXRX_CSR4);
1558 tmp &= ~RT2573_SHORT_PREAMBLE;
1559 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
1560 tmp |= RT2573_SHORT_PREAMBLE;
1562 rum_write(sc, RT2573_TXRX_CSR4, tmp);
1566 rum_set_basicrates(struct rum_softc *sc)
1568 struct ifnet *ifp = sc->sc_ifp;
1569 struct ieee80211com *ic = ifp->if_l2com;
1571 /* update basic rate set */
1572 if (ic->ic_curmode == IEEE80211_MODE_11B) {
1573 /* 11b basic rates: 1, 2Mbps */
1574 rum_write(sc, RT2573_TXRX_CSR5, 0x3);
1575 } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bsschan)) {
1576 /* 11a basic rates: 6, 12, 24Mbps */
1577 rum_write(sc, RT2573_TXRX_CSR5, 0x150);
1579 /* 11b/g basic rates: 1, 2, 5.5, 11Mbps */
1580 rum_write(sc, RT2573_TXRX_CSR5, 0xf);
1585 * Reprogram MAC/BBP to switch to a new band. Values taken from the reference
1589 rum_select_band(struct rum_softc *sc, struct ieee80211_channel *c)
1591 uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104;
1594 /* update all BBP registers that depend on the band */
1595 bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c;
1596 bbp35 = 0x50; bbp97 = 0x48; bbp98 = 0x48;
1597 if (IEEE80211_IS_CHAN_5GHZ(c)) {
1598 bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c;
1599 bbp35 += 0x10; bbp97 += 0x10; bbp98 += 0x10;
1601 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1602 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1603 bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10;
1607 rum_bbp_write(sc, 17, bbp17);
1608 rum_bbp_write(sc, 96, bbp96);
1609 rum_bbp_write(sc, 104, bbp104);
1611 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1612 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1613 rum_bbp_write(sc, 75, 0x80);
1614 rum_bbp_write(sc, 86, 0x80);
1615 rum_bbp_write(sc, 88, 0x80);
1618 rum_bbp_write(sc, 35, bbp35);
1619 rum_bbp_write(sc, 97, bbp97);
1620 rum_bbp_write(sc, 98, bbp98);
1622 tmp = rum_read(sc, RT2573_PHY_CSR0);
1623 tmp &= ~(RT2573_PA_PE_2GHZ | RT2573_PA_PE_5GHZ);
1624 if (IEEE80211_IS_CHAN_2GHZ(c))
1625 tmp |= RT2573_PA_PE_2GHZ;
1627 tmp |= RT2573_PA_PE_5GHZ;
1628 rum_write(sc, RT2573_PHY_CSR0, tmp);
1632 rum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c)
1634 struct ifnet *ifp = sc->sc_ifp;
1635 struct ieee80211com *ic = ifp->if_l2com;
1636 const struct rfprog *rfprog;
1637 uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT;
1641 chan = ieee80211_chan2ieee(ic, c);
1642 if (chan == 0 || chan == IEEE80211_CHAN_ANY)
1645 /* select the appropriate RF settings based on what EEPROM says */
1646 rfprog = (sc->rf_rev == RT2573_RF_5225 ||
1647 sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226;
1649 /* find the settings for this channel (we know it exists) */
1650 for (i = 0; rfprog[i].chan != chan; i++);
1652 power = sc->txpow[i];
1656 } else if (power > 31) {
1657 bbp94 += power - 31;
1662 * If we are switching from the 2GHz band to the 5GHz band or
1663 * vice-versa, BBP registers need to be reprogrammed.
1665 if (c->ic_flags != ic->ic_curchan->ic_flags) {
1666 rum_select_band(sc, c);
1667 rum_select_antenna(sc);
1671 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1672 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1673 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1674 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1676 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1677 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1678 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7 | 1);
1679 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1681 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1682 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1683 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1684 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1686 rum_pause(sc, hz / 100);
1688 /* enable smart mode for MIMO-capable RFs */
1689 bbp3 = rum_bbp_read(sc, 3);
1691 bbp3 &= ~RT2573_SMART_MODE;
1692 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527)
1693 bbp3 |= RT2573_SMART_MODE;
1695 rum_bbp_write(sc, 3, bbp3);
1697 if (bbp94 != RT2573_BBPR94_DEFAULT)
1698 rum_bbp_write(sc, 94, bbp94);
1700 /* give the chip some extra time to do the switchover */
1701 rum_pause(sc, hz / 100);
1705 * Enable TSF synchronization and tell h/w to start sending beacons for IBSS
1706 * and HostAP operating modes.
1709 rum_enable_tsf_sync(struct rum_softc *sc)
1711 struct ifnet *ifp = sc->sc_ifp;
1712 struct ieee80211com *ic = ifp->if_l2com;
1713 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
1716 if (vap->iv_opmode != IEEE80211_M_STA) {
1718 * Change default 16ms TBTT adjustment to 8ms.
1719 * Must be done before enabling beacon generation.
1721 rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8);
1724 tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000;
1726 /* set beacon interval (in 1/16ms unit) */
1727 tmp |= vap->iv_bss->ni_intval * 16;
1729 tmp |= RT2573_TSF_TICKING | RT2573_ENABLE_TBTT;
1730 if (vap->iv_opmode == IEEE80211_M_STA)
1731 tmp |= RT2573_TSF_MODE(1);
1733 tmp |= RT2573_TSF_MODE(2) | RT2573_GENERATE_BEACON;
1735 rum_write(sc, RT2573_TXRX_CSR9, tmp);
1739 rum_enable_tsf(struct rum_softc *sc)
1741 rum_write(sc, RT2573_TXRX_CSR9,
1742 (rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000) |
1743 RT2573_TSF_TICKING | RT2573_TSF_MODE(2));
1747 rum_update_slot(struct ifnet *ifp)
1749 struct rum_softc *sc = ifp->if_softc;
1750 struct ieee80211com *ic = ifp->if_l2com;
1754 slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
1756 tmp = rum_read(sc, RT2573_MAC_CSR9);
1757 tmp = (tmp & ~0xff) | slottime;
1758 rum_write(sc, RT2573_MAC_CSR9, tmp);
1760 DPRINTF("setting slot time to %uus\n", slottime);
1764 rum_set_bssid(struct rum_softc *sc, const uint8_t *bssid)
1768 tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24;
1769 rum_write(sc, RT2573_MAC_CSR4, tmp);
1771 tmp = bssid[4] | bssid[5] << 8 | RT2573_ONE_BSSID << 16;
1772 rum_write(sc, RT2573_MAC_CSR5, tmp);
1776 rum_set_macaddr(struct rum_softc *sc, const uint8_t *addr)
1780 tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24;
1781 rum_write(sc, RT2573_MAC_CSR2, tmp);
1783 tmp = addr[4] | addr[5] << 8 | 0xff << 16;
1784 rum_write(sc, RT2573_MAC_CSR3, tmp);
1788 rum_setpromisc(struct rum_softc *sc)
1790 struct ifnet *ifp = sc->sc_ifp;
1793 tmp = rum_read(sc, RT2573_TXRX_CSR0);
1795 tmp &= ~RT2573_DROP_NOT_TO_ME;
1796 if (!(ifp->if_flags & IFF_PROMISC))
1797 tmp |= RT2573_DROP_NOT_TO_ME;
1799 rum_write(sc, RT2573_TXRX_CSR0, tmp);
1801 DPRINTF("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
1802 "entering" : "leaving");
1806 rum_update_promisc(struct ifnet *ifp)
1808 struct rum_softc *sc = ifp->if_softc;
1810 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1822 case RT2573_RF_2527: return "RT2527 (MIMO XR)";
1823 case RT2573_RF_2528: return "RT2528";
1824 case RT2573_RF_5225: return "RT5225 (MIMO XR)";
1825 case RT2573_RF_5226: return "RT5226";
1826 default: return "unknown";
1831 rum_read_eeprom(struct rum_softc *sc)
1838 /* read MAC address */
1839 rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, sc->sc_bssid, 6);
1841 rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2);
1843 sc->rf_rev = (val >> 11) & 0x1f;
1844 sc->hw_radio = (val >> 10) & 0x1;
1845 sc->rx_ant = (val >> 4) & 0x3;
1846 sc->tx_ant = (val >> 2) & 0x3;
1847 sc->nb_ant = val & 0x3;
1849 DPRINTF("RF revision=%d\n", sc->rf_rev);
1851 rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2);
1853 sc->ext_5ghz_lna = (val >> 6) & 0x1;
1854 sc->ext_2ghz_lna = (val >> 4) & 0x1;
1856 DPRINTF("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n",
1857 sc->ext_2ghz_lna, sc->ext_5ghz_lna);
1859 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2);
1861 if ((val & 0xff) != 0xff)
1862 sc->rssi_2ghz_corr = (int8_t)(val & 0xff); /* signed */
1864 /* Only [-10, 10] is valid */
1865 if (sc->rssi_2ghz_corr < -10 || sc->rssi_2ghz_corr > 10)
1866 sc->rssi_2ghz_corr = 0;
1868 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2);
1870 if ((val & 0xff) != 0xff)
1871 sc->rssi_5ghz_corr = (int8_t)(val & 0xff); /* signed */
1873 /* Only [-10, 10] is valid */
1874 if (sc->rssi_5ghz_corr < -10 || sc->rssi_5ghz_corr > 10)
1875 sc->rssi_5ghz_corr = 0;
1877 if (sc->ext_2ghz_lna)
1878 sc->rssi_2ghz_corr -= 14;
1879 if (sc->ext_5ghz_lna)
1880 sc->rssi_5ghz_corr -= 14;
1882 DPRINTF("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n",
1883 sc->rssi_2ghz_corr, sc->rssi_5ghz_corr);
1885 rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2);
1887 if ((val & 0xff) != 0xff)
1888 sc->rffreq = val & 0xff;
1890 DPRINTF("RF freq=%d\n", sc->rffreq);
1892 /* read Tx power for all a/b/g channels */
1893 rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14);
1894 /* XXX default Tx power for 802.11a channels */
1895 memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14);
1897 for (i = 0; i < 14; i++)
1898 DPRINTF("Channel=%d Tx power=%d\n", i + 1, sc->txpow[i]);
1901 /* read default values for BBP registers */
1902 rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
1904 for (i = 0; i < 14; i++) {
1905 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
1907 DPRINTF("BBP R%d=%02x\n", sc->bbp_prom[i].reg,
1908 sc->bbp_prom[i].val);
1914 rum_bbp_init(struct rum_softc *sc)
1916 #define N(a) (sizeof (a) / sizeof ((a)[0]))
1919 /* wait for BBP to be ready */
1920 for (ntries = 0; ntries < 100; ntries++) {
1921 const uint8_t val = rum_bbp_read(sc, 0);
1922 if (val != 0 && val != 0xff)
1924 if (rum_pause(sc, hz / 100))
1927 if (ntries == 100) {
1928 device_printf(sc->sc_dev, "timeout waiting for BBP\n");
1932 /* initialize BBP registers to default values */
1933 for (i = 0; i < N(rum_def_bbp); i++)
1934 rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val);
1936 /* write vendor-specific BBP values (from EEPROM) */
1937 for (i = 0; i < 16; i++) {
1938 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
1940 rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
1948 rum_init_locked(struct rum_softc *sc)
1950 #define N(a) (sizeof (a) / sizeof ((a)[0]))
1951 struct ifnet *ifp = sc->sc_ifp;
1952 struct ieee80211com *ic = ifp->if_l2com;
1957 RUM_LOCK_ASSERT(sc, MA_OWNED);
1961 /* initialize MAC registers to default values */
1962 for (i = 0; i < N(rum_def_mac); i++)
1963 rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val);
1965 /* set host ready */
1966 rum_write(sc, RT2573_MAC_CSR1, 3);
1967 rum_write(sc, RT2573_MAC_CSR1, 0);
1969 /* wait for BBP/RF to wakeup */
1970 for (ntries = 0; ntries < 100; ntries++) {
1971 if (rum_read(sc, RT2573_MAC_CSR12) & 8)
1973 rum_write(sc, RT2573_MAC_CSR12, 4); /* force wakeup */
1974 if (rum_pause(sc, hz / 100))
1977 if (ntries == 100) {
1978 device_printf(sc->sc_dev,
1979 "timeout waiting for BBP/RF to wakeup\n");
1983 if ((error = rum_bbp_init(sc)) != 0)
1986 /* select default channel */
1987 rum_select_band(sc, ic->ic_curchan);
1988 rum_select_antenna(sc);
1989 rum_set_chan(sc, ic->ic_curchan);
1991 /* clear STA registers */
1992 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
1994 rum_set_macaddr(sc, IF_LLADDR(ifp));
1996 /* initialize ASIC */
1997 rum_write(sc, RT2573_MAC_CSR1, 4);
2000 * Allocate Tx and Rx xfer queues.
2002 rum_setup_tx_list(sc);
2004 /* update Rx filter */
2005 tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff;
2007 tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR;
2008 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2009 tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR |
2011 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2012 tmp |= RT2573_DROP_TODS;
2013 if (!(ifp->if_flags & IFF_PROMISC))
2014 tmp |= RT2573_DROP_NOT_TO_ME;
2016 rum_write(sc, RT2573_TXRX_CSR0, tmp);
2018 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2019 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2020 usbd_xfer_set_stall(sc->sc_xfer[RUM_BULK_WR]);
2021 usbd_transfer_start(sc->sc_xfer[RUM_BULK_RD]);
2029 rum_init(void *priv)
2031 struct rum_softc *sc = priv;
2032 struct ifnet *ifp = sc->sc_ifp;
2033 struct ieee80211com *ic = ifp->if_l2com;
2036 rum_init_locked(sc);
2039 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2040 ieee80211_start_all(ic); /* start all vap's */
2044 rum_stop(struct rum_softc *sc)
2046 struct ifnet *ifp = sc->sc_ifp;
2049 RUM_LOCK_ASSERT(sc, MA_OWNED);
2051 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2056 * Drain the USB transfers, if not already drained:
2058 usbd_transfer_drain(sc->sc_xfer[RUM_BULK_WR]);
2059 usbd_transfer_drain(sc->sc_xfer[RUM_BULK_RD]);
2063 rum_unsetup_tx_list(sc);
2066 tmp = rum_read(sc, RT2573_TXRX_CSR0);
2067 rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
2070 rum_write(sc, RT2573_MAC_CSR1, 3);
2071 rum_write(sc, RT2573_MAC_CSR1, 0);
2075 rum_load_microcode(struct rum_softc *sc, const uint8_t *ucode, size_t size)
2077 struct usb_device_request req;
2078 uint16_t reg = RT2573_MCU_CODE_BASE;
2081 /* copy firmware image into NIC */
2082 for (; size >= 4; reg += 4, ucode += 4, size -= 4) {
2083 err = rum_write(sc, reg, UGETDW(ucode));
2085 /* firmware already loaded ? */
2086 device_printf(sc->sc_dev, "Firmware load "
2087 "failure! (ignored)\n");
2092 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2093 req.bRequest = RT2573_MCU_CNTL;
2094 USETW(req.wValue, RT2573_MCU_RUN);
2095 USETW(req.wIndex, 0);
2096 USETW(req.wLength, 0);
2098 err = rum_do_request(sc, &req, NULL);
2100 device_printf(sc->sc_dev, "could not run firmware: %s\n",
2104 /* give the chip some time to boot */
2105 rum_pause(sc, hz / 8);
2109 rum_prepare_beacon(struct rum_softc *sc, struct ieee80211vap *vap)
2111 struct ieee80211com *ic = vap->iv_ic;
2112 const struct ieee80211_txparam *tp;
2113 struct rum_tx_desc desc;
2116 m0 = ieee80211_beacon_alloc(vap->iv_bss, &RUM_VAP(vap)->bo);
2121 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)];
2122 rum_setup_tx_desc(sc, &desc, RT2573_TX_TIMESTAMP, RT2573_TX_HWSEQ,
2123 m0->m_pkthdr.len, tp->mgmtrate);
2125 /* copy the first 24 bytes of Tx descriptor into NIC memory */
2126 rum_write_multi(sc, RT2573_HW_BEACON_BASE0, (uint8_t *)&desc, 24);
2128 /* copy beacon header and payload into NIC memory */
2129 rum_write_multi(sc, RT2573_HW_BEACON_BASE0 + 24, mtod(m0, uint8_t *),
2138 rum_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2139 const struct ieee80211_bpf_params *params)
2141 struct ifnet *ifp = ni->ni_ic->ic_ifp;
2142 struct rum_softc *sc = ifp->if_softc;
2145 /* prevent management frames from being sent if we're not ready */
2146 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2149 ieee80211_free_node(ni);
2152 if (sc->tx_nfree < RUM_TX_MINFREE) {
2153 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2156 ieee80211_free_node(ni);
2162 if (params == NULL) {
2164 * Legacy path; interpret frame contents to decide
2165 * precisely how to send the frame.
2167 if (rum_tx_mgt(sc, m, ni) != 0)
2171 * Caller supplied explicit parameters to use in
2172 * sending the frame.
2174 if (rum_tx_raw(sc, m, ni, params) != 0)
2183 ieee80211_free_node(ni);
2188 rum_ratectl_start(struct rum_softc *sc, struct ieee80211_node *ni)
2190 struct ieee80211vap *vap = ni->ni_vap;
2191 struct rum_vap *rvp = RUM_VAP(vap);
2193 /* clear statistic registers (STA_CSR0 to STA_CSR5) */
2194 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
2196 ieee80211_ratectl_node_init(ni);
2198 usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp);
2202 rum_ratectl_timeout(void *arg)
2204 struct rum_vap *rvp = arg;
2205 struct ieee80211vap *vap = &rvp->vap;
2206 struct ieee80211com *ic = vap->iv_ic;
2208 ieee80211_runtask(ic, &rvp->ratectl_task);
2212 rum_ratectl_task(void *arg, int pending)
2214 struct rum_vap *rvp = arg;
2215 struct ieee80211vap *vap = &rvp->vap;
2216 struct ieee80211com *ic = vap->iv_ic;
2217 struct ifnet *ifp = ic->ic_ifp;
2218 struct rum_softc *sc = ifp->if_softc;
2219 struct ieee80211_node *ni = vap->iv_bss;
2224 /* read and clear statistic registers (STA_CSR0 to STA_CSR10) */
2225 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof(sc->sta));
2227 ok = (le32toh(sc->sta[4]) >> 16) + /* TX ok w/o retry */
2228 (le32toh(sc->sta[5]) & 0xffff); /* TX ok w/ retry */
2229 fail = (le32toh(sc->sta[5]) >> 16); /* TX retry-fail count */
2231 retrycnt = (le32toh(sc->sta[5]) & 0xffff) + fail;
2233 ieee80211_ratectl_tx_update(vap, ni, &sum, &ok, &retrycnt);
2234 (void) ieee80211_ratectl_rate(ni, NULL, 0);
2236 ifp->if_oerrors += fail; /* count TX retry-fail as Tx errors */
2238 usb_callout_reset(&rvp->ratectl_ch, hz, rum_ratectl_timeout, rvp);
2243 rum_scan_start(struct ieee80211com *ic)
2245 struct ifnet *ifp = ic->ic_ifp;
2246 struct rum_softc *sc = ifp->if_softc;
2250 /* abort TSF synchronization */
2251 tmp = rum_read(sc, RT2573_TXRX_CSR9);
2252 rum_write(sc, RT2573_TXRX_CSR9, tmp & ~0x00ffffff);
2253 rum_set_bssid(sc, ifp->if_broadcastaddr);
2259 rum_scan_end(struct ieee80211com *ic)
2261 struct rum_softc *sc = ic->ic_ifp->if_softc;
2264 rum_enable_tsf_sync(sc);
2265 rum_set_bssid(sc, sc->sc_bssid);
2271 rum_set_channel(struct ieee80211com *ic)
2273 struct rum_softc *sc = ic->ic_ifp->if_softc;
2276 rum_set_chan(sc, ic->ic_curchan);
2281 rum_get_rssi(struct rum_softc *sc, uint8_t raw)
2283 struct ifnet *ifp = sc->sc_ifp;
2284 struct ieee80211com *ic = ifp->if_l2com;
2287 lna = (raw >> 5) & 0x3;
2294 * NB: Since RSSI is relative to noise floor, -1 is
2295 * adequate for caller to know error happened.
2300 rssi = (2 * agc) - RT2573_NOISE_FLOOR;
2302 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) {
2303 rssi += sc->rssi_2ghz_corr;
2312 rssi += sc->rssi_5ghz_corr;
2314 if (!sc->ext_5ghz_lna && lna != 1)
2328 rum_pause(struct rum_softc *sc, int timeout)
2331 usb_pause_mtx(&sc->sc_mtx, timeout);
2335 static device_method_t rum_methods[] = {
2336 /* Device interface */
2337 DEVMETHOD(device_probe, rum_match),
2338 DEVMETHOD(device_attach, rum_attach),
2339 DEVMETHOD(device_detach, rum_detach),
2344 static driver_t rum_driver = {
2346 .methods = rum_methods,
2347 .size = sizeof(struct rum_softc),
2350 static devclass_t rum_devclass;
2352 DRIVER_MODULE(rum, uhub, rum_driver, rum_devclass, NULL, 0);