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_COREGA, USB_PRODUCT_COREGA_CGWLUSB2GL },
94 { USB_VENDOR_COREGA, USB_PRODUCT_COREGA_CGWLUSB2GPX },
95 { USB_VENDOR_DICKSMITH, USB_PRODUCT_DICKSMITH_CWD854F },
96 { USB_VENDOR_DICKSMITH, USB_PRODUCT_DICKSMITH_RT2573 },
97 { USB_VENDOR_DLINK2, USB_PRODUCT_DLINK2_DWLG122C1 },
98 { USB_VENDOR_DLINK2, USB_PRODUCT_DLINK2_WUA1340 },
99 { USB_VENDOR_GIGABYTE, USB_PRODUCT_GIGABYTE_GNWB01GS },
100 { USB_VENDOR_GIGABYTE, USB_PRODUCT_GIGABYTE_GNWI05GS },
101 { USB_VENDOR_GIGASET, USB_PRODUCT_GIGASET_RT2573 },
102 { USB_VENDOR_GOODWAY, USB_PRODUCT_GOODWAY_RT2573 },
103 { USB_VENDOR_GUILLEMOT, USB_PRODUCT_GUILLEMOT_HWGUSB254LB },
104 { USB_VENDOR_GUILLEMOT, USB_PRODUCT_GUILLEMOT_HWGUSB254V2AP },
105 { USB_VENDOR_HUAWEI3COM, USB_PRODUCT_HUAWEI3COM_WUB320G },
106 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_G54HP },
107 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_SG54HP },
108 { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2573_1 },
109 { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2573_2 },
110 { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2573_3 },
111 { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2573_4 },
112 { USB_VENDOR_NOVATECH, USB_PRODUCT_NOVATECH_RT2573 },
113 { USB_VENDOR_PLANEX2, USB_PRODUCT_PLANEX2_GWUS54HP },
114 { USB_VENDOR_PLANEX2, USB_PRODUCT_PLANEX2_GWUS54MINI2 },
115 { USB_VENDOR_PLANEX2, USB_PRODUCT_PLANEX2_GWUSMM },
116 { USB_VENDOR_QCOM, USB_PRODUCT_QCOM_RT2573 },
117 { USB_VENDOR_QCOM, USB_PRODUCT_QCOM_RT2573_2 },
118 { USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2573 },
119 { USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2573_2 },
120 { USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2671 },
121 { USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_WL113R2 },
122 { USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_WL172 },
123 { USB_VENDOR_SPARKLAN, USB_PRODUCT_SPARKLAN_RT2573 },
124 { USB_VENDOR_SURECOM, USB_PRODUCT_SURECOM_RT2573 }
127 MODULE_DEPEND(rum, wlan, 1, 1, 1);
128 MODULE_DEPEND(rum, wlan_amrr, 1, 1, 1);
129 MODULE_DEPEND(rum, usb, 1, 1, 1);
131 static int rum_alloc_tx_list(struct rum_softc *);
132 static void rum_free_tx_list(struct rum_softc *);
133 static int rum_alloc_rx_list(struct rum_softc *);
134 static void rum_free_rx_list(struct rum_softc *);
135 static int rum_media_change(struct ifnet *);
136 static void rum_task(void *);
137 static void rum_scantask(void *);
138 static int rum_newstate(struct ieee80211com *,
139 enum ieee80211_state, int);
140 static void rum_txeof(usbd_xfer_handle, usbd_private_handle,
142 static void rum_rxeof(usbd_xfer_handle, usbd_private_handle,
144 static int rum_rxrate(struct rum_rx_desc *);
145 static int rum_ack_rate(struct ieee80211com *, int);
146 static uint16_t rum_txtime(int, int, uint32_t);
147 static uint8_t rum_plcp_signal(int);
148 static void rum_setup_tx_desc(struct rum_softc *,
149 struct rum_tx_desc *, uint32_t, uint16_t, int,
151 static int rum_tx_mgt(struct rum_softc *, struct mbuf *,
152 struct ieee80211_node *);
153 static int rum_tx_raw(struct rum_softc *, struct mbuf *,
154 struct ieee80211_node *,
155 const struct ieee80211_bpf_params *);
156 static int rum_tx_data(struct rum_softc *, struct mbuf *,
157 struct ieee80211_node *);
158 static void rum_start(struct ifnet *);
159 static void rum_watchdog(void *);
160 static int rum_ioctl(struct ifnet *, u_long, caddr_t);
161 static void rum_eeprom_read(struct rum_softc *, uint16_t, void *,
163 static uint32_t rum_read(struct rum_softc *, uint16_t);
164 static void rum_read_multi(struct rum_softc *, uint16_t, void *,
166 static void rum_write(struct rum_softc *, uint16_t, uint32_t);
167 static void rum_write_multi(struct rum_softc *, uint16_t, void *,
169 static void rum_bbp_write(struct rum_softc *, uint8_t, uint8_t);
170 static uint8_t rum_bbp_read(struct rum_softc *, uint8_t);
171 static void rum_rf_write(struct rum_softc *, uint8_t, uint32_t);
172 static void rum_select_antenna(struct rum_softc *);
173 static void rum_enable_mrr(struct rum_softc *);
174 static void rum_set_txpreamble(struct rum_softc *);
175 static void rum_set_basicrates(struct rum_softc *);
176 static void rum_select_band(struct rum_softc *,
177 struct ieee80211_channel *);
178 static void rum_set_chan(struct rum_softc *,
179 struct ieee80211_channel *);
180 static void rum_enable_tsf_sync(struct rum_softc *);
181 static void rum_update_slot(struct ifnet *);
182 static void rum_set_bssid(struct rum_softc *, const uint8_t *);
183 static void rum_set_macaddr(struct rum_softc *, const uint8_t *);
184 static void rum_update_promisc(struct rum_softc *);
185 static const char *rum_get_rf(int);
186 static void rum_read_eeprom(struct rum_softc *);
187 static int rum_bbp_init(struct rum_softc *);
188 static void rum_init(void *);
189 static void rum_stop(void *);
190 static int rum_load_microcode(struct rum_softc *, const u_char *,
192 static int rum_prepare_beacon(struct rum_softc *);
193 static int rum_raw_xmit(struct ieee80211_node *, struct mbuf *,
194 const struct ieee80211_bpf_params *);
195 static void rum_scan_start(struct ieee80211com *);
196 static void rum_scan_end(struct ieee80211com *);
197 static void rum_set_channel(struct ieee80211com *);
198 static int rum_get_rssi(struct rum_softc *, uint8_t);
199 static void rum_amrr_start(struct rum_softc *,
200 struct ieee80211_node *);
201 static void rum_amrr_timeout(void *);
202 static void rum_amrr_update(usbd_xfer_handle, usbd_private_handle,
205 static const struct {
209 { RT2573_TXRX_CSR0, 0x025fb032 },
210 { RT2573_TXRX_CSR1, 0x9eaa9eaf },
211 { RT2573_TXRX_CSR2, 0x8a8b8c8d },
212 { RT2573_TXRX_CSR3, 0x00858687 },
213 { RT2573_TXRX_CSR7, 0x2e31353b },
214 { RT2573_TXRX_CSR8, 0x2a2a2a2c },
215 { RT2573_TXRX_CSR15, 0x0000000f },
216 { RT2573_MAC_CSR6, 0x00000fff },
217 { RT2573_MAC_CSR8, 0x016c030a },
218 { RT2573_MAC_CSR10, 0x00000718 },
219 { RT2573_MAC_CSR12, 0x00000004 },
220 { RT2573_MAC_CSR13, 0x00007f00 },
221 { RT2573_SEC_CSR0, 0x00000000 },
222 { RT2573_SEC_CSR1, 0x00000000 },
223 { RT2573_SEC_CSR5, 0x00000000 },
224 { RT2573_PHY_CSR1, 0x000023b0 },
225 { RT2573_PHY_CSR5, 0x00040a06 },
226 { RT2573_PHY_CSR6, 0x00080606 },
227 { RT2573_PHY_CSR7, 0x00000408 },
228 { RT2573_AIFSN_CSR, 0x00002273 },
229 { RT2573_CWMIN_CSR, 0x00002344 },
230 { RT2573_CWMAX_CSR, 0x000034aa }
233 static const struct {
265 static const struct rfprog {
267 uint32_t r1, r2, r3, r4;
269 { 1, 0x00b03, 0x001e1, 0x1a014, 0x30282 },
270 { 2, 0x00b03, 0x001e1, 0x1a014, 0x30287 },
271 { 3, 0x00b03, 0x001e2, 0x1a014, 0x30282 },
272 { 4, 0x00b03, 0x001e2, 0x1a014, 0x30287 },
273 { 5, 0x00b03, 0x001e3, 0x1a014, 0x30282 },
274 { 6, 0x00b03, 0x001e3, 0x1a014, 0x30287 },
275 { 7, 0x00b03, 0x001e4, 0x1a014, 0x30282 },
276 { 8, 0x00b03, 0x001e4, 0x1a014, 0x30287 },
277 { 9, 0x00b03, 0x001e5, 0x1a014, 0x30282 },
278 { 10, 0x00b03, 0x001e5, 0x1a014, 0x30287 },
279 { 11, 0x00b03, 0x001e6, 0x1a014, 0x30282 },
280 { 12, 0x00b03, 0x001e6, 0x1a014, 0x30287 },
281 { 13, 0x00b03, 0x001e7, 0x1a014, 0x30282 },
282 { 14, 0x00b03, 0x001e8, 0x1a014, 0x30284 },
284 { 34, 0x00b03, 0x20266, 0x36014, 0x30282 },
285 { 38, 0x00b03, 0x20267, 0x36014, 0x30284 },
286 { 42, 0x00b03, 0x20268, 0x36014, 0x30286 },
287 { 46, 0x00b03, 0x20269, 0x36014, 0x30288 },
289 { 36, 0x00b03, 0x00266, 0x26014, 0x30288 },
290 { 40, 0x00b03, 0x00268, 0x26014, 0x30280 },
291 { 44, 0x00b03, 0x00269, 0x26014, 0x30282 },
292 { 48, 0x00b03, 0x0026a, 0x26014, 0x30284 },
293 { 52, 0x00b03, 0x0026b, 0x26014, 0x30286 },
294 { 56, 0x00b03, 0x0026c, 0x26014, 0x30288 },
295 { 60, 0x00b03, 0x0026e, 0x26014, 0x30280 },
296 { 64, 0x00b03, 0x0026f, 0x26014, 0x30282 },
298 { 100, 0x00b03, 0x0028a, 0x2e014, 0x30280 },
299 { 104, 0x00b03, 0x0028b, 0x2e014, 0x30282 },
300 { 108, 0x00b03, 0x0028c, 0x2e014, 0x30284 },
301 { 112, 0x00b03, 0x0028d, 0x2e014, 0x30286 },
302 { 116, 0x00b03, 0x0028e, 0x2e014, 0x30288 },
303 { 120, 0x00b03, 0x002a0, 0x2e014, 0x30280 },
304 { 124, 0x00b03, 0x002a1, 0x2e014, 0x30282 },
305 { 128, 0x00b03, 0x002a2, 0x2e014, 0x30284 },
306 { 132, 0x00b03, 0x002a3, 0x2e014, 0x30286 },
307 { 136, 0x00b03, 0x002a4, 0x2e014, 0x30288 },
308 { 140, 0x00b03, 0x002a6, 0x2e014, 0x30280 },
310 { 149, 0x00b03, 0x002a8, 0x2e014, 0x30287 },
311 { 153, 0x00b03, 0x002a9, 0x2e014, 0x30289 },
312 { 157, 0x00b03, 0x002ab, 0x2e014, 0x30281 },
313 { 161, 0x00b03, 0x002ac, 0x2e014, 0x30283 },
314 { 165, 0x00b03, 0x002ad, 0x2e014, 0x30285 }
316 { 1, 0x00b33, 0x011e1, 0x1a014, 0x30282 },
317 { 2, 0x00b33, 0x011e1, 0x1a014, 0x30287 },
318 { 3, 0x00b33, 0x011e2, 0x1a014, 0x30282 },
319 { 4, 0x00b33, 0x011e2, 0x1a014, 0x30287 },
320 { 5, 0x00b33, 0x011e3, 0x1a014, 0x30282 },
321 { 6, 0x00b33, 0x011e3, 0x1a014, 0x30287 },
322 { 7, 0x00b33, 0x011e4, 0x1a014, 0x30282 },
323 { 8, 0x00b33, 0x011e4, 0x1a014, 0x30287 },
324 { 9, 0x00b33, 0x011e5, 0x1a014, 0x30282 },
325 { 10, 0x00b33, 0x011e5, 0x1a014, 0x30287 },
326 { 11, 0x00b33, 0x011e6, 0x1a014, 0x30282 },
327 { 12, 0x00b33, 0x011e6, 0x1a014, 0x30287 },
328 { 13, 0x00b33, 0x011e7, 0x1a014, 0x30282 },
329 { 14, 0x00b33, 0x011e8, 0x1a014, 0x30284 },
331 { 34, 0x00b33, 0x01266, 0x26014, 0x30282 },
332 { 38, 0x00b33, 0x01267, 0x26014, 0x30284 },
333 { 42, 0x00b33, 0x01268, 0x26014, 0x30286 },
334 { 46, 0x00b33, 0x01269, 0x26014, 0x30288 },
336 { 36, 0x00b33, 0x01266, 0x26014, 0x30288 },
337 { 40, 0x00b33, 0x01268, 0x26014, 0x30280 },
338 { 44, 0x00b33, 0x01269, 0x26014, 0x30282 },
339 { 48, 0x00b33, 0x0126a, 0x26014, 0x30284 },
340 { 52, 0x00b33, 0x0126b, 0x26014, 0x30286 },
341 { 56, 0x00b33, 0x0126c, 0x26014, 0x30288 },
342 { 60, 0x00b33, 0x0126e, 0x26014, 0x30280 },
343 { 64, 0x00b33, 0x0126f, 0x26014, 0x30282 },
345 { 100, 0x00b33, 0x0128a, 0x2e014, 0x30280 },
346 { 104, 0x00b33, 0x0128b, 0x2e014, 0x30282 },
347 { 108, 0x00b33, 0x0128c, 0x2e014, 0x30284 },
348 { 112, 0x00b33, 0x0128d, 0x2e014, 0x30286 },
349 { 116, 0x00b33, 0x0128e, 0x2e014, 0x30288 },
350 { 120, 0x00b33, 0x012a0, 0x2e014, 0x30280 },
351 { 124, 0x00b33, 0x012a1, 0x2e014, 0x30282 },
352 { 128, 0x00b33, 0x012a2, 0x2e014, 0x30284 },
353 { 132, 0x00b33, 0x012a3, 0x2e014, 0x30286 },
354 { 136, 0x00b33, 0x012a4, 0x2e014, 0x30288 },
355 { 140, 0x00b33, 0x012a6, 0x2e014, 0x30280 },
357 { 149, 0x00b33, 0x012a8, 0x2e014, 0x30287 },
358 { 153, 0x00b33, 0x012a9, 0x2e014, 0x30289 },
359 { 157, 0x00b33, 0x012ab, 0x2e014, 0x30281 },
360 { 161, 0x00b33, 0x012ac, 0x2e014, 0x30283 },
361 { 165, 0x00b33, 0x012ad, 0x2e014, 0x30285 }
365 rum_match(device_t self)
367 struct usb_attach_arg *uaa = device_get_ivars(self);
369 if (uaa->iface != NULL)
372 return (usb_lookup(rum_devs, uaa->vendor, uaa->product) != NULL) ?
373 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
377 rum_attach(device_t self)
379 struct rum_softc *sc = device_get_softc(self);
380 struct usb_attach_arg *uaa = device_get_ivars(self);
381 struct ieee80211com *ic = &sc->sc_ic;
383 const uint8_t *ucode = NULL;
384 usb_interface_descriptor_t *id;
385 usb_endpoint_descriptor_t *ed;
387 int i, ntries, size, bands;
390 sc->sc_udev = uaa->device;
393 if (usbd_set_config_no(sc->sc_udev, RT2573_CONFIG_NO, 0) != 0) {
394 printf("%s: could not set configuration no\n",
395 device_get_nameunit(sc->sc_dev));
399 /* get the first interface handle */
400 error = usbd_device2interface_handle(sc->sc_udev, RT2573_IFACE_INDEX,
403 printf("%s: could not get interface handle\n",
404 device_get_nameunit(sc->sc_dev));
411 id = usbd_get_interface_descriptor(sc->sc_iface);
413 sc->sc_rx_no = sc->sc_tx_no = -1;
414 for (i = 0; i < id->bNumEndpoints; i++) {
415 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
417 printf("%s: no endpoint descriptor for iface %d\n",
418 device_get_nameunit(sc->sc_dev), i);
422 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
423 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
424 sc->sc_rx_no = ed->bEndpointAddress;
425 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
426 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
427 sc->sc_tx_no = ed->bEndpointAddress;
429 if (sc->sc_rx_no == -1 || sc->sc_tx_no == -1) {
430 printf("%s: missing endpoint\n",
431 device_get_nameunit(sc->sc_dev));
435 mtx_init(&sc->sc_mtx, device_get_nameunit(sc->sc_dev), MTX_NETWORK_LOCK,
436 MTX_DEF | MTX_RECURSE);
438 usb_init_task(&sc->sc_task, rum_task, sc);
439 usb_init_task(&sc->sc_scantask, rum_scantask, sc);
440 callout_init(&sc->watchdog_ch, 0);
441 callout_init(&sc->amrr_ch, 0);
443 /* retrieve RT2573 rev. no */
444 for (ntries = 0; ntries < 1000; ntries++) {
445 if ((tmp = rum_read(sc, RT2573_MAC_CSR0)) != 0)
449 if (ntries == 1000) {
450 printf("%s: timeout waiting for chip to settle\n",
451 device_get_nameunit(sc->sc_dev));
455 /* retrieve MAC address and various other things from EEPROM */
458 printf("%s: MAC/BBP RT2573 (rev 0x%05x), RF %s\n",
459 device_get_nameunit(sc->sc_dev), tmp, rum_get_rf(sc->rf_rev));
461 ucode = rt2573_ucode;
462 size = sizeof rt2573_ucode;
463 error = rum_load_microcode(sc, ucode, size);
465 device_printf(sc->sc_dev, "could not load 8051 microcode\n");
466 mtx_destroy(&sc->sc_mtx);
470 ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
472 printf("%s: can not if_alloc()\n",
473 device_get_nameunit(sc->sc_dev));
474 mtx_destroy(&sc->sc_mtx);
479 if_initname(ifp, "rum", device_get_unit(sc->sc_dev));
480 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST |
481 IFF_NEEDSGIANT; /* USB stack is still under Giant lock */
482 ifp->if_init = rum_init;
483 ifp->if_ioctl = rum_ioctl;
484 ifp->if_start = rum_start;
485 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
486 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
487 IFQ_SET_READY(&ifp->if_snd);
490 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
491 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
492 ic->ic_state = IEEE80211_S_INIT;
494 /* set device capabilities */
496 IEEE80211_C_IBSS | /* IBSS mode supported */
497 IEEE80211_C_MONITOR | /* monitor mode supported */
498 IEEE80211_C_HOSTAP | /* HostAp mode supported */
499 IEEE80211_C_TXPMGT | /* tx power management */
500 IEEE80211_C_SHPREAMBLE | /* short preamble supported */
501 IEEE80211_C_SHSLOT | /* short slot time supported */
502 IEEE80211_C_BGSCAN | /* bg scanning supported */
503 IEEE80211_C_WPA; /* 802.11i */
506 setbit(&bands, IEEE80211_MODE_11B);
507 setbit(&bands, IEEE80211_MODE_11G);
508 ieee80211_init_channels(ic, 0, CTRY_DEFAULT, bands, 0, 1);
510 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_5226) {
511 struct ieee80211_channel *c;
513 /* set supported .11a channels */
514 for (i = 34; i <= 46; i += 4) {
515 c = &ic->ic_channels[ic->ic_nchans++];
516 c->ic_freq = ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
517 c->ic_flags = IEEE80211_CHAN_A;
520 for (i = 36; i <= 64; i += 4) {
521 c = &ic->ic_channels[ic->ic_nchans++];
522 c->ic_freq = ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
523 c->ic_flags = IEEE80211_CHAN_A;
526 for (i = 100; i <= 140; i += 4) {
527 c = &ic->ic_channels[ic->ic_nchans++];
528 c->ic_freq = ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
529 c->ic_flags = IEEE80211_CHAN_A;
532 for (i = 149; i <= 165; i += 4) {
533 c = &ic->ic_channels[ic->ic_nchans++];
534 c->ic_freq = ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
535 c->ic_flags = IEEE80211_CHAN_A;
540 ieee80211_ifattach(ic);
541 ic->ic_scan_start = rum_scan_start;
542 ic->ic_scan_end = rum_scan_end;
543 ic->ic_set_channel = rum_set_channel;
545 /* enable s/w bmiss handling in sta mode */
546 ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS;
548 /* override state transition machine */
549 sc->sc_newstate = ic->ic_newstate;
550 ic->ic_newstate = rum_newstate;
551 ic->ic_raw_xmit = rum_raw_xmit;
552 ieee80211_media_init(ic, rum_media_change, ieee80211_media_status);
554 ieee80211_amrr_init(&sc->amrr, ic,
555 IEEE80211_AMRR_MIN_SUCCESS_THRESHOLD,
556 IEEE80211_AMRR_MAX_SUCCESS_THRESHOLD);
558 bpfattach2(ifp, DLT_IEEE802_11_RADIO,
559 sizeof (struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN,
562 sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
563 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
564 sc->sc_rxtap.wr_ihdr.it_present = htole32(RT2573_RX_RADIOTAP_PRESENT);
566 sc->sc_txtap_len = sizeof sc->sc_txtapu;
567 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
568 sc->sc_txtap.wt_ihdr.it_present = htole32(RT2573_TX_RADIOTAP_PRESENT);
571 ieee80211_announce(ic);
577 rum_detach(device_t self)
579 struct rum_softc *sc = device_get_softc(self);
580 struct ieee80211com *ic = &sc->sc_ic;
581 struct ifnet *ifp = ic->ic_ifp;
584 usb_rem_task(sc->sc_udev, &sc->sc_task);
585 usb_rem_task(sc->sc_udev, &sc->sc_scantask);
586 callout_stop(&sc->watchdog_ch);
587 callout_stop(&sc->amrr_ch);
589 if (sc->amrr_xfer != NULL) {
590 usbd_free_xfer(sc->amrr_xfer);
591 sc->amrr_xfer = NULL;
594 if (sc->sc_rx_pipeh != NULL) {
595 usbd_abort_pipe(sc->sc_rx_pipeh);
596 usbd_close_pipe(sc->sc_rx_pipeh);
598 if (sc->sc_tx_pipeh != NULL) {
599 usbd_abort_pipe(sc->sc_tx_pipeh);
600 usbd_close_pipe(sc->sc_tx_pipeh);
603 rum_free_rx_list(sc);
604 rum_free_tx_list(sc);
607 ieee80211_ifdetach(ic);
610 mtx_destroy(&sc->sc_mtx);
616 rum_alloc_tx_list(struct rum_softc *sc)
618 struct rum_tx_data *data;
623 for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
624 data = &sc->tx_data[i];
628 data->xfer = usbd_alloc_xfer(sc->sc_udev);
629 if (data->xfer == NULL) {
630 printf("%s: could not allocate tx xfer\n",
631 device_get_nameunit(sc->sc_dev));
635 data->buf = usbd_alloc_buffer(data->xfer,
636 RT2573_TX_DESC_SIZE + MCLBYTES);
637 if (data->buf == NULL) {
638 printf("%s: could not allocate tx buffer\n",
639 device_get_nameunit(sc->sc_dev));
643 /* clean Tx descriptor */
644 bzero(data->buf, RT2573_TX_DESC_SIZE);
649 fail: rum_free_tx_list(sc);
654 rum_free_tx_list(struct rum_softc *sc)
656 struct rum_tx_data *data;
659 for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
660 data = &sc->tx_data[i];
662 if (data->xfer != NULL) {
663 usbd_free_xfer(data->xfer);
667 if (data->ni != NULL) {
668 ieee80211_free_node(data->ni);
675 rum_alloc_rx_list(struct rum_softc *sc)
677 struct rum_rx_data *data;
680 for (i = 0; i < RUM_RX_LIST_COUNT; i++) {
681 data = &sc->rx_data[i];
685 data->xfer = usbd_alloc_xfer(sc->sc_udev);
686 if (data->xfer == NULL) {
687 printf("%s: could not allocate rx xfer\n",
688 device_get_nameunit(sc->sc_dev));
692 if (usbd_alloc_buffer(data->xfer, MCLBYTES) == NULL) {
693 printf("%s: could not allocate rx buffer\n",
694 device_get_nameunit(sc->sc_dev));
699 data->m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
700 if (data->m == NULL) {
701 printf("%s: could not allocate rx mbuf\n",
702 device_get_nameunit(sc->sc_dev));
707 data->buf = mtod(data->m, uint8_t *);
712 fail: rum_free_tx_list(sc);
717 rum_free_rx_list(struct rum_softc *sc)
719 struct rum_rx_data *data;
722 for (i = 0; i < RUM_RX_LIST_COUNT; i++) {
723 data = &sc->rx_data[i];
725 if (data->xfer != NULL) {
726 usbd_free_xfer(data->xfer);
729 if (data->m != NULL) {
737 rum_media_change(struct ifnet *ifp)
739 struct rum_softc *sc = ifp->if_softc;
744 error = ieee80211_media_change(ifp);
745 if (error != ENETRESET) {
750 if ((ifp->if_flags & IFF_UP) &&
751 (ifp->if_drv_flags & IFF_DRV_RUNNING))
762 struct rum_softc *sc = arg;
763 struct ieee80211com *ic = &sc->sc_ic;
764 enum ieee80211_state ostate;
765 struct ieee80211_node *ni;
768 ostate = ic->ic_state;
772 switch (sc->sc_state) {
773 case IEEE80211_S_INIT:
774 if (ostate == IEEE80211_S_RUN) {
775 /* abort TSF synchronization */
776 tmp = rum_read(sc, RT2573_TXRX_CSR9);
777 rum_write(sc, RT2573_TXRX_CSR9, tmp & ~0x00ffffff);
781 case IEEE80211_S_RUN:
784 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
785 rum_update_slot(ic->ic_ifp);
787 rum_set_txpreamble(sc);
788 rum_set_basicrates(sc);
789 rum_set_bssid(sc, ni->ni_bssid);
792 if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
793 ic->ic_opmode == IEEE80211_M_IBSS)
794 rum_prepare_beacon(sc);
796 if (ic->ic_opmode != IEEE80211_M_MONITOR)
797 rum_enable_tsf_sync(sc);
799 /* enable automatic rate adaptation in STA mode */
800 if (ic->ic_opmode == IEEE80211_M_STA &&
801 ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE)
802 rum_amrr_start(sc, ni);
810 sc->sc_newstate(ic, sc->sc_state, sc->sc_arg);
814 rum_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
816 struct rum_softc *sc = ic->ic_ifp->if_softc;
818 callout_stop(&sc->amrr_ch);
820 /* do it in a process context */
821 sc->sc_state = nstate;
824 usb_rem_task(sc->sc_udev, &sc->sc_task);
825 if (nstate == IEEE80211_S_INIT)
826 sc->sc_newstate(ic, nstate, arg);
828 usb_add_task(sc->sc_udev, &sc->sc_task, USB_TASKQ_DRIVER);
832 /* quickly determine if a given rate is CCK or OFDM */
833 #define RUM_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
835 #define RUM_ACK_SIZE 14 /* 10 + 4(FCS) */
836 #define RUM_CTS_SIZE 14 /* 10 + 4(FCS) */
839 rum_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
841 struct rum_tx_data *data = priv;
842 struct rum_softc *sc = data->sc;
843 struct ifnet *ifp = sc->sc_ic.ic_ifp;
845 if (data->m != NULL && data->m->m_flags & M_TXCB)
846 ieee80211_process_callback(data->ni, data->m, 0/*XXX*/);
848 if (status != USBD_NORMAL_COMPLETION) {
849 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
852 printf("%s: could not transmit buffer: %s\n",
853 device_get_nameunit(sc->sc_dev), usbd_errstr(status));
855 if (status == USBD_STALLED)
856 usbd_clear_endpoint_stall_async(sc->sc_tx_pipeh);
864 ieee80211_free_node(data->ni);
870 DPRINTFN(10, ("tx done\n"));
873 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
878 rum_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
880 struct rum_rx_data *data = priv;
881 struct rum_softc *sc = data->sc;
882 struct ieee80211com *ic = &sc->sc_ic;
883 struct ifnet *ifp = ic->ic_ifp;
884 struct rum_rx_desc *desc;
885 struct ieee80211_frame *wh;
886 struct ieee80211_node *ni;
887 struct mbuf *mnew, *m;
890 if (status != USBD_NORMAL_COMPLETION) {
891 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
894 if (status == USBD_STALLED)
895 usbd_clear_endpoint_stall_async(sc->sc_rx_pipeh);
899 usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
901 if (len < RT2573_RX_DESC_SIZE + sizeof (struct ieee80211_frame_min)) {
902 DPRINTF(("%s: xfer too short %d\n",
903 device_get_nameunit(sc->sc_dev), len));
908 desc = (struct rum_rx_desc *)data->buf;
910 if (le32toh(desc->flags) & RT2573_RX_CRC_ERROR) {
912 * This should not happen since we did not request to receive
913 * those frames when we filled RT2573_TXRX_CSR0.
915 DPRINTFN(5, ("CRC error\n"));
920 mnew = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
928 data->buf = mtod(data->m, uint8_t *);
931 m->m_pkthdr.rcvif = ifp;
932 m->m_data = (caddr_t)(desc + 1);
933 m->m_pkthdr.len = m->m_len = (le32toh(desc->flags) >> 16) & 0xfff;
935 rssi = rum_get_rssi(sc, desc->rssi);
937 wh = mtod(m, struct ieee80211_frame *);
938 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
940 /* Error happened during RSSI conversion. */
944 if (bpf_peers_present(sc->sc_drvbpf)) {
945 struct rum_rx_radiotap_header *tap = &sc->sc_rxtap;
947 tap->wr_flags = IEEE80211_RADIOTAP_F_FCS;
948 tap->wr_rate = rum_rxrate(desc);
949 tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
950 tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
951 tap->wr_antenna = sc->rx_ant;
952 tap->wr_antsignal = rssi;
954 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m);
957 /* send the frame to the 802.11 layer */
958 ieee80211_input(ic, m, ni, rssi, RT2573_NOISE_FLOOR, 0);
960 /* node is no longer needed */
961 ieee80211_free_node(ni);
963 DPRINTFN(15, ("rx done\n"));
965 skip: /* setup a new transfer */
966 usbd_setup_xfer(xfer, sc->sc_rx_pipeh, data, data->buf, MCLBYTES,
967 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rum_rxeof);
972 * This function is only used by the Rx radiotap code.
975 rum_rxrate(struct rum_rx_desc *desc)
977 if (le32toh(desc->flags) & RT2573_RX_OFDM) {
978 /* reverse function of rum_plcp_signal */
979 switch (desc->rate) {
987 case 0xc: return 108;
990 if (desc->rate == 10)
992 if (desc->rate == 20)
994 if (desc->rate == 55)
996 if (desc->rate == 110)
999 return 2; /* should not get there */
1003 * Return the expected ack rate for a frame transmitted at rate `rate'.
1006 rum_ack_rate(struct ieee80211com *ic, int rate)
1015 return (ic->ic_curmode == IEEE80211_MODE_11B) ? 4 : rate;
1031 /* default to 1Mbps */
1036 * Compute the duration (in us) needed to transmit `len' bytes at rate `rate'.
1037 * The function automatically determines the operating mode depending on the
1038 * given rate. `flags' indicates whether short preamble is in use or not.
1041 rum_txtime(int len, int rate, uint32_t flags)
1045 if (RUM_RATE_IS_OFDM(rate)) {
1046 /* IEEE Std 802.11a-1999, pp. 37 */
1047 txtime = (8 + 4 * len + 3 + rate - 1) / rate;
1048 txtime = 16 + 4 + 4 * txtime + 6;
1050 /* IEEE Std 802.11b-1999, pp. 28 */
1051 txtime = (16 * len + rate - 1) / rate;
1052 if (rate != 2 && (flags & IEEE80211_F_SHPREAMBLE))
1061 rum_plcp_signal(int rate)
1064 /* CCK rates (returned values are device-dependent) */
1067 case 11: return 0x2;
1068 case 22: return 0x3;
1070 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1071 case 12: return 0xb;
1072 case 18: return 0xf;
1073 case 24: return 0xa;
1074 case 36: return 0xe;
1075 case 48: return 0x9;
1076 case 72: return 0xd;
1077 case 96: return 0x8;
1078 case 108: return 0xc;
1080 /* unsupported rates (should not get there) */
1081 default: return 0xff;
1086 rum_setup_tx_desc(struct rum_softc *sc, struct rum_tx_desc *desc,
1087 uint32_t flags, uint16_t xflags, int len, int rate)
1089 struct ieee80211com *ic = &sc->sc_ic;
1090 uint16_t plcp_length;
1093 desc->flags = htole32(flags);
1094 desc->flags |= htole32(RT2573_TX_VALID);
1095 desc->flags |= htole32(len << 16);
1097 desc->xflags = htole16(xflags);
1099 desc->wme = htole16(RT2573_QID(0) | RT2573_AIFSN(2) |
1100 RT2573_LOGCWMIN(4) | RT2573_LOGCWMAX(10));
1102 /* setup PLCP fields */
1103 desc->plcp_signal = rum_plcp_signal(rate);
1104 desc->plcp_service = 4;
1106 len += IEEE80211_CRC_LEN;
1107 if (RUM_RATE_IS_OFDM(rate)) {
1108 desc->flags |= htole32(RT2573_TX_OFDM);
1110 plcp_length = len & 0xfff;
1111 desc->plcp_length_hi = plcp_length >> 6;
1112 desc->plcp_length_lo = plcp_length & 0x3f;
1114 plcp_length = (16 * len + rate - 1) / rate;
1116 remainder = (16 * len) % 22;
1117 if (remainder != 0 && remainder < 7)
1118 desc->plcp_service |= RT2573_PLCP_LENGEXT;
1120 desc->plcp_length_hi = plcp_length >> 8;
1121 desc->plcp_length_lo = plcp_length & 0xff;
1123 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1124 desc->plcp_signal |= 0x08;
1128 #define RUM_TX_TIMEOUT 5000
1131 rum_tx_mgt(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1133 struct ieee80211com *ic = &sc->sc_ic;
1134 struct rum_tx_desc *desc;
1135 struct rum_tx_data *data;
1136 struct ieee80211_frame *wh;
1137 struct ieee80211_key *k;
1143 data = &sc->tx_data[0];
1144 desc = (struct rum_tx_desc *)data->buf;
1146 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
1151 wh = mtod(m0, struct ieee80211_frame *);
1153 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1154 k = ieee80211_crypto_encap(ic, ni, m0);
1161 wh = mtod(m0, struct ieee80211_frame *);
1163 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1164 flags |= RT2573_TX_NEED_ACK;
1166 dur = rum_txtime(RUM_ACK_SIZE, rum_ack_rate(ic, rate),
1167 ic->ic_flags) + sc->sifs;
1168 *(uint16_t *)wh->i_dur = htole16(dur);
1170 /* tell hardware to add timestamp for probe responses */
1172 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
1173 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
1174 flags |= RT2573_TX_TIMESTAMP;
1177 if (bpf_peers_present(sc->sc_drvbpf)) {
1178 struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
1181 tap->wt_rate = rate;
1182 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1183 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1184 tap->wt_antenna = sc->tx_ant;
1186 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
1189 m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RT2573_TX_DESC_SIZE);
1190 rum_setup_tx_desc(sc, desc, flags, 0, m0->m_pkthdr.len, rate);
1192 /* align end on a 4-bytes boundary */
1193 xferlen = (RT2573_TX_DESC_SIZE + m0->m_pkthdr.len + 3) & ~3;
1196 * No space left in the last URB to store the extra 4 bytes, force
1197 * sending of another URB.
1199 if ((xferlen % 64) == 0)
1202 DPRINTFN(10, ("sending mgt frame len=%d rate=%d xfer len=%d\n",
1203 m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, rate, xferlen));
1205 usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf, xferlen,
1206 USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RUM_TX_TIMEOUT, rum_txeof);
1208 error = usbd_transfer(data->xfer);
1209 if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS) {
1222 rum_tx_raw(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
1223 const struct ieee80211_bpf_params *params)
1225 struct ieee80211com *ic = &sc->sc_ic;
1226 struct rum_tx_desc *desc;
1227 struct rum_tx_data *data;
1232 data = &sc->tx_data[0];
1233 desc = (struct rum_tx_desc *)data->buf;
1235 rate = params->ibp_rate0 & IEEE80211_RATE_VAL;
1242 if (bpf_peers_present(sc->sc_drvbpf)) {
1243 struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
1246 tap->wt_rate = rate;
1247 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1248 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1249 tap->wt_antenna = sc->tx_ant;
1251 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
1258 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
1259 flags |= RT2573_TX_NEED_ACK;
1261 m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RT2573_TX_DESC_SIZE);
1262 /* XXX need to setup descriptor ourself */
1263 rum_setup_tx_desc(sc, desc, flags, 0, m0->m_pkthdr.len, rate);
1265 /* align end on a 4-bytes boundary */
1266 xferlen = (RT2573_TX_DESC_SIZE + m0->m_pkthdr.len + 3) & ~3;
1269 * No space left in the last URB to store the extra 4 bytes, force
1270 * sending of another URB.
1272 if ((xferlen % 64) == 0)
1275 DPRINTFN(10, ("sending raw frame len=%u rate=%u xfer len=%u\n",
1276 m0->m_pkthdr.len, rate, xferlen));
1278 usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf,
1279 xferlen, USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RUM_TX_TIMEOUT,
1282 error = usbd_transfer(data->xfer);
1283 if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS)
1292 rum_tx_data(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1294 struct ieee80211com *ic = &sc->sc_ic;
1295 struct rum_tx_desc *desc;
1296 struct rum_tx_data *data;
1297 struct ieee80211_frame *wh;
1298 struct ieee80211_key *k;
1304 wh = mtod(m0, struct ieee80211_frame *);
1306 if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE)
1307 rate = ic->ic_fixed_rate;
1309 rate = ni->ni_rates.rs_rates[ni->ni_txrate];
1311 rate &= IEEE80211_RATE_VAL;
1313 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1314 k = ieee80211_crypto_encap(ic, ni, m0);
1320 /* packet header may have moved, reset our local pointer */
1321 wh = mtod(m0, struct ieee80211_frame *);
1324 data = &sc->tx_data[0];
1325 desc = (struct rum_tx_desc *)data->buf;
1330 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1331 flags |= RT2573_TX_NEED_ACK;
1332 flags |= RT2573_TX_MORE_FRAG;
1334 dur = rum_txtime(RUM_ACK_SIZE, rum_ack_rate(ic, rate),
1335 ic->ic_flags) + sc->sifs;
1336 *(uint16_t *)wh->i_dur = htole16(dur);
1339 if (bpf_peers_present(sc->sc_drvbpf)) {
1340 struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
1343 tap->wt_rate = rate;
1344 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1345 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1346 tap->wt_antenna = sc->tx_ant;
1348 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
1351 m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RT2573_TX_DESC_SIZE);
1352 rum_setup_tx_desc(sc, desc, flags, 0, m0->m_pkthdr.len, rate);
1354 /* align end on a 4-bytes boundary */
1355 xferlen = (RT2573_TX_DESC_SIZE + m0->m_pkthdr.len + 3) & ~3;
1358 * No space left in the last URB to store the extra 4 bytes, force
1359 * sending of another URB.
1361 if ((xferlen % 64) == 0)
1364 DPRINTFN(10, ("sending frame len=%d rate=%d xfer len=%d\n",
1365 m0->m_pkthdr.len + (int)RT2573_TX_DESC_SIZE, rate, xferlen));
1367 usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf, xferlen,
1368 USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RUM_TX_TIMEOUT, rum_txeof);
1370 error = usbd_transfer(data->xfer);
1371 if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS) {
1384 rum_start(struct ifnet *ifp)
1386 struct rum_softc *sc = ifp->if_softc;
1387 struct ieee80211com *ic = &sc->sc_ic;
1388 struct ieee80211_node *ni;
1390 struct ether_header *eh;
1393 IF_POLL(&ic->ic_mgtq, m0);
1395 if (sc->tx_queued >= RUM_TX_LIST_COUNT) {
1396 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1399 IF_DEQUEUE(&ic->ic_mgtq, m0);
1401 ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
1402 m0->m_pkthdr.rcvif = NULL;
1404 if (bpf_peers_present(ic->ic_rawbpf))
1405 bpf_mtap(ic->ic_rawbpf, m0);
1407 if (rum_tx_mgt(sc, m0, ni) != 0) {
1408 ieee80211_free_node(ni);
1412 if (ic->ic_state != IEEE80211_S_RUN)
1414 IFQ_DRV_DEQUEUE(&ifp->if_snd, m0);
1417 if (sc->tx_queued >= RUM_TX_LIST_COUNT) {
1418 IFQ_DRV_PREPEND(&ifp->if_snd, m0);
1419 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1423 * Cancel any background scan.
1425 if (ic->ic_flags & IEEE80211_F_SCAN)
1426 ieee80211_cancel_scan(ic);
1428 if (m0->m_len < sizeof (struct ether_header) &&
1429 !(m0 = m_pullup(m0, sizeof (struct ether_header))))
1432 eh = mtod(m0, struct ether_header *);
1433 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1440 m0 = ieee80211_encap(ic, m0, ni);
1442 ieee80211_free_node(ni);
1446 if (bpf_peers_present(ic->ic_rawbpf))
1447 bpf_mtap(ic->ic_rawbpf, m0);
1449 if (rum_tx_data(sc, m0, ni) != 0) {
1450 ieee80211_free_node(ni);
1456 sc->sc_tx_timer = 5;
1457 ic->ic_lastdata = ticks;
1458 callout_reset(&sc->watchdog_ch, hz, rum_watchdog, sc);
1463 rum_watchdog(void *arg)
1465 struct rum_softc *sc = arg;
1469 if (sc->sc_tx_timer > 0) {
1470 if (--sc->sc_tx_timer == 0) {
1471 device_printf(sc->sc_dev, "device timeout\n");
1472 /*rum_init(ifp); XXX needs a process context! */
1473 sc->sc_ifp->if_oerrors++;
1477 callout_reset(&sc->watchdog_ch, hz, rum_watchdog, sc);
1484 rum_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1486 struct rum_softc *sc = ifp->if_softc;
1487 struct ieee80211com *ic = &sc->sc_ic;
1494 if (ifp->if_flags & IFF_UP) {
1495 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1496 rum_update_promisc(sc);
1500 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1505 error = ieee80211_ioctl(ic, cmd, data);
1508 if (error == ENETRESET) {
1509 if ((ifp->if_flags & IFF_UP) &&
1510 (ifp->if_drv_flags & IFF_DRV_RUNNING) &&
1511 (ic->ic_roaming != IEEE80211_ROAMING_MANUAL))
1522 rum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len)
1524 usb_device_request_t req;
1527 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1528 req.bRequest = RT2573_READ_EEPROM;
1529 USETW(req.wValue, 0);
1530 USETW(req.wIndex, addr);
1531 USETW(req.wLength, len);
1533 error = usbd_do_request(sc->sc_udev, &req, buf);
1535 printf("%s: could not read EEPROM: %s\n",
1536 device_get_nameunit(sc->sc_dev), usbd_errstr(error));
1541 rum_read(struct rum_softc *sc, uint16_t reg)
1545 rum_read_multi(sc, reg, &val, sizeof val);
1547 return le32toh(val);
1551 rum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len)
1553 usb_device_request_t req;
1556 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1557 req.bRequest = RT2573_READ_MULTI_MAC;
1558 USETW(req.wValue, 0);
1559 USETW(req.wIndex, reg);
1560 USETW(req.wLength, len);
1562 error = usbd_do_request(sc->sc_udev, &req, buf);
1564 printf("%s: could not multi read MAC register: %s\n",
1565 device_get_nameunit(sc->sc_dev), usbd_errstr(error));
1570 rum_write(struct rum_softc *sc, uint16_t reg, uint32_t val)
1572 uint32_t tmp = htole32(val);
1574 rum_write_multi(sc, reg, &tmp, sizeof tmp);
1578 rum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len)
1580 usb_device_request_t req;
1583 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1584 req.bRequest = RT2573_WRITE_MULTI_MAC;
1585 USETW(req.wValue, 0);
1586 USETW(req.wIndex, reg);
1587 USETW(req.wLength, len);
1589 error = usbd_do_request(sc->sc_udev, &req, buf);
1591 printf("%s: could not multi write MAC register: %s\n",
1592 device_get_nameunit(sc->sc_dev), usbd_errstr(error));
1597 rum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val)
1602 for (ntries = 0; ntries < 5; ntries++) {
1603 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1607 printf("%s: could not write to BBP\n",
1608 device_get_nameunit(sc->sc_dev));
1612 tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val;
1613 rum_write(sc, RT2573_PHY_CSR3, tmp);
1617 rum_bbp_read(struct rum_softc *sc, uint8_t reg)
1622 for (ntries = 0; ntries < 5; ntries++) {
1623 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1627 printf("%s: could not read BBP\n",
1628 device_get_nameunit(sc->sc_dev));
1632 val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8;
1633 rum_write(sc, RT2573_PHY_CSR3, val);
1635 for (ntries = 0; ntries < 100; ntries++) {
1636 val = rum_read(sc, RT2573_PHY_CSR3);
1637 if (!(val & RT2573_BBP_BUSY))
1642 printf("%s: could not read BBP\n", device_get_nameunit(sc->sc_dev));
1647 rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val)
1652 for (ntries = 0; ntries < 5; ntries++) {
1653 if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY))
1657 printf("%s: could not write to RF\n",
1658 device_get_nameunit(sc->sc_dev));
1662 tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 |
1664 rum_write(sc, RT2573_PHY_CSR4, tmp);
1666 /* remember last written value in sc */
1667 sc->rf_regs[reg] = val;
1669 DPRINTFN(15, ("RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff));
1673 rum_select_antenna(struct rum_softc *sc)
1675 uint8_t bbp4, bbp77;
1678 bbp4 = rum_bbp_read(sc, 4);
1679 bbp77 = rum_bbp_read(sc, 77);
1683 /* make sure Rx is disabled before switching antenna */
1684 tmp = rum_read(sc, RT2573_TXRX_CSR0);
1685 rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
1687 rum_bbp_write(sc, 4, bbp4);
1688 rum_bbp_write(sc, 77, bbp77);
1690 rum_write(sc, RT2573_TXRX_CSR0, tmp);
1694 * Enable multi-rate retries for frames sent at OFDM rates.
1695 * In 802.11b/g mode, allow fallback to CCK rates.
1698 rum_enable_mrr(struct rum_softc *sc)
1700 struct ieee80211com *ic = &sc->sc_ic;
1703 tmp = rum_read(sc, RT2573_TXRX_CSR4);
1705 tmp &= ~RT2573_MRR_CCK_FALLBACK;
1706 if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan))
1707 tmp |= RT2573_MRR_CCK_FALLBACK;
1708 tmp |= RT2573_MRR_ENABLED;
1710 rum_write(sc, RT2573_TXRX_CSR4, tmp);
1714 rum_set_txpreamble(struct rum_softc *sc)
1718 tmp = rum_read(sc, RT2573_TXRX_CSR4);
1720 tmp &= ~RT2573_SHORT_PREAMBLE;
1721 if (sc->sc_ic.ic_flags & IEEE80211_F_SHPREAMBLE)
1722 tmp |= RT2573_SHORT_PREAMBLE;
1724 rum_write(sc, RT2573_TXRX_CSR4, tmp);
1728 rum_set_basicrates(struct rum_softc *sc)
1730 struct ieee80211com *ic = &sc->sc_ic;
1732 /* update basic rate set */
1733 if (ic->ic_curmode == IEEE80211_MODE_11B) {
1734 /* 11b basic rates: 1, 2Mbps */
1735 rum_write(sc, RT2573_TXRX_CSR5, 0x3);
1736 } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bss->ni_chan)) {
1737 /* 11a basic rates: 6, 12, 24Mbps */
1738 rum_write(sc, RT2573_TXRX_CSR5, 0x150);
1740 /* 11b/g basic rates: 1, 2, 5.5, 11Mbps */
1741 rum_write(sc, RT2573_TXRX_CSR5, 0xf);
1746 * Reprogram MAC/BBP to switch to a new band. Values taken from the reference
1750 rum_select_band(struct rum_softc *sc, struct ieee80211_channel *c)
1752 uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104;
1755 /* update all BBP registers that depend on the band */
1756 bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c;
1757 bbp35 = 0x50; bbp97 = 0x48; bbp98 = 0x48;
1758 if (IEEE80211_IS_CHAN_5GHZ(c)) {
1759 bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c;
1760 bbp35 += 0x10; bbp97 += 0x10; bbp98 += 0x10;
1762 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1763 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1764 bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10;
1768 rum_bbp_write(sc, 17, bbp17);
1769 rum_bbp_write(sc, 96, bbp96);
1770 rum_bbp_write(sc, 104, bbp104);
1772 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1773 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1774 rum_bbp_write(sc, 75, 0x80);
1775 rum_bbp_write(sc, 86, 0x80);
1776 rum_bbp_write(sc, 88, 0x80);
1779 rum_bbp_write(sc, 35, bbp35);
1780 rum_bbp_write(sc, 97, bbp97);
1781 rum_bbp_write(sc, 98, bbp98);
1783 tmp = rum_read(sc, RT2573_PHY_CSR0);
1784 tmp &= ~(RT2573_PA_PE_2GHZ | RT2573_PA_PE_5GHZ);
1785 if (IEEE80211_IS_CHAN_2GHZ(c))
1786 tmp |= RT2573_PA_PE_2GHZ;
1788 tmp |= RT2573_PA_PE_5GHZ;
1789 rum_write(sc, RT2573_PHY_CSR0, tmp);
1791 /* 802.11a uses a 16 microseconds short interframe space */
1792 sc->sifs = IEEE80211_IS_CHAN_5GHZ(c) ? 16 : 10;
1796 rum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c)
1798 struct ieee80211com *ic = &sc->sc_ic;
1799 const struct rfprog *rfprog;
1800 uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT;
1804 chan = ieee80211_chan2ieee(ic, c);
1805 if (chan == 0 || chan == IEEE80211_CHAN_ANY)
1808 /* select the appropriate RF settings based on what EEPROM says */
1809 rfprog = (sc->rf_rev == RT2573_RF_5225 ||
1810 sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226;
1812 /* find the settings for this channel (we know it exists) */
1813 for (i = 0; rfprog[i].chan != chan; i++);
1815 power = sc->txpow[i];
1819 } else if (power > 31) {
1820 bbp94 += power - 31;
1825 * If we are switching from the 2GHz band to the 5GHz band or
1826 * vice-versa, BBP registers need to be reprogrammed.
1828 if (c->ic_flags != ic->ic_curchan->ic_flags) {
1829 rum_select_band(sc, c);
1830 rum_select_antenna(sc);
1834 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1835 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1836 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1837 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1839 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1840 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1841 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7 | 1);
1842 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1844 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1845 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1846 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1847 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1851 /* enable smart mode for MIMO-capable RFs */
1852 bbp3 = rum_bbp_read(sc, 3);
1854 bbp3 &= ~RT2573_SMART_MODE;
1855 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527)
1856 bbp3 |= RT2573_SMART_MODE;
1858 rum_bbp_write(sc, 3, bbp3);
1860 if (bbp94 != RT2573_BBPR94_DEFAULT)
1861 rum_bbp_write(sc, 94, bbp94);
1865 * Enable TSF synchronization and tell h/w to start sending beacons for IBSS
1866 * and HostAP operating modes.
1869 rum_enable_tsf_sync(struct rum_softc *sc)
1871 struct ieee80211com *ic = &sc->sc_ic;
1874 if (ic->ic_opmode != IEEE80211_M_STA) {
1876 * Change default 16ms TBTT adjustment to 8ms.
1877 * Must be done before enabling beacon generation.
1879 rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8);
1882 tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000;
1884 /* set beacon interval (in 1/16ms unit) */
1885 tmp |= ic->ic_bss->ni_intval * 16;
1887 tmp |= RT2573_TSF_TICKING | RT2573_ENABLE_TBTT;
1888 if (ic->ic_opmode == IEEE80211_M_STA)
1889 tmp |= RT2573_TSF_MODE(1);
1891 tmp |= RT2573_TSF_MODE(2) | RT2573_GENERATE_BEACON;
1893 rum_write(sc, RT2573_TXRX_CSR9, tmp);
1897 rum_update_slot(struct ifnet *ifp)
1899 struct rum_softc *sc = ifp->if_softc;
1900 struct ieee80211com *ic = &sc->sc_ic;
1904 slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
1906 tmp = rum_read(sc, RT2573_MAC_CSR9);
1907 tmp = (tmp & ~0xff) | slottime;
1908 rum_write(sc, RT2573_MAC_CSR9, tmp);
1910 DPRINTF(("setting slot time to %uus\n", slottime));
1914 rum_set_bssid(struct rum_softc *sc, const uint8_t *bssid)
1918 tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24;
1919 rum_write(sc, RT2573_MAC_CSR4, tmp);
1921 tmp = bssid[4] | bssid[5] << 8 | RT2573_ONE_BSSID << 16;
1922 rum_write(sc, RT2573_MAC_CSR5, tmp);
1926 rum_set_macaddr(struct rum_softc *sc, const uint8_t *addr)
1930 tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24;
1931 rum_write(sc, RT2573_MAC_CSR2, tmp);
1933 tmp = addr[4] | addr[5] << 8 | 0xff << 16;
1934 rum_write(sc, RT2573_MAC_CSR3, tmp);
1938 rum_update_promisc(struct rum_softc *sc)
1940 struct ifnet *ifp = sc->sc_ic.ic_ifp;
1943 tmp = rum_read(sc, RT2573_TXRX_CSR0);
1945 tmp &= ~RT2573_DROP_NOT_TO_ME;
1946 if (!(ifp->if_flags & IFF_PROMISC))
1947 tmp |= RT2573_DROP_NOT_TO_ME;
1949 rum_write(sc, RT2573_TXRX_CSR0, tmp);
1951 DPRINTF(("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
1952 "entering" : "leaving"));
1959 case RT2573_RF_2527: return "RT2527 (MIMO XR)";
1960 case RT2573_RF_2528: return "RT2528";
1961 case RT2573_RF_5225: return "RT5225 (MIMO XR)";
1962 case RT2573_RF_5226: return "RT5226";
1963 default: return "unknown";
1968 rum_read_eeprom(struct rum_softc *sc)
1970 struct ieee80211com *ic = &sc->sc_ic;
1976 /* read MAC address */
1977 rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, ic->ic_myaddr, 6);
1979 rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2);
1981 sc->rf_rev = (val >> 11) & 0x1f;
1982 sc->hw_radio = (val >> 10) & 0x1;
1983 sc->rx_ant = (val >> 4) & 0x3;
1984 sc->tx_ant = (val >> 2) & 0x3;
1985 sc->nb_ant = val & 0x3;
1987 DPRINTF(("RF revision=%d\n", sc->rf_rev));
1989 rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2);
1991 sc->ext_5ghz_lna = (val >> 6) & 0x1;
1992 sc->ext_2ghz_lna = (val >> 4) & 0x1;
1994 DPRINTF(("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n",
1995 sc->ext_2ghz_lna, sc->ext_5ghz_lna));
1997 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2);
1999 if ((val & 0xff) != 0xff)
2000 sc->rssi_2ghz_corr = (int8_t)(val & 0xff); /* signed */
2002 /* Only [-10, 10] is valid */
2003 if (sc->rssi_2ghz_corr < -10 || sc->rssi_2ghz_corr > 10)
2004 sc->rssi_2ghz_corr = 0;
2006 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2);
2008 if ((val & 0xff) != 0xff)
2009 sc->rssi_5ghz_corr = (int8_t)(val & 0xff); /* signed */
2011 /* Only [-10, 10] is valid */
2012 if (sc->rssi_5ghz_corr < -10 || sc->rssi_5ghz_corr > 10)
2013 sc->rssi_5ghz_corr = 0;
2015 if (sc->ext_2ghz_lna)
2016 sc->rssi_2ghz_corr -= 14;
2017 if (sc->ext_5ghz_lna)
2018 sc->rssi_5ghz_corr -= 14;
2020 DPRINTF(("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n",
2021 sc->rssi_2ghz_corr, sc->rssi_5ghz_corr));
2023 rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2);
2025 if ((val & 0xff) != 0xff)
2026 sc->rffreq = val & 0xff;
2028 DPRINTF(("RF freq=%d\n", sc->rffreq));
2030 /* read Tx power for all a/b/g channels */
2031 rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14);
2032 /* XXX default Tx power for 802.11a channels */
2033 memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14);
2035 for (i = 0; i < 14; i++)
2036 DPRINTF(("Channel=%d Tx power=%d\n", i + 1, sc->txpow[i]));
2039 /* read default values for BBP registers */
2040 rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
2042 for (i = 0; i < 14; i++) {
2043 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
2045 DPRINTF(("BBP R%d=%02x\n", sc->bbp_prom[i].reg,
2046 sc->bbp_prom[i].val));
2052 rum_bbp_init(struct rum_softc *sc)
2054 #define N(a) (sizeof (a) / sizeof ((a)[0]))
2057 /* wait for BBP to be ready */
2058 for (ntries = 0; ntries < 100; ntries++) {
2059 const uint8_t val = rum_bbp_read(sc, 0);
2060 if (val != 0 && val != 0xff)
2064 if (ntries == 100) {
2065 device_printf(sc->sc_dev, "timeout waiting for BBP\n");
2069 /* initialize BBP registers to default values */
2070 for (i = 0; i < N(rum_def_bbp); i++)
2071 rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val);
2073 /* write vendor-specific BBP values (from EEPROM) */
2074 for (i = 0; i < 16; i++) {
2075 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
2077 rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
2085 rum_init(void *priv)
2087 #define N(a) (sizeof (a) / sizeof ((a)[0]))
2088 struct rum_softc *sc = priv;
2089 struct ieee80211com *ic = &sc->sc_ic;
2090 struct ifnet *ifp = ic->ic_ifp;
2091 struct rum_rx_data *data;
2098 /* initialize MAC registers to default values */
2099 for (i = 0; i < N(rum_def_mac); i++)
2100 rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val);
2102 /* set host ready */
2103 rum_write(sc, RT2573_MAC_CSR1, 3);
2104 rum_write(sc, RT2573_MAC_CSR1, 0);
2106 /* wait for BBP/RF to wakeup */
2107 for (ntries = 0; ntries < 1000; ntries++) {
2108 if (rum_read(sc, RT2573_MAC_CSR12) & 8)
2110 rum_write(sc, RT2573_MAC_CSR12, 4); /* force wakeup */
2113 if (ntries == 1000) {
2114 printf("%s: timeout waiting for BBP/RF to wakeup\n",
2115 device_get_nameunit(sc->sc_dev));
2119 if ((error = rum_bbp_init(sc)) != 0)
2122 /* select default channel */
2123 rum_select_band(sc, ic->ic_curchan);
2124 rum_select_antenna(sc);
2125 rum_set_chan(sc, ic->ic_curchan);
2127 /* clear STA registers */
2128 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
2130 IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
2131 rum_set_macaddr(sc, ic->ic_myaddr);
2133 /* initialize ASIC */
2134 rum_write(sc, RT2573_MAC_CSR1, 4);
2137 * Allocate xfer for AMRR statistics requests.
2139 sc->amrr_xfer = usbd_alloc_xfer(sc->sc_udev);
2140 if (sc->amrr_xfer == NULL) {
2141 printf("%s: could not allocate AMRR xfer\n",
2142 device_get_nameunit(sc->sc_dev));
2147 * Open Tx and Rx USB bulk pipes.
2149 error = usbd_open_pipe(sc->sc_iface, sc->sc_tx_no, USBD_EXCLUSIVE_USE,
2152 printf("%s: could not open Tx pipe: %s\n",
2153 device_get_nameunit(sc->sc_dev), usbd_errstr(error));
2156 error = usbd_open_pipe(sc->sc_iface, sc->sc_rx_no, USBD_EXCLUSIVE_USE,
2159 printf("%s: could not open Rx pipe: %s\n",
2160 device_get_nameunit(sc->sc_dev), usbd_errstr(error));
2165 * Allocate Tx and Rx xfer queues.
2167 error = rum_alloc_tx_list(sc);
2169 printf("%s: could not allocate Tx list\n",
2170 device_get_nameunit(sc->sc_dev));
2173 error = rum_alloc_rx_list(sc);
2175 printf("%s: could not allocate Rx list\n",
2176 device_get_nameunit(sc->sc_dev));
2181 * Start up the receive pipe.
2183 for (i = 0; i < RUM_RX_LIST_COUNT; i++) {
2184 data = &sc->rx_data[i];
2186 usbd_setup_xfer(data->xfer, sc->sc_rx_pipeh, data, data->buf,
2187 MCLBYTES, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rum_rxeof);
2188 usbd_transfer(data->xfer);
2191 /* update Rx filter */
2192 tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff;
2194 tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR;
2195 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2196 tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR |
2198 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2199 tmp |= RT2573_DROP_TODS;
2200 if (!(ifp->if_flags & IFF_PROMISC))
2201 tmp |= RT2573_DROP_NOT_TO_ME;
2203 rum_write(sc, RT2573_TXRX_CSR0, tmp);
2205 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2206 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2208 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2209 if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
2210 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2212 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2221 rum_stop(void *priv)
2223 struct rum_softc *sc = priv;
2224 struct ieee80211com *ic = &sc->sc_ic;
2225 struct ifnet *ifp = ic->ic_ifp;
2228 sc->sc_tx_timer = 0;
2229 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2231 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2234 tmp = rum_read(sc, RT2573_TXRX_CSR0);
2235 rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
2238 rum_write(sc, RT2573_MAC_CSR1, 3);
2239 rum_write(sc, RT2573_MAC_CSR1, 0);
2241 if (sc->amrr_xfer != NULL) {
2242 usbd_free_xfer(sc->amrr_xfer);
2243 sc->amrr_xfer = NULL;
2246 if (sc->sc_rx_pipeh != NULL) {
2247 usbd_abort_pipe(sc->sc_rx_pipeh);
2248 usbd_close_pipe(sc->sc_rx_pipeh);
2249 sc->sc_rx_pipeh = NULL;
2251 if (sc->sc_tx_pipeh != NULL) {
2252 usbd_abort_pipe(sc->sc_tx_pipeh);
2253 usbd_close_pipe(sc->sc_tx_pipeh);
2254 sc->sc_tx_pipeh = NULL;
2257 rum_free_rx_list(sc);
2258 rum_free_tx_list(sc);
2262 rum_load_microcode(struct rum_softc *sc, const u_char *ucode, size_t size)
2264 usb_device_request_t req;
2265 uint16_t reg = RT2573_MCU_CODE_BASE;
2268 /* copy firmware image into NIC */
2269 for (; size >= 4; reg += 4, ucode += 4, size -= 4)
2270 rum_write(sc, reg, UGETDW(ucode));
2272 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2273 req.bRequest = RT2573_MCU_CNTL;
2274 USETW(req.wValue, RT2573_MCU_RUN);
2275 USETW(req.wIndex, 0);
2276 USETW(req.wLength, 0);
2278 error = usbd_do_request(sc->sc_udev, &req, NULL);
2280 printf("%s: could not run firmware: %s\n",
2281 device_get_nameunit(sc->sc_dev), usbd_errstr(error));
2287 rum_prepare_beacon(struct rum_softc *sc)
2289 struct ieee80211com *ic = &sc->sc_ic;
2290 struct rum_tx_desc desc;
2294 m0 = ieee80211_beacon_alloc(ic->ic_bss, &sc->sc_bo);
2299 /* send beacons at the lowest available rate */
2300 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
2302 rum_setup_tx_desc(sc, &desc, RT2573_TX_TIMESTAMP, RT2573_TX_HWSEQ,
2303 m0->m_pkthdr.len, rate);
2305 /* copy the first 24 bytes of Tx descriptor into NIC memory */
2306 rum_write_multi(sc, RT2573_HW_BEACON_BASE0, (uint8_t *)&desc, 24);
2308 /* copy beacon header and payload into NIC memory */
2309 rum_write_multi(sc, RT2573_HW_BEACON_BASE0 + 24, mtod(m0, uint8_t *),
2318 rum_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2319 const struct ieee80211_bpf_params *params)
2321 struct ieee80211com *ic = ni->ni_ic;
2322 struct ifnet *ifp = ic->ic_ifp;
2323 struct rum_softc *sc = ifp->if_softc;
2325 /* prevent management frames from being sent if we're not ready */
2326 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2328 ieee80211_free_node(ni);
2331 if (sc->tx_queued >= RUM_TX_LIST_COUNT) {
2332 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2334 ieee80211_free_node(ni);
2338 if (bpf_peers_present(ic->ic_rawbpf))
2339 bpf_mtap(ic->ic_rawbpf, m);
2343 if (params == NULL) {
2345 * Legacy path; interpret frame contents to decide
2346 * precisely how to send the frame.
2348 if (rum_tx_mgt(sc, m, ni) != 0)
2352 * Caller supplied explicit parameters to use in
2353 * sending the frame.
2355 if (rum_tx_raw(sc, m, ni, params) != 0)
2358 sc->sc_tx_timer = 5;
2359 callout_reset(&sc->watchdog_ch, hz, rum_watchdog, sc);
2364 ieee80211_free_node(ni);
2369 rum_amrr_start(struct rum_softc *sc, struct ieee80211_node *ni)
2373 /* clear statistic registers (STA_CSR0 to STA_CSR5) */
2374 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
2376 ieee80211_amrr_node_init(&sc->amrr, &sc->amn);
2378 /* set rate to some reasonable initial value */
2379 for (i = ni->ni_rates.rs_nrates - 1;
2380 i > 0 && (ni->ni_rates.rs_rates[i] & IEEE80211_RATE_VAL) > 72;
2384 callout_reset(&sc->amrr_ch, hz, rum_amrr_timeout, sc);
2388 rum_amrr_timeout(void *arg)
2390 struct rum_softc *sc = (struct rum_softc *)arg;
2391 usb_device_request_t req;
2394 * Asynchronously read statistic registers (cleared by read).
2396 req.bmRequestType = UT_READ_VENDOR_DEVICE;
2397 req.bRequest = RT2573_READ_MULTI_MAC;
2398 USETW(req.wValue, 0);
2399 USETW(req.wIndex, RT2573_STA_CSR0);
2400 USETW(req.wLength, sizeof sc->sta);
2402 usbd_setup_default_xfer(sc->amrr_xfer, sc->sc_udev, sc,
2403 USBD_DEFAULT_TIMEOUT, &req, sc->sta, sizeof sc->sta, 0,
2405 (void)usbd_transfer(sc->amrr_xfer);
2409 rum_amrr_update(usbd_xfer_handle xfer, usbd_private_handle priv,
2412 struct rum_softc *sc = (struct rum_softc *)priv;
2413 struct ifnet *ifp = sc->sc_ic.ic_ifp;
2415 if (status != USBD_NORMAL_COMPLETION) {
2416 device_printf(sc->sc_dev, "could not retrieve Tx statistics - "
2417 "cancelling automatic rate control\n");
2421 /* count TX retry-fail as Tx errors */
2422 ifp->if_oerrors += le32toh(sc->sta[5]) >> 16;
2424 sc->amn.amn_retrycnt =
2425 (le32toh(sc->sta[4]) >> 16) + /* TX one-retry ok count */
2426 (le32toh(sc->sta[5]) & 0xffff) + /* TX more-retry ok count */
2427 (le32toh(sc->sta[5]) >> 16); /* TX retry-fail count */
2430 sc->amn.amn_retrycnt +
2431 (le32toh(sc->sta[4]) & 0xffff); /* TX no-retry ok count */
2433 ieee80211_amrr_choose(&sc->amrr, sc->sc_ic.ic_bss, &sc->amn);
2435 callout_reset(&sc->amrr_ch, hz, rum_amrr_timeout, sc);
2439 rum_scan_start(struct ieee80211com *ic)
2441 struct rum_softc *sc = ic->ic_ifp->if_softc;
2443 usb_rem_task(sc->sc_udev, &sc->sc_scantask);
2445 /* do it in a process context */
2446 sc->sc_scan_action = RUM_SCAN_START;
2447 usb_add_task(sc->sc_udev, &sc->sc_scantask, USB_TASKQ_DRIVER);
2451 rum_scan_end(struct ieee80211com *ic)
2453 struct rum_softc *sc = ic->ic_ifp->if_softc;
2455 usb_rem_task(sc->sc_udev, &sc->sc_scantask);
2457 /* do it in a process context */
2458 sc->sc_scan_action = RUM_SCAN_END;
2459 usb_add_task(sc->sc_udev, &sc->sc_scantask, USB_TASKQ_DRIVER);
2463 rum_set_channel(struct ieee80211com *ic)
2465 struct rum_softc *sc = ic->ic_ifp->if_softc;
2467 usb_rem_task(sc->sc_udev, &sc->sc_scantask);
2469 /* do it in a process context */
2470 sc->sc_scan_action = RUM_SET_CHANNEL;
2471 usb_add_task(sc->sc_udev, &sc->sc_scantask, USB_TASKQ_DRIVER);
2475 rum_scantask(void *arg)
2477 struct rum_softc *sc = arg;
2478 struct ieee80211com *ic = &sc->sc_ic;
2479 struct ifnet *ifp = ic->ic_ifp;
2484 switch (sc->sc_scan_action) {
2485 case RUM_SCAN_START:
2486 /* abort TSF synchronization */
2487 tmp = rum_read(sc, RT2573_TXRX_CSR9);
2488 rum_write(sc, RT2573_TXRX_CSR9, tmp & ~0x00ffffff);
2489 rum_set_bssid(sc, ifp->if_broadcastaddr);
2493 rum_enable_tsf_sync(sc);
2494 /* XXX keep local copy */
2495 rum_set_bssid(sc, ic->ic_bss->ni_bssid);
2498 case RUM_SET_CHANNEL:
2500 rum_set_chan(sc, ic->ic_curchan);
2505 panic("unknown scan action %d\n", sc->sc_scan_action);
2514 rum_get_rssi(struct rum_softc *sc, uint8_t raw)
2518 lna = (raw >> 5) & 0x3;
2525 * NB: Since RSSI is relative to noise floor, -1 is
2526 * adequate for caller to know error happened.
2531 rssi = (2 * agc) - RT2573_NOISE_FLOOR;
2533 if (IEEE80211_IS_CHAN_2GHZ(sc->sc_ic.ic_curchan)) {
2534 rssi += sc->rssi_2ghz_corr;
2543 rssi += sc->rssi_5ghz_corr;
2545 if (!sc->ext_5ghz_lna && lna != 1)
2558 static device_method_t rum_methods[] = {
2559 /* Device interface */
2560 DEVMETHOD(device_probe, rum_match),
2561 DEVMETHOD(device_attach, rum_attach),
2562 DEVMETHOD(device_detach, rum_detach),
2567 static driver_t rum_driver = {
2570 sizeof(struct rum_softc)
2573 static devclass_t rum_devclass;
2575 DRIVER_MODULE(rum, uhub, rum_driver, rum_devclass, usbd_driver_load, 0);