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