FreeBSD/Linux Kernel Cross Reference
sys/dev/usb/if_ural.c
1 /* $FreeBSD: src/sys/dev/usb/if_ural.c,v 1.75 2008/07/30 00:38:10 thompsa Exp $ */
2
3 /*-
4 * Copyright (c) 2005, 2006
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 #include <sys/cdefs.h>
21 __FBSDID("$FreeBSD: src/sys/dev/usb/if_ural.c,v 1.75 2008/07/30 00:38:10 thompsa Exp $");
22
23 /*-
24 * Ralink Technology RT2500USB chipset driver
25 * http://www.ralinktech.com/
26 */
27
28 #include <sys/param.h>
29 #include <sys/sysctl.h>
30 #include <sys/sockio.h>
31 #include <sys/mbuf.h>
32 #include <sys/kernel.h>
33 #include <sys/socket.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/bus.h>
38 #include <sys/endian.h>
39
40 #include <machine/bus.h>
41 #include <machine/resource.h>
42 #include <sys/rman.h>
43
44 #include <net/bpf.h>
45 #include <net/if.h>
46 #include <net/if_arp.h>
47 #include <net/ethernet.h>
48 #include <net/if_dl.h>
49 #include <net/if_media.h>
50 #include <net/if_types.h>
51
52 #include <net80211/ieee80211_var.h>
53 #include <net80211/ieee80211_amrr.h>
54 #include <net80211/ieee80211_phy.h>
55 #include <net80211/ieee80211_radiotap.h>
56 #include <net80211/ieee80211_regdomain.h>
57
58 #include <dev/usb/usb.h>
59 #include <dev/usb/usbdi.h>
60 #include <dev/usb/usbdi_util.h>
61 #include "usbdevs.h"
62
63 #include <dev/usb/if_uralreg.h>
64 #include <dev/usb/if_uralvar.h>
65
66 #ifdef USB_DEBUG
67 #define DPRINTF(x) do { if (uraldebug > 0) printf x; } while (0)
68 #define DPRINTFN(n, x) do { if (uraldebug >= (n)) printf x; } while (0)
69 int uraldebug = 0;
70 SYSCTL_NODE(_hw_usb, OID_AUTO, ural, CTLFLAG_RW, 0, "USB ural");
71 SYSCTL_INT(_hw_usb_ural, OID_AUTO, debug, CTLFLAG_RW, &uraldebug, 0,
72 "ural debug level");
73 #else
74 #define DPRINTF(x)
75 #define DPRINTFN(n, x)
76 #endif
77
78 #define URAL_RSSI(rssi) \
79 ((rssi) > (RAL_NOISE_FLOOR + RAL_RSSI_CORR) ? \
80 ((rssi) - (RAL_NOISE_FLOOR + RAL_RSSI_CORR)) : 0)
81
82 /* various supported device vendors/products */
83 static const struct usb_devno ural_devs[] = {
84 { USB_VENDOR_ASUS, USB_PRODUCT_ASUS_WL167G },
85 { USB_VENDOR_ASUS, USB_PRODUCT_RALINK_RT2570 },
86 { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D7050 },
87 { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D7051 },
88 { USB_VENDOR_CONCEPTRONIC2, USB_PRODUCT_CONCEPTRONIC2_C54RU },
89 { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DWLG122 },
90 { USB_VENDOR_GIGABYTE, USB_PRODUCT_GIGABYTE_GNWBKG },
91 { USB_VENDOR_GIGABYTE, USB_PRODUCT_GIGABYTE_GN54G },
92 { USB_VENDOR_GUILLEMOT, USB_PRODUCT_GUILLEMOT_HWGUSB254 },
93 { USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_WUSB54G },
94 { USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_WUSB54GP },
95 { USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_HU200TS },
96 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_KG54 },
97 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_KG54AI },
98 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_KG54YB },
99 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_NINWIFI },
100 { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2570 },
101 { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2570_2 },
102 { USB_VENDOR_MSI, USB_PRODUCT_MSI_RT2570_3 },
103 { USB_VENDOR_NOVATECH, USB_PRODUCT_NOVATECH_NV902 },
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_SIEMENS2, USB_PRODUCT_SIEMENS2_WL54G },
108 { USB_VENDOR_SMC, USB_PRODUCT_SMC_2862WG },
109 { USB_VENDOR_SPHAIRON, USB_PRODUCT_SPHAIRON_UB801R},
110 { USB_VENDOR_SURECOM, USB_PRODUCT_SURECOM_RT2570 },
111 { USB_VENDOR_VTECH, USB_PRODUCT_VTECH_RT2570 },
112 { USB_VENDOR_ZINWELL, USB_PRODUCT_ZINWELL_RT2570 }
113 };
114
115 MODULE_DEPEND(ural, wlan, 1, 1, 1);
116 MODULE_DEPEND(ural, wlan_amrr, 1, 1, 1);
117 MODULE_DEPEND(ural, usb, 1, 1, 1);
118
119 static struct ieee80211vap *ural_vap_create(struct ieee80211com *,
120 const char name[IFNAMSIZ], int unit, int opmode,
121 int flags, const uint8_t bssid[IEEE80211_ADDR_LEN],
122 const uint8_t mac[IEEE80211_ADDR_LEN]);
123 static void ural_vap_delete(struct ieee80211vap *);
124 static int ural_alloc_tx_list(struct ural_softc *);
125 static void ural_free_tx_list(struct ural_softc *);
126 static int ural_alloc_rx_list(struct ural_softc *);
127 static void ural_free_rx_list(struct ural_softc *);
128 static void ural_task(void *);
129 static void ural_scantask(void *);
130 static int ural_newstate(struct ieee80211vap *,
131 enum ieee80211_state, int);
132 static void ural_txeof(usbd_xfer_handle, usbd_private_handle,
133 usbd_status);
134 static void ural_rxeof(usbd_xfer_handle, usbd_private_handle,
135 usbd_status);
136 static void ural_setup_tx_desc(struct ural_softc *,
137 struct ural_tx_desc *, uint32_t, int, int);
138 static int ural_tx_bcn(struct ural_softc *, struct mbuf *,
139 struct ieee80211_node *);
140 static int ural_tx_mgt(struct ural_softc *, struct mbuf *,
141 struct ieee80211_node *);
142 static int ural_tx_data(struct ural_softc *, struct mbuf *,
143 struct ieee80211_node *);
144 static void ural_start(struct ifnet *);
145 static void ural_watchdog(void *);
146 static int ural_ioctl(struct ifnet *, u_long, caddr_t);
147 static void ural_set_testmode(struct ural_softc *);
148 static void ural_eeprom_read(struct ural_softc *, uint16_t, void *,
149 int);
150 static uint16_t ural_read(struct ural_softc *, uint16_t);
151 static void ural_read_multi(struct ural_softc *, uint16_t, void *,
152 int);
153 static void ural_write(struct ural_softc *, uint16_t, uint16_t);
154 static void ural_write_multi(struct ural_softc *, uint16_t, void *,
155 int) __unused;
156 static void ural_bbp_write(struct ural_softc *, uint8_t, uint8_t);
157 static uint8_t ural_bbp_read(struct ural_softc *, uint8_t);
158 static void ural_rf_write(struct ural_softc *, uint8_t, uint32_t);
159 static struct ieee80211_node *ural_node_alloc(struct ieee80211vap *,
160 const uint8_t mac[IEEE80211_ADDR_LEN]);
161 static void ural_newassoc(struct ieee80211_node *, int);
162 static void ural_scan_start(struct ieee80211com *);
163 static void ural_scan_end(struct ieee80211com *);
164 static void ural_set_channel(struct ieee80211com *);
165 static void ural_set_chan(struct ural_softc *,
166 struct ieee80211_channel *);
167 static void ural_disable_rf_tune(struct ural_softc *);
168 static void ural_enable_tsf_sync(struct ural_softc *);
169 static void ural_update_slot(struct ifnet *);
170 static void ural_set_txpreamble(struct ural_softc *);
171 static void ural_set_basicrates(struct ural_softc *,
172 const struct ieee80211_channel *);
173 static void ural_set_bssid(struct ural_softc *, const uint8_t *);
174 static void ural_set_macaddr(struct ural_softc *, uint8_t *);
175 static void ural_update_promisc(struct ural_softc *);
176 static const char *ural_get_rf(int);
177 static void ural_read_eeprom(struct ural_softc *);
178 static int ural_bbp_init(struct ural_softc *);
179 static void ural_set_txantenna(struct ural_softc *, int);
180 static void ural_set_rxantenna(struct ural_softc *, int);
181 static void ural_init_locked(struct ural_softc *);
182 static void ural_init(void *);
183 static void ural_stop(void *);
184 static int ural_raw_xmit(struct ieee80211_node *, struct mbuf *,
185 const struct ieee80211_bpf_params *);
186 static void ural_amrr_start(struct ural_softc *,
187 struct ieee80211_node *);
188 static void ural_amrr_timeout(void *);
189 static void ural_amrr_update(usbd_xfer_handle, usbd_private_handle,
190 usbd_status status);
191
192 /*
193 * Default values for MAC registers; values taken from the reference driver.
194 */
195 static const struct {
196 uint16_t reg;
197 uint16_t val;
198 } ural_def_mac[] = {
199 { RAL_TXRX_CSR5, 0x8c8d },
200 { RAL_TXRX_CSR6, 0x8b8a },
201 { RAL_TXRX_CSR7, 0x8687 },
202 { RAL_TXRX_CSR8, 0x0085 },
203 { RAL_MAC_CSR13, 0x1111 },
204 { RAL_MAC_CSR14, 0x1e11 },
205 { RAL_TXRX_CSR21, 0xe78f },
206 { RAL_MAC_CSR9, 0xff1d },
207 { RAL_MAC_CSR11, 0x0002 },
208 { RAL_MAC_CSR22, 0x0053 },
209 { RAL_MAC_CSR15, 0x0000 },
210 { RAL_MAC_CSR8, 0x0780 },
211 { RAL_TXRX_CSR19, 0x0000 },
212 { RAL_TXRX_CSR18, 0x005a },
213 { RAL_PHY_CSR2, 0x0000 },
214 { RAL_TXRX_CSR0, 0x1ec0 },
215 { RAL_PHY_CSR4, 0x000f }
216 };
217
218 /*
219 * Default values for BBP registers; values taken from the reference driver.
220 */
221 static const struct {
222 uint8_t reg;
223 uint8_t val;
224 } ural_def_bbp[] = {
225 { 3, 0x02 },
226 { 4, 0x19 },
227 { 14, 0x1c },
228 { 15, 0x30 },
229 { 16, 0xac },
230 { 17, 0x48 },
231 { 18, 0x18 },
232 { 19, 0xff },
233 { 20, 0x1e },
234 { 21, 0x08 },
235 { 22, 0x08 },
236 { 23, 0x08 },
237 { 24, 0x80 },
238 { 25, 0x50 },
239 { 26, 0x08 },
240 { 27, 0x23 },
241 { 30, 0x10 },
242 { 31, 0x2b },
243 { 32, 0xb9 },
244 { 34, 0x12 },
245 { 35, 0x50 },
246 { 39, 0xc4 },
247 { 40, 0x02 },
248 { 41, 0x60 },
249 { 53, 0x10 },
250 { 54, 0x18 },
251 { 56, 0x08 },
252 { 57, 0x10 },
253 { 58, 0x08 },
254 { 61, 0x60 },
255 { 62, 0x10 },
256 { 75, 0xff }
257 };
258
259 /*
260 * Default values for RF register R2 indexed by channel numbers.
261 */
262 static const uint32_t ural_rf2522_r2[] = {
263 0x307f6, 0x307fb, 0x30800, 0x30805, 0x3080a, 0x3080f, 0x30814,
264 0x30819, 0x3081e, 0x30823, 0x30828, 0x3082d, 0x30832, 0x3083e
265 };
266
267 static const uint32_t ural_rf2523_r2[] = {
268 0x00327, 0x00328, 0x00329, 0x0032a, 0x0032b, 0x0032c, 0x0032d,
269 0x0032e, 0x0032f, 0x00340, 0x00341, 0x00342, 0x00343, 0x00346
270 };
271
272 static const uint32_t ural_rf2524_r2[] = {
273 0x00327, 0x00328, 0x00329, 0x0032a, 0x0032b, 0x0032c, 0x0032d,
274 0x0032e, 0x0032f, 0x00340, 0x00341, 0x00342, 0x00343, 0x00346
275 };
276
277 static const uint32_t ural_rf2525_r2[] = {
278 0x20327, 0x20328, 0x20329, 0x2032a, 0x2032b, 0x2032c, 0x2032d,
279 0x2032e, 0x2032f, 0x20340, 0x20341, 0x20342, 0x20343, 0x20346
280 };
281
282 static const uint32_t ural_rf2525_hi_r2[] = {
283 0x2032f, 0x20340, 0x20341, 0x20342, 0x20343, 0x20344, 0x20345,
284 0x20346, 0x20347, 0x20348, 0x20349, 0x2034a, 0x2034b, 0x2034e
285 };
286
287 static const uint32_t ural_rf2525e_r2[] = {
288 0x2044d, 0x2044e, 0x2044f, 0x20460, 0x20461, 0x20462, 0x20463,
289 0x20464, 0x20465, 0x20466, 0x20467, 0x20468, 0x20469, 0x2046b
290 };
291
292 static const uint32_t ural_rf2526_hi_r2[] = {
293 0x0022a, 0x0022b, 0x0022b, 0x0022c, 0x0022c, 0x0022d, 0x0022d,
294 0x0022e, 0x0022e, 0x0022f, 0x0022d, 0x00240, 0x00240, 0x00241
295 };
296
297 static const uint32_t ural_rf2526_r2[] = {
298 0x00226, 0x00227, 0x00227, 0x00228, 0x00228, 0x00229, 0x00229,
299 0x0022a, 0x0022a, 0x0022b, 0x0022b, 0x0022c, 0x0022c, 0x0022d
300 };
301
302 /*
303 * For dual-band RF, RF registers R1 and R4 also depend on channel number;
304 * values taken from the reference driver.
305 */
306 static const struct {
307 uint8_t chan;
308 uint32_t r1;
309 uint32_t r2;
310 uint32_t r4;
311 } ural_rf5222[] = {
312 { 1, 0x08808, 0x0044d, 0x00282 },
313 { 2, 0x08808, 0x0044e, 0x00282 },
314 { 3, 0x08808, 0x0044f, 0x00282 },
315 { 4, 0x08808, 0x00460, 0x00282 },
316 { 5, 0x08808, 0x00461, 0x00282 },
317 { 6, 0x08808, 0x00462, 0x00282 },
318 { 7, 0x08808, 0x00463, 0x00282 },
319 { 8, 0x08808, 0x00464, 0x00282 },
320 { 9, 0x08808, 0x00465, 0x00282 },
321 { 10, 0x08808, 0x00466, 0x00282 },
322 { 11, 0x08808, 0x00467, 0x00282 },
323 { 12, 0x08808, 0x00468, 0x00282 },
324 { 13, 0x08808, 0x00469, 0x00282 },
325 { 14, 0x08808, 0x0046b, 0x00286 },
326
327 { 36, 0x08804, 0x06225, 0x00287 },
328 { 40, 0x08804, 0x06226, 0x00287 },
329 { 44, 0x08804, 0x06227, 0x00287 },
330 { 48, 0x08804, 0x06228, 0x00287 },
331 { 52, 0x08804, 0x06229, 0x00287 },
332 { 56, 0x08804, 0x0622a, 0x00287 },
333 { 60, 0x08804, 0x0622b, 0x00287 },
334 { 64, 0x08804, 0x0622c, 0x00287 },
335
336 { 100, 0x08804, 0x02200, 0x00283 },
337 { 104, 0x08804, 0x02201, 0x00283 },
338 { 108, 0x08804, 0x02202, 0x00283 },
339 { 112, 0x08804, 0x02203, 0x00283 },
340 { 116, 0x08804, 0x02204, 0x00283 },
341 { 120, 0x08804, 0x02205, 0x00283 },
342 { 124, 0x08804, 0x02206, 0x00283 },
343 { 128, 0x08804, 0x02207, 0x00283 },
344 { 132, 0x08804, 0x02208, 0x00283 },
345 { 136, 0x08804, 0x02209, 0x00283 },
346 { 140, 0x08804, 0x0220a, 0x00283 },
347
348 { 149, 0x08808, 0x02429, 0x00281 },
349 { 153, 0x08808, 0x0242b, 0x00281 },
350 { 157, 0x08808, 0x0242d, 0x00281 },
351 { 161, 0x08808, 0x0242f, 0x00281 }
352 };
353
354 static device_probe_t ural_match;
355 static device_attach_t ural_attach;
356 static device_detach_t ural_detach;
357
358 static device_method_t ural_methods[] = {
359 /* Device interface */
360 DEVMETHOD(device_probe, ural_match),
361 DEVMETHOD(device_attach, ural_attach),
362 DEVMETHOD(device_detach, ural_detach),
363
364 { 0, 0 }
365 };
366
367 static driver_t ural_driver = {
368 "ural",
369 ural_methods,
370 sizeof(struct ural_softc)
371 };
372
373 static devclass_t ural_devclass;
374
375 DRIVER_MODULE(ural, uhub, ural_driver, ural_devclass, usbd_driver_load, 0);
376
377 static int
378 ural_match(device_t self)
379 {
380 struct usb_attach_arg *uaa = device_get_ivars(self);
381
382 if (uaa->iface != NULL)
383 return UMATCH_NONE;
384
385 return (usb_lookup(ural_devs, uaa->vendor, uaa->product) != NULL) ?
386 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
387 }
388
389 static int
390 ural_attach(device_t self)
391 {
392 struct ural_softc *sc = device_get_softc(self);
393 struct usb_attach_arg *uaa = device_get_ivars(self);
394 struct ifnet *ifp;
395 struct ieee80211com *ic;
396 usb_interface_descriptor_t *id;
397 usb_endpoint_descriptor_t *ed;
398 usbd_status error;
399 int i;
400 uint8_t bands;
401
402 sc->sc_udev = uaa->device;
403 sc->sc_dev = self;
404
405 if (usbd_set_config_no(sc->sc_udev, RAL_CONFIG_NO, 0) != 0) {
406 device_printf(self, "could not set configuration no\n");
407 return ENXIO;
408 }
409
410 /* get the first interface handle */
411 error = usbd_device2interface_handle(sc->sc_udev, RAL_IFACE_INDEX,
412 &sc->sc_iface);
413 if (error != 0) {
414 device_printf(self, "could not get interface handle\n");
415 return ENXIO;
416 }
417
418 /*
419 * Find endpoints.
420 */
421 id = usbd_get_interface_descriptor(sc->sc_iface);
422
423 sc->sc_rx_no = sc->sc_tx_no = -1;
424 for (i = 0; i < id->bNumEndpoints; i++) {
425 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
426 if (ed == NULL) {
427 device_printf(self, "no endpoint descriptor for %d\n",
428 i);
429 return ENXIO;
430 }
431
432 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
433 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
434 sc->sc_rx_no = ed->bEndpointAddress;
435 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
436 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
437 sc->sc_tx_no = ed->bEndpointAddress;
438 }
439 if (sc->sc_rx_no == -1 || sc->sc_tx_no == -1) {
440 device_printf(self, "missing endpoint\n");
441 return ENXIO;
442 }
443
444 ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
445 if (ifp == NULL) {
446 device_printf(sc->sc_dev, "can not if_alloc()\n");
447 return ENXIO;
448 }
449 ic = ifp->if_l2com;
450
451 mtx_init(&sc->sc_mtx, device_get_nameunit(sc->sc_dev), MTX_NETWORK_LOCK,
452 MTX_DEF | MTX_RECURSE);
453
454 usb_init_task(&sc->sc_task, ural_task, sc);
455 usb_init_task(&sc->sc_scantask, ural_scantask, sc);
456 callout_init(&sc->watchdog_ch, 0);
457
458 /* retrieve RT2570 rev. no */
459 sc->asic_rev = ural_read(sc, RAL_MAC_CSR0);
460
461 /* retrieve MAC address and various other things from EEPROM */
462 ural_read_eeprom(sc);
463
464 device_printf(sc->sc_dev, "MAC/BBP RT2570 (rev 0x%02x), RF %s\n",
465 sc->asic_rev, ural_get_rf(sc->rf_rev));
466
467 ifp->if_softc = sc;
468 if_initname(ifp, "ural", device_get_unit(sc->sc_dev));
469 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST |
470 IFF_NEEDSGIANT; /* USB stack is still under Giant lock */
471 ifp->if_init = ural_init;
472 ifp->if_ioctl = ural_ioctl;
473 ifp->if_start = ural_start;
474 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
475 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
476 IFQ_SET_READY(&ifp->if_snd);
477
478 ic->ic_ifp = ifp;
479 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
480
481 /* set device capabilities */
482 ic->ic_caps =
483 IEEE80211_C_STA /* station mode supported */
484 | IEEE80211_C_IBSS /* IBSS mode supported */
485 | IEEE80211_C_MONITOR /* monitor mode supported */
486 | IEEE80211_C_HOSTAP /* HostAp mode supported */
487 | IEEE80211_C_TXPMGT /* tx power management */
488 | IEEE80211_C_SHPREAMBLE /* short preamble supported */
489 | IEEE80211_C_SHSLOT /* short slot time supported */
490 | IEEE80211_C_BGSCAN /* bg scanning supported */
491 | IEEE80211_C_WPA /* 802.11i */
492 ;
493
494 bands = 0;
495 setbit(&bands, IEEE80211_MODE_11B);
496 setbit(&bands, IEEE80211_MODE_11G);
497 if (sc->rf_rev == RAL_RF_5222)
498 setbit(&bands, IEEE80211_MODE_11A);
499 ieee80211_init_channels(ic, NULL, &bands);
500
501 ieee80211_ifattach(ic);
502 ic->ic_newassoc = ural_newassoc;
503 ic->ic_raw_xmit = ural_raw_xmit;
504 ic->ic_node_alloc = ural_node_alloc;
505 ic->ic_scan_start = ural_scan_start;
506 ic->ic_scan_end = ural_scan_end;
507 ic->ic_set_channel = ural_set_channel;
508
509 ic->ic_vap_create = ural_vap_create;
510 ic->ic_vap_delete = ural_vap_delete;
511
512 sc->sc_rates = ieee80211_get_ratetable(ic->ic_curchan);
513
514 bpfattach(ifp, DLT_IEEE802_11_RADIO,
515 sizeof (struct ieee80211_frame) + sizeof(sc->sc_txtap));
516
517 sc->sc_rxtap_len = sizeof sc->sc_rxtap;
518 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
519 sc->sc_rxtap.wr_ihdr.it_present = htole32(RAL_RX_RADIOTAP_PRESENT);
520
521 sc->sc_txtap_len = sizeof sc->sc_txtap;
522 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
523 sc->sc_txtap.wt_ihdr.it_present = htole32(RAL_TX_RADIOTAP_PRESENT);
524
525 if (bootverbose)
526 ieee80211_announce(ic);
527
528 return 0;
529 }
530
531 static int
532 ural_detach(device_t self)
533 {
534 struct ural_softc *sc = device_get_softc(self);
535 struct ifnet *ifp = sc->sc_ifp;
536 struct ieee80211com *ic = ifp->if_l2com;
537
538 ural_stop(sc);
539 bpfdetach(ifp);
540 ieee80211_ifdetach(ic);
541
542 usb_rem_task(sc->sc_udev, &sc->sc_task);
543 usb_rem_task(sc->sc_udev, &sc->sc_scantask);
544 callout_stop(&sc->watchdog_ch);
545
546 if (sc->amrr_xfer != NULL) {
547 usbd_free_xfer(sc->amrr_xfer);
548 sc->amrr_xfer = NULL;
549 }
550
551 if (sc->sc_rx_pipeh != NULL) {
552 usbd_abort_pipe(sc->sc_rx_pipeh);
553 usbd_close_pipe(sc->sc_rx_pipeh);
554 }
555
556 if (sc->sc_tx_pipeh != NULL) {
557 usbd_abort_pipe(sc->sc_tx_pipeh);
558 usbd_close_pipe(sc->sc_tx_pipeh);
559 }
560
561 ural_free_rx_list(sc);
562 ural_free_tx_list(sc);
563
564 if_free(ifp);
565 mtx_destroy(&sc->sc_mtx);
566
567 return 0;
568 }
569
570 static struct ieee80211vap *
571 ural_vap_create(struct ieee80211com *ic,
572 const char name[IFNAMSIZ], int unit, int opmode, int flags,
573 const uint8_t bssid[IEEE80211_ADDR_LEN],
574 const uint8_t mac[IEEE80211_ADDR_LEN])
575 {
576 struct ural_vap *uvp;
577 struct ieee80211vap *vap;
578
579 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */
580 return NULL;
581 uvp = (struct ural_vap *) malloc(sizeof(struct ural_vap),
582 M_80211_VAP, M_NOWAIT | M_ZERO);
583 if (uvp == NULL)
584 return NULL;
585 vap = &uvp->vap;
586 /* enable s/w bmiss handling for sta mode */
587 ieee80211_vap_setup(ic, vap, name, unit, opmode,
588 flags | IEEE80211_CLONE_NOBEACONS, bssid, mac);
589
590 /* override state transition machine */
591 uvp->newstate = vap->iv_newstate;
592 vap->iv_newstate = ural_newstate;
593
594 callout_init(&uvp->amrr_ch, 0);
595 ieee80211_amrr_init(&uvp->amrr, vap,
596 IEEE80211_AMRR_MIN_SUCCESS_THRESHOLD,
597 IEEE80211_AMRR_MAX_SUCCESS_THRESHOLD,
598 1000 /* 1 sec */);
599
600 /* complete setup */
601 ieee80211_vap_attach(vap, ieee80211_media_change, ieee80211_media_status);
602 ic->ic_opmode = opmode;
603 return vap;
604 }
605
606 static void
607 ural_vap_delete(struct ieee80211vap *vap)
608 {
609 struct ural_vap *uvp = URAL_VAP(vap);
610
611 callout_stop(&uvp->amrr_ch);
612 ieee80211_amrr_cleanup(&uvp->amrr);
613 ieee80211_vap_detach(vap);
614 free(uvp, M_80211_VAP);
615 }
616
617 static int
618 ural_alloc_tx_list(struct ural_softc *sc)
619 {
620 struct ural_tx_data *data;
621 int i, error;
622
623 sc->tx_queued = sc->tx_cur = 0;
624
625 for (i = 0; i < RAL_TX_LIST_COUNT; i++) {
626 data = &sc->tx_data[i];
627
628 data->sc = sc;
629
630 data->xfer = usbd_alloc_xfer(sc->sc_udev);
631 if (data->xfer == NULL) {
632 device_printf(sc->sc_dev,
633 "could not allocate tx xfer\n");
634 error = ENOMEM;
635 goto fail;
636 }
637
638 data->buf = usbd_alloc_buffer(data->xfer,
639 RAL_TX_DESC_SIZE + MCLBYTES);
640 if (data->buf == NULL) {
641 device_printf(sc->sc_dev,
642 "could not allocate tx buffer\n");
643 error = ENOMEM;
644 goto fail;
645 }
646 }
647
648 return 0;
649
650 fail: ural_free_tx_list(sc);
651 return error;
652 }
653
654 static void
655 ural_free_tx_list(struct ural_softc *sc)
656 {
657 struct ural_tx_data *data;
658 int i;
659
660 for (i = 0; i < RAL_TX_LIST_COUNT; i++) {
661 data = &sc->tx_data[i];
662
663 if (data->xfer != NULL) {
664 usbd_free_xfer(data->xfer);
665 data->xfer = NULL;
666 }
667
668 if (data->ni != NULL) {
669 ieee80211_free_node(data->ni);
670 data->ni = NULL;
671 }
672 }
673 }
674
675 static int
676 ural_alloc_rx_list(struct ural_softc *sc)
677 {
678 struct ural_rx_data *data;
679 int i, error;
680
681 for (i = 0; i < RAL_RX_LIST_COUNT; i++) {
682 data = &sc->rx_data[i];
683
684 data->sc = sc;
685
686 data->xfer = usbd_alloc_xfer(sc->sc_udev);
687 if (data->xfer == NULL) {
688 device_printf(sc->sc_dev,
689 "could not allocate rx xfer\n");
690 error = ENOMEM;
691 goto fail;
692 }
693
694 if (usbd_alloc_buffer(data->xfer, MCLBYTES) == NULL) {
695 device_printf(sc->sc_dev,
696 "could not allocate rx buffer\n");
697 error = ENOMEM;
698 goto fail;
699 }
700
701 data->m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
702 if (data->m == NULL) {
703 device_printf(sc->sc_dev,
704 "could not allocate rx mbuf\n");
705 error = ENOMEM;
706 goto fail;
707 }
708
709 data->buf = mtod(data->m, uint8_t *);
710 }
711
712 return 0;
713
714 fail: ural_free_rx_list(sc);
715 return error;
716 }
717
718 static void
719 ural_free_rx_list(struct ural_softc *sc)
720 {
721 struct ural_rx_data *data;
722 int i;
723
724 for (i = 0; i < RAL_RX_LIST_COUNT; i++) {
725 data = &sc->rx_data[i];
726
727 if (data->xfer != NULL) {
728 usbd_free_xfer(data->xfer);
729 data->xfer = NULL;
730 }
731
732 if (data->m != NULL) {
733 m_freem(data->m);
734 data->m = NULL;
735 }
736 }
737 }
738
739 static void
740 ural_task(void *xarg)
741 {
742 struct ural_softc *sc = xarg;
743 struct ifnet *ifp = sc->sc_ifp;
744 struct ieee80211com *ic = ifp->if_l2com;
745 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
746 struct ural_vap *uvp = URAL_VAP(vap);
747 const struct ieee80211_txparam *tp;
748 enum ieee80211_state ostate;
749 struct ieee80211_node *ni;
750 struct mbuf *m;
751
752 ostate = vap->iv_state;
753
754 RAL_LOCK(sc);
755 switch (sc->sc_state) {
756 case IEEE80211_S_INIT:
757 if (ostate == IEEE80211_S_RUN) {
758 /* abort TSF synchronization */
759 ural_write(sc, RAL_TXRX_CSR19, 0);
760
761 /* force tx led to stop blinking */
762 ural_write(sc, RAL_MAC_CSR20, 0);
763 }
764 break;
765
766 case IEEE80211_S_RUN:
767 ni = vap->iv_bss;
768
769 if (vap->iv_opmode != IEEE80211_M_MONITOR) {
770 ural_update_slot(ic->ic_ifp);
771 ural_set_txpreamble(sc);
772 ural_set_basicrates(sc, ic->ic_bsschan);
773 ural_set_bssid(sc, ni->ni_bssid);
774 }
775
776 if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
777 vap->iv_opmode == IEEE80211_M_IBSS) {
778 m = ieee80211_beacon_alloc(ni, &uvp->bo);
779 if (m == NULL) {
780 device_printf(sc->sc_dev,
781 "could not allocate beacon\n");
782 return;
783 }
784
785 if (ural_tx_bcn(sc, m, ni) != 0) {
786 device_printf(sc->sc_dev,
787 "could not send beacon\n");
788 return;
789 }
790 }
791
792 /* make tx led blink on tx (controlled by ASIC) */
793 ural_write(sc, RAL_MAC_CSR20, 1);
794
795 if (vap->iv_opmode != IEEE80211_M_MONITOR)
796 ural_enable_tsf_sync(sc);
797
798 /* enable automatic rate adaptation */
799 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_bsschan)];
800 if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE)
801 ural_amrr_start(sc, ni);
802
803 break;
804
805 default:
806 break;
807 }
808
809 RAL_UNLOCK(sc);
810
811 IEEE80211_LOCK(ic);
812 uvp->newstate(vap, sc->sc_state, sc->sc_arg);
813 if (vap->iv_newstate_cb != NULL)
814 vap->iv_newstate_cb(vap, sc->sc_state, sc->sc_arg);
815 IEEE80211_UNLOCK(ic);
816 }
817
818 static void
819 ural_scantask(void *arg)
820 {
821 struct ural_softc *sc = arg;
822 struct ifnet *ifp = sc->sc_ifp;
823 struct ieee80211com *ic = ifp->if_l2com;
824 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
825
826 RAL_LOCK(sc);
827 if (sc->sc_scan_action == URAL_SCAN_START) {
828 /* abort TSF synchronization */
829 ural_write(sc, RAL_TXRX_CSR19, 0);
830 ural_set_bssid(sc, ifp->if_broadcastaddr);
831 } else if (sc->sc_scan_action == URAL_SET_CHANNEL) {
832 mtx_lock(&Giant);
833 ural_set_chan(sc, ic->ic_curchan);
834 mtx_unlock(&Giant);
835 } else {
836 ural_enable_tsf_sync(sc);
837 /* XXX keep local copy */
838 ural_set_bssid(sc, vap->iv_bss->ni_bssid);
839 }
840 RAL_UNLOCK(sc);
841 }
842
843 static int
844 ural_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
845 {
846 struct ural_vap *uvp = URAL_VAP(vap);
847 struct ieee80211com *ic = vap->iv_ic;
848 struct ural_softc *sc = ic->ic_ifp->if_softc;
849
850 callout_stop(&uvp->amrr_ch);
851
852 /* do it in a process context */
853 sc->sc_state = nstate;
854 sc->sc_arg = arg;
855
856 usb_ |