1 /* $OpenBSD: if_ral.c,v 1.36 2005/06/20 18:54:59 damien Exp $ */
2
3 /*-
4 * Copyright (c) 2005
5 * Damien Bergamini <damien.bergamini@free.fr>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20 /*-
21 * Ralink Technology RT2500USB chipset driver
22 * http://www.ralinktech.com/
23 */
24
25 #include "bpfilter.h"
26
27 #include <sys/param.h>
28 #include <sys/sockio.h>
29 #include <sys/sysctl.h>
30 #include <sys/mbuf.h>
31 #include <sys/kernel.h>
32 #include <sys/socket.h>
33 #include <sys/systm.h>
34 #include <sys/malloc.h>
35 #include <sys/timeout.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_media.h>
50 #include <net/if_types.h>
51
52 #include <netinet/in.h>
53 #include <netinet/in_systm.h>
54 #include <netinet/in_var.h>
55 #include <netinet/if_ether.h>
56 #include <netinet/ip.h>
57
58 #include <net80211/ieee80211_var.h>
59 #include <net80211/ieee80211_rssadapt.h>
60 #include <net80211/ieee80211_radiotap.h>
61
62 #include <dev/usb/usb.h>
63 #include <dev/usb/usbdi.h>
64 #include <dev/usb/usbdi_util.h>
65 #include <dev/usb/usbdevs.h>
66
67 #include <dev/usb/if_ralreg.h>
68 #include <dev/usb/if_ralvar.h>
69
70 #ifdef USB_DEBUG
71 #define URAL_DEBUG
72 #endif
73
74 #ifdef URAL_DEBUG
75 #define DPRINTF(x) do { if (ural_debug) logprintf x; } while (0)
76 #define DPRINTFN(n, x) do { if (ural_debug >= (n)) logprintf x; } while (0)
77 int ural_debug = 0;
78 #else
79 #define DPRINTF(x)
80 #define DPRINTFN(n, x)
81 #endif
82
83 /* various supported device vendors/products */
84 static const struct usb_devno ural_devs[] = {
85 { USB_VENDOR_ASUS, USB_PRODUCT_ASUS_RT2570 },
86 { USB_VENDOR_ASUS, USB_PRODUCT_RALINK_RT2570 },
87 { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D7050 },
88 { USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_WUSB54G },
89 { USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_WUSB54GP },
90 { USB_VENDOR_CONCEPTRONIC2, USB_PRODUCT_CONCEPTRONIC2_C54RU },
91 { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_RT2570 },
92 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_KG54 },
93 { USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2570 },
94 { USB_VENDOR_RALINK, USB_PRODUCT_RALINK_RT2570_2 },
95 { USB_VENDOR_SMC, USB_PRODUCT_SMC_RT2570 }
96 };
97
98 Static int ural_alloc_tx_list(struct ural_softc *);
99 Static void ural_free_tx_list(struct ural_softc *);
100 Static int ural_alloc_rx_list(struct ural_softc *);
101 Static void ural_free_rx_list(struct ural_softc *);
102 Static int ural_media_change(struct ifnet *);
103 Static void ural_next_scan(void *);
104 Static void ural_task(void *);
105 Static int ural_newstate(struct ieee80211com *,
106 enum ieee80211_state, int);
107 Static void ural_txeof(usbd_xfer_handle, usbd_private_handle,
108 usbd_status);
109 Static void ural_rxeof(usbd_xfer_handle, usbd_private_handle,
110 usbd_status);
111 Static int ural_ack_rate(int);
112 Static uint16_t ural_txtime(int, int, uint32_t);
113 Static uint8_t ural_plcp_signal(int);
114 Static void ural_setup_tx_desc(struct ural_softc *,
115 struct ural_tx_desc *, uint32_t, int, int);
116 Static int ural_tx_bcn(struct ural_softc *, struct mbuf *,
117 struct ieee80211_node *);
118 Static int ural_tx_mgt(struct ural_softc *, struct mbuf *,
119 struct ieee80211_node *);
120 Static int ural_tx_data(struct ural_softc *, struct mbuf *,
121 struct ieee80211_node *);
122 Static void ural_start(struct ifnet *);
123 Static void ural_watchdog(struct ifnet *);
124 Static int ural_ioctl(struct ifnet *, u_long, caddr_t);
125 Static void ural_eeprom_read(struct ural_softc *, uint16_t, void *,
126 int);
127 Static uint16_t ural_read(struct ural_softc *, uint16_t);
128 Static void ural_read_multi(struct ural_softc *, uint16_t, void *,
129 int);
130 Static void ural_write(struct ural_softc *, uint16_t, uint16_t);
131 Static void ural_write_multi(struct ural_softc *, uint16_t, void *,
132 int);
133 Static void ural_bbp_write(struct ural_softc *, uint8_t, uint8_t);
134 Static uint8_t ural_bbp_read(struct ural_softc *, uint8_t);
135 Static void ural_rf_write(struct ural_softc *, uint8_t, uint32_t);
136 Static void ural_set_chan(struct ural_softc *,
137 struct ieee80211_channel *);
138 Static void ural_disable_rf_tune(struct ural_softc *);
139 Static void ural_enable_tsf_sync(struct ural_softc *);
140 Static void ural_set_bssid(struct ural_softc *, uint8_t *);
141 Static void ural_set_macaddr(struct ural_softc *, uint8_t *);
142 Static void ural_update_promisc(struct ural_softc *);
143 Static const char *ural_get_rf(int);
144 Static void ural_read_eeprom(struct ural_softc *);
145 Static int ural_bbp_init(struct ural_softc *);
146 Static void ural_set_txantenna(struct ural_softc *, int);
147 Static void ural_set_rxantenna(struct ural_softc *, int);
148 Static int ural_init(struct ifnet *);
149 Static void ural_stop(struct ifnet *, int);
150
151 /*
152 * Supported rates for 802.11a/b/g modes (in 500Kbps unit).
153 */
154 static const struct ieee80211_rateset ural_rateset_11a =
155 { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
156
157 static const struct ieee80211_rateset ural_rateset_11b =
158 { 4, { 2, 4, 11, 22 } };
159
160 static const struct ieee80211_rateset ural_rateset_11g =
161 { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
162
163 /*
164 * Default values for MAC registers; values taken from the reference driver.
165 */
166 static const struct {
167 uint16_t reg;
168 uint16_t val;
169 } ural_def_mac[] = {
170 { RAL_TXRX_CSR5, 0x8c8d },
171 { RAL_TXRX_CSR6, 0x8b8a },
172 { RAL_TXRX_CSR7, 0x8687 },
173 { RAL_TXRX_CSR8, 0x0085 },
174 { RAL_MAC_CSR13, 0x1111 },
175 { RAL_MAC_CSR14, 0x1e11 },
176 { RAL_TXRX_CSR21, 0xe78f },
177 { RAL_MAC_CSR9, 0xff1d },
178 { RAL_MAC_CSR11, 0x0002 },
179 { RAL_MAC_CSR22, 0x0053 },
180 { RAL_MAC_CSR15, 0x0000 },
181 { RAL_MAC_CSR8, 0x0780 },
182 { RAL_TXRX_CSR19, 0x0000 },
183 { RAL_TXRX_CSR18, 0x005a },
184 { RAL_PHY_CSR2, 0x0000 },
185 { RAL_TXRX_CSR0, 0x1ec0 },
186 { RAL_PHY_CSR4, 0x000f }
187 };
188
189 /*
190 * Default values for BBP registers; values taken from the reference driver.
191 */
192 static const struct {
193 uint8_t reg;
194 uint8_t val;
195 } ural_def_bbp[] = {
196 { 3, 0x02 },
197 { 4, 0x19 },
198 { 14, 0x1c },
199 { 15, 0x30 },
200 { 16, 0xac },
201 { 17, 0x48 },
202 { 18, 0x18 },
203 { 19, 0xff },
204 { 20, 0x1e },
205 { 21, 0x08 },
206 { 22, 0x08 },
207 { 23, 0x08 },
208 { 24, 0x80 },
209 { 25, 0x50 },
210 { 26, 0x08 },
211 { 27, 0x23 },
212 { 30, 0x10 },
213 { 31, 0x2b },
214 { 32, 0xb9 },
215 { 34, 0x12 },
216 { 35, 0x50 },
217 { 39, 0xc4 },
218 { 40, 0x02 },
219 { 41, 0x60 },
220 { 53, 0x10 },
221 { 54, 0x18 },
222 { 56, 0x08 },
223 { 57, 0x10 },
224 { 58, 0x08 },
225 { 61, 0x60 },
226 { 62, 0x10 },
227 { 75, 0xff }
228 };
229
230 /*
231 * Default values for RF register R2 indexed by channel numbers.
232 */
233 static const uint32_t ural_rf2522_r2[] = {
234 0x307f6, 0x307fb, 0x30800, 0x30805, 0x3080a, 0x3080f, 0x30814,
235 0x30819, 0x3081e, 0x30823, 0x30828, 0x3082d, 0x30832, 0x3083e
236 };
237
238 static const uint32_t ural_rf2523_r2[] = {
239 0x00327, 0x00328, 0x00329, 0x0032a, 0x0032b, 0x0032c, 0x0032d,
240 0x0032e, 0x0032f, 0x00340, 0x00341, 0x00342, 0x00343, 0x00346
241 };
242
243 static const uint32_t ural_rf2524_r2[] = {
244 0x00327, 0x00328, 0x00329, 0x0032a, 0x0032b, 0x0032c, 0x0032d,
245 0x0032e, 0x0032f, 0x00340, 0x00341, 0x00342, 0x00343, 0x00346
246 };
247
248 static const uint32_t ural_rf2525_r2[] = {
249 0x20327, 0x20328, 0x20329, 0x2032a, 0x2032b, 0x2032c, 0x2032d,
250 0x2032e, 0x2032f, 0x20340, 0x20341, 0x20342, 0x20343, 0x20346
251 };
252
253 static const uint32_t ural_rf2525_hi_r2[] = {
254 0x2032f, 0x20340, 0x20341, 0x20342, 0x20343, 0x20344, 0x20345,
255 0x20346, 0x20347, 0x20348, 0x20349, 0x2034a, 0x2034b, 0x2034e
256 };
257
258 static const uint32_t ural_rf2525e_r2[] = {
259 0x2044d, 0x2044e, 0x2044f, 0x20460, 0x20461, 0x20462, 0x20463,
260 0x20464, 0x20465, 0x20466, 0x20467, 0x20468, 0x20469, 0x2046b
261 };
262
263 static const uint32_t ural_rf2526_hi_r2[] = {
264 0x0022a, 0x0022b, 0x0022b, 0x0022c, 0x0022c, 0x0022d, 0x0022d,
265 0x0022e, 0x0022e, 0x0022f, 0x0022d, 0x00240, 0x00240, 0x00241
266 };
267
268 static const uint32_t ural_rf2526_r2[] = {
269 0x00226, 0x00227, 0x00227, 0x00228, 0x00228, 0x00229, 0x00229,
270 0x0022a, 0x0022a, 0x0022b, 0x0022b, 0x0022c, 0x0022c, 0x0022d
271 };
272
273 /*
274 * For dual-band RF, RF registers R1 and R4 also depend on channel number;
275 * values taken from the reference driver.
276 */
277 static const struct {
278 uint8_t chan;
279 uint32_t r1;
280 uint32_t r2;
281 uint32_t r4;
282 } ural_rf5222[] = {
283 /* channels in the 2.4GHz band */
284 { 1, 0x08808, 0x0044d, 0x00282 },
285 { 2, 0x08808, 0x0044e, 0x00282 },
286 { 3, 0x08808, 0x0044f, 0x00282 },
287 { 4, 0x08808, 0x00460, 0x00282 },
288 { 5, 0x08808, 0x00461, 0x00282 },
289 { 6, 0x08808, 0x00462, 0x00282 },
290 { 7, 0x08808, 0x00463, 0x00282 },
291 { 8, 0x08808, 0x00464, 0x00282 },
292 { 9, 0x08808, 0x00465, 0x00282 },
293 { 10, 0x08808, 0x00466, 0x00282 },
294 { 11, 0x08808, 0x00467, 0x00282 },
295 { 12, 0x08808, 0x00468, 0x00282 },
296 { 13, 0x08808, 0x00469, 0x00282 },
297 { 14, 0x08808, 0x0046b, 0x00286 },
298
299 /* channels in the 5.2GHz band */
300 { 36, 0x08804, 0x06225, 0x00287 },
301 { 40, 0x08804, 0x06226, 0x00287 },
302 { 44, 0x08804, 0x06227, 0x00287 },
303 { 48, 0x08804, 0x06228, 0x00287 },
304 { 52, 0x08804, 0x06229, 0x00287 },
305 { 56, 0x08804, 0x0622a, 0x00287 },
306 { 60, 0x08804, 0x0622b, 0x00287 },
307 { 64, 0x08804, 0x0622c, 0x00287 },
308
309 { 100, 0x08804, 0x02200, 0x00283 },
310 { 104, 0x08804, 0x02201, 0x00283 },
311 { 108, 0x08804, 0x02202, 0x00283 },
312 { 112, 0x08804, 0x02203, 0x00283 },
313 { 116, 0x08804, 0x02204, 0x00283 },
314 { 120, 0x08804, 0x02205, 0x00283 },
315 { 124, 0x08804, 0x02206, 0x00283 },
316 { 128, 0x08804, 0x02207, 0x00283 },
317 { 132, 0x08804, 0x02208, 0x00283 },
318 { 136, 0x08804, 0x02209, 0x00283 },
319 { 140, 0x08804, 0x0220a, 0x00283 },
320
321 { 149, 0x08808, 0x02429, 0x00281 },
322 { 153, 0x08808, 0x0242b, 0x00281 },
323 { 157, 0x08808, 0x0242d, 0x00281 },
324 { 161, 0x08808, 0x0242f, 0x00281 }
325 };
326
327 USB_DECLARE_DRIVER(ural);
328
USB_MATCH(ural)329 USB_MATCH(ural)
330 {
331 USB_MATCH_START(ural, uaa);
332
333 if (uaa->iface != NULL)
334 return UMATCH_NONE;
335
336 return (usb_lookup(ural_devs, uaa->vendor, uaa->product) != NULL) ?
337 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
338 }
339
USB_ATTACH(ural)340 USB_ATTACH(ural)
341 {
342 USB_ATTACH_START(ural, sc, uaa);
343 struct ieee80211com *ic = &sc->sc_ic;
344 struct ifnet *ifp = &ic->ic_if;
345 usb_interface_descriptor_t *id;
346 usb_endpoint_descriptor_t *ed;
347 usbd_status error;
348 char devinfo[1024];
349 int i;
350
351 sc->sc_udev = uaa->device;
352
353 usbd_devinfo(sc->sc_udev, 0, devinfo, sizeof devinfo);
354 USB_ATTACH_SETUP;
355 printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
356
357 if (usbd_set_config_no(sc->sc_udev, RAL_CONFIG_NO, 0) != 0) {
358 printf("%s: could not set configuration no\n",
359 USBDEVNAME(sc->sc_dev));
360 USB_ATTACH_ERROR_RETURN;
361 }
362
363 /* get the first interface handle */
364 error = usbd_device2interface_handle(sc->sc_udev, RAL_IFACE_INDEX,
365 &sc->sc_iface);
366 if (error != 0) {
367 printf("%s: could not get interface handle\n",
368 USBDEVNAME(sc->sc_dev));
369 USB_ATTACH_ERROR_RETURN;
370 }
371
372 /*
373 * Find endpoints.
374 */
375 id = usbd_get_interface_descriptor(sc->sc_iface);
376
377 sc->sc_rx_no = sc->sc_tx_no = -1;
378 for (i = 0; i < id->bNumEndpoints; i++) {
379 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
380 if (ed == NULL) {
381 printf("%s: no endpoint descriptor for iface %d\n",
382 USBDEVNAME(sc->sc_dev), i);
383 USB_ATTACH_ERROR_RETURN;
384 }
385
386 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
387 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
388 sc->sc_rx_no = ed->bEndpointAddress;
389 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
390 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
391 sc->sc_tx_no = ed->bEndpointAddress;
392 }
393 if (sc->sc_rx_no == -1 || sc->sc_tx_no == -1) {
394 printf("%s: missing endpoint\n", USBDEVNAME(sc->sc_dev));
395 USB_ATTACH_ERROR_RETURN;
396 }
397
398 usb_init_task(&sc->sc_task, ural_task, sc);
399 timeout_set(&sc->scan_ch, ural_next_scan, sc);
400
401 /* retrieve RT2570 rev. no */
402 sc->asic_rev = ural_read(sc, RAL_MAC_CSR0);
403
404 /* retrieve MAC address and various other things from EEPROM */
405 ural_read_eeprom(sc);
406
407 printf("%s: MAC/BBP RT2570 (rev 0x%02x), RF %s, address %s\n",
408 USBDEVNAME(sc->sc_dev), sc->asic_rev, ural_get_rf(sc->rf_rev),
409 ether_sprintf(ic->ic_myaddr));
410
411 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
412 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
413 ic->ic_state = IEEE80211_S_INIT;
414
415 /* set device capabilities */
416 ic->ic_caps = IEEE80211_C_MONITOR | IEEE80211_C_IBSS |
417 IEEE80211_C_HOSTAP | IEEE80211_C_SHPREAMBLE | IEEE80211_C_PMGT |
418 IEEE80211_C_TXPMGT | IEEE80211_C_WEP;
419
420 if (sc->rf_rev == RAL_RF_5222) {
421 /* set supported .11a rates */
422 ic->ic_sup_rates[IEEE80211_MODE_11A] = ural_rateset_11a;
423
424 /* set supported .11a channels */
425 for (i = 36; i <= 64; i += 4) {
426 ic->ic_channels[i].ic_freq =
427 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
428 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
429 }
430 for (i = 100; i <= 140; i += 4) {
431 ic->ic_channels[i].ic_freq =
432 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
433 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
434 }
435 for (i = 149; i <= 161; i += 4) {
436 ic->ic_channels[i].ic_freq =
437 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
438 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
439 }
440 }
441
442 /* set supported .11b and .11g rates */
443 ic->ic_sup_rates[IEEE80211_MODE_11B] = ural_rateset_11b;
444 ic->ic_sup_rates[IEEE80211_MODE_11G] = ural_rateset_11g;
445
446 /* set supported .11b and .11g channels (1 through 14) */
447 for (i = 1; i <= 14; i++) {
448 ic->ic_channels[i].ic_freq =
449 ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
450 ic->ic_channels[i].ic_flags =
451 IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
452 IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
453 }
454
455 ifp->if_softc = sc;
456 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
457 ifp->if_init = ural_init;
458 ifp->if_ioctl = ural_ioctl;
459 ifp->if_start = ural_start;
460 ifp->if_watchdog = ural_watchdog;
461 IFQ_SET_READY(&ifp->if_snd);
462 memcpy(ifp->if_xname, USBDEVNAME(sc->sc_dev), IFNAMSIZ);
463
464 if_attach(ifp);
465 ieee80211_ifattach(ifp);
466
467 /* override state transition machine */
468 sc->sc_newstate = ic->ic_newstate;
469 ic->ic_newstate = ural_newstate;
470 ieee80211_media_init(ifp, ural_media_change, ieee80211_media_status);
471
472 #if NBPFILTER > 0
473 bpfattach(&sc->sc_drvbpf, ifp, DLT_IEEE802_11_RADIO,
474 sizeof (struct ieee80211_frame) + 64);
475
476 sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
477 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
478 sc->sc_rxtap.wr_ihdr.it_present = htole32(RAL_RX_RADIOTAP_PRESENT);
479
480 sc->sc_txtap_len = sizeof sc->sc_txtapu;
481 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
482 sc->sc_txtap.wt_ihdr.it_present = htole32(RAL_TX_RADIOTAP_PRESENT);
483 #endif
484
485 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
486 USBDEV(sc->sc_dev));
487
488 USB_ATTACH_SUCCESS_RETURN;
489 }
490
USB_DETACH(ural)491 USB_DETACH(ural)
492 {
493 USB_DETACH_START(ural, sc);
494 struct ifnet *ifp = &sc->sc_ic.ic_if;
495 int s;
496
497 s = splusb();
498
499 usb_rem_task(sc->sc_udev, &sc->sc_task);
500 timeout_del(&sc->scan_ch);
501
502 if (sc->sc_rx_pipeh != NULL) {
503 usbd_abort_pipe(sc->sc_rx_pipeh);
504 usbd_close_pipe(sc->sc_rx_pipeh);
505 }
506
507 if (sc->sc_tx_pipeh != NULL) {
508 usbd_abort_pipe(sc->sc_tx_pipeh);
509 usbd_close_pipe(sc->sc_tx_pipeh);
510 }
511
512 ural_free_rx_list(sc);
513 ural_free_tx_list(sc);
514
515 #if NBPFILTER > 0
516 bpfdetach(ifp);
517 #endif
518 ieee80211_ifdetach(ifp);
519 if_detach(ifp);
520
521 splx(s);
522
523 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
524 USBDEV(sc->sc_dev));
525
526 return 0;
527 }
528
529 Static int
ural_alloc_tx_list(struct ural_softc * sc)530 ural_alloc_tx_list(struct ural_softc *sc)
531 {
532 struct ural_tx_data *data;
533 int i, error;
534
535 sc->tx_queued = 0;
536
537 for (i = 0; i < RAL_TX_LIST_COUNT; i++) {
538 data = &sc->tx_data[i];
539
540 data->sc = sc;
541
542 data->xfer = usbd_alloc_xfer(sc->sc_udev);
543 if (data->xfer == NULL) {
544 printf("%s: could not allocate tx xfer\n",
545 USBDEVNAME(sc->sc_dev));
546 error = ENOMEM;
547 goto fail;
548 }
549
550 data->buf = usbd_alloc_buffer(data->xfer,
551 RAL_TX_DESC_SIZE + MCLBYTES);
552 if (data->buf == NULL) {
553 printf("%s: could not allocate tx buffer\n",
554 USBDEVNAME(sc->sc_dev));
555 error = ENOMEM;
556 goto fail;
557 }
558 }
559
560 return 0;
561
562 fail: ural_free_tx_list(sc);
563 return error;
564 }
565
566 Static void
ural_free_tx_list(struct ural_softc * sc)567 ural_free_tx_list(struct ural_softc *sc)
568 {
569 struct ieee80211com *ic = &sc->sc_ic;
570 struct ural_tx_data *data;
571 int i;
572
573 for (i = 0; i < RAL_TX_LIST_COUNT; i++) {
574 data = &sc->tx_data[i];
575
576 if (data->xfer != NULL) {
577 usbd_free_xfer(data->xfer);
578 data->xfer = NULL;
579 }
580
581 if (data->ni != NULL) {
582 ieee80211_release_node(ic, data->ni);
583 data->ni = NULL;
584 }
585 }
586 }
587
588 Static int
ural_alloc_rx_list(struct ural_softc * sc)589 ural_alloc_rx_list(struct ural_softc *sc)
590 {
591 struct ural_rx_data *data;
592 int i, error;
593
594 for (i = 0; i < RAL_RX_LIST_COUNT; i++) {
595 data = &sc->rx_data[i];
596
597 data->sc = sc;
598
599 data->xfer = usbd_alloc_xfer(sc->sc_udev);
600 if (data->xfer == NULL) {
601 printf("%s: could not allocate rx xfer\n",
602 USBDEVNAME(sc->sc_dev));
603 error = ENOMEM;
604 goto fail;
605 }
606
607 if (usbd_alloc_buffer(data->xfer, MCLBYTES) == NULL) {
608 printf("%s: could not allocate rx buffer\n",
609 USBDEVNAME(sc->sc_dev));
610 error = ENOMEM;
611 goto fail;
612 }
613
614 MGETHDR(data->m, M_DONTWAIT, MT_DATA);
615 if (data->m == NULL) {
616 printf("%s: could not allocate rx mbuf\n",
617 USBDEVNAME(sc->sc_dev));
618 error = ENOMEM;
619 goto fail;
620 }
621
622 MCLGET(data->m, M_DONTWAIT);
623 if (!(data->m->m_flags & M_EXT)) {
624 printf("%s: could not allocate rx mbuf cluster\n",
625 USBDEVNAME(sc->sc_dev));
626 error = ENOMEM;
627 goto fail;
628 }
629
630 data->buf = mtod(data->m, uint8_t *);
631 }
632
633 return 0;
634
635 fail: ural_free_tx_list(sc);
636 return error;
637 }
638
639 Static void
ural_free_rx_list(struct ural_softc * sc)640 ural_free_rx_list(struct ural_softc *sc)
641 {
642 struct ural_rx_data *data;
643 int i;
644
645 for (i = 0; i < RAL_RX_LIST_COUNT; i++) {
646 data = &sc->rx_data[i];
647
648 if (data->xfer != NULL) {
649 usbd_free_xfer(data->xfer);
650 data->xfer = NULL;
651 }
652
653 if (data->m != NULL) {
654 m_freem(data->m);
655 data->m = NULL;
656 }
657 }
658 }
659
660 Static int
ural_media_change(struct ifnet * ifp)661 ural_media_change(struct ifnet *ifp)
662 {
663 int error;
664
665 error = ieee80211_media_change(ifp);
666 if (error != ENETRESET)
667 return error;
668
669 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
670 ural_init(ifp);
671
672 return 0;
673 }
674
675 /*
676 * This function is called periodically (every 200ms) during scanning to
677 * switch from one channel to another.
678 */
679 Static void
ural_next_scan(void * arg)680 ural_next_scan(void *arg)
681 {
682 struct ural_softc *sc = arg;
683 struct ieee80211com *ic = &sc->sc_ic;
684 struct ifnet *ifp = &ic->ic_if;
685
686 if (ic->ic_state == IEEE80211_S_SCAN)
687 ieee80211_next_scan(ifp);
688 }
689
690 Static void
ural_task(void * arg)691 ural_task(void *arg)
692 {
693 struct ural_softc *sc = arg;
694 struct ieee80211com *ic = &sc->sc_ic;
695 enum ieee80211_state ostate;
696 struct mbuf *m;
697
698 ostate = ic->ic_state;
699
700 switch (sc->sc_state) {
701 case IEEE80211_S_INIT:
702 if (ostate == IEEE80211_S_RUN) {
703 /* abort TSF synchronization */
704 ural_write(sc, RAL_TXRX_CSR19, 0);
705
706 /* force tx led to stop blinking */
707 ural_write(sc, RAL_MAC_CSR20, 0);
708 }
709 break;
710
711 case IEEE80211_S_SCAN:
712 ural_set_chan(sc, ic->ic_bss->ni_chan);
713 timeout_add(&sc->scan_ch, hz / 5);
714 break;
715
716 case IEEE80211_S_AUTH:
717 ural_set_chan(sc, ic->ic_bss->ni_chan);
718 break;
719
720 case IEEE80211_S_ASSOC:
721 ural_set_chan(sc, ic->ic_bss->ni_chan);
722 break;
723
724 case IEEE80211_S_RUN:
725 ural_set_chan(sc, ic->ic_bss->ni_chan);
726
727 if (ic->ic_opmode != IEEE80211_M_MONITOR)
728 ural_set_bssid(sc, ic->ic_bss->ni_bssid);
729
730 if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
731 ic->ic_opmode == IEEE80211_M_IBSS) {
732 m = ieee80211_beacon_alloc(ic, ic->ic_bss);
733 if (m == NULL) {
734 printf("%s: could not allocate beacon\n",
735 USBDEVNAME(sc->sc_dev));
736 return;
737 }
738
739 if (ural_tx_bcn(sc, m, ic->ic_bss) != 0) {
740 m_freem(m);
741 printf("%s: could not transmit beacon\n",
742 USBDEVNAME(sc->sc_dev));
743 return;
744 }
745
746 /* beacon is no longer needed */
747 m_freem(m);
748 }
749
750 /* make tx led blink on tx (controlled by ASIC) */
751 ural_write(sc, RAL_MAC_CSR20, 1);
752
753 if (ic->ic_opmode != IEEE80211_M_MONITOR)
754 ural_enable_tsf_sync(sc);
755 break;
756 }
757
758 sc->sc_newstate(ic, sc->sc_state, -1);
759 }
760
761 Static int
ural_newstate(struct ieee80211com * ic,enum ieee80211_state nstate,int arg)762 ural_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
763 {
764 struct ural_softc *sc = ic->ic_if.if_softc;
765
766 usb_rem_task(sc->sc_udev, &sc->sc_task);
767 timeout_del(&sc->scan_ch);
768
769 /* do it in a process context */
770 sc->sc_state = nstate;
771 usb_add_task(sc->sc_udev, &sc->sc_task);
772
773 return 0;
774 }
775
776 /* quickly determine if a given rate is CCK or OFDM */
777 #define RAL_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
778
779 #define RAL_ACK_SIZE 14 /* 10 + 4(FCS) */
780 #define RAL_CTS_SIZE 14 /* 10 + 4(FCS) */
781 #define RAL_SIFS 10
782
783 Static void
ural_txeof(usbd_xfer_handle xfer,usbd_private_handle priv,usbd_status status)784 ural_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
785 {
786 struct ural_tx_data *data = priv;
787 struct ural_softc *sc = data->sc;
788 struct ieee80211com *ic = &sc->sc_ic;
789 struct ifnet *ifp = &ic->ic_if;
790 int s;
791
792 if (status != USBD_NORMAL_COMPLETION) {
793 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
794 return;
795
796 printf("%s: could not transmit buffer: %s\n",
797 USBDEVNAME(sc->sc_dev), usbd_errstr(status));
798
799 if (status == USBD_STALLED)
800 usbd_clear_endpoint_stall(sc->sc_tx_pipeh);
801
802 ifp->if_oerrors++;
803 return;
804 }
805
806 s = splnet();
807
808 m_freem(data->m);
809 data->m = NULL;
810 ieee80211_release_node(ic, data->ni);
811 data->ni = NULL;
812
813 sc->tx_queued--;
814 ifp->if_opackets++;
815
816 DPRINTFN(10, ("tx done\n"));
817
818 sc->sc_tx_timer = 0;
819 ifp->if_flags &= ~IFF_OACTIVE;
820 ural_start(ifp);
821
822 splx(s);
823 }
824
825 Static void
ural_rxeof(usbd_xfer_handle xfer,usbd_private_handle priv,usbd_status status)826 ural_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
827 {
828 struct ural_rx_data *data = priv;
829 struct ural_softc *sc = data->sc;
830 struct ieee80211com *ic = &sc->sc_ic;
831 struct ifnet *ifp = &ic->ic_if;
832 struct ural_rx_desc *desc;
833 struct ieee80211_frame *wh;
834 struct ieee80211_node *ni;
835 struct mbuf *m;
836 int s, len;
837
838 if (status != USBD_NORMAL_COMPLETION) {
839 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
840 return;
841
842 if (status == USBD_STALLED)
843 usbd_clear_endpoint_stall(sc->sc_rx_pipeh);
844 goto skip;
845 }
846
847 usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
848
849 if (len < RAL_RX_DESC_SIZE) {
850 printf("%s: xfer too short %d\n", USBDEVNAME(sc->sc_dev), len);
851 ifp->if_ierrors++;
852 goto skip;
853 }
854
855 /* rx descriptor is located at the end */
856 desc = (struct ural_rx_desc *)(data->buf + len - RAL_RX_DESC_SIZE);
857
858 if (letoh32(desc->flags) & (RAL_RX_PHY_ERROR | RAL_RX_CRC_ERROR)) {
859 /*
860 * This should not happen since we did not request to receive
861 * those frames when we filled RAL_TXRX_CSR2.
862 */
863 DPRINTFN(5, ("PHY or CRC error\n"));
864 ifp->if_ierrors++;
865 goto skip;
866 }
867
868 /* finalize mbuf */
869 m = data->m;
870 m->m_pkthdr.rcvif = ifp;
871 m->m_pkthdr.len = m->m_len = (letoh32(desc->flags) >> 16) & 0xfff;
872 m->m_flags |= M_HASFCS; /* hardware appends FCS */
873
874 s = splnet();
875
876 #if NBPFILTER > 0
877 if (sc->sc_drvbpf != NULL) {
878 struct mbuf mb;
879 struct ural_rx_radiotap_header *tap = &sc->sc_rxtap;
880
881 tap->wr_flags = 0;
882 tap->wr_chan_freq = htole16(ic->ic_ibss_chan->ic_freq);
883 tap->wr_chan_flags = htole16(ic->ic_ibss_chan->ic_flags);
884 tap->wr_antenna = sc->rx_ant;
885 tap->wr_antsignal = desc->rssi;
886
887 M_DUP_PKTHDR(&mb, m);
888 mb.m_data = (caddr_t)tap;
889 mb.m_len = sc->sc_txtap_len;
890 mb.m_next = m;
891 mb.m_pkthdr.len += mb.m_len;
892 bpf_mtap(sc->sc_drvbpf, &mb);
893 }
894 #endif
895
896 wh = mtod(m, struct ieee80211_frame *);
897 ni = ieee80211_find_rxnode(ic, wh);
898
899 /* send the frame to the 802.11 layer */
900 ieee80211_input(ifp, m, ni, desc->rssi, 0);
901
902 /* node is no longer needed */
903 ieee80211_release_node(ic, ni);
904
905 splx(s);
906
907 MGETHDR(data->m, M_DONTWAIT, MT_DATA);
908 if (data->m == NULL) {
909 printf("%s: could not allocate rx mbuf\n",
910 USBDEVNAME(sc->sc_dev));
911 return;
912 }
913
914 MCLGET(data->m, M_DONTWAIT);
915 if (!(data->m->m_flags & M_EXT)) {
916 printf("%s: could not allocate rx mbuf cluster\n",
917 USBDEVNAME(sc->sc_dev));
918 m_freem(data->m);
919 data->m = NULL;
920 return;
921 }
922
923 data->buf = mtod(data->m, uint8_t *);
924
925 DPRINTFN(15, ("rx done\n"));
926
927 skip: /* setup a new transfer */
928 usbd_setup_xfer(xfer, sc->sc_rx_pipeh, data, data->buf, MCLBYTES,
929 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, ural_rxeof);
930 usbd_transfer(xfer);
931 }
932
933 /*
934 * Return the expected ack rate for a frame transmitted at rate `rate'.
935 * XXX: this should depend on the destination node basic rate set.
936 */
937 Static int
ural_ack_rate(int rate)938 ural_ack_rate(int rate)
939 {
940 switch (rate) {
941 /* CCK rates */
942 case 2:
943 return 2;
944 case 4:
945 case 11:
946 case 22:
947 return 4;
948
949 /* OFDM rates */
950 case 12:
951 case 18:
952 return 12;
953 case 24:
954 case 36:
955 return 24;
956 case 48:
957 case 72:
958 case 96:
959 case 108:
960 return 48;
961 }
962
963 /* default to 1Mbps */
964 return 2;
965 }
966
967 /*
968 * Compute the duration (in us) needed to transmit `len' bytes at rate `rate'.
969 * The function automatically determines the operating mode depending on the
970 * given rate. `flags' indicates whether short preamble is in use or not.
971 */
972 Static uint16_t
ural_txtime(int len,int rate,uint32_t flags)973 ural_txtime(int len, int rate, uint32_t flags)
974 {
975 uint16_t txtime;
976 int ceil, dbps;
977
978 if (RAL_RATE_IS_OFDM(rate)) {
979 /*
980 * OFDM TXTIME calculation.
981 * From IEEE Std 802.11a-1999, pp. 37.
982 */
983 dbps = rate * 2; /* data bits per OFDM symbol */
984
985 ceil = (16 + 8 * len + 6) / dbps;
986 if ((16 + 8 * len + 6) % dbps != 0)
987 ceil++;
988
989 txtime = 16 + 4 + 4 * ceil + 6;
990 } else {
991 /*
992 * High Rate TXTIME calculation.
993 * From IEEE Std 802.11b-1999, pp. 28.
994 */
995 ceil = (8 * len * 2) / rate;
996 if ((8 * len * 2) % rate != 0)
997 ceil++;
998
999 if (rate != 2 && (flags & IEEE80211_F_SHPREAMBLE))
1000 txtime = 72 + 24 + ceil;
1001 else
1002 txtime = 144 + 48 + ceil;
1003 }
1004
1005 return txtime;
1006 }
1007
1008 Static uint8_t
ural_plcp_signal(int rate)1009 ural_plcp_signal(int rate)
1010 {
1011 switch (rate) {
1012 /* CCK rates (returned values are device-dependent) */
1013 case 2: return 0x0;
1014 case 4: return 0x1;
1015 case 11: return 0x2;
1016 case 22: return 0x3;
1017
1018 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1019 case 12: return 0xb;
1020 case 18: return 0xf;
1021 case 24: return 0xa;
1022 case 36: return 0xe;
1023 case 48: return 0x9;
1024 case 72: return 0xd;
1025 case 96: return 0x8;
1026 case 108: return 0xc;
1027
1028 /* unsupported rates (should not get there) */
1029 default: return 0xff;
1030 }
1031 }
1032
1033 Static void
ural_setup_tx_desc(struct ural_softc * sc,struct ural_tx_desc * desc,uint32_t flags,int len,int rate)1034 ural_setup_tx_desc(struct ural_softc *sc, struct ural_tx_desc *desc,
1035 uint32_t flags, int len, int rate)
1036 {
1037 struct ieee80211com *ic = &sc->sc_ic;
1038 uint16_t plcp_length;
1039 int remainder;
1040
1041 desc->flags = htole32(flags);
1042 desc->flags |= htole32(RAL_TX_NEWSEQ);
1043 desc->flags |= htole32(len << 16);
1044
1045 if (RAL_RATE_IS_OFDM(rate))
1046 desc->flags |= htole32(RAL_TX_OFDM);
1047
1048 desc->wme = htole16(RAL_LOGCWMAX(5) | RAL_LOGCWMIN(3) | RAL_AIFSN(2));
1049
1050 /*
1051 * Fill PLCP fields.
1052 */
1053 desc->plcp_service = 4;
1054
1055 len += 4; /* account for FCS */
1056 if (RAL_RATE_IS_OFDM(rate)) {
1057 /*
1058 * PLCP length field (LENGTH).
1059 * From IEEE Std 802.11a-1999, pp. 14.
1060 */
1061 plcp_length = len & 0xfff;
1062 desc->plcp_length = htole16((plcp_length >> 6) << 8 |
1063 (plcp_length & 0x3f));
1064 } else {
1065 /*
1066 * Long PLCP LENGTH field.
1067 * From IEEE Std 802.11b-1999, pp. 16.
1068 */
1069 plcp_length = (8 * len * 2) / rate;
1070 remainder = (8 * len * 2) % rate;
1071 if (remainder != 0) {
1072 if (rate == 22 && (rate - remainder) / 16 != 0)
1073 desc->plcp_service |= RAL_PLCP_LENGEXT;
1074 plcp_length++;
1075 }
1076 desc->plcp_length = htole16(plcp_length);
1077 }
1078
1079 desc->plcp_signal = ural_plcp_signal(rate);
1080 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1081 desc->plcp_signal |= 0x08;
1082
1083 desc->iv = 0;
1084 desc->eiv = 0;
1085 }
1086
1087 #define RAL_TX_TIMEOUT 5000
1088
1089 Static int
ural_tx_bcn(struct ural_softc * sc,struct mbuf * m0,struct ieee80211_node * ni)1090 ural_tx_bcn(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1091 {
1092 struct ural_tx_desc *desc;
1093 usbd_xfer_handle xfer;
1094 usbd_status error;
1095 uint8_t cmd = 0;
1096 uint8_t *buf;
1097 int xferlen, rate;
1098
1099 rate = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? 12 : 4;
1100
1101 xfer = usbd_alloc_xfer(sc->sc_udev);
1102 if (xfer == NULL)
1103 return ENOMEM;
1104
1105 /* xfer length needs to be a multiple of two! */
1106 xferlen = (RAL_TX_DESC_SIZE + m0->m_pkthdr.len + 1) & ~1;
1107
1108 buf = usbd_alloc_buffer(xfer, xferlen);
1109 if (buf == NULL) {
1110 usbd_free_xfer(xfer);
1111 return ENOMEM;
1112 }
1113
1114 usbd_setup_xfer(xfer, sc->sc_tx_pipeh, NULL, &cmd, sizeof cmd,
1115 USBD_FORCE_SHORT_XFER, RAL_TX_TIMEOUT, NULL);
1116
1117 error = usbd_sync_transfer(xfer);
1118 if (error != 0) {
1119 usbd_free_xfer(xfer);
1120 return error;
1121 }
1122
1123 desc = (struct ural_tx_desc *)buf;
1124
1125 m_copydata(m0, 0, m0->m_pkthdr.len, buf + RAL_TX_DESC_SIZE);
1126 ural_setup_tx_desc(sc, desc, RAL_TX_IFS_NEWBACKOFF | RAL_TX_TIMESTAMP,
1127 m0->m_pkthdr.len, rate);
1128
1129 DPRINTFN(10, ("sending beacon frame len=%u rate=%u xfer len=%u\n",
1130 m0->m_pkthdr.len, rate, xferlen));
1131
1132 usbd_setup_xfer(xfer, sc->sc_tx_pipeh, NULL, buf, xferlen,
1133 USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RAL_TX_TIMEOUT, NULL);
1134
1135 error = usbd_sync_transfer(xfer);
1136 usbd_free_xfer(xfer);
1137
1138 return error;
1139 }
1140
1141 Static int
ural_tx_mgt(struct ural_softc * sc,struct mbuf * m0,struct ieee80211_node * ni)1142 ural_tx_mgt(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1143 {
1144 struct ieee80211com *ic = &sc->sc_ic;
1145 struct ural_tx_desc *desc;
1146 struct ural_tx_data *data;
1147 struct ieee80211_frame *wh;
1148 uint32_t flags = 0;
1149 uint16_t dur;
1150 usbd_status error;
1151 int xferlen, rate;
1152
1153 data = &sc->tx_data[0];
1154 desc = (struct ural_tx_desc *)data->buf;
1155
1156 rate = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? 12 : 4;
1157
1158 #if NBPFILTER > 0
1159 if (sc->sc_drvbpf != NULL) {
1160 struct mbuf mb;
1161 struct ural_tx_radiotap_header *tap = &sc->sc_txtap;
1162
1163 tap->wt_flags = 0;
1164 tap->wt_rate = rate;
1165 tap->wt_chan_freq = htole16(ic->ic_ibss_chan->ic_freq);
1166 tap->wt_chan_flags = htole16(ic->ic_ibss_chan->ic_flags);
1167 tap->wt_antenna = sc->tx_ant;
1168
1169 M_DUP_PKTHDR(&mb, m0);
1170 mb.m_data = (caddr_t)tap;
1171 mb.m_len = sc->sc_txtap_len;
1172 mb.m_next = m0;
1173 mb.m_pkthdr.len += mb.m_len;
1174 bpf_mtap(sc->sc_drvbpf, &mb);
1175 }
1176 #endif
1177
1178 data->m = m0;
1179 data->ni = ni;
1180
1181 wh = mtod(m0, struct ieee80211_frame *);
1182
1183 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1184 flags |= RAL_TX_ACK;
1185
1186 dur = ural_txtime(RAL_ACK_SIZE, rate, ic->ic_flags) + RAL_SIFS;
1187 *(uint16_t *)wh->i_dur = htole16(dur);
1188
1189 /* tell hardware to add timestamp for probe responses */
1190 if ((wh->i_fc[0] &
1191 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
1192 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
1193 flags |= RAL_TX_TIMESTAMP;
1194 }
1195
1196 m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RAL_TX_DESC_SIZE);
1197 ural_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate);
1198
1199 /* xfer length needs to be a multiple of two! */
1200 xferlen = (RAL_TX_DESC_SIZE + m0->m_pkthdr.len + 1) & ~1;
1201
1202 DPRINTFN(10, ("sending mgt frame len=%u rate=%u xfer len=%u\n",
1203 m0->m_pkthdr.len, rate, xferlen));
1204
1205 usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf, xferlen,
1206 USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RAL_TX_TIMEOUT, ural_txeof);
1207
1208 error = usbd_transfer(data->xfer);
1209 if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS) {
1210 m_freem(m0);
1211 return error;
1212 }
1213
1214 sc->tx_queued++;
1215
1216 return 0;
1217 }
1218
1219 Static int
ural_tx_data(struct ural_softc * sc,struct mbuf * m0,struct ieee80211_node * ni)1220 ural_tx_data(struct ural_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
1221 {
1222 struct ieee80211com *ic = &sc->sc_ic;
1223 struct ifnet *ifp = &ic->ic_if;
1224 struct ieee80211_rateset *rs;
1225 struct ural_tx_desc *desc;
1226 struct ural_tx_data *data;
1227 struct ieee80211_frame *wh;
1228 uint32_t flags = 0;
1229 uint16_t dur;
1230 usbd_status error;
1231 int xferlen, rate;
1232
1233 /* XXX this should be reworked! */
1234 if (ic->ic_fixed_rate != -1) {
1235 if (ic->ic_curmode != IEEE80211_MODE_AUTO)
1236 rs = &ic->ic_sup_rates[ic->ic_curmode];
1237 else
1238 rs = &ic->ic_sup_rates[IEEE80211_MODE_11G];
1239
1240 rate = rs->rs_rates[ic->ic_fixed_rate];
1241 } else {
1242 rs = &ni->ni_rates;
1243 rate = rs->rs_rates[ni->ni_txrate];
1244 }
1245 rate &= IEEE80211_RATE_VAL;
1246
1247 if (ic->ic_flags & IEEE80211_F_WEPON) {
1248 m0 = ieee80211_wep_crypt(ifp, m0, 1);
1249 if (m0 == NULL)
1250 return ENOBUFS;
1251 }
1252
1253 #if NBPFILTER > 0
1254 if (sc->sc_drvbpf != NULL) {
1255 struct mbuf mb;
1256 struct ural_tx_radiotap_header *tap = &sc->sc_txtap;
1257
1258 tap->wt_flags = 0;
1259 tap->wt_rate = rate;
1260 tap->wt_chan_freq = htole16(ic->ic_ibss_chan->ic_freq);
1261 tap->wt_chan_flags = htole16(ic->ic_ibss_chan->ic_flags);
1262 tap->wt_antenna = sc->tx_ant;
1263
1264 M_DUP_PKTHDR(&mb, m0);
1265 mb.m_data = (caddr_t)tap;
1266 mb.m_len = sc->sc_txtap_len;
1267 mb.m_next = m0;
1268 mb.m_pkthdr.len += mb.m_len;
1269 bpf_mtap(sc->sc_drvbpf, &mb);
1270 }
1271 #endif
1272
1273 data = &sc->tx_data[0];
1274 desc = (struct ural_tx_desc *)data->buf;
1275
1276 data->m = m0;
1277 data->ni = ni;
1278
1279 wh = mtod(m0, struct ieee80211_frame *);
1280
1281 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1282 flags |= RAL_TX_ACK;
1283 flags |= RAL_TX_RETRY(7);
1284
1285 dur = ural_txtime(RAL_ACK_SIZE, ural_ack_rate(rate),
1286 ic->ic_flags) + RAL_SIFS;
1287 *(uint16_t *)wh->i_dur = htole16(dur);
1288 }
1289
1290 m_copydata(m0, 0, m0->m_pkthdr.len, data->buf + RAL_TX_DESC_SIZE);
1291 ural_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate);
1292
1293 /* xfer length needs to be a multiple of two! */
1294 xferlen = (RAL_TX_DESC_SIZE + m0->m_pkthdr.len + 1) & ~1;
1295
1296 DPRINTFN(10, ("sending data frame len=%u rate=%u xfer len=%u\n",
1297 m0->m_pkthdr.len, rate, xferlen));
1298
1299 usbd_setup_xfer(data->xfer, sc->sc_tx_pipeh, data, data->buf, xferlen,
1300 USBD_FORCE_SHORT_XFER | USBD_NO_COPY, RAL_TX_TIMEOUT, ural_txeof);
1301
1302 error = usbd_transfer(data->xfer);
1303 if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS) {
1304 m_freem(m0);
1305 return error;
1306 }
1307
1308 sc->tx_queued++;
1309
1310 return 0;
1311 }
1312
1313 Static void
ural_start(struct ifnet * ifp)1314 ural_start(struct ifnet *ifp)
1315 {
1316 struct ural_softc *sc = ifp->if_softc;
1317 struct ieee80211com *ic = &sc->sc_ic;
1318 struct ieee80211_node *ni;
1319 struct mbuf *m0;
1320
1321 for (;;) {
1322 IF_POLL(&ic->ic_mgtq, m0);
1323 if (m0 != NULL) {
1324 if (sc->tx_queued >= RAL_TX_LIST_COUNT) {
1325 ifp->if_flags |= IFF_OACTIVE;
1326 break;
1327 }
1328 IF_DEQUEUE(&ic->ic_mgtq, m0);
1329
1330 ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
1331 m0->m_pkthdr.rcvif = NULL;
1332 #if NBPFILTER > 0
1333 if (ic->ic_rawbpf != NULL)
1334 bpf_mtap(ic->ic_rawbpf, m0);
1335 #endif
1336 if (ural_tx_mgt(sc, m0, ni) != 0)
1337 break;
1338
1339 } else {
1340 if (ic->ic_state != IEEE80211_S_RUN)
1341 break;
1342 IFQ_DEQUEUE(&ifp->if_snd, m0);
1343 if (m0 == NULL)
1344 break;
1345 if (sc->tx_queued >= RAL_TX_LIST_COUNT) {
1346 IF_PREPEND(&ifp->if_snd, m0);
1347 ifp->if_flags |= IFF_OACTIVE;
1348 break;
1349 }
1350
1351 #if NBPFILTER > 0
1352 if (ifp->if_bpf != NULL)
1353 bpf_mtap(ifp->if_bpf, m0);
1354 #endif
1355 m0 = ieee80211_encap(ifp, m0, &ni);
1356 if (m0 == NULL)
1357 continue;
1358 #if NBPFILTER > 0
1359 if (ic->ic_rawbpf != NULL)
1360 bpf_mtap(ic->ic_rawbpf, m0);
1361 #endif
1362 if (ural_tx_data(sc, m0, ni) != 0) {
1363 if (ni != NULL)
1364 ieee80211_release_node(ic, ni);
1365 ifp->if_oerrors++;
1366 break;
1367 }
1368 }
1369
1370 sc->sc_tx_timer = 5;
1371 ifp->if_timer = 1;
1372 }
1373 }
1374
1375 Static void
ural_watchdog(struct ifnet * ifp)1376 ural_watchdog(struct ifnet *ifp)
1377 {
1378 struct ural_softc *sc = ifp->if_softc;
1379
1380 ifp->if_timer = 0;
1381
1382 if (sc->sc_tx_timer > 0) {
1383 if (--sc->sc_tx_timer == 0) {
1384 printf("%s: device timeout\n", USBDEVNAME(sc->sc_dev));
1385 /*ural_init(ifp); XXX needs a process context! */
1386 ifp->if_oerrors++;
1387 return;
1388 }
1389 ifp->if_timer = 1;
1390 }
1391
1392 ieee80211_watchdog(ifp);
1393 }
1394
1395 Static int
ural_ioctl(struct ifnet * ifp,u_long cmd,caddr_t data)1396 ural_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1397 {
1398 struct ural_softc *sc = ifp->if_softc;
1399 struct ieee80211com *ic = &sc->sc_ic;
1400 struct ifaddr *ifa;
1401 struct ifreq *ifr;
1402 int s, error = 0;
1403
1404 s = splnet();
1405
1406 switch (cmd) {
1407 case SIOCSIFADDR:
1408 ifa = (struct ifaddr *)data;
1409 ifp->if_flags |= IFF_UP;
1410 #ifdef INET
1411 if (ifa->ifa_addr->sa_family == AF_INET)
1412 arp_ifinit(&ic->ic_ac, ifa);
1413 #endif
1414 /* FALLTHROUGH */
1415 case SIOCSIFFLAGS:
1416 if (ifp->if_flags & IFF_UP) {
1417 if (ifp->if_flags & IFF_RUNNING)
1418 ural_update_promisc(sc);
1419 else
1420 ural_init(ifp);
1421 } else {
1422 if (ifp->if_flags & IFF_RUNNING)
1423 ural_stop(ifp, 1);
1424 }
1425 break;
1426
1427 case SIOCADDMULTI:
1428 case SIOCDELMULTI:
1429 ifr = (struct ifreq *)data;
1430 error = (cmd == SIOCADDMULTI) ?
1431 ether_addmulti(ifr, &ic->ic_ac) :
1432 ether_delmulti(ifr, &ic->ic_ac);
1433
1434 if (error == ENETRESET)
1435 error = 0;
1436 break;
1437
1438 case SIOCS80211CHANNEL:
1439 /*
1440 * This allows for fast channel switching in monitor mode
1441 * (used by kismet). In IBSS mode, we must explicitly reset
1442 * the interface to generate a new beacon frame.
1443 */
1444 error = ieee80211_ioctl(ifp, cmd, data);
1445 if (error == ENETRESET &&
1446 ic->ic_opmode == IEEE80211_M_MONITOR) {
1447 ural_set_chan(sc, ic->ic_ibss_chan);
1448 error = 0;
1449 }
1450 break;
1451
1452 default:
1453 error = ieee80211_ioctl(ifp, cmd, data);
1454 }
1455
1456 if (error == ENETRESET) {
1457 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
1458 (IFF_UP | IFF_RUNNING))
1459 ural_init(ifp);
1460 error = 0;
1461 }
1462
1463 splx(s);
1464
1465 return error;
1466 }
1467
1468 Static void
ural_eeprom_read(struct ural_softc * sc,uint16_t addr,void * buf,int len)1469 ural_eeprom_read(struct ural_softc *sc, uint16_t addr, void *buf, int len)
1470 {
1471 usb_device_request_t req;
1472 usbd_status error;
1473
1474 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1475 req.bRequest = RAL_READ_EEPROM;
1476 USETW(req.wValue, 0);
1477 USETW(req.wIndex, addr);
1478 USETW(req.wLength, len);
1479
1480 error = usbd_do_request(sc->sc_udev, &req, buf);
1481 if (error != 0) {
1482 printf("%s: could not read EEPROM: %s\n",
1483 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1484 }
1485 }
1486
1487 Static uint16_t
ural_read(struct ural_softc * sc,uint16_t reg)1488 ural_read(struct ural_softc *sc, uint16_t reg)
1489 {
1490 usb_device_request_t req;
1491 usbd_status error;
1492 uint16_t val;
1493
1494 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1495 req.bRequest = RAL_READ_MAC;
1496 USETW(req.wValue, 0);
1497 USETW(req.wIndex, reg);
1498 USETW(req.wLength, sizeof (uint16_t));
1499
1500 error = usbd_do_request(sc->sc_udev, &req, &val);
1501 if (error != 0) {
1502 printf("%s: could not read MAC register: %s\n",
1503 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1504 return 0;
1505 }
1506
1507 return le16toh(val);
1508 }
1509
1510 Static void
ural_read_multi(struct ural_softc * sc,uint16_t reg,void * buf,int len)1511 ural_read_multi(struct ural_softc *sc, uint16_t reg, void *buf, int len)
1512 {
1513 usb_device_request_t req;
1514 usbd_status error;
1515
1516 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1517 req.bRequest = RAL_READ_MULTI_MAC;
1518 USETW(req.wValue, 0);
1519 USETW(req.wIndex, reg);
1520 USETW(req.wLength, len);
1521
1522 error = usbd_do_request(sc->sc_udev, &req, buf);
1523 if (error != 0) {
1524 printf("%s: could not read MAC register: %s\n",
1525 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1526 return;
1527 }
1528 }
1529
1530 Static void
ural_write(struct ural_softc * sc,uint16_t reg,uint16_t val)1531 ural_write(struct ural_softc *sc, uint16_t reg, uint16_t val)
1532 {
1533 usb_device_request_t req;
1534 usbd_status error;
1535
1536 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1537 req.bRequest = RAL_WRITE_MAC;
1538 USETW(req.wValue, val);
1539 USETW(req.wIndex, reg);
1540 USETW(req.wLength, 0);
1541
1542 error = usbd_do_request(sc->sc_udev, &req, NULL);
1543 if (error != 0) {
1544 printf("%s: could not write MAC register: %s\n",
1545 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1546 }
1547 }
1548
1549 Static void
ural_write_multi(struct ural_softc * sc,uint16_t reg,void * buf,int len)1550 ural_write_multi(struct ural_softc *sc, uint16_t reg, void *buf, int len)
1551 {
1552 usb_device_request_t req;
1553 usbd_status error;
1554
1555 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
1556 req.bRequest = RAL_WRITE_MULTI_MAC;
1557 USETW(req.wValue, 0);
1558 USETW(req.wIndex, reg);
1559 USETW(req.wLength, len);
1560
1561 error = usbd_do_request(sc->sc_udev, &req, buf);
1562 if (error != 0) {
1563 printf("%s: could not write MAC register: %s\n",
1564 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
1565 }
1566 }
1567
1568 Static void
ural_bbp_write(struct ural_softc * sc,uint8_t reg,uint8_t val)1569 ural_bbp_write(struct ural_softc *sc, uint8_t reg, uint8_t val)
1570 {
1571 uint16_t tmp;
1572 int ntries;
1573
1574 for (ntries = 0; ntries < 5; ntries++) {
1575 if (!(ural_read(sc, RAL_PHY_CSR8) & RAL_BBP_BUSY))
1576 break;
1577 }
1578 if (ntries == 5) {
1579 printf("%s: could not write to BBP\n", USBDEVNAME(sc->sc_dev));
1580 return;
1581 }
1582
1583 tmp = reg << 8 | val;
1584 ural_write(sc, RAL_PHY_CSR7, tmp);
1585 }
1586
1587 Static uint8_t
ural_bbp_read(struct ural_softc * sc,uint8_t reg)1588 ural_bbp_read(struct ural_softc *sc, uint8_t reg)
1589 {
1590 uint16_t val;
1591 int ntries;
1592
1593 val = RAL_BBP_WRITE | reg << 8;
1594 ural_write(sc, RAL_PHY_CSR7, val);
1595
1596 for (ntries = 0; ntries < 5; ntries++) {
1597 if (!(ural_read(sc, RAL_PHY_CSR8) & RAL_BBP_BUSY))
1598 break;
1599 }
1600 if (ntries == 5) {
1601 printf("%s: could not read BBP\n", USBDEVNAME(sc->sc_dev));
1602 return 0;
1603 }
1604
1605 return ural_read(sc, RAL_PHY_CSR7) & 0xff;
1606 }
1607
1608 Static void
ural_rf_write(struct ural_softc * sc,uint8_t reg,uint32_t val)1609 ural_rf_write(struct ural_softc *sc, uint8_t reg, uint32_t val)
1610 {
1611 uint32_t tmp;
1612 int ntries;
1613
1614 for (ntries = 0; ntries < 5; ntries++) {
1615 if (!(ural_read(sc, RAL_PHY_CSR10) & RAL_RF_LOBUSY))
1616 break;
1617 }
1618 if (ntries == 5) {
1619 printf("%s: could not write to RF\n", USBDEVNAME(sc->sc_dev));
1620 return;
1621 }
1622
1623 tmp = RAL_RF_BUSY | RAL_RF_20BIT | (val & 0xfffff) << 2 | (reg & 0x3);
1624 ural_write(sc, RAL_PHY_CSR9, tmp & 0xffff);
1625 ural_write(sc, RAL_PHY_CSR10, tmp >> 16);
1626
1627 /* remember last written value in sc */
1628 sc->rf_regs[reg] = val;
1629
1630 DPRINTFN(15, ("RF R[%u] <- 0x%05x\n", reg & 0x3, val & 0xfffff));
1631 }
1632
1633 Static void
ural_set_chan(struct ural_softc * sc,struct ieee80211_channel * c)1634 ural_set_chan(struct ural_softc *sc, struct ieee80211_channel *c)
1635 {
1636 #define N(a) (sizeof (a) / sizeof ((a)[0]))
1637 struct ieee80211com *ic = &sc->sc_ic;
1638 uint8_t power, tmp;
1639 u_int i, chan;
1640
1641 chan = ieee80211_chan2ieee(ic, c);
1642 if (chan == 0 || chan == IEEE80211_CHAN_ANY)
1643 return;
1644
1645 if (IEEE80211_IS_CHAN_2GHZ(c))
1646 power = min(sc->txpow[chan - 1], 31);
1647 else
1648 power = 31;
1649
1650 DPRINTFN(2, ("setting channel to %u, txpower to %u\n", chan, power));
1651
1652 switch (sc->rf_rev) {
1653 case RAL_RF_2522:
1654 ural_rf_write(sc, RAL_RF1, 0x00814);
1655 ural_rf_write(sc, RAL_RF2, ural_rf2522_r2[chan - 1]);
1656 ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
1657 break;
1658
1659 case RAL_RF_2523:
1660 ural_rf_write(sc, RAL_RF1, 0x08804);
1661 ural_rf_write(sc, RAL_RF2, ural_rf2523_r2[chan - 1]);
1662 ural_rf_write(sc, RAL_RF3, power << 7 | 0x38044);
1663 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
1664 break;
1665
1666 case RAL_RF_2524:
1667 ural_rf_write(sc, RAL_RF1, 0x0c808);
1668 ural_rf_write(sc, RAL_RF2, ural_rf2524_r2[chan - 1]);
1669 ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
1670 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
1671 break;
1672
1673 case RAL_RF_2525:
1674 ural_rf_write(sc, RAL_RF1, 0x08808);
1675 ural_rf_write(sc, RAL_RF2, ural_rf2525_hi_r2[chan - 1]);
1676 ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
1677 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
1678
1679 ural_rf_write(sc, RAL_RF1, 0x08808);
1680 ural_rf_write(sc, RAL_RF2, ural_rf2525_r2[chan - 1]);
1681 ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
1682 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
1683 break;
1684
1685 case RAL_RF_2525E:
1686 ural_rf_write(sc, RAL_RF1, 0x08808);
1687 ural_rf_write(sc, RAL_RF2, ural_rf2525e_r2[chan - 1]);
1688 ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
1689 ural_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00286 : 0x00282);
1690 break;
1691
1692 case RAL_RF_2526:
1693 ural_rf_write(sc, RAL_RF2, ural_rf2526_hi_r2[chan - 1]);
1694 ural_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
1695 ural_rf_write(sc, RAL_RF1, 0x08804);
1696
1697 ural_rf_write(sc, RAL_RF2, ural_rf2526_r2[chan - 1]);
1698 ural_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
1699 ural_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
1700 break;
1701
1702 /* dual-band RF */
1703 case RAL_RF_5222:
1704 for (i = 0; i < N(ural_rf5222); i++)
1705 if (ural_rf5222[i].chan == chan)
1706 break;
1707
1708 if (i < N(ural_rf5222)) {
1709 ural_rf_write(sc, RAL_RF1, ural_rf5222[i].r1);
1710 ural_rf_write(sc, RAL_RF2, ural_rf5222[i].r2);
1711 ural_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
1712 ural_rf_write(sc, RAL_RF4, ural_rf5222[i].r4);
1713 }
1714 break;
1715 }
1716
1717 if (ic->ic_opmode != IEEE80211_M_MONITOR &&
1718 ic->ic_state != IEEE80211_S_SCAN) {
1719 /* set Japan filter bit for channel 14 */
1720 tmp = ural_bbp_read(sc, 70);
1721
1722 tmp &= ~RAL_JAPAN_FILTER;
1723 if (chan == 14)
1724 tmp |= RAL_JAPAN_FILTER;
1725
1726 ural_bbp_write(sc, 70, tmp);
1727
1728 /* clear CRC errors */
1729 ural_read(sc, RAL_STA_CSR0);
1730
1731 DELAY(1000); /* RF needs a 1ms delay here */
1732 ural_disable_rf_tune(sc);
1733 }
1734 #undef N
1735 }
1736
1737 /*
1738 * Disable RF auto-tuning.
1739 */
1740 Static void
ural_disable_rf_tune(struct ural_softc * sc)1741 ural_disable_rf_tune(struct ural_softc *sc)
1742 {
1743 uint32_t tmp;
1744
1745 if (sc->rf_rev != RAL_RF_2523) {
1746 tmp = sc->rf_regs[RAL_RF1] & ~RAL_RF1_AUTOTUNE;
1747 ural_rf_write(sc, RAL_RF1, tmp);
1748 }
1749
1750 tmp = sc->rf_regs[RAL_RF3] & ~RAL_RF3_AUTOTUNE;
1751 ural_rf_write(sc, RAL_RF3, tmp);
1752
1753 DPRINTFN(2, ("disabling RF autotune\n"));
1754 }
1755
1756 /*
1757 * Refer to IEEE Std 802.11-1999 pp. 123 for more information on TSF
1758 * synchronization.
1759 */
1760 Static void
ural_enable_tsf_sync(struct ural_softc * sc)1761 ural_enable_tsf_sync(struct ural_softc *sc)
1762 {
1763 struct ieee80211com *ic = &sc->sc_ic;
1764 uint16_t logcwmin, preload, tmp;
1765
1766 /* first, disable TSF synchronization */
1767 ural_write(sc, RAL_TXRX_CSR19, 0);
1768
1769 tmp = (16 * ic->ic_bss->ni_intval) << 4;
1770 ural_write(sc, RAL_TXRX_CSR18, tmp);
1771
1772 logcwmin = (ic->ic_opmode == IEEE80211_M_IBSS) ? 2 : 0;
1773 preload = (ic->ic_opmode == IEEE80211_M_IBSS) ? 320 : 6;
1774 tmp = logcwmin << 12 | preload;
1775 ural_write(sc, RAL_TXRX_CSR20, tmp);
1776
1777 /* finally, enable TSF synchronization */
1778 tmp = RAL_ENABLE_TSF | RAL_ENABLE_TBCN;
1779 if (ic->ic_opmode == IEEE80211_M_STA)
1780 tmp |= RAL_ENABLE_TSF_SYNC(1);
1781 else
1782 tmp |= RAL_ENABLE_TSF_SYNC(2) | RAL_ENABLE_BEACON_GENERATOR;
1783 ural_write(sc, RAL_TXRX_CSR19, tmp);
1784
1785 DPRINTF(("enabling TSF synchronization\n"));
1786 }
1787
1788 Static void
ural_set_bssid(struct ural_softc * sc,uint8_t * bssid)1789 ural_set_bssid(struct ural_softc *sc, uint8_t *bssid)
1790 {
1791 uint16_t tmp;
1792
1793 tmp = bssid[0] | bssid[1] << 8;
1794 ural_write(sc, RAL_MAC_CSR5, tmp);
1795
1796 tmp = bssid[2] | bssid[3] << 8;
1797 ural_write(sc, RAL_MAC_CSR6, tmp);
1798
1799 tmp = bssid[4] | bssid[5] << 8;
1800 ural_write(sc, RAL_MAC_CSR7, tmp);
1801
1802 DPRINTF(("setting BSSID to %s\n", ether_sprintf(bssid)));
1803 }
1804
1805 Static void
ural_set_macaddr(struct ural_softc * sc,uint8_t * addr)1806 ural_set_macaddr(struct ural_softc *sc, uint8_t *addr)
1807 {
1808 uint16_t tmp;
1809
1810 tmp = addr[0] | addr[1] << 8;
1811 ural_write(sc, RAL_MAC_CSR2, tmp);
1812
1813 tmp = addr[2] | addr[3] << 8;
1814 ural_write(sc, RAL_MAC_CSR3, tmp);
1815
1816 tmp = addr[4] | addr[5] << 8;
1817 ural_write(sc, RAL_MAC_CSR4, tmp);
1818
1819 DPRINTF(("setting MAC address to %s\n", ether_sprintf(addr)));
1820 }
1821
1822 Static void
ural_update_promisc(struct ural_softc * sc)1823 ural_update_promisc(struct ural_softc *sc)
1824 {
1825 struct ifnet *ifp = &sc->sc_ic.ic_if;
1826 uint16_t tmp;
1827
1828 tmp = ural_read(sc, RAL_TXRX_CSR2);
1829
1830 tmp &= ~RAL_DROP_NOT_TO_ME;
1831 if (!(ifp->if_flags & IFF_PROMISC))
1832 tmp |= RAL_DROP_NOT_TO_ME;
1833
1834 ural_write(sc, RAL_TXRX_CSR2, tmp);
1835
1836 DPRINTF(("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
1837 "entering" : "leaving"));
1838 }
1839
1840 Static const char *
ural_get_rf(int rev)1841 ural_get_rf(int rev)
1842 {
1843 switch (rev) {
1844 case RAL_RF_2522: return "RT2522";
1845 case RAL_RF_2523: return "RT2523";
1846 case RAL_RF_2524: return "RT2524";
1847 case RAL_RF_2525: return "RT2525";
1848 case RAL_RF_2525E: return "RT2525e";
1849 case RAL_RF_2526: return "RT2526";
1850 case RAL_RF_5222: return "RT5222";
1851 default: return "unknown";
1852 }
1853 }
1854
1855 Static void
ural_read_eeprom(struct ural_softc * sc)1856 ural_read_eeprom(struct ural_softc *sc)
1857 {
1858 struct ieee80211com *ic = &sc->sc_ic;
1859 uint16_t val;
1860
1861 ural_eeprom_read(sc, RAL_EEPROM_CONFIG0, &val, 2);
1862 val = letoh16(val);
1863 sc->rf_rev = (val >> 11) & 0x7;
1864 sc->hw_radio = (val >> 10) & 0x1;
1865 sc->led_mode = (val >> 6) & 0x7;
1866 sc->rx_ant = (val >> 4) & 0x3;
1867 sc->tx_ant = (val >> 2) & 0x3;
1868 sc->nb_ant = val & 0x3;
1869
1870 /* read MAC address */
1871 ural_eeprom_read(sc, RAL_EEPROM_ADDRESS, ic->ic_myaddr, 6);
1872
1873 /* read default values for BBP registers */
1874 ural_eeprom_read(sc, RAL_EEPROM_BBP_BASE, sc->bbp_prom, 2 * 16);
1875
1876 /* read Tx power for all b/g channels */
1877 ural_eeprom_read(sc, RAL_EEPROM_TXPOWER, sc->txpow, 14);
1878 }
1879
1880 Static int
ural_bbp_init(struct ural_softc * sc)1881 ural_bbp_init(struct ural_softc *sc)
1882 {
1883 #define N(a) (sizeof (a) / sizeof ((a)[0]))
1884 int i, ntries;
1885
1886 /* wait for BBP to be ready */
1887 for (ntries = 0; ntries < 100; ntries++) {
1888 if (ural_bbp_read(sc, RAL_BBP_VERSION) != 0)
1889 break;
1890 DELAY(1000);
1891 }
1892 if (ntries == 100) {
1893 printf("%s: timeout waiting for BBP\n", USBDEVNAME(sc->sc_dev));
1894 return EIO;
1895 }
1896
1897 /* initialize BBP registers to default values */
1898 for (i = 0; i < N(ural_def_bbp); i++)
1899 ural_bbp_write(sc, ural_def_bbp[i].reg, ural_def_bbp[i].val);
1900
1901 #if 0
1902 /* initialize BBP registers to values stored in EEPROM */
1903 for (i = 0; i < 16; i++) {
1904 if (sc->bbp_prom[i].reg == 0xff)
1905 continue;
1906 ural_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
1907 }
1908 #endif
1909
1910 return 0;
1911 #undef N
1912 }
1913
1914 Static void
ural_set_txantenna(struct ural_softc * sc,int antenna)1915 ural_set_txantenna(struct ural_softc *sc, int antenna)
1916 {
1917 uint16_t tmp;
1918 uint8_t tx;
1919
1920 tx = ural_bbp_read(sc, RAL_BBP_TX) & ~RAL_BBP_ANTMASK;
1921 if (antenna == 1)
1922 tx |= RAL_BBP_ANTA;
1923 else if (antenna == 2)
1924 tx |= RAL_BBP_ANTB;
1925 else
1926 tx |= RAL_BBP_DIVERSITY;
1927
1928 /* need to force I/Q flip for RF 2525e, 2526 and 5222 */
1929 if (sc->rf_rev == RAL_RF_2525E || sc->rf_rev == RAL_RF_2526 ||
1930 sc->rf_rev == RAL_RF_5222)
1931 tx |= RAL_BBP_FLIPIQ;
1932
1933 ural_bbp_write(sc, RAL_BBP_TX, tx);
1934
1935 /* update flags in PHY_CSR5 and PHY_CSR6 too */
1936 tmp = ural_read(sc, RAL_PHY_CSR5) & ~0x7;
1937 ural_write(sc, RAL_PHY_CSR5, tmp | (tx & 0x7));
1938
1939 tmp = ural_read(sc, RAL_PHY_CSR6) & ~0x7;
1940 ural_write(sc, RAL_PHY_CSR6, tmp | (tx & 0x7));
1941 }
1942
1943 Static void
ural_set_rxantenna(struct ural_softc * sc,int antenna)1944 ural_set_rxantenna(struct ural_softc *sc, int antenna)
1945 {
1946 uint8_t rx;
1947
1948 rx = ural_bbp_read(sc, RAL_BBP_RX) & ~RAL_BBP_ANTMASK;
1949 if (antenna == 1)
1950 rx |= RAL_BBP_ANTA;
1951 else if (antenna == 2)
1952 rx |= RAL_BBP_ANTB;
1953 else
1954 rx |= RAL_BBP_DIVERSITY;
1955
1956 /* need to force no I/Q flip for RF 2525e and 2526 */
1957 if (sc->rf_rev == RAL_RF_2525E || sc->rf_rev == RAL_RF_2526)
1958 rx &= ~RAL_BBP_FLIPIQ;
1959
1960 ural_bbp_write(sc, RAL_BBP_RX, rx);
1961 }
1962
1963 Static int
ural_init(struct ifnet * ifp)1964 ural_init(struct ifnet *ifp)
1965 {
1966 #define N(a) (sizeof (a) / sizeof ((a)[0]))
1967 struct ural_softc *sc = ifp->if_softc;
1968 struct ieee80211com *ic = &sc->sc_ic;
1969 struct ieee80211_wepkey *wk;
1970 struct ural_rx_data *data;
1971 uint16_t sta[11], tmp;
1972 usbd_status error;
1973 int i, ntries;
1974
1975 ural_stop(ifp, 0);
1976
1977 /* initialize MAC registers to default values */
1978 for (i = 0; i < N(ural_def_mac); i++)
1979 ural_write(sc, ural_def_mac[i].reg, ural_def_mac[i].val);
1980
1981 /* wait for BBP and RF to wake up (this can take a long time!) */
1982 for (ntries = 0; ntries < 100; ntries++) {
1983 tmp = ural_read(sc, RAL_MAC_CSR17);
1984 if ((tmp & (RAL_BBP_AWAKE | RAL_RF_AWAKE)) ==
1985 (RAL_BBP_AWAKE | RAL_RF_AWAKE))
1986 break;
1987 DELAY(1000);
1988 }
1989 if (ntries == 100) {
1990 printf("%s: timeout waiting for BBP/RF to wakeup\n",
1991 USBDEVNAME(sc->sc_dev));
1992 error = EIO;
1993 goto fail;
1994 }
1995
1996 /* we're ready! */
1997 ural_write(sc, RAL_MAC_CSR1, RAL_HOST_READY);
1998
1999 /* set supported basic rates (1, 2, 6, 12, 24) */
2000 ural_write(sc, RAL_TXRX_CSR11, 0x153);
2001
2002 error = ural_bbp_init(sc);
2003 if (error != 0)
2004 goto fail;
2005
2006 /* set default BSS channel */
2007 ic->ic_bss->ni_chan = ic->ic_ibss_chan;
2008 ural_set_chan(sc, ic->ic_bss->ni_chan);
2009
2010 /* clear statistic registers (STA_CSR0 to STA_CSR10) */
2011 ural_read_multi(sc, RAL_STA_CSR0, sta, sizeof sta);
2012
2013 /* set default sensitivity */
2014 ural_bbp_write(sc, 17, 0x48);
2015
2016 ural_set_txantenna(sc, 1);
2017 ural_set_rxantenna(sc, 1);
2018
2019 IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl));
2020 ural_set_macaddr(sc, ic->ic_myaddr);
2021
2022 /*
2023 * Copy WEP keys into adapter's memory (SEC_CSR0 to SEC_CSR31).
2024 */
2025 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2026 wk = &ic->ic_nw_keys[i];
2027 ural_write_multi(sc, RAL_SEC_CSR0 + i * IEEE80211_KEYBUF_SIZE,
2028 wk->wk_key, IEEE80211_KEYBUF_SIZE);
2029 }
2030
2031 /*
2032 * Open Tx and Rx USB bulk pipes.
2033 */
2034 error = usbd_open_pipe(sc->sc_iface, sc->sc_tx_no, USBD_EXCLUSIVE_USE,
2035 &sc->sc_tx_pipeh);
2036 if (error != 0) {
2037 printf("%s: could not open Tx pipe: %s\n",
2038 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
2039 goto fail;
2040 }
2041
2042 error = usbd_open_pipe(sc->sc_iface, sc->sc_rx_no, USBD_EXCLUSIVE_USE,
2043 &sc->sc_rx_pipeh);
2044 if (error != 0) {
2045 printf("%s: could not open Rx pipe: %s\n",
2046 USBDEVNAME(sc->sc_dev), usbd_errstr(error));
2047 goto fail;
2048 }
2049
2050 /*
2051 * Allocate Tx and Rx xfer queues.
2052 */
2053 error = ural_alloc_tx_list(sc);
2054 if (error != 0) {
2055 printf("%s: could not allocate Tx list\n",
2056 USBDEVNAME(sc->sc_dev));
2057 goto fail;
2058 }
2059
2060 error = ural_alloc_rx_list(sc);
2061 if (error != 0) {
2062 printf("%s: could not allocate Rx list\n",
2063 USBDEVNAME(sc->sc_dev));
2064 goto fail;
2065 }
2066
2067 /*
2068 * Start up the receive pipe.
2069 */
2070 for (i = 0; i < RAL_RX_LIST_COUNT; i++) {
2071 data = &sc->rx_data[i];
2072
2073 usbd_setup_xfer(data->xfer, sc->sc_rx_pipeh, data, data->buf,
2074 MCLBYTES, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, ural_rxeof);
2075 usbd_transfer(data->xfer);
2076 }
2077
2078 /* kick Rx */
2079 tmp = RAL_DROP_PHY_ERROR | RAL_DROP_CRC_ERROR;
2080 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2081 tmp |= RAL_DROP_CTL | RAL_DROP_VERSION_ERROR;
2082 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2083 tmp |= RAL_DROP_TODS;
2084 if (!(ifp->if_flags & IFF_PROMISC))
2085 tmp |= RAL_DROP_NOT_TO_ME;
2086 }
2087 ural_write(sc, RAL_TXRX_CSR2, tmp);
2088
2089 ifp->if_flags &= ~IFF_OACTIVE;
2090 ifp->if_flags |= IFF_RUNNING;
2091
2092 if (ic->ic_opmode == IEEE80211_M_MONITOR)
2093 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2094 else
2095 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2096
2097 return 0;
2098
2099 fail: ural_stop(ifp, 1);
2100 return error;
2101 #undef N
2102 }
2103
2104 Static void
ural_stop(struct ifnet * ifp,int disable)2105 ural_stop(struct ifnet *ifp, int disable)
2106 {
2107 struct ural_softc *sc = ifp->if_softc;
2108 struct ieee80211com *ic = &sc->sc_ic;
2109
2110 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2111
2112 /* disable Rx */
2113 ural_write(sc, RAL_TXRX_CSR2, RAL_DISABLE_RX);
2114
2115 /* reset ASIC and BBP (but won't reset MAC registers!) */
2116 ural_write(sc, RAL_MAC_CSR1, RAL_RESET_ASIC | RAL_RESET_BBP);
2117 ural_write(sc, RAL_MAC_CSR1, 0);
2118
2119 sc->sc_tx_timer = 0;
2120 ifp->if_timer = 0;
2121 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2122
2123 if (sc->sc_rx_pipeh != NULL) {
2124 usbd_abort_pipe(sc->sc_rx_pipeh);
2125 usbd_close_pipe(sc->sc_rx_pipeh);
2126 sc->sc_rx_pipeh = NULL;
2127 }
2128
2129 if (sc->sc_tx_pipeh != NULL) {
2130 usbd_abort_pipe(sc->sc_tx_pipeh);
2131 usbd_close_pipe(sc->sc_tx_pipeh);
2132 sc->sc_tx_pipeh = NULL;
2133 }
2134
2135 ural_free_rx_list(sc);
2136 ural_free_tx_list(sc);
2137 }
2138
2139 Static int
ural_activate(device_ptr_t self,enum devact act)2140 ural_activate(device_ptr_t self, enum devact act)
2141 {
2142 switch (act) {
2143 case DVACT_ACTIVATE:
2144 return EOPNOTSUPP;
2145
2146 case DVACT_DEACTIVATE:
2147 /*if_deactivate(&sc->sc_ic.ic_if);*/
2148 break;
2149 }
2150
2151 return 0;
2152 }
2153