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