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