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