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