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