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