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