4 * Copyright (c) 2005-2007 Damien Bergamini <damien.bergamini@free.fr>
5 * Copyright (c) 2006 Niall O'Higgins <niallo@openbsd.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <sys/cdefs.h>
21 __FBSDID("$FreeBSD$");
24 * Ralink Technology RT2501USB/RT2601USB chipset driver
25 * http://www.ralinktech.com.tw/
28 #include <sys/param.h>
29 #include <sys/sysctl.h>
30 #include <sys/sockio.h>
32 #include <sys/kernel.h>
33 #include <sys/socket.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
38 #include <sys/endian.h>
40 #include <machine/bus.h>
41 #include <machine/resource.h>
46 #include <net/if_arp.h>
47 #include <net/ethernet.h>
48 #include <net/if_dl.h>
49 #include <net/if_media.h>
50 #include <net/if_types.h>
52 #include <net80211/ieee80211_var.h>
53 #include <net80211/ieee80211_amrr.h>
54 #include <net80211/ieee80211_radiotap.h>
55 #include <net80211/ieee80211_regdomain.h>
57 #include <dev/usb/usb.h>
58 #include <dev/usb/usbdi.h>
59 #include <dev/usb/usbdi_util.h>
62 #include <dev/usb/if_rumreg.h>
63 #include <dev/usb/if_rumvar.h>
64 #include <dev/usb/rt2573_ucode.h>
67 #define DPRINTF(x) do { if (rumdebug > 0) printf x; } while (0)
68 #define DPRINTFN(n, x) do { if (rumdebug >= (n)) printf x; } while (0)
70 SYSCTL_NODE(_hw_usb, OID_AUTO, rum, CTLFLAG_RW, 0, "USB rum");
71 SYSCTL_INT(_hw_usb_rum, OID_AUTO, debug, CTLFLAG_RW, &rumdebug, 0,
75 #define DPRINTFN(n, x)
78 /* various supported device vendors/products */
79 static const struct usb_devno rum_devs[] = {
80 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_HWU54DM },
81 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_RT2573_2 },
82 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_RT2573_3 },
83 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_RT2573_4 },
84 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_WUG2700 },
85 { USB_VENDOR_AMIT, USB_PRODUCT_AMIT_CGWLUSB2GO },
86 { USB_VENDOR_ASUS, USB_PRODUCT_ASUS_RT2573_1 },
87 { USB_VENDOR_ASUS, USB_PRODUCT_ASUS_RT2573_2 },
88 { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D7050A },
89 { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D9050V3 },
90 { USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_WUSB54GC },
91 { USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_WUSB54GR },
92 { USB_VENDOR_CONCEPTRONIC2, USB_PRODUCT_CONCEPTRONIC2_C54RU2 },
93 { USB_VENDOR_DICKSMITH, USB_PRODUCT_DICKSMITH_CWD854F },
94 { USB_VENDOR_DICKSMITH, USB_PRODUCT_DICKSMITH_RT2573 },
95 { USB_VENDOR_DLINK2, USB_PRODUCT_DLINK2_DWLG122C1 },
96 { USB_VENDOR_DLINK2, USB_PRODUCT_DLINK2_WUA1340 },
97 { USB_VENDOR_GIGABYTE, USB_PRODUCT_GIGABYTE_GNWB01GS },
98 { USB_VENDOR_GIGABYTE, USB_PRODUCT_GIGABYTE_GNWI05GS },
99 { USB_VENDOR_GIGASET, USB_PRODUCT_GIGASET_RT2573 },
100 { USB_VENDOR_GOODWAY, USB_PRODUCT_GOODWAY_RT2573 },
101 { USB_VENDOR_GUILLEMOT, USB_PRODUCT_GUILLEMOT_HWGUSB254LB },
102 { USB_VENDOR_GUILLEMOT, USB_PRODUCT_GUILLEMOT_HWGUSB254V2AP },
103 { USB_VENDOR_HUAWEI3COM, USB_PRODUCT_HUAWEI3COM_WUB320G },
104 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_G54HP },
105 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_SG54HP },
106 { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2573_1 },
107 { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2573_2 },
108 { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2573_3 },
109 { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2573_4 },
110 { USB_VENDOR_NOVATECH, USB_PRODUCT_NOVATECH_RT2573 },
111 { USB_VENDOR_PLANEX2, USB_PRODUCT_PLANEX2_GWUS54HP },
112 { USB_VENDOR_PLANEX2, USB_PRODUCT_PLANEX2_GWUS54MINI2 },
113 { USB_VENDOR_PLANEX2, USB_PRODUCT_PLANEX2_GWUSMM },
114 { USB_VENDOR_QCOM, USB_PRODUCT_QCOM_RT2573 },
115 { USB_VENDOR_QCOM, USB_PRODUCT_QCOM_RT2573_2 },
116 { USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2573 },
117 { USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2573_2 },
118 { USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2671 },
119 { USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_WL113R2 },
120 { USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_WL172 },
121 { USB_VENDOR_SURECOM, USB_PRODUCT_SURECOM_RT2573 }
124 MODULE_DEPEND(rum, wlan, 1, 1, 1);
125 MODULE_DEPEND(rum, wlan_amrr, 1, 1, 1);
126 MODULE_DEPEND(rum, usb, 1, 1, 1);
128 static int rum_alloc_tx_list(struct rum_softc *);
129 static void rum_free_tx_list(struct rum_softc *);
130 static int rum_alloc_rx_list(struct rum_softc *);
131 static void rum_free_rx_list(struct rum_softc *);
132 static int rum_media_change(struct ifnet *);
133 static void rum_task(void *);
134 static void rum_scantask(void *);
135 static int rum_newstate(struct ieee80211com *,
136 enum ieee80211_state, int);
137 static void rum_txeof(usbd_xfer_handle, usbd_private_handle,
139 static void rum_rxeof(usbd_xfer_handle, usbd_private_handle,
141 static int rum_rxrate(struct rum_rx_desc *);
142 static int rum_ack_rate(struct ieee80211com *, int);
143 static uint16_t rum_txtime(int, int, uint32_t);
144 static uint8_t rum_plcp_signal(int);
145 static void rum_setup_tx_desc(struct rum_softc *,
146 struct rum_tx_desc *, uint32_t, uint16_t, int,
148 static int rum_tx_mgt(struct rum_softc *, struct mbuf *,
149 struct ieee80211_node *);
150 static int rum_tx_raw(struct rum_softc *, struct mbuf *,
151 struct ieee80211_node *,
152 const struct ieee80211_bpf_params *);
153 static int rum_tx_data(struct rum_softc *, struct mbuf *,
154 struct ieee80211_node *);
155 static void rum_start(struct ifnet *);
156 static void rum_watchdog(void *);
157 static int rum_ioctl(struct ifnet *, u_long, caddr_t);
158 static void rum_eeprom_read(struct rum_softc *, uint16_t, void *,
160 static uint32_t rum_read(struct rum_softc *, uint16_t);
161 static void rum_read_multi(struct rum_softc *, uint16_t, void *,
163 static void rum_write(struct rum_softc *, uint16_t, uint32_t);
164 static void rum_write_multi(struct rum_softc *, uint16_t, void *,
166 static void rum_bbp_write(struct rum_softc *, uint8_t, uint8_t);
167 static uint8_t rum_bbp_read(struct rum_softc *, uint8_t);
168 static void rum_rf_write(struct rum_softc *, uint8_t, uint32_t);
169 static void rum_select_antenna(struct rum_softc *);
170 static void rum_enable_mrr(struct rum_softc *);
171 static void rum_set_txpreamble(struct rum_softc *);
172 static void rum_set_basicrates(struct rum_softc *);
173 static void rum_select_band(struct rum_softc *,
174 struct ieee80211_channel *);
175 static void rum_set_chan(struct rum_softc *,
176 struct ieee80211_channel *);
177 static void rum_enable_tsf_sync(struct rum_softc *);
178 static void rum_update_slot(struct ifnet *);
179 static void rum_set_bssid(struct rum_softc *, const uint8_t *);
180 static void rum_set_macaddr(struct rum_softc *, const uint8_t *);
181 static void rum_update_promisc(struct rum_softc *);
182 static const char *rum_get_rf(int);
183 static void rum_read_eeprom(struct rum_softc *);
184 static int rum_bbp_init(struct rum_softc *);
185 static void rum_init(void *);
186 static void rum_stop(void *);
187 static int rum_load_microcode(struct rum_softc *, const u_char *,
189 static int rum_prepare_beacon(struct rum_softc *);
190 static int rum_raw_xmit(struct ieee80211_node *, struct mbuf *,
191 const struct ieee80211_bpf_params *);
192 static void rum_scan_start(struct ieee80211com *);
193 static void rum_scan_end(struct ieee80211com *);
194 static void rum_set_channel(struct ieee80211com *);
195 static int rum_get_rssi(struct rum_softc *, uint8_t);
196 static void rum_amrr_start(struct rum_softc *,
197 struct ieee80211_node *);
198 static void rum_amrr_timeout(void *);
199 static void rum_amrr_update(usbd_xfer_handle, usbd_private_handle,
202 static const struct {
206 { RT2573_TXRX_CSR0, 0x025fb032 },
207 { RT2573_TXRX_CSR1, 0x9eaa9eaf },
208 { RT2573_TXRX_CSR2, 0x8a8b8c8d },
209 { RT2573_TXRX_CSR3, 0x00858687 },
210 { RT2573_TXRX_CSR7, 0x2e31353b },
211 { RT2573_TXRX_CSR8, 0x2a2a2a2c },
212 { RT2573_TXRX_CSR15, 0x0000000f },
213 { RT2573_MAC_CSR6, 0x00000fff },
214 { RT2573_MAC_CSR8, 0x016c030a },
215 { RT2573_MAC_CSR10, 0x00000718 },
216 { RT2573_MAC_CSR12, 0x00000004 },
217 { RT2573_MAC_CSR13, 0x00007f00 },
218 { RT2573_SEC_CSR0, 0x00000000 },
219 { RT2573_SEC_CSR1, 0x00000000 },
220 { RT2573_SEC_CSR5, 0x00000000 },
221 { RT2573_PHY_CSR1, 0x000023b0 },
222 { RT2573_PHY_CSR5, 0x00040a06 },
223 { RT2573_PHY_CSR6, 0x00080606 },
224 { RT2573_PHY_CSR7, 0x00000408 },
225 { RT2573_AIFSN_CSR, 0x00002273 },
226 { RT2573_CWMIN_CSR, 0x00002344 },
227 { RT2573_CWMAX_CSR, 0x000034aa }
230 static const struct {
262 static const struct rfprog {
264 uint32_t r1, r2, r3, r4;
266 { 1, 0x00b03, 0x001e1, 0x1a014, 0x30282 },
267 { 2, 0x00b03, 0x001e1, 0x1a014, 0x30287 },
268 { 3, 0x00b03, 0x001e2, 0x1a014, 0x30282 },
269 { 4, 0x00b03, 0x001e2, 0x1a014, 0x30287 },
270 { 5, 0x00b03, 0x001e3, 0x1a014, 0x30282 },
271 { 6, 0x00b03, 0x001e3, 0x1a014, 0x30287 },
272 { 7, 0x00b03, 0x001e4, 0x1a014, 0x30282 },
273 { 8, 0x00b03, 0x001e4, 0x1a014, 0x30287 },
274 { 9, 0x00b03, 0x001e5, 0x1a014, 0x30282 },
275 { 10, 0x00b03, 0x001e5, 0x1a014, 0x30287 },
276 { 11, 0x00b03, 0x001e6, 0x1a014, 0x30282 },
277 { 12, 0x00b03, 0x001e6, 0x1a014, 0x30287 },
278 { 13, 0x00b03, 0x001e7, 0x1a014, 0x30282 },
279 { 14, 0x00b03, 0x001e8, 0x1a014, 0x30284 },
281 { 34, 0x00b03, 0x20266, 0x36014, 0x30282 },
282 { 38, 0x00b03, 0x20267, 0x36014, 0x30284 },
283 { 42, 0x00b03, 0x20268, 0x36014, 0x30286 },
284 { 46, 0x00b03, 0x20269, 0x36014, 0x30288 },
286 { 36, 0x00b03, 0x00266, 0x26014, 0x30288 },
287 { 40, 0x00b03, 0x00268, 0x26014, 0x30280 },
288 { 44, 0x00b03, 0x00269, 0x26014, 0x30282 },
289 { 48, 0x00b03, 0x0026a, 0x26014, 0x30284 },
290 { 52, 0x00b03, 0x0026b, 0x26014, 0x30286 },
291 { 56, 0x00b03, 0x0026c, 0x26014, 0x30288 },
292 { 60, 0x00b03, 0x0026e, 0x26014, 0x30280 },
293 { 64, 0x00b03, 0x0026f, 0x26014, 0x30282 },
295 { 100, 0x00b03, 0x0028a, 0x2e014, 0x30280 },
296 { 104, 0x00b03, 0x0028b, 0x2e014, 0x30282 },
297 { 108, 0x00b03, 0x0028c, 0x2e014, 0x30284 },
298 { 112, 0x00b03, 0x0028d, 0x2e014, 0x30286 },
299 { 116, 0x00b03, 0x0028e, 0x2e014, 0x30288 },
300 { 120, 0x00b03, 0x002a0, 0x2e014, 0x30280 },
301 { 124, 0x00b03, 0x002a1, 0x2e014, 0x30282 },
302 { 128, 0x00b03, 0x002a2, 0x2e014, 0x30284 },
303 { 132, 0x00b03, 0x002a3, 0x2e014, 0x30286 },
304 { 136, 0x00b03, 0x002a4, 0x2e014, 0x30288 },
305 { 140, 0x00b03, 0x002a6, 0x2e014, 0x30280 },
307 { 149, 0x00b03, 0x002a8, 0x2e014, 0x30287 },
308 { 153, 0x00b03, 0x002a9, 0x2e014, 0x30289 },
309 { 157, 0x00b03, 0x002ab, 0x2e014, 0x30281 },
310 { 161, 0x00b03, 0x002ac, 0x2e014, 0x30283 },
311 { 165, 0x00b03, 0x002ad, 0x2e014, 0x30285 }
313 { 1, 0x00b33, 0x011e1, 0x1a014, 0x30282 },
314 { 2, 0x00b33, 0x011e1, 0x1a014, 0x30287 },
315 { 3, 0x00b33, 0x011e2, 0x1a014, 0x30282 },
316 { 4, 0x00b33, 0x011e2, 0x1a014, 0x30287 },
317 { 5, 0x00b33, 0x011e3, 0x1a014, 0x30282 },
318 { 6, 0x00b33, 0x011e3, 0x1a014, 0x30287 },
319 { 7, 0x00b33, 0x011e4, 0x1a014, 0x30282 },
320 { 8, 0x00b33, 0x011e4, 0x1a014, 0x30287 },
321 { 9, 0x00b33, 0x011e5, 0x1a014, 0x30282 },
322 { 10, 0x00b33, 0x011e5, 0x1a014, 0x30287 },
323 { 11, 0x00b33, 0x011e6, 0x1a014, 0x30282 },
324 { 12, 0x00b33, 0x011e6, 0x1a014, 0x30287 },
325 { 13, 0x00b33, 0x011e7, 0x1a014, 0x30282 },
326 { 14, 0x00b33, 0x011e8, 0x1a014, 0x30284 },
328 { 34, 0x00b33, 0x01266, 0x26014, 0x30282 },
329 { 38, 0x00b33, 0x01267, 0x26014, 0x30284 },
330 { 42, 0x00b33, 0x01268, 0x26014, 0x30286 },
331 { 46, 0x00b33, 0x01269, 0x26014, 0x30288 },
333 { 36, 0x00b33, 0x01266, 0x26014, 0x30288 },
334 { 40, 0x00b33, 0x01268, 0x26014, 0x30280 },
335 { 44, 0x00b33, 0x01269, 0x26014, 0x30282 },
336 { 48, 0x00b33, 0x0126a, 0x26014, 0x30284 },
337 { 52, 0x00b33, 0x0126b, 0x26014, 0x30286 },
338 { 56, 0x00b33, 0x0126c, 0x26014, 0x30288 },
339 { 60, 0x00b33, 0x0126e, 0x26014, 0x30280 },
340 { 64, 0x00b33, 0x0126f, 0x26014, 0x30282 },
342 { 100, 0x00b33, 0x0128a, 0x2e014, 0x30280 },
343 { 104, 0x00b33, 0x0128b, 0x2e014, 0x30282 },
344 { 108, 0x00b33, 0x0128c, 0x2e014, 0x30284 },
345 { 112, 0x00b33, 0x0128d, 0x2e014, 0x30286 },
346 { 116, 0x00b33, 0x0128e, 0x2e014, 0x30288 },
347 { 120, 0x00b33, 0x012a0, 0x2e014, 0x30280 },
348 { 124, 0x00b33, 0x012a1, 0x2e014, 0x30282 },
349 { 128, 0x00b33, 0x012a2, 0x2e014, 0x30284 },
350 { 132, 0x00b33, 0x012a3, 0x2e014, 0x30286 },
351 { 136, 0x00b33, 0x012a4, 0x2e014, 0x30288 },
352 { 140, 0x00b33, 0x012a6, 0x2e014, 0x30280 },
354 { 149, 0x00b33, 0x012a8, 0x2e014, 0x30287 },
355 { 153, 0x00b33, 0x012a9, 0x2e014, 0x30289 },
356 { 157, 0x00b33, 0x012ab, 0x2e014, 0x30281 },
357 { 161, 0x00b33, 0x012ac, 0x2e014, 0x30283 },
358 { 165, 0x00b33, 0x012ad, 0x2e014, 0x30285 }
362 rum_match(device_t self)
364 struct usb_attach_arg *uaa = device_get_ivars(self);
366 if (uaa->iface != NULL)
369 return (usb_lookup(rum_devs, uaa->vendor, uaa->product) != NULL) ?
370 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
374 rum_attach(device_t self)
376 struct rum_softc *sc = device_get_softc(self);
377 struct usb_attach_arg *uaa = device_get_ivars(self);
378 struct ieee80211com *ic = &sc->sc_ic;
380 const uint8_t *ucode = NULL;
381 usb_interface_descriptor_t *id;
382 usb_endpoint_descriptor_t *ed;
384 int i, ntries, size, bands;
387 sc->sc_udev = uaa->device;
390 if (usbd_set_config_no(sc->sc_udev, RT2573_CONFIG_NO, 0) != 0) {
391 printf("%s: could not set configuration no\n",
392 device_get_nameunit(sc->sc_dev));
396 /* get the first interface handle */
397 error = usbd_device2interface_handle(sc->sc_udev, RT2573_IFACE_INDEX,
400 printf("%s: could not get interface handle\n",
401 device_get_nameunit(sc->sc_dev));
408 id = usbd_get_interface_descriptor(sc->sc_iface);
410 sc->sc_rx_no = sc->sc_tx_no = -1;
411 for (i = 0; i < id->bNumEndpoints; i++) {
412 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
414 printf("%s: no endpoint descriptor for iface %d\n",
415 device_get_nameunit(sc->sc_dev), i);
419 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
420 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
421 sc->sc_rx_no = ed->bEndpointAddress;
422 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
423 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
424 sc->sc_tx_no = ed->bEndpointAddress;
426 if (sc->sc_rx_no == -1 || sc->sc_tx_no == -1) {
427 printf("%s: missing endpoint\n",
428 device_get_nameunit(sc->sc_dev));
432 mtx_init(&sc->sc_mtx, device_get_nameunit(sc->sc_dev), MTX_NETWORK_LOCK,
433 MTX_DEF | MTX_RECURSE);
435 usb_init_task(&sc->sc_task, rum_task, sc);
436 usb_init_task(&sc->sc_scantask, rum_scantask, sc);
437 callout_init(&sc->watchdog_ch, 0);
438 callout_init(&sc->amrr_ch, 0);
440 /* retrieve RT2573 rev. no */
441 for (ntries = 0; ntries < 1000; ntries++) {
442 if ((tmp = rum_read(sc, RT2573_MAC_CSR0)) != 0)
446 if (ntries == 1000) {
447 printf("%s: timeout waiting for chip to settle\n",
448 device_get_nameunit(sc->sc_dev));
452 /* retrieve MAC address and various other things from EEPROM */
455 printf("%s: MAC/BBP RT2573 (rev 0x%05x), RF %s\n",
456 device_get_nameunit(sc->sc_dev), tmp, rum_get_rf(sc->rf_rev));
458 ucode = rt2573_ucode;
459 size = sizeof rt2573_ucode;
460 error = rum_load_microcode(sc, ucode, size);
462 device_printf(sc->sc_dev, "could not load 8051 microcode\n");
463 mtx_destroy(&sc->sc_mtx);
467 ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
469 printf("%s: can not if_alloc()\n",
470 device_get_nameunit(sc->sc_dev));
471 mtx_destroy(&sc->sc_mtx);
476 if_initname(ifp, "rum", device_get_unit(sc->sc_dev));
477 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST |
478 IFF_NEEDSGIANT; /* USB stack is still under Giant lock */
479 ifp->if_init = rum_init;
480 ifp->if_ioctl = rum_ioctl;
481 ifp->if_start = rum_start;
482 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
483 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
484 IFQ_SET_READY(&ifp->if_snd);
487 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
488 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
489 ic->ic_state = IEEE80211_S_INIT;
491 /* set device capabilities */
493 IEEE80211_C_IBSS | /* IBSS mode supported */
494 IEEE80211_C_MONITOR | /* monitor mode supported */
495 IEEE80211_C_HOSTAP | /* HostAp mode supported */
496 IEEE80211_C_TXPMGT | /* tx power management */
497 IEEE80211_C_SHPREAMBLE | /* short preamble supported */
498 IEEE80211_C_SHSLOT | /* short slot time supported */
499 IEEE80211_C_BGSCAN | /* bg scanning supported */
500 IEEE80211_C_WPA; /* 802.11i */
503 setbit(&bands, IEEE80211_MODE_11B);
504 setbit(&bands, IEEE80211_MODE_11G);
505 ieee80211_init_channels(ic, 0, CTRY_DEFAULT, bands, 0, 1);
507 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_5226) {
508 struct ieee80211_channel *c;
510 /* set supported .11a channels */
511 for (i = 34; i <= 46; i += 4) {
512 c = &ic->ic_channels[ic->ic_nchans++];
513 c->ic_freq = ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
514 c->ic_flags = IEEE80211_CHAN_A;
517 for (i = 36; i <= 64; i += 4) {
518 c = &ic->ic_channels[ic->ic_nchans++];
519 c->ic_freq = ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
520 c->ic_flags = IEEE80211_CHAN_A;
523 for (i = 100; i <= 140; i += 4) {
524 c = &ic->ic_channels[ic->ic_nchans++];
525 c->ic_freq = ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
526 c->ic_flags = IEEE80211_CHAN_A;
529 for (i = 149; i <= 165; i += 4) {
530 c = &ic->ic_channels[ic->ic_nchans++];
531 c->ic_freq = ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
532 c->ic_flags = IEEE80211_CHAN_A;
537 ieee80211_ifattach(ic);
538 ic->ic_scan_start = rum_scan_start;
539 ic->ic_scan_end = rum_scan_end;
540 ic->ic_set_channel = rum_set_channel;
542 /* enable s/w bmiss handling in sta mode */
543 ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS;
545 /* override state transition machine */
546 sc->sc_newstate = ic->ic_newstate;
547 ic->ic_newstate = rum_newstate;
548 ic->ic_raw_xmit = rum_raw_xmit;
549 ieee80211_media_init(ic, rum_media_change, ieee80211_media_status);
551 ieee80211_amrr_init(&sc->amrr, ic,
552 IEEE80211_AMRR_MIN_SUCCESS_THRESHOLD,
553 IEEE80211_AMRR_MAX_SUCCESS_THRESHOLD);
555 bpfattach2(ifp, DLT_IEEE802_11_RADIO,
556 sizeof (struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN,
559 sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
560 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
561 sc->sc_rxtap.wr_ihdr.it_present = htole32(RT2573_RX_RADIOTAP_PRESENT);
563 sc->sc_txtap_len = sizeof sc->sc_txtapu;
564 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
565 sc->sc_txtap.wt_ihdr.it_present = htole32(RT2573_TX_RADIOTAP_PRESENT);
568 ieee80211_announce(ic);
574 rum_detach(device_t self)
576 struct rum_softc *sc = device_get_softc(self);
577 struct ieee80211com *ic = &sc->sc_ic;
578 struct ifnet *ifp = ic->ic_ifp;
581 usb_rem_task(sc->sc_udev, &sc->sc_task);
582 usb_rem_task(sc->sc_udev, &sc->sc_scantask);
583 callout_stop(&sc->watchdog_ch);
584 callout_stop(&sc->amrr_ch);
586 if (sc->amrr_xfer != NULL) {
587 usbd_free_xfer(sc->amrr_xfer);
588 sc->amrr_xfer = NULL;
591 if (sc->sc_rx_pipeh != NULL) {
592 usbd_abort_pipe(sc->sc_rx_pipeh);
593 usbd_close_pipe(sc->sc_rx_pipeh);
595 if (sc->sc_tx_pipeh != NULL) {
596 usbd_abort_pipe(sc->sc_tx_pipeh);
597 usbd_close_pipe(sc->sc_tx_pipeh);
600 rum_free_rx_list(sc);
601 rum_free_tx_list(sc);
604 ieee80211_ifdetach(ic);
607 mtx_destroy(&sc->sc_mtx);
613 rum_alloc_tx_list(struct rum_softc *sc)
615 struct rum_tx_data *data;
620 for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
621 data = &sc->tx_data[i];
625 data->xfer = usbd_alloc_xfer(sc->sc_udev);
626 if (data->xfer == NULL) {
627 printf("%s: could not allocate tx xfer\n",
628 device_get_nameunit(sc->sc_dev));
632 data->buf = usbd_alloc_buffer(data->xfer,
633 RT2573_TX_DESC_SIZE + MCLBYTES);
634 if (data->buf == NULL) {
635 printf("%s: could not allocate tx buffer\n",
636 device_get_nameunit(sc->sc_dev));
640 /* clean Tx descriptor */
641 bzero(data->buf, RT2573_TX_DESC_SIZE);
646 fail: rum_free_tx_list(sc);
651 rum_free_tx_list(struct rum_softc *sc)
653 struct rum_tx_data *data;
656 for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
657 data = &sc->tx_data[i];
659 if (data->xfer != NULL) {
660 usbd_free_xfer(data->xfer);
664 if (data->ni != NULL) {
665 ieee80211_free_node(data->ni);
672 rum_alloc_rx_list(struct rum_softc *sc)
674 struct rum_rx_data *data;
677 for (i = 0; i < RUM_RX_LIST_COUNT; i++) {
678 data = &sc->rx_data[i];
682 data->xfer = usbd_alloc_xfer(sc->sc_udev);
683 if (data->xfer == NULL) {
684 printf("%s: could not allocate rx xfer\n",
685 device_get_nameunit(sc->sc_dev));
689 if (usbd_alloc_buffer(data->xfer, MCLBYTES) == NULL) {
690 printf("%s: could not allocate rx buffer\n",
691 device_get_nameunit(sc->sc_dev));
696 data->m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
697 if (data->m == NULL) {
698 printf("%s: could not allocate rx mbuf\n",
699 device_get_nameunit(sc->sc_dev));
704 data->buf = mtod(data->m, uint8_t *);
709 fail: rum_free_tx_list(sc);
714 rum_free_rx_list(struct rum_softc *sc)
716 struct rum_rx_data *data;
719 for (i = 0; i < RUM_RX_LIST_COUNT; i++) {
720 data = &sc->rx_data[i];
722 if (data->xfer != NULL) {
723 usbd_free_xfer(data->xfer);
726 if (data->m != NULL) {
734 rum_media_change(struct ifnet *ifp)
736 struct rum_softc *sc = ifp->if_softc;
741 error = ieee80211_media_change(ifp);
742 if (error != ENETRESET) {
747 if ((ifp->if_flags & IFF_UP) &&
748 (ifp->if_drv_flags & IFF_DRV_RUNNING))
759 struct rum_softc *sc = arg;
760 struct ieee80211com *ic = &sc->sc_ic;
761 enum ieee80211_state ostate;
762 struct ieee80211_node *ni;
765 ostate = ic->ic_state;
769 switch (sc->sc_state) {
770 case IEEE80211_S_INIT:
771 if (ostate == IEEE80211_S_RUN) {
772 /* abort TSF synchronization */
773 tmp = rum_read(sc, RT2573_TXRX_CSR9);
774 rum_write(sc, RT2573_TXRX_CSR9, tmp & ~0x00ffffff);
778 case IEEE80211_S_RUN:
781 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
782 rum_update_slot(ic->ic_ifp);
784 rum_set_txpreamble(sc);
785 rum_set_basicrates(sc);
786 rum_set_bssid(sc, ni->ni_bssid);
789 if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
790 ic->ic_opmode == IEEE80211_M_IBSS)
791 rum_prepare_beacon(sc);
793 if (ic->ic_opmode != IEEE80211_M_MONITOR)
794 rum_enable_tsf_sync(sc);
796 /* enable automatic rate adaptation in STA mode */
797 if (ic->ic_opmode == IEEE80211_M_STA &&
798 ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE)
799 rum_amrr_start(sc, ni);
807 sc->sc_newstate(ic, sc->sc_state, sc->sc_arg);
811 rum_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
813 struct rum_softc *sc = ic->ic_ifp->if_softc;
815 callout_stop(&sc->amrr_ch);
817 /* do it in a process context */
818 sc->sc_state = nstate;
821 usb_rem_task(sc->sc_udev, &sc->sc_task);
822 if (nstate == IEEE80211_S_INIT)
823 sc->sc_newstate(ic, nstate, arg);
825 usb_add_task(sc->sc_udev, &sc->sc_task, USB_TASKQ_DRIVER);
829 /* quickly determine if a given rate is CCK or OFDM */
830 #define RUM_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
832 #define RUM_ACK_SIZE 14 /* 10 + 4(FCS) */
833 #define RUM_CTS_SIZE 14 /* 10 + 4(FCS) */
836 rum_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
838 struct rum_tx_data *data = priv;
839 struct rum_softc *sc = data->sc;
840 struct ifnet *ifp = sc->sc_ic.ic_ifp;
842 if (data->m->m_flags & M_TXCB)
843 ieee80211_process_callback(data->ni, data->m,
844 status == USBD_NORMAL_COMPLETION ? 0 : ETIMEDOUT);
846 if (status != USBD_NORMAL_COMPLETION) {
847 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
850 printf("%s: could not transmit buffer: %s\n",
851 device_get_nameunit(sc->sc_dev), usbd_errstr(status));
853 if (status == USBD_STALLED)
854 usbd_clear_endpoint_stall_async(sc->sc_tx_pipeh);
862 ieee80211_free_node(data->ni);
868 DPRINTFN(10, ("tx done\n"));
871 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
876 rum_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
878 struct rum_rx_data *data = priv;
879 struct rum_softc *sc = data->sc;
880 struct ieee80211com *ic = &sc->sc_ic;
881 struct ifnet *ifp = ic->ic_ifp;
882 struct rum_rx_desc *desc;
883 struct ieee80211_frame *wh;
884 struct ieee80211_node *ni;
885 struct mbuf *mnew, *m;
888 if (status != USBD_NORMAL_COMPLETION) {
889 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
892 if (status == USBD_STALLED)
893 usbd_clear_endpoint_stall_async(sc->sc_rx_pipeh);
897 usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
899 if (len < RT2573_RX_DESC_SIZE + sizeof (struct ieee80211_frame_min)) {
900 DPRINTF(("%s: xfer too short %d\n",
901 device_get_nameunit(sc->sc_dev), len));
906 desc = (struct rum_rx_desc *)data->buf;
908 if (le32toh(desc->flags) & RT2573_RX_CRC_ERROR) {
910 * This should not happen since we did not request to receive
911 * those frames when we filled RT2573_TXRX_CSR0.
913 DPRINTFN(5, ("CRC error\n"));
918 mnew = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
926 data->buf = mtod(data->m, uint8_t *);
929 m->m_pkthdr.rcvif = ifp;
930 m->m_data = (caddr_t)(desc + 1);
931 m->m_pkthdr.len = m->m_len = (le32toh(desc->flags) >> 16) & 0xfff;
933 rssi = rum_get_rssi(sc, desc->rssi);
935 wh = mtod(m, struct ieee80211_frame *);
936 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
938 /* Error happened during RSSI conversion. */
942 if (bpf_peers_present(sc->sc_drvbpf)) {
943 struct rum_rx_radiotap_header *tap = &sc->sc_rxtap;
945 tap->wr_flags = IEEE80211_RADIOTAP_F_FCS;
946 tap->wr_rate = rum_rxrate(desc);
947 tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
948 tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
949 tap->wr_antenna = sc->rx_ant;
950 tap->wr_antsignal = rssi;
952 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m);
955 /* send the frame to the 802.11 layer */
956 ieee80211_input(ic, m, ni, rssi, RT2573_NOISE_FLOOR, 0);
958 /* node is no longer needed */
959 ieee80211_free_node(ni);
961 DPRINTFN(15, ("rx done\n"));
963 skip: /* setup a new transfer */
964 usbd_setup_xfer(xfer, sc->sc_rx_pipeh, data, data->buf, MCLBYTES,
965 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rum_rxeof);
970 * This function is only used by the Rx radiotap code.
973 rum_rxrate(struct rum_rx_desc *desc)
975 if (le32toh(desc->flags) & RT2573_RX_OFDM) {
976 /* reverse function of rum_plcp_signal */
977 switch (desc->rate) {
985 case 0xc: return 108;
988 if (desc->rate == 10)
990 if (desc->rate == 20)
992 if (desc->rate == 55)
994 if (desc->rate == 110)
997 return 2; /* should not get there */
1001 * Return the expected ack rate for a frame transmitted at rate `rate'.
1004 rum_ack_rate(struct ieee80211com *ic, int rate)
1013 return (ic->ic_curmode == IEEE80211_MODE_11B) ? 4 : rate;
1029 /* default to 1Mbps */
1034 * Compute the duration (in us) needed to transmit `len' bytes at rate `rate'.
1035 * The function automatically determines the operating mode depending on the
1036 * given rate. `flags' indicates whether short preamble is in use or not.
1039 rum_txtime(int len, int rate, uint32_t flags)
1043 if (RUM_RATE_IS_OFDM(rate)) {
1044 /* IEEE Std 802.11a-1999, pp. 37 */
1045 txtime = (8 + 4 * len + 3 + rate - 1) / rate;
1046 txtime = 16 + 4 + 4 * txtime + 6;
1048 /* IEEE Std 802.11b-1999, pp. 28 */
1049 txtime = (16 * len + rate - 1) / rate;
1050 if (rate != 2 && (flags & IEEE80211_F_SHPREAMBLE))
1059 rum_plcp_signal(int rate)
1062 /* CCK rates (returned values are device-dependent) */
1065 case 11: return 0x2;
1066 case 22: return 0x3;
1068 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1069 case 12: return 0xb;
1070 case 18: return 0xf;
1071 case 24: return 0xa;
1072 case 36: return 0xe;
1073 case 48: return 0x9;
1074 case 72: return 0xd;
1075 case 96: return 0x8;
1076 case 108: return 0xc;
1078 /* unsupported rates (should not get there) */
1079 default: return 0xff;
1084 rum_setup_tx_desc(struct rum_softc *sc, struct rum_tx_desc *desc,
1085 uint32_t flags, uint16_t xflags, int len, int rate)
1087 struct ieee80211com *ic = &sc->sc_ic;
1088 uint16_t plcp_length;
1091 desc->flags = htole32(flags);
1092 desc->flags |= htole32(RT2573_TX_VALID);
1093 desc->flags |= htole32(len << 16);
1095 desc->xflags = htole16(xflags);
1097 desc->wme = htole16(RT2573_QID(0) | RT2573_AIFSN(2) |
1098 RT2573_LOGCWMIN(4) | RT2573_LOGCWMAX(10));
1100 /* setup PLCP fields */
1101 desc->plcp_signal = rum_plcp_signal(rate);
1102 desc->plcp_service = 4;
1104 len += IEEE80211_CRC_LEN;
1105 if (RUM_RATE_IS_OFDM(rate)) {
1106 desc->flags |= htole32(RT2573_TX_OFDM);
1108 plcp_length = len & 0xfff;
1109 desc->plcp_length_hi = plcp_length >> 6;
1110 desc->plcp_length_lo = plcp_length & 0x3f;
1112 plcp_length = (16 * len + rate - 1) / rate;
1114 remainder = (16 * len) % 22;
1115 if (remainder != 0 && remainder < 7)
1116 desc->plcp_service |= RT2573_PLCP_LENGEXT;
1118 desc->plcp_length_hi = plcp_length >> 8;
1119 desc->plcp_length_lo = plcp_length & 0xff;
1121 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1122 desc->plcp_signal |= 0x08;
1126 #define RUM_TX_TIMEOUT 5000
1129 rum_tx_mgt(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1131 struct ieee80211com *ic = &sc->sc_ic;
1132 struct rum_tx_desc *desc;
1133 struct rum_tx_data *data;
1134 struct ieee80211_frame *wh;
1140 data = &sc->tx_data[0];
1141 desc = (struct rum_tx_desc *)data->buf;
1143 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
1148 wh = mtod(m0, struct ieee80211_frame *);
1150 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1151 flags |= RT2573_TX_NEED_ACK;
1153 dur = rum_txtime(RUM_ACK_SIZE, rum_ack_rate(ic, rate),
1154 ic->ic_flags) + sc->sifs;
1155 *(uint16_t *)wh->i_dur = htole16(dur);
1157 /* tell hardware to add timestamp for probe responses */
1159 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
1160 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
1161 flags |= RT2573_TX_TIMESTAMP;
1164 if (bpf_peers_present(sc->sc_drvbpf)) {
1165 struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
1168 tap->wt_rate = rate;
1169 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1170 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1171 tap->wt_antenna = sc->tx_ant;
1173 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
1176 m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RT2573_TX_DESC_SIZE);
1177 rum_setup_tx_desc(sc, desc, flags, 0, m0->m_pkthdr.len, rate);
1179 /* align end on a 4-bytes boundary */
1180 xferlen = (RT2573_TX_DESC_SIZE + m0->m_pkthdr.len + 3) & ~3;
1183 * No space left in the last URB to store the extra 4 bytes, force
1184 * sending of another URB.
1186 if ((xferlen % 64) == 0)
1189 DPRINTFN(10, ("sending mgt frame len=%d rate=%d xfer len=%d\n",
1190 m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, rate, xferlen));
1192 usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf, xferlen,
1193 USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RUM_TX_TIMEOUT, rum_txeof);
1195 error = usbd_transfer(data->xfer);
1196 if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS) {
1209 rum_tx_raw(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
1210 const struct ieee80211_bpf_params *params)
1212 struct ieee80211com *ic = &sc->sc_ic;
1213 struct rum_tx_desc *desc;
1214 struct rum_tx_data *data;
1219 data = &sc->tx_data[0];
1220 desc = (struct rum_tx_desc *)data->buf;
1222 rate = params->ibp_rate0 & IEEE80211_RATE_VAL;
1229 if (bpf_peers_present(sc->sc_drvbpf)) {
1230 struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
1233 tap->wt_rate = rate;
1234 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1235 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1236 tap->wt_antenna = sc->tx_ant;
1238 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
1245 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
1246 flags |= RT2573_TX_NEED_ACK;
1248 m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RT2573_TX_DESC_SIZE);
1249 /* XXX need to setup descriptor ourself */
1250 rum_setup_tx_desc(sc, desc, flags, 0, m0->m_pkthdr.len, rate);
1252 /* align end on a 4-bytes boundary */
1253 xferlen = (RT2573_TX_DESC_SIZE + m0->m_pkthdr.len + 3) & ~3;
1256 * No space left in the last URB to store the extra 4 bytes, force
1257 * sending of another URB.
1259 if ((xferlen % 64) == 0)
1262 DPRINTFN(10, ("sending raw frame len=%u rate=%u xfer len=%u\n",
1263 m0->m_pkthdr.len, rate, xferlen));
1265 usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf,
1266 xferlen, USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RUM_TX_TIMEOUT,
1269 error = usbd_transfer(data->xfer);
1270 if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS)
1279 rum_tx_data(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1281 struct ieee80211com *ic = &sc->sc_ic;
1282 struct rum_tx_desc *desc;
1283 struct rum_tx_data *data;
1284 struct ieee80211_frame *wh;
1285 struct ieee80211_key *k;
1291 wh = mtod(m0, struct ieee80211_frame *);
1293 if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE)
1294 rate = ic->ic_fixed_rate;
1296 rate = ni->ni_rates.rs_rates[ni->ni_txrate];
1298 rate &= IEEE80211_RATE_VAL;
1300 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1301 k = ieee80211_crypto_encap(ic, ni, m0);
1307 /* packet header may have moved, reset our local pointer */
1308 wh = mtod(m0, struct ieee80211_frame *);
1311 data = &sc->tx_data[0];
1312 desc = (struct rum_tx_desc *)data->buf;
1317 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1318 flags |= RT2573_TX_NEED_ACK;
1319 flags |= RT2573_TX_MORE_FRAG;
1321 dur = rum_txtime(RUM_ACK_SIZE, rum_ack_rate(ic, rate),
1322 ic->ic_flags) + sc->sifs;
1323 *(uint16_t *)wh->i_dur = htole16(dur);
1326 if (bpf_peers_present(sc->sc_drvbpf)) {
1327 struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
1330 tap->wt_rate = rate;
1331 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1332 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1333 tap->wt_antenna = sc->tx_ant;
1335 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
1338 m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RT2573_TX_DESC_SIZE);
1339 rum_setup_tx_desc(sc, desc, flags, 0, m0->m_pkthdr.len, rate);
1341 /* align end on a 4-bytes boundary */
1342 xferlen = (RT2573_TX_DESC_SIZE + m0->m_pkthdr.len + 3) & ~3;
1345 * No space left in the last URB to store the extra 4 bytes, force
1346 * sending of another URB.
1348 if ((xferlen % 64) == 0)
1351 DPRINTFN(10, ("sending frame len=%d rate=%d xfer len=%d\n",
1352 m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, rate, xferlen));
1354 usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf, xferlen,
1355 USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RUM_TX_TIMEOUT, rum_txeof);
1357 error = usbd_transfer(data->xfer);
1358 if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS) {
1371 rum_start(struct ifnet *ifp)
1373 struct rum_softc *sc = ifp->if_softc;
1374 struct ieee80211com *ic = &sc->sc_ic;
1375 struct ieee80211_node *ni;
1377 struct ether_header *eh;
1380 IF_POLL(&ic->ic_mgtq, m0);
1382 if (sc->tx_queued >= RUM_TX_LIST_COUNT) {
1383 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1386 IF_DEQUEUE(&ic->ic_mgtq, m0);
1388 ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
1389 m0->m_pkthdr.rcvif = NULL;
1391 if (bpf_peers_present(ic->ic_rawbpf))
1392 bpf_mtap(ic->ic_rawbpf, m0);
1394 if (rum_tx_mgt(sc, m0, ni) != 0) {
1395 ieee80211_free_node(ni);
1399 if (ic->ic_state != IEEE80211_S_RUN)
1401 IFQ_DRV_DEQUEUE(&ifp->if_snd, m0);
1404 if (sc->tx_queued >= RUM_TX_LIST_COUNT) {
1405 IFQ_DRV_PREPEND(&ifp->if_snd, m0);
1406 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1410 * Cancel any background scan.
1412 if (ic->ic_flags & IEEE80211_F_SCAN)
1413 ieee80211_cancel_scan(ic);
1415 if (m0->m_len < sizeof (struct ether_header) &&
1416 !(m0 = m_pullup(m0, sizeof (struct ether_header))))
1419 eh = mtod(m0, struct ether_header *);
1420 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1427 m0 = ieee80211_encap(ic, m0, ni);
1429 ieee80211_free_node(ni);
1433 if (bpf_peers_present(ic->ic_rawbpf))
1434 bpf_mtap(ic->ic_rawbpf, m0);
1436 if (rum_tx_data(sc, m0, ni) != 0) {
1437 ieee80211_free_node(ni);
1443 sc->sc_tx_timer = 5;
1444 ic->ic_lastdata = ticks;
1445 callout_reset(&sc->watchdog_ch, hz, rum_watchdog, sc);
1450 rum_watchdog(void *arg)
1452 struct rum_softc *sc = arg;
1456 if (sc->sc_tx_timer > 0) {
1457 if (--sc->sc_tx_timer == 0) {
1458 device_printf(sc->sc_dev, "device timeout\n");
1459 /*rum_init(ifp); XXX needs a process context! */
1460 sc->sc_ifp->if_oerrors++;
1464 callout_reset(&sc->watchdog_ch, hz, rum_watchdog, sc);
1471 rum_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1473 struct rum_softc *sc = ifp->if_softc;
1474 struct ieee80211com *ic = &sc->sc_ic;
1481 if (ifp->if_flags & IFF_UP) {
1482 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1483 rum_update_promisc(sc);
1487 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1492 error = ieee80211_ioctl(ic, cmd, data);
1495 if (error == ENETRESET) {
1496 if ((ifp->if_flags & IFF_UP) &&
1497 (ifp->if_drv_flags & IFF_DRV_RUNNING) &&
1498 (ic->ic_roaming != IEEE80211_ROAMING_MANUAL))
1509 rum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len)
1511 usb_device_request_t req;
1514 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1515 req.bRequest = RT2573_READ_EEPROM;
1516 USETW(req.wValue, 0);
1517 USETW(req.wIndex, addr);
1518 USETW(req.wLength, len);
1520 error = usbd_do_request(sc->sc_udev, &req, buf);
1522 printf("%s: could not read EEPROM: %s\n",
1523 device_get_nameunit(sc->sc_dev), usbd_errstr(error));
1528 rum_read(struct rum_softc *sc, uint16_t reg)
1532 rum_read_multi(sc, reg, &val, sizeof val);
1534 return le32toh(val);
1538 rum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len)
1540 usb_device_request_t req;
1543 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1544 req.bRequest = RT2573_READ_MULTI_MAC;
1545 USETW(req.wValue, 0);
1546 USETW(req.wIndex, reg);
1547 USETW(req.wLength, len);
1549 error = usbd_do_request(sc->sc_udev, &req, buf);
1551 printf("%s: could not multi read MAC register: %s\n",
1552 device_get_nameunit(sc->sc_dev), usbd_errstr(error));
1557 rum_write(struct rum_softc *sc, uint16_t reg, uint32_t val)
1559 uint32_t tmp = htole32(val);
1561 rum_write_multi(sc, reg, &tmp, sizeof tmp);
1565 rum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len)
1567 usb_device_request_t req;
1570 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1571 req.bRequest = RT2573_WRITE_MULTI_MAC;
1572 USETW(req.wValue, 0);
1573 USETW(req.wIndex, reg);
1574 USETW(req.wLength, len);
1576 error = usbd_do_request(sc->sc_udev, &req, buf);
1578 printf("%s: could not multi write MAC register: %s\n",
1579 device_get_nameunit(sc->sc_dev), usbd_errstr(error));
1584 rum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val)
1589 for (ntries = 0; ntries < 5; ntries++) {
1590 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1594 printf("%s: could not write to BBP\n",
1595 device_get_nameunit(sc->sc_dev));
1599 tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val;
1600 rum_write(sc, RT2573_PHY_CSR3, tmp);
1604 rum_bbp_read(struct rum_softc *sc, uint8_t reg)
1609 for (ntries = 0; ntries < 5; ntries++) {
1610 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1614 printf("%s: could not read BBP\n",
1615 device_get_nameunit(sc->sc_dev));
1619 val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8;
1620 rum_write(sc, RT2573_PHY_CSR3, val);
1622 for (ntries = 0; ntries < 100; ntries++) {
1623 val = rum_read(sc, RT2573_PHY_CSR3);
1624 if (!(val & RT2573_BBP_BUSY))
1629 printf("%s: could not read BBP\n", device_get_nameunit(sc->sc_dev));
1634 rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val)
1639 for (ntries = 0; ntries < 5; ntries++) {
1640 if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY))
1644 printf("%s: could not write to RF\n",
1645 device_get_nameunit(sc->sc_dev));
1649 tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 |
1651 rum_write(sc, RT2573_PHY_CSR4, tmp);
1653 /* remember last written value in sc */
1654 sc->rf_regs[reg] = val;
1656 DPRINTFN(15, ("RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff));
1660 rum_select_antenna(struct rum_softc *sc)
1662 uint8_t bbp4, bbp77;
1665 bbp4 = rum_bbp_read(sc, 4);
1666 bbp77 = rum_bbp_read(sc, 77);
1670 /* make sure Rx is disabled before switching antenna */
1671 tmp = rum_read(sc, RT2573_TXRX_CSR0);
1672 rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
1674 rum_bbp_write(sc, 4, bbp4);
1675 rum_bbp_write(sc, 77, bbp77);
1677 rum_write(sc, RT2573_TXRX_CSR0, tmp);
1681 * Enable multi-rate retries for frames sent at OFDM rates.
1682 * In 802.11b/g mode, allow fallback to CCK rates.
1685 rum_enable_mrr(struct rum_softc *sc)
1687 struct ieee80211com *ic = &sc->sc_ic;
1690 tmp = rum_read(sc, RT2573_TXRX_CSR4);
1692 tmp &= ~RT2573_MRR_CCK_FALLBACK;
1693 if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan))
1694 tmp |= RT2573_MRR_CCK_FALLBACK;
1695 tmp |= RT2573_MRR_ENABLED;
1697 rum_write(sc, RT2573_TXRX_CSR4, tmp);
1701 rum_set_txpreamble(struct rum_softc *sc)
1705 tmp = rum_read(sc, RT2573_TXRX_CSR4);
1707 tmp &= ~RT2573_SHORT_PREAMBLE;
1708 if (sc->sc_ic.ic_flags & IEEE80211_F_SHPREAMBLE)
1709 tmp |= RT2573_SHORT_PREAMBLE;
1711 rum_write(sc, RT2573_TXRX_CSR4, tmp);
1715 rum_set_basicrates(struct rum_softc *sc)
1717 struct ieee80211com *ic = &sc->sc_ic;
1719 /* update basic rate set */
1720 if (ic->ic_curmode == IEEE80211_MODE_11B) {
1721 /* 11b basic rates: 1, 2Mbps */
1722 rum_write(sc, RT2573_TXRX_CSR5, 0x3);
1723 } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bss->ni_chan)) {
1724 /* 11a basic rates: 6, 12, 24Mbps */
1725 rum_write(sc, RT2573_TXRX_CSR5, 0x150);
1727 /* 11b/g basic rates: 1, 2, 5.5, 11Mbps */
1728 rum_write(sc, RT2573_TXRX_CSR5, 0xf);
1733 * Reprogram MAC/BBP to switch to a new band. Values taken from the reference
1737 rum_select_band(struct rum_softc *sc, struct ieee80211_channel *c)
1739 uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104;
1742 /* update all BBP registers that depend on the band */
1743 bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c;
1744 bbp35 = 0x50; bbp97 = 0x48; bbp98 = 0x48;
1745 if (IEEE80211_IS_CHAN_5GHZ(c)) {
1746 bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c;
1747 bbp35 += 0x10; bbp97 += 0x10; bbp98 += 0x10;
1749 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1750 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1751 bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10;
1755 rum_bbp_write(sc, 17, bbp17);
1756 rum_bbp_write(sc, 96, bbp96);
1757 rum_bbp_write(sc, 104, bbp104);
1759 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1760 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1761 rum_bbp_write(sc, 75, 0x80);
1762 rum_bbp_write(sc, 86, 0x80);
1763 rum_bbp_write(sc, 88, 0x80);
1766 rum_bbp_write(sc, 35, bbp35);
1767 rum_bbp_write(sc, 97, bbp97);
1768 rum_bbp_write(sc, 98, bbp98);
1770 tmp = rum_read(sc, RT2573_PHY_CSR0);
1771 tmp &= ~(RT2573_PA_PE_2GHZ | RT2573_PA_PE_5GHZ);
1772 if (IEEE80211_IS_CHAN_2GHZ(c))
1773 tmp |= RT2573_PA_PE_2GHZ;
1775 tmp |= RT2573_PA_PE_5GHZ;
1776 rum_write(sc, RT2573_PHY_CSR0, tmp);
1778 /* 802.11a uses a 16 microseconds short interframe space */
1779 sc->sifs = IEEE80211_IS_CHAN_5GHZ(c) ? 16 : 10;
1783 rum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c)
1785 struct ieee80211com *ic = &sc->sc_ic;
1786 const struct rfprog *rfprog;
1787 uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT;
1791 chan = ieee80211_chan2ieee(ic, c);
1792 if (chan == 0 || chan == IEEE80211_CHAN_ANY)
1795 /* select the appropriate RF settings based on what EEPROM says */
1796 rfprog = (sc->rf_rev == RT2573_RF_5225 ||
1797 sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226;
1799 /* find the settings for this channel (we know it exists) */
1800 for (i = 0; rfprog[i].chan != chan; i++);
1802 power = sc->txpow[i];
1806 } else if (power > 31) {
1807 bbp94 += power - 31;
1812 * If we are switching from the 2GHz band to the 5GHz band or
1813 * vice-versa, BBP registers need to be reprogrammed.
1815 if (c->ic_flags != ic->ic_curchan->ic_flags) {
1816 rum_select_band(sc, c);
1817 rum_select_antenna(sc);
1821 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1822 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1823 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1824 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1826 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1827 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1828 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7 | 1);
1829 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1831 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1832 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1833 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1834 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1838 /* enable smart mode for MIMO-capable RFs */
1839 bbp3 = rum_bbp_read(sc, 3);
1841 bbp3 &= ~RT2573_SMART_MODE;
1842 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527)
1843 bbp3 |= RT2573_SMART_MODE;
1845 rum_bbp_write(sc, 3, bbp3);
1847 if (bbp94 != RT2573_BBPR94_DEFAULT)
1848 rum_bbp_write(sc, 94, bbp94);
1852 * Enable TSF synchronization and tell h/w to start sending beacons for IBSS
1853 * and HostAP operating modes.
1856 rum_enable_tsf_sync(struct rum_softc *sc)
1858 struct ieee80211com *ic = &sc->sc_ic;
1861 if (ic->ic_opmode != IEEE80211_M_STA) {
1863 * Change default 16ms TBTT adjustment to 8ms.
1864 * Must be done before enabling beacon generation.
1866 rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8);
1869 tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000;
1871 /* set beacon interval (in 1/16ms unit) */
1872 tmp |= ic->ic_bss->ni_intval * 16;
1874 tmp |= RT2573_TSF_TICKING | RT2573_ENABLE_TBTT;
1875 if (ic->ic_opmode == IEEE80211_M_STA)
1876 tmp |= RT2573_TSF_MODE(1);
1878 tmp |= RT2573_TSF_MODE(2) | RT2573_GENERATE_BEACON;
1880 rum_write(sc, RT2573_TXRX_CSR9, tmp);
1884 rum_update_slot(struct ifnet *ifp)
1886 struct rum_softc *sc = ifp->if_softc;
1887 struct ieee80211com *ic = &sc->sc_ic;
1891 slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
1893 tmp = rum_read(sc, RT2573_MAC_CSR9);
1894 tmp = (tmp & ~0xff) | slottime;
1895 rum_write(sc, RT2573_MAC_CSR9, tmp);
1897 DPRINTF(("setting slot time to %uus\n", slottime));
1901 rum_set_bssid(struct rum_softc *sc, const uint8_t *bssid)
1905 tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24;
1906 rum_write(sc, RT2573_MAC_CSR4, tmp);
1908 tmp = bssid[4] | bssid[5] << 8 | RT2573_ONE_BSSID << 16;
1909 rum_write(sc, RT2573_MAC_CSR5, tmp);
1913 rum_set_macaddr(struct rum_softc *sc, const uint8_t *addr)
1917 tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24;
1918 rum_write(sc, RT2573_MAC_CSR2, tmp);
1920 tmp = addr[4] | addr[5] << 8 | 0xff << 16;
1921 rum_write(sc, RT2573_MAC_CSR3, tmp);
1925 rum_update_promisc(struct rum_softc *sc)
1927 struct ifnet *ifp = sc->sc_ic.ic_ifp;
1930 tmp = rum_read(sc, RT2573_TXRX_CSR0);
1932 tmp &= ~RT2573_DROP_NOT_TO_ME;
1933 if (!(ifp->if_flags & IFF_PROMISC))
1934 tmp |= RT2573_DROP_NOT_TO_ME;
1936 rum_write(sc, RT2573_TXRX_CSR0, tmp);
1938 DPRINTF(("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
1939 "entering" : "leaving"));
1946 case RT2573_RF_2527: return "RT2527 (MIMO XR)";
1947 case RT2573_RF_2528: return "RT2528";
1948 case RT2573_RF_5225: return "RT5225 (MIMO XR)";
1949 case RT2573_RF_5226: return "RT5226";
1950 default: return "unknown";
1955 rum_read_eeprom(struct rum_softc *sc)
1957 struct ieee80211com *ic = &sc->sc_ic;
1963 /* read MAC address */
1964 rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, ic->ic_myaddr, 6);
1966 rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2);
1968 sc->rf_rev = (val >> 11) & 0x1f;
1969 sc->hw_radio = (val >> 10) & 0x1;
1970 sc->rx_ant = (val >> 4) & 0x3;
1971 sc->tx_ant = (val >> 2) & 0x3;
1972 sc->nb_ant = val & 0x3;
1974 DPRINTF(("RF revision=%d\n", sc->rf_rev));
1976 rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2);
1978 sc->ext_5ghz_lna = (val >> 6) & 0x1;
1979 sc->ext_2ghz_lna = (val >> 4) & 0x1;
1981 DPRINTF(("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n",
1982 sc->ext_2ghz_lna, sc->ext_5ghz_lna));
1984 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2);
1986 if ((val & 0xff) != 0xff)
1987 sc->rssi_2ghz_corr = (int8_t)(val & 0xff); /* signed */
1989 /* Only [-10, 10] is valid */
1990 if (sc->rssi_2ghz_corr < -10 || sc->rssi_2ghz_corr > 10)
1991 sc->rssi_2ghz_corr = 0;
1993 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2);
1995 if ((val & 0xff) != 0xff)
1996 sc->rssi_5ghz_corr = (int8_t)(val & 0xff); /* signed */
1998 /* Only [-10, 10] is valid */
1999 if (sc->rssi_5ghz_corr < -10 || sc->rssi_5ghz_corr > 10)
2000 sc->rssi_5ghz_corr = 0;
2002 if (sc->ext_2ghz_lna)
2003 sc->rssi_2ghz_corr -= 14;
2004 if (sc->ext_5ghz_lna)
2005 sc->rssi_5ghz_corr -= 14;
2007 DPRINTF(("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n",
2008 sc->rssi_2ghz_corr, sc->rssi_5ghz_corr));
2010 rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2);
2012 if ((val & 0xff) != 0xff)
2013 sc->rffreq = val & 0xff;
2015 DPRINTF(("RF freq=%d\n", sc->rffreq));
2017 /* read Tx power for all a/b/g channels */
2018 rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14);
2019 /* XXX default Tx power for 802.11a channels */
2020 memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14);
2022 for (i = 0; i < 14; i++)
2023 DPRINTF(("Channel=%d Tx power=%d\n", i + 1, sc->txpow[i]));
2026 /* read default values for BBP registers */
2027 rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
2029 for (i = 0; i < 14; i++) {
2030 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
2032 DPRINTF(("BBP R%d=%02x\n", sc->bbp_prom[i].reg,
2033 sc->bbp_prom[i].val));
2039 rum_bbp_init(struct rum_softc *sc)
2041 #define N(a) (sizeof (a) / sizeof ((a)[0]))
2044 /* wait for BBP to be ready */
2045 for (ntries = 0; ntries < 100; ntries++) {
2046 const uint8_t val = rum_bbp_read(sc, 0);
2047 if (val != 0 && val != 0xff)
2051 if (ntries == 100) {
2052 device_printf(sc->sc_dev, "timeout waiting for BBP\n");
2056 /* initialize BBP registers to default values */
2057 for (i = 0; i < N(rum_def_bbp); i++)
2058 rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val);
2060 /* write vendor-specific BBP values (from EEPROM) */
2061 for (i = 0; i < 16; i++) {
2062 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
2064 rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
2072 rum_init(void *priv)
2074 #define N(a) (sizeof (a) / sizeof ((a)[0]))
2075 struct rum_softc *sc = priv;
2076 struct ieee80211com *ic = &sc->sc_ic;
2077 struct ifnet *ifp = ic->ic_ifp;
2078 struct rum_rx_data *data;
2085 /* initialize MAC registers to default values */
2086 for (i = 0; i < N(rum_def_mac); i++)
2087 rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val);
2089 /* set host ready */
2090 rum_write(sc, RT2573_MAC_CSR1, 3);
2091 rum_write(sc, RT2573_MAC_CSR1, 0);
2093 /* wait for BBP/RF to wakeup */
2094 for (ntries = 0; ntries < 1000; ntries++) {
2095 if (rum_read(sc, RT2573_MAC_CSR12) & 8)
2097 rum_write(sc, RT2573_MAC_CSR12, 4); /* force wakeup */
2100 if (ntries == 1000) {
2101 printf("%s: timeout waiting for BBP/RF to wakeup\n",
2102 device_get_nameunit(sc->sc_dev));
2106 if ((error = rum_bbp_init(sc)) != 0)
2109 /* select default channel */
2110 rum_select_band(sc, ic->ic_curchan);
2111 rum_select_antenna(sc);
2112 rum_set_chan(sc, ic->ic_curchan);
2114 /* clear STA registers */
2115 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
2117 IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
2118 rum_set_macaddr(sc, ic->ic_myaddr);
2120 /* initialize ASIC */
2121 rum_write(sc, RT2573_MAC_CSR1, 4);
2124 * Allocate xfer for AMRR statistics requests.
2126 sc->amrr_xfer = usbd_alloc_xfer(sc->sc_udev);
2127 if (sc->amrr_xfer == NULL) {
2128 printf("%s: could not allocate AMRR xfer\n",
2129 device_get_nameunit(sc->sc_dev));
2134 * Open Tx and Rx USB bulk pipes.
2136 error = usbd_open_pipe(sc->sc_iface, sc->sc_tx_no, USBD_EXCLUSIVE_USE,
2139 printf("%s: could not open Tx pipe: %s\n",
2140 device_get_nameunit(sc->sc_dev), usbd_errstr(error));
2143 error = usbd_open_pipe(sc->sc_iface, sc->sc_rx_no, USBD_EXCLUSIVE_USE,
2146 printf("%s: could not open Rx pipe: %s\n",
2147 device_get_nameunit(sc->sc_dev), usbd_errstr(error));
2152 * Allocate Tx and Rx xfer queues.
2154 error = rum_alloc_tx_list(sc);
2156 printf("%s: could not allocate Tx list\n",
2157 device_get_nameunit(sc->sc_dev));
2160 error = rum_alloc_rx_list(sc);
2162 printf("%s: could not allocate Rx list\n",
2163 device_get_nameunit(sc->sc_dev));
2168 * Start up the receive pipe.
2170 for (i = 0; i < RUM_RX_LIST_COUNT; i++) {
2171 data = &sc->rx_data[i];
2173 usbd_setup_xfer(data->xfer, sc->sc_rx_pipeh, data, data->buf,
2174 MCLBYTES, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rum_rxeof);
2175 usbd_transfer(data->xfer);
2178 /* update Rx filter */
2179 tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff;
2181 tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR;
2182 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2183 tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR |
2185 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2186 tmp |= RT2573_DROP_TODS;
2187 if (!(ifp->if_flags & IFF_PROMISC))
2188 tmp |= RT2573_DROP_NOT_TO_ME;
2190 rum_write(sc, RT2573_TXRX_CSR0, tmp);
2192 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2193 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2195 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2196 if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
2197 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2199 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2208 rum_stop(void *priv)
2210 struct rum_softc *sc = priv;
2211 struct ieee80211com *ic = &sc->sc_ic;
2212 struct ifnet *ifp = ic->ic_ifp;
2215 sc->sc_tx_timer = 0;
2216 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2218 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2221 tmp = rum_read(sc, RT2573_TXRX_CSR0);
2222 rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
2225 rum_write(sc, RT2573_MAC_CSR1, 3);
2226 rum_write(sc, RT2573_MAC_CSR1, 0);
2228 if (sc->amrr_xfer != NULL) {
2229 usbd_free_xfer(sc->amrr_xfer);
2230 sc->amrr_xfer = NULL;
2233 if (sc->sc_rx_pipeh != NULL) {
2234 usbd_abort_pipe(sc->sc_rx_pipeh);
2235 usbd_close_pipe(sc->sc_rx_pipeh);
2236 sc->sc_rx_pipeh = NULL;
2238 if (sc->sc_tx_pipeh != NULL) {
2239 usbd_abort_pipe(sc->sc_tx_pipeh);
2240 usbd_close_pipe(sc->sc_tx_pipeh);
2241 sc->sc_tx_pipeh = NULL;
2244 rum_free_rx_list(sc);
2245 rum_free_tx_list(sc);
2249 rum_load_microcode(struct rum_softc *sc, const u_char *ucode, size_t size)
2251 usb_device_request_t req;
2252 uint16_t reg = RT2573_MCU_CODE_BASE;
2255 /* copy firmware image into NIC */
2256 for (; size >= 4; reg += 4, ucode += 4, size -= 4)
2257 rum_write(sc, reg, UGETDW(ucode));
2259 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2260 req.bRequest = RT2573_MCU_CNTL;
2261 USETW(req.wValue, RT2573_MCU_RUN);
2262 USETW(req.wIndex, 0);
2263 USETW(req.wLength, 0);
2265 error = usbd_do_request(sc->sc_udev, &req, NULL);
2267 printf("%s: could not run firmware: %s\n",
2268 device_get_nameunit(sc->sc_dev), usbd_errstr(error));
2274 rum_prepare_beacon(struct rum_softc *sc)
2276 struct ieee80211com *ic = &sc->sc_ic;
2277 struct rum_tx_desc desc;
2281 m0 = ieee80211_beacon_alloc(ic->ic_bss, &sc->sc_bo);
2286 /* send beacons at the lowest available rate */
2287 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
2289 rum_setup_tx_desc(sc, &desc, RT2573_TX_TIMESTAMP, RT2573_TX_HWSEQ,
2290 m0->m_pkthdr.len, rate);
2292 /* copy the first 24 bytes of Tx descriptor into NIC memory */
2293 rum_write_multi(sc, RT2573_HW_BEACON_BASE0, (uint8_t *)&desc, 24);
2295 /* copy beacon header and payload into NIC memory */
2296 rum_write_multi(sc, RT2573_HW_BEACON_BASE0 + 24, mtod(m0, uint8_t *),
2305 rum_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2306 const struct ieee80211_bpf_params *params)
2308 struct ieee80211com *ic = ni->ni_ic;
2309 struct ifnet *ifp = ic->ic_ifp;
2310 struct rum_softc *sc = ifp->if_softc;
2312 /* prevent management frames from being sent if we're not ready */
2313 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2315 ieee80211_free_node(ni);
2318 if (sc->tx_queued >= RUM_TX_LIST_COUNT) {
2319 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2321 ieee80211_free_node(ni);
2325 if (bpf_peers_present(ic->ic_rawbpf))
2326 bpf_mtap(ic->ic_rawbpf, m);
2330 if (params == NULL) {
2332 * Legacy path; interpret frame contents to decide
2333 * precisely how to send the frame.
2335 if (rum_tx_mgt(sc, m, ni) != 0)
2339 * Caller supplied explicit parameters to use in
2340 * sending the frame.
2342 if (rum_tx_raw(sc, m, ni, params) != 0)
2345 sc->sc_tx_timer = 5;
2346 callout_reset(&sc->watchdog_ch, hz, rum_watchdog, sc);
2351 ieee80211_free_node(ni);
2356 rum_amrr_start(struct rum_softc *sc, struct ieee80211_node *ni)
2360 /* clear statistic registers (STA_CSR0 to STA_CSR5) */
2361 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
2363 ieee80211_amrr_node_init(&sc->amrr, &sc->amn);
2365 /* set rate to some reasonable initial value */
2366 for (i = ni->ni_rates.rs_nrates - 1;
2367 i > 0 && (ni->ni_rates.rs_rates[i] & IEEE80211_RATE_VAL) > 72;
2371 callout_reset(&sc->amrr_ch, hz, rum_amrr_timeout, sc);
2375 rum_amrr_timeout(void *arg)
2377 struct rum_softc *sc = (struct rum_softc *)arg;
2378 usb_device_request_t req;
2381 * Asynchronously read statistic registers (cleared by read).
2383 req.bmRequestType = UT_READ_VENDOR_DEVICE;
2384 req.bRequest = RT2573_READ_MULTI_MAC;
2385 USETW(req.wValue, 0);
2386 USETW(req.wIndex, RT2573_STA_CSR0);
2387 USETW(req.wLength, sizeof sc->sta);
2389 usbd_setup_default_xfer(sc->amrr_xfer, sc->sc_udev, sc,
2390 USBD_DEFAULT_TIMEOUT, &req, sc->sta, sizeof sc->sta, 0,
2392 (void)usbd_transfer(sc->amrr_xfer);
2396 rum_amrr_update(usbd_xfer_handle xfer, usbd_private_handle priv,
2399 struct rum_softc *sc = (struct rum_softc *)priv;
2400 struct ifnet *ifp = sc->sc_ic.ic_ifp;
2402 if (status != USBD_NORMAL_COMPLETION) {
2403 device_printf(sc->sc_dev, "could not retrieve Tx statistics - "
2404 "cancelling automatic rate control\n");
2408 /* count TX retry-fail as Tx errors */
2409 ifp->if_oerrors += le32toh(sc->sta[5]) >> 16;
2411 sc->amn.amn_retrycnt =
2412 (le32toh(sc->sta[4]) >> 16) + /* TX one-retry ok count */
2413 (le32toh(sc->sta[5]) & 0xffff) + /* TX more-retry ok count */
2414 (le32toh(sc->sta[5]) >> 16); /* TX retry-fail count */
2417 sc->amn.amn_retrycnt +
2418 (le32toh(sc->sta[4]) & 0xffff); /* TX no-retry ok count */
2420 ieee80211_amrr_choose(&sc->amrr, sc->sc_ic.ic_bss, &sc->amn);
2422 callout_reset(&sc->amrr_ch, hz, rum_amrr_timeout, sc);
2426 rum_scan_start(struct ieee80211com *ic)
2428 struct rum_softc *sc = ic->ic_ifp->if_softc;
2430 usb_rem_task(sc->sc_udev, &sc->sc_scantask);
2432 /* do it in a process context */
2433 sc->sc_scan_action = RUM_SCAN_START;
2434 usb_add_task(sc->sc_udev, &sc->sc_scantask, USB_TASKQ_DRIVER);
2438 rum_scan_end(struct ieee80211com *ic)
2440 struct rum_softc *sc = ic->ic_ifp->if_softc;
2442 usb_rem_task(sc->sc_udev, &sc->sc_scantask);
2444 /* do it in a process context */
2445 sc->sc_scan_action = RUM_SCAN_END;
2446 usb_add_task(sc->sc_udev, &sc->sc_scantask, USB_TASKQ_DRIVER);
2450 rum_set_channel(struct ieee80211com *ic)
2452 struct rum_softc *sc = ic->ic_ifp->if_softc;
2454 usb_rem_task(sc->sc_udev, &sc->sc_scantask);
2456 /* do it in a process context */
2457 sc->sc_scan_action = RUM_SET_CHANNEL;
2458 usb_add_task(sc->sc_udev, &sc->sc_scantask, USB_TASKQ_DRIVER);
2462 rum_scantask(void *arg)
2464 struct rum_softc *sc = arg;
2465 struct ieee80211com *ic = &sc->sc_ic;
2466 struct ifnet *ifp = ic->ic_ifp;
2471 switch (sc->sc_scan_action) {
2472 case RUM_SCAN_START:
2473 /* abort TSF synchronization */
2474 tmp = rum_read(sc, RT2573_TXRX_CSR9);
2475 rum_write(sc, RT2573_TXRX_CSR9, tmp & ~0x00ffffff);
2476 rum_set_bssid(sc, ifp->if_broadcastaddr);
2480 rum_enable_tsf_sync(sc);
2481 /* XXX keep local copy */
2482 rum_set_bssid(sc, ic->ic_bss->ni_bssid);
2485 case RUM_SET_CHANNEL:
2487 rum_set_chan(sc, ic->ic_curchan);
2492 panic("unknown scan action %d\n", sc->sc_scan_action);
2501 rum_get_rssi(struct rum_softc *sc, uint8_t raw)
2505 lna = (raw >> 5) & 0x3;
2512 * NB: Since RSSI is relative to noise floor, -1 is
2513 * adequate for caller to know error happened.
2518 rssi = (2 * agc) - RT2573_NOISE_FLOOR;
2520 if (IEEE80211_IS_CHAN_2GHZ(sc->sc_ic.ic_curchan)) {
2521 rssi += sc->rssi_2ghz_corr;
2530 rssi += sc->rssi_5ghz_corr;
2532 if (!sc->ext_5ghz_lna && lna != 1)
2545 static device_method_t rum_methods[] = {
2546 /* Device interface */
2547 DEVMETHOD(device_probe, rum_match),
2548 DEVMETHOD(device_attach, rum_attach),
2549 DEVMETHOD(device_detach, rum_detach),
2554 static driver_t rum_driver = {
2557 sizeof(struct rum_softc)
2560 static devclass_t rum_devclass;
2562 DRIVER_MODULE(rum, uhub, rum_driver, rum_devclass, usbd_driver_load, 0);