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