if_rum.c revision 1.4 1 /* $OpenBSD: if_rum.c,v 1.40 2006/09/18 16:20:20 damien Exp $ */
2 /* $NetBSD: if_rum.c,v 1.4 2007/02/06 14:30:09 elad 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.4 2007/02/06 14:30:09 elad Exp $");
28
29 #include "bpfilter.h"
30
31 #include <sys/param.h>
32 #include <sys/sockio.h>
33 #include <sys/sysctl.h>
34 #include <sys/mbuf.h>
35 #include <sys/kernel.h>
36 #include <sys/socket.h>
37 #include <sys/systm.h>
38 #include <sys/malloc.h>
39 #include <sys/conf.h>
40 #include <sys/device.h>
41
42 #include <machine/bus.h>
43 #include <machine/endian.h>
44 #include <machine/intr.h>
45
46 #if NBPFILTER > 0
47 #include <net/bpf.h>
48 #endif
49 #include <net/if.h>
50 #include <net/if_arp.h>
51 #include <net/if_dl.h>
52 #include <net/if_ether.h>
53 #include <net/if_media.h>
54 #include <net/if_types.h>
55
56 #include <netinet/in.h>
57 #include <netinet/in_systm.h>
58 #include <netinet/in_var.h>
59 #include <netinet/ip.h>
60
61 #include <net80211/ieee80211_netbsd.h>
62 #include <net80211/ieee80211_var.h>
63 #include <net80211/ieee80211_amrr.h>
64 #include <net80211/ieee80211_radiotap.h>
65
66 #include <dev/firmload.h>
67
68 #include <dev/usb/usb.h>
69 #include <dev/usb/usbdi.h>
70 #include <dev/usb/usbdi_util.h>
71 #include <dev/usb/usbdevs.h>
72
73 #include <dev/usb/if_rumreg.h>
74 #include <dev/usb/if_rumvar.h>
75
76 #ifdef USB_DEBUG
77 #define RUM_DEBUG
78 #endif
79
80 #ifdef RUM_DEBUG
81 #define DPRINTF(x) do { if (rum_debug) logprintf x; } while (0)
82 #define DPRINTFN(n, x) do { if (rum_debug >= (n)) logprintf x; } while (0)
83 int rum_debug = 0;
84 #else
85 #define DPRINTF(x)
86 #define DPRINTFN(n, x)
87 #endif
88
89 /* various supported device vendors/products */
90 static const struct usb_devno rum_devs[] = {
91 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_RT2573 },
92 { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D7050A },
93 { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D9050V3 },
94 { USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_WUSB54GC },
95 { USB_VENDOR_CONCEPTRONIC, USB_PRODUCT_CONCEPTRONIC_C54RU2 },
96 { USB_VENDOR_DICKSMITH, USB_PRODUCT_DICKSMITH_CWD854F },
97 { USB_VENDOR_DICKSMITH, USB_PRODUCT_DICKSMITH_RT2573 },
98 { USB_VENDOR_DLINK2, USB_PRODUCT_DLINK2_DWLG122C1 },
99 { USB_VENDOR_DLINK2, USB_PRODUCT_DLINK2_WUA1340 },
100 { USB_VENDOR_GIGABYTE, USB_PRODUCT_GIGABYTE_GNWB01GS },
101 { USB_VENDOR_GIGASET, USB_PRODUCT_GIGASET_RT2573 },
102 { USB_VENDOR_GOODWAY, USB_PRODUCT_GOODWAY_RT2573 },
103 { USB_VENDOR_HUAWEI3COM, USB_PRODUCT_HUAWEI3COM_RT2573 },
104 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_SG54HP },
105 { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2573 },
106 { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2573_2 },
107 { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2573_3 },
108 { USB_VENDOR_PLANEX2, USB_PRODUCT_PLANEX2_GWUS54MINI2 },
109 { USB_VENDOR_PLANEX2, USB_PRODUCT_PLANEX2_GWUSMM },
110 { USB_VENDOR_QCOM, USB_PRODUCT_QCOM_RT2573 },
111 { USB_VENDOR_QCOM, USB_PRODUCT_QCOM_RT2573_2 },
112 { USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2573 },
113 { USB_VENDOR_RALINK_2, USB_PRODUCT_RALINK_2_RT2573 },
114 { USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2671 },
115 { USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_WL113R2 },
116 { USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_WL172 },
117 { USB_VENDOR_SURECOM, USB_PRODUCT_SURECOM_RT2573 }
118 };
119
120 Static int rum_attachhook(void *);
121 Static int rum_alloc_tx_list(struct rum_softc *);
122 Static void rum_free_tx_list(struct rum_softc *);
123 Static int rum_alloc_rx_list(struct rum_softc *);
124 Static void rum_free_rx_list(struct rum_softc *);
125 Static int rum_media_change(struct ifnet *);
126 Static void rum_next_scan(void *);
127 Static void rum_task(void *);
128 Static int rum_newstate(struct ieee80211com *,
129 enum ieee80211_state, int);
130 Static void rum_txeof(usbd_xfer_handle, usbd_private_handle,
131 usbd_status);
132 Static void rum_rxeof(usbd_xfer_handle, usbd_private_handle,
133 usbd_status);
134 #if NBPFILTER > 0
135 Static uint8_t rum_rxrate(struct rum_rx_desc *);
136 #endif
137 Static int rum_ack_rate(struct ieee80211com *, int);
138 Static uint16_t rum_txtime(int, int, uint32_t);
139 Static uint8_t rum_plcp_signal(int);
140 Static void rum_setup_tx_desc(struct rum_softc *,
141 struct rum_tx_desc *, uint32_t, uint16_t, int,
142 int);
143 Static int rum_tx_mgt(struct rum_softc *, struct mbuf *,
144 struct ieee80211_node *);
145 Static int rum_tx_data(struct rum_softc *, struct mbuf *,
146 struct ieee80211_node *);
147 Static void rum_start(struct ifnet *);
148 Static void rum_watchdog(struct ifnet *);
149 Static int rum_ioctl(struct ifnet *, u_long, caddr_t);
150 Static void rum_eeprom_read(struct rum_softc *, uint16_t, void *,
151 int);
152 Static uint32_t rum_read(struct rum_softc *, uint16_t);
153 Static void rum_read_multi(struct rum_softc *, uint16_t, void *,
154 int);
155 Static void rum_write(struct rum_softc *, uint16_t, uint32_t);
156 Static void rum_write_multi(struct rum_softc *, uint16_t, void *,
157 size_t);
158 Static void rum_bbp_write(struct rum_softc *, uint8_t, uint8_t);
159 Static uint8_t rum_bbp_read(struct rum_softc *, uint8_t);
160 Static void rum_rf_write(struct rum_softc *, uint8_t, uint32_t);
161 Static void rum_select_antenna(struct rum_softc *);
162 Static void rum_enable_mrr(struct rum_softc *);
163 Static void rum_set_txpreamble(struct rum_softc *);
164 Static void rum_set_basicrates(struct rum_softc *);
165 Static void rum_select_band(struct rum_softc *,
166 struct ieee80211_channel *);
167 Static void rum_set_chan(struct rum_softc *,
168 struct ieee80211_channel *);
169 Static void rum_enable_tsf_sync(struct rum_softc *);
170 Static void rum_update_slot(struct rum_softc *);
171 Static void rum_set_bssid(struct rum_softc *, const uint8_t *);
172 Static void rum_set_macaddr(struct rum_softc *, const uint8_t *);
173 Static void rum_update_promisc(struct rum_softc *);
174 Static const char *rum_get_rf(int);
175 Static void rum_read_eeprom(struct rum_softc *);
176 Static int rum_bbp_init(struct rum_softc *);
177 Static int rum_init(struct ifnet *);
178 Static void rum_stop(struct ifnet *, int);
179 Static int rum_load_microcode(struct rum_softc *, const u_char *,
180 size_t);
181 Static int rum_prepare_beacon(struct rum_softc *);
182 Static void rum_amrr_start(struct rum_softc *,
183 struct ieee80211_node *);
184 Static void rum_amrr_timeout(void *);
185 Static void rum_amrr_update(usbd_xfer_handle, usbd_private_handle,
186 usbd_status status);
187
188 /*
189 * Supported rates for 802.11a/b/g modes (in 500Kbps unit).
190 */
191 static const struct ieee80211_rateset rum_rateset_11a =
192 { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
193
194 static const struct ieee80211_rateset rum_rateset_11b =
195 { 4, { 2, 4, 11, 22 } };
196
197 static const struct ieee80211_rateset rum_rateset_11g =
198 { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
199
200 static const struct {
201 uint32_t reg;
202 uint32_t val;
203 } rum_def_mac[] = {
204 RT2573_DEF_MAC
205 };
206
207 static const struct {
208 uint8_t reg;
209 uint8_t val;
210 } rum_def_bbp[] = {
211 RT2573_DEF_BBP
212 };
213
214 static const struct rfprog {
215 uint8_t chan;
216 uint32_t r1, r2, r3, r4;
217 } rum_rf5226[] = {
218 RT2573_RF5226
219 }, rum_rf5225[] = {
220 RT2573_RF5225
221 };
222
223 USB_DECLARE_DRIVER(rum);
224
225 USB_MATCH(rum)
226 {
227 USB_MATCH_START(rum, uaa);
228
229 if (uaa->iface != NULL)
230 return UMATCH_NONE;
231
232 return (usb_lookup(rum_devs, uaa->vendor, uaa->product) != NULL) ?
233 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
234 }
235
236 Static int
237 rum_attachhook(void *xsc)
238 {
239 struct rum_softc *sc = xsc;
240 firmware_handle_t fwh;
241 const char *name = "rum-rt2573";
242 u_char *ucode;
243 size_t size;
244 int error;
245
246 if ((error = firmware_open("rum", name, &fwh)) != 0) {
247 printf("%s: failed loadfirmware of file %s (error %d)\n",
248 USBDEVNAME(sc->sc_dev), name, error);
249 return error;
250 }
251 size = firmware_get_size(fwh);
252 ucode = firmware_malloc(size);
253 if (ucode == NULL) {
254 printf("%s: failed to allocate firmware memory\n",
255 USBDEVNAME(sc->sc_dev));
256 firmware_close(fwh);
257 return ENOMEM;;
258 }
259 error = firmware_read(fwh, 0, ucode, size);
260 firmware_close(fwh);
261 if (error != 0) {
262 printf("%s: failed to read firmware (error %d)\n",
263 USBDEVNAME(sc->sc_dev), error);
264 firmware_free(ucode, 0);
265 return error;
266 }
267
268 if (rum_load_microcode(sc, ucode, size) != 0) {
269 printf("%s: could not load 8051 microcode\n",
270 USBDEVNAME(sc->sc_dev));
271 firmware_free(ucode, 0);
272 return ENXIO;
273 }
274
275 firmware_free(ucode, 0);
276 sc->sc_flags |= RT2573_FWLOADED;
277
278 return 0;
279 }
280
281 USB_ATTACH(rum)
282 {
283 USB_ATTACH_START(rum, sc, uaa);
284 struct ieee80211com *ic = &sc->sc_ic;
285 struct ifnet *ifp = &sc->sc_if;
286 usb_interface_descriptor_t *id;
287 usb_endpoint_descriptor_t *ed;
288 usbd_status error;
289 char *devinfop;
290 int i, ntries;
291 uint32_t tmp;
292
293 sc->sc_udev = uaa->device;
294 sc->sc_flags = 0;
295
296 devinfop = usbd_devinfo_alloc(sc->sc_udev, 0);
297 USB_ATTACH_SETUP;
298 printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfop);
299 usbd_devinfo_free(devinfop);
300
301 if (usbd_set_config_no(sc->sc_udev, RT2573_CONFIG_NO, 0) != 0) {
302 printf("%s: could not set configuration no\n",
303 USBDEVNAME(sc->sc_dev));
304 USB_ATTACH_ERROR_RETURN;
305 }
306
307 /* get the first interface handle */
308 error = usbd_device2interface_handle(sc->sc_udev, RT2573_IFACE_INDEX,
309 &sc->sc_iface);
310 if (error != 0) {
311 printf("%s: could not get interface handle\n",
312 USBDEVNAME(sc->sc_dev));
313 USB_ATTACH_ERROR_RETURN;
314 }
315
316 /*
317 * Find endpoints.
318 */
319 id = usbd_get_interface_descriptor(sc->sc_iface);
320
321 sc->sc_rx_no = sc->sc_tx_no = -1;
322 for (i = 0; i < id->bNumEndpoints; i++) {
323 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
324 if (ed == NULL) {
325 printf("%s: no endpoint descriptor for iface %d\n",
326 USBDEVNAME(sc->sc_dev), i);
327 USB_ATTACH_ERROR_RETURN;
328 }
329
330 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
331 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
332 sc->sc_rx_no = ed->bEndpointAddress;
333 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
334 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
335 sc->sc_tx_no = ed->bEndpointAddress;
336 }
337 if (sc->sc_rx_no == -1 || sc->sc_tx_no == -1) {
338 printf("%s: missing endpoint\n", USBDEVNAME(sc->sc_dev));
339 USB_ATTACH_ERROR_RETURN;
340 }
341
342 usb_init_task(&sc->sc_task, rum_task, sc);
343 callout_init(&sc->scan_ch);
344
345 sc->amrr.amrr_min_success_threshold = 1;
346 sc->amrr.amrr_max_success_threshold = 10;
347 callout_init(&sc->amrr_ch);
348
349 /* retrieve RT2573 rev. no */
350 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 = (caddr_t)(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=%u rate=%u xfer len=%u\n",
1132 m0->m_pkthdr.len + RT2573_TX_DESC_SIZE, rate, xferlen));
1133
1134 usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf, xferlen,
1135 USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RUM_TX_TIMEOUT, rum_txeof);
1136
1137 error = usbd_transfer(data->xfer);
1138 if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS) {
1139 m_freem(m0);
1140 return error;
1141 }
1142
1143 sc->tx_queued++;
1144
1145 return 0;
1146 }
1147
1148 Static int
1149 rum_tx_data(struct rum_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1150 {
1151 struct ieee80211com *ic = &sc->sc_ic;
1152 struct rum_tx_desc *desc;
1153 struct rum_tx_data *data;
1154 struct ieee80211_frame *wh;
1155 struct ieee80211_key *k;
1156 uint32_t flags = 0;
1157 uint16_t dur;
1158 usbd_status error;
1159 int xferlen, rate;
1160
1161 wh = mtod(m0, struct ieee80211_frame *);
1162
1163 if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE)
1164 rate = ic->ic_bss->ni_rates.rs_rates[ic->ic_fixed_rate];
1165 else
1166 rate = ni->ni_rates.rs_rates[ni->ni_txrate];
1167 rate &= IEEE80211_RATE_VAL;
1168
1169 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1170 k = ieee80211_crypto_encap(ic, ni, m0);
1171 if (k == NULL) {
1172 m_freem(m0);
1173 return ENOBUFS;
1174 }
1175
1176 /* packet header may have moved, reset our local pointer */
1177 wh = mtod(m0, struct ieee80211_frame *);
1178 }
1179
1180 data = &sc->tx_data[0];
1181 desc = (struct rum_tx_desc *)data->buf;
1182
1183 data->m = m0;
1184 data->ni = ni;
1185
1186 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1187 flags |= RT2573_TX_ACK;
1188
1189 dur = rum_txtime(RUM_ACK_SIZE, rum_ack_rate(ic, rate),
1190 ic->ic_flags) + sc->sifs;
1191 *(uint16_t *)wh->i_dur = htole16(dur);
1192 }
1193
1194 #if NBPFILTER > 0
1195 if (sc->sc_drvbpf != NULL) {
1196 struct rum_tx_radiotap_header *tap = &sc->sc_txtap;
1197
1198 tap->wt_flags = 0;
1199 tap->wt_rate = rate;
1200 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1201 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1202 tap->wt_antenna = sc->tx_ant;
1203
1204 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
1205 }
1206 #endif
1207
1208 m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RT2573_TX_DESC_SIZE);
1209 rum_setup_tx_desc(sc, desc, flags, 0, m0->m_pkthdr.len, rate);
1210
1211 /* align end on a 4-bytes boundary */
1212 xferlen = (RT2573_TX_DESC_SIZE + m0->m_pkthdr.len + 3) & ~3;
1213
1214 /*
1215 * No space left in the last URB to store the extra 4 bytes, force
1216 * sending of another URB.
1217 */
1218 if ((xferlen % 64) == 0)
1219 xferlen += 4;
1220
1221 DPRINTFN(10, ("sending data frame len=%u rate=%u xfer len=%u\n",
1222 m0->m_pkthdr.len + RT2573_TX_DESC_SIZE, rate, xferlen));
1223
1224 usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf, xferlen,
1225 USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RUM_TX_TIMEOUT, rum_txeof);
1226
1227 error = usbd_transfer(data->xfer);
1228 if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS) {
1229 m_freem(m0);
1230 return error;
1231 }
1232
1233 sc->tx_queued++;
1234
1235 return 0;
1236 }
1237
1238 Static void
1239 rum_start(struct ifnet *ifp)
1240 {
1241 struct rum_softc *sc = ifp->if_softc;
1242 struct ieee80211com *ic = &sc->sc_ic;
1243 struct ether_header *eh;
1244 struct ieee80211_node *ni;
1245 struct mbuf *m0;
1246
1247 for (;;) {
1248 IF_POLL(&ic->ic_mgtq, m0);
1249 if (m0 != NULL) {
1250 if (sc->tx_queued >= RT2573_TX_LIST_COUNT) {
1251 ifp->if_flags |= IFF_OACTIVE;
1252 break;
1253 }
1254 IF_DEQUEUE(&ic->ic_mgtq, m0);
1255
1256 ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
1257 m0->m_pkthdr.rcvif = NULL;
1258 #if NBPFILTER > 0
1259 if (ic->ic_rawbpf != NULL)
1260 bpf_mtap(ic->ic_rawbpf, m0);
1261 #endif
1262 if (rum_tx_mgt(sc, m0, ni) != 0)
1263 break;
1264
1265 } else {
1266 if (ic->ic_state != IEEE80211_S_RUN)
1267 break;
1268 IFQ_POLL(&ifp->if_snd, m0);
1269 if (m0 == NULL)
1270 break;
1271 if (sc->tx_queued >= RT2573_TX_LIST_COUNT) {
1272 ifp->if_flags |= IFF_OACTIVE;
1273 break;
1274 }
1275 IFQ_DEQUEUE(&ifp->if_snd, m0);
1276 if (m0->m_len < sizeof(struct ether_header) &&
1277 !(m0 = m_pullup(m0, sizeof(struct ether_header))))
1278 continue;
1279
1280 eh = mtod(m0, struct ether_header *);
1281 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1282 if (ni == NULL) {
1283 m_freem(m0);
1284 continue;
1285 }
1286 #if NBPFILTER > 0
1287 if (ifp->if_bpf != NULL)
1288 bpf_mtap(ifp->if_bpf, m0);
1289 #endif
1290 m0 = ieee80211_encap(ic, m0, ni);
1291 if (m0 == NULL) {
1292 ieee80211_free_node(ni);
1293 continue;
1294 }
1295 #if NBPFILTER > 0
1296 if (ic->ic_rawbpf != NULL)
1297 bpf_mtap(ic->ic_rawbpf, m0);
1298 #endif
1299 if (rum_tx_data(sc, m0, ni) != 0) {
1300 ieee80211_free_node(ni);
1301 ifp->if_oerrors++;
1302 break;
1303 }
1304 }
1305
1306 sc->sc_tx_timer = 5;
1307 ifp->if_timer = 1;
1308 }
1309 }
1310
1311 Static void
1312 rum_watchdog(struct ifnet *ifp)
1313 {
1314 struct rum_softc *sc = ifp->if_softc;
1315 struct ieee80211com *ic = &sc->sc_ic;
1316
1317 ifp->if_timer = 0;
1318
1319 if (sc->sc_tx_timer > 0) {
1320 if (--sc->sc_tx_timer == 0) {
1321 printf("%s: device timeout\n", USBDEVNAME(sc->sc_dev));
1322 /*rum_init(ifp); XXX needs a process context! */
1323 ifp->if_oerrors++;
1324 return;
1325 }
1326 ifp->if_timer = 1;
1327 }
1328
1329 ieee80211_watchdog(ic);
1330 }
1331
1332 Static int
1333 rum_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1334 {
1335 struct rum_softc *sc = ifp->if_softc;
1336 struct ieee80211com *ic = &sc->sc_ic;
1337 int s, error = 0;
1338
1339 s = splnet();
1340
1341 switch (cmd) {
1342 case SIOCSIFFLAGS:
1343 if (ifp->if_flags & IFF_UP) {
1344 if (ifp->if_flags & IFF_RUNNING)
1345 rum_update_promisc(sc);
1346 else
1347 rum_init(ifp);
1348 } else {
1349 if (ifp->if_flags & IFF_RUNNING)
1350 rum_stop(ifp, 1);
1351 }
1352 break;
1353
1354 default:
1355 error = ieee80211_ioctl(ic, cmd, data);
1356 }
1357
1358 if (error == ENETRESET) {
1359 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
1360 (IFF_UP | IFF_RUNNING))
1361 rum_init(ifp);
1362 error = 0;
1363 }
1364
1365 splx(s);
1366
1367 return error;
1368 }
1369
1370 Static void
1371 rum_eeprom_read(struct rum_softc *sc, uint16_t addr, void *buf, int len)
1372 {
1373 usb_device_request_t req;
1374 usbd_status error;
1375
1376 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1377 req.bRequest = RT2573_READ_EEPROM;
1378 USETW(req.wValue, 0);
1379 USETW(req.wIndex, addr);
1380 USETW(req.wLength, len);
1381
1382 error = usbd_do_request(sc->sc_udev, &req, buf);
1383 if (error != 0) {
1384 printf("%s: could not read EEPROM: %s\n",
1385 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1386 }
1387 }
1388
1389 Static uint32_t
1390 rum_read(struct rum_softc *sc, uint16_t reg)
1391 {
1392 uint32_t val;
1393
1394 rum_read_multi(sc, reg, &val, sizeof val);
1395
1396 return le32toh(val);
1397 }
1398
1399 Static void
1400 rum_read_multi(struct rum_softc *sc, uint16_t reg, void *buf, int len)
1401 {
1402 usb_device_request_t req;
1403 usbd_status error;
1404
1405 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1406 req.bRequest = RT2573_READ_MULTI_MAC;
1407 USETW(req.wValue, 0);
1408 USETW(req.wIndex, reg);
1409 USETW(req.wLength, len);
1410
1411 error = usbd_do_request(sc->sc_udev, &req, buf);
1412 if (error != 0) {
1413 printf("%s: could not multi read MAC register: %s\n",
1414 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1415 }
1416 }
1417
1418 Static void
1419 rum_write(struct rum_softc *sc, uint16_t reg, uint32_t val)
1420 {
1421 uint32_t tmp = htole32(val);
1422
1423 rum_write_multi(sc, reg, &tmp, sizeof tmp);
1424 }
1425
1426 Static void
1427 rum_write_multi(struct rum_softc *sc, uint16_t reg, void *buf, size_t len)
1428 {
1429 usb_device_request_t req;
1430 usbd_status error;
1431
1432 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1433 req.bRequest = RT2573_WRITE_MULTI_MAC;
1434 USETW(req.wValue, 0);
1435 USETW(req.wIndex, reg);
1436 USETW(req.wLength, len);
1437
1438 error = usbd_do_request(sc->sc_udev, &req, buf);
1439 if (error != 0) {
1440 printf("%s: could not multi write MAC register: %s\n",
1441 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1442 }
1443 }
1444
1445 Static void
1446 rum_bbp_write(struct rum_softc *sc, uint8_t reg, uint8_t val)
1447 {
1448 uint32_t tmp;
1449 int ntries;
1450
1451 for (ntries = 0; ntries < 5; ntries++) {
1452 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1453 break;
1454 }
1455 if (ntries == 5) {
1456 printf("%s: could not write to BBP\n", USBDEVNAME(sc->sc_dev));
1457 return;
1458 }
1459
1460 tmp = RT2573_BBP_BUSY | (reg & 0x7f) << 8 | val;
1461 rum_write(sc, RT2573_PHY_CSR3, tmp);
1462 }
1463
1464 Static uint8_t
1465 rum_bbp_read(struct rum_softc *sc, uint8_t reg)
1466 {
1467 uint32_t val;
1468 int ntries;
1469
1470 for (ntries = 0; ntries < 5; ntries++) {
1471 if (!(rum_read(sc, RT2573_PHY_CSR3) & RT2573_BBP_BUSY))
1472 break;
1473 }
1474 if (ntries == 5) {
1475 printf("%s: could not read BBP\n", USBDEVNAME(sc->sc_dev));
1476 return 0;
1477 }
1478
1479 val = RT2573_BBP_BUSY | RT2573_BBP_READ | reg << 8;
1480 rum_write(sc, RT2573_PHY_CSR3, val);
1481
1482 for (ntries = 0; ntries < 100; ntries++) {
1483 val = rum_read(sc, RT2573_PHY_CSR3);
1484 if (!(val & RT2573_BBP_BUSY))
1485 return val & 0xff;
1486 DELAY(1);
1487 }
1488
1489 printf("%s: could not read BBP\n", USBDEVNAME(sc->sc_dev));
1490 return 0;
1491 }
1492
1493 Static void
1494 rum_rf_write(struct rum_softc *sc, uint8_t reg, uint32_t val)
1495 {
1496 uint32_t tmp;
1497 int ntries;
1498
1499 for (ntries = 0; ntries < 5; ntries++) {
1500 if (!(rum_read(sc, RT2573_PHY_CSR4) & RT2573_RF_BUSY))
1501 break;
1502 }
1503 if (ntries == 5) {
1504 printf("%s: could not write to RF\n", USBDEVNAME(sc->sc_dev));
1505 return;
1506 }
1507
1508 tmp = RT2573_RF_BUSY | RT2573_RF_20BIT | (val & 0xfffff) << 2 |
1509 (reg & 3);
1510 rum_write(sc, RT2573_PHY_CSR4, tmp);
1511
1512 /* remember last written value in sc */
1513 sc->rf_regs[reg] = val;
1514
1515 DPRINTFN(15, ("RF R[%u] <- 0x%05x\n", reg & 3, val & 0xfffff));
1516 }
1517
1518 Static void
1519 rum_select_antenna(struct rum_softc *sc)
1520 {
1521 uint8_t bbp4, bbp77;
1522 uint32_t tmp;
1523
1524 bbp4 = rum_bbp_read(sc, 4);
1525 bbp77 = rum_bbp_read(sc, 77);
1526
1527 /* TBD */
1528
1529 /* make sure Rx is disabled before switching antenna */
1530 tmp = rum_read(sc, RT2573_TXRX_CSR0);
1531 rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
1532
1533 rum_bbp_write(sc, 4, bbp4);
1534 rum_bbp_write(sc, 77, bbp77);
1535
1536 rum_write(sc, RT2573_TXRX_CSR0, tmp);
1537 }
1538
1539 /*
1540 * Enable multi-rate retries for frames sent at OFDM rates.
1541 * In 802.11b/g mode, allow fallback to CCK rates.
1542 */
1543 Static void
1544 rum_enable_mrr(struct rum_softc *sc)
1545 {
1546 struct ieee80211com *ic = &sc->sc_ic;
1547 uint32_t tmp;
1548
1549 tmp = rum_read(sc, RT2573_TXRX_CSR4);
1550
1551 tmp &= ~RT2573_MRR_CCK_FALLBACK;
1552 if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan))
1553 tmp |= RT2573_MRR_CCK_FALLBACK;
1554 tmp |= RT2573_MRR_ENABLED;
1555
1556 rum_write(sc, RT2573_TXRX_CSR4, tmp);
1557 }
1558
1559 Static void
1560 rum_set_txpreamble(struct rum_softc *sc)
1561 {
1562 uint32_t tmp;
1563
1564 tmp = rum_read(sc, RT2573_TXRX_CSR4);
1565
1566 tmp &= ~RT2573_SHORT_PREAMBLE;
1567 if (sc->sc_ic.ic_flags & IEEE80211_F_SHPREAMBLE)
1568 tmp |= RT2573_SHORT_PREAMBLE;
1569
1570 rum_write(sc, RT2573_TXRX_CSR4, tmp);
1571 }
1572
1573 Static void
1574 rum_set_basicrates(struct rum_softc *sc)
1575 {
1576 struct ieee80211com *ic = &sc->sc_ic;
1577
1578 /* update basic rate set */
1579 if (ic->ic_curmode == IEEE80211_MODE_11B) {
1580 /* 11b basic rates: 1, 2Mbps */
1581 rum_write(sc, RT2573_TXRX_CSR5, 0x3);
1582 } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_bss->ni_chan)) {
1583 /* 11a basic rates: 6, 12, 24Mbps */
1584 rum_write(sc, RT2573_TXRX_CSR5, 0x150);
1585 } else {
1586 /* 11g basic rates: 1, 2, 5.5, 11, 6, 12, 24Mbps */
1587 rum_write(sc, RT2573_TXRX_CSR5, 0x15f);
1588 }
1589 }
1590
1591 /*
1592 * Reprogram MAC/BBP to switch to a new band. Values taken from the reference
1593 * driver.
1594 */
1595 Static void
1596 rum_select_band(struct rum_softc *sc, struct ieee80211_channel *c)
1597 {
1598 uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104;
1599 uint32_t tmp;
1600
1601 /* update all BBP registers that depend on the band */
1602 bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c;
1603 bbp35 = 0x50; bbp97 = 0x48; bbp98 = 0x48;
1604 if (IEEE80211_IS_CHAN_5GHZ(c)) {
1605 bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c;
1606 bbp35 += 0x10; bbp97 += 0x10; bbp98 += 0x10;
1607 }
1608 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1609 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1610 bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10;
1611 }
1612
1613 sc->bbp17 = bbp17;
1614 rum_bbp_write(sc, 17, bbp17);
1615 rum_bbp_write(sc, 96, bbp96);
1616 rum_bbp_write(sc, 104, bbp104);
1617
1618 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
1619 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
1620 rum_bbp_write(sc, 75, 0x80);
1621 rum_bbp_write(sc, 86, 0x80);
1622 rum_bbp_write(sc, 88, 0x80);
1623 }
1624
1625 rum_bbp_write(sc, 35, bbp35);
1626 rum_bbp_write(sc, 97, bbp97);
1627 rum_bbp_write(sc, 98, bbp98);
1628
1629 tmp = rum_read(sc, RT2573_PHY_CSR0);
1630 tmp &= ~(RT2573_PA_PE_2GHZ | RT2573_PA_PE_5GHZ);
1631 if (IEEE80211_IS_CHAN_2GHZ(c))
1632 tmp |= RT2573_PA_PE_2GHZ;
1633 else
1634 tmp |= RT2573_PA_PE_5GHZ;
1635 rum_write(sc, RT2573_PHY_CSR0, tmp);
1636
1637 /* 802.11a uses a 16 microseconds short interframe space */
1638 sc->sifs = IEEE80211_IS_CHAN_5GHZ(c) ? 16 : 10;
1639 }
1640
1641 Static void
1642 rum_set_chan(struct rum_softc *sc, struct ieee80211_channel *c)
1643 {
1644 struct ieee80211com *ic = &sc->sc_ic;
1645 const struct rfprog *rfprog;
1646 uint8_t bbp3, bbp94 = RT2573_BBPR94_DEFAULT;
1647 int8_t power;
1648 u_int i, chan;
1649
1650 chan = ieee80211_chan2ieee(ic, c);
1651 if (chan == 0 || chan == IEEE80211_CHAN_ANY)
1652 return;
1653
1654 /* select the appropriate RF settings based on what EEPROM says */
1655 rfprog = (sc->rf_rev == RT2573_RF_5225 ||
1656 sc->rf_rev == RT2573_RF_2527) ? rum_rf5225 : rum_rf5226;
1657
1658 /* find the settings for this channel (we know it exists) */
1659 for (i = 0; rfprog[i].chan != chan; i++);
1660
1661 power = sc->txpow[i];
1662 if (power < 0) {
1663 bbp94 += power;
1664 power = 0;
1665 } else if (power > 31) {
1666 bbp94 += power - 31;
1667 power = 31;
1668 }
1669
1670 /*
1671 * If we are switching from the 2GHz band to the 5GHz band or
1672 * vice-versa, BBP registers need to be reprogrammed.
1673 */
1674 if (c->ic_flags != ic->ic_curchan->ic_flags) {
1675 rum_select_band(sc, c);
1676 rum_select_antenna(sc);
1677 }
1678 ic->ic_curchan = c;
1679
1680 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1681 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1682 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1683 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1684
1685 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1686 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1687 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7 | 1);
1688 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1689
1690 rum_rf_write(sc, RT2573_RF1, rfprog[i].r1);
1691 rum_rf_write(sc, RT2573_RF2, rfprog[i].r2);
1692 rum_rf_write(sc, RT2573_RF3, rfprog[i].r3 | power << 7);
1693 rum_rf_write(sc, RT2573_RF4, rfprog[i].r4 | sc->rffreq << 10);
1694
1695 DELAY(10);
1696
1697 /* enable smart mode for MIMO-capable RFs */
1698 bbp3 = rum_bbp_read(sc, 3);
1699
1700 bbp3 &= ~RT2573_SMART_MODE;
1701 if (sc->rf_rev == RT2573_RF_5225 || sc->rf_rev == RT2573_RF_2527)
1702 bbp3 |= RT2573_SMART_MODE;
1703
1704 rum_bbp_write(sc, 3, bbp3);
1705
1706 if (bbp94 != RT2573_BBPR94_DEFAULT)
1707 rum_bbp_write(sc, 94, bbp94);
1708 }
1709
1710 /*
1711 * Enable TSF synchronization and tell h/w to start sending beacons for IBSS
1712 * and HostAP operating modes.
1713 */
1714 Static void
1715 rum_enable_tsf_sync(struct rum_softc *sc)
1716 {
1717 struct ieee80211com *ic = &sc->sc_ic;
1718 uint32_t tmp;
1719
1720 if (ic->ic_opmode != IEEE80211_M_STA) {
1721 /*
1722 * Change default 16ms TBTT adjustment to 8ms.
1723 * Must be done before enabling beacon generation.
1724 */
1725 rum_write(sc, RT2573_TXRX_CSR10, 1 << 12 | 8);
1726 }
1727
1728 tmp = rum_read(sc, RT2573_TXRX_CSR9) & 0xff000000;
1729
1730 /* set beacon interval (in 1/16ms unit) */
1731 tmp |= ic->ic_bss->ni_intval * 16;
1732
1733 tmp |= RT2573_TSF_TICKING | RT2573_ENABLE_TBTT;
1734 if (ic->ic_opmode == IEEE80211_M_STA)
1735 tmp |= RT2573_TSF_MODE(1);
1736 else
1737 tmp |= RT2573_TSF_MODE(2) | RT2573_GENERATE_BEACON;
1738
1739 rum_write(sc, RT2573_TXRX_CSR9, tmp);
1740 }
1741
1742 Static void
1743 rum_update_slot(struct rum_softc *sc)
1744 {
1745 struct ieee80211com *ic = &sc->sc_ic;
1746 uint8_t slottime;
1747 uint32_t tmp;
1748
1749 slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
1750
1751 tmp = rum_read(sc, RT2573_MAC_CSR9);
1752 tmp = (tmp & ~0xff) | slottime;
1753 rum_write(sc, RT2573_MAC_CSR9, tmp);
1754
1755 DPRINTF(("setting slot time to %uus\n", slottime));
1756 }
1757
1758 Static void
1759 rum_set_bssid(struct rum_softc *sc, const uint8_t *bssid)
1760 {
1761 uint32_t tmp;
1762
1763 tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24;
1764 rum_write(sc, RT2573_MAC_CSR4, tmp);
1765
1766 tmp = bssid[4] | bssid[5] << 8 | RT2573_ONE_BSSID << 16;
1767 rum_write(sc, RT2573_MAC_CSR5, tmp);
1768 }
1769
1770 Static void
1771 rum_set_macaddr(struct rum_softc *sc, const uint8_t *addr)
1772 {
1773 uint32_t tmp;
1774
1775 tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24;
1776 rum_write(sc, RT2573_MAC_CSR2, tmp);
1777
1778 tmp = addr[4] | addr[5] << 8 | 0xff << 16;
1779 rum_write(sc, RT2573_MAC_CSR3, tmp);
1780 }
1781
1782 Static void
1783 rum_update_promisc(struct rum_softc *sc)
1784 {
1785 struct ifnet *ifp = sc->sc_ic.ic_ifp;
1786 uint32_t tmp;
1787
1788 tmp = rum_read(sc, RT2573_TXRX_CSR0);
1789
1790 tmp &= ~RT2573_DROP_NOT_TO_ME;
1791 if (!(ifp->if_flags & IFF_PROMISC))
1792 tmp |= RT2573_DROP_NOT_TO_ME;
1793
1794 rum_write(sc, RT2573_TXRX_CSR0, tmp);
1795
1796 DPRINTF(("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
1797 "entering" : "leaving"));
1798 }
1799
1800 Static const char *
1801 rum_get_rf(int rev)
1802 {
1803 switch (rev) {
1804 case RT2573_RF_2527: return "RT2527 (MIMO XR)";
1805 case RT2573_RF_2528: return "RT2528";
1806 case RT2573_RF_5225: return "RT5225 (MIMO XR)";
1807 case RT2573_RF_5226: return "RT5226";
1808 default: return "unknown";
1809 }
1810 }
1811
1812 Static void
1813 rum_read_eeprom(struct rum_softc *sc)
1814 {
1815 struct ieee80211com *ic = &sc->sc_ic;
1816 uint16_t val;
1817 #ifdef RUM_DEBUG
1818 int i;
1819 #endif
1820
1821 /* read MAC/BBP type */
1822 rum_eeprom_read(sc, RT2573_EEPROM_MACBBP, &val, 2);
1823 sc->macbbp_rev = le16toh(val);
1824
1825 /* read MAC address */
1826 rum_eeprom_read(sc, RT2573_EEPROM_ADDRESS, ic->ic_myaddr, 6);
1827
1828 rum_eeprom_read(sc, RT2573_EEPROM_ANTENNA, &val, 2);
1829 val = le16toh(val);
1830 sc->rf_rev = (val >> 11) & 0x1f;
1831 sc->hw_radio = (val >> 10) & 0x1;
1832 sc->rx_ant = (val >> 4) & 0x3;
1833 sc->tx_ant = (val >> 2) & 0x3;
1834 sc->nb_ant = val & 0x3;
1835
1836 DPRINTF(("RF revision=%d\n", sc->rf_rev));
1837
1838 rum_eeprom_read(sc, RT2573_EEPROM_CONFIG2, &val, 2);
1839 val = le16toh(val);
1840 sc->ext_5ghz_lna = (val >> 6) & 0x1;
1841 sc->ext_2ghz_lna = (val >> 4) & 0x1;
1842
1843 DPRINTF(("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n",
1844 sc->ext_2ghz_lna, sc->ext_5ghz_lna));
1845
1846 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_2GHZ_OFFSET, &val, 2);
1847 val = le16toh(val);
1848 if ((val & 0xff) != 0xff)
1849 sc->rssi_2ghz_corr = (int8_t)(val & 0xff); /* signed */
1850
1851 rum_eeprom_read(sc, RT2573_EEPROM_RSSI_5GHZ_OFFSET, &val, 2);
1852 val = le16toh(val);
1853 if ((val & 0xff) != 0xff)
1854 sc->rssi_5ghz_corr = (int8_t)(val & 0xff); /* signed */
1855
1856 DPRINTF(("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n",
1857 sc->rssi_2ghz_corr, sc->rssi_5ghz_corr));
1858
1859 rum_eeprom_read(sc, RT2573_EEPROM_FREQ_OFFSET, &val, 2);
1860 val = le16toh(val);
1861 if ((val & 0xff) != 0xff)
1862 sc->rffreq = val & 0xff;
1863
1864 DPRINTF(("RF freq=%d\n", sc->rffreq));
1865
1866 /* read Tx power for all a/b/g channels */
1867 rum_eeprom_read(sc, RT2573_EEPROM_TXPOWER, sc->txpow, 14);
1868 /* XXX default Tx power for 802.11a channels */
1869 memset(sc->txpow + 14, 24, sizeof (sc->txpow) - 14);
1870 #ifdef RUM_DEBUG
1871 for (i = 0; i < 14; i++)
1872 DPRINTF(("Channel=%d Tx power=%d\n", i + 1, sc->txpow[i]));
1873 #endif
1874
1875 /* read default values for BBP registers */
1876 rum_eeprom_read(sc, RT2573_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
1877 #ifdef RUM_DEBUG
1878 for (i = 0; i < 14; i++) {
1879 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
1880 continue;
1881 DPRINTF(("BBP R%d=%02x\n", sc->bbp_prom[i].reg,
1882 sc->bbp_prom[i].val));
1883 }
1884 #endif
1885 }
1886
1887 Static int
1888 rum_bbp_init(struct rum_softc *sc)
1889 {
1890 #define N(a) (sizeof (a) / sizeof ((a)[0]))
1891 int i, ntries;
1892 uint8_t val;
1893
1894 /* wait for BBP to be ready */
1895 for (ntries = 0; ntries < 100; ntries++) {
1896 val = rum_bbp_read(sc, 0);
1897 if (val != 0 && val != 0xff)
1898 break;
1899 DELAY(1000);
1900 }
1901 if (ntries == 100) {
1902 printf("%s: timeout waiting for BBP\n",
1903 USBDEVNAME(sc->sc_dev));
1904 return EIO;
1905 }
1906
1907 /* initialize BBP registers to default values */
1908 for (i = 0; i < N(rum_def_bbp); i++)
1909 rum_bbp_write(sc, rum_def_bbp[i].reg, rum_def_bbp[i].val);
1910
1911 /* write vendor-specific BBP values (from EEPROM) */
1912 for (i = 0; i < 16; i++) {
1913 if (sc->bbp_prom[i].reg == 0 || sc->bbp_prom[i].reg == 0xff)
1914 continue;
1915 rum_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
1916 }
1917
1918 return 0;
1919 #undef N
1920 }
1921
1922 Static int
1923 rum_init(struct ifnet *ifp)
1924 {
1925 #define N(a) (sizeof (a) / sizeof ((a)[0]))
1926 struct rum_softc *sc = ifp->if_softc;
1927 struct ieee80211com *ic = &sc->sc_ic;
1928 struct rum_rx_data *data;
1929 uint32_t tmp;
1930 usbd_status error = 0;
1931 int i, ntries;
1932
1933 if ((sc->sc_flags & RT2573_FWLOADED) == 0) {
1934 if (rum_attachhook(sc))
1935 goto fail;
1936 }
1937
1938 rum_stop(ifp, 0);
1939
1940 /* initialize MAC registers to default values */
1941 for (i = 0; i < N(rum_def_mac); i++)
1942 rum_write(sc, rum_def_mac[i].reg, rum_def_mac[i].val);
1943
1944 /* set host ready */
1945 rum_write(sc, RT2573_MAC_CSR1, 3);
1946 rum_write(sc, RT2573_MAC_CSR1, 0);
1947
1948 /* wait for BBP/RF to wakeup */
1949 for (ntries = 0; ntries < 1000; ntries++) {
1950 if (rum_read(sc, RT2573_MAC_CSR12) & 8)
1951 break;
1952 rum_write(sc, RT2573_MAC_CSR12, 4); /* force wakeup */
1953 DELAY(1000);
1954 }
1955 if (ntries == 1000) {
1956 printf("%s: timeout waiting for BBP/RF to wakeup\n",
1957 USBDEVNAME(sc->sc_dev));
1958 goto fail;
1959 }
1960
1961 if ((error = rum_bbp_init(sc)) != 0)
1962 goto fail;
1963
1964 /* select default channel */
1965 rum_select_band(sc, ic->ic_curchan);
1966 rum_select_antenna(sc);
1967 rum_set_chan(sc, ic->ic_curchan);
1968
1969 /* clear STA registers */
1970 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
1971
1972 IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl));
1973 rum_set_macaddr(sc, ic->ic_myaddr);
1974
1975 /* initialize ASIC */
1976 rum_write(sc, RT2573_MAC_CSR1, 4);
1977
1978 /*
1979 * Allocate xfer for AMRR statistics requests.
1980 */
1981 sc->amrr_xfer = usbd_alloc_xfer(sc->sc_udev);
1982 if (sc->amrr_xfer == NULL) {
1983 printf("%s: could not allocate AMRR xfer\n",
1984 USBDEVNAME(sc->sc_dev));
1985 goto fail;
1986 }
1987
1988 /*
1989 * Open Tx and Rx USB bulk pipes.
1990 */
1991 error = usbd_open_pipe(sc->sc_iface, sc->sc_tx_no, USBD_EXCLUSIVE_USE,
1992 &sc->sc_tx_pipeh);
1993 if (error != 0) {
1994 printf("%s: could not open Tx pipe: %s\n",
1995 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1996 goto fail;
1997 }
1998
1999 error = usbd_open_pipe(sc->sc_iface, sc->sc_rx_no, USBD_EXCLUSIVE_USE,
2000 &sc->sc_rx_pipeh);
2001 if (error != 0) {
2002 printf("%s: could not open Rx pipe: %s\n",
2003 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
2004 goto fail;
2005 }
2006
2007 /*
2008 * Allocate Tx and Rx xfer queues.
2009 */
2010 error = rum_alloc_tx_list(sc);
2011 if (error != 0) {
2012 printf("%s: could not allocate Tx list\n",
2013 USBDEVNAME(sc->sc_dev));
2014 goto fail;
2015 }
2016
2017 error = rum_alloc_rx_list(sc);
2018 if (error != 0) {
2019 printf("%s: could not allocate Rx list\n",
2020 USBDEVNAME(sc->sc_dev));
2021 goto fail;
2022 }
2023
2024 /*
2025 * Start up the receive pipe.
2026 */
2027 for (i = 0; i < RT2573_RX_LIST_COUNT; i++) {
2028 data = &sc->rx_data[i];
2029
2030 usbd_setup_xfer(data->xfer, sc->sc_rx_pipeh, data, data->buf,
2031 MCLBYTES, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rum_rxeof);
2032 usbd_transfer(data->xfer);
2033 }
2034
2035 /* update Rx filter */
2036 tmp = rum_read(sc, RT2573_TXRX_CSR0) & 0xffff;
2037
2038 tmp |= RT2573_DROP_PHY_ERROR | RT2573_DROP_CRC_ERROR;
2039 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2040 tmp |= RT2573_DROP_CTL | RT2573_DROP_VER_ERROR |
2041 RT2573_DROP_ACKCTS;
2042 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2043 tmp |= RT2573_DROP_TODS;
2044 if (!(ifp->if_flags & IFF_PROMISC))
2045 tmp |= RT2573_DROP_NOT_TO_ME;
2046 }
2047 rum_write(sc, RT2573_TXRX_CSR0, tmp);
2048
2049 ifp->if_flags &= ~IFF_OACTIVE;
2050 ifp->if_flags |= IFF_RUNNING;
2051
2052 if (ic->ic_opmode == IEEE80211_M_MONITOR)
2053 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2054 else
2055 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2056
2057 return 0;
2058
2059 fail: rum_stop(ifp, 1);
2060 return error;
2061 #undef N
2062 }
2063
2064 Static void
2065 rum_stop(struct ifnet *ifp, int disable)
2066 {
2067 struct rum_softc *sc = ifp->if_softc;
2068 struct ieee80211com *ic = &sc->sc_ic;
2069 uint32_t tmp;
2070
2071 ieee80211_new_state(ic, IEEE80211_S_INIT, -1); /* free all nodes */
2072
2073 sc->sc_tx_timer = 0;
2074 ifp->if_timer = 0;
2075 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2076
2077 /* disable Rx */
2078 tmp = rum_read(sc, RT2573_TXRX_CSR0);
2079 rum_write(sc, RT2573_TXRX_CSR0, tmp | RT2573_DISABLE_RX);
2080
2081 /* reset ASIC */
2082 rum_write(sc, RT2573_MAC_CSR1, 3);
2083 rum_write(sc, RT2573_MAC_CSR1, 0);
2084
2085 if (sc->sc_rx_pipeh != NULL) {
2086 usbd_abort_pipe(sc->sc_rx_pipeh);
2087 usbd_close_pipe(sc->sc_rx_pipeh);
2088 sc->sc_rx_pipeh = NULL;
2089 }
2090
2091 if (sc->sc_tx_pipeh != NULL) {
2092 usbd_abort_pipe(sc->sc_tx_pipeh);
2093 usbd_close_pipe(sc->sc_tx_pipeh);
2094 sc->sc_tx_pipeh = NULL;
2095 }
2096
2097 rum_free_rx_list(sc);
2098 rum_free_tx_list(sc);
2099 }
2100
2101 Static int
2102 rum_load_microcode(struct rum_softc *sc, const u_char *ucode, size_t size)
2103 {
2104 usb_device_request_t req;
2105 uint16_t reg = RT2573_MCU_CODE_BASE;
2106 usbd_status error;
2107
2108 /* copy firmware image into NIC */
2109 for (; size >= 4; reg += 4, ucode += 4, size -= 4)
2110 rum_write(sc, reg, UGETDW(ucode));
2111
2112 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2113 req.bRequest = RT2573_MCU_CNTL;
2114 USETW(req.wValue, RT2573_MCU_RUN);
2115 USETW(req.wIndex, 0);
2116 USETW(req.wLength, 0);
2117
2118 error = usbd_do_request(sc->sc_udev, &req, NULL);
2119 if (error != 0) {
2120 printf("%s: could not run firmware: %s\n",
2121 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
2122 }
2123 return error;
2124 }
2125
2126 Static int
2127 rum_prepare_beacon(struct rum_softc *sc)
2128 {
2129 struct ieee80211com *ic = &sc->sc_ic;
2130 struct rum_tx_desc desc;
2131 struct mbuf *m0;
2132 int rate;
2133
2134 m0 = ieee80211_beacon_alloc(ic, ic->ic_bss, &sc->sc_bo);
2135 if (m0 == NULL) {
2136 printf("%s: could not allocate beacon frame\n",
2137 sc->sc_dev.dv_xname);
2138 return ENOBUFS;
2139 }
2140
2141 /* send beacons at the lowest available rate */
2142 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
2143
2144 rum_setup_tx_desc(sc, &desc, RT2573_TX_TIMESTAMP, RT2573_TX_HWSEQ,
2145 m0->m_pkthdr.len, rate);
2146
2147 /* copy the first 24 bytes of Tx descriptor into NIC memory */
2148 rum_write_multi(sc, RT2573_HW_BEACON_BASE0, (uint8_t *)&desc, 24);
2149
2150 /* copy beacon header and payload into NIC memory */
2151 rum_write_multi(sc, RT2573_HW_BEACON_BASE0 + 24, mtod(m0, uint8_t *),
2152 m0->m_pkthdr.len);
2153
2154 m_freem(m0);
2155
2156 return 0;
2157 }
2158
2159 Static void
2160 rum_amrr_start(struct rum_softc *sc, struct ieee80211_node *ni)
2161 {
2162 int i;
2163
2164 /* clear statistic registers (STA_CSR0 to STA_CSR5) */
2165 rum_read_multi(sc, RT2573_STA_CSR0, sc->sta, sizeof sc->sta);
2166
2167 ieee80211_amrr_node_init(&sc->amrr, &sc->amn);
2168
2169 /* set rate to some reasonable initial value */
2170 for (i = ni->ni_rates.rs_nrates - 1;
2171 i > 0 && (ni->ni_rates.rs_rates[i] & IEEE80211_RATE_VAL) > 72;
2172 i--);
2173 ni->ni_txrate = i;
2174
2175 callout_reset(&sc->amrr_ch, hz, rum_amrr_timeout, sc);
2176 }
2177
2178 Static void
2179 rum_amrr_timeout(void *arg)
2180 {
2181 struct rum_softc *sc = arg;
2182 usb_device_request_t req;
2183 int s;
2184
2185 s = splusb();
2186
2187 /*
2188 * Asynchronously read statistic registers (cleared by read).
2189 */
2190 req.bmRequestType = UT_READ_VENDOR_DEVICE;
2191 req.bRequest = RT2573_READ_MULTI_MAC;
2192 USETW(req.wValue, 0);
2193 USETW(req.wIndex, RT2573_STA_CSR0);
2194 USETW(req.wLength, sizeof sc->sta);
2195
2196 usbd_setup_default_xfer(sc->amrr_xfer, sc->sc_udev, sc,
2197 USBD_DEFAULT_TIMEOUT, &req, sc->sta, sizeof sc->sta, 0,
2198 rum_amrr_update);
2199 (void)usbd_transfer(sc->amrr_xfer);
2200
2201 splx(s);
2202 }
2203
2204 Static void
2205 rum_amrr_update(usbd_xfer_handle xfer, usbd_private_handle priv,
2206 usbd_status status)
2207 {
2208 struct rum_softc *sc = (struct rum_softc *)priv;
2209 struct ifnet *ifp = sc->sc_ic.ic_ifp;
2210
2211 if (status != USBD_NORMAL_COMPLETION) {
2212 printf("%s: could not retrieve Tx statistics - cancelling "
2213 "automatic rate control\n", USBDEVNAME(sc->sc_dev));
2214 return;
2215 }
2216
2217 /* count TX retry-fail as Tx errors */
2218 ifp->if_oerrors += le32toh(sc->sta[5]) >> 16;
2219
2220 sc->amn.amn_retrycnt =
2221 (le32toh(sc->sta[4]) >> 16) + /* TX one-retry ok count */
2222 (le32toh(sc->sta[5]) & 0xffff) + /* TX more-retry ok count */
2223 (le32toh(sc->sta[5]) >> 16); /* TX retry-fail count */
2224
2225 sc->amn.amn_txcnt =
2226 sc->amn.amn_retrycnt +
2227 (le32toh(sc->sta[4]) & 0xffff); /* TX no-retry ok count */
2228
2229 ieee80211_amrr_choose(&sc->amrr, sc->sc_ic.ic_bss, &sc->amn);
2230
2231 callout_reset(&sc->amrr_ch, hz, rum_amrr_timeout, sc);
2232 }
2233
2234 int
2235 rum_activate(device_ptr_t self, enum devact act)
2236 {
2237 switch (act) {
2238 case DVACT_ACTIVATE:
2239 return EOPNOTSUPP;
2240
2241 case DVACT_DEACTIVATE:
2242 /*if_deactivate(&sc->sc_ic.ic_if);*/
2243 break;
2244 }
2245
2246 return 0;
2247 }
2248