Nuke SIMPLEQ_* and logprintf.
[dragonfly/vkernel-mp.git] / sys / dev / netif / rum / if_rum.c
blob7481c95c92a136db952fe2176e5a93da4a76ef29
1 /* $OpenBSD: if_rum.c,v 1.40 2006/09/18 16:20:20 damien Exp $ */
2 /* $DragonFly: src/sys/dev/netif/rum/if_rum.c,v 1.15 2007/06/28 13:55:12 hasso Exp $ */
4 /*-
5 * Copyright (c) 2005, 2006 Damien Bergamini <damien.bergamini@free.fr>
6 * Copyright (c) 2006 Niall O'Higgins <niallo@openbsd.org>
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 /*-
22 * Ralink Technology RT2501USB/RT2601USB chipset driver
23 * http://www.ralinktech.com/
26 #include <sys/param.h>
27 #include <sys/bus.h>
28 #include <sys/endian.h>
29 #include <sys/kernel.h>
30 #include <sys/malloc.h>
31 #include <sys/mbuf.h>
32 #include <sys/rman.h>
33 #include <sys/serialize.h>
34 #include <sys/socket.h>
35 #include <sys/sockio.h>
37 #include <net/bpf.h>
38 #include <net/ethernet.h>
39 #include <net/if.h>
40 #include <net/if_arp.h>
41 #include <net/if_dl.h>
42 #include <net/if_media.h>
43 #include <net/ifq_var.h>
45 #include <netproto/802_11/ieee80211_var.h>
46 #include <netproto/802_11/ieee80211_radiotap.h>
47 #include <netproto/802_11/wlan_ratectl/onoe/ieee80211_onoe_param.h>
49 #include <bus/usb/usb.h>
50 #include <bus/usb/usbdi.h>
51 #include <bus/usb/usbdi_util.h>
52 #include <bus/usb/usbdevs.h>
54 #include "if_rumreg.h"
55 #include "if_rumvar.h"
56 #include "rum_ucode.h"
58 #ifdef USB_DEBUG
59 #define RUM_DEBUG
60 #endif
62 #ifdef RUM_DEBUG
63 #define DPRINTF(x) do { if (rum_debug) kprintf x; } while (0)
64 #define DPRINTFN(n, x) do { if (rum_debug >= (n)) kprintf x; } while (0)
65 int rum_debug = 0;
66 #else
67 #define DPRINTF(x)
68 #define DPRINTFN(n, x)
69 #endif
71 /* various supported device vendors/products */
72 static const struct usb_devno rum_devs[] = {
73 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_HWU54DM },
74 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_RT2573 },
75 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_RT2573_1 },
76 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_RT2573_2 },
77 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_WUG2700 },
78 { USB_VENDOR_AMIT, USB_PRODUCT_AMIT_CGWLUSB2GO },
79 { USB_VENDOR_ASUS, USB_PRODUCT_ASUS_WL167G_2 },
80 { USB_VENDOR_ASUS, USB_PRODUCT_ASUS_WL167G_3 },
81 { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D7050A },
82 { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D9050V3 },
83 { USB_VENDOR_LINKSYS4, USB_PRODUCT_LINKSYS4_WUSB54GC },
84 { USB_VENDOR_LINKSYS4, USB_PRODUCT_LINKSYS4_WUSB54GR },
85 { USB_VENDOR_CONCEPTRONIC, USB_PRODUCT_CONCEPTRONIC_C54RU2 },
86 { USB_VENDOR_DICKSMITH, USB_PRODUCT_DICKSMITH_CWD854F },
87 { USB_VENDOR_DICKSMITH, USB_PRODUCT_DICKSMITH_RT2573 },
88 { USB_VENDOR_DLINK2, USB_PRODUCT_DLINK2_DWLG122C1 },
89 { USB_VENDOR_DLINK2, USB_PRODUCT_DLINK2_WUA1340 },
90 { USB_VENDOR_GIGABYTE, USB_PRODUCT_GIGABYTE_GNWB01GS },
91 { USB_VENDOR_GIGABYTE, USB_PRODUCT_GIGABYTE_GNWI05GS },
92 { USB_VENDOR_GIGASET, USB_PRODUCT_GIGASET_RT2573 },
93 { USB_VENDOR_GOODWAY, USB_PRODUCT_GOODWAY_RT2573 },
94 { USB_VENDOR_GUILLEMOT, USB_PRODUCT_GUILLEMOT_HWGUSB254LB },
95 { USB_VENDOR_GUILLEMOT, USB_PRODUCT_GUILLEMOT_HWGUSB254V2AP },
96 { USB_VENDOR_HUAWEI3COM, USB_PRODUCT_HUAWEI3COM_RT2573 },
97 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_G54HP },
98 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_SG54HP },
99 { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2573 },
100 { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2573_1 },
101 { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2573_2 },
102 { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2573_3 },
103 { USB_VENDOR_NOVATECH, USB_PRODUCT_NOVATECH_RT2573 },
104 { USB_VENDOR_PLANEX2, USB_PRODUCT_PLANEX2_GWUS54HP },
105 { USB_VENDOR_PLANEX2, USB_PRODUCT_PLANEX2_GWUS54MINI2 },
106 { USB_VENDOR_PLANEX2, USB_PRODUCT_PLANEX2_GWUSMM },
107 { USB_VENDOR_QCOM, USB_PRODUCT_QCOM_RT2573 },
108 { USB_VENDOR_QCOM, USB_PRODUCT_QCOM_RT2573_2 },
109 { USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2573 },
110 { USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2671 },
111 { USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2573_2 },
112 { USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_WL113R2 },
113 { USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_WL172 },
114 { USB_VENDOR_SURECOM, USB_PRODUCT_SURECOM_RT2573 }
117 static int rum_alloc_tx_list(struct rum_softc *);
118 static void rum_free_tx_list(struct rum_softc *);
119 static int rum_alloc_rx_list(struct rum_softc *);
120 static void rum_free_rx_list(struct rum_softc *);
121 static int rum_media_change(struct ifnet *);
122 static void rum_next_scan(void *);
123 static void rum_task(void *);
124 static int rum_newstate(struct ieee80211com *,
125 enum ieee80211_state, int);
126 static void rum_txeof(usbd_xfer_handle, usbd_private_handle,
127 usbd_status);
128 static void rum_rxeof(usbd_xfer_handle, usbd_private_handle,
129 usbd_status);
130 static uint8_t rum_rxrate(struct rum_rx_desc *);
131 static uint8_t rum_plcp_signal(int);
132 static void rum_setup_tx_desc(struct rum_softc *,
133 struct rum_tx_desc *, uint32_t, uint16_t, int,
134 int);
135 static int rum_tx_data(struct rum_softc *, struct mbuf *,
136 struct ieee80211_node *);
137 static void rum_start(struct ifnet *);
138 static void rum_watchdog(struct ifnet *);
139 static int rum_ioctl(struct ifnet *, u_long, caddr_t,
140 struct ucred *);
141 static void rum_eeprom_read(struct rum_softc *, uint16_t, void *,
142 int);
143 static uint32_t rum_read(struct rum_softc *, uint16_t);
144 static void rum_read_multi(struct rum_softc *, uint16_t, void *,
145 int);
146 static void rum_write(struct rum_softc *, uint16_t, uint32_t);
147 static void rum_write_multi(struct rum_softc *, uint16_t, void *,
148 size_t);
149 static void rum_bbp_write(struct rum_softc *, uint8_t, uint8_t);
150 static uint8_t rum_bbp_read(struct rum_softc *, uint8_t);
151 static void rum_rf_write(struct rum_softc *, uint8_t, uint32_t);
152 static void rum_select_antenna(struct rum_softc *);
153 static void rum_enable_mrr(struct rum_softc *);
154 static void rum_set_txpreamble(struct rum_softc *);
155 static void rum_set_basicrates(struct rum_softc *);
156 static void rum_select_band(struct rum_softc *,
157 struct ieee80211_channel *);
158 static void rum_set_chan(struct rum_softc *,
159 struct ieee80211_channel *);
160 static void rum_enable_tsf_sync(struct rum_softc *);
161 static void rum_update_slot(struct rum_softc *);
162 static void rum_set_bssid(struct rum_softc *, const uint8_t *);
163 static void rum_set_macaddr(struct rum_softc *, const uint8_t *);
164 static void rum_update_promisc(struct rum_softc *);
165 static const char *rum_get_rf(int);
166 static void rum_read_eeprom(struct rum_softc *);
167 static int rum_bbp_init(struct rum_softc *);
168 static void rum_init(void *);
169 static void rum_stop(struct rum_softc *);
170 static int rum_load_microcode(struct rum_softc *, const uint8_t *,
171 size_t);
172 static int rum_prepare_beacon(struct rum_softc *);
174 static void rum_stats_timeout(void *);
175 static void rum_stats_update(usbd_xfer_handle, usbd_private_handle,
176 usbd_status);
177 static void rum_stats(struct ieee80211com *,
178 struct ieee80211_node *,
179 struct ieee80211_ratectl_stats *);
180 static void rum_ratectl_change(struct ieee80211com *ic, u_int,
181 u_int);
182 static int rum_get_rssi(struct rum_softc *, uint8_t);
185 * Supported rates for 802.11a/b/g modes (in 500Kbps unit).
187 static const struct ieee80211_rateset rum_rateset_11a =
188 { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
190 static const struct ieee80211_rateset rum_rateset_11b =
191 { 4, { 2, 4, 11, 22 } };
193 static const struct ieee80211_rateset rum_rateset_11g =
194 { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
196 static const struct {
197 uint32_t reg;
198 uint32_t val;
199 } rum_def_mac[] = {
200 RT2573_DEF_MAC
203 static const struct {
204 uint8_t reg;
205 uint8_t val;
206 } rum_def_bbp[] = {
207 RT2573_DEF_BBP
210 static const struct rfprog {
211 uint8_t chan;
212 uint32_t r1, r2, r3, r4;
213 } rum_rf5226[] = {
214 RT2573_RF5226
215 }, rum_rf5225[] = {
216 RT2573_RF5225
219 USB_DECLARE_DRIVER(rum);
220 DRIVER_MODULE(rum, uhub, rum_driver, rum_devclass, usbd_driver_load, 0);
222 USB_MATCH(rum)
224 USB_MATCH_START(rum, uaa);
226 if (uaa->iface != NULL)
227 return UMATCH_NONE;
229 return (usb_lookup(rum_devs, uaa->vendor, uaa->product) != NULL) ?
230 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
233 USB_ATTACH(rum)
235 USB_ATTACH_START(rum, sc, uaa);
236 struct ieee80211com *ic = &sc->sc_ic;
237 struct ifnet *ifp = &ic->ic_if;
238 usb_interface_descriptor_t *id;
239 usb_endpoint_descriptor_t *ed;
240 usbd_status error;
241 char devinfo[1024];
242 int i, ntries;
243 uint32_t tmp;
245 sc->sc_udev = uaa->device;
247 usbd_devinfo(uaa->device, 0, devinfo);
248 USB_ATTACH_SETUP;
250 if (usbd_set_config_no(sc->sc_udev, RT2573_CONFIG_NO, 0) != 0) {
251 kprintf("%s: could not set configuration no\n",
252 device_get_nameunit(sc->sc_dev));
253 USB_ATTACH_ERROR_RETURN;
256 /* get the first interface handle */
257 error = usbd_device2interface_handle(sc->sc_udev, RT2573_IFACE_INDEX,
258 &sc->sc_iface);
259 if (error != 0) {
260 kprintf("%s: could not get interface handle\n",
261 device_get_nameunit(sc->sc_dev));
262 USB_ATTACH_ERROR_RETURN;
266 * Find endpoints.
268 id = usbd_get_interface_descriptor(sc->sc_iface);
270 sc->sc_rx_no = sc->sc_tx_no = -1;
271 for (i = 0; i < id->bNumEndpoints; i++) {
272 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
273 if (ed == NULL) {
274 kprintf("%s: no endpoint descriptor for iface %d\n",
275 device_get_nameunit(sc->sc_dev), i);
276 USB_ATTACH_ERROR_RETURN;
279 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
280 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
281 sc->sc_rx_no = ed->bEndpointAddress;
282 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
283 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
284 sc->sc_tx_no = ed->bEndpointAddress;
286 if (sc->sc_rx_no == -1 || sc->sc_tx_no == -1) {
287 kprintf("%s: missing endpoint\n", device_get_nameunit(sc->sc_dev));
288 USB_ATTACH_ERROR_RETURN;
291 usb_init_task(&sc->sc_task, rum_task, sc);
293 callout_init(&sc->scan_ch);
294 callout_init(&sc->stats_ch);
296 /* retrieve RT2573 rev. no */
297 for (ntries = 0; ntries < 1000; ntries++) {
298 if ((tmp = rum_read(sc, RT2573_MAC_CSR0)) != 0)
299 break;
300 DELAY(1000);
302 if (ntries == 1000) {
303 kprintf("%s: timeout waiting for chip to settle\n",
304 device_get_nameunit(sc->sc_dev));
305 USB_ATTACH_ERROR_RETURN;
308 /* retrieve MAC address and various other things from EEPROM */
309 rum_read_eeprom(sc);
311 kprintf("%s: MAC/BBP RT%04x (rev 0x%05x), RF %s, address %6D\n",
312 device_get_nameunit(sc->sc_dev), sc->macbbp_rev, tmp,
313 rum_get_rf(sc->rf_rev), ic->ic_myaddr, ":");
315 error = rum_load_microcode(sc, rt2573, sizeof(rt2573));
316 if (error != 0) {
317 device_printf(self, "can't load microcode\n");
318 USB_ATTACH_ERROR_RETURN;
321 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
322 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
323 ic->ic_state = IEEE80211_S_INIT;
325 /* set device capabilities */
326 ic->ic_caps =
327 IEEE80211_C_IBSS | /* IBSS mode supported */
328 IEEE80211_C_MONITOR | /* monitor mode supported */
329 IEEE80211_C_HOSTAP | /* HostAp mode supported */
330 IEEE80211_C_TXPMGT | /* tx power management */
331 IEEE80211_C_SHPREAMBLE | /* short preamble supported */
332 IEEE80211_C_SHSLOT | /* short slot time supported */
333 IEEE80211_C_WPA; /* WPA 1+2 */
335 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_5226) {
336 /* set supported .11a rates */
337 ic->ic_sup_rates[IEEE80211_MODE_11A] = rum_rateset_11a;
339 /* set supported .11a channels */
340 for (i = 34; i <= 46; i += 4) {
341 ic->ic_channels[i].ic_freq =
342 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
343 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
345 for (i = 36; i <= 64; i += 4) {
346 ic->ic_channels[i].ic_freq =
347 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
348 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
350 for (i = 100; i <= 140; i += 4) {
351 ic->ic_channels[i].ic_freq =
352 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
353 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
355 for (i = 149; i <= 165; i += 4) {
356 ic->ic_channels[i].ic_freq =
357 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
358 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
362 /* set supported .11b and .11g rates */
363 ic->ic_sup_rates[IEEE80211_MODE_11B] = rum_rateset_11b;
364 ic->ic_sup_rates[IEEE80211_MODE_11G] = rum_rateset_11g;
366 /* set supported .11b and .11g channels (1 through 14) */
367 for (i = 1; i <= 14; i++) {
368 ic->ic_channels[i].ic_freq =
369 ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
370 ic->ic_channels[i].ic_flags =
371 IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
372 IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
375 sc->sc_sifs = IEEE80211_DUR_SIFS; /* Default SIFS */
377 if_initname(ifp, device_get_name(self), device_get_unit(self));
378 ifp->if_softc = sc;
379 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
380 ifp->if_init = rum_init;
381 ifp->if_ioctl = rum_ioctl;
382 ifp->if_start = rum_start;
383 ifp->if_watchdog = rum_watchdog;
384 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
385 ifq_set_ready(&ifp->if_snd);
387 ic->ic_ratectl.rc_st_ratectl_cap = IEEE80211_RATECTL_CAP_ONOE;
388 ic->ic_ratectl.rc_st_ratectl = IEEE80211_RATECTL_ONOE;
389 ic->ic_ratectl.rc_st_valid_stats =
390 IEEE80211_RATECTL_STATS_PKT_NORETRY |
391 IEEE80211_RATECTL_STATS_PKT_OK |
392 IEEE80211_RATECTL_STATS_PKT_ERR |
393 IEEE80211_RATECTL_STATS_RETRIES;
394 ic->ic_ratectl.rc_st_stats = rum_stats;
395 ic->ic_ratectl.rc_st_change = rum_ratectl_change;
397 ieee80211_ifattach(ic);
399 /* Enable software beacon missing handling. */
400 ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS;
402 /* override state transition machine */
403 sc->sc_newstate = ic->ic_newstate;
404 ic->ic_newstate = rum_newstate;
405 ieee80211_media_init(ic, rum_media_change, ieee80211_media_status);
407 bpfattach_dlt(ifp, DLT_IEEE802_11_RADIO,
408 sizeof(struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN,
409 &sc->sc_drvbpf);
411 sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
412 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
413 sc->sc_rxtap.wr_ihdr.it_present = htole32(RT2573_RX_RADIOTAP_PRESENT);
415 sc->sc_txtap_len = sizeof sc->sc_txtapu;
416 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
417 sc->sc_txtap.wt_ihdr.it_present = htole32(RT2573_TX_RADIOTAP_PRESENT);
419 if (bootverbose)
420 ieee80211_announce(ic);
422 USB_ATTACH_SUCCESS_RETURN;
425 USB_DETACH(rum)
427 USB_DETACH_START(rum, sc);
428 struct ifnet *ifp = &sc->sc_ic.ic_if;
429 #ifdef INVARIANTS
430 int i;
431 #endif
433 crit_enter();
435 callout_stop(&sc->scan_ch);
436 callout_stop(&sc->stats_ch);
438 lwkt_serialize_enter(ifp->if_serializer);
439 rum_stop(sc);
440 lwkt_serialize_exit(ifp->if_serializer);
442 usb_rem_task(sc->sc_udev, &sc->sc_task);
444 bpfdetach(ifp);
445 ieee80211_ifdetach(&sc->sc_ic); /* free all nodes */
447 crit_exit();
449 KKASSERT(sc->stats_xfer == NULL);
450 KKASSERT(sc->sc_rx_pipeh == NULL);
451 KKASSERT(sc->sc_tx_pipeh == NULL);
453 #ifdef INVARIANTS
455 * Make sure TX/RX list is empty
457 for (i = 0; i < RT2573_TX_LIST_COUNT; i++) {
458 struct rum_tx_data *data = &sc->tx_data[i];
460 KKASSERT(data->xfer == NULL);
461 KKASSERT(data->ni == NULL);
462 KKASSERT(data->m == NULL);
464 for (i = 0; i < RT2573_RX_LIST_COUNT; i++) {
465 struct rum_rx_data *data = &sc->rx_data[i];
467 KKASSERT(data->xfer == NULL);
468 KKASSERT(data->m == NULL);
470 #endif
471 return 0;
474 static int
475 rum_alloc_tx_list(struct rum_softc *sc)
477 int i;
479 sc->tx_queued = 0;
480 for (i = 0; i < RT2573_TX_LIST_COUNT; i++) {
481 struct rum_tx_data *data = &sc->tx_data[i];
483 data->sc = sc;
485 data->xfer = usbd_alloc_xfer(sc->sc_udev);
486 if (data->xfer == NULL) {
487 kprintf("%s: could not allocate tx xfer\n",
488 device_get_nameunit(sc->sc_dev));
489 return ENOMEM;
492 data->buf = usbd_alloc_buffer(data->xfer,
493 RT2573_TX_DESC_SIZE + IEEE80211_MAX_LEN);
494 if (data->buf == NULL) {
495 kprintf("%s: could not allocate tx buffer\n",
496 device_get_nameunit(sc->sc_dev));
497 return ENOMEM;
500 /* clean Tx descriptor */
501 bzero(data->buf, RT2573_TX_DESC_SIZE);
503 return 0;
506 static void
507 rum_free_tx_list(struct rum_softc *sc)
509 int i;
511 for (i = 0; i < RT2573_TX_LIST_COUNT; i++) {
512 struct rum_tx_data *data = &sc->tx_data[i];
514 if (data->xfer != NULL) {
515 usbd_free_xfer(data->xfer);
516 data->xfer = NULL;
518 if (data->ni != NULL) {
519 ieee80211_free_node(data->ni);
520 data->ni = NULL;
522 if (data->m != NULL) {
523 m_freem(data->m);
524 data->m = NULL;
527 sc->tx_queued = 0;
530 static int
531 rum_alloc_rx_list(struct rum_softc *sc)
533 int i;
535 for (i = 0; i < RT2573_RX_LIST_COUNT; i++) {
536 struct rum_rx_data *data = &sc->rx_data[i];
538 data->sc = sc;
540 data->xfer = usbd_alloc_xfer(sc->sc_udev);
541 if (data->xfer == NULL) {
542 kprintf("%s: could not allocate rx xfer\n",
543 device_get_nameunit(sc->sc_dev));
544 return ENOMEM;
547 if (usbd_alloc_buffer(data->xfer, MCLBYTES) == NULL) {
548 kprintf("%s: could not allocate rx buffer\n",
549 device_get_nameunit(sc->sc_dev));
550 return ENOMEM;
553 data->m = m_getcl(MB_WAIT, MT_DATA, M_PKTHDR);
555 data->buf = mtod(data->m, uint8_t *);
556 bzero(data->buf, sizeof(struct rum_rx_desc));
558 return 0;
561 static void
562 rum_free_rx_list(struct rum_softc *sc)
564 int i;
566 for (i = 0; i < RT2573_RX_LIST_COUNT; i++) {
567 struct rum_rx_data *data = &sc->rx_data[i];
569 if (data->xfer != NULL) {
570 usbd_free_xfer(data->xfer);
571 data->xfer = NULL;
573 if (data->m != NULL) {
574 m_freem(data->m);
575 data->m = NULL;
580 static int
581 rum_media_change(struct ifnet *ifp)
583 int error;
585 error = ieee80211_media_change(ifp);
586 if (error != ENETRESET)
587 return error;
589 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
590 rum_init(ifp->if_softc);
592 return 0;
596 * This function is called periodically (every 200ms) during scanning to
597 * switch from one channel to another.
599 static void
600 rum_next_scan(void *arg)
602 struct rum_softc *sc = arg;
603 struct ieee80211com *ic = &sc->sc_ic;
604 struct ifnet *ifp = &ic->ic_if;
606 if (sc->sc_stopped)
607 return;
609 crit_enter();
611 if (ic->ic_state == IEEE80211_S_SCAN) {
612 lwkt_serialize_enter(ifp->if_serializer);
613 ieee80211_next_scan(ic);
614 lwkt_serialize_exit(ifp->if_serializer);
617 crit_exit();
620 static void
621 rum_task(void *xarg)
623 struct rum_softc *sc = xarg;
624 struct ieee80211com *ic = &sc->sc_ic;
625 struct ifnet *ifp = &ic->ic_if;
626 enum ieee80211_state nstate;
627 struct ieee80211_node *ni;
628 int arg;
630 if (sc->sc_stopped)
631 return;
633 crit_enter();
635 nstate = sc->sc_state;
636 arg = sc->sc_arg;
638 KASSERT(nstate != IEEE80211_S_INIT,
639 ("->INIT state transition should not be defered\n"));
640 rum_set_chan(sc, ic->ic_curchan);
642 switch (nstate) {
643 case IEEE80211_S_RUN:
644 ni = ic->ic_bss;
646 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
647 rum_update_slot(sc);
648 rum_enable_mrr(sc);
649 rum_set_txpreamble(sc);
650 rum_set_basicrates(sc);
651 rum_set_bssid(sc, ni->ni_bssid);
654 if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
655 ic->ic_opmode == IEEE80211_M_IBSS)
656 rum_prepare_beacon(sc);
658 if (ic->ic_opmode != IEEE80211_M_MONITOR)
659 rum_enable_tsf_sync(sc);
661 /* clear statistic registers (STA_CSR0 to STA_CSR5) */
662 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof(sc->sta));
663 callout_reset(&sc->stats_ch, 4 * hz / 5, rum_stats_timeout, sc);
664 break;
666 case IEEE80211_S_SCAN:
667 callout_reset(&sc->scan_ch, hz / 5, rum_next_scan, sc);
668 break;
670 default:
671 break;
674 lwkt_serialize_enter(ifp->if_serializer);
675 ieee80211_ratectl_newstate(ic, nstate);
676 sc->sc_newstate(ic, nstate, arg);
677 lwkt_serialize_exit(ifp->if_serializer);
679 crit_exit();
682 static int
683 rum_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
685 struct rum_softc *sc = ic->ic_if.if_softc;
686 struct ifnet *ifp = &ic->ic_if;
688 crit_enter();
690 ASSERT_SERIALIZED(ifp->if_serializer);
692 callout_stop(&sc->scan_ch);
693 callout_stop(&sc->stats_ch);
695 /* do it in a process context */
696 sc->sc_state = nstate;
697 sc->sc_arg = arg;
699 lwkt_serialize_exit(ifp->if_serializer);
700 usb_rem_task(sc->sc_udev, &sc->sc_task);
702 if (nstate == IEEE80211_S_INIT) {
703 lwkt_serialize_enter(ifp->if_serializer);
704 ieee80211_ratectl_newstate(ic, nstate);
705 sc->sc_newstate(ic, nstate, arg);
706 } else {
707 usb_add_task(sc->sc_udev, &sc->sc_task, USB_TASKQ_DRIVER);
708 lwkt_serialize_enter(ifp->if_serializer);
711 crit_exit();
712 return 0;
715 /* quickly determine if a given rate is CCK or OFDM */
716 #define RUM_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
718 #define RUM_ACK_SIZE (sizeof(struct ieee80211_frame_ack) + IEEE80211_FCS_LEN)
720 static void
721 rum_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
723 struct rum_tx_data *data = priv;
724 struct rum_softc *sc = data->sc;
725 struct ieee80211com *ic = &sc->sc_ic;
726 struct ifnet *ifp = &ic->ic_if;
727 struct ieee80211_node *ni;
729 if (sc->sc_stopped)
730 return;
732 crit_enter();
734 if (status != USBD_NORMAL_COMPLETION) {
735 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
736 crit_exit();
737 return;
740 kprintf("%s: could not transmit buffer: %s\n",
741 device_get_nameunit(sc->sc_dev), usbd_errstr(status));
743 if (status == USBD_STALLED)
744 usbd_clear_endpoint_stall_async(sc->sc_tx_pipeh);
746 ifp->if_oerrors++;
747 crit_exit();
748 return;
751 m_freem(data->m);
752 data->m = NULL;
753 ni = data->ni;
754 data->ni = NULL;
756 bzero(data->buf, sizeof(struct rum_tx_data));
757 sc->tx_queued--;
758 ifp->if_opackets++; /* XXX may fail too */
760 DPRINTFN(10, ("tx done\n"));
762 sc->sc_tx_timer = 0;
763 ifp->if_flags &= ~IFF_OACTIVE;
765 lwkt_serialize_enter(ifp->if_serializer);
766 ieee80211_free_node(ni);
767 ifp->if_start(ifp);
768 lwkt_serialize_exit(ifp->if_serializer);
770 crit_exit();
773 static void
774 rum_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
776 struct rum_rx_data *data = priv;
777 struct rum_softc *sc = data->sc;
778 struct ieee80211com *ic = &sc->sc_ic;
779 struct ifnet *ifp = &ic->ic_if;
780 struct rum_rx_desc *desc;
781 struct ieee80211_frame_min *wh;
782 struct ieee80211_node *ni;
783 struct mbuf *mnew, *m;
784 int len, rssi;
786 if (sc->sc_stopped)
787 return;
789 crit_enter();
791 if (status != USBD_NORMAL_COMPLETION) {
792 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
793 crit_exit();
794 return;
797 if (status == USBD_STALLED)
798 usbd_clear_endpoint_stall_async(sc->sc_rx_pipeh);
799 goto skip;
802 usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
804 if (len < RT2573_RX_DESC_SIZE + sizeof(struct ieee80211_frame_min)) {
805 DPRINTF(("%s: xfer too short %d\n", device_get_nameunit(sc->sc_dev),
806 len));
807 ifp->if_ierrors++;
808 goto skip;
811 desc = (struct rum_rx_desc *)data->buf;
813 if (le32toh(desc->flags) & RT2573_RX_CRC_ERROR) {
815 * This should not happen since we did not request to receive
816 * those frames when we filled RT2573_TXRX_CSR0.
818 DPRINTFN(5, ("CRC error\n"));
819 ifp->if_ierrors++;
820 goto skip;
823 mnew = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
824 if (mnew == NULL) {
825 kprintf("%s: could not allocate rx mbuf\n",
826 device_get_nameunit(sc->sc_dev));
827 ifp->if_ierrors++;
828 goto skip;
831 m = data->m;
832 data->m = NULL;
833 data->buf = NULL;
835 lwkt_serialize_enter(ifp->if_serializer);
837 /* finalize mbuf */
838 m->m_pkthdr.rcvif = ifp;
839 m->m_data = (caddr_t)(desc + 1);
840 m->m_pkthdr.len = m->m_len = (le32toh(desc->flags) >> 16) & 0xfff;
842 rssi = rum_get_rssi(sc, desc->rssi);
844 wh = mtod(m, struct ieee80211_frame_min *);
845 ni = ieee80211_find_rxnode(ic, wh);
847 /* Error happened during RSSI conversion. */
848 if (rssi < 0)
849 rssi = ni->ni_rssi;
851 if (sc->sc_drvbpf != NULL) {
852 struct rum_rx_radiotap_header *tap = &sc->sc_rxtap;
854 tap->wr_flags = 0;
855 tap->wr_rate = rum_rxrate(desc);
856 tap->wr_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq);
857 tap->wr_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
858 tap->wr_antenna = sc->rx_ant;
859 tap->wr_antsignal = rssi;
861 bpf_ptap(sc->sc_drvbpf, m, tap, sc->sc_rxtap_len);
864 /* send the frame to the 802.11 layer */
865 ieee80211_input(ic, m, ni, rssi, 0);
867 /* node is no longer needed */
868 ieee80211_free_node(ni);
870 if ((ifp->if_flags & IFF_OACTIVE) == 0)
871 ifp->if_start(ifp);
873 lwkt_serialize_exit(ifp->if_serializer);
875 data->m = mnew;
876 data->buf = mtod(data->m, uint8_t *);
878 DPRINTFN(15, ("rx done\n"));
880 skip: /* setup a new transfer */
881 bzero(data->buf, sizeof(struct rum_rx_desc));
882 usbd_setup_xfer(xfer, sc->sc_rx_pipeh, data, data->buf, MCLBYTES,
883 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rum_rxeof);
884 usbd_transfer(xfer);
886 crit_exit();
890 * This function is only used by the Rx radiotap code. It returns the rate at
891 * which a given frame was received.
893 static uint8_t
894 rum_rxrate(struct rum_rx_desc *desc)
896 if (le32toh(desc->flags) & RT2573_RX_OFDM) {
897 /* reverse function of rum_plcp_signal */
898 switch (desc->rate) {
899 case 0xb: return 12;
900 case 0xf: return 18;
901 case 0xa: return 24;
902 case 0xe: return 36;
903 case 0x9: return 48;
904 case 0xd: return 72;
905 case 0x8: return 96;
906 case 0xc: return 108;
908 } else {
909 if (desc->rate == 10)
910 return 2;
911 if (desc->rate == 20)
912 return 4;
913 if (desc->rate == 55)
914 return 11;
915 if (desc->rate == 110)
916 return 22;
918 return 2; /* should not get there */
921 static uint8_t
922 rum_plcp_signal(int rate)
924 switch (rate) {
925 /* CCK rates (returned values are device-dependent) */
926 case 2: return 0x0;
927 case 4: return 0x1;
928 case 11: return 0x2;
929 case 22: return 0x3;
931 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
932 case 12: return 0xb;
933 case 18: return 0xf;
934 case 24: return 0xa;
935 case 36: return 0xe;
936 case 48: return 0x9;
937 case 72: return 0xd;
938 case 96: return 0x8;
939 case 108: return 0xc;
941 /* unsupported rates (should not get there) */
942 default: return 0xff;
946 static void
947 rum_setup_tx_desc(struct rum_softc *sc, struct rum_tx_desc *desc,
948 uint32_t flags, uint16_t xflags, int len, int rate)
950 struct ieee80211com *ic = &sc->sc_ic;
951 uint16_t plcp_length;
952 int remainder;
954 desc->flags = htole32(flags);
955 desc->flags |= htole32(len << 16);
957 desc->xflags = htole16(xflags);
959 desc->wme = htole16(
960 RT2573_QID(0) |
961 RT2573_AIFSN(2) |
962 RT2573_LOGCWMIN(4) |
963 RT2573_LOGCWMAX(10));
965 /* setup PLCP fields */
966 desc->plcp_signal = rum_plcp_signal(rate);
967 desc->plcp_service = 4;
969 len += IEEE80211_CRC_LEN;
970 if (RUM_RATE_IS_OFDM(rate)) {
971 desc->flags |= htole32(RT2573_TX_OFDM);
973 plcp_length = len & 0xfff;
974 desc->plcp_length_hi = plcp_length >> 6;
975 desc->plcp_length_lo = plcp_length & 0x3f;
976 } else {
977 plcp_length = (16 * len + rate - 1) / rate;
978 if (rate == 22) {
979 remainder = (16 * len) % 22;
980 if (remainder != 0 && remainder < 7)
981 desc->plcp_service |= RT2573_PLCP_LENGEXT;
983 desc->plcp_length_hi = plcp_length >> 8;
984 desc->plcp_length_lo = plcp_length & 0xff;
986 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
987 desc->plcp_signal |= 0x08;
989 desc->flags |= htole32(RT2573_TX_VALID);
992 #define RUM_TX_TIMEOUT 5000
994 static int
995 rum_tx_data(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
997 struct ieee80211com *ic = &sc->sc_ic;
998 struct ifnet *ifp = &ic->ic_if;
999 struct rum_tx_desc *desc;
1000 struct rum_tx_data *data;
1001 struct ieee80211_frame *wh;
1002 uint32_t flags = 0;
1003 uint16_t dur;
1004 usbd_status error;
1005 int xferlen, rate, rateidx;
1007 wh = mtod(m0, struct ieee80211_frame *);
1009 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1010 if (ieee80211_crypto_encap(ic, ni, m0) == NULL) {
1011 m_freem(m0);
1012 return ENOBUFS;
1015 /* packet header may have moved, reset our local pointer */
1016 wh = mtod(m0, struct ieee80211_frame *);
1019 /* pickup a rate */
1020 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
1021 IEEE80211_FC0_TYPE_MGT) {
1022 /* mgmt frames are sent at the lowest available bit-rate */
1023 rateidx = 0;
1024 } else {
1025 ieee80211_ratectl_findrate(ni, m0->m_pkthdr.len, &rateidx, 1);
1027 rate = IEEE80211_RS_RATE(&ni->ni_rates, rateidx);
1029 data = &sc->tx_data[0];
1030 desc = (struct rum_tx_desc *)data->buf;
1032 data->m = m0;
1033 data->ni = ni;
1035 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1036 flags |= RT2573_TX_ACK;
1038 dur = ieee80211_txtime(ni, RUM_ACK_SIZE,
1039 ieee80211_ack_rate(ni, rate), ic->ic_flags) +
1040 sc->sc_sifs;
1041 *(uint16_t *)wh->i_dur = htole16(dur);
1043 /* tell hardware to set timestamp in probe responses */
1044 if ((wh->i_fc[0] &
1045 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
1046 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
1047 flags |= RT2573_TX_TIMESTAMP;
1050 if (sc->sc_drvbpf != NULL) {
1051 struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
1053 tap->wt_flags = 0;
1054 tap->wt_rate = rate;
1055 tap->wt_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq);
1056 tap->wt_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
1057 tap->wt_antenna = sc->tx_ant;
1059 bpf_ptap(sc->sc_drvbpf, m0, tap, sc->sc_txtap_len);
1062 m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RT2573_TX_DESC_SIZE);
1063 rum_setup_tx_desc(sc, desc, flags, 0, m0->m_pkthdr.len, rate);
1065 /* Align end on a 4-bytes boundary */
1066 xferlen = roundup(RT2573_TX_DESC_SIZE + m0->m_pkthdr.len, 4);
1069 * No space left in the last URB to store the extra 4 bytes, force
1070 * sending of another URB.
1072 if ((xferlen % 64) == 0)
1073 xferlen += 4;
1075 DPRINTFN(10, ("sending frame len=%u rate=%u xfer len=%u\n",
1076 m0->m_pkthdr.len + RT2573_TX_DESC_SIZE, rate, xferlen));
1078 lwkt_serialize_exit(ifp->if_serializer);
1080 usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf, xferlen,
1081 USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RUM_TX_TIMEOUT, rum_txeof);
1083 error = usbd_transfer(data->xfer);
1084 if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS) {
1085 m_freem(m0);
1086 data->m = NULL;
1087 data->ni = NULL;
1088 } else {
1089 sc->tx_queued++;
1090 error = 0;
1093 lwkt_serialize_enter(ifp->if_serializer);
1094 return error;
1097 static void
1098 rum_start(struct ifnet *ifp)
1100 struct rum_softc *sc = ifp->if_softc;
1101 struct ieee80211com *ic = &sc->sc_ic;
1103 ASSERT_SERIALIZED(ifp->if_serializer);
1105 if (sc->sc_stopped)
1106 return;
1108 crit_enter();
1110 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING) {
1111 crit_exit();
1112 return;
1115 for (;;) {
1116 struct ieee80211_node *ni;
1117 struct mbuf *m0;
1119 if (!IF_QEMPTY(&ic->ic_mgtq)) {
1120 if (sc->tx_queued >= RT2573_TX_LIST_COUNT) {
1121 ifp->if_flags |= IFF_OACTIVE;
1122 break;
1124 IF_DEQUEUE(&ic->ic_mgtq, m0);
1126 ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
1127 m0->m_pkthdr.rcvif = NULL;
1129 BPF_MTAP(ifp, m0);
1131 if (rum_tx_data(sc, m0, ni) != 0) {
1132 ieee80211_free_node(ni);
1133 break;
1135 } else {
1136 struct ether_header *eh;
1138 if (ic->ic_state != IEEE80211_S_RUN)
1139 break;
1141 m0 = ifq_poll(&ifp->if_snd);
1142 if (m0 == NULL)
1143 break;
1144 if (sc->tx_queued >= RT2573_TX_LIST_COUNT) {
1145 ifp->if_flags |= IFF_OACTIVE;
1146 break;
1148 ifq_dequeue(&ifp->if_snd, m0);
1150 if (m0->m_len < sizeof(struct ether_header)) {
1151 m0 = m_pullup(m0, sizeof(struct ether_header));
1152 if (m0 == NULL) {
1153 ifp->if_oerrors++;
1154 continue;
1157 eh = mtod(m0, struct ether_header *);
1159 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1160 if (ni == NULL) {
1161 m_freem(m0);
1162 continue;
1165 BPF_MTAP(ifp, m0);
1167 m0 = ieee80211_encap(ic, m0, ni);
1168 if (m0 == NULL) {
1169 ieee80211_free_node(ni);
1170 continue;
1173 if (ic->ic_rawbpf != NULL)
1174 bpf_mtap(ic->ic_rawbpf, m0);
1176 if (rum_tx_data(sc, m0, ni) != 0) {
1177 ieee80211_free_node(ni);
1178 ifp->if_oerrors++;
1179 break;
1183 sc->sc_tx_timer = 5;
1184 ifp->if_timer = 1;
1187 crit_exit();
1190 static void
1191 rum_watchdog(struct ifnet *ifp)
1193 struct rum_softc *sc = ifp->if_softc;
1195 ASSERT_SERIALIZED(ifp->if_serializer);
1197 crit_enter();
1199 ifp->if_timer = 0;
1201 if (sc->sc_tx_timer > 0) {
1202 if (--sc->sc_tx_timer == 0) {
1203 kprintf("%s: device timeout\n", device_get_nameunit(sc->sc_dev));
1204 /*rum_init(sc); XXX needs a process context! */
1205 ifp->if_oerrors++;
1207 crit_exit();
1208 return;
1210 ifp->if_timer = 1;
1213 ieee80211_watchdog(&sc->sc_ic);
1215 crit_exit();
1218 static int
1219 rum_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
1221 struct rum_softc *sc = ifp->if_softc;
1222 struct ieee80211com *ic = &sc->sc_ic;
1223 int error = 0;
1225 ASSERT_SERIALIZED(ifp->if_serializer);
1227 crit_enter();
1229 switch (cmd) {
1230 case SIOCSIFFLAGS:
1231 if (ifp->if_flags & IFF_UP) {
1232 if (ifp->if_flags & IFF_RUNNING) {
1233 lwkt_serialize_exit(ifp->if_serializer);
1234 rum_update_promisc(sc);
1235 lwkt_serialize_enter(ifp->if_serializer);
1236 } else {
1237 rum_init(sc);
1239 } else {
1240 if (ifp->if_flags & IFF_RUNNING)
1241 rum_stop(sc);
1243 break;
1244 default:
1245 error = ieee80211_ioctl(ic, cmd, data, cr);
1246 break;
1249 if (error == ENETRESET) {
1250 struct ieee80211req *ireq = (struct ieee80211req *)data;
1252 if (cmd == SIOCS80211 &&
1253 ireq->i_type == IEEE80211_IOC_CHANNEL &&
1254 ic->ic_opmode == IEEE80211_M_MONITOR) {
1256 * This allows for fast channel switching in monitor
1257 * mode (used by kismet). In IBSS mode, we must
1258 * explicitly reset the interface to generate a new
1259 * beacon frame.
1261 lwkt_serialize_exit(ifp->if_serializer);
1262 rum_set_chan(sc, ic->ic_ibss_chan);
1263 lwkt_serialize_enter(ifp->if_serializer);
1264 } else if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
1265 (IFF_UP | IFF_RUNNING)) {
1266 rum_init(sc);
1268 error = 0;
1271 crit_exit();
1272 return error;
1275 static void
1276 rum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len)
1278 usb_device_request_t req;
1279 usbd_status error;
1281 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1282 req.bRequest = RT2573_READ_EEPROM;
1283 USETW(req.wValue, 0);
1284 USETW(req.wIndex, addr);
1285 USETW(req.wLength, len);
1287 error = usbd_do_request(sc->sc_udev, &req, buf);
1288 if (error != 0) {
1289 kprintf("%s: could not read EEPROM: %s\n",
1290 device_get_nameunit(sc->sc_dev), usbd_errstr(error));
1294 static uint32_t
1295 rum_read(struct rum_softc *sc, uint16_t reg)
1297 uint32_t val;
1299 rum_read_multi(sc, reg, &val, sizeof val);
1301 return le32toh(val);
1304 static void
1305 rum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len)
1307 usb_device_request_t req;
1308 usbd_status error;
1310 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1311 req.bRequest = RT2573_READ_MULTI_MAC;
1312 USETW(req.wValue, 0);
1313 USETW(req.wIndex, reg);
1314 USETW(req.wLength, len);
1316 error = usbd_do_request(sc->sc_udev, &req, buf);
1317 if (error != 0) {
1318 kprintf("%s: could not multi read MAC register: %s\n",
1319 device_get_nameunit(sc->sc_dev), usbd_errstr(error));
1323 static void
1324 rum_write(struct rum_softc *sc, uint16_t reg, uint32_t val)
1326 uint32_t tmp = htole32(val);
1328 rum_write_multi(sc, reg, &tmp, sizeof tmp);
1331 static void
1332 rum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len)
1334 usb_device_request_t req;
1335 usbd_status error;
1337 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1338 req.bRequest = RT2573_WRITE_MULTI_MAC;
1339 USETW(req.wValue, 0);
1340 USETW(req.wIndex, reg);
1341 USETW(req.wLength, len);
1343 error = usbd_do_request(sc->sc_udev, &req, buf);
1344 if (error != 0) {
1345 kprintf("%s: could not multi write MAC register: %s\n",
1346 device_get_nameunit(sc->sc_dev), usbd_errstr(error));
1350 static void
1351 rum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val)
1353 uint32_t tmp;
1354 int ntries;
1356 for (ntries = 0; ntries < 5; ntries++) {
1357 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1358 break;
1360 if (ntries == 5) {
1361 kprintf("%s: could not write to BBP\n", device_get_nameunit(sc->sc_dev));
1362 return;
1365 tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val;
1366 rum_write(sc, RT2573_PHY_CSR3, tmp);
1369 static uint8_t
1370 rum_bbp_read(struct rum_softc *sc, uint8_t reg)
1372 uint32_t val;
1373 int ntries;
1375 for (ntries = 0; ntries < 5; ntries++) {
1376 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1377 break;
1379 if (ntries == 5) {
1380 kprintf("%s: could not read BBP\n", device_get_nameunit(sc->sc_dev));
1381 return 0;
1384 val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8;
1385 rum_write(sc, RT2573_PHY_CSR3, val);
1387 for (ntries = 0; ntries < 100; ntries++) {
1388 val = rum_read(sc, RT2573_PHY_CSR3);
1389 if (!(val & RT2573_BBP_BUSY))
1390 return val & 0xff;
1391 DELAY(1);
1394 kprintf("%s: could not read BBP\n", device_get_nameunit(sc->sc_dev));
1395 return 0;
1398 static void
1399 rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val)
1401 uint32_t tmp;
1402 int ntries;
1404 for (ntries = 0; ntries < 5; ntries++) {
1405 if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY))
1406 break;
1408 if (ntries == 5) {
1409 kprintf("%s: could not write to RF\n", device_get_nameunit(sc->sc_dev));
1410 return;
1413 tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 |
1414 (reg & 3);
1415 rum_write(sc, RT2573_PHY_CSR4, tmp);
1417 /* remember last written value in sc */
1418 sc->rf_regs[reg] = val;
1420 DPRINTFN(15, ("RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff));
1423 static void
1424 rum_select_antenna(struct rum_softc *sc)
1426 uint8_t bbp4, bbp77;
1427 uint32_t tmp;
1429 bbp4 = rum_bbp_read(sc, 4);
1430 bbp77 = rum_bbp_read(sc, 77);
1432 /* TBD */
1434 /* make sure Rx is disabled before switching antenna */
1435 tmp = rum_read(sc, RT2573_TXRX_CSR0);
1436 rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
1438 rum_bbp_write(sc, 4, bbp4);
1439 rum_bbp_write(sc, 77, bbp77);
1441 rum_write(sc, RT2573_TXRX_CSR0, tmp);
1445 * Enable multi-rate retries for frames sent at OFDM rates.
1446 * In 802.11b/g mode, allow fallback to CCK rates.
1448 static void
1449 rum_enable_mrr(struct rum_softc *sc)
1451 struct ieee80211com *ic = &sc->sc_ic;
1452 uint32_t tmp;
1454 tmp = rum_read(sc, RT2573_TXRX_CSR4);
1456 tmp &= ~RT2573_MRR_CCK_FALLBACK;
1457 if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan))
1458 tmp |= RT2573_MRR_CCK_FALLBACK;
1459 tmp |= RT2573_MRR_ENABLED;
1461 rum_write(sc, RT2573_TXRX_CSR4, tmp);
1464 static void
1465 rum_set_txpreamble(struct rum_softc *sc)
1467 uint32_t tmp;
1469 tmp = rum_read(sc, RT2573_TXRX_CSR4);
1471 tmp &= ~RT2573_SHORT_PREAMBLE;
1472 if (sc->sc_ic.ic_flags & IEEE80211_F_SHPREAMBLE)
1473 tmp |= RT2573_SHORT_PREAMBLE;
1475 rum_write(sc, RT2573_TXRX_CSR4, tmp);
1478 static void
1479 rum_set_basicrates(struct rum_softc *sc)
1481 struct ieee80211com *ic = &sc->sc_ic;
1483 /* update basic rate set */
1484 if (ic->ic_curmode == IEEE80211_MODE_11B) {
1485 /* 11b basic rates: 1, 2Mbps */
1486 rum_write(sc, RT2573_TXRX_CSR5, 0x3);
1487 } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bss->ni_chan)) {
1488 /* 11a basic rates: 6, 12, 24Mbps */
1489 rum_write(sc, RT2573_TXRX_CSR5, 0x150);
1490 } else {
1491 /* 11g basic rates: 1, 2, 5.5, 11, 6, 12, 24Mbps */
1492 rum_write(sc, RT2573_TXRX_CSR5, 0x15f);
1497 * Reprogram MAC/BBP to switch to a new band. Values taken from the reference
1498 * driver.
1500 static void
1501 rum_select_band(struct rum_softc *sc, struct ieee80211_channel *c)
1503 uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104;
1504 uint32_t tmp;
1506 /* update all BBP registers that depend on the band */
1507 bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c;
1508 bbp35 = 0x50; bbp97 = 0x48; bbp98 = 0x48;
1509 if (IEEE80211_IS_CHAN_5GHZ(c)) {
1510 bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c;
1511 bbp35 += 0x10; bbp97 += 0x10; bbp98 += 0x10;
1513 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1514 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1515 bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10;
1518 sc->bbp17 = bbp17;
1519 rum_bbp_write(sc, 17, bbp17);
1520 rum_bbp_write(sc, 96, bbp96);
1521 rum_bbp_write(sc, 104, bbp104);
1523 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1524 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1525 rum_bbp_write(sc, 75, 0x80);
1526 rum_bbp_write(sc, 86, 0x80);
1527 rum_bbp_write(sc, 88, 0x80);
1530 rum_bbp_write(sc, 35, bbp35);
1531 rum_bbp_write(sc, 97, bbp97);
1532 rum_bbp_write(sc, 98, bbp98);
1534 tmp = rum_read(sc, RT2573_PHY_CSR0);
1535 tmp &= ~(RT2573_PA_PE_2GHZ | RT2573_PA_PE_5GHZ);
1536 if (IEEE80211_IS_CHAN_2GHZ(c))
1537 tmp |= RT2573_PA_PE_2GHZ;
1538 else
1539 tmp |= RT2573_PA_PE_5GHZ;
1540 rum_write(sc, RT2573_PHY_CSR0, tmp);
1543 static void
1544 rum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c)
1546 struct ieee80211com *ic = &sc->sc_ic;
1547 const struct rfprog *rfprog;
1548 uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT;
1549 int8_t power;
1550 u_int i, chan;
1552 chan = ieee80211_chan2ieee(ic, c);
1553 if (chan == 0 || chan == IEEE80211_CHAN_ANY)
1554 return;
1556 /* select the appropriate RF settings based on what EEPROM says */
1557 rfprog = (sc->rf_rev == RT2573_RF_5225 ||
1558 sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226;
1560 /* find the settings for this channel (we know it exists) */
1561 for (i = 0; rfprog[i].chan != chan; i++)
1562 ; /* EMPTY */
1564 power = sc->txpow[i];
1565 if (power < 0) {
1566 bbp94 += power;
1567 power = 0;
1568 } else if (power > 31) {
1569 bbp94 += power - 31;
1570 power = 31;
1574 * If we are switching from the 2GHz band to the 5GHz band or
1575 * vice-versa, BBP registers need to be reprogrammed.
1577 if (c->ic_flags != sc->sc_curchan->ic_flags) {
1578 rum_select_band(sc, c);
1579 rum_select_antenna(sc);
1581 sc->sc_curchan = c;
1583 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1584 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1585 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1586 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1588 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1589 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1590 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7 | 1);
1591 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1593 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1594 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1595 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1596 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1598 DELAY(10);
1600 /* enable smart mode for MIMO-capable RFs */
1601 bbp3 = rum_bbp_read(sc, 3);
1603 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527)
1604 bbp3 &= ~RT2573_SMART_MODE;
1605 else
1606 bbp3 |= RT2573_SMART_MODE;
1608 rum_bbp_write(sc, 3, bbp3);
1610 if (bbp94 != RT2573_BBPR94_DEFAULT)
1611 rum_bbp_write(sc, 94, bbp94);
1613 sc->sc_sifs = IEEE80211_IS_CHAN_5GHZ(c) ? IEEE80211_DUR_OFDM_SIFS
1614 : IEEE80211_DUR_SIFS;
1618 * Enable TSF synchronization and tell h/w to start sending beacons for IBSS
1619 * and HostAP operating modes.
1621 static void
1622 rum_enable_tsf_sync(struct rum_softc *sc)
1624 struct ieee80211com *ic = &sc->sc_ic;
1625 uint32_t tmp;
1627 if (ic->ic_opmode != IEEE80211_M_STA) {
1629 * Change default 16ms TBTT adjustment to 8ms.
1630 * Must be done before enabling beacon generation.
1632 rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8);
1635 tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000;
1637 /* set beacon interval (in 1/16ms unit) */
1638 tmp |= ic->ic_bss->ni_intval * 16;
1640 tmp |= RT2573_TSF_TICKING | RT2573_ENABLE_TBTT;
1641 if (ic->ic_opmode == IEEE80211_M_STA)
1642 tmp |= RT2573_TSF_MODE(1);
1643 else
1644 tmp |= RT2573_TSF_MODE(2) | RT2573_GENERATE_BEACON;
1646 rum_write(sc, RT2573_TXRX_CSR9, tmp);
1649 static void
1650 rum_update_slot(struct rum_softc *sc)
1652 struct ieee80211com *ic = &sc->sc_ic;
1653 uint8_t slottime;
1654 uint32_t tmp;
1656 slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
1658 tmp = rum_read(sc, RT2573_MAC_CSR9);
1659 tmp = (tmp & ~0xff) | slottime;
1660 rum_write(sc, RT2573_MAC_CSR9, tmp);
1662 DPRINTF(("setting slot time to %uus\n", slottime));
1665 static void
1666 rum_set_bssid(struct rum_softc *sc, const uint8_t *bssid)
1668 uint32_t tmp;
1670 tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24;
1671 rum_write(sc, RT2573_MAC_CSR4, tmp);
1673 tmp = bssid[4] | bssid[5] << 8 | RT2573_ONE_BSSID << 16;
1674 rum_write(sc, RT2573_MAC_CSR5, tmp);
1677 static void
1678 rum_set_macaddr(struct rum_softc *sc, const uint8_t *addr)
1680 uint32_t tmp;
1682 tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24;
1683 rum_write(sc, RT2573_MAC_CSR2, tmp);
1685 tmp = addr[4] | addr[5] << 8 | 0xff << 16;
1686 rum_write(sc, RT2573_MAC_CSR3, tmp);
1689 static void
1690 rum_update_promisc(struct rum_softc *sc)
1692 struct ifnet *ifp = &sc->sc_ic.ic_if;
1693 uint32_t tmp;
1695 tmp = rum_read(sc, RT2573_TXRX_CSR0);
1697 tmp &= ~RT2573_DROP_NOT_TO_ME;
1698 if (!(ifp->if_flags & IFF_PROMISC))
1699 tmp |= RT2573_DROP_NOT_TO_ME;
1701 rum_write(sc, RT2573_TXRX_CSR0, tmp);
1703 DPRINTF(("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
1704 "entering" : "leaving"));
1707 static const char *
1708 rum_get_rf(int rev)
1710 switch (rev) {
1711 case RT2573_RF_2527: return "RT2527 (MIMO XR)";
1712 case RT2573_RF_2528: return "RT2528";
1713 case RT2573_RF_5225: return "RT5225 (MIMO XR)";
1714 case RT2573_RF_5226: return "RT5226";
1715 default: return "unknown";
1719 static void
1720 rum_read_eeprom(struct rum_softc *sc)
1722 struct ieee80211com *ic = &sc->sc_ic;
1723 uint16_t val;
1724 #ifdef RUM_DEBUG
1725 int i;
1726 #endif
1728 /* read MAC/BBP type */
1729 rum_eeprom_read(sc, RT2573_EEPROM_MACBBP, &val, 2);
1730 sc->macbbp_rev = le16toh(val);
1732 /* read MAC address */
1733 rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, ic->ic_myaddr, 6);
1735 rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2);
1736 val = le16toh(val);
1737 sc->rf_rev = (val >> 11) & 0x1f;
1738 sc->hw_radio = (val >> 10) & 0x1;
1739 sc->rx_ant = (val >> 4) & 0x3;
1740 sc->tx_ant = (val >> 2) & 0x3;
1741 sc->nb_ant = val & 0x3;
1743 DPRINTF(("RF revision=%d\n", sc->rf_rev));
1745 rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2);
1746 val = le16toh(val);
1747 sc->ext_5ghz_lna = (val >> 6) & 0x1;
1748 sc->ext_2ghz_lna = (val >> 4) & 0x1;
1750 DPRINTF(("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n",
1751 sc->ext_2ghz_lna, sc->ext_5ghz_lna));
1753 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2);
1754 val = le16toh(val);
1755 if ((val & 0xff) != 0xff)
1756 sc->rssi_2ghz_corr = (int8_t)(val & 0xff); /* signed */
1758 /* Only [-10, 10] is valid */
1759 if (sc->rssi_2ghz_corr < -10 || sc->rssi_2ghz_corr > 10)
1760 sc->rssi_2ghz_corr = 0;
1762 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2);
1763 val = le16toh(val);
1764 if ((val & 0xff) != 0xff)
1765 sc->rssi_5ghz_corr = (int8_t)(val & 0xff); /* signed */
1767 /* Only [-10, 10] is valid */
1768 if (sc->rssi_5ghz_corr < -10 || sc->rssi_5ghz_corr > 10)
1769 sc->rssi_5ghz_corr = 0;
1771 if (sc->ext_2ghz_lna)
1772 sc->rssi_2ghz_corr -= 14;
1773 if (sc->ext_5ghz_lna)
1774 sc->rssi_5ghz_corr -= 14;
1776 DPRINTF(("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n",
1777 sc->rssi_2ghz_corr, sc->rssi_5ghz_corr));
1779 rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2);
1780 val = le16toh(val);
1781 if ((val & 0xff) != 0xff)
1782 sc->rffreq = val & 0xff;
1784 DPRINTF(("RF freq=%d\n", sc->rffreq));
1786 /* read Tx power for all a/b/g channels */
1787 rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14);
1788 /* XXX default Tx power for 802.11a channels */
1789 memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14);
1790 #ifdef RUM_DEBUG
1791 for (i = 0; i < 14; i++)
1792 DPRINTF(("Channel=%d Tx power=%d\n", i + 1, sc->txpow[i]));
1793 #endif
1795 /* read default values for BBP registers */
1796 rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
1797 #ifdef RUM_DEBUG
1798 for (i = 0; i < 14; i++) {
1799 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
1800 continue;
1801 DPRINTF(("BBP R%d=%02x\n", sc->bbp_prom[i].reg,
1802 sc->bbp_prom[i].val));
1804 #endif
1807 static int
1808 rum_bbp_init(struct rum_softc *sc)
1810 #define N(a) (sizeof (a) / sizeof ((a)[0]))
1811 int i, ntries;
1812 uint8_t val;
1814 /* wait for BBP to be ready */
1815 for (ntries = 0; ntries < 100; ntries++) {
1816 val = rum_bbp_read(sc, 0);
1817 if (val != 0 && val != 0xff)
1818 break;
1819 DELAY(1000);
1821 if (ntries == 100) {
1822 kprintf("%s: timeout waiting for BBP\n",
1823 device_get_nameunit(sc->sc_dev));
1824 return EIO;
1827 /* initialize BBP registers to default values */
1828 for (i = 0; i < N(rum_def_bbp); i++)
1829 rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val);
1831 /* write vendor-specific BBP values (from EEPROM) */
1832 for (i = 0; i < 16; i++) {
1833 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
1834 continue;
1835 rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
1838 return 0;
1839 #undef N
1842 static void
1843 rum_init(void *xsc)
1845 #define N(a) (sizeof(a) / sizeof((a)[0]))
1846 struct rum_softc *sc = xsc;
1847 struct ieee80211com *ic = &sc->sc_ic;
1848 struct ifnet *ifp = &ic->ic_if;
1849 struct rum_rx_data *data;
1850 uint32_t tmp;
1851 usbd_status usb_err;
1852 int i, ntries, error;
1854 ASSERT_SERIALIZED(ifp->if_serializer);
1856 crit_enter();
1858 rum_stop(sc);
1859 sc->sc_stopped = 0;
1861 lwkt_serialize_exit(ifp->if_serializer);
1863 /* initialize MAC registers to default values */
1864 for (i = 0; i < N(rum_def_mac); i++)
1865 rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val);
1867 /* set host ready */
1868 rum_write(sc, RT2573_MAC_CSR1, 3);
1869 rum_write(sc, RT2573_MAC_CSR1, 0);
1871 /* wait for BBP/RF to wakeup */
1872 for (ntries = 0; ntries < 1000; ntries++) {
1873 if (rum_read(sc, RT2573_MAC_CSR12) & 8)
1874 break;
1875 rum_write(sc, RT2573_MAC_CSR12, 4); /* force wakeup */
1876 DELAY(1000);
1878 if (ntries == 1000) {
1879 kprintf("%s: timeout waiting for BBP/RF to wakeup\n",
1880 device_get_nameunit(sc->sc_dev));
1881 error = ETIMEDOUT;
1882 goto fail;
1885 error = rum_bbp_init(sc);
1886 if (error)
1887 goto fail;
1889 /* select default channel */
1890 sc->sc_curchan = ic->ic_curchan = ic->ic_ibss_chan;
1892 rum_select_band(sc, sc->sc_curchan);
1893 rum_select_antenna(sc);
1894 rum_set_chan(sc, sc->sc_curchan);
1896 /* clear STA registers */
1897 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
1899 IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
1900 rum_set_macaddr(sc, ic->ic_myaddr);
1902 /* initialize ASIC */
1903 rum_write(sc, RT2573_MAC_CSR1, 4);
1906 * Allocate xfer for AMRR statistics requests.
1908 sc->stats_xfer = usbd_alloc_xfer(sc->sc_udev);
1909 if (sc->stats_xfer == NULL) {
1910 kprintf("%s: could not allocate AMRR xfer\n",
1911 device_get_nameunit(sc->sc_dev));
1912 error = ENOMEM;
1913 goto fail;
1917 * Open Tx and Rx USB bulk pipes.
1919 usb_err = usbd_open_pipe(sc->sc_iface, sc->sc_tx_no, USBD_EXCLUSIVE_USE,
1920 &sc->sc_tx_pipeh);
1921 if (usb_err != USBD_NORMAL_COMPLETION) {
1922 kprintf("%s: could not open Tx pipe: %s\n",
1923 device_get_nameunit(sc->sc_dev), usbd_errstr(usb_err));
1924 error = EIO;
1925 goto fail;
1928 usb_err = usbd_open_pipe(sc->sc_iface, sc->sc_rx_no, USBD_EXCLUSIVE_USE,
1929 &sc->sc_rx_pipeh);
1930 if (usb_err != USBD_NORMAL_COMPLETION) {
1931 kprintf("%s: could not open Rx pipe: %s\n",
1932 device_get_nameunit(sc->sc_dev), usbd_errstr(usb_err));
1933 error = EIO;
1934 goto fail;
1938 * Allocate Tx and Rx xfer queues.
1940 error = rum_alloc_tx_list(sc);
1941 if (error) {
1942 kprintf("%s: could not allocate Tx list\n",
1943 device_get_nameunit(sc->sc_dev));
1944 goto fail;
1947 error = rum_alloc_rx_list(sc);
1948 if (error) {
1949 kprintf("%s: could not allocate Rx list\n",
1950 device_get_nameunit(sc->sc_dev));
1951 goto fail;
1955 * Start up the receive pipe.
1957 for (i = 0; i < RT2573_RX_LIST_COUNT; i++) {
1958 data = &sc->rx_data[i];
1960 usbd_setup_xfer(data->xfer, sc->sc_rx_pipeh, data, data->buf,
1961 MCLBYTES, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rum_rxeof);
1962 usbd_transfer(data->xfer);
1965 /* update Rx filter */
1966 tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff;
1968 tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR;
1969 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1970 tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR |
1971 RT2573_DROP_ACKCTS;
1972 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
1973 tmp |= RT2573_DROP_TODS;
1974 if (!(ifp->if_flags & IFF_PROMISC))
1975 tmp |= RT2573_DROP_NOT_TO_ME;
1977 rum_write(sc, RT2573_TXRX_CSR0, tmp);
1978 fail:
1979 lwkt_serialize_enter(ifp->if_serializer);
1981 if (error) {
1982 rum_stop(sc);
1983 } else {
1984 ifp->if_flags &= ~IFF_OACTIVE;
1985 ifp->if_flags |= IFF_RUNNING;
1987 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1988 if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
1989 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
1990 } else {
1991 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1995 crit_exit();
1996 #undef N
1999 static void
2000 rum_stop(struct rum_softc *sc)
2002 struct ieee80211com *ic = &sc->sc_ic;
2003 struct ifnet *ifp = &ic->ic_if;
2004 uint32_t tmp;
2006 ASSERT_SERIALIZED(ifp->if_serializer);
2008 crit_enter();
2010 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2011 sc->sc_stopped = 1;
2013 ieee80211_new_state(ic, IEEE80211_S_INIT, -1); /* free all nodes */
2015 sc->sc_tx_timer = 0;
2016 ifp->if_timer = 0;
2018 lwkt_serialize_exit(ifp->if_serializer);
2020 /* disable Rx */
2021 tmp = rum_read(sc, RT2573_TXRX_CSR0);
2022 rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
2024 /* reset ASIC */
2025 rum_write(sc, RT2573_MAC_CSR1, 3);
2026 rum_write(sc, RT2573_MAC_CSR1, 0);
2028 if (sc->stats_xfer != NULL) {
2029 usbd_free_xfer(sc->stats_xfer);
2030 sc->stats_xfer = NULL;
2033 if (sc->sc_rx_pipeh != NULL) {
2034 usbd_abort_pipe(sc->sc_rx_pipeh);
2035 usbd_close_pipe(sc->sc_rx_pipeh);
2036 sc->sc_rx_pipeh = NULL;
2039 if (sc->sc_tx_pipeh != NULL) {
2040 usbd_abort_pipe(sc->sc_tx_pipeh);
2041 usbd_close_pipe(sc->sc_tx_pipeh);
2042 sc->sc_tx_pipeh = NULL;
2045 lwkt_serialize_enter(ifp->if_serializer);
2047 rum_free_rx_list(sc);
2048 rum_free_tx_list(sc);
2050 crit_exit();
2053 static int
2054 rum_load_microcode(struct rum_softc *sc, const uint8_t *ucode, size_t size)
2056 usb_device_request_t req;
2057 uint16_t reg = RT2573_MCU_CODE_BASE;
2058 usbd_status error;
2060 /* copy firmware image into NIC */
2061 for (; size >= 4; reg += 4, ucode += 4, size -= 4)
2062 rum_write(sc, reg, UGETDW(ucode));
2064 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2065 req.bRequest = RT2573_MCU_CNTL;
2066 USETW(req.wValue, RT2573_MCU_RUN);
2067 USETW(req.wIndex, 0);
2068 USETW(req.wLength, 0);
2070 error = usbd_do_request(sc->sc_udev, &req, NULL);
2071 if (error != 0) {
2072 kprintf("%s: could not run firmware: %s\n",
2073 device_get_nameunit(sc->sc_dev), usbd_errstr(error));
2075 return error;
2078 static int
2079 rum_prepare_beacon(struct rum_softc *sc)
2081 struct ieee80211com *ic = &sc->sc_ic;
2082 struct ifnet *ifp = &ic->ic_if;
2083 struct ieee80211_beacon_offsets bo;
2084 struct rum_tx_desc desc;
2085 struct mbuf *m0;
2086 int rate;
2088 lwkt_serialize_enter(ifp->if_serializer);
2089 m0 = ieee80211_beacon_alloc(ic, ic->ic_bss, &bo);
2090 lwkt_serialize_exit(ifp->if_serializer);
2092 if (m0 == NULL) {
2093 if_printf(&ic->ic_if, "could not allocate beacon frame\n");
2094 return ENOBUFS;
2097 /* send beacons at the lowest available rate */
2098 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_bss->ni_chan) ? 12 : 2;
2100 rum_setup_tx_desc(sc, &desc, RT2573_TX_TIMESTAMP, RT2573_TX_HWSEQ,
2101 m0->m_pkthdr.len, rate);
2103 /* copy the first 24 bytes of Tx descriptor into NIC memory */
2104 rum_write_multi(sc, RT2573_HW_BEACON_BASE0, (uint8_t *)&desc, 24);
2106 /* copy beacon header and payload into NIC memory */
2107 rum_write_multi(sc, RT2573_HW_BEACON_BASE0 + 24, mtod(m0, uint8_t *),
2108 m0->m_pkthdr.len);
2110 m_freem(m0);
2112 return 0;
2115 static void
2116 rum_stats_timeout(void *arg)
2118 struct rum_softc *sc = arg;
2119 usb_device_request_t req;
2121 if (sc->sc_stopped)
2122 return;
2124 crit_enter();
2127 * Asynchronously read statistic registers (cleared by read).
2129 req.bmRequestType = UT_READ_VENDOR_DEVICE;
2130 req.bRequest = RT2573_READ_MULTI_MAC;
2131 USETW(req.wValue, 0);
2132 USETW(req.wIndex, RT2573_STA_CSR0);
2133 USETW(req.wLength, sizeof(sc->sta));
2135 usbd_setup_default_xfer(sc->stats_xfer, sc->sc_udev, sc,
2136 USBD_DEFAULT_TIMEOUT, &req,
2137 sc->sta, sizeof(sc->sta), 0,
2138 rum_stats_update);
2139 usbd_transfer(sc->stats_xfer);
2141 crit_exit();
2144 static void
2145 rum_stats_update(usbd_xfer_handle xfer, usbd_private_handle priv,
2146 usbd_status status)
2148 struct rum_softc *sc = (struct rum_softc *)priv;
2149 struct ifnet *ifp = &sc->sc_ic.ic_if;
2150 struct ieee80211_ratectl_stats *stats = &sc->sc_stats;
2152 if (status != USBD_NORMAL_COMPLETION) {
2153 kprintf("%s: could not retrieve Tx statistics - cancelling "
2154 "automatic rate control\n", device_get_nameunit(sc->sc_dev));
2155 return;
2158 crit_enter();
2160 /* count TX retry-fail as Tx errors */
2161 ifp->if_oerrors += RUM_TX_PKT_FAIL(sc);
2163 stats->stats_pkt_noretry += RUM_TX_PKT_NO_RETRY(sc);
2164 stats->stats_pkt_ok += RUM_TX_PKT_NO_RETRY(sc) +
2165 RUM_TX_PKT_ONE_RETRY(sc) +
2166 RUM_TX_PKT_MULTI_RETRY(sc);
2167 stats->stats_pkt_err += RUM_TX_PKT_FAIL(sc);
2169 stats->stats_retries += RUM_TX_PKT_ONE_RETRY(sc);
2170 #if 1
2172 * XXX Estimated average:
2173 * Actual number of retries for each packet should belong to
2174 * [2, RUM_TX_SHORT_RETRY_MAX]
2176 stats->stats_retries += RUM_TX_PKT_MULTI_RETRY(sc) *
2177 ((2 + RUM_TX_SHORT_RETRY_MAX) / 2);
2178 #else
2179 stats->stats_retries += RUM_TX_PKT_MULTI_RETRY(sc);
2180 #endif
2181 stats->stats_retries += RUM_TX_PKT_FAIL(sc) * RUM_TX_SHORT_RETRY_MAX;
2183 callout_reset(&sc->stats_ch, 4 * hz / 5, rum_stats_timeout, sc);
2185 crit_exit();
2188 static void
2189 rum_stats(struct ieee80211com *ic, struct ieee80211_node *ni __unused,
2190 struct ieee80211_ratectl_stats *stats)
2192 struct ifnet *ifp = &ic->ic_if;
2193 struct rum_softc *sc = ifp->if_softc;
2195 ASSERT_SERIALIZED(ifp->if_serializer);
2197 bcopy(&sc->sc_stats, stats, sizeof(*stats));
2198 bzero(&sc->sc_stats, sizeof(sc->sc_stats));
2201 static void
2202 rum_ratectl_change(struct ieee80211com *ic, u_int orc __unused, u_int nrc)
2204 struct ieee80211_ratectl_state *st = &ic->ic_ratectl;
2205 struct ieee80211_onoe_param *oparam;
2207 if (st->rc_st_param != NULL) {
2208 kfree(st->rc_st_param, M_DEVBUF);
2209 st->rc_st_param = NULL;
2212 switch (nrc) {
2213 case IEEE80211_RATECTL_ONOE:
2214 oparam = kmalloc(sizeof(*oparam), M_DEVBUF, M_INTWAIT);
2216 IEEE80211_ONOE_PARAM_SETUP(oparam);
2217 oparam->onoe_raise = 15;
2219 st->rc_st_param = oparam;
2220 break;
2221 case IEEE80211_RATECTL_NONE:
2222 /* This could only happen during detaching */
2223 break;
2224 default:
2225 panic("unknown rate control algo %u\n", nrc);
2229 static int
2230 rum_get_rssi(struct rum_softc *sc, uint8_t raw)
2232 int lna, agc, rssi;
2234 lna = (raw >> 5) & 0x3;
2235 agc = raw & 0x1f;
2237 if (lna == 0) {
2239 * No RSSI mapping
2241 * NB: Since RSSI is relative to noise floor, -1 is
2242 * adequate for caller to know error happened.
2244 return -1;
2247 rssi = (2 * agc) - RT2573_NOISE_FLOOR;
2249 if (IEEE80211_IS_CHAN_2GHZ(sc->sc_curchan)) {
2250 rssi += sc->rssi_2ghz_corr;
2252 if (lna == 1)
2253 rssi -= 64;
2254 else if (lna == 2)
2255 rssi -= 74;
2256 else if (lna == 3)
2257 rssi -= 90;
2258 } else {
2259 rssi += sc->rssi_5ghz_corr;
2261 if (!sc->ext_5ghz_lna && lna != 1)
2262 rssi += 4;
2264 if (lna == 1)
2265 rssi -= 64;
2266 else if (lna == 2)
2267 rssi -= 86;
2268 else if (lna == 3)
2269 rssi -= 100;
2271 return rssi;