if_rum.c revision 1.48 1 /* $OpenBSD: if_rum.c,v 1.40 2006/09/18 16:20:20 damien Exp $ */
2 /* $NetBSD: if_rum.c,v 1.48 2014/03/29 00:59:05 zafer Exp $ */
3
4 /*-
5 * Copyright (c) 2005-2007 Damien Bergamini <damien.bergamini (at) free.fr>
6 * Copyright (c) 2006 Niall O'Higgins <niallo (at) openbsd.org>
7 *
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 */
20
21 /*-
22 * Ralink Technology RT2501USB/RT2601USB chipset driver
23 * http://www.ralinktech.com.tw/
24 */
25
26 #include <sys/cdefs.h>
27 __KERNEL_RCSID(0, "$NetBSD: if_rum.c,v 1.48 2014/03/29 00:59:05 zafer Exp $");
28
29 #include <sys/param.h>
30 #include <sys/sockio.h>
31 #include <sys/sysctl.h>
32 #include <sys/mbuf.h>
33 #include <sys/kernel.h>
34 #include <sys/socket.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/conf.h>
39 #include <sys/device.h>
40
41 #include <sys/bus.h>
42 #include <machine/endian.h>
43 #include <sys/intr.h>
44
45 #include <net/bpf.h>
46 #include <net/if.h>
47 #include <net/if_arp.h>
48 #include <net/if_dl.h>
49 #include <net/if_ether.h>
50 #include <net/if_media.h>
51 #include <net/if_types.h>
52
53 #include <netinet/in.h>
54 #include <netinet/in_systm.h>
55 #include <netinet/in_var.h>
56 #include <netinet/ip.h>
57
58 #include <net80211/ieee80211_netbsd.h>
59 #include <net80211/ieee80211_var.h>
60 #include <net80211/ieee80211_amrr.h>
61 #include <net80211/ieee80211_radiotap.h>
62
63 #include <dev/firmload.h>
64
65 #include <dev/usb/usb.h>
66 #include <dev/usb/usbdi.h>
67 #include <dev/usb/usbdi_util.h>
68 #include <dev/usb/usbdevs.h>
69
70 #include <dev/usb/if_rumreg.h>
71 #include <dev/usb/if_rumvar.h>
72
73 #ifdef RUM_DEBUG
74 #define DPRINTF(x) do { if (rum_debug) printf x; } while (0)
75 #define DPRINTFN(n, x) do { if (rum_debug >= (n)) printf x; } while (0)
76 int rum_debug = 1;
77 #else
78 #define DPRINTF(x)
79 #define DPRINTFN(n, x)
80 #endif
81
82 /* various supported device vendors/products */
83 static const struct usb_devno rum_devs[] = {
84 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_HWU54DM },
85 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_RT2573_2 },
86 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_RT2573_3 },
87 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_RT2573_4 },
88 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_WUG2700 },
89 { USB_VENDOR_AMIT, USB_PRODUCT_AMIT_CGWLUSB2GO },
90 { USB_VENDOR_ASUSTEK, USB_PRODUCT_ASUSTEK_WL167G_2 },
91 { USB_VENDOR_ASUSTEK, USB_PRODUCT_ASUSTEK_WL167G_3 },
92 { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D7050A },
93 { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D9050V3 },
94 { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D9050C },
95 { USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_WUSB200 },
96 { USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_WUSB54GC },
97 { USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_WUSB54GR },
98 { USB_VENDOR_CONCEPTRONIC, USB_PRODUCT_CONCEPTRONIC_C54RU2 },
99 { USB_VENDOR_CONCEPTRONIC, USB_PRODUCT_CONCEPTRONIC_RT2573 },
100 { USB_VENDOR_COREGA, USB_PRODUCT_COREGA_CGWLUSB2GL },
101 { USB_VENDOR_COREGA, USB_PRODUCT_COREGA_CGWLUSB2GPX },
102 { USB_VENDOR_DICKSMITH, USB_PRODUCT_DICKSMITH_CWD854F },
103 { USB_VENDOR_DICKSMITH, USB_PRODUCT_DICKSMITH_RT2573 },
104 { USB_VENDOR_DLINK2, USB_PRODUCT_DLINK2_DWLG122C1 },
105 { USB_VENDOR_DLINK2, USB_PRODUCT_DLINK2_WUA1340 },
106 { USB_VENDOR_DLINK2, USB_PRODUCT_DLINK2_DWA110 },
107 { USB_VENDOR_DLINK2, USB_PRODUCT_DLINK2_DWA111 },
108 { USB_VENDOR_EDIMAX, USB_PRODUCT_EDIMAX_EW7318 },
109 { USB_VENDOR_EDIMAX, USB_PRODUCT_EDIMAX_EW7618 },
110 { USB_VENDOR_GIGABYTE, USB_PRODUCT_GIGABYTE_GNWB01GS },
111 { USB_VENDOR_GIGABYTE, USB_PRODUCT_GIGABYTE_GNWI05GS },
112 { USB_VENDOR_GIGASET, USB_PRODUCT_GIGASET_RT2573 },
113 { USB_VENDOR_GOODWAY, USB_PRODUCT_GOODWAY_RT2573 },
114 { USB_VENDOR_GUILLEMOT, USB_PRODUCT_GUILLEMOT_HWGUSB254LB },
115 { USB_VENDOR_GUILLEMOT, USB_PRODUCT_GUILLEMOT_HWGUSB254V2AP },
116 { USB_VENDOR_HUAWEI3COM, USB_PRODUCT_HUAWEI3COM_RT2573 },
117 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_G54HP },
118 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_SG54HP },
119 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_SG54HG },
120 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_WLIUCG },
121 { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2573 },
122 { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2573_2 },
123 { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2573_3 },
124 { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2573_4 },
125 { USB_VENDOR_NOVATECH, USB_PRODUCT_NOVATECH_RT2573 },
126 { USB_VENDOR_PLANEX2, USB_PRODUCT_PLANEX2_GWUS54HP },
127 { USB_VENDOR_PLANEX2, USB_PRODUCT_PLANEX2_GWUS54MINI2 },
128 { USB_VENDOR_PLANEX2, USB_PRODUCT_PLANEX2_GWUSMM },
129 { USB_VENDOR_QCOM, USB_PRODUCT_QCOM_RT2573 },
130 { USB_VENDOR_QCOM, USB_PRODUCT_QCOM_RT2573_2 },
131 { USB_VENDOR_QCOM, USB_PRODUCT_QCOM_RT2573_3 },
132 { USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2573 },
133 { USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2671 },
134 { USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_WL113R2 },
135 { USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_WL172 },
136 { USB_VENDOR_SPARKLAN, USB_PRODUCT_SPARKLAN_RT2573 },
137 { USB_VENDOR_SURECOM, USB_PRODUCT_SURECOM_RT2573 },
138 { USB_VENDOR_ZYXEL, USB_PRODUCT_ZYXEL_RT2573 }
139 };
140
141 static int rum_attachhook(void *);
142 static int rum_alloc_tx_list(struct rum_softc *);
143 static void rum_free_tx_list(struct rum_softc *);
144 static int rum_alloc_rx_list(struct rum_softc *);
145 static void rum_free_rx_list(struct rum_softc *);
146 static int rum_media_change(struct ifnet *);
147 static void rum_next_scan(void *);
148 static void rum_task(void *);
149 static int rum_newstate(struct ieee80211com *,
150 enum ieee80211_state, int);
151 static void rum_txeof(usbd_xfer_handle, usbd_private_handle,
152 usbd_status);
153 static void rum_rxeof(usbd_xfer_handle, usbd_private_handle,
154 usbd_status);
155 static uint8_t rum_rxrate(const struct rum_rx_desc *);
156 static int rum_ack_rate(struct ieee80211com *, int);
157 static uint16_t rum_txtime(int, int, uint32_t);
158 static uint8_t rum_plcp_signal(int);
159 static void rum_setup_tx_desc(struct rum_softc *,
160 struct rum_tx_desc *, uint32_t, uint16_t, int,
161 int);
162 static int rum_tx_data(struct rum_softc *, struct mbuf *,
163 struct ieee80211_node *);
164 static void rum_start(struct ifnet *);
165 static void rum_watchdog(struct ifnet *);
166 static int rum_ioctl(struct ifnet *, u_long, void *);
167 static void rum_eeprom_read(struct rum_softc *, uint16_t, void *,
168 int);
169 static uint32_t rum_read(struct rum_softc *, uint16_t);
170 static void rum_read_multi(struct rum_softc *, uint16_t, void *,
171 int);
172 static void rum_write(struct rum_softc *, uint16_t, uint32_t);
173 static void rum_write_multi(struct rum_softc *, uint16_t, void *,
174 size_t);
175 static void rum_bbp_write(struct rum_softc *, uint8_t, uint8_t);
176 static uint8_t rum_bbp_read(struct rum_softc *, uint8_t);
177 static void rum_rf_write(struct rum_softc *, uint8_t, uint32_t);
178 static void rum_select_antenna(struct rum_softc *);
179 static void rum_enable_mrr(struct rum_softc *);
180 static void rum_set_txpreamble(struct rum_softc *);
181 static void rum_set_basicrates(struct rum_softc *);
182 static void rum_select_band(struct rum_softc *,
183 struct ieee80211_channel *);
184 static void rum_set_chan(struct rum_softc *,
185 struct ieee80211_channel *);
186 static void rum_enable_tsf_sync(struct rum_softc *);
187 static void rum_update_slot(struct rum_softc *);
188 static void rum_set_bssid(struct rum_softc *, const uint8_t *);
189 static void rum_set_macaddr(struct rum_softc *, const uint8_t *);
190 static void rum_update_promisc(struct rum_softc *);
191 static const char *rum_get_rf(int);
192 static void rum_read_eeprom(struct rum_softc *);
193 static int rum_bbp_init(struct rum_softc *);
194 static int rum_init(struct ifnet *);
195 static void rum_stop(struct ifnet *, int);
196 static int rum_load_microcode(struct rum_softc *, const u_char *,
197 size_t);
198 static int rum_prepare_beacon(struct rum_softc *);
199 static void rum_newassoc(struct ieee80211_node *, int);
200 static void rum_amrr_start(struct rum_softc *,
201 struct ieee80211_node *);
202 static void rum_amrr_timeout(void *);
203 static void rum_amrr_update(usbd_xfer_handle, usbd_private_handle,
204 usbd_status status);
205
206 /*
207 * Supported rates for 802.11a/b/g modes (in 500Kbps unit).
208 */
209 static const struct ieee80211_rateset rum_rateset_11a =
210 { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
211
212 static const struct ieee80211_rateset rum_rateset_11b =
213 { 4, { 2, 4, 11, 22 } };
214
215 static const struct ieee80211_rateset rum_rateset_11g =
216 { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
217
218 static const struct {
219 uint32_t reg;
220 uint32_t val;
221 } rum_def_mac[] = {
222 RT2573_DEF_MAC
223 };
224
225 static const struct {
226 uint8_t reg;
227 uint8_t val;
228 } rum_def_bbp[] = {
229 RT2573_DEF_BBP
230 };
231
232 static const struct rfprog {
233 uint8_t chan;
234 uint32_t r1, r2, r3, r4;
235 } rum_rf5226[] = {
236 RT2573_RF5226
237 }, rum_rf5225[] = {
238 RT2573_RF5225
239 };
240
241 static int rum_match(device_t, cfdata_t, void *);
242 static void rum_attach(device_t, device_t, void *);
243 static int rum_detach(device_t, int);
244 static int rum_activate(device_t, enum devact);
245 extern struct cfdriver rum_cd;
246 CFATTACH_DECL_NEW(rum, sizeof(struct rum_softc), rum_match, rum_attach,
247 rum_detach, rum_activate);
248
249 static int
250 rum_match(device_t parent, cfdata_t match, void *aux)
251 {
252 struct usb_attach_arg *uaa = aux;
253
254 return (usb_lookup(rum_devs, uaa->vendor, uaa->product) != NULL) ?
255 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
256 }
257
258 static int
259 rum_attachhook(void *xsc)
260 {
261 struct rum_softc *sc = xsc;
262 firmware_handle_t fwh;
263 const char *name = "rum-rt2573";
264 u_char *ucode;
265 size_t size;
266 int error;
267
268 if ((error = firmware_open("rum", name, &fwh)) != 0) {
269 printf("%s: failed loadfirmware of file %s (error %d)\n",
270 device_xname(sc->sc_dev), name, error);
271 return error;
272 }
273 size = firmware_get_size(fwh);
274 ucode = firmware_malloc(size);
275 if (ucode == NULL) {
276 printf("%s: failed to allocate firmware memory\n",
277 device_xname(sc->sc_dev));
278 firmware_close(fwh);
279 return ENOMEM;
280 }
281 error = firmware_read(fwh, 0, ucode, size);
282 firmware_close(fwh);
283 if (error != 0) {
284 printf("%s: failed to read firmware (error %d)\n",
285 device_xname(sc->sc_dev), error);
286 firmware_free(ucode, 0);
287 return error;
288 }
289
290 if (rum_load_microcode(sc, ucode, size) != 0) {
291 printf("%s: could not load 8051 microcode\n",
292 device_xname(sc->sc_dev));
293 firmware_free(ucode, 0);
294 return ENXIO;
295 }
296
297 firmware_free(ucode, 0);
298 sc->sc_flags |= RT2573_FWLOADED;
299
300 return 0;
301 }
302
303 static void
304 rum_attach(device_t parent, device_t self, void *aux)
305 {
306 struct rum_softc *sc = device_private(self);
307 struct usb_attach_arg *uaa = aux;
308 struct ieee80211com *ic = &sc->sc_ic;
309 struct ifnet *ifp = &sc->sc_if;
310 usb_interface_descriptor_t *id;
311 usb_endpoint_descriptor_t *ed;
312 usbd_status error;
313 char *devinfop;
314 int i, ntries;
315 uint32_t tmp;
316
317 sc->sc_dev = self;
318 sc->sc_udev = uaa->device;
319 sc->sc_flags = 0;
320
321 aprint_naive("\n");
322 aprint_normal("\n");
323
324 devinfop = usbd_devinfo_alloc(sc->sc_udev, 0);
325 aprint_normal_dev(self, "%s\n", devinfop);
326 usbd_devinfo_free(devinfop);
327
328 error = usbd_set_config_no(sc->sc_udev, RT2573_CONFIG_NO, 0);
329 if (error != 0) {
330 aprint_error_dev(self, "failed to set configuration"
331 ", err=%s\n", usbd_errstr(error));
332 return;
333 }
334
335 /* get the first interface handle */
336 error = usbd_device2interface_handle(sc->sc_udev, RT2573_IFACE_INDEX,
337 &sc->sc_iface);
338 if (error != 0) {
339 aprint_error_dev(self, "could not get interface handle\n");
340 return;
341 }
342
343 /*
344 * Find endpoints.
345 */
346 id = usbd_get_interface_descriptor(sc->sc_iface);
347
348 sc->sc_rx_no = sc->sc_tx_no = -1;
349 for (i = 0; i < id->bNumEndpoints; i++) {
350 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
351 if (ed == NULL) {
352 aprint_error_dev(self,
353 "no endpoint descriptor for iface %d\n", i);
354 return;
355 }
356
357 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
358 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
359 sc->sc_rx_no = ed->bEndpointAddress;
360 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
361 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
362 sc->sc_tx_no = ed->bEndpointAddress;
363 }
364 if (sc->sc_rx_no == -1 || sc->sc_tx_no == -1) {
365 aprint_error_dev(self, "missing endpoint\n");
366 return;
367 }
368
369 usb_init_task(&sc->sc_task, rum_task, sc, 0);
370 callout_init(&sc->sc_scan_ch, 0);
371
372 sc->amrr.amrr_min_success_threshold = 1;
373 sc->amrr.amrr_max_success_threshold = 10;
374 callout_init(&sc->sc_amrr_ch, 0);
375
376 /* retrieve RT2573 rev. no */
377 for (ntries = 0; ntries < 1000; ntries++) {
378 if ((tmp = rum_read(sc, RT2573_MAC_CSR0)) != 0)
379 break;
380 DELAY(1000);
381 }
382 if (ntries == 1000) {
383 aprint_error_dev(self, "timeout waiting for chip to settle\n");
384 return;
385 }
386
387 /* retrieve MAC address and various other things from EEPROM */
388 rum_read_eeprom(sc);
389
390 aprint_normal_dev(self,
391 "MAC/BBP RT%04x (rev 0x%05x), RF %s, address %s\n",
392 sc->macbbp_rev, tmp,
393 rum_get_rf(sc->rf_rev), ether_sprintf(ic->ic_myaddr));
394
395 ic->ic_ifp = ifp;
396 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
397 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
398 ic->ic_state = IEEE80211_S_INIT;
399
400 /* set device capabilities */
401 ic->ic_caps =
402 IEEE80211_C_IBSS | /* IBSS mode supported */
403 IEEE80211_C_MONITOR | /* monitor mode supported */
404 IEEE80211_C_HOSTAP | /* HostAp mode supported */
405 IEEE80211_C_TXPMGT | /* tx power management */
406 IEEE80211_C_SHPREAMBLE | /* short preamble supported */
407 IEEE80211_C_SHSLOT | /* short slot time supported */
408 IEEE80211_C_WPA; /* 802.11i */
409
410 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_5226) {
411 /* set supported .11a rates */
412 ic->ic_sup_rates[IEEE80211_MODE_11A] = rum_rateset_11a;
413
414 /* set supported .11a channels */
415 for (i = 34; i <= 46; i += 4) {
416 ic->ic_channels[i].ic_freq =
417 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
418 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
419 }
420 for (i = 36; i <= 64; 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 = 100; i <= 140; 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 = 149; i <= 165; 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 }
436
437 /* set supported .11b and .11g rates */
438 ic->ic_sup_rates[IEEE80211_MODE_11B] = rum_rateset_11b;
439 ic->ic_sup_rates[IEEE80211_MODE_11G] = rum_rateset_11g;
440
441 /* set supported .11b and .11g channels (1 through 14) */
442 for (i = 1; i <= 14; i++) {
443 ic->ic_channels[i].ic_freq =
444 ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
445 ic->ic_channels[i].ic_flags =
446 IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
447 IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
448 }
449
450 ifp->if_softc = sc;
451 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
452 ifp->if_init = rum_init;
453 ifp->if_ioctl = rum_ioctl;
454 ifp->if_start = rum_start;
455 ifp->if_watchdog = rum_watchdog;
456 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
457 IFQ_SET_READY(&ifp->if_snd);
458 memcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
459
460 if_attach(ifp);
461 ieee80211_ifattach(ic);
462 ic->ic_newassoc = rum_newassoc;
463
464 /* override state transition machine */
465 sc->sc_newstate = ic->ic_newstate;
466 ic->ic_newstate = rum_newstate;
467 ieee80211_media_init(ic, rum_media_change, ieee80211_media_status);
468
469 bpf_attach2(ifp, DLT_IEEE802_11_RADIO,
470 sizeof (struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN,
471 &sc->sc_drvbpf);
472
473 sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
474 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
475 sc->sc_rxtap.wr_ihdr.it_present = htole32(RT2573_RX_RADIOTAP_PRESENT);
476
477 sc->sc_txtap_len = sizeof sc->sc_txtapu;
478 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
479 sc->sc_txtap.wt_ihdr.it_present = htole32(RT2573_TX_RADIOTAP_PRESENT);
480
481 ieee80211_announce(ic);
482
483 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
484 sc->sc_dev);
485
486 return;
487 }
488
489 static int
490 rum_detach(device_t self, int flags)
491 {
492 struct rum_softc *sc = device_private(self);
493 struct ieee80211com *ic = &sc->sc_ic;
494 struct ifnet *ifp = &sc->sc_if;
495 int s;
496
497 if (!ifp->if_softc)
498 return 0;
499
500 s = splusb();
501
502 rum_stop(ifp, 1);
503 usb_rem_task(sc->sc_udev, &sc->sc_task);
504 callout_stop(&sc->sc_scan_ch);
505 callout_stop(&sc->sc_amrr_ch);
506
507 if (sc->amrr_xfer != NULL) {
508 usbd_free_xfer(sc->amrr_xfer);
509 sc->amrr_xfer = NULL;
510 }
511
512 if (sc->sc_rx_pipeh != NULL) {
513 usbd_abort_pipe(sc->sc_rx_pipeh);
514 usbd_close_pipe(sc->sc_rx_pipeh);
515 }
516
517 if (sc->sc_tx_pipeh != NULL) {
518 usbd_abort_pipe(sc->sc_tx_pipeh);
519 usbd_close_pipe(sc->sc_tx_pipeh);
520 }
521
522 bpf_detach(ifp);
523 ieee80211_ifdetach(ic); /* free all nodes */
524 if_detach(ifp);
525
526 splx(s);
527
528 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
529
530 return 0;
531 }
532
533 static int
534 rum_alloc_tx_list(struct rum_softc *sc)
535 {
536 struct rum_tx_data *data;
537 int i, error;
538
539 sc->tx_cur = sc->tx_queued = 0;
540
541 for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
542 data = &sc->tx_data[i];
543
544 data->sc = sc;
545
546 data->xfer = usbd_alloc_xfer(sc->sc_udev);
547 if (data->xfer == NULL) {
548 printf("%s: could not allocate tx xfer\n",
549 device_xname(sc->sc_dev));
550 error = ENOMEM;
551 goto fail;
552 }
553
554 data->buf = usbd_alloc_buffer(data->xfer,
555 RT2573_TX_DESC_SIZE + IEEE80211_MAX_LEN);
556 if (data->buf == NULL) {
557 printf("%s: could not allocate tx buffer\n",
558 device_xname(sc->sc_dev));
559 error = ENOMEM;
560 goto fail;
561 }
562
563 /* clean Tx descriptor */
564 memset(data->buf, 0, RT2573_TX_DESC_SIZE);
565 }
566
567 return 0;
568
569 fail: rum_free_tx_list(sc);
570 return error;
571 }
572
573 static void
574 rum_free_tx_list(struct rum_softc *sc)
575 {
576 struct rum_tx_data *data;
577 int i;
578
579 for (i = 0; i < RUM_TX_LIST_COUNT; i++) {
580 data = &sc->tx_data[i];
581
582 if (data->xfer != NULL) {
583 usbd_free_xfer(data->xfer);
584 data->xfer = NULL;
585 }
586
587 if (data->ni != NULL) {
588 ieee80211_free_node(data->ni);
589 data->ni = NULL;
590 }
591 }
592 }
593
594 static int
595 rum_alloc_rx_list(struct rum_softc *sc)
596 {
597 struct rum_rx_data *data;
598 int i, error;
599
600 for (i = 0; i < RUM_RX_LIST_COUNT; i++) {
601 data = &sc->rx_data[i];
602
603 data->sc = sc;
604
605 data->xfer = usbd_alloc_xfer(sc->sc_udev);
606 if (data->xfer == NULL) {
607 printf("%s: could not allocate rx xfer\n",
608 device_xname(sc->sc_dev));
609 error = ENOMEM;
610 goto fail;
611 }
612
613 if (usbd_alloc_buffer(data->xfer, MCLBYTES) == NULL) {
614 printf("%s: could not allocate rx buffer\n",
615 device_xname(sc->sc_dev));
616 error = ENOMEM;
617 goto fail;
618 }
619
620 MGETHDR(data->m, M_DONTWAIT, MT_DATA);
621 if (data->m == NULL) {
622 printf("%s: could not allocate rx mbuf\n",
623 device_xname(sc->sc_dev));
624 error = ENOMEM;
625 goto fail;
626 }
627
628 MCLGET(data->m, M_DONTWAIT);
629 if (!(data->m->m_flags & M_EXT)) {
630 printf("%s: could not allocate rx mbuf cluster\n",
631 device_xname(sc->sc_dev));
632 error = ENOMEM;
633 goto fail;
634 }
635
636 data->buf = mtod(data->m, uint8_t *);
637 }
638
639 return 0;
640
641 fail: rum_free_rx_list(sc);
642 return error;
643 }
644
645 static void
646 rum_free_rx_list(struct rum_softc *sc)
647 {
648 struct rum_rx_data *data;
649 int i;
650
651 for (i = 0; i < RUM_RX_LIST_COUNT; i++) {
652 data = &sc->rx_data[i];
653
654 if (data->xfer != NULL) {
655 usbd_free_xfer(data->xfer);
656 data->xfer = NULL;
657 }
658
659 if (data->m != NULL) {
660 m_freem(data->m);
661 data->m = NULL;
662 }
663 }
664 }
665
666 static int
667 rum_media_change(struct ifnet *ifp)
668 {
669 int error;
670
671 error = ieee80211_media_change(ifp);
672 if (error != ENETRESET)
673 return error;
674
675 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
676 rum_init(ifp);
677
678 return 0;
679 }
680
681 /*
682 * This function is called periodically (every 200ms) during scanning to
683 * switch from one channel to another.
684 */
685 static void
686 rum_next_scan(void *arg)
687 {
688 struct rum_softc *sc = arg;
689 struct ieee80211com *ic = &sc->sc_ic;
690 int s;
691
692 s = splnet();
693 if (ic->ic_state == IEEE80211_S_SCAN)
694 ieee80211_next_scan(ic);
695 splx(s);
696 }
697
698 static void
699 rum_task(void *arg)
700 {
701 struct rum_softc *sc = arg;
702 struct ieee80211com *ic = &sc->sc_ic;
703 enum ieee80211_state ostate;
704 struct ieee80211_node *ni;
705 uint32_t tmp;
706
707 ostate = ic->ic_state;
708
709 switch (sc->sc_state) {
710 case IEEE80211_S_INIT:
711 if (ostate == IEEE80211_S_RUN) {
712 /* abort TSF synchronization */
713 tmp = rum_read(sc, RT2573_TXRX_CSR9);
714 rum_write(sc, RT2573_TXRX_CSR9, tmp & ~0x00ffffff);
715 }
716 break;
717
718 case IEEE80211_S_SCAN:
719 rum_set_chan(sc, ic->ic_curchan);
720 callout_reset(&sc->sc_scan_ch, hz / 5, rum_next_scan, sc);
721 break;
722
723 case IEEE80211_S_AUTH:
724 rum_set_chan(sc, ic->ic_curchan);
725 break;
726
727 case IEEE80211_S_ASSOC:
728 rum_set_chan(sc, ic->ic_curchan);
729 break;
730
731 case IEEE80211_S_RUN:
732 rum_set_chan(sc, ic->ic_curchan);
733
734 ni = ic->ic_bss;
735
736 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
737 rum_update_slot(sc);
738 rum_enable_mrr(sc);
739 rum_set_txpreamble(sc);
740 rum_set_basicrates(sc);
741 rum_set_bssid(sc, ni->ni_bssid);
742 }
743
744 if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
745 ic->ic_opmode == IEEE80211_M_IBSS)
746 rum_prepare_beacon(sc);
747
748 if (ic->ic_opmode != IEEE80211_M_MONITOR)
749 rum_enable_tsf_sync(sc);
750
751 if (ic->ic_opmode == IEEE80211_M_STA) {
752 /* fake a join to init the tx rate */
753 rum_newassoc(ic->ic_bss, 1);
754
755 /* enable automatic rate adaptation in STA mode */
756 if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE)
757 rum_amrr_start(sc, ni);
758 }
759
760 break;
761 }
762
763 sc->sc_newstate(ic, sc->sc_state, sc->sc_arg);
764 }
765
766 static int
767 rum_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
768 {
769 struct rum_softc *sc = ic->ic_ifp->if_softc;
770
771 usb_rem_task(sc->sc_udev, &sc->sc_task);
772 callout_stop(&sc->sc_scan_ch);
773 callout_stop(&sc->sc_amrr_ch);
774
775 /* do it in a process context */
776 sc->sc_state = nstate;
777 sc->sc_arg = arg;
778 usb_add_task(sc->sc_udev, &sc->sc_task, USB_TASKQ_DRIVER);
779
780 return 0;
781 }
782
783 /* quickly determine if a given rate is CCK or OFDM */
784 #define RUM_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
785
786 #define RUM_ACK_SIZE 14 /* 10 + 4(FCS) */
787 #define RUM_CTS_SIZE 14 /* 10 + 4(FCS) */
788
789 static void
790 rum_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
791 {
792 struct rum_tx_data *data = priv;
793 struct rum_softc *sc = data->sc;
794 struct ifnet *ifp = &sc->sc_if;
795 int s;
796
797 if (status != USBD_NORMAL_COMPLETION) {
798 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
799 return;
800
801 printf("%s: could not transmit buffer: %s\n",
802 device_xname(sc->sc_dev), usbd_errstr(status));
803
804 if (status == USBD_STALLED)
805 usbd_clear_endpoint_stall_async(sc->sc_tx_pipeh);
806
807 ifp->if_oerrors++;
808 return;
809 }
810
811 s = splnet();
812
813 ieee80211_free_node(data->ni);
814 data->ni = NULL;
815
816 sc->tx_queued--;
817 ifp->if_opackets++;
818
819 DPRINTFN(10, ("tx done\n"));
820
821 sc->sc_tx_timer = 0;
822 ifp->if_flags &= ~IFF_OACTIVE;
823 rum_start(ifp);
824
825 splx(s);
826 }
827
828 static void
829 rum_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
830 {
831 struct rum_rx_data *data = priv;
832 struct rum_softc *sc = data->sc;
833 struct ieee80211com *ic = &sc->sc_ic;
834 struct ifnet *ifp = &sc->sc_if;
835 struct rum_rx_desc *desc;
836 struct ieee80211_frame *wh;
837 struct ieee80211_node *ni;
838 struct mbuf *mnew, *m;
839 int s, len;
840
841 if (status != USBD_NORMAL_COMPLETION) {
842 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
843 return;
844
845 if (status == USBD_STALLED)
846 usbd_clear_endpoint_stall_async(sc->sc_rx_pipeh);
847 goto skip;
848 }
849
850 usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
851
852 if (len < (int)(RT2573_RX_DESC_SIZE +
853 sizeof(struct ieee80211_frame_min))) {
854 DPRINTF(("%s: xfer too short %d\n", device_xname(sc->sc_dev),
855 len));
856 ifp->if_ierrors++;
857 goto skip;
858 }
859
860 desc = (struct rum_rx_desc *)data->buf;
861
862 if (le32toh(desc->flags) & RT2573_RX_CRC_ERROR) {
863 /*
864 * This should not happen since we did not request to receive
865 * those frames when we filled RT2573_TXRX_CSR0.
866 */
867 DPRINTFN(5, ("CRC error\n"));
868 ifp->if_ierrors++;
869 goto skip;
870 }
871
872 MGETHDR(mnew, M_DONTWAIT, MT_DATA);
873 if (mnew == NULL) {
874 printf("%s: could not allocate rx mbuf\n",
875 device_xname(sc->sc_dev));
876 ifp->if_ierrors++;
877 goto skip;
878 }
879
880 MCLGET(mnew, M_DONTWAIT);
881 if (!(mnew->m_flags & M_EXT)) {
882 printf("%s: could not allocate rx mbuf cluster\n",
883 device_xname(sc->sc_dev));
884 m_freem(mnew);
885 ifp->if_ierrors++;
886 goto skip;
887 }
888
889 m = data->m;
890 data->m = mnew;
891 data->buf = mtod(data->m, uint8_t *);
892
893 /* finalize mbuf */
894 m->m_pkthdr.rcvif = ifp;
895 m->m_data = (void *)(desc + 1);
896 m->m_pkthdr.len = m->m_len = (le32toh(desc->flags) >> 16) & 0xfff;
897
898 s = splnet();
899
900 if (sc->sc_drvbpf != NULL) {
901 struct rum_rx_radiotap_header *tap = &sc->sc_rxtap;
902
903 tap->wr_flags = IEEE80211_RADIOTAP_F_FCS;
904 tap->wr_rate = rum_rxrate(desc);
905 tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
906 tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
907 tap->wr_antenna = sc->rx_ant;
908 tap->wr_antsignal = desc->rssi;
909
910 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m);
911 }
912
913 wh = mtod(m, struct ieee80211_frame *);
914 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
915
916 /* send the frame to the 802.11 layer */
917 ieee80211_input(ic, m, ni, desc->rssi, 0);
918
919 /* node is no longer needed */
920 ieee80211_free_node(ni);
921
922 splx(s);
923
924 DPRINTFN(15, ("rx done\n"));
925
926 skip: /* setup a new transfer */
927 usbd_setup_xfer(xfer, sc->sc_rx_pipeh, data, data->buf, MCLBYTES,
928 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rum_rxeof);
929 usbd_transfer(xfer);
930 }
931
932 /*
933 * This function is only used by the Rx radiotap code. It returns the rate at
934 * which a given frame was received.
935 */
936 static uint8_t
937 rum_rxrate(const struct rum_rx_desc *desc)
938 {
939 if (le32toh(desc->flags) & RT2573_RX_OFDM) {
940 /* reverse function of rum_plcp_signal */
941 switch (desc->rate) {
942 case 0xb: return 12;
943 case 0xf: return 18;
944 case 0xa: return 24;
945 case 0xe: return 36;
946 case 0x9: return 48;
947 case 0xd: return 72;
948 case 0x8: return 96;
949 case 0xc: return 108;
950 }
951 } else {
952 if (desc->rate == 10)
953 return 2;
954 if (desc->rate == 20)
955 return 4;
956 if (desc->rate == 55)
957 return 11;
958 if (desc->rate == 110)
959 return 22;
960 }
961 return 2; /* should not get there */
962 }
963
964 /*
965 * Return the expected ack rate for a frame transmitted at rate `rate'.
966 * XXX: this should depend on the destination node basic rate set.
967 */
968 static int
969 rum_ack_rate(struct ieee80211com *ic, int rate)
970 {
971 switch (rate) {
972 /* CCK rates */
973 case 2:
974 return 2;
975 case 4:
976 case 11:
977 case 22:
978 return (ic->ic_curmode == IEEE80211_MODE_11B) ? 4 : rate;
979
980 /* OFDM rates */
981 case 12:
982 case 18:
983 return 12;
984 case 24:
985 case 36:
986 return 24;
987 case 48:
988 case 72:
989 case 96:
990 case 108:
991 return 48;
992 }
993
994 /* default to 1Mbps */
995 return 2;
996 }
997
998 /*
999 * Compute the duration (in us) needed to transmit `len' bytes at rate `rate'.
1000 * The function automatically determines the operating mode depending on the
1001 * given rate. `flags' indicates whether short preamble is in use or not.
1002 */
1003 static uint16_t
1004 rum_txtime(int len, int rate, uint32_t flags)
1005 {
1006 uint16_t txtime;
1007
1008 if (RUM_RATE_IS_OFDM(rate)) {
1009 /* IEEE Std 802.11a-1999, pp. 37 */
1010 txtime = (8 + 4 * len + 3 + rate - 1) / rate;
1011 txtime = 16 + 4 + 4 * txtime + 6;
1012 } else {
1013 /* IEEE Std 802.11b-1999, pp. 28 */
1014 txtime = (16 * len + rate - 1) / rate;
1015 if (rate != 2 && (flags & IEEE80211_F_SHPREAMBLE))
1016 txtime += 72 + 24;
1017 else
1018 txtime += 144 + 48;
1019 }
1020 return txtime;
1021 }
1022
1023 static uint8_t
1024 rum_plcp_signal(int rate)
1025 {
1026 switch (rate) {
1027 /* CCK rates (returned values are device-dependent) */
1028 case 2: return 0x0;
1029 case 4: return 0x1;
1030 case 11: return 0x2;
1031 case 22: return 0x3;
1032
1033 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1034 case 12: return 0xb;
1035 case 18: return 0xf;
1036 case 24: return 0xa;
1037 case 36: return 0xe;
1038 case 48: return 0x9;
1039 case 72: return 0xd;
1040 case 96: return 0x8;
1041 case 108: return 0xc;
1042
1043 /* unsupported rates (should not get there) */
1044 default: return 0xff;
1045 }
1046 }
1047
1048 static void
1049 rum_setup_tx_desc(struct rum_softc *sc, struct rum_tx_desc *desc,
1050 uint32_t flags, uint16_t xflags, int len, int rate)
1051 {
1052 struct ieee80211com *ic = &sc->sc_ic;
1053 uint16_t plcp_length;
1054 int remainder;
1055
1056 desc->flags = htole32(flags);
1057 desc->flags |= htole32(RT2573_TX_VALID);
1058 desc->flags |= htole32(len << 16);
1059
1060 desc->xflags = htole16(xflags);
1061
1062 desc->wme = htole16(
1063 RT2573_QID(0) |
1064 RT2573_AIFSN(2) |
1065 RT2573_LOGCWMIN(4) |
1066 RT2573_LOGCWMAX(10));
1067
1068 /* setup PLCP fields */
1069 desc->plcp_signal = rum_plcp_signal(rate);
1070 desc->plcp_service = 4;
1071
1072 len += IEEE80211_CRC_LEN;
1073 if (RUM_RATE_IS_OFDM(rate)) {
1074 desc->flags |= htole32(RT2573_TX_OFDM);
1075
1076 plcp_length = len & 0xfff;
1077 desc->plcp_length_hi = plcp_length >> 6;
1078 desc->plcp_length_lo = plcp_length & 0x3f;
1079 } else {
1080 plcp_length = (16 * len + rate - 1) / rate;
1081 if (rate == 22) {
1082 remainder = (16 * len) % 22;
1083 if (remainder != 0 && remainder < 7)
1084 desc->plcp_service |= RT2573_PLCP_LENGEXT;
1085 }
1086 desc->plcp_length_hi = plcp_length >> 8;
1087 desc->plcp_length_lo = plcp_length & 0xff;
1088
1089 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1090 desc->plcp_signal |= 0x08;
1091 }
1092 }
1093
1094 #define RUM_TX_TIMEOUT 5000
1095
1096 static int
1097 rum_tx_data(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1098 {
1099 struct ieee80211com *ic = &sc->sc_ic;
1100 struct rum_tx_desc *desc;
1101 struct rum_tx_data *data;
1102 struct ieee80211_frame *wh;
1103 struct ieee80211_key *k;
1104 uint32_t flags = 0;
1105 uint16_t dur;
1106 usbd_status error;
1107 int rate, xferlen, pktlen, needrts = 0, needcts = 0;
1108
1109 wh = mtod(m0, struct ieee80211_frame *);
1110
1111 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1112 k = ieee80211_crypto_encap(ic, ni, m0);
1113 if (k == NULL) {
1114 m_freem(m0);
1115 return ENOBUFS;
1116 }
1117
1118 /* packet header may have moved, reset our local pointer */
1119 wh = mtod(m0, struct ieee80211_frame *);
1120 }
1121
1122 /* compute actual packet length (including CRC and crypto overhead) */
1123 pktlen = m0->m_pkthdr.len + IEEE80211_CRC_LEN;
1124
1125 /* pickup a rate */
1126 if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
1127 ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
1128 IEEE80211_FC0_TYPE_MGT)) {
1129 /* mgmt/multicast frames are sent at the lowest avail. rate */
1130 rate = ni->ni_rates.rs_rates[0];
1131 } else if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE) {
1132 rate = ic->ic_bss->ni_rates.rs_rates[ic->ic_fixed_rate];
1133 } else
1134 rate = ni->ni_rates.rs_rates[ni->ni_txrate];
1135 if (rate == 0)
1136 rate = 2; /* XXX should not happen */
1137 rate &= IEEE80211_RATE_VAL;
1138
1139 /* check if RTS/CTS or CTS-to-self protection must be used */
1140 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1141 /* multicast frames are not sent at OFDM rates in 802.11b/g */
1142 if (pktlen > ic->ic_rtsthreshold) {
1143 needrts = 1; /* RTS/CTS based on frame length */
1144 } else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
1145 RUM_RATE_IS_OFDM(rate)) {
1146 if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
1147 needcts = 1; /* CTS-to-self */
1148 else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
1149 needrts = 1; /* RTS/CTS */
1150 }
1151 }
1152 if (needrts || needcts) {
1153 struct mbuf *mprot;
1154 int protrate, ackrate;
1155
1156 protrate = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? 12 : 2;
1157 ackrate = rum_ack_rate(ic, rate);
1158
1159 dur = rum_txtime(pktlen, rate, ic->ic_flags) +
1160 rum_txtime(RUM_ACK_SIZE, ackrate, ic->ic_flags) +
1161 2 * sc->sifs;
1162 if (needrts) {
1163 dur += rum_txtime(RUM_CTS_SIZE, rum_ack_rate(ic,
1164 protrate), ic->ic_flags) + sc->sifs;
1165 mprot = ieee80211_get_rts(ic, wh, dur);
1166 } else {
1167 mprot = ieee80211_get_cts_to_self(ic, dur);
1168 }
1169 if (mprot == NULL) {
1170 aprint_error_dev(sc->sc_dev,
1171 "couldn't allocate protection frame\n");
1172 m_freem(m0);
1173 return ENOBUFS;
1174 }
1175
1176 data = &sc->tx_data[sc->tx_cur];
1177 desc = (struct rum_tx_desc *)data->buf;
1178
1179 /* avoid multiple free() of the same node for each fragment */
1180 data->ni = ieee80211_ref_node(ni);
1181
1182 m_copydata(mprot, 0, mprot->m_pkthdr.len,
1183 data->buf + RT2573_TX_DESC_SIZE);
1184 rum_setup_tx_desc(sc, desc,
1185 (needrts ? RT2573_TX_NEED_ACK : 0) | RT2573_TX_MORE_FRAG,
1186 0, mprot->m_pkthdr.len, protrate);
1187
1188 /* no roundup necessary here */
1189 xferlen = RT2573_TX_DESC_SIZE + mprot->m_pkthdr.len;
1190
1191 /* XXX may want to pass the protection frame to BPF */
1192
1193 /* mbuf is no longer needed */
1194 m_freem(mprot);
1195
1196 usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf,
1197 xferlen, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
1198 RUM_TX_TIMEOUT, rum_txeof);
1199 error = usbd_transfer(data->xfer);
1200 if (error != USBD_NORMAL_COMPLETION &&
1201 error != USBD_IN_PROGRESS) {
1202 m_freem(m0);
1203 return error;
1204 }
1205
1206 sc->tx_queued++;
1207 sc->tx_cur = (sc->tx_cur + 1) % RUM_TX_LIST_COUNT;
1208
1209 flags |= RT2573_TX_LONG_RETRY | RT2573_TX_IFS_SIFS;
1210 }
1211
1212 data = &sc->tx_data[sc->tx_cur];
1213 desc = (struct rum_tx_desc *)data->buf;
1214
1215 data->ni = ni;
1216
1217 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1218 flags |= RT2573_TX_NEED_ACK;
1219
1220 dur = rum_txtime(RUM_ACK_SIZE, rum_ack_rate(ic, rate),
1221 ic->ic_flags) + sc->sifs;
1222 *(uint16_t *)wh->i_dur = htole16(dur);
1223
1224 /* tell hardware to set timestamp in probe responses */
1225 if ((wh->i_fc[0] &
1226 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
1227 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
1228 flags |= RT2573_TX_TIMESTAMP;
1229 }
1230
1231 if (sc->sc_drvbpf != NULL) {
1232 struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
1233
1234 tap->wt_flags = 0;
1235 tap->wt_rate = rate;
1236 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1237 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1238 tap->wt_antenna = sc->tx_ant;
1239
1240 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
1241 }
1242
1243 m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RT2573_TX_DESC_SIZE);
1244 rum_setup_tx_desc(sc, desc, flags, 0, m0->m_pkthdr.len, rate);
1245
1246 /* align end on a 4-bytes boundary */
1247 xferlen = (RT2573_TX_DESC_SIZE + m0->m_pkthdr.len + 3) & ~3;
1248
1249 /*
1250 * No space left in the last URB to store the extra 4 bytes, force
1251 * sending of another URB.
1252 */
1253 if ((xferlen % 64) == 0)
1254 xferlen += 4;
1255
1256 DPRINTFN(10, ("sending data frame len=%zu rate=%u xfer len=%u\n",
1257 (size_t)m0->m_pkthdr.len + RT2573_TX_DESC_SIZE,
1258 rate, xferlen));
1259
1260 /* mbuf is no longer needed */
1261 m_freem(m0);
1262
1263 usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf, xferlen,
1264 USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RUM_TX_TIMEOUT, rum_txeof);
1265 error = usbd_transfer(data->xfer);
1266 if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS)
1267 return error;
1268
1269 sc->tx_queued++;
1270 sc->tx_cur = (sc->tx_cur + 1) % RUM_TX_LIST_COUNT;
1271
1272 return 0;
1273 }
1274
1275 static void
1276 rum_start(struct ifnet *ifp)
1277 {
1278 struct rum_softc *sc = ifp->if_softc;
1279 struct ieee80211com *ic = &sc->sc_ic;
1280 struct ether_header *eh;
1281 struct ieee80211_node *ni;
1282 struct mbuf *m0;
1283
1284 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
1285 return;
1286
1287 for (;;) {
1288 IF_POLL(&ic->ic_mgtq, m0);
1289 if (m0 != NULL) {
1290 if (sc->tx_queued >= RUM_TX_LIST_COUNT - 1) {
1291 ifp->if_flags |= IFF_OACTIVE;
1292 break;
1293 }
1294 IF_DEQUEUE(&ic->ic_mgtq, m0);
1295
1296 ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
1297 m0->m_pkthdr.rcvif = NULL;
1298 bpf_mtap3(ic->ic_rawbpf, m0);
1299 if (rum_tx_data(sc, m0, ni) != 0)
1300 break;
1301
1302 } else {
1303 if (ic->ic_state != IEEE80211_S_RUN)
1304 break;
1305 IFQ_POLL(&ifp->if_snd, m0);
1306 if (m0 == NULL)
1307 break;
1308 if (sc->tx_queued >= RUM_TX_LIST_COUNT - 1) {
1309 ifp->if_flags |= IFF_OACTIVE;
1310 break;
1311 }
1312 IFQ_DEQUEUE(&ifp->if_snd, m0);
1313 if (m0->m_len < (int)sizeof(struct ether_header) &&
1314 !(m0 = m_pullup(m0, sizeof(struct ether_header))))
1315 continue;
1316
1317 eh = mtod(m0, struct ether_header *);
1318 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1319 if (ni == NULL) {
1320 m_freem(m0);
1321 continue;
1322 }
1323 bpf_mtap(ifp, m0);
1324 m0 = ieee80211_encap(ic, m0, ni);
1325 if (m0 == NULL) {
1326 ieee80211_free_node(ni);
1327 continue;
1328 }
1329 bpf_mtap3(ic->ic_rawbpf, m0);
1330 if (rum_tx_data(sc, m0, ni) != 0) {
1331 ieee80211_free_node(ni);
1332 ifp->if_oerrors++;
1333 break;
1334 }
1335 }
1336
1337 sc->sc_tx_timer = 5;
1338 ifp->if_timer = 1;
1339 }
1340 }
1341
1342 static void
1343 rum_watchdog(struct ifnet *ifp)
1344 {
1345 struct rum_softc *sc = ifp->if_softc;
1346 struct ieee80211com *ic = &sc->sc_ic;
1347
1348 ifp->if_timer = 0;
1349
1350 if (sc->sc_tx_timer > 0) {
1351 if (--sc->sc_tx_timer == 0) {
1352 printf("%s: device timeout\n", device_xname(sc->sc_dev));
1353 /*rum_init(ifp); XXX needs a process context! */
1354 ifp->if_oerrors++;
1355 return;
1356 }
1357 ifp->if_timer = 1;
1358 }
1359
1360 ieee80211_watchdog(ic);
1361 }
1362
1363 static int
1364 rum_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1365 {
1366 #define IS_RUNNING(ifp) \
1367 (((ifp)->if_flags & IFF_UP) && ((ifp)->if_flags & IFF_RUNNING))
1368
1369 struct rum_softc *sc = ifp->if_softc;
1370 struct ieee80211com *ic = &sc->sc_ic;
1371 int s, error = 0;
1372
1373 s = splnet();
1374
1375 switch (cmd) {
1376 case SIOCSIFFLAGS:
1377 if ((error = ifioctl_common(ifp, cmd, data)) != 0)
1378 break;
1379 switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
1380 case IFF_UP|IFF_RUNNING:
1381 rum_update_promisc(sc);
1382 break;
1383 case IFF_UP:
1384 rum_init(ifp);
1385 break;
1386 case IFF_RUNNING:
1387 rum_stop(ifp, 1);
1388 break;
1389 case 0:
1390 break;
1391 }
1392 break;
1393
1394 case SIOCADDMULTI:
1395 case SIOCDELMULTI:
1396 if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
1397 error = 0;
1398 }
1399 break;
1400
1401 default:
1402 error = ieee80211_ioctl(ic, cmd, data);
1403 }
1404
1405 if (error == ENETRESET) {
1406 if (IS_RUNNING(ifp) &&
1407 (ic->ic_roaming != IEEE80211_ROAMING_MANUAL))
1408 rum_init(ifp);
1409 error = 0;
1410 }
1411
1412 splx(s);
1413
1414 return error;
1415 #undef IS_RUNNING
1416 }
1417
1418 static void
1419 rum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len)
1420 {
1421 usb_device_request_t req;
1422 usbd_status error;
1423
1424 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1425 req.bRequest = RT2573_READ_EEPROM;
1426 USETW(req.wValue, 0);
1427 USETW(req.wIndex, addr);
1428 USETW(req.wLength, len);
1429
1430 error = usbd_do_request(sc->sc_udev, &req, buf);
1431 if (error != 0) {
1432 printf("%s: could not read EEPROM: %s\n",
1433 device_xname(sc->sc_dev), usbd_errstr(error));
1434 }
1435 }
1436
1437 static uint32_t
1438 rum_read(struct rum_softc *sc, uint16_t reg)
1439 {
1440 uint32_t val;
1441
1442 rum_read_multi(sc, reg, &val, sizeof val);
1443
1444 return le32toh(val);
1445 }
1446
1447 static void
1448 rum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len)
1449 {
1450 usb_device_request_t req;
1451 usbd_status error;
1452
1453 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1454 req.bRequest = RT2573_READ_MULTI_MAC;
1455 USETW(req.wValue, 0);
1456 USETW(req.wIndex, reg);
1457 USETW(req.wLength, len);
1458
1459 error = usbd_do_request(sc->sc_udev, &req, buf);
1460 if (error != 0) {
1461 printf("%s: could not multi read MAC register: %s\n",
1462 device_xname(sc->sc_dev), usbd_errstr(error));
1463 }
1464 }
1465
1466 static void
1467 rum_write(struct rum_softc *sc, uint16_t reg, uint32_t val)
1468 {
1469 uint32_t tmp = htole32(val);
1470
1471 rum_write_multi(sc, reg, &tmp, sizeof tmp);
1472 }
1473
1474 static void
1475 rum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len)
1476 {
1477 usb_device_request_t req;
1478 usbd_status error;
1479 int offset;
1480
1481 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1482 req.bRequest = RT2573_WRITE_MULTI_MAC;
1483 USETW(req.wValue, 0);
1484
1485 /* write at most 64 bytes at a time */
1486 for (offset = 0; offset < len; offset += 64) {
1487 USETW(req.wIndex, reg + offset);
1488 USETW(req.wLength, MIN(len - offset, 64));
1489
1490 error = usbd_do_request(sc->sc_udev, &req, (char *)buf + offset);
1491 if (error != 0) {
1492 printf("%s: could not multi write MAC register: %s\n",
1493 device_xname(sc->sc_dev), usbd_errstr(error));
1494 }
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