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