ifq: Remove the unused parameter 'mpolled' from ifq dequeue interface
[dragonfly.git] / sys / dev / netif / rum / if_rum.c
1 /*      $OpenBSD: if_rum.c,v 1.40 2006/09/18 16:20:20 damien Exp $      */
2
3 /*-
4  * Copyright (c) 2005, 2006 Damien Bergamini <damien.bergamini@free.fr>
5  * Copyright (c) 2006 Niall O'Higgins <niallo@openbsd.org>
6  *
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.
10  *
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.
18  */
19
20 /*-
21  * Ralink Technology RT2501USB/RT2601USB chipset driver
22  * http://www.ralinktech.com/
23  */
24
25 #include <sys/param.h>
26 #include <sys/bus.h>
27 #include <sys/endian.h>
28 #include <sys/kernel.h>
29 #include <sys/malloc.h>
30 #include <sys/mbuf.h>
31 #include <sys/rman.h>
32 #include <sys/serialize.h>
33 #include <sys/socket.h>
34 #include <sys/sockio.h>
35
36 #include <net/bpf.h>
37 #include <net/ethernet.h>
38 #include <net/if.h>
39 #include <net/if_arp.h>
40 #include <net/if_dl.h>
41 #include <net/if_media.h>
42 #include <net/ifq_var.h>
43
44 #include <netproto/802_11/ieee80211_var.h>
45 #include <netproto/802_11/ieee80211_radiotap.h>
46 #include <netproto/802_11/wlan_ratectl/onoe/ieee80211_onoe_param.h>
47
48 #include <bus/usb/usb.h>
49 #include <bus/usb/usbdi.h>
50 #include <bus/usb/usbdi_util.h>
51
52 #include "if_rumreg.h"
53 #include "if_rumvar.h"
54 #include "rum_ucode.h"
55
56 #ifdef USB_DEBUG
57 #define RUM_DEBUG
58 #endif
59
60 #ifdef RUM_DEBUG
61 #define DPRINTF(x)      do { if (rum_debug) kprintf x; } while (0)
62 #define DPRINTFN(n, x)  do { if (rum_debug >= (n)) kprintf x; } while (0)
63 int rum_debug = 0;
64 #else
65 #define DPRINTF(x)
66 #define DPRINTFN(n, x)
67 #endif
68
69 /* various supported device vendors/products */
70 static const struct usb_devno rum_devs[] = {
71         { USB_DEVICE(0x0411, 0x00d8) }, /* Melco WLI-U2-SG54HP */
72         { USB_DEVICE(0x0411, 0x00d9) }, /* Melco WLI-U2-G54HP */
73         { USB_DEVICE(0x050d, 0x705a) }, /* Belkin F5D7050A */
74         { USB_DEVICE(0x050d, 0x905b) }, /* Belkin F5D9050 ver3 */
75         { USB_DEVICE(0x0586, 0x3415) }, /* ZyXEL RT2573 */
76         { USB_DEVICE(0x06f8, 0xe010) }, /* Guillemot HWGUSB2-54-LB */
77         { USB_DEVICE(0x06f8, 0xe020) }, /* Guillemot HWGUSB2-54V2-AP */
78         { USB_DEVICE(0x0769, 0x31f3) }, /* Surecom RT2573 */
79         { USB_DEVICE(0x07b8, 0xb21b) }, /* AboCom HWU54DM */
80         { USB_DEVICE(0x07b8, 0xb21c) }, /* AboCom RT2573 */
81         { USB_DEVICE(0x07b8, 0xb21d) }, /* AboCom RT2573 */
82         { USB_DEVICE(0x07b8, 0xb21e) }, /* AboCom RT2573 */
83         { USB_DEVICE(0x07b8, 0xb21f) }, /* AboCom WUG2700 */
84         { USB_DEVICE(0x07d1, 0x3c03) }, /* D-Link DWL-G122 rev c1 */
85         { USB_DEVICE(0x07d1, 0x3c04) }, /* D-Link WUA-1340 */
86         { USB_DEVICE(0x0b05, 0x1723) }, /* Asus WL-167g */
87         { USB_DEVICE(0x0b05, 0x1724) }, /* Asus WL-167g */
88         { USB_DEVICE(0x0db0, 0x6874) }, /* MSI RT2573 */
89         { USB_DEVICE(0x0db0, 0x6877) }, /* MSI RT2573 */
90         { USB_DEVICE(0x0db0, 0xa861) }, /* MSI RT2573 */
91         { USB_DEVICE(0x0db0, 0xa874) }, /* MSI RT2573 */
92         { USB_DEVICE(0x0df6, 0x90ac) }, /* Sitecom WL-172 */
93         { USB_DEVICE(0x0df6, 0x9712) }, /* Sitecom WL-113 rev 2 */
94         { USB_DEVICE(0x0eb0, 0x9021) }, /* Nova Technology RT2573 */
95         { USB_DEVICE(0x1044, 0x8008) }, /* GIGABYTE GN-WB01GS */
96         { USB_DEVICE(0x1044, 0x800a) }, /* GIGABYTE GN-WI05GS */
97         { USB_DEVICE(0x1371, 0x9022) }, /* (really) C-Net RT2573 */
98         { USB_DEVICE(0x1371, 0x9032) }, /* (really) C-Net CWD854F */
99         { USB_DEVICE(0x13b1, 0x0020) }, /* Cisco-Linksys WUSB54GC */
100         { USB_DEVICE(0x13b1, 0x0023) }, /* Cisco-Linksys WUSB54GR */
101         { USB_DEVICE(0x1472, 0x0009) }, /* Huawei RT2573 */
102         { USB_DEVICE(0x148f, 0x2573) }, /* Ralink RT2573 */
103         { USB_DEVICE(0x148f, 0x2671) }, /* Ralink RT2671 */
104         { USB_DEVICE(0x148f, 0x9021) }, /* Ralink RT2573 */
105         { USB_DEVICE(0x14b2, 0x3c22) }, /* Conceptronic C54RU */
106         { USB_DEVICE(0x15a9, 0x0004) }, /* SparkLan RT2573 */
107         { USB_DEVICE(0x1631, 0xc019) }, /* Good Way Technology RT2573 */
108         { USB_DEVICE(0x1690, 0x0722) }, /* Gigaset RT2573 */
109         { USB_DEVICE(0x1737, 0x0020) }, /* Linksys WUSB54GC */
110         { USB_DEVICE(0x1737, 0x0023) }, /* Linksys WUSB54GR */
111         { USB_DEVICE(0x18c5, 0x0002) }, /* AMIT CG-WLUSB2GO */
112         { USB_DEVICE(0x18e8, 0x6196) }, /* Qcom RT2573 */
113         { USB_DEVICE(0x18e8, 0x6229) }, /* Qcom RT2573 */
114         { USB_DEVICE(0x18e8, 0x6238) }, /* Qcom RT2573 */
115         { USB_DEVICE(0x2019, 0xab01) }, /* Planex GW-US54HP */
116         { USB_DEVICE(0x2019, 0xab50) }, /* Planex GW-US54Mini2 */
117         { USB_DEVICE(0x2019, 0xed02) }, /* Planex GW-USMM */
118 };
119
120 static int              rum_alloc_tx_list(struct rum_softc *);
121 static void             rum_free_tx_list(struct rum_softc *);
122 static int              rum_alloc_rx_list(struct rum_softc *);
123 static void             rum_free_rx_list(struct rum_softc *);
124 static int              rum_media_change(struct ifnet *);
125 static void             rum_next_scan(void *);
126 static void             rum_task(void *);
127 static int              rum_newstate(struct ieee80211com *,
128                             enum ieee80211_state, int);
129 static void             rum_txeof(usbd_xfer_handle, usbd_private_handle,
130                             usbd_status);
131 static void             rum_rxeof(usbd_xfer_handle, usbd_private_handle,
132                             usbd_status);
133 static uint8_t          rum_rxrate(struct rum_rx_desc *);
134 static uint8_t          rum_plcp_signal(int);
135 static void             rum_setup_tx_desc(struct rum_softc *,
136                             struct rum_tx_desc *, uint32_t, uint16_t, int,
137                             int);
138 static int              rum_tx_data(struct rum_softc *, struct mbuf *,
139                             struct ieee80211_node *);
140 static void             rum_start(struct ifnet *, struct ifaltq_subque *);
141 static void             rum_watchdog(struct ifnet *);
142 static int              rum_ioctl(struct ifnet *, u_long, caddr_t,
143                                   struct ucred *);
144 static void             rum_eeprom_read(struct rum_softc *, uint16_t, void *,
145                             int);
146 static uint32_t         rum_read(struct rum_softc *, uint16_t);
147 static void             rum_read_multi(struct rum_softc *, uint16_t, void *,
148                             int);
149 static void             rum_write(struct rum_softc *, uint16_t, uint32_t);
150 static void             rum_write_multi(struct rum_softc *, uint16_t, void *,
151                             size_t);
152 static void             rum_bbp_write(struct rum_softc *, uint8_t, uint8_t);
153 static uint8_t          rum_bbp_read(struct rum_softc *, uint8_t);
154 static void             rum_rf_write(struct rum_softc *, uint8_t, uint32_t);
155 static void             rum_select_antenna(struct rum_softc *);
156 static void             rum_enable_mrr(struct rum_softc *);
157 static void             rum_set_txpreamble(struct rum_softc *);
158 static void             rum_set_basicrates(struct rum_softc *);
159 static void             rum_select_band(struct rum_softc *,
160                             struct ieee80211_channel *);
161 static void             rum_set_chan(struct rum_softc *,
162                             struct ieee80211_channel *);
163 static void             rum_enable_tsf_sync(struct rum_softc *);
164 static void             rum_update_slot(struct rum_softc *);
165 static void             rum_set_bssid(struct rum_softc *, const uint8_t *);
166 static void             rum_set_macaddr(struct rum_softc *, const uint8_t *);
167 static void             rum_update_promisc(struct rum_softc *);
168 static const char       *rum_get_rf(int);
169 static void             rum_read_eeprom(struct rum_softc *);
170 static int              rum_bbp_init(struct rum_softc *);
171 static void             rum_init(void *);
172 static void             rum_stop(struct rum_softc *);
173 static int              rum_load_microcode(struct rum_softc *, const uint8_t *,
174                             size_t);
175 static int              rum_prepare_beacon(struct rum_softc *);
176
177 static void             rum_stats_timeout(void *);
178 static void             rum_stats_update(usbd_xfer_handle, usbd_private_handle,
179                                          usbd_status);
180 static void             rum_stats(struct ieee80211com *,
181                                   struct ieee80211_node *,
182                                   struct ieee80211_ratectl_stats *);
183 static void             *rum_ratectl_attach(struct ieee80211com *, u_int);
184 static int              rum_get_rssi(struct rum_softc *, uint8_t);
185
186 /*
187  * Supported rates for 802.11a/b/g modes (in 500Kbps unit).
188  */
189 static const struct ieee80211_rateset rum_rateset_11a =
190         { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
191
192 static const struct ieee80211_rateset rum_rateset_11b =
193         { 4, { 2, 4, 11, 22 } };
194
195 static const struct ieee80211_rateset rum_rateset_11g =
196         { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
197
198 static const struct {
199         uint32_t        reg;
200         uint32_t        val;
201 } rum_def_mac[] = {
202         RT2573_DEF_MAC
203 };
204
205 static const struct {
206         uint8_t reg;
207         uint8_t val;
208 } rum_def_bbp[] = {
209         RT2573_DEF_BBP
210 };
211
212 static const struct rfprog {
213         uint8_t         chan;
214         uint32_t        r1, r2, r3, r4;
215 }  rum_rf5226[] = {
216         RT2573_RF5226
217 }, rum_rf5225[] = {
218         RT2573_RF5225
219 };
220
221 static device_probe_t rum_match;
222 static device_attach_t rum_attach;
223 static device_detach_t rum_detach;
224
225 static devclass_t rum_devclass;
226
227 static kobj_method_t rum_methods[] = {
228         DEVMETHOD(device_probe, rum_match),
229         DEVMETHOD(device_attach, rum_attach),
230         DEVMETHOD(device_detach, rum_detach),
231         DEVMETHOD_END
232 };
233
234 static driver_t rum_driver = {
235         "rum",
236         rum_methods,
237         sizeof(struct rum_softc)
238 };
239
240 DRIVER_MODULE(rum, uhub, rum_driver, rum_devclass, usbd_driver_load, NULL);
241
242 MODULE_DEPEND(rum, usb, 1, 1, 1);
243 MODULE_DEPEND(rum, wlan, 1, 1, 1);
244 MODULE_DEPEND(rum, wlan_ratectl_onoe, 1, 1, 1);
245
246 static int
247 rum_match(device_t self)
248 {
249         struct usb_attach_arg *uaa = device_get_ivars(self);
250
251         if (uaa->iface != NULL)
252                 return UMATCH_NONE;
253
254         return (usb_lookup(rum_devs, uaa->vendor, uaa->product) != NULL) ?
255             UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
256 }
257
258 static int
259 rum_attach(device_t self)
260 {
261         struct rum_softc *sc = device_get_softc(self);
262         struct usb_attach_arg *uaa = device_get_ivars(self);
263         struct ieee80211com *ic = &sc->sc_ic;
264         struct ifnet *ifp = &ic->ic_if;
265         usb_interface_descriptor_t *id;
266         usb_endpoint_descriptor_t *ed;
267         usbd_status error;
268         int i, ntries;
269         uint32_t tmp;
270         char ethstr[ETHER_ADDRSTRLEN + 1];
271
272         sc->sc_udev = uaa->device;
273         sc->sc_dev = self;
274
275         if (usbd_set_config_no(sc->sc_udev, RT2573_CONFIG_NO, 0) != 0) {
276                 kprintf("%s: could not set configuration no\n",
277                     device_get_nameunit(sc->sc_dev));
278                 return ENXIO;
279         }
280
281         /* get the first interface handle */
282         error = usbd_device2interface_handle(sc->sc_udev, RT2573_IFACE_INDEX,
283             &sc->sc_iface);
284         if (error != 0) {
285                 kprintf("%s: could not get interface handle\n",
286                     device_get_nameunit(sc->sc_dev));
287                 return ENXIO;
288         }
289
290         /*
291          * Find endpoints.
292          */
293         id = usbd_get_interface_descriptor(sc->sc_iface);
294
295         sc->sc_rx_no = sc->sc_tx_no = -1;
296         for (i = 0; i < id->bNumEndpoints; i++) {
297                 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
298                 if (ed == NULL) {
299                         kprintf("%s: no endpoint descriptor for iface %d\n",
300                             device_get_nameunit(sc->sc_dev), i);
301                         return ENXIO;
302                 }
303
304                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
305                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
306                         sc->sc_rx_no = ed->bEndpointAddress;
307                 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
308                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
309                         sc->sc_tx_no = ed->bEndpointAddress;
310         }
311         if (sc->sc_rx_no == -1 || sc->sc_tx_no == -1) {
312                 kprintf("%s: missing endpoint\n", device_get_nameunit(sc->sc_dev));
313                 return ENXIO;
314         }
315
316         usb_init_task(&sc->sc_task, rum_task, sc);
317
318         callout_init(&sc->scan_ch);
319         callout_init(&sc->stats_ch);
320
321         /* retrieve RT2573 rev. no */
322         for (ntries = 0; ntries < 1000; ntries++) {
323                 if ((tmp = rum_read(sc, RT2573_MAC_CSR0)) != 0)
324                         break;
325                 DELAY(1000);
326         }
327         if (ntries == 1000) {
328                 kprintf("%s: timeout waiting for chip to settle\n",
329                     device_get_nameunit(sc->sc_dev));
330                 return ENXIO;
331         }
332
333         /* retrieve MAC address and various other things from EEPROM */
334         rum_read_eeprom(sc);
335
336         kprintf("%s: MAC/BBP RT%04x (rev 0x%05x), RF %s, address %s\n",
337             device_get_nameunit(sc->sc_dev), sc->macbbp_rev, tmp,
338             rum_get_rf(sc->rf_rev), kether_addr(ic->ic_myaddr, ethstr));
339
340         error = rum_load_microcode(sc, rt2573, sizeof(rt2573));
341         if (error != 0) {
342                 device_printf(self, "can't load microcode\n");
343                 return ENXIO;
344         }
345
346         ic->ic_phytype = IEEE80211_T_OFDM;      /* not only, but not used */
347         ic->ic_opmode = IEEE80211_M_STA;        /* default to BSS mode */
348         ic->ic_state = IEEE80211_S_INIT;
349
350         /* set device capabilities */
351         ic->ic_caps =
352             IEEE80211_C_IBSS |          /* IBSS mode supported */
353             IEEE80211_C_MONITOR |       /* monitor mode supported */
354             IEEE80211_C_HOSTAP |        /* HostAp mode supported */
355             IEEE80211_C_TXPMGT |        /* tx power management */
356             IEEE80211_C_SHPREAMBLE |    /* short preamble supported */
357             IEEE80211_C_SHSLOT |        /* short slot time supported */
358             IEEE80211_C_WPA;            /* WPA 1+2 */
359
360         if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_5226) {
361                 /* set supported .11a rates */
362                 ic->ic_sup_rates[IEEE80211_MODE_11A] = rum_rateset_11a;
363
364                 /* set supported .11a channels */
365                 for (i = 34; i <= 46; i += 4) {
366                         ic->ic_channels[i].ic_freq =
367                             ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
368                         ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
369                 }
370                 for (i = 36; i <= 64; i += 4) {
371                         ic->ic_channels[i].ic_freq =
372                             ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
373                         ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
374                 }
375                 for (i = 100; i <= 140; i += 4) {
376                         ic->ic_channels[i].ic_freq =
377                             ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
378                         ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
379                 }
380                 for (i = 149; i <= 165; i += 4) {
381                         ic->ic_channels[i].ic_freq =
382                             ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
383                         ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
384                 }
385         }
386
387         /* set supported .11b and .11g rates */
388         ic->ic_sup_rates[IEEE80211_MODE_11B] = rum_rateset_11b;
389         ic->ic_sup_rates[IEEE80211_MODE_11G] = rum_rateset_11g;
390
391         /* set supported .11b and .11g channels (1 through 14) */
392         for (i = 1; i <= 14; i++) {
393                 ic->ic_channels[i].ic_freq =
394                     ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
395                 ic->ic_channels[i].ic_flags =
396                     IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
397                     IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
398         }
399
400         sc->sc_sifs = IEEE80211_DUR_SIFS;       /* Default SIFS */
401
402         if_initname(ifp, device_get_name(self), device_get_unit(self));
403         ifp->if_softc = sc;
404         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
405         ifp->if_init = rum_init;
406         ifp->if_ioctl = rum_ioctl;
407         ifp->if_start = rum_start;
408         ifp->if_watchdog = rum_watchdog;
409         ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
410         ifq_set_ready(&ifp->if_snd);
411
412         IEEE80211_ONOE_PARAM_SETUP(&sc->sc_onoe_param);
413         sc->sc_onoe_param.onoe_raise = 15;
414         ic->ic_ratectl.rc_st_ratectl_cap = IEEE80211_RATECTL_CAP_ONOE;
415         ic->ic_ratectl.rc_st_ratectl = IEEE80211_RATECTL_ONOE;
416         ic->ic_ratectl.rc_st_stats = rum_stats;
417         ic->ic_ratectl.rc_st_attach = rum_ratectl_attach;
418
419         ieee80211_ifattach(ic);
420
421         /* Enable software beacon missing handling. */
422         ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS;
423
424         /* override state transition machine */
425         sc->sc_newstate = ic->ic_newstate;
426         ic->ic_newstate = rum_newstate;
427         ieee80211_media_init(ic, rum_media_change, ieee80211_media_status);
428
429         bpfattach_dlt(ifp, DLT_IEEE802_11_RADIO,
430             sizeof(struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN,
431             &sc->sc_drvbpf);
432
433         sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
434         sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
435         sc->sc_rxtap.wr_ihdr.it_present = htole32(RT2573_RX_RADIOTAP_PRESENT);
436
437         sc->sc_txtap_len = sizeof sc->sc_txtapu;
438         sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
439         sc->sc_txtap.wt_ihdr.it_present = htole32(RT2573_TX_RADIOTAP_PRESENT);
440
441         if (bootverbose)
442                 ieee80211_announce(ic);
443
444         return 0;
445 }
446
447 static int
448 rum_detach(device_t self)
449 {
450         struct rum_softc *sc = device_get_softc(self);
451         struct ifnet *ifp = &sc->sc_ic.ic_if;
452 #ifdef INVARIANTS
453         int i;
454 #endif
455
456         crit_enter();
457
458         callout_stop(&sc->scan_ch);
459         callout_stop(&sc->stats_ch);
460
461         lwkt_serialize_enter(ifp->if_serializer);
462         rum_stop(sc);
463         lwkt_serialize_exit(ifp->if_serializer);
464
465         usb_rem_task(sc->sc_udev, &sc->sc_task);
466
467         bpfdetach(ifp);
468         ieee80211_ifdetach(&sc->sc_ic); /* free all nodes */
469
470         crit_exit();
471
472         KKASSERT(sc->stats_xfer == NULL);
473         KKASSERT(sc->sc_rx_pipeh == NULL);
474         KKASSERT(sc->sc_tx_pipeh == NULL);
475
476 #ifdef INVARIANTS
477         /*
478          * Make sure TX/RX list is empty
479          */
480         for (i = 0; i < RT2573_TX_LIST_COUNT; i++) {
481                 struct rum_tx_data *data = &sc->tx_data[i];
482
483                 KKASSERT(data->xfer == NULL);
484                 KKASSERT(data->ni == NULL);
485                 KKASSERT(data->m == NULL);
486         }
487         for (i = 0; i < RT2573_RX_LIST_COUNT; i++) {
488                 struct rum_rx_data *data = &sc->rx_data[i];
489
490                 KKASSERT(data->xfer == NULL);
491                 KKASSERT(data->m == NULL);
492         }
493 #endif
494         return 0;
495 }
496
497 static int
498 rum_alloc_tx_list(struct rum_softc *sc)
499 {
500         int i;
501
502         sc->tx_queued = 0;
503         for (i = 0; i < RT2573_TX_LIST_COUNT; i++) {
504                 struct rum_tx_data *data = &sc->tx_data[i];
505
506                 data->sc = sc;
507
508                 data->xfer = usbd_alloc_xfer(sc->sc_udev);
509                 if (data->xfer == NULL) {
510                         kprintf("%s: could not allocate tx xfer\n",
511                             device_get_nameunit(sc->sc_dev));
512                         return ENOMEM;
513                 }
514
515                 data->buf = usbd_alloc_buffer(data->xfer,
516                     RT2573_TX_DESC_SIZE + IEEE80211_MAX_LEN);
517                 if (data->buf == NULL) {
518                         kprintf("%s: could not allocate tx buffer\n",
519                             device_get_nameunit(sc->sc_dev));
520                         return ENOMEM;
521                 }
522
523                 /* clean Tx descriptor */
524                 bzero(data->buf, RT2573_TX_DESC_SIZE);
525         }
526         return 0;
527 }
528
529 static void
530 rum_free_tx_list(struct rum_softc *sc)
531 {
532         int i;
533
534         for (i = 0; i < RT2573_TX_LIST_COUNT; i++) {
535                 struct rum_tx_data *data = &sc->tx_data[i];
536
537                 if (data->xfer != NULL) {
538                         usbd_free_xfer(data->xfer);
539                         data->xfer = NULL;
540                 }
541                 if (data->ni != NULL) {
542                         ieee80211_free_node(data->ni);
543                         data->ni = NULL;
544                 }
545                 if (data->m != NULL) {
546                         m_freem(data->m);
547                         data->m = NULL;
548                 }
549         }
550         sc->tx_queued = 0;
551 }
552
553 static int
554 rum_alloc_rx_list(struct rum_softc *sc)
555 {
556         int i;
557
558         for (i = 0; i < RT2573_RX_LIST_COUNT; i++) {
559                 struct rum_rx_data *data = &sc->rx_data[i];
560
561                 data->sc = sc;
562
563                 data->xfer = usbd_alloc_xfer(sc->sc_udev);
564                 if (data->xfer == NULL) {
565                         kprintf("%s: could not allocate rx xfer\n",
566                             device_get_nameunit(sc->sc_dev));
567                         return ENOMEM;
568                 }
569
570                 if (usbd_alloc_buffer(data->xfer, MCLBYTES) == NULL) {
571                         kprintf("%s: could not allocate rx buffer\n",
572                             device_get_nameunit(sc->sc_dev));
573                         return ENOMEM;
574                 }
575
576                 data->m = m_getcl(MB_WAIT, MT_DATA, M_PKTHDR);
577
578                 data->buf = mtod(data->m, uint8_t *);
579                 bzero(data->buf, sizeof(struct rum_rx_desc));
580         }
581         return 0;
582 }
583
584 static void
585 rum_free_rx_list(struct rum_softc *sc)
586 {
587         int i;
588
589         for (i = 0; i < RT2573_RX_LIST_COUNT; i++) {
590                 struct rum_rx_data *data = &sc->rx_data[i];
591
592                 if (data->xfer != NULL) {
593                         usbd_free_xfer(data->xfer);
594                         data->xfer = NULL;
595                 }
596                 if (data->m != NULL) {
597                         m_freem(data->m);
598                         data->m = NULL;
599                 }
600         }
601 }
602
603 static int
604 rum_media_change(struct ifnet *ifp)
605 {
606         int error;
607
608         error = ieee80211_media_change(ifp);
609         if (error != ENETRESET)
610                 return error;
611
612         if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
613                 rum_init(ifp->if_softc);
614
615         return 0;
616 }
617
618 /*
619  * This function is called periodically (every 200ms) during scanning to
620  * switch from one channel to another.
621  */
622 static void
623 rum_next_scan(void *arg)
624 {
625         struct rum_softc *sc = arg;
626         struct ieee80211com *ic = &sc->sc_ic;
627         struct ifnet *ifp = &ic->ic_if;
628
629         if (sc->sc_stopped)
630                 return;
631
632         crit_enter();
633
634         if (ic->ic_state == IEEE80211_S_SCAN) {
635                 lwkt_serialize_enter(ifp->if_serializer);
636                 ieee80211_next_scan(ic);
637                 lwkt_serialize_exit(ifp->if_serializer);
638         }
639
640         crit_exit();
641 }
642
643 static void
644 rum_task(void *xarg)
645 {
646         struct rum_softc *sc = xarg;
647         struct ieee80211com *ic = &sc->sc_ic;
648         struct ifnet *ifp = &ic->ic_if;
649         enum ieee80211_state nstate;
650         struct ieee80211_node *ni;
651         int arg;
652
653         if (sc->sc_stopped)
654                 return;
655
656         crit_enter();
657
658         nstate = sc->sc_state;
659         arg = sc->sc_arg;
660
661         KASSERT(nstate != IEEE80211_S_INIT,
662                 ("->INIT state transition should not be defered\n"));
663         rum_set_chan(sc, ic->ic_curchan);
664
665         switch (nstate) {
666         case IEEE80211_S_RUN:
667                 ni = ic->ic_bss;
668
669                 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
670                         rum_update_slot(sc);
671                         rum_enable_mrr(sc);
672                         rum_set_txpreamble(sc);
673                         rum_set_basicrates(sc);
674                         rum_set_bssid(sc, ni->ni_bssid);
675                 }
676
677                 if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
678                     ic->ic_opmode == IEEE80211_M_IBSS)
679                         rum_prepare_beacon(sc);
680
681                 if (ic->ic_opmode != IEEE80211_M_MONITOR)
682                         rum_enable_tsf_sync(sc);
683
684                 /* clear statistic registers (STA_CSR0 to STA_CSR5) */
685                 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof(sc->sta));
686                 callout_reset(&sc->stats_ch, 4 * hz / 5, rum_stats_timeout, sc);
687                 break;
688
689         case IEEE80211_S_SCAN:
690                 callout_reset(&sc->scan_ch, hz / 5, rum_next_scan, sc);
691                 break;
692
693         default:
694                 break;
695         }
696
697         lwkt_serialize_enter(ifp->if_serializer);
698         ieee80211_ratectl_newstate(ic, nstate);
699         sc->sc_newstate(ic, nstate, arg);
700         lwkt_serialize_exit(ifp->if_serializer);
701
702         crit_exit();
703 }
704
705 static int
706 rum_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
707 {
708         struct rum_softc *sc = ic->ic_if.if_softc;
709         struct ifnet *ifp = &ic->ic_if;
710
711         crit_enter();
712
713         ASSERT_SERIALIZED(ifp->if_serializer);
714
715         callout_stop(&sc->scan_ch);
716         callout_stop(&sc->stats_ch);
717
718         /* do it in a process context */
719         sc->sc_state = nstate;
720         sc->sc_arg = arg;
721
722         lwkt_serialize_exit(ifp->if_serializer);
723         usb_rem_task(sc->sc_udev, &sc->sc_task);
724
725         if (nstate == IEEE80211_S_INIT) {
726                 lwkt_serialize_enter(ifp->if_serializer);
727                 ieee80211_ratectl_newstate(ic, nstate);
728                 sc->sc_newstate(ic, nstate, arg);
729         } else {
730                 usb_add_task(sc->sc_udev, &sc->sc_task, USB_TASKQ_DRIVER);
731                 lwkt_serialize_enter(ifp->if_serializer);
732         }
733
734         crit_exit();
735         return 0;
736 }
737
738 /* quickly determine if a given rate is CCK or OFDM */
739 #define RUM_RATE_IS_OFDM(rate)  ((rate) >= 12 && (rate) != 22)
740
741 #define RUM_ACK_SIZE    (sizeof(struct ieee80211_frame_ack) + IEEE80211_CRC_LEN)
742
743 static void
744 rum_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
745 {
746         struct rum_tx_data *data = priv;
747         struct rum_softc *sc = data->sc;
748         struct ieee80211com *ic = &sc->sc_ic;
749         struct ifnet *ifp = &ic->ic_if;
750         struct ieee80211_node *ni;
751
752         if (sc->sc_stopped)
753                 return;
754
755         crit_enter();
756
757         if (status != USBD_NORMAL_COMPLETION) {
758                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
759                         crit_exit();
760                         return;
761                 }
762
763                 kprintf("%s: could not transmit buffer: %s\n",
764                     device_get_nameunit(sc->sc_dev), usbd_errstr(status));
765
766                 if (status == USBD_STALLED)
767                         usbd_clear_endpoint_stall_async(sc->sc_tx_pipeh);
768
769                 IFNET_STAT_INC(ifp, oerrors, 1);
770                 crit_exit();
771                 return;
772         }
773
774         m_freem(data->m);
775         data->m = NULL;
776         ni = data->ni;
777         data->ni = NULL;
778
779         bzero(data->buf, sizeof(struct rum_tx_data));
780         sc->tx_queued--;
781         IFNET_STAT_INC(ifp, opackets, 1);       /* XXX may fail too */
782
783         DPRINTFN(10, ("tx done\n"));
784
785         sc->sc_tx_timer = 0;
786         ifq_clr_oactive(&ifp->if_snd);
787
788         lwkt_serialize_enter(ifp->if_serializer);
789         ieee80211_free_node(ni);
790         ifp->if_start(ifp);
791         lwkt_serialize_exit(ifp->if_serializer);
792
793         crit_exit();
794 }
795
796 static void
797 rum_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
798 {
799         struct rum_rx_data *data = priv;
800         struct rum_softc *sc = data->sc;
801         struct ieee80211com *ic = &sc->sc_ic;
802         struct ifnet *ifp = &ic->ic_if;
803         struct rum_rx_desc *desc;
804         struct ieee80211_frame_min *wh;
805         struct ieee80211_node *ni;
806         struct mbuf *mnew, *m;
807         int len, rssi;
808
809         if (sc->sc_stopped)
810                 return;
811
812         crit_enter();
813
814         if (status != USBD_NORMAL_COMPLETION) {
815                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
816                         crit_exit();
817                         return;
818                 }
819
820                 if (status == USBD_STALLED)
821                         usbd_clear_endpoint_stall_async(sc->sc_rx_pipeh);
822                 goto skip;
823         }
824
825         usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
826
827         if (len < RT2573_RX_DESC_SIZE + sizeof(struct ieee80211_frame_min)) {
828                 DPRINTF(("%s: xfer too short %d\n", device_get_nameunit(sc->sc_dev),
829                     len));
830                 IFNET_STAT_INC(ifp, ierrors, 1);
831                 goto skip;
832         }
833
834         desc = (struct rum_rx_desc *)data->buf;
835
836         if (le32toh(desc->flags) & RT2573_RX_CRC_ERROR) {
837                 /*
838                  * This should not happen since we did not request to receive
839                  * those frames when we filled RT2573_TXRX_CSR0.
840                  */
841                 DPRINTFN(5, ("CRC error\n"));
842                 IFNET_STAT_INC(ifp, ierrors, 1);
843                 goto skip;
844         }
845
846         mnew = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
847         if (mnew == NULL) {
848                 kprintf("%s: could not allocate rx mbuf\n",
849                     device_get_nameunit(sc->sc_dev));
850                 IFNET_STAT_INC(ifp, ierrors, 1);
851                 goto skip;
852         }
853
854         m = data->m;
855         data->m = NULL;
856         data->buf = NULL;
857
858         lwkt_serialize_enter(ifp->if_serializer);
859
860         /* finalize mbuf */
861         m->m_pkthdr.rcvif = ifp;
862         m->m_data = (caddr_t)(desc + 1);
863         m->m_pkthdr.len = m->m_len = (le32toh(desc->flags) >> 16) & 0xfff;
864
865         rssi = rum_get_rssi(sc, desc->rssi);
866
867         wh = mtod(m, struct ieee80211_frame_min *);
868         ni = ieee80211_find_rxnode(ic, wh);
869
870         /* Error happened during RSSI conversion. */
871         if (rssi < 0)
872                 rssi = ni->ni_rssi;
873
874         if (sc->sc_drvbpf != NULL) {
875                 struct rum_rx_radiotap_header *tap = &sc->sc_rxtap;
876
877                 tap->wr_flags = 0;
878                 tap->wr_rate = rum_rxrate(desc);
879                 tap->wr_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq);
880                 tap->wr_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
881                 tap->wr_antenna = sc->rx_ant;
882                 tap->wr_antsignal = rssi;
883
884                 bpf_ptap(sc->sc_drvbpf, m, tap, sc->sc_rxtap_len);
885         }
886
887         /* send the frame to the 802.11 layer */
888         ieee80211_input(ic, m, ni, rssi, 0);
889
890         /* node is no longer needed */
891         ieee80211_free_node(ni);
892
893         if (!ifq_is_oactive(&ifp->if_snd))
894                 ifp->if_start(ifp);
895
896         lwkt_serialize_exit(ifp->if_serializer);
897
898         data->m = mnew;
899         data->buf = mtod(data->m, uint8_t *);
900
901         DPRINTFN(15, ("rx done\n"));
902
903 skip:   /* setup a new transfer */
904         bzero(data->buf, sizeof(struct rum_rx_desc));
905         usbd_setup_xfer(xfer, sc->sc_rx_pipeh, data, data->buf, MCLBYTES,
906             USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rum_rxeof);
907         usbd_transfer(xfer);
908
909         crit_exit();
910 }
911
912 /*
913  * This function is only used by the Rx radiotap code. It returns the rate at
914  * which a given frame was received.
915  */
916 static uint8_t
917 rum_rxrate(struct rum_rx_desc *desc)
918 {
919         if (le32toh(desc->flags) & RT2573_RX_OFDM) {
920                 /* reverse function of rum_plcp_signal */
921                 switch (desc->rate) {
922                 case 0xb:       return 12;
923                 case 0xf:       return 18;
924                 case 0xa:       return 24;
925                 case 0xe:       return 36;
926                 case 0x9:       return 48;
927                 case 0xd:       return 72;
928                 case 0x8:       return 96;
929                 case 0xc:       return 108;
930                 }
931         } else {
932                 if (desc->rate == 10)
933                         return 2;
934                 if (desc->rate == 20)
935                         return 4;
936                 if (desc->rate == 55)
937                         return 11;
938                 if (desc->rate == 110)
939                         return 22;
940         }
941         return 2;       /* should not get there */
942 }
943
944 static uint8_t
945 rum_plcp_signal(int rate)
946 {
947         switch (rate) {
948         /* CCK rates (returned values are device-dependent) */
949         case 2:         return 0x0;
950         case 4:         return 0x1;
951         case 11:        return 0x2;
952         case 22:        return 0x3;
953
954         /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
955         case 12:        return 0xb;
956         case 18:        return 0xf;
957         case 24:        return 0xa;
958         case 36:        return 0xe;
959         case 48:        return 0x9;
960         case 72:        return 0xd;
961         case 96:        return 0x8;
962         case 108:       return 0xc;
963
964         /* unsupported rates (should not get there) */
965         default:        return 0xff;
966         }
967 }
968
969 static void
970 rum_setup_tx_desc(struct rum_softc *sc, struct rum_tx_desc *desc,
971     uint32_t flags, uint16_t xflags, int len, int rate)
972 {
973         struct ieee80211com *ic = &sc->sc_ic;
974         uint16_t plcp_length;
975         int remainder;
976
977         desc->flags = htole32(flags);
978         desc->flags |= htole32(len << 16);
979
980         desc->xflags = htole16(xflags);
981
982         desc->wme = htole16(
983             RT2573_QID(0) |
984             RT2573_AIFSN(2) |
985             RT2573_LOGCWMIN(4) |
986             RT2573_LOGCWMAX(10));
987
988         /* setup PLCP fields */
989         desc->plcp_signal  = rum_plcp_signal(rate);
990         desc->plcp_service = 4;
991
992         len += IEEE80211_CRC_LEN;
993         if (RUM_RATE_IS_OFDM(rate)) {
994                 desc->flags |= htole32(RT2573_TX_OFDM);
995
996                 plcp_length = len & 0xfff;
997                 desc->plcp_length_hi = plcp_length >> 6;
998                 desc->plcp_length_lo = plcp_length & 0x3f;
999         } else {
1000                 plcp_length = (16 * len + rate - 1) / rate;
1001                 if (rate == 22) {
1002                         remainder = (16 * len) % 22;
1003                         if (remainder != 0 && remainder < 7)
1004                                 desc->plcp_service |= RT2573_PLCP_LENGEXT;
1005                 }
1006                 desc->plcp_length_hi = plcp_length >> 8;
1007                 desc->plcp_length_lo = plcp_length & 0xff;
1008
1009                 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1010                         desc->plcp_signal |= 0x08;
1011         }
1012         desc->flags |= htole32(RT2573_TX_VALID);
1013 }
1014
1015 #define RUM_TX_TIMEOUT  5000
1016
1017 static int
1018 rum_tx_data(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1019 {
1020         struct ieee80211com *ic = &sc->sc_ic;
1021         struct ifnet *ifp = &ic->ic_if;
1022         struct rum_tx_desc *desc;
1023         struct rum_tx_data *data;
1024         struct ieee80211_frame *wh;
1025         uint32_t flags = 0;
1026         uint16_t dur;
1027         usbd_status error;
1028         int xferlen, rate, rateidx;
1029
1030         wh = mtod(m0, struct ieee80211_frame *);
1031
1032         if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1033                 if (ieee80211_crypto_encap(ic, ni, m0) == NULL) {
1034                         m_freem(m0);
1035                         return ENOBUFS;
1036                 }
1037
1038                 /* packet header may have moved, reset our local pointer */
1039                 wh = mtod(m0, struct ieee80211_frame *);
1040         }
1041
1042         /* pickup a rate */
1043         if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
1044             IEEE80211_FC0_TYPE_MGT) {
1045                 /* mgmt frames are sent at the lowest available bit-rate */
1046                 rateidx = 0;
1047         } else {
1048                 ieee80211_ratectl_findrate(ni, m0->m_pkthdr.len, &rateidx, 1);
1049         }
1050         rate = IEEE80211_RS_RATE(&ni->ni_rates, rateidx);
1051
1052         data = &sc->tx_data[0];
1053         desc = (struct rum_tx_desc *)data->buf;
1054
1055         data->m = m0;
1056         data->ni = ni;
1057
1058         if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1059                 flags |= RT2573_TX_ACK;
1060
1061                 dur = ieee80211_txtime(ni, RUM_ACK_SIZE,
1062                         ieee80211_ack_rate(ni, rate), ic->ic_flags) +
1063                         sc->sc_sifs;
1064                 *(uint16_t *)wh->i_dur = htole16(dur);
1065
1066                 /* tell hardware to set timestamp in probe responses */
1067                 if ((wh->i_fc[0] &
1068                     (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
1069                     (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
1070                         flags |= RT2573_TX_TIMESTAMP;
1071         }
1072
1073         if (sc->sc_drvbpf != NULL) {
1074                 struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
1075
1076                 tap->wt_flags = 0;
1077                 tap->wt_rate = rate;
1078                 tap->wt_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq);
1079                 tap->wt_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
1080                 tap->wt_antenna = sc->tx_ant;
1081
1082                 bpf_ptap(sc->sc_drvbpf, m0, tap, sc->sc_txtap_len);
1083         }
1084
1085         m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RT2573_TX_DESC_SIZE);
1086         rum_setup_tx_desc(sc, desc, flags, 0, m0->m_pkthdr.len, rate);
1087
1088         /* Align end on a 4-bytes boundary */
1089         xferlen = roundup(RT2573_TX_DESC_SIZE + m0->m_pkthdr.len, 4);
1090
1091         /*
1092          * No space left in the last URB to store the extra 4 bytes, force
1093          * sending of another URB.
1094          */
1095         if ((xferlen % 64) == 0)
1096                 xferlen += 4;
1097
1098         DPRINTFN(10, ("sending frame len=%u rate=%u xfer len=%u\n",
1099             m0->m_pkthdr.len + RT2573_TX_DESC_SIZE, rate, xferlen));
1100
1101         lwkt_serialize_exit(ifp->if_serializer);
1102
1103         usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf, xferlen,
1104             USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RUM_TX_TIMEOUT, rum_txeof);
1105
1106         error = usbd_transfer(data->xfer);
1107         if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS) {
1108                 m_freem(m0);
1109                 data->m = NULL;
1110                 data->ni = NULL;
1111         } else {
1112                 sc->tx_queued++;
1113                 error = 0;
1114         }
1115
1116         lwkt_serialize_enter(ifp->if_serializer);
1117         return error;
1118 }
1119
1120 static void
1121 rum_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
1122 {
1123         struct rum_softc *sc = ifp->if_softc;
1124         struct ieee80211com *ic = &sc->sc_ic;
1125
1126         ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
1127         ASSERT_SERIALIZED(ifp->if_serializer);
1128
1129         if (sc->sc_stopped) {
1130                 ifq_purge(&ifp->if_snd);
1131                 return;
1132         }
1133
1134         crit_enter();
1135
1136         if ((ifp->if_flags & IFF_RUNNING) == 0 ||
1137             ifq_is_oactive(&ifp->if_snd)) {
1138                 crit_exit();
1139                 return;
1140         }
1141
1142         for (;;) {
1143                 struct ieee80211_node *ni;
1144                 struct mbuf *m0;
1145
1146                 if (!IF_QEMPTY(&ic->ic_mgtq)) {
1147                         if (sc->tx_queued >= RT2573_TX_LIST_COUNT) {
1148                                 ifq_set_oactive(&ifp->if_snd);
1149                                 break;
1150                         }
1151                         IF_DEQUEUE(&ic->ic_mgtq, m0);
1152
1153                         ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
1154                         m0->m_pkthdr.rcvif = NULL;
1155
1156                         BPF_MTAP(ifp, m0);
1157
1158                         if (rum_tx_data(sc, m0, ni) != 0) {
1159                                 ieee80211_free_node(ni);
1160                                 break;
1161                         }
1162                 } else {
1163                         struct ether_header *eh;
1164
1165                         if (ic->ic_state != IEEE80211_S_RUN) {
1166                                 ifq_purge(&ifp->if_snd);
1167                                 break;
1168                         }
1169
1170                         if (sc->tx_queued >= RT2573_TX_LIST_COUNT) {
1171                                 ifq_set_oactive(&ifp->if_snd);
1172                                 break;
1173                         }
1174
1175                         m0 = ifq_dequeue(&ifp->if_snd);
1176                         if (m0 == NULL)
1177                                 break;
1178
1179                         if (m0->m_len < sizeof(struct ether_header)) {
1180                                 m0 = m_pullup(m0, sizeof(struct ether_header));
1181                                 if (m0 == NULL) {
1182                                         IFNET_STAT_INC(ifp, oerrors, 1);
1183                                         continue;
1184                                 }
1185                         }
1186                         eh = mtod(m0, struct ether_header *);
1187
1188                         ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1189                         if (ni == NULL) {
1190                                 m_freem(m0);
1191                                 continue;
1192                         }
1193
1194                         BPF_MTAP(ifp, m0);
1195
1196                         m0 = ieee80211_encap(ic, m0, ni);
1197                         if (m0 == NULL) {
1198                                 ieee80211_free_node(ni);
1199                                 continue;
1200                         }
1201
1202                         if (ic->ic_rawbpf != NULL)
1203                                 bpf_mtap(ic->ic_rawbpf, m0);
1204
1205                         if (rum_tx_data(sc, m0, ni) != 0) {
1206                                 ieee80211_free_node(ni);
1207                                 IFNET_STAT_INC(ifp, oerrors, 1);
1208                                 break;
1209                         }
1210                 }
1211
1212                 sc->sc_tx_timer = 5;
1213                 ifp->if_timer = 1;
1214         }
1215
1216         crit_exit();
1217 }
1218
1219 static void
1220 rum_watchdog(struct ifnet *ifp)
1221 {
1222         struct rum_softc *sc = ifp->if_softc;
1223
1224         ASSERT_SERIALIZED(ifp->if_serializer);
1225
1226         crit_enter();
1227
1228         ifp->if_timer = 0;
1229
1230         if (sc->sc_tx_timer > 0) {
1231                 if (--sc->sc_tx_timer == 0) {
1232                         kprintf("%s: device timeout\n", device_get_nameunit(sc->sc_dev));
1233                         /*rum_init(sc); XXX needs a process context! */
1234                         IFNET_STAT_INC(ifp, oerrors, 1);
1235
1236                         crit_exit();
1237                         return;
1238                 }
1239                 ifp->if_timer = 1;
1240         }
1241
1242         ieee80211_watchdog(&sc->sc_ic);
1243
1244         crit_exit();
1245 }
1246
1247 static int
1248 rum_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
1249 {
1250         struct rum_softc *sc = ifp->if_softc;
1251         struct ieee80211com *ic = &sc->sc_ic;
1252         int error = 0;
1253
1254         ASSERT_SERIALIZED(ifp->if_serializer);
1255
1256         crit_enter();
1257
1258         switch (cmd) {
1259         case SIOCSIFFLAGS:
1260                 if (ifp->if_flags & IFF_UP) {
1261                         if (ifp->if_flags & IFF_RUNNING) {
1262                                 lwkt_serialize_exit(ifp->if_serializer);
1263                                 rum_update_promisc(sc);
1264                                 lwkt_serialize_enter(ifp->if_serializer);
1265                         } else {
1266                                 rum_init(sc);
1267                         }
1268                 } else {
1269                         if (ifp->if_flags & IFF_RUNNING)
1270                                 rum_stop(sc);
1271                 }
1272                 break;
1273         default:
1274                 error = ieee80211_ioctl(ic, cmd, data, cr);
1275                 break;
1276         }
1277
1278         if (error == ENETRESET) {
1279                 struct ieee80211req *ireq = (struct ieee80211req *)data;
1280
1281                 if (cmd == SIOCS80211 &&
1282                     ireq->i_type == IEEE80211_IOC_CHANNEL &&
1283                     ic->ic_opmode == IEEE80211_M_MONITOR) {
1284                         /*
1285                          * This allows for fast channel switching in monitor
1286                          * mode (used by kismet). In IBSS mode, we must
1287                          * explicitly reset the interface to generate a new
1288                          * beacon frame.
1289                          */
1290                         lwkt_serialize_exit(ifp->if_serializer);
1291                         rum_set_chan(sc, ic->ic_ibss_chan);
1292                         lwkt_serialize_enter(ifp->if_serializer);
1293                 } else if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
1294                            (IFF_UP | IFF_RUNNING)) {
1295                         rum_init(sc);
1296                 }
1297                 error = 0;
1298         }
1299
1300         crit_exit();
1301         return error;
1302 }
1303
1304 static void
1305 rum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len)
1306 {
1307         usb_device_request_t req;
1308         usbd_status error;
1309
1310         req.bmRequestType = UT_READ_VENDOR_DEVICE;
1311         req.bRequest = RT2573_READ_EEPROM;
1312         USETW(req.wValue, 0);
1313         USETW(req.wIndex, addr);
1314         USETW(req.wLength, len);
1315
1316         error = usbd_do_request(sc->sc_udev, &req, buf);
1317         if (error != 0) {
1318                 kprintf("%s: could not read EEPROM: %s\n",
1319                     device_get_nameunit(sc->sc_dev), usbd_errstr(error));
1320         }
1321 }
1322
1323 static uint32_t
1324 rum_read(struct rum_softc *sc, uint16_t reg)
1325 {
1326         uint32_t val;
1327
1328         rum_read_multi(sc, reg, &val, sizeof val);
1329
1330         return le32toh(val);
1331 }
1332
1333 static void
1334 rum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len)
1335 {
1336         usb_device_request_t req;
1337         usbd_status error;
1338
1339         req.bmRequestType = UT_READ_VENDOR_DEVICE;
1340         req.bRequest = RT2573_READ_MULTI_MAC;
1341         USETW(req.wValue, 0);
1342         USETW(req.wIndex, reg);
1343         USETW(req.wLength, len);
1344
1345         error = usbd_do_request(sc->sc_udev, &req, buf);
1346         if (error != 0) {
1347                 kprintf("%s: could not multi read MAC register: %s\n",
1348                     device_get_nameunit(sc->sc_dev), usbd_errstr(error));
1349         }
1350 }
1351
1352 static void
1353 rum_write(struct rum_softc *sc, uint16_t reg, uint32_t val)
1354 {
1355         uint32_t tmp = htole32(val);
1356
1357         rum_write_multi(sc, reg, &tmp, sizeof tmp);
1358 }
1359
1360 static void
1361 rum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len)
1362 {
1363         usb_device_request_t req;
1364         usbd_status error;
1365
1366         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1367         req.bRequest = RT2573_WRITE_MULTI_MAC;
1368         USETW(req.wValue, 0);
1369         USETW(req.wIndex, reg);
1370         USETW(req.wLength, len);
1371
1372         error = usbd_do_request(sc->sc_udev, &req, buf);
1373         if (error != 0) {
1374                 kprintf("%s: could not multi write MAC register: %s\n",
1375                     device_get_nameunit(sc->sc_dev), usbd_errstr(error));
1376         }
1377 }
1378
1379 static void
1380 rum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val)
1381 {
1382         uint32_t tmp;
1383         int ntries;
1384
1385         for (ntries = 0; ntries < 5; ntries++) {
1386                 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1387                         break;
1388         }
1389         if (ntries == 5) {
1390                 kprintf("%s: could not write to BBP\n", device_get_nameunit(sc->sc_dev));
1391                 return;
1392         }
1393
1394         tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val;
1395         rum_write(sc, RT2573_PHY_CSR3, tmp);
1396 }
1397
1398 static uint8_t
1399 rum_bbp_read(struct rum_softc *sc, uint8_t reg)
1400 {
1401         uint32_t val;
1402         int ntries;
1403
1404         for (ntries = 0; ntries < 5; ntries++) {
1405                 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1406                         break;
1407         }
1408         if (ntries == 5) {
1409                 kprintf("%s: could not read BBP\n", device_get_nameunit(sc->sc_dev));
1410                 return 0;
1411         }
1412
1413         val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8;
1414         rum_write(sc, RT2573_PHY_CSR3, val);
1415
1416         for (ntries = 0; ntries < 100; ntries++) {
1417                 val = rum_read(sc, RT2573_PHY_CSR3);
1418                 if (!(val & RT2573_BBP_BUSY))
1419                         return val & 0xff;
1420                 DELAY(1);
1421         }
1422
1423         kprintf("%s: could not read BBP\n", device_get_nameunit(sc->sc_dev));
1424         return 0;
1425 }
1426
1427 static void
1428 rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val)
1429 {
1430         uint32_t tmp;
1431         int ntries;
1432
1433         for (ntries = 0; ntries < 5; ntries++) {
1434                 if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY))
1435                         break;
1436         }
1437         if (ntries == 5) {
1438                 kprintf("%s: could not write to RF\n", device_get_nameunit(sc->sc_dev));
1439                 return;
1440         }
1441
1442         tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 |
1443             (reg & 3);
1444         rum_write(sc, RT2573_PHY_CSR4, tmp);
1445
1446         /* remember last written value in sc */
1447         sc->rf_regs[reg] = val;
1448
1449         DPRINTFN(15, ("RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff));
1450 }
1451
1452 static void
1453 rum_select_antenna(struct rum_softc *sc)
1454 {
1455         uint8_t bbp4, bbp77;
1456         uint32_t tmp;
1457
1458         bbp4  = rum_bbp_read(sc, 4);
1459         bbp77 = rum_bbp_read(sc, 77);
1460
1461         /* TBD */
1462
1463         /* make sure Rx is disabled before switching antenna */
1464         tmp = rum_read(sc, RT2573_TXRX_CSR0);
1465         rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
1466
1467         rum_bbp_write(sc,  4, bbp4);
1468         rum_bbp_write(sc, 77, bbp77);
1469
1470         rum_write(sc, RT2573_TXRX_CSR0, tmp);
1471 }
1472
1473 /*
1474  * Enable multi-rate retries for frames sent at OFDM rates.
1475  * In 802.11b/g mode, allow fallback to CCK rates.
1476  */
1477 static void
1478 rum_enable_mrr(struct rum_softc *sc)
1479 {
1480         struct ieee80211com *ic = &sc->sc_ic;
1481         uint32_t tmp;
1482
1483         tmp = rum_read(sc, RT2573_TXRX_CSR4);
1484
1485         tmp &= ~RT2573_MRR_CCK_FALLBACK;
1486         if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan))
1487                 tmp |= RT2573_MRR_CCK_FALLBACK;
1488         tmp |= RT2573_MRR_ENABLED;
1489
1490         rum_write(sc, RT2573_TXRX_CSR4, tmp);
1491 }
1492
1493 static void
1494 rum_set_txpreamble(struct rum_softc *sc)
1495 {
1496         uint32_t tmp;
1497
1498         tmp = rum_read(sc, RT2573_TXRX_CSR4);
1499
1500         tmp &= ~RT2573_SHORT_PREAMBLE;
1501         if (sc->sc_ic.ic_flags & IEEE80211_F_SHPREAMBLE)
1502                 tmp |= RT2573_SHORT_PREAMBLE;
1503
1504         rum_write(sc, RT2573_TXRX_CSR4, tmp);
1505 }
1506
1507 static void
1508 rum_set_basicrates(struct rum_softc *sc)
1509 {
1510         struct ieee80211com *ic = &sc->sc_ic;
1511
1512         /* update basic rate set */
1513         if (ic->ic_curmode == IEEE80211_MODE_11B) {
1514                 /* 11b basic rates: 1, 2Mbps */
1515                 rum_write(sc, RT2573_TXRX_CSR5, 0x3);
1516         } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bss->ni_chan)) {
1517                 /* 11a basic rates: 6, 12, 24Mbps */
1518                 rum_write(sc, RT2573_TXRX_CSR5, 0x150);
1519         } else {
1520                 /* 11g basic rates: 1, 2, 5.5, 11, 6, 12, 24Mbps */
1521                 rum_write(sc, RT2573_TXRX_CSR5, 0x15f);
1522         }
1523 }
1524
1525 /*
1526  * Reprogram MAC/BBP to switch to a new band.  Values taken from the reference
1527  * driver.
1528  */
1529 static void
1530 rum_select_band(struct rum_softc *sc, struct ieee80211_channel *c)
1531 {
1532         uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104;
1533         uint32_t tmp;
1534
1535         /* update all BBP registers that depend on the band */
1536         bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c;
1537         bbp35 = 0x50; bbp97 = 0x48; bbp98  = 0x48;
1538         if (IEEE80211_IS_CHAN_5GHZ(c)) {
1539                 bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c;
1540                 bbp35 += 0x10; bbp97 += 0x10; bbp98  += 0x10;
1541         }
1542         if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1543             (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1544                 bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10;
1545         }
1546
1547         sc->bbp17 = bbp17;
1548         rum_bbp_write(sc,  17, bbp17);
1549         rum_bbp_write(sc,  96, bbp96);
1550         rum_bbp_write(sc, 104, bbp104);
1551
1552         if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1553             (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1554                 rum_bbp_write(sc, 75, 0x80);
1555                 rum_bbp_write(sc, 86, 0x80);
1556                 rum_bbp_write(sc, 88, 0x80);
1557         }
1558
1559         rum_bbp_write(sc, 35, bbp35);
1560         rum_bbp_write(sc, 97, bbp97);
1561         rum_bbp_write(sc, 98, bbp98);
1562
1563         tmp = rum_read(sc, RT2573_PHY_CSR0);
1564         tmp &= ~(RT2573_PA_PE_2GHZ | RT2573_PA_PE_5GHZ);
1565         if (IEEE80211_IS_CHAN_2GHZ(c))
1566                 tmp |= RT2573_PA_PE_2GHZ;
1567         else
1568                 tmp |= RT2573_PA_PE_5GHZ;
1569         rum_write(sc, RT2573_PHY_CSR0, tmp);
1570 }
1571
1572 static void
1573 rum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c)
1574 {
1575         struct ieee80211com *ic = &sc->sc_ic;
1576         const struct rfprog *rfprog;
1577         uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT;
1578         int8_t power;
1579         u_int i, chan;
1580
1581         chan = ieee80211_chan2ieee(ic, c);
1582         if (chan == 0 || chan == IEEE80211_CHAN_ANY)
1583                 return;
1584
1585         /* select the appropriate RF settings based on what EEPROM says */
1586         rfprog = (sc->rf_rev == RT2573_RF_5225 ||
1587                   sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226;
1588
1589         /* find the settings for this channel (we know it exists) */
1590         for (i = 0; rfprog[i].chan != chan; i++)
1591                 ;       /* EMPTY */
1592
1593         power = sc->txpow[i];
1594         if (power < 0) {
1595                 bbp94 += power;
1596                 power = 0;
1597         } else if (power > 31) {
1598                 bbp94 += power - 31;
1599                 power = 31;
1600         }
1601
1602         /*
1603          * If we are switching from the 2GHz band to the 5GHz band or
1604          * vice-versa, BBP registers need to be reprogrammed.
1605          */
1606         if (c->ic_flags != sc->sc_curchan->ic_flags) {
1607                 rum_select_band(sc, c);
1608                 rum_select_antenna(sc);
1609         }
1610         sc->sc_curchan = c;
1611
1612         rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1613         rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1614         rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1615         rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1616
1617         rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1618         rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1619         rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7 | 1);
1620         rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1621
1622         rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1623         rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1624         rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1625         rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1626
1627         DELAY(10);
1628
1629         /* enable smart mode for MIMO-capable RFs */
1630         bbp3 = rum_bbp_read(sc, 3);
1631
1632         if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527)
1633                 bbp3 &= ~RT2573_SMART_MODE;
1634         else
1635                 bbp3 |= RT2573_SMART_MODE;
1636
1637         rum_bbp_write(sc, 3, bbp3);
1638
1639         if (bbp94 != RT2573_BBPR94_DEFAULT)
1640                 rum_bbp_write(sc, 94, bbp94);
1641
1642         sc->sc_sifs = IEEE80211_IS_CHAN_5GHZ(c) ? IEEE80211_DUR_OFDM_SIFS
1643                                                 : IEEE80211_DUR_SIFS;
1644 }
1645
1646 /*
1647  * Enable TSF synchronization and tell h/w to start sending beacons for IBSS
1648  * and HostAP operating modes.
1649  */
1650 static void
1651 rum_enable_tsf_sync(struct rum_softc *sc)
1652 {
1653         struct ieee80211com *ic = &sc->sc_ic;
1654         uint32_t tmp;
1655
1656         if (ic->ic_opmode != IEEE80211_M_STA) {
1657                 /*
1658                  * Change default 16ms TBTT adjustment to 8ms.
1659                  * Must be done before enabling beacon generation.
1660                  */
1661                 rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8);
1662         }
1663
1664         tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000;
1665
1666         /* set beacon interval (in 1/16ms unit) */
1667         tmp |= ic->ic_bss->ni_intval * 16;
1668
1669         tmp |= RT2573_TSF_TICKING | RT2573_ENABLE_TBTT;
1670         if (ic->ic_opmode == IEEE80211_M_STA)
1671                 tmp |= RT2573_TSF_MODE(1);
1672         else
1673                 tmp |= RT2573_TSF_MODE(2) | RT2573_GENERATE_BEACON;
1674
1675         rum_write(sc, RT2573_TXRX_CSR9, tmp);
1676 }
1677
1678 static void
1679 rum_update_slot(struct rum_softc *sc)
1680 {
1681         struct ieee80211com *ic = &sc->sc_ic;
1682         uint8_t slottime;
1683         uint32_t tmp;
1684
1685         slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
1686
1687         tmp = rum_read(sc, RT2573_MAC_CSR9);
1688         tmp = (tmp & ~0xff) | slottime;
1689         rum_write(sc, RT2573_MAC_CSR9, tmp);
1690
1691         DPRINTF(("setting slot time to %uus\n", slottime));
1692 }
1693
1694 static void
1695 rum_set_bssid(struct rum_softc *sc, const uint8_t *bssid)
1696 {
1697         uint32_t tmp;
1698
1699         tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24;
1700         rum_write(sc, RT2573_MAC_CSR4, tmp);
1701
1702         tmp = bssid[4] | bssid[5] << 8 | RT2573_ONE_BSSID << 16;
1703         rum_write(sc, RT2573_MAC_CSR5, tmp);
1704 }
1705
1706 static void
1707 rum_set_macaddr(struct rum_softc *sc, const uint8_t *addr)
1708 {
1709         uint32_t tmp;
1710
1711         tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24;
1712         rum_write(sc, RT2573_MAC_CSR2, tmp);
1713
1714         tmp = addr[4] | addr[5] << 8 | 0xff << 16;
1715         rum_write(sc, RT2573_MAC_CSR3, tmp);
1716 }
1717
1718 static void
1719 rum_update_promisc(struct rum_softc *sc)
1720 {
1721         struct ifnet *ifp = &sc->sc_ic.ic_if;
1722         uint32_t tmp;
1723
1724         tmp = rum_read(sc, RT2573_TXRX_CSR0);
1725
1726         tmp &= ~RT2573_DROP_NOT_TO_ME;
1727         if (!(ifp->if_flags & IFF_PROMISC))
1728                 tmp |= RT2573_DROP_NOT_TO_ME;
1729
1730         rum_write(sc, RT2573_TXRX_CSR0, tmp);
1731
1732         DPRINTF(("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
1733             "entering" : "leaving"));
1734 }
1735
1736 static const char *
1737 rum_get_rf(int rev)
1738 {
1739         switch (rev) {
1740         case RT2573_RF_2527:    return "RT2527 (MIMO XR)";
1741         case RT2573_RF_2528:    return "RT2528";
1742         case RT2573_RF_5225:    return "RT5225 (MIMO XR)";
1743         case RT2573_RF_5226:    return "RT5226";
1744         default:                return "unknown";
1745         }
1746 }
1747
1748 static void
1749 rum_read_eeprom(struct rum_softc *sc)
1750 {
1751         struct ieee80211com *ic = &sc->sc_ic;
1752         uint16_t val;
1753 #ifdef RUM_DEBUG
1754         int i;
1755 #endif
1756
1757         /* read MAC/BBP type */
1758         rum_eeprom_read(sc, RT2573_EEPROM_MACBBP, &val, 2);
1759         sc->macbbp_rev = le16toh(val);
1760
1761         /* read MAC address */
1762         rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, ic->ic_myaddr, 6);
1763
1764         rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2);
1765         val = le16toh(val);
1766         sc->rf_rev =   (val >> 11) & 0x1f;
1767         sc->hw_radio = (val >> 10) & 0x1;
1768         sc->rx_ant =   (val >> 4)  & 0x3;
1769         sc->tx_ant =   (val >> 2)  & 0x3;
1770         sc->nb_ant =   val & 0x3;
1771
1772         DPRINTF(("RF revision=%d\n", sc->rf_rev));
1773
1774         rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2);
1775         val = le16toh(val);
1776         sc->ext_5ghz_lna = (val >> 6) & 0x1;
1777         sc->ext_2ghz_lna = (val >> 4) & 0x1;
1778
1779         DPRINTF(("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n",
1780             sc->ext_2ghz_lna, sc->ext_5ghz_lna));
1781
1782         rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2);
1783         val = le16toh(val);
1784         if ((val & 0xff) != 0xff)
1785                 sc->rssi_2ghz_corr = (int8_t)(val & 0xff);      /* signed */
1786
1787         /* Only [-10, 10] is valid */
1788         if (sc->rssi_2ghz_corr < -10 || sc->rssi_2ghz_corr > 10)
1789                 sc->rssi_2ghz_corr = 0;
1790
1791         rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2);
1792         val = le16toh(val);
1793         if ((val & 0xff) != 0xff)
1794                 sc->rssi_5ghz_corr = (int8_t)(val & 0xff);      /* signed */
1795
1796         /* Only [-10, 10] is valid */
1797         if (sc->rssi_5ghz_corr < -10 || sc->rssi_5ghz_corr > 10)
1798                 sc->rssi_5ghz_corr = 0;
1799
1800         if (sc->ext_2ghz_lna)
1801                 sc->rssi_2ghz_corr -= 14;
1802         if (sc->ext_5ghz_lna)
1803                 sc->rssi_5ghz_corr -= 14;
1804
1805         DPRINTF(("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n",
1806             sc->rssi_2ghz_corr, sc->rssi_5ghz_corr));
1807
1808         rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2);
1809         val = le16toh(val);
1810         if ((val & 0xff) != 0xff)
1811                 sc->rffreq = val & 0xff;
1812
1813         DPRINTF(("RF freq=%d\n", sc->rffreq));
1814
1815         /* read Tx power for all a/b/g channels */
1816         rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14);
1817         /* XXX default Tx power for 802.11a channels */
1818         memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14);
1819 #ifdef RUM_DEBUG
1820         for (i = 0; i < 14; i++)
1821                 DPRINTF(("Channel=%d Tx power=%d\n", i + 1,  sc->txpow[i]));
1822 #endif
1823
1824         /* read default values for BBP registers */
1825         rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
1826 #ifdef RUM_DEBUG
1827         for (i = 0; i < 14; i++) {
1828                 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
1829                         continue;
1830                 DPRINTF(("BBP R%d=%02x\n", sc->bbp_prom[i].reg,
1831                     sc->bbp_prom[i].val));
1832         }
1833 #endif
1834 }
1835
1836 static int
1837 rum_bbp_init(struct rum_softc *sc)
1838 {
1839 #define N(a)    (sizeof (a) / sizeof ((a)[0]))
1840         int i, ntries;
1841         uint8_t val;
1842
1843         /* wait for BBP to be ready */
1844         for (ntries = 0; ntries < 100; ntries++) {
1845                 val = rum_bbp_read(sc, 0);
1846                 if (val != 0 && val != 0xff)
1847                         break;
1848                 DELAY(1000);
1849         }
1850         if (ntries == 100) {
1851                 kprintf("%s: timeout waiting for BBP\n",
1852                     device_get_nameunit(sc->sc_dev));
1853                 return EIO;
1854         }
1855
1856         /* initialize BBP registers to default values */
1857         for (i = 0; i < N(rum_def_bbp); i++)
1858                 rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val);
1859
1860         /* write vendor-specific BBP values (from EEPROM) */
1861         for (i = 0; i < 16; i++) {
1862                 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
1863                         continue;
1864                 rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
1865         }
1866
1867         return 0;
1868 #undef N
1869 }
1870
1871 static void
1872 rum_init(void *xsc)
1873 {
1874 #define N(a)    (sizeof(a) / sizeof((a)[0]))
1875         struct rum_softc *sc = xsc;
1876         struct ieee80211com *ic = &sc->sc_ic;
1877         struct ifnet *ifp = &ic->ic_if;
1878         struct rum_rx_data *data;
1879         uint32_t tmp;
1880         usbd_status usb_err;
1881         int i, ntries, error;
1882
1883         ASSERT_SERIALIZED(ifp->if_serializer);
1884
1885         crit_enter();
1886
1887         rum_stop(sc);
1888         sc->sc_stopped = 0;
1889
1890         lwkt_serialize_exit(ifp->if_serializer);
1891
1892         /* initialize MAC registers to default values */
1893         for (i = 0; i < N(rum_def_mac); i++)
1894                 rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val);
1895
1896         /* set host ready */
1897         rum_write(sc, RT2573_MAC_CSR1, 3);
1898         rum_write(sc, RT2573_MAC_CSR1, 0);
1899
1900         /* wait for BBP/RF to wakeup */
1901         for (ntries = 0; ntries < 1000; ntries++) {
1902                 if (rum_read(sc, RT2573_MAC_CSR12) & 8)
1903                         break;
1904                 rum_write(sc, RT2573_MAC_CSR12, 4);     /* force wakeup */
1905                 DELAY(1000);
1906         }
1907         if (ntries == 1000) {
1908                 kprintf("%s: timeout waiting for BBP/RF to wakeup\n",
1909                         device_get_nameunit(sc->sc_dev));
1910                 error = ETIMEDOUT;
1911                 goto fail;
1912         }
1913
1914         error = rum_bbp_init(sc);
1915         if (error)
1916                 goto fail;
1917
1918         /* select default channel */
1919         sc->sc_curchan = ic->ic_curchan = ic->ic_ibss_chan;
1920
1921         rum_select_band(sc, sc->sc_curchan);
1922         rum_select_antenna(sc);
1923         rum_set_chan(sc, sc->sc_curchan);
1924
1925         /* clear STA registers */
1926         rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
1927
1928         IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
1929         rum_set_macaddr(sc, ic->ic_myaddr);
1930
1931         /* initialize ASIC */
1932         rum_write(sc, RT2573_MAC_CSR1, 4);
1933
1934         /*
1935          * Allocate xfer for AMRR statistics requests.
1936          */
1937         sc->stats_xfer = usbd_alloc_xfer(sc->sc_udev);
1938         if (sc->stats_xfer == NULL) {
1939                 kprintf("%s: could not allocate AMRR xfer\n",
1940                         device_get_nameunit(sc->sc_dev));
1941                 error = ENOMEM;
1942                 goto fail;
1943         }
1944
1945         /*
1946          * Open Tx and Rx USB bulk pipes.
1947          */
1948         usb_err = usbd_open_pipe(sc->sc_iface, sc->sc_tx_no, USBD_EXCLUSIVE_USE,
1949                                  &sc->sc_tx_pipeh);
1950         if (usb_err != USBD_NORMAL_COMPLETION) {
1951                 kprintf("%s: could not open Tx pipe: %s\n",
1952                         device_get_nameunit(sc->sc_dev), usbd_errstr(usb_err));
1953                 error = EIO;
1954                 goto fail;
1955         }
1956
1957         usb_err = usbd_open_pipe(sc->sc_iface, sc->sc_rx_no, USBD_EXCLUSIVE_USE,
1958                                  &sc->sc_rx_pipeh);
1959         if (usb_err != USBD_NORMAL_COMPLETION) {
1960                 kprintf("%s: could not open Rx pipe: %s\n",
1961                     device_get_nameunit(sc->sc_dev), usbd_errstr(usb_err));
1962                 error = EIO;
1963                 goto fail;
1964         }
1965
1966         /*
1967          * Allocate Tx and Rx xfer queues.
1968          */
1969         error = rum_alloc_tx_list(sc);
1970         if (error) {
1971                 kprintf("%s: could not allocate Tx list\n",
1972                         device_get_nameunit(sc->sc_dev));
1973                 goto fail;
1974         }
1975
1976         error = rum_alloc_rx_list(sc);
1977         if (error) {
1978                 kprintf("%s: could not allocate Rx list\n",
1979                         device_get_nameunit(sc->sc_dev));
1980                 goto fail;
1981         }
1982
1983         /*
1984          * Start up the receive pipe.
1985          */
1986         for (i = 0; i < RT2573_RX_LIST_COUNT; i++) {
1987                 data = &sc->rx_data[i];
1988
1989                 usbd_setup_xfer(data->xfer, sc->sc_rx_pipeh, data, data->buf,
1990                     MCLBYTES, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rum_rxeof);
1991                 usbd_transfer(data->xfer);
1992         }
1993
1994         /* update Rx filter */
1995         tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff;
1996
1997         tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR;
1998         if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1999                 tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR |
2000                        RT2573_DROP_ACKCTS;
2001                 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2002                         tmp |= RT2573_DROP_TODS;
2003                 if (!(ifp->if_flags & IFF_PROMISC))
2004                         tmp |= RT2573_DROP_NOT_TO_ME;
2005         }
2006         rum_write(sc, RT2573_TXRX_CSR0, tmp);
2007 fail:
2008         lwkt_serialize_enter(ifp->if_serializer);
2009
2010         if (error) {
2011                 rum_stop(sc);
2012         } else {
2013                 ifq_clr_oactive(&ifp->if_snd);
2014                 ifp->if_flags |= IFF_RUNNING;
2015
2016                 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2017                         if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
2018                                 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2019                 } else {
2020                         ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2021                 }
2022         }
2023
2024         crit_exit();
2025 #undef N
2026 }
2027
2028 static void
2029 rum_stop(struct rum_softc *sc)
2030 {
2031         struct ieee80211com *ic = &sc->sc_ic;
2032         struct ifnet *ifp = &ic->ic_if;
2033         uint32_t tmp;
2034
2035         ASSERT_SERIALIZED(ifp->if_serializer);
2036
2037         crit_enter();
2038
2039         ifp->if_flags &= ~IFF_RUNNING;
2040         ifq_clr_oactive(&ifp->if_snd);
2041         sc->sc_stopped = 1;
2042
2043         ieee80211_new_state(ic, IEEE80211_S_INIT, -1);  /* free all nodes */
2044
2045         sc->sc_tx_timer = 0;
2046         ifp->if_timer = 0;
2047
2048         lwkt_serialize_exit(ifp->if_serializer);
2049
2050         /* disable Rx */
2051         tmp = rum_read(sc, RT2573_TXRX_CSR0);
2052         rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
2053
2054         /* reset ASIC */
2055         rum_write(sc, RT2573_MAC_CSR1, 3);
2056         rum_write(sc, RT2573_MAC_CSR1, 0);
2057
2058         if (sc->stats_xfer != NULL) {
2059                 usbd_free_xfer(sc->stats_xfer);
2060                 sc->stats_xfer = NULL;
2061         }
2062
2063         if (sc->sc_rx_pipeh != NULL) {
2064                 usbd_abort_pipe(sc->sc_rx_pipeh);
2065                 usbd_close_pipe(sc->sc_rx_pipeh);
2066                 sc->sc_rx_pipeh = NULL;
2067         }
2068
2069         if (sc->sc_tx_pipeh != NULL) {
2070                 usbd_abort_pipe(sc->sc_tx_pipeh);
2071                 usbd_close_pipe(sc->sc_tx_pipeh);
2072                 sc->sc_tx_pipeh = NULL;
2073         }
2074
2075         lwkt_serialize_enter(ifp->if_serializer);
2076
2077         rum_free_rx_list(sc);
2078         rum_free_tx_list(sc);
2079
2080         crit_exit();
2081 }
2082
2083 static int
2084 rum_load_microcode(struct rum_softc *sc, const uint8_t *ucode, size_t size)
2085 {
2086         usb_device_request_t req;
2087         uint16_t reg = RT2573_MCU_CODE_BASE;
2088         usbd_status error;
2089
2090         /* copy firmware image into NIC */
2091         for (; size >= 4; reg += 4, ucode += 4, size -= 4)
2092                 rum_write(sc, reg, UGETDW(ucode));
2093
2094         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2095         req.bRequest = RT2573_MCU_CNTL;
2096         USETW(req.wValue, RT2573_MCU_RUN);
2097         USETW(req.wIndex, 0);
2098         USETW(req.wLength, 0);
2099
2100         error = usbd_do_request(sc->sc_udev, &req, NULL);
2101         if (error != 0) {
2102                 kprintf("%s: could not run firmware: %s\n",
2103                     device_get_nameunit(sc->sc_dev), usbd_errstr(error));
2104         }
2105         return error;
2106 }
2107
2108 static int
2109 rum_prepare_beacon(struct rum_softc *sc)
2110 {
2111         struct ieee80211com *ic = &sc->sc_ic;
2112         struct ifnet *ifp = &ic->ic_if;
2113         struct ieee80211_beacon_offsets bo;
2114         struct rum_tx_desc desc;
2115         struct mbuf *m0;
2116         int rate;
2117
2118         lwkt_serialize_enter(ifp->if_serializer);
2119         m0 = ieee80211_beacon_alloc(ic, ic->ic_bss, &bo);
2120         lwkt_serialize_exit(ifp->if_serializer);
2121
2122         if (m0 == NULL) {
2123                 if_printf(&ic->ic_if, "could not allocate beacon frame\n");
2124                 return ENOBUFS;
2125         }
2126
2127         /* send beacons at the lowest available rate */
2128         rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_bss->ni_chan) ? 12 : 2;
2129
2130         rum_setup_tx_desc(sc, &desc, RT2573_TX_TIMESTAMP, RT2573_TX_HWSEQ,
2131             m0->m_pkthdr.len, rate);
2132
2133         /* copy the first 24 bytes of Tx descriptor into NIC memory */
2134         rum_write_multi(sc, RT2573_HW_BEACON_BASE0, (uint8_t *)&desc, 24);
2135
2136         /* copy beacon header and payload into NIC memory */
2137         rum_write_multi(sc, RT2573_HW_BEACON_BASE0 + 24, mtod(m0, uint8_t *),
2138             m0->m_pkthdr.len);
2139
2140         m_freem(m0);
2141
2142         return 0;
2143 }
2144
2145 static void
2146 rum_stats_timeout(void *arg)
2147 {
2148         struct rum_softc *sc = arg;
2149         usb_device_request_t req;
2150
2151         if (sc->sc_stopped)
2152                 return;
2153
2154         crit_enter();
2155
2156         /*
2157          * Asynchronously read statistic registers (cleared by read).
2158          */
2159         req.bmRequestType = UT_READ_VENDOR_DEVICE;
2160         req.bRequest = RT2573_READ_MULTI_MAC;
2161         USETW(req.wValue, 0);
2162         USETW(req.wIndex, RT2573_STA_CSR0);
2163         USETW(req.wLength, sizeof(sc->sta));
2164
2165         usbd_setup_default_xfer(sc->stats_xfer, sc->sc_udev, sc,
2166                                 USBD_DEFAULT_TIMEOUT, &req,
2167                                 sc->sta, sizeof(sc->sta), 0,
2168                                 rum_stats_update);
2169         usbd_transfer(sc->stats_xfer);
2170
2171         crit_exit();
2172 }
2173
2174 static void
2175 rum_stats_update(usbd_xfer_handle xfer, usbd_private_handle priv,
2176                  usbd_status status)
2177 {
2178         struct rum_softc *sc = (struct rum_softc *)priv;
2179         struct ifnet *ifp = &sc->sc_ic.ic_if;
2180         struct ieee80211_ratectl_stats *stats = &sc->sc_stats;
2181
2182         if (status != USBD_NORMAL_COMPLETION) {
2183                 kprintf("%s: could not retrieve Tx statistics - cancelling "
2184                     "automatic rate control\n", device_get_nameunit(sc->sc_dev));
2185                 return;
2186         }
2187
2188         crit_enter();
2189
2190         /* count TX retry-fail as Tx errors */
2191         IFNET_STAT_INC(ifp, oerrors, RUM_TX_PKT_FAIL(sc));
2192
2193         stats->stats_pkt_noretry += RUM_TX_PKT_NO_RETRY(sc);
2194         stats->stats_pkt_ok += RUM_TX_PKT_NO_RETRY(sc) +
2195                                RUM_TX_PKT_ONE_RETRY(sc) +
2196                                RUM_TX_PKT_MULTI_RETRY(sc);
2197         stats->stats_pkt_err += RUM_TX_PKT_FAIL(sc);
2198
2199         stats->stats_retries += RUM_TX_PKT_ONE_RETRY(sc);
2200 #if 1
2201         /*
2202          * XXX Estimated average:
2203          * Actual number of retries for each packet should belong to
2204          * [2, RUM_TX_SHORT_RETRY_MAX]
2205          */
2206         stats->stats_retries += RUM_TX_PKT_MULTI_RETRY(sc) *
2207                                 ((2 + RUM_TX_SHORT_RETRY_MAX) / 2);
2208 #else
2209         stats->stats_retries += RUM_TX_PKT_MULTI_RETRY(sc);
2210 #endif
2211         stats->stats_retries += RUM_TX_PKT_FAIL(sc) * RUM_TX_SHORT_RETRY_MAX;
2212
2213         callout_reset(&sc->stats_ch, 4 * hz / 5, rum_stats_timeout, sc);
2214
2215         crit_exit();
2216 }
2217
2218 static void
2219 rum_stats(struct ieee80211com *ic, struct ieee80211_node *ni __unused,
2220           struct ieee80211_ratectl_stats *stats)
2221 {
2222         struct ifnet *ifp = &ic->ic_if;
2223         struct rum_softc *sc = ifp->if_softc;
2224
2225         ASSERT_SERIALIZED(ifp->if_serializer);
2226
2227         bcopy(&sc->sc_stats, stats, sizeof(*stats));
2228         bzero(&sc->sc_stats, sizeof(sc->sc_stats));
2229 }
2230
2231 static void *
2232 rum_ratectl_attach(struct ieee80211com *ic, u_int rc)
2233 {
2234         struct rum_softc *sc = ic->ic_if.if_softc;
2235
2236         switch (rc) {
2237         case IEEE80211_RATECTL_ONOE:
2238                 return &sc->sc_onoe_param;
2239         case IEEE80211_RATECTL_NONE:
2240                 /* This could only happen during detaching */
2241                 return NULL;
2242         default:
2243                 panic("unknown rate control algo %u", rc);
2244                 return NULL;
2245         }
2246 }
2247
2248 static int
2249 rum_get_rssi(struct rum_softc *sc, uint8_t raw)
2250 {
2251         int lna, agc, rssi;
2252
2253         lna = (raw >> 5) & 0x3;
2254         agc = raw & 0x1f;
2255
2256         if (lna == 0) {
2257                 /*
2258                  * No RSSI mapping
2259                  *
2260                  * NB: Since RSSI is relative to noise floor, -1 is
2261                  *     adequate for caller to know error happened.
2262                  */
2263                 return -1;
2264         }
2265
2266         rssi = (2 * agc) - RT2573_NOISE_FLOOR;
2267
2268         if (IEEE80211_IS_CHAN_2GHZ(sc->sc_curchan)) {
2269                 rssi += sc->rssi_2ghz_corr;
2270
2271                 if (lna == 1)
2272                         rssi -= 64;
2273                 else if (lna == 2)
2274                         rssi -= 74;
2275                 else if (lna == 3)
2276                         rssi -= 90;
2277         } else {
2278                 rssi += sc->rssi_5ghz_corr;
2279
2280                 if (!sc->ext_5ghz_lna && lna != 1)
2281                         rssi += 4;
2282
2283                 if (lna == 1)
2284                         rssi -= 64;
2285                 else if (lna == 2)
2286                         rssi -= 86;
2287                 else if (lna == 3)
2288                         rssi -= 100;
2289         }
2290         return rssi;
2291 }