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