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