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