if_zyd.c revision 1.48.2.2 1 /* $OpenBSD: if_zyd.c,v 1.52 2007/02/11 00:08:04 jsg Exp $ */
2 /* $NetBSD: if_zyd.c,v 1.48.2.2 2020/04/08 14:08:13 martin Exp $ */
3
4 /*-
5 * Copyright (c) 2006 by Damien Bergamini <damien.bergamini (at) free.fr>
6 * Copyright (c) 2006 by Florian Stoehr <ich (at) florian-stoehr.de>
7 *
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 */
20
21 /*-
22 * ZyDAS ZD1211/ZD1211B USB WLAN driver.
23 */
24
25 #include <sys/cdefs.h>
26 __KERNEL_RCSID(0, "$NetBSD: if_zyd.c,v 1.48.2.2 2020/04/08 14:08:13 martin Exp $");
27
28 #ifdef _KERNEL_OPT
29 #include "opt_usb.h"
30 #endif
31
32 #include <sys/param.h>
33 #include <sys/sockio.h>
34 #include <sys/proc.h>
35 #include <sys/mbuf.h>
36 #include <sys/kernel.h>
37 #include <sys/kmem.h>
38 #include <sys/socket.h>
39 #include <sys/systm.h>
40 #include <sys/malloc.h>
41 #include <sys/conf.h>
42 #include <sys/device.h>
43
44 #include <sys/bus.h>
45 #include <machine/endian.h>
46
47 #include <net/bpf.h>
48 #include <net/if.h>
49 #include <net/if_arp.h>
50 #include <net/if_dl.h>
51 #include <net/if_ether.h>
52 #include <net/if_media.h>
53 #include <net/if_types.h>
54
55 #include <netinet/in.h>
56 #include <netinet/in_systm.h>
57 #include <netinet/in_var.h>
58 #include <netinet/ip.h>
59
60 #include <net80211/ieee80211_netbsd.h>
61 #include <net80211/ieee80211_var.h>
62 #include <net80211/ieee80211_amrr.h>
63 #include <net80211/ieee80211_radiotap.h>
64
65 #include <dev/firmload.h>
66
67 #include <dev/usb/usb.h>
68 #include <dev/usb/usbdi.h>
69 #include <dev/usb/usbdi_util.h>
70 #include <dev/usb/usbdevs.h>
71
72 #include <dev/usb/if_zydreg.h>
73
74 #ifdef ZYD_DEBUG
75 #define DPRINTF(x) do { if (zyddebug > 0) printf x; } while (0)
76 #define DPRINTFN(n, x) do { if (zyddebug > (n)) printf x; } while (0)
77 int zyddebug = 0;
78 #else
79 #define DPRINTF(x)
80 #define DPRINTFN(n, x)
81 #endif
82
83 static const struct zyd_phy_pair zyd_def_phy[] = ZYD_DEF_PHY;
84 static const struct zyd_phy_pair zyd_def_phyB[] = ZYD_DEF_PHYB;
85
86 /* various supported device vendors/products */
87 #define ZYD_ZD1211_DEV(v, p) \
88 { { USB_VENDOR_##v, USB_PRODUCT_##v##_##p }, ZYD_ZD1211 }
89 #define ZYD_ZD1211B_DEV(v, p) \
90 { { USB_VENDOR_##v, USB_PRODUCT_##v##_##p }, ZYD_ZD1211B }
91 static const struct zyd_type {
92 struct usb_devno dev;
93 uint8_t rev;
94 #define ZYD_ZD1211 0
95 #define ZYD_ZD1211B 1
96 } zyd_devs[] = {
97 ZYD_ZD1211_DEV(3COM2, 3CRUSB10075),
98 ZYD_ZD1211_DEV(ABOCOM, WL54),
99 ZYD_ZD1211_DEV(ASUSTEK, WL159G),
100 ZYD_ZD1211_DEV(CYBERTAN, TG54USB),
101 ZYD_ZD1211_DEV(DRAYTEK, VIGOR550),
102 ZYD_ZD1211_DEV(PLANEX2, GWUS54GD),
103 ZYD_ZD1211_DEV(PLANEX2, GWUS54GZL),
104 ZYD_ZD1211_DEV(PLANEX3, GWUS54GZ),
105 ZYD_ZD1211_DEV(PLANEX3, GWUS54MINI),
106 ZYD_ZD1211_DEV(SAGEM, XG760A),
107 ZYD_ZD1211_DEV(SENAO, NUB8301),
108 ZYD_ZD1211_DEV(SITECOMEU, WL113),
109 ZYD_ZD1211_DEV(SWEEX, ZD1211),
110 ZYD_ZD1211_DEV(TEKRAM, QUICKWLAN),
111 ZYD_ZD1211_DEV(TEKRAM, ZD1211_1),
112 ZYD_ZD1211_DEV(TEKRAM, ZD1211_2),
113 ZYD_ZD1211_DEV(TWINMOS, G240),
114 ZYD_ZD1211_DEV(UMEDIA, ALL0298V2),
115 ZYD_ZD1211_DEV(UMEDIA, TEW429UB_A),
116 ZYD_ZD1211_DEV(UMEDIA, TEW429UB),
117 ZYD_ZD1211_DEV(WISTRONNEWEB, UR055G),
118 ZYD_ZD1211_DEV(ZCOM, ZD1211),
119 ZYD_ZD1211_DEV(ZYDAS, ZD1211),
120 ZYD_ZD1211_DEV(ZYXEL, AG225H),
121 ZYD_ZD1211_DEV(ZYXEL, ZYAIRG220),
122 ZYD_ZD1211_DEV(ZYXEL, G200V2),
123
124 ZYD_ZD1211B_DEV(ACCTON, SMCWUSBG),
125 ZYD_ZD1211B_DEV(ACCTON, WN4501H_LF_IR),
126 ZYD_ZD1211B_DEV(ACCTON, WUS201),
127 ZYD_ZD1211B_DEV(ACCTON, ZD1211B),
128 ZYD_ZD1211B_DEV(ASUSTEK, A9T_WIFI),
129 ZYD_ZD1211B_DEV(BELKIN, F5D7050C),
130 ZYD_ZD1211B_DEV(BELKIN, ZD1211B),
131 ZYD_ZD1211B_DEV(BEWAN, BWIFI_USB54AR),
132 ZYD_ZD1211B_DEV(CISCOLINKSYS, WUSBF54G),
133 ZYD_ZD1211B_DEV(CYBERTAN, ZD1211B),
134 ZYD_ZD1211B_DEV(FIBERLINE, WL430U),
135 ZYD_ZD1211B_DEV(MELCO, KG54L),
136 ZYD_ZD1211B_DEV(PHILIPS, SNU5600),
137 ZYD_ZD1211B_DEV(PHILIPS, SNU5630NS05),
138 ZYD_ZD1211B_DEV(PLANEX2, GWUS54GXS),
139 ZYD_ZD1211B_DEV(SAGEM, XG76NA),
140 ZYD_ZD1211B_DEV(SITECOMEU, WL603),
141 ZYD_ZD1211B_DEV(SITECOMEU, ZD1211B),
142 ZYD_ZD1211B_DEV(SONY, IFU_WLM2),
143 ZYD_ZD1211B_DEV(UMEDIA, TEW429UBC1),
144 ZYD_ZD1211B_DEV(UNKNOWN1, ZD1211B_1),
145 ZYD_ZD1211B_DEV(UNKNOWN1, ZD1211B_2),
146 ZYD_ZD1211B_DEV(UNKNOWN2, ZD1211B),
147 ZYD_ZD1211B_DEV(UNKNOWN3, ZD1211B),
148 ZYD_ZD1211B_DEV(USR, USR5423),
149 ZYD_ZD1211B_DEV(VTECH, ZD1211B),
150 ZYD_ZD1211B_DEV(ZCOM, ZD1211B),
151 ZYD_ZD1211B_DEV(ZYDAS, ZD1211B),
152 ZYD_ZD1211B_DEV(ZYDAS, ZD1211B_2),
153 ZYD_ZD1211B_DEV(ZYXEL, M202),
154 ZYD_ZD1211B_DEV(ZYXEL, G220V2),
155 };
156 #define zyd_lookup(v, p) \
157 ((const struct zyd_type *)usb_lookup(zyd_devs, v, p))
158
159 static int zyd_match(device_t, cfdata_t, void *);
160 static void zyd_attach(device_t, device_t, void *);
161 static int zyd_detach(device_t, int);
162 static int zyd_activate(device_t, enum devact);
163
164
165 CFATTACH_DECL_NEW(zyd, sizeof(struct zyd_softc), zyd_match,
166 zyd_attach, zyd_detach, zyd_activate);
167
168 Static void zyd_attachhook(device_t);
169 Static int zyd_complete_attach(struct zyd_softc *);
170 Static int zyd_open_pipes(struct zyd_softc *);
171 Static void zyd_close_pipes(struct zyd_softc *);
172 Static int zyd_alloc_tx_list(struct zyd_softc *);
173 Static void zyd_free_tx_list(struct zyd_softc *);
174 Static int zyd_alloc_rx_list(struct zyd_softc *);
175 Static void zyd_free_rx_list(struct zyd_softc *);
176 Static struct ieee80211_node *zyd_node_alloc(struct ieee80211_node_table *);
177 Static int zyd_media_change(struct ifnet *);
178 Static void zyd_next_scan(void *);
179 Static void zyd_task(void *);
180 Static int zyd_newstate(struct ieee80211com *, enum ieee80211_state, int);
181 Static int zyd_cmd(struct zyd_softc *, uint16_t, const void *, int,
182 void *, int, u_int);
183 Static int zyd_read16(struct zyd_softc *, uint16_t, uint16_t *);
184 Static int zyd_read32(struct zyd_softc *, uint16_t, uint32_t *);
185 Static int zyd_write16(struct zyd_softc *, uint16_t, uint16_t);
186 Static int zyd_write32(struct zyd_softc *, uint16_t, uint32_t);
187 Static int zyd_rfwrite(struct zyd_softc *, uint32_t);
188 Static void zyd_lock_phy(struct zyd_softc *);
189 Static void zyd_unlock_phy(struct zyd_softc *);
190 Static int zyd_rfmd_init(struct zyd_rf *);
191 Static int zyd_rfmd_switch_radio(struct zyd_rf *, int);
192 Static int zyd_rfmd_set_channel(struct zyd_rf *, uint8_t);
193 Static int zyd_al2230_init(struct zyd_rf *);
194 Static int zyd_al2230_switch_radio(struct zyd_rf *, int);
195 Static int zyd_al2230_set_channel(struct zyd_rf *, uint8_t);
196 Static int zyd_al2230_init_b(struct zyd_rf *);
197 Static int zyd_al7230B_init(struct zyd_rf *);
198 Static int zyd_al7230B_switch_radio(struct zyd_rf *, int);
199 Static int zyd_al7230B_set_channel(struct zyd_rf *, uint8_t);
200 Static int zyd_al2210_init(struct zyd_rf *);
201 Static int zyd_al2210_switch_radio(struct zyd_rf *, int);
202 Static int zyd_al2210_set_channel(struct zyd_rf *, uint8_t);
203 Static int zyd_gct_init(struct zyd_rf *);
204 Static int zyd_gct_switch_radio(struct zyd_rf *, int);
205 Static int zyd_gct_set_channel(struct zyd_rf *, uint8_t);
206 Static int zyd_maxim_init(struct zyd_rf *);
207 Static int zyd_maxim_switch_radio(struct zyd_rf *, int);
208 Static int zyd_maxim_set_channel(struct zyd_rf *, uint8_t);
209 Static int zyd_maxim2_init(struct zyd_rf *);
210 Static int zyd_maxim2_switch_radio(struct zyd_rf *, int);
211 Static int zyd_maxim2_set_channel(struct zyd_rf *, uint8_t);
212 Static int zyd_rf_attach(struct zyd_softc *, uint8_t);
213 Static const char *zyd_rf_name(uint8_t);
214 Static int zyd_hw_init(struct zyd_softc *);
215 Static int zyd_read_eeprom(struct zyd_softc *);
216 Static int zyd_set_macaddr(struct zyd_softc *, const uint8_t *);
217 Static int zyd_set_bssid(struct zyd_softc *, const uint8_t *);
218 Static int zyd_switch_radio(struct zyd_softc *, int);
219 Static void zyd_set_led(struct zyd_softc *, int, int);
220 Static int zyd_set_rxfilter(struct zyd_softc *);
221 Static void zyd_set_chan(struct zyd_softc *, struct ieee80211_channel *);
222 Static int zyd_set_beacon_interval(struct zyd_softc *, int);
223 Static uint8_t zyd_plcp_signal(int);
224 Static void zyd_intr(struct usbd_xfer *, void *, usbd_status);
225 Static void zyd_rx_data(struct zyd_softc *, const uint8_t *, uint16_t);
226 Static void zyd_rxeof(struct usbd_xfer *, void *, usbd_status);
227 Static void zyd_txeof(struct usbd_xfer *, void *, usbd_status);
228 Static int zyd_tx_mgt(struct zyd_softc *, struct mbuf *,
229 struct ieee80211_node *);
230 Static int zyd_tx_data(struct zyd_softc *, struct mbuf *,
231 struct ieee80211_node *);
232 Static void zyd_start(struct ifnet *);
233 Static void zyd_watchdog(struct ifnet *);
234 Static int zyd_ioctl(struct ifnet *, u_long, void *);
235 Static int zyd_init(struct ifnet *);
236 Static void zyd_stop(struct ifnet *, int);
237 Static int zyd_loadfirmware(struct zyd_softc *, u_char *, size_t);
238 Static void zyd_iter_func(void *, struct ieee80211_node *);
239 Static void zyd_amrr_timeout(void *);
240 Static void zyd_newassoc(struct ieee80211_node *, int);
241
242 static int
243 zyd_match(device_t parent, cfdata_t match, void *aux)
244 {
245 struct usb_attach_arg *uaa = aux;
246
247 return (zyd_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL) ?
248 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
249 }
250
251 Static void
252 zyd_attachhook(device_t self)
253 {
254 struct zyd_softc *sc = device_private(self);
255 firmware_handle_t fwh;
256 const char *fwname;
257 u_char *fw;
258 size_t size;
259 int error;
260
261 fwname = (sc->mac_rev == ZYD_ZD1211) ? "zyd-zd1211" : "zyd-zd1211b";
262 if ((error = firmware_open("zyd", fwname, &fwh)) != 0) {
263 aprint_error_dev(sc->sc_dev,
264 "failed to open firmware %s (error=%d)\n", fwname, error);
265 return;
266 }
267 size = firmware_get_size(fwh);
268 fw = firmware_malloc(size);
269 if (fw == NULL) {
270 aprint_error_dev(sc->sc_dev,
271 "failed to allocate firmware memory\n");
272 firmware_close(fwh);
273 return;
274 }
275 error = firmware_read(fwh, 0, fw, size);
276 firmware_close(fwh);
277 if (error != 0) {
278 aprint_error_dev(sc->sc_dev,
279 "failed to read firmware (error %d)\n", error);
280 firmware_free(fw, size);
281 return;
282 }
283
284 error = zyd_loadfirmware(sc, fw, size);
285 if (error != 0) {
286 aprint_error_dev(sc->sc_dev,
287 "could not load firmware (error=%d)\n", error);
288 firmware_free(fw, size);
289 return;
290 }
291
292 firmware_free(fw, size);
293
294 /* complete the attach process */
295 if ((error = zyd_complete_attach(sc)) == 0)
296 sc->attached = 1;
297 return;
298 }
299
300 static void
301 zyd_attach(device_t parent, device_t self, void *aux)
302 {
303 struct zyd_softc *sc = device_private(self);
304 struct usb_attach_arg *uaa = aux;
305 char *devinfop;
306 usb_device_descriptor_t* ddesc;
307 struct ifnet *ifp = &sc->sc_if;
308
309 sc->sc_dev = self;
310 sc->sc_udev = uaa->uaa_device;
311
312 aprint_naive("\n");
313 aprint_normal("\n");
314
315 devinfop = usbd_devinfo_alloc(uaa->uaa_device, 0);
316 aprint_normal_dev(self, "%s\n", devinfop);
317 usbd_devinfo_free(devinfop);
318
319 sc->mac_rev = zyd_lookup(uaa->uaa_vendor, uaa->uaa_product)->rev;
320
321 ddesc = usbd_get_device_descriptor(sc->sc_udev);
322 if (UGETW(ddesc->bcdDevice) < 0x4330) {
323 aprint_error_dev(self, "device version mismatch: %#x "
324 "(only >= 43.30 supported)\n", UGETW(ddesc->bcdDevice));
325 return;
326 }
327
328 ifp->if_softc = sc;
329 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
330 ifp->if_init = zyd_init;
331 ifp->if_ioctl = zyd_ioctl;
332 ifp->if_start = zyd_start;
333 ifp->if_watchdog = zyd_watchdog;
334 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
335 IFQ_SET_READY(&ifp->if_snd);
336 memcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
337
338 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
339 cv_init(&sc->sc_cmdcv, "zydcmd");
340 SIMPLEQ_INIT(&sc->sc_rqh);
341
342 /* defer configrations after file system is ready to load firmware */
343 config_mountroot(self, zyd_attachhook);
344 }
345
346 Static int
347 zyd_complete_attach(struct zyd_softc *sc)
348 {
349 struct ieee80211com *ic = &sc->sc_ic;
350 struct ifnet *ifp = &sc->sc_if;
351 usbd_status error;
352 int i;
353
354 usb_init_task(&sc->sc_task, zyd_task, sc, 0);
355 callout_init(&(sc->sc_scan_ch), 0);
356
357 sc->amrr.amrr_min_success_threshold = 1;
358 sc->amrr.amrr_max_success_threshold = 10;
359 callout_init(&sc->sc_amrr_ch, 0);
360
361 error = usbd_set_config_no(sc->sc_udev, ZYD_CONFIG_NO, 1);
362 if (error != 0) {
363 aprint_error_dev(sc->sc_dev, "failed to set configuration"
364 ", err=%s\n", usbd_errstr(error));
365 goto fail;
366 }
367
368 error = usbd_device2interface_handle(sc->sc_udev, ZYD_IFACE_INDEX,
369 &sc->sc_iface);
370 if (error != 0) {
371 aprint_error_dev(sc->sc_dev,
372 "getting interface handle failed\n");
373 goto fail;
374 }
375
376 if ((error = zyd_open_pipes(sc)) != 0) {
377 aprint_error_dev(sc->sc_dev, "could not open pipes\n");
378 goto fail;
379 }
380
381 if ((error = zyd_read_eeprom(sc)) != 0) {
382 aprint_error_dev(sc->sc_dev, "could not read EEPROM\n");
383 goto fail;
384 }
385
386 if ((error = zyd_rf_attach(sc, sc->rf_rev)) != 0) {
387 aprint_error_dev(sc->sc_dev, "could not attach RF\n");
388 goto fail;
389 }
390
391 if ((error = zyd_hw_init(sc)) != 0) {
392 aprint_error_dev(sc->sc_dev,
393 "hardware initialization failed\n");
394 goto fail;
395 }
396
397 aprint_normal_dev(sc->sc_dev,
398 "HMAC ZD1211%s, FW %02x.%02x, RF %s, PA %x, address %s\n",
399 (sc->mac_rev == ZYD_ZD1211) ? "": "B",
400 sc->fw_rev >> 8, sc->fw_rev & 0xff, zyd_rf_name(sc->rf_rev),
401 sc->pa_rev, ether_sprintf(ic->ic_myaddr));
402
403 ic->ic_ifp = ifp;
404 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
405 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
406 ic->ic_state = IEEE80211_S_INIT;
407
408 /* set device capabilities */
409 ic->ic_caps =
410 IEEE80211_C_MONITOR | /* monitor mode supported */
411 IEEE80211_C_TXPMGT | /* tx power management */
412 IEEE80211_C_SHPREAMBLE | /* short preamble supported */
413 IEEE80211_C_WEP; /* s/w WEP */
414
415 /* set supported .11b and .11g rates */
416 ic->ic_sup_rates[IEEE80211_MODE_11B] = ieee80211_std_rateset_11b;
417 ic->ic_sup_rates[IEEE80211_MODE_11G] = ieee80211_std_rateset_11g;
418
419 /* set supported .11b and .11g channels (1 through 14) */
420 for (i = 1; i <= 14; i++) {
421 ic->ic_channels[i].ic_freq =
422 ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
423 ic->ic_channels[i].ic_flags =
424 IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
425 IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
426 }
427
428 if_attach(ifp);
429 ieee80211_ifattach(ic);
430 ic->ic_node_alloc = zyd_node_alloc;
431 ic->ic_newassoc = zyd_newassoc;
432
433 /* override state transition machine */
434 sc->sc_newstate = ic->ic_newstate;
435 ic->ic_newstate = zyd_newstate;
436
437 /* XXX media locking needs revisiting */
438 mutex_init(&sc->sc_media_mtx, MUTEX_DEFAULT, IPL_SOFTUSB);
439 ieee80211_media_init_with_lock(ic,
440 zyd_media_change, ieee80211_media_status, &sc->sc_media_mtx);
441
442 bpf_attach2(ifp, DLT_IEEE802_11_RADIO,
443 sizeof(struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN,
444 &sc->sc_drvbpf);
445
446 sc->sc_rxtap_len = sizeof(sc->sc_rxtapu);
447 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
448 sc->sc_rxtap.wr_ihdr.it_present = htole32(ZYD_RX_RADIOTAP_PRESENT);
449
450 sc->sc_txtap_len = sizeof(sc->sc_txtapu);
451 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
452 sc->sc_txtap.wt_ihdr.it_present = htole32(ZYD_TX_RADIOTAP_PRESENT);
453
454 ieee80211_announce(ic);
455
456 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
457
458 fail: return error;
459 }
460
461 static int
462 zyd_detach(device_t self, int flags)
463 {
464 struct zyd_softc *sc = device_private(self);
465 struct ieee80211com *ic = &sc->sc_ic;
466 struct ifnet *ifp = &sc->sc_if;
467
468 if (!sc->attached)
469 return 0;
470
471 mutex_enter(&sc->sc_lock);
472
473 zyd_stop(ifp, 1);
474 callout_halt(&sc->sc_scan_ch, NULL);
475 callout_halt(&sc->sc_amrr_ch, NULL);
476 usb_rem_task_wait(sc->sc_udev, &sc->sc_task, USB_TASKQ_DRIVER, NULL);
477
478 /* Abort, etc. done by zyd_stop */
479 zyd_close_pipes(sc);
480
481 sc->attached = 0;
482
483 bpf_detach(ifp);
484 ieee80211_ifdetach(ic);
485 if_detach(ifp);
486
487 mutex_exit(&sc->sc_lock);
488
489 mutex_destroy(&sc->sc_lock);
490 cv_destroy(&sc->sc_cmdcv);
491
492 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
493
494 return 0;
495 }
496
497 Static int
498 zyd_open_pipes(struct zyd_softc *sc)
499 {
500 usb_endpoint_descriptor_t *edesc;
501 usbd_status error;
502
503 /* interrupt in */
504 edesc = usbd_get_endpoint_descriptor(sc->sc_iface, 0x83);
505 if (edesc == NULL)
506 return EINVAL;
507
508 sc->ibuf_size = UGETW(edesc->wMaxPacketSize);
509 if (sc->ibuf_size == 0) /* should not happen */
510 return EINVAL;
511
512 sc->ibuf = kmem_alloc(sc->ibuf_size, KM_SLEEP);
513
514 error = usbd_open_pipe_intr(sc->sc_iface, 0x83, USBD_SHORT_XFER_OK,
515 &sc->zyd_ep[ZYD_ENDPT_IIN], sc, sc->ibuf, sc->ibuf_size, zyd_intr,
516 USBD_DEFAULT_INTERVAL);
517 if (error != 0) {
518 printf("%s: open rx intr pipe failed: %s\n",
519 device_xname(sc->sc_dev), usbd_errstr(error));
520 goto fail;
521 }
522
523 /* interrupt out (not necessarily an interrupt pipe) */
524 error = usbd_open_pipe(sc->sc_iface, 0x04, USBD_EXCLUSIVE_USE,
525 &sc->zyd_ep[ZYD_ENDPT_IOUT]);
526 if (error != 0) {
527 printf("%s: open tx intr pipe failed: %s\n",
528 device_xname(sc->sc_dev), usbd_errstr(error));
529 goto fail;
530 }
531
532 /* bulk in */
533 error = usbd_open_pipe(sc->sc_iface, 0x82, USBD_EXCLUSIVE_USE,
534 &sc->zyd_ep[ZYD_ENDPT_BIN]);
535 if (error != 0) {
536 printf("%s: open rx pipe failed: %s\n",
537 device_xname(sc->sc_dev), usbd_errstr(error));
538 goto fail;
539 }
540
541 /* bulk out */
542 error = usbd_open_pipe(sc->sc_iface, 0x01, USBD_EXCLUSIVE_USE,
543 &sc->zyd_ep[ZYD_ENDPT_BOUT]);
544 if (error != 0) {
545 printf("%s: open tx pipe failed: %s\n",
546 device_xname(sc->sc_dev), usbd_errstr(error));
547 goto fail;
548 }
549
550 return 0;
551
552 fail: zyd_close_pipes(sc);
553 return error;
554 }
555
556 Static void
557 zyd_close_pipes(struct zyd_softc *sc)
558 {
559 int i;
560
561 for (i = 0; i < ZYD_ENDPT_CNT; i++) {
562 if (sc->zyd_ep[i] != NULL) {
563 usbd_close_pipe(sc->zyd_ep[i]);
564 sc->zyd_ep[i] = NULL;
565 }
566 }
567 if (sc->ibuf != NULL) {
568 kmem_free(sc->ibuf, sc->ibuf_size);
569 sc->ibuf = NULL;
570 }
571 }
572
573 Static int
574 zyd_alloc_tx_list(struct zyd_softc *sc)
575 {
576 int i, error;
577
578 sc->tx_queued = 0;
579
580 for (i = 0; i < ZYD_TX_LIST_CNT; i++) {
581 struct zyd_tx_data *data = &sc->tx_data[i];
582
583 data->sc = sc; /* backpointer for callbacks */
584
585 error = usbd_create_xfer(sc->zyd_ep[ZYD_ENDPT_BOUT],
586 ZYD_MAX_TXBUFSZ, USBD_FORCE_SHORT_XFER, 0, &data->xfer);
587 if (error) {
588 printf("%s: could not allocate tx xfer\n",
589 device_xname(sc->sc_dev));
590 goto fail;
591 }
592 data->buf = usbd_get_buffer(data->xfer);
593
594 /* clear Tx descriptor */
595 memset(data->buf, 0, sizeof(struct zyd_tx_desc));
596 }
597 return 0;
598
599 fail: zyd_free_tx_list(sc);
600 return error;
601 }
602
603 Static void
604 zyd_free_tx_list(struct zyd_softc *sc)
605 {
606 int i;
607
608 for (i = 0; i < ZYD_TX_LIST_CNT; i++) {
609 struct zyd_tx_data *data = &sc->tx_data[i];
610
611 if (data->xfer != NULL) {
612 usbd_destroy_xfer(data->xfer);
613 data->xfer = NULL;
614 }
615 if (data->ni != NULL) {
616 ieee80211_free_node(data->ni);
617 data->ni = NULL;
618 }
619 }
620 }
621
622 Static int
623 zyd_alloc_rx_list(struct zyd_softc *sc)
624 {
625 int i, error;
626
627 for (i = 0; i < ZYD_RX_LIST_CNT; i++) {
628 struct zyd_rx_data *data = &sc->rx_data[i];
629
630 data->sc = sc; /* backpointer for callbacks */
631
632 error = usbd_create_xfer(sc->zyd_ep[ZYD_ENDPT_BIN],
633 ZYX_MAX_RXBUFSZ, 0, 0, &data->xfer);
634 if (error) {
635 printf("%s: could not allocate rx xfer\n",
636 device_xname(sc->sc_dev));
637 goto fail;
638 }
639 data->buf = usbd_get_buffer(data->xfer);
640 }
641 return 0;
642
643 fail: zyd_free_rx_list(sc);
644 return error;
645 }
646
647 Static void
648 zyd_free_rx_list(struct zyd_softc *sc)
649 {
650 int i;
651
652 for (i = 0; i < ZYD_RX_LIST_CNT; i++) {
653 struct zyd_rx_data *data = &sc->rx_data[i];
654
655 if (data->xfer != NULL) {
656 usbd_destroy_xfer(data->xfer);
657 data->xfer = NULL;
658 }
659 }
660 }
661
662 /* ARGUSED */
663 Static struct ieee80211_node *
664 zyd_node_alloc(struct ieee80211_node_table *nt __unused)
665 {
666 struct zyd_node *zn;
667
668 zn = malloc(sizeof(struct zyd_node), M_80211_NODE, M_NOWAIT | M_ZERO);
669 return zn ? &zn->ni : NULL;
670 }
671
672 Static int
673 zyd_media_change(struct ifnet *ifp)
674 {
675 int error;
676
677 error = ieee80211_media_change(ifp);
678 if (error != ENETRESET)
679 return error;
680
681 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
682 zyd_init(ifp);
683
684 return 0;
685 }
686
687 /*
688 * This function is called periodically (every 200ms) during scanning to
689 * switch from one channel to another.
690 */
691 Static void
692 zyd_next_scan(void *arg)
693 {
694 struct zyd_softc *sc = arg;
695 struct ieee80211com *ic = &sc->sc_ic;
696
697 if (ic->ic_state == IEEE80211_S_SCAN)
698 ieee80211_next_scan(ic);
699 }
700
701 Static void
702 zyd_task(void *arg)
703 {
704 struct zyd_softc *sc = arg;
705 struct ieee80211com *ic = &sc->sc_ic;
706 enum ieee80211_state ostate;
707
708 ostate = ic->ic_state;
709
710 switch (sc->sc_state) {
711 case IEEE80211_S_INIT:
712 if (ostate == IEEE80211_S_RUN) {
713 /* turn link LED off */
714 zyd_set_led(sc, ZYD_LED1, 0);
715
716 /* stop data LED from blinking */
717 zyd_write32(sc, sc->fwbase + ZYD_FW_LINK_STATUS, 0);
718 }
719 break;
720
721 case IEEE80211_S_SCAN:
722 zyd_set_chan(sc, ic->ic_curchan);
723 callout_reset(&sc->sc_scan_ch, hz / 5, zyd_next_scan, sc);
724 break;
725
726 case IEEE80211_S_AUTH:
727 case IEEE80211_S_ASSOC:
728 zyd_set_chan(sc, ic->ic_curchan);
729 break;
730
731 case IEEE80211_S_RUN:
732 {
733 struct ieee80211_node *ni = ic->ic_bss;
734
735 zyd_set_chan(sc, ic->ic_curchan);
736
737 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
738 /* turn link LED on */
739 zyd_set_led(sc, ZYD_LED1, 1);
740
741 /* make data LED blink upon Tx */
742 zyd_write32(sc, sc->fwbase + ZYD_FW_LINK_STATUS, 1);
743
744 zyd_set_bssid(sc, ni->ni_bssid);
745 }
746
747 if (ic->ic_opmode == IEEE80211_M_STA) {
748 /* fake a join to init the tx rate */
749 zyd_newassoc(ni, 1);
750 }
751
752 /* start automatic rate control timer */
753 if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE)
754 callout_reset(&sc->sc_amrr_ch, hz, zyd_amrr_timeout, sc);
755
756 break;
757 }
758 }
759
760 sc->sc_newstate(ic, sc->sc_state, -1);
761 }
762
763 Static int
764 zyd_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
765 {
766 struct zyd_softc *sc = ic->ic_ifp->if_softc;
767
768 if (!sc->attached)
769 return ENXIO;
770
771 /*
772 * XXXSMP: This does not wait for the task, if it is in flight,
773 * to complete. If this code works at all, it must rely on the
774 * kernel lock to serialize with the USB task thread.
775 */
776 usb_rem_task(sc->sc_udev, &sc->sc_task);
777 callout_stop(&sc->sc_scan_ch);
778 callout_stop(&sc->sc_amrr_ch);
779
780 /* do it in a process context */
781 sc->sc_state = nstate;
782 usb_add_task(sc->sc_udev, &sc->sc_task, USB_TASKQ_DRIVER);
783
784 return 0;
785 }
786
787 Static int
788 zyd_cmd(struct zyd_softc *sc, uint16_t code, const void *idata, int ilen,
789 void *odata, int olen, u_int flags)
790 {
791 struct usbd_xfer *xfer;
792 struct zyd_cmd cmd;
793 struct rq rq;
794 uint16_t xferflags;
795 int error;
796 usbd_status uerror;
797
798 error = usbd_create_xfer(sc->zyd_ep[ZYD_ENDPT_IOUT],
799 sizeof(uint16_t) + ilen, USBD_FORCE_SHORT_XFER, 0, &xfer);
800 if (error)
801 return error;
802
803 cmd.code = htole16(code);
804 memcpy(cmd.data, idata, ilen);
805
806 xferflags = USBD_FORCE_SHORT_XFER;
807 if (!(flags & ZYD_CMD_FLAG_READ))
808 xferflags |= USBD_SYNCHRONOUS;
809 else {
810 rq.idata = idata;
811 rq.odata = odata;
812 rq.len = olen / sizeof(struct zyd_pair);
813 mutex_enter(&sc->sc_lock);
814 SIMPLEQ_INSERT_TAIL(&sc->sc_rqh, &rq, rq);
815 mutex_exit(&sc->sc_lock);
816 }
817
818 usbd_setup_xfer(xfer, 0, &cmd, sizeof(uint16_t) + ilen, xferflags,
819 ZYD_INTR_TIMEOUT, NULL);
820 uerror = usbd_transfer(xfer);
821 if (uerror != USBD_IN_PROGRESS && uerror != 0) {
822 printf("%s: could not send command (error=%s)\n",
823 device_xname(sc->sc_dev), usbd_errstr(uerror));
824 (void)usbd_destroy_xfer(xfer);
825 return EIO;
826 }
827 if (!(flags & ZYD_CMD_FLAG_READ)) {
828 (void)usbd_destroy_xfer(xfer);
829 return 0; /* write: don't wait for reply */
830 }
831 /* wait at most one second for command reply */
832 mutex_enter(&sc->sc_lock);
833 error = cv_timedwait_sig(&sc->sc_cmdcv, &sc->sc_lock, hz);
834 if (error == EWOULDBLOCK)
835 printf("%s: zyd_read sleep timeout\n", device_xname(sc->sc_dev));
836 SIMPLEQ_REMOVE(&sc->sc_rqh, &rq, rq, rq);
837 mutex_exit(&sc->sc_lock);
838
839 (void)usbd_destroy_xfer(xfer);
840 return error;
841 }
842
843 Static int
844 zyd_read16(struct zyd_softc *sc, uint16_t reg, uint16_t *val)
845 {
846 struct zyd_pair tmp;
847 int error;
848
849 reg = htole16(reg);
850 error = zyd_cmd(sc, ZYD_CMD_IORD, ®, sizeof(reg), &tmp, sizeof(tmp),
851 ZYD_CMD_FLAG_READ);
852 if (error == 0)
853 *val = le16toh(tmp.val);
854 else
855 *val = 0;
856 return error;
857 }
858
859 Static int
860 zyd_read32(struct zyd_softc *sc, uint16_t reg, uint32_t *val)
861 {
862 struct zyd_pair tmp[2];
863 uint16_t regs[2];
864 int error;
865
866 regs[0] = htole16(ZYD_REG32_HI(reg));
867 regs[1] = htole16(ZYD_REG32_LO(reg));
868 error = zyd_cmd(sc, ZYD_CMD_IORD, regs, sizeof(regs), tmp, sizeof(tmp),
869 ZYD_CMD_FLAG_READ);
870 if (error == 0)
871 *val = le16toh(tmp[0].val) << 16 | le16toh(tmp[1].val);
872 else
873 *val = 0;
874 return error;
875 }
876
877 Static int
878 zyd_write16(struct zyd_softc *sc, uint16_t reg, uint16_t val)
879 {
880 struct zyd_pair pair;
881
882 pair.reg = htole16(reg);
883 pair.val = htole16(val);
884
885 return zyd_cmd(sc, ZYD_CMD_IOWR, &pair, sizeof(pair), NULL, 0, 0);
886 }
887
888 Static int
889 zyd_write32(struct zyd_softc *sc, uint16_t reg, uint32_t val)
890 {
891 struct zyd_pair pair[2];
892
893 pair[0].reg = htole16(ZYD_REG32_HI(reg));
894 pair[0].val = htole16(val >> 16);
895 pair[1].reg = htole16(ZYD_REG32_LO(reg));
896 pair[1].val = htole16(val & 0xffff);
897
898 return zyd_cmd(sc, ZYD_CMD_IOWR, pair, sizeof(pair), NULL, 0, 0);
899 }
900
901 Static int
902 zyd_rfwrite(struct zyd_softc *sc, uint32_t val)
903 {
904 struct zyd_rf *rf = &sc->sc_rf;
905 struct zyd_rfwrite req;
906 uint16_t cr203;
907 int i;
908
909 (void)zyd_read16(sc, ZYD_CR203, &cr203);
910 cr203 &= ~(ZYD_RF_IF_LE | ZYD_RF_CLK | ZYD_RF_DATA);
911
912 req.code = htole16(2);
913 req.width = htole16(rf->width);
914 for (i = 0; i < rf->width; i++) {
915 req.bit[i] = htole16(cr203);
916 if (val & (1 << (rf->width - 1 - i)))
917 req.bit[i] |= htole16(ZYD_RF_DATA);
918 }
919 return zyd_cmd(sc, ZYD_CMD_RFCFG, &req, 4 + 2 * rf->width, NULL, 0, 0);
920 }
921
922 Static void
923 zyd_lock_phy(struct zyd_softc *sc)
924 {
925 uint32_t tmp;
926
927 (void)zyd_read32(sc, ZYD_MAC_MISC, &tmp);
928 tmp &= ~ZYD_UNLOCK_PHY_REGS;
929 (void)zyd_write32(sc, ZYD_MAC_MISC, tmp);
930 }
931
932 Static void
933 zyd_unlock_phy(struct zyd_softc *sc)
934 {
935 uint32_t tmp;
936
937 (void)zyd_read32(sc, ZYD_MAC_MISC, &tmp);
938 tmp |= ZYD_UNLOCK_PHY_REGS;
939 (void)zyd_write32(sc, ZYD_MAC_MISC, tmp);
940 }
941
942 /*
943 * RFMD RF methods.
944 */
945 Static int
946 zyd_rfmd_init(struct zyd_rf *rf)
947 {
948 struct zyd_softc *sc = rf->rf_sc;
949 static const struct zyd_phy_pair phyini[] = ZYD_RFMD_PHY;
950 static const uint32_t rfini[] = ZYD_RFMD_RF;
951 int error;
952 size_t i;
953
954 /* init RF-dependent PHY registers */
955 for (i = 0; i < __arraycount(phyini); i++) {
956 error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
957 if (error != 0)
958 return error;
959 }
960
961 /* init RFMD radio */
962 for (i = 0; i < __arraycount(rfini); i++) {
963 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
964 return error;
965 }
966 return 0;
967 }
968
969 Static int
970 zyd_rfmd_switch_radio(struct zyd_rf *rf, int on)
971 {
972 struct zyd_softc *sc = rf->rf_sc;
973
974 (void)zyd_write16(sc, ZYD_CR10, on ? 0x89 : 0x15);
975 (void)zyd_write16(sc, ZYD_CR11, on ? 0x00 : 0x81);
976
977 return 0;
978 }
979
980 Static int
981 zyd_rfmd_set_channel(struct zyd_rf *rf, uint8_t chan)
982 {
983 struct zyd_softc *sc = rf->rf_sc;
984 static const struct {
985 uint32_t r1, r2;
986 } rfprog[] = ZYD_RFMD_CHANTABLE;
987
988 (void)zyd_rfwrite(sc, rfprog[chan - 1].r1);
989 (void)zyd_rfwrite(sc, rfprog[chan - 1].r2);
990
991 return 0;
992 }
993
994 /*
995 * AL2230 RF methods.
996 */
997 Static int
998 zyd_al2230_init(struct zyd_rf *rf)
999 {
1000 struct zyd_softc *sc = rf->rf_sc;
1001 static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY;
1002 static const struct zyd_phy_pair phy2230s[] = ZYD_AL2230S_PHY_INIT;
1003 static const uint32_t rfini[] = ZYD_AL2230_RF;
1004 int error;
1005 size_t i;
1006
1007 /* init RF-dependent PHY registers */
1008 for (i = 0; i < __arraycount(phyini); i++) {
1009 error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1010 if (error != 0)
1011 return error;
1012 }
1013
1014 if (sc->rf_rev == ZYD_RF_AL2230S) {
1015 for (i = 0; i < __arraycount(phy2230s); i++) {
1016 error = zyd_write16(sc, phy2230s[i].reg,
1017 phy2230s[i].val);
1018 if (error != 0)
1019 return error;
1020 }
1021 }
1022
1023 /* init AL2230 radio */
1024 for (i = 0; i < __arraycount(rfini); i++) {
1025 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1026 return error;
1027 }
1028 return 0;
1029 }
1030
1031 Static int
1032 zyd_al2230_init_b(struct zyd_rf *rf)
1033 {
1034 struct zyd_softc *sc = rf->rf_sc;
1035 static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY_B;
1036 static const uint32_t rfini[] = ZYD_AL2230_RF_B;
1037 int error;
1038 size_t i;
1039
1040 /* init RF-dependent PHY registers */
1041 for (i = 0; i < __arraycount(phyini); i++) {
1042 error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1043 if (error != 0)
1044 return error;
1045 }
1046
1047 /* init AL2230 radio */
1048 for (i = 0; i < __arraycount(rfini); i++) {
1049 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1050 return error;
1051 }
1052 return 0;
1053 }
1054
1055 Static int
1056 zyd_al2230_switch_radio(struct zyd_rf *rf, int on)
1057 {
1058 struct zyd_softc *sc = rf->rf_sc;
1059 int on251 = (sc->mac_rev == ZYD_ZD1211) ? 0x3f : 0x7f;
1060
1061 (void)zyd_write16(sc, ZYD_CR11, on ? 0x00 : 0x04);
1062 (void)zyd_write16(sc, ZYD_CR251, on ? on251 : 0x2f);
1063
1064 return 0;
1065 }
1066
1067 Static int
1068 zyd_al2230_set_channel(struct zyd_rf *rf, uint8_t chan)
1069 {
1070 struct zyd_softc *sc = rf->rf_sc;
1071 static const struct {
1072 uint32_t r1, r2, r3;
1073 } rfprog[] = ZYD_AL2230_CHANTABLE;
1074
1075 (void)zyd_rfwrite(sc, rfprog[chan - 1].r1);
1076 (void)zyd_rfwrite(sc, rfprog[chan - 1].r2);
1077 (void)zyd_rfwrite(sc, rfprog[chan - 1].r3);
1078
1079 (void)zyd_write16(sc, ZYD_CR138, 0x28);
1080 (void)zyd_write16(sc, ZYD_CR203, 0x06);
1081
1082 return 0;
1083 }
1084
1085 /*
1086 * AL7230B RF methods.
1087 */
1088 Static int
1089 zyd_al7230B_init(struct zyd_rf *rf)
1090 {
1091 struct zyd_softc *sc = rf->rf_sc;
1092 static const struct zyd_phy_pair phyini_1[] = ZYD_AL7230B_PHY_1;
1093 static const struct zyd_phy_pair phyini_2[] = ZYD_AL7230B_PHY_2;
1094 static const struct zyd_phy_pair phyini_3[] = ZYD_AL7230B_PHY_3;
1095 static const uint32_t rfini_1[] = ZYD_AL7230B_RF_1;
1096 static const uint32_t rfini_2[] = ZYD_AL7230B_RF_2;
1097 int error;
1098 size_t i;
1099
1100 /* for AL7230B, PHY and RF need to be initialized in "phases" */
1101
1102 /* init RF-dependent PHY registers, part one */
1103 for (i = 0; i < __arraycount(phyini_1); i++) {
1104 error = zyd_write16(sc, phyini_1[i].reg, phyini_1[i].val);
1105 if (error != 0)
1106 return error;
1107 }
1108 /* init AL7230B radio, part one */
1109 for (i = 0; i < __arraycount(rfini_1); i++) {
1110 if ((error = zyd_rfwrite(sc, rfini_1[i])) != 0)
1111 return error;
1112 }
1113 /* init RF-dependent PHY registers, part two */
1114 for (i = 0; i < __arraycount(phyini_2); i++) {
1115 error = zyd_write16(sc, phyini_2[i].reg, phyini_2[i].val);
1116 if (error != 0)
1117 return error;
1118 }
1119 /* init AL7230B radio, part two */
1120 for (i = 0; i < __arraycount(rfini_2); i++) {
1121 if ((error = zyd_rfwrite(sc, rfini_2[i])) != 0)
1122 return error;
1123 }
1124 /* init RF-dependent PHY registers, part three */
1125 for (i = 0; i < __arraycount(phyini_3); i++) {
1126 error = zyd_write16(sc, phyini_3[i].reg, phyini_3[i].val);
1127 if (error != 0)
1128 return error;
1129 }
1130
1131 return 0;
1132 }
1133
1134 Static int
1135 zyd_al7230B_switch_radio(struct zyd_rf *rf, int on)
1136 {
1137 struct zyd_softc *sc = rf->rf_sc;
1138
1139 (void)zyd_write16(sc, ZYD_CR11, on ? 0x00 : 0x04);
1140 (void)zyd_write16(sc, ZYD_CR251, on ? 0x3f : 0x2f);
1141
1142 return 0;
1143 }
1144
1145 Static int
1146 zyd_al7230B_set_channel(struct zyd_rf *rf, uint8_t chan)
1147 {
1148 struct zyd_softc *sc = rf->rf_sc;
1149 static const struct {
1150 uint32_t r1, r2;
1151 } rfprog[] = ZYD_AL7230B_CHANTABLE;
1152 static const uint32_t rfsc[] = ZYD_AL7230B_RF_SETCHANNEL;
1153 int error;
1154 size_t i;
1155
1156 (void)zyd_write16(sc, ZYD_CR240, 0x57);
1157 (void)zyd_write16(sc, ZYD_CR251, 0x2f);
1158
1159 for (i = 0; i < __arraycount(rfsc); i++) {
1160 if ((error = zyd_rfwrite(sc, rfsc[i])) != 0)
1161 return error;
1162 }
1163
1164 (void)zyd_write16(sc, ZYD_CR128, 0x14);
1165 (void)zyd_write16(sc, ZYD_CR129, 0x12);
1166 (void)zyd_write16(sc, ZYD_CR130, 0x10);
1167 (void)zyd_write16(sc, ZYD_CR38, 0x38);
1168 (void)zyd_write16(sc, ZYD_CR136, 0xdf);
1169
1170 (void)zyd_rfwrite(sc, rfprog[chan - 1].r1);
1171 (void)zyd_rfwrite(sc, rfprog[chan - 1].r2);
1172 (void)zyd_rfwrite(sc, 0x3c9000);
1173
1174 (void)zyd_write16(sc, ZYD_CR251, 0x3f);
1175 (void)zyd_write16(sc, ZYD_CR203, 0x06);
1176 (void)zyd_write16(sc, ZYD_CR240, 0x08);
1177
1178 return 0;
1179 }
1180
1181 /*
1182 * AL2210 RF methods.
1183 */
1184 Static int
1185 zyd_al2210_init(struct zyd_rf *rf)
1186 {
1187 struct zyd_softc *sc = rf->rf_sc;
1188 static const struct zyd_phy_pair phyini[] = ZYD_AL2210_PHY;
1189 static const uint32_t rfini[] = ZYD_AL2210_RF;
1190 uint32_t tmp;
1191 int error;
1192 size_t i;
1193
1194 (void)zyd_write32(sc, ZYD_CR18, 2);
1195
1196 /* init RF-dependent PHY registers */
1197 for (i = 0; i < __arraycount(phyini); i++) {
1198 error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1199 if (error != 0)
1200 return error;
1201 }
1202 /* init AL2210 radio */
1203 for (i = 0; i < __arraycount(rfini); i++) {
1204 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1205 return error;
1206 }
1207 (void)zyd_write16(sc, ZYD_CR47, 0x1e);
1208 (void)zyd_read32(sc, ZYD_CR_RADIO_PD, &tmp);
1209 (void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp & ~1);
1210 (void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp | 1);
1211 (void)zyd_write32(sc, ZYD_CR_RFCFG, 0x05);
1212 (void)zyd_write32(sc, ZYD_CR_RFCFG, 0x00);
1213 (void)zyd_write16(sc, ZYD_CR47, 0x1e);
1214 (void)zyd_write32(sc, ZYD_CR18, 3);
1215
1216 return 0;
1217 }
1218
1219 Static int
1220 zyd_al2210_switch_radio(struct zyd_rf *rf, int on)
1221 {
1222 /* vendor driver does nothing for this RF chip */
1223
1224 return 0;
1225 }
1226
1227 Static int
1228 zyd_al2210_set_channel(struct zyd_rf *rf, uint8_t chan)
1229 {
1230 struct zyd_softc *sc = rf->rf_sc;
1231 static const uint32_t rfprog[] = ZYD_AL2210_CHANTABLE;
1232 uint32_t tmp;
1233
1234 (void)zyd_write32(sc, ZYD_CR18, 2);
1235 (void)zyd_write16(sc, ZYD_CR47, 0x1e);
1236 (void)zyd_read32(sc, ZYD_CR_RADIO_PD, &tmp);
1237 (void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp & ~1);
1238 (void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp | 1);
1239 (void)zyd_write32(sc, ZYD_CR_RFCFG, 0x05);
1240
1241 (void)zyd_write32(sc, ZYD_CR_RFCFG, 0x00);
1242 (void)zyd_write16(sc, ZYD_CR47, 0x1e);
1243
1244 /* actually set the channel */
1245 (void)zyd_rfwrite(sc, rfprog[chan - 1]);
1246
1247 (void)zyd_write32(sc, ZYD_CR18, 3);
1248
1249 return 0;
1250 }
1251
1252 /*
1253 * GCT RF methods.
1254 */
1255 Static int
1256 zyd_gct_init(struct zyd_rf *rf)
1257 {
1258 struct zyd_softc *sc = rf->rf_sc;
1259 static const struct zyd_phy_pair phyini[] = ZYD_GCT_PHY;
1260 static const uint32_t rfini[] = ZYD_GCT_RF;
1261 int error;
1262 size_t i;
1263
1264 /* init RF-dependent PHY registers */
1265 for (i = 0; i < __arraycount(phyini); i++) {
1266 error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1267 if (error != 0)
1268 return error;
1269 }
1270 /* init cgt radio */
1271 for (i = 0; i < __arraycount(rfini); i++) {
1272 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1273 return error;
1274 }
1275 return 0;
1276 }
1277
1278 Static int
1279 zyd_gct_switch_radio(struct zyd_rf *rf, int on)
1280 {
1281 /* vendor driver does nothing for this RF chip */
1282
1283 return 0;
1284 }
1285
1286 Static int
1287 zyd_gct_set_channel(struct zyd_rf *rf, uint8_t chan)
1288 {
1289 struct zyd_softc *sc = rf->rf_sc;
1290 static const uint32_t rfprog[] = ZYD_GCT_CHANTABLE;
1291
1292 (void)zyd_rfwrite(sc, 0x1c0000);
1293 (void)zyd_rfwrite(sc, rfprog[chan - 1]);
1294 (void)zyd_rfwrite(sc, 0x1c0008);
1295
1296 return 0;
1297 }
1298
1299 /*
1300 * Maxim RF methods.
1301 */
1302 Static int
1303 zyd_maxim_init(struct zyd_rf *rf)
1304 {
1305 struct zyd_softc *sc = rf->rf_sc;
1306 static const struct zyd_phy_pair phyini[] = ZYD_MAXIM_PHY;
1307 static const uint32_t rfini[] = ZYD_MAXIM_RF;
1308 uint16_t tmp;
1309 int error;
1310 size_t i;
1311
1312 /* init RF-dependent PHY registers */
1313 for (i = 0; i < __arraycount(phyini); i++) {
1314 error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1315 if (error != 0)
1316 return error;
1317 }
1318 (void)zyd_read16(sc, ZYD_CR203, &tmp);
1319 (void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4));
1320
1321 /* init maxim radio */
1322 for (i = 0; i < __arraycount(rfini); i++) {
1323 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1324 return error;
1325 }
1326 (void)zyd_read16(sc, ZYD_CR203, &tmp);
1327 (void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4));
1328
1329 return 0;
1330 }
1331
1332 Static int
1333 zyd_maxim_switch_radio(struct zyd_rf *rf, int on)
1334 {
1335 /* vendor driver does nothing for this RF chip */
1336
1337 return 0;
1338 }
1339
1340 Static int
1341 zyd_maxim_set_channel(struct zyd_rf *rf, uint8_t chan)
1342 {
1343 struct zyd_softc *sc = rf->rf_sc;
1344 static const struct zyd_phy_pair phyini[] = ZYD_MAXIM_PHY;
1345 static const uint32_t rfini[] = ZYD_MAXIM_RF;
1346 static const struct {
1347 uint32_t r1, r2;
1348 } rfprog[] = ZYD_MAXIM_CHANTABLE;
1349 uint16_t tmp;
1350 int error;
1351 size_t i;
1352
1353 /*
1354 * Do the same as we do when initializing it, except for the channel
1355 * values coming from the two channel tables.
1356 */
1357
1358 /* init RF-dependent PHY registers */
1359 for (i = 0; i < __arraycount(phyini); i++) {
1360 error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1361 if (error != 0)
1362 return error;
1363 }
1364 (void)zyd_read16(sc, ZYD_CR203, &tmp);
1365 (void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4));
1366
1367 /* first two values taken from the chantables */
1368 (void)zyd_rfwrite(sc, rfprog[chan - 1].r1);
1369 (void)zyd_rfwrite(sc, rfprog[chan - 1].r2);
1370
1371 /* init maxim radio - skipping the two first values */
1372 for (i = 2; i < __arraycount(rfini); i++) {
1373 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1374 return error;
1375 }
1376 (void)zyd_read16(sc, ZYD_CR203, &tmp);
1377 (void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4));
1378
1379 return 0;
1380 }
1381
1382 /*
1383 * Maxim2 RF methods.
1384 */
1385 Static int
1386 zyd_maxim2_init(struct zyd_rf *rf)
1387 {
1388 struct zyd_softc *sc = rf->rf_sc;
1389 static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY;
1390 static const uint32_t rfini[] = ZYD_MAXIM2_RF;
1391 uint16_t tmp;
1392 int error;
1393 size_t i;
1394
1395 /* init RF-dependent PHY registers */
1396 for (i = 0; i < __arraycount(phyini); i++) {
1397 error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1398 if (error != 0)
1399 return error;
1400 }
1401 (void)zyd_read16(sc, ZYD_CR203, &tmp);
1402 (void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4));
1403
1404 /* init maxim2 radio */
1405 for (i = 0; i < __arraycount(rfini); i++) {
1406 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1407 return error;
1408 }
1409 (void)zyd_read16(sc, ZYD_CR203, &tmp);
1410 (void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4));
1411
1412 return 0;
1413 }
1414
1415 Static int
1416 zyd_maxim2_switch_radio(struct zyd_rf *rf, int on)
1417 {
1418 /* vendor driver does nothing for this RF chip */
1419
1420 return 0;
1421 }
1422
1423 Static int
1424 zyd_maxim2_set_channel(struct zyd_rf *rf, uint8_t chan)
1425 {
1426 struct zyd_softc *sc = rf->rf_sc;
1427 static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY;
1428 static const uint32_t rfini[] = ZYD_MAXIM2_RF;
1429 static const struct {
1430 uint32_t r1, r2;
1431 } rfprog[] = ZYD_MAXIM2_CHANTABLE;
1432 uint16_t tmp;
1433 int error;
1434 size_t i;
1435
1436 /*
1437 * Do the same as we do when initializing it, except for the channel
1438 * values coming from the two channel tables.
1439 */
1440
1441 /* init RF-dependent PHY registers */
1442 for (i = 0; i < __arraycount(phyini); i++) {
1443 error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1444 if (error != 0)
1445 return error;
1446 }
1447 (void)zyd_read16(sc, ZYD_CR203, &tmp);
1448 (void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4));
1449
1450 /* first two values taken from the chantables */
1451 (void)zyd_rfwrite(sc, rfprog[chan - 1].r1);
1452 (void)zyd_rfwrite(sc, rfprog[chan - 1].r2);
1453
1454 /* init maxim2 radio - skipping the two first values */
1455 for (i = 2; i < __arraycount(rfini); i++) {
1456 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1457 return error;
1458 }
1459 (void)zyd_read16(sc, ZYD_CR203, &tmp);
1460 (void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4));
1461
1462 return 0;
1463 }
1464
1465 Static int
1466 zyd_rf_attach(struct zyd_softc *sc, uint8_t type)
1467 {
1468 struct zyd_rf *rf = &sc->sc_rf;
1469
1470 rf->rf_sc = sc;
1471
1472 switch (type) {
1473 case ZYD_RF_RFMD:
1474 rf->init = zyd_rfmd_init;
1475 rf->switch_radio = zyd_rfmd_switch_radio;
1476 rf->set_channel = zyd_rfmd_set_channel;
1477 rf->width = 24; /* 24-bit RF values */
1478 break;
1479 case ZYD_RF_AL2230:
1480 case ZYD_RF_AL2230S:
1481 if (sc->mac_rev == ZYD_ZD1211B)
1482 rf->init = zyd_al2230_init_b;
1483 else
1484 rf->init = zyd_al2230_init;
1485 rf->switch_radio = zyd_al2230_switch_radio;
1486 rf->set_channel = zyd_al2230_set_channel;
1487 rf->width = 24; /* 24-bit RF values */
1488 break;
1489 case ZYD_RF_AL7230B:
1490 rf->init = zyd_al7230B_init;
1491 rf->switch_radio = zyd_al7230B_switch_radio;
1492 rf->set_channel = zyd_al7230B_set_channel;
1493 rf->width = 24; /* 24-bit RF values */
1494 break;
1495 case ZYD_RF_AL2210:
1496 rf->init = zyd_al2210_init;
1497 rf->switch_radio = zyd_al2210_switch_radio;
1498 rf->set_channel = zyd_al2210_set_channel;
1499 rf->width = 24; /* 24-bit RF values */
1500 break;
1501 case ZYD_RF_GCT:
1502 rf->init = zyd_gct_init;
1503 rf->switch_radio = zyd_gct_switch_radio;
1504 rf->set_channel = zyd_gct_set_channel;
1505 rf->width = 21; /* 21-bit RF values */
1506 break;
1507 case ZYD_RF_MAXIM_NEW:
1508 rf->init = zyd_maxim_init;
1509 rf->switch_radio = zyd_maxim_switch_radio;
1510 rf->set_channel = zyd_maxim_set_channel;
1511 rf->width = 18; /* 18-bit RF values */
1512 break;
1513 case ZYD_RF_MAXIM_NEW2:
1514 rf->init = zyd_maxim2_init;
1515 rf->switch_radio = zyd_maxim2_switch_radio;
1516 rf->set_channel = zyd_maxim2_set_channel;
1517 rf->width = 18; /* 18-bit RF values */
1518 break;
1519 default:
1520 printf("%s: sorry, radio \"%s\" is not supported yet\n",
1521 device_xname(sc->sc_dev), zyd_rf_name(type));
1522 return EINVAL;
1523 }
1524 return 0;
1525 }
1526
1527 Static const char *
1528 zyd_rf_name(uint8_t type)
1529 {
1530 static const char * const zyd_rfs[] = {
1531 "unknown", "unknown", "UW2451", "UCHIP", "AL2230",
1532 "AL7230B", "THETA", "AL2210", "MAXIM_NEW", "GCT",
1533 "AL2230S", "RALINK", "INTERSIL", "RFMD", "MAXIM_NEW2",
1534 "PHILIPS"
1535 };
1536
1537 return zyd_rfs[(type > 15) ? 0 : type];
1538 }
1539
1540 Static int
1541 zyd_hw_init(struct zyd_softc *sc)
1542 {
1543 struct zyd_rf *rf = &sc->sc_rf;
1544 const struct zyd_phy_pair *phyp;
1545 int error;
1546
1547 /* specify that the plug and play is finished */
1548 (void)zyd_write32(sc, ZYD_MAC_AFTER_PNP, 1);
1549
1550 (void)zyd_read16(sc, ZYD_FIRMWARE_BASE_ADDR, &sc->fwbase);
1551 DPRINTF(("firmware base address=0x%04x\n", sc->fwbase));
1552
1553 /* retrieve firmware revision number */
1554 (void)zyd_read16(sc, sc->fwbase + ZYD_FW_FIRMWARE_REV, &sc->fw_rev);
1555
1556 (void)zyd_write32(sc, ZYD_CR_GPI_EN, 0);
1557 (void)zyd_write32(sc, ZYD_MAC_CONT_WIN_LIMIT, 0x7f043f);
1558
1559 /* disable interrupts */
1560 (void)zyd_write32(sc, ZYD_CR_INTERRUPT, 0);
1561
1562 /* PHY init */
1563 zyd_lock_phy(sc);
1564 phyp = (sc->mac_rev == ZYD_ZD1211B) ? zyd_def_phyB : zyd_def_phy;
1565 for (; phyp->reg != 0; phyp++) {
1566 if ((error = zyd_write16(sc, phyp->reg, phyp->val)) != 0)
1567 goto fail;
1568 }
1569 zyd_unlock_phy(sc);
1570
1571 /* HMAC init */
1572 zyd_write32(sc, ZYD_MAC_ACK_EXT, 0x00000020);
1573 zyd_write32(sc, ZYD_CR_ADDA_MBIAS_WT, 0x30000808);
1574
1575 if (sc->mac_rev == ZYD_ZD1211) {
1576 zyd_write32(sc, ZYD_MAC_RETRY, 0x00000002);
1577 } else {
1578 zyd_write32(sc, ZYD_MAC_RETRY, 0x02020202);
1579 zyd_write32(sc, ZYD_MACB_TXPWR_CTL4, 0x007f003f);
1580 zyd_write32(sc, ZYD_MACB_TXPWR_CTL3, 0x007f003f);
1581 zyd_write32(sc, ZYD_MACB_TXPWR_CTL2, 0x003f001f);
1582 zyd_write32(sc, ZYD_MACB_TXPWR_CTL1, 0x001f000f);
1583 zyd_write32(sc, ZYD_MACB_AIFS_CTL1, 0x00280028);
1584 zyd_write32(sc, ZYD_MACB_AIFS_CTL2, 0x008C003C);
1585 zyd_write32(sc, ZYD_MACB_TXOP, 0x01800824);
1586 }
1587
1588 zyd_write32(sc, ZYD_MAC_SNIFFER, 0x00000000);
1589 zyd_write32(sc, ZYD_MAC_RXFILTER, 0x00000000);
1590 zyd_write32(sc, ZYD_MAC_GHTBL, 0x00000000);
1591 zyd_write32(sc, ZYD_MAC_GHTBH, 0x80000000);
1592 zyd_write32(sc, ZYD_MAC_MISC, 0x000000a4);
1593 zyd_write32(sc, ZYD_CR_ADDA_PWR_DWN, 0x0000007f);
1594 zyd_write32(sc, ZYD_MAC_BCNCFG, 0x00f00401);
1595 zyd_write32(sc, ZYD_MAC_PHY_DELAY2, 0x00000000);
1596 zyd_write32(sc, ZYD_MAC_ACK_EXT, 0x00000080);
1597 zyd_write32(sc, ZYD_CR_ADDA_PWR_DWN, 0x00000000);
1598 zyd_write32(sc, ZYD_MAC_SIFS_ACK_TIME, 0x00000100);
1599 zyd_write32(sc, ZYD_MAC_DIFS_EIFS_SIFS, 0x0547c032);
1600 zyd_write32(sc, ZYD_CR_RX_PE_DELAY, 0x00000070);
1601 zyd_write32(sc, ZYD_CR_PS_CTRL, 0x10000000);
1602 zyd_write32(sc, ZYD_MAC_RTSCTSRATE, 0x02030203);
1603 zyd_write32(sc, ZYD_MAC_RX_THRESHOLD, 0x000c0640);
1604 zyd_write32(sc, ZYD_MAC_BACKOFF_PROTECT, 0x00000114);
1605
1606 /* RF chip init */
1607 zyd_lock_phy(sc);
1608 error = (*rf->init)(rf);
1609 zyd_unlock_phy(sc);
1610 if (error != 0) {
1611 printf("%s: radio initialization failed\n",
1612 device_xname(sc->sc_dev));
1613 goto fail;
1614 }
1615
1616 /* init beacon interval to 100ms */
1617 if ((error = zyd_set_beacon_interval(sc, 100)) != 0)
1618 goto fail;
1619
1620 fail: return error;
1621 }
1622
1623 Static int
1624 zyd_read_eeprom(struct zyd_softc *sc)
1625 {
1626 struct ieee80211com *ic = &sc->sc_ic;
1627 uint32_t tmp;
1628 uint16_t val;
1629 int i;
1630
1631 /* read MAC address */
1632 (void)zyd_read32(sc, ZYD_EEPROM_MAC_ADDR_P1, &tmp);
1633 ic->ic_myaddr[0] = tmp & 0xff;
1634 ic->ic_myaddr[1] = tmp >> 8;
1635 ic->ic_myaddr[2] = tmp >> 16;
1636 ic->ic_myaddr[3] = tmp >> 24;
1637 (void)zyd_read32(sc, ZYD_EEPROM_MAC_ADDR_P2, &tmp);
1638 ic->ic_myaddr[4] = tmp & 0xff;
1639 ic->ic_myaddr[5] = tmp >> 8;
1640
1641 (void)zyd_read32(sc, ZYD_EEPROM_POD, &tmp);
1642 sc->rf_rev = tmp & 0x0f;
1643 sc->pa_rev = (tmp >> 16) & 0x0f;
1644
1645 /* read regulatory domain (currently unused) */
1646 (void)zyd_read32(sc, ZYD_EEPROM_SUBID, &tmp);
1647 sc->regdomain = tmp >> 16;
1648 DPRINTF(("regulatory domain %x\n", sc->regdomain));
1649
1650 /* read Tx power calibration tables */
1651 for (i = 0; i < 7; i++) {
1652 (void)zyd_read16(sc, ZYD_EEPROM_PWR_CAL + i, &val);
1653 sc->pwr_cal[i * 2] = val >> 8;
1654 sc->pwr_cal[i * 2 + 1] = val & 0xff;
1655
1656 (void)zyd_read16(sc, ZYD_EEPROM_PWR_INT + i, &val);
1657 sc->pwr_int[i * 2] = val >> 8;
1658 sc->pwr_int[i * 2 + 1] = val & 0xff;
1659
1660 (void)zyd_read16(sc, ZYD_EEPROM_36M_CAL + i, &val);
1661 sc->ofdm36_cal[i * 2] = val >> 8;
1662 sc->ofdm36_cal[i * 2 + 1] = val & 0xff;
1663
1664 (void)zyd_read16(sc, ZYD_EEPROM_48M_CAL + i, &val);
1665 sc->ofdm48_cal[i * 2] = val >> 8;
1666 sc->ofdm48_cal[i * 2 + 1] = val & 0xff;
1667
1668 (void)zyd_read16(sc, ZYD_EEPROM_54M_CAL + i, &val);
1669 sc->ofdm54_cal[i * 2] = val >> 8;
1670 sc->ofdm54_cal[i * 2 + 1] = val & 0xff;
1671 }
1672 return 0;
1673 }
1674
1675 Static int
1676 zyd_set_macaddr(struct zyd_softc *sc, const uint8_t *addr)
1677 {
1678 uint32_t tmp;
1679
1680 tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
1681 (void)zyd_write32(sc, ZYD_MAC_MACADRL, tmp);
1682
1683 tmp = addr[5] << 8 | addr[4];
1684 (void)zyd_write32(sc, ZYD_MAC_MACADRH, tmp);
1685
1686 return 0;
1687 }
1688
1689 Static int
1690 zyd_set_bssid(struct zyd_softc *sc, const uint8_t *addr)
1691 {
1692 uint32_t tmp;
1693
1694 tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
1695 (void)zyd_write32(sc, ZYD_MAC_BSSADRL, tmp);
1696
1697 tmp = addr[5] << 8 | addr[4];
1698 (void)zyd_write32(sc, ZYD_MAC_BSSADRH, tmp);
1699
1700 return 0;
1701 }
1702
1703 Static int
1704 zyd_switch_radio(struct zyd_softc *sc, int on)
1705 {
1706 struct zyd_rf *rf = &sc->sc_rf;
1707 int error;
1708
1709 zyd_lock_phy(sc);
1710 error = (*rf->switch_radio)(rf, on);
1711 zyd_unlock_phy(sc);
1712
1713 return error;
1714 }
1715
1716 Static void
1717 zyd_set_led(struct zyd_softc *sc, int which, int on)
1718 {
1719 uint32_t tmp;
1720
1721 (void)zyd_read32(sc, ZYD_MAC_TX_PE_CONTROL, &tmp);
1722 tmp &= ~which;
1723 if (on)
1724 tmp |= which;
1725 (void)zyd_write32(sc, ZYD_MAC_TX_PE_CONTROL, tmp);
1726 }
1727
1728 Static int
1729 zyd_set_rxfilter(struct zyd_softc *sc)
1730 {
1731 uint32_t rxfilter;
1732
1733 switch (sc->sc_ic.ic_opmode) {
1734 case IEEE80211_M_STA:
1735 rxfilter = ZYD_FILTER_BSS;
1736 break;
1737 case IEEE80211_M_IBSS:
1738 case IEEE80211_M_HOSTAP:
1739 rxfilter = ZYD_FILTER_HOSTAP;
1740 break;
1741 case IEEE80211_M_MONITOR:
1742 rxfilter = ZYD_FILTER_MONITOR;
1743 break;
1744 default:
1745 /* should not get there */
1746 return EINVAL;
1747 }
1748 return zyd_write32(sc, ZYD_MAC_RXFILTER, rxfilter);
1749 }
1750
1751 Static void
1752 zyd_set_chan(struct zyd_softc *sc, struct ieee80211_channel *c)
1753 {
1754 struct ieee80211com *ic = &sc->sc_ic;
1755 struct zyd_rf *rf = &sc->sc_rf;
1756 u_int chan;
1757
1758 chan = ieee80211_chan2ieee(ic, c);
1759 if (chan == 0 || chan == IEEE80211_CHAN_ANY)
1760 return;
1761
1762 zyd_lock_phy(sc);
1763
1764 (*rf->set_channel)(rf, chan);
1765
1766 /* update Tx power */
1767 (void)zyd_write32(sc, ZYD_CR31, sc->pwr_int[chan - 1]);
1768 (void)zyd_write32(sc, ZYD_CR68, sc->pwr_cal[chan - 1]);
1769
1770 if (sc->mac_rev == ZYD_ZD1211B) {
1771 (void)zyd_write32(sc, ZYD_CR67, sc->ofdm36_cal[chan - 1]);
1772 (void)zyd_write32(sc, ZYD_CR66, sc->ofdm48_cal[chan - 1]);
1773 (void)zyd_write32(sc, ZYD_CR65, sc->ofdm54_cal[chan - 1]);
1774
1775 (void)zyd_write32(sc, ZYD_CR69, 0x28);
1776 (void)zyd_write32(sc, ZYD_CR69, 0x2a);
1777 }
1778
1779 zyd_unlock_phy(sc);
1780 }
1781
1782 Static int
1783 zyd_set_beacon_interval(struct zyd_softc *sc, int bintval)
1784 {
1785 /* XXX this is probably broken.. */
1786 (void)zyd_write32(sc, ZYD_CR_ATIM_WND_PERIOD, bintval - 2);
1787 (void)zyd_write32(sc, ZYD_CR_PRE_TBTT, bintval - 1);
1788 (void)zyd_write32(sc, ZYD_CR_BCN_INTERVAL, bintval);
1789
1790 return 0;
1791 }
1792
1793 Static uint8_t
1794 zyd_plcp_signal(int rate)
1795 {
1796 switch (rate) {
1797 /* CCK rates (returned values are device-dependent) */
1798 case 2: return 0x0;
1799 case 4: return 0x1;
1800 case 11: return 0x2;
1801 case 22: return 0x3;
1802
1803 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1804 case 12: return 0xb;
1805 case 18: return 0xf;
1806 case 24: return 0xa;
1807 case 36: return 0xe;
1808 case 48: return 0x9;
1809 case 72: return 0xd;
1810 case 96: return 0x8;
1811 case 108: return 0xc;
1812
1813 /* unsupported rates (should not get there) */
1814 default: return 0xff;
1815 }
1816 }
1817
1818 Static void
1819 zyd_intr(struct usbd_xfer *xfer, void * priv, usbd_status status)
1820 {
1821 struct zyd_softc *sc = (struct zyd_softc *)priv;
1822 struct zyd_cmd *cmd;
1823 uint32_t datalen;
1824
1825 if (status != USBD_NORMAL_COMPLETION) {
1826 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1827 return;
1828
1829 if (status == USBD_STALLED) {
1830 usbd_clear_endpoint_stall_async(
1831 sc->zyd_ep[ZYD_ENDPT_IIN]);
1832 }
1833 return;
1834 }
1835
1836 cmd = (struct zyd_cmd *)sc->ibuf;
1837
1838 if (le16toh(cmd->code) == ZYD_NOTIF_RETRYSTATUS) {
1839 struct zyd_notif_retry *retry =
1840 (struct zyd_notif_retry *)cmd->data;
1841 struct ieee80211com *ic = &sc->sc_ic;
1842 struct ifnet *ifp = &sc->sc_if;
1843 struct ieee80211_node *ni;
1844
1845 DPRINTF(("retry intr: rate=%#x addr=%s count=%d (%#x)\n",
1846 le16toh(retry->rate), ether_sprintf(retry->macaddr),
1847 le16toh(retry->count) & 0xff, le16toh(retry->count)));
1848
1849 /*
1850 * Find the node to which the packet was sent and update its
1851 * retry statistics. In BSS mode, this node is the AP we're
1852 * associated to so no lookup is actually needed.
1853 */
1854 if (ic->ic_opmode != IEEE80211_M_STA) {
1855 ni = ieee80211_find_node(&ic->ic_scan, retry->macaddr);
1856 if (ni == NULL)
1857 return; /* just ignore */
1858 } else
1859 ni = ic->ic_bss;
1860
1861 ((struct zyd_node *)ni)->amn.amn_retrycnt++;
1862
1863 if (le16toh(retry->count) & 0x100)
1864 if_statinc(ifp, if_oerrors);
1865
1866 } else if (le16toh(cmd->code) == ZYD_NOTIF_IORD) {
1867 struct rq *rqp;
1868
1869 if (le16toh(*(uint16_t *)cmd->data) == ZYD_CR_INTERRUPT)
1870 return; /* HMAC interrupt */
1871
1872 usbd_get_xfer_status(xfer, NULL, NULL, &datalen, NULL);
1873 datalen -= sizeof(cmd->code);
1874 datalen -= 2; /* XXX: padding? */
1875
1876 mutex_enter(&sc->sc_lock);
1877 SIMPLEQ_FOREACH(rqp, &sc->sc_rqh, rq) {
1878 int i;
1879
1880 if (sizeof(struct zyd_pair) * rqp->len != datalen)
1881 continue;
1882 for (i = 0; i < rqp->len; i++) {
1883 if (*(((const uint16_t *)rqp->idata) + i) !=
1884 (((struct zyd_pair *)cmd->data) + i)->reg)
1885 break;
1886 }
1887 if (i != rqp->len)
1888 continue;
1889
1890 /* copy answer into caller-supplied buffer */
1891 memcpy(rqp->odata, cmd->data,
1892 sizeof(struct zyd_pair) * rqp->len);
1893 cv_signal(&sc->sc_cmdcv);
1894 mutex_exit(&sc->sc_lock);
1895 return;
1896 }
1897 mutex_exit(&sc->sc_lock);
1898 return; /* unexpected IORD notification */
1899 } else {
1900 printf("%s: unknown notification %x\n", device_xname(sc->sc_dev),
1901 le16toh(cmd->code));
1902 }
1903 }
1904
1905 Static void
1906 zyd_rx_data(struct zyd_softc *sc, const uint8_t *buf, uint16_t len)
1907 {
1908 struct ieee80211com *ic = &sc->sc_ic;
1909 struct ifnet *ifp = &sc->sc_if;
1910 struct ieee80211_node *ni;
1911 struct ieee80211_frame *wh;
1912 const struct zyd_plcphdr *plcp;
1913 const struct zyd_rx_stat *stat;
1914 struct mbuf *m;
1915 int rlen, s;
1916
1917 if (len < ZYD_MIN_FRAGSZ) {
1918 printf("%s: frame too short (length=%d)\n",
1919 device_xname(sc->sc_dev), len);
1920 if_statinc(ifp, if_ierrors);
1921 return;
1922 }
1923
1924 plcp = (const struct zyd_plcphdr *)buf;
1925 stat = (const struct zyd_rx_stat *)
1926 (buf + len - sizeof(struct zyd_rx_stat));
1927
1928 if (stat->flags & ZYD_RX_ERROR) {
1929 DPRINTF(("%s: RX status indicated error (%x)\n",
1930 device_xname(sc->sc_dev), stat->flags));
1931 if_statinc(ifp, if_ierrors);
1932 return;
1933 }
1934
1935 /* compute actual frame length */
1936 rlen = len - sizeof(struct zyd_plcphdr) -
1937 sizeof(struct zyd_rx_stat) - IEEE80211_CRC_LEN;
1938
1939 /* allocate a mbuf to store the frame */
1940 MGETHDR(m, M_DONTWAIT, MT_DATA);
1941 if (m == NULL) {
1942 printf("%s: could not allocate rx mbuf\n",
1943 device_xname(sc->sc_dev));
1944 if_statinc(ifp, if_ierrors);
1945 return;
1946 }
1947 if (rlen > MHLEN) {
1948 MCLGET(m, M_DONTWAIT);
1949 if (!(m->m_flags & M_EXT)) {
1950 printf("%s: could not allocate rx mbuf cluster\n",
1951 device_xname(sc->sc_dev));
1952 m_freem(m);
1953 if_statinc(ifp, if_ierrors);
1954 return;
1955 }
1956 }
1957 m_set_rcvif(m, ifp);
1958 m->m_pkthdr.len = m->m_len = rlen;
1959 memcpy(mtod(m, uint8_t *), (const uint8_t *)(plcp + 1), rlen);
1960
1961 s = splnet();
1962
1963 if (sc->sc_drvbpf != NULL) {
1964 struct zyd_rx_radiotap_header *tap = &sc->sc_rxtap;
1965 static const uint8_t rates[] = {
1966 /* reverse function of zyd_plcp_signal() */
1967 2, 4, 11, 22, 0, 0, 0, 0,
1968 96, 48, 24, 12, 108, 72, 36, 18
1969 };
1970
1971 tap->wr_flags = IEEE80211_RADIOTAP_F_FCS;
1972 tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
1973 tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
1974 tap->wr_rssi = stat->rssi;
1975 tap->wr_rate = rates[plcp->signal & 0xf];
1976
1977 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m, BPF_D_IN);
1978 }
1979
1980 wh = mtod(m, struct ieee80211_frame *);
1981 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
1982 ieee80211_input(ic, m, ni, stat->rssi, 0);
1983
1984 /* node is no longer needed */
1985 ieee80211_free_node(ni);
1986
1987 splx(s);
1988 }
1989
1990 Static void
1991 zyd_rxeof(struct usbd_xfer *xfer, void * priv, usbd_status status)
1992 {
1993 struct zyd_rx_data *data = priv;
1994 struct zyd_softc *sc = data->sc;
1995 struct ifnet *ifp = &sc->sc_if;
1996 const struct zyd_rx_desc *desc;
1997 int len;
1998
1999 if (status != USBD_NORMAL_COMPLETION) {
2000 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
2001 return;
2002
2003 if (status == USBD_STALLED)
2004 usbd_clear_endpoint_stall(sc->zyd_ep[ZYD_ENDPT_BIN]);
2005
2006 goto skip;
2007 }
2008 usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
2009
2010 if (len < ZYD_MIN_RXBUFSZ) {
2011 printf("%s: xfer too short (length=%d)\n",
2012 device_xname(sc->sc_dev), len);
2013 if_statinc(ifp, if_ierrors);
2014 goto skip;
2015 }
2016
2017 desc = (const struct zyd_rx_desc *)
2018 (data->buf + len - sizeof(struct zyd_rx_desc));
2019
2020 if (UGETW(desc->tag) == ZYD_TAG_MULTIFRAME) {
2021 const uint8_t *p = data->buf, *end = p + len;
2022 int i;
2023
2024 DPRINTFN(3, ("received multi-frame transfer\n"));
2025
2026 for (i = 0; i < ZYD_MAX_RXFRAMECNT; i++) {
2027 const uint16_t len16 = UGETW(desc->len[i]);
2028
2029 if (len16 == 0 || p + len16 > end)
2030 break;
2031
2032 zyd_rx_data(sc, p, len16);
2033 /* next frame is aligned on a 32-bit boundary */
2034 p += (len16 + 3) & ~3;
2035 }
2036 } else {
2037 DPRINTFN(3, ("received single-frame transfer\n"));
2038
2039 zyd_rx_data(sc, data->buf, len);
2040 }
2041
2042 skip: /* setup a new transfer */
2043
2044 usbd_setup_xfer(xfer, data, NULL, ZYX_MAX_RXBUFSZ, USBD_SHORT_XFER_OK,
2045 USBD_NO_TIMEOUT, zyd_rxeof);
2046 (void)usbd_transfer(xfer);
2047 }
2048
2049 Static int
2050 zyd_tx_mgt(struct zyd_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
2051 {
2052 struct ieee80211com *ic = &sc->sc_ic;
2053 struct ifnet *ifp = &sc->sc_if;
2054 struct zyd_tx_desc *desc;
2055 struct zyd_tx_data *data;
2056 struct ieee80211_frame *wh;
2057 struct ieee80211_key *k;
2058 int xferlen, totlen, rate;
2059 uint16_t pktlen;
2060 usbd_status error;
2061
2062 data = &sc->tx_data[0];
2063 desc = (struct zyd_tx_desc *)data->buf;
2064
2065 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
2066
2067 wh = mtod(m0, struct ieee80211_frame *);
2068
2069 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
2070 k = ieee80211_crypto_encap(ic, ni, m0);
2071 if (k == NULL) {
2072 m_freem(m0);
2073 return ENOBUFS;
2074 }
2075 }
2076
2077 data->ni = ni;
2078
2079 wh = mtod(m0, struct ieee80211_frame *);
2080
2081 xferlen = sizeof(struct zyd_tx_desc) + m0->m_pkthdr.len;
2082 totlen = m0->m_pkthdr.len + IEEE80211_CRC_LEN;
2083
2084 /* fill Tx descriptor */
2085 desc->len = htole16(totlen);
2086
2087 desc->flags = ZYD_TX_FLAG_BACKOFF;
2088 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2089 /* multicast frames are not sent at OFDM rates in 802.11b/g */
2090 if (totlen > ic->ic_rtsthreshold) {
2091 desc->flags |= ZYD_TX_FLAG_RTS;
2092 } else if (ZYD_RATE_IS_OFDM(rate) &&
2093 (ic->ic_flags & IEEE80211_F_USEPROT)) {
2094 if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
2095 desc->flags |= ZYD_TX_FLAG_CTS_TO_SELF;
2096 else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
2097 desc->flags |= ZYD_TX_FLAG_RTS;
2098 }
2099 } else
2100 desc->flags |= ZYD_TX_FLAG_MULTICAST;
2101
2102 if ((wh->i_fc[0] &
2103 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
2104 (IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_PS_POLL))
2105 desc->flags |= ZYD_TX_FLAG_TYPE(ZYD_TX_TYPE_PS_POLL);
2106
2107 desc->phy = zyd_plcp_signal(rate);
2108 if (ZYD_RATE_IS_OFDM(rate)) {
2109 desc->phy |= ZYD_TX_PHY_OFDM;
2110 if (ic->ic_curmode == IEEE80211_MODE_11A)
2111 desc->phy |= ZYD_TX_PHY_5GHZ;
2112 } else if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
2113 desc->phy |= ZYD_TX_PHY_SHPREAMBLE;
2114
2115 /* actual transmit length (XXX why +10?) */
2116 pktlen = sizeof(struct zyd_tx_desc) + 10;
2117 if (sc->mac_rev == ZYD_ZD1211)
2118 pktlen += totlen;
2119 desc->pktlen = htole16(pktlen);
2120
2121 desc->plcp_length = (16 * totlen + rate - 1) / rate;
2122 desc->plcp_service = 0;
2123 if (rate == 22) {
2124 const int remainder = (16 * totlen) % 22;
2125 if (remainder != 0 && remainder < 7)
2126 desc->plcp_service |= ZYD_PLCP_LENGEXT;
2127 }
2128
2129 if (sc->sc_drvbpf != NULL) {
2130 struct zyd_tx_radiotap_header *tap = &sc->sc_txtap;
2131
2132 tap->wt_flags = 0;
2133 tap->wt_rate = rate;
2134 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
2135 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
2136
2137 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0, BPF_D_OUT);
2138 }
2139
2140 m_copydata(m0, 0, m0->m_pkthdr.len,
2141 data->buf + sizeof(struct zyd_tx_desc));
2142
2143 DPRINTFN(10, ("%s: sending mgt frame len=%zu rate=%u xferlen=%u\n",
2144 device_xname(sc->sc_dev), (size_t)m0->m_pkthdr.len, rate, xferlen));
2145
2146 m_freem(m0); /* mbuf no longer needed */
2147
2148 usbd_setup_xfer(data->xfer, data, data->buf, xferlen,
2149 USBD_FORCE_SHORT_XFER, ZYD_TX_TIMEOUT, zyd_txeof);
2150 error = usbd_transfer(data->xfer);
2151 if (error != USBD_IN_PROGRESS && error != 0) {
2152 if_statinc(ifp, if_oerrors);
2153 return EIO;
2154 }
2155 sc->tx_queued++;
2156
2157 return 0;
2158 }
2159
2160 Static void
2161 zyd_txeof(struct usbd_xfer *xfer, void * priv, usbd_status status)
2162 {
2163 struct zyd_tx_data *data = priv;
2164 struct zyd_softc *sc = data->sc;
2165 struct ifnet *ifp = &sc->sc_if;
2166 int s;
2167
2168 if (status != USBD_NORMAL_COMPLETION) {
2169 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
2170 return;
2171
2172 printf("%s: could not transmit buffer: %s\n",
2173 device_xname(sc->sc_dev), usbd_errstr(status));
2174
2175 if (status == USBD_STALLED) {
2176 usbd_clear_endpoint_stall_async(
2177 sc->zyd_ep[ZYD_ENDPT_BOUT]);
2178 }
2179 if_statinc(ifp, if_oerrors);
2180 return;
2181 }
2182
2183 s = splnet();
2184
2185 /* update rate control statistics */
2186 ((struct zyd_node *)data->ni)->amn.amn_txcnt++;
2187
2188 ieee80211_free_node(data->ni);
2189 data->ni = NULL;
2190
2191 sc->tx_queued--;
2192 if_statinc(ifp, if_opackets);
2193
2194 sc->tx_timer = 0;
2195 ifp->if_flags &= ~IFF_OACTIVE;
2196 zyd_start(ifp);
2197
2198 splx(s);
2199 }
2200
2201 Static int
2202 zyd_tx_data(struct zyd_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
2203 {
2204 struct ieee80211com *ic = &sc->sc_ic;
2205 struct ifnet *ifp = &sc->sc_if;
2206 struct zyd_tx_desc *desc;
2207 struct zyd_tx_data *data;
2208 struct ieee80211_frame *wh;
2209 struct ieee80211_key *k;
2210 int xferlen, totlen, rate;
2211 uint16_t pktlen;
2212 usbd_status error;
2213
2214 wh = mtod(m0, struct ieee80211_frame *);
2215
2216 if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE)
2217 rate = ic->ic_bss->ni_rates.rs_rates[ic->ic_fixed_rate];
2218 else
2219 rate = ni->ni_rates.rs_rates[ni->ni_txrate];
2220 rate &= IEEE80211_RATE_VAL;
2221
2222 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
2223 k = ieee80211_crypto_encap(ic, ni, m0);
2224 if (k == NULL) {
2225 m_freem(m0);
2226 return ENOBUFS;
2227 }
2228
2229 /* packet header may have moved, reset our local pointer */
2230 wh = mtod(m0, struct ieee80211_frame *);
2231 }
2232
2233 data = &sc->tx_data[0];
2234 desc = (struct zyd_tx_desc *)data->buf;
2235
2236 data->ni = ni;
2237
2238 xferlen = sizeof(struct zyd_tx_desc) + m0->m_pkthdr.len;
2239 totlen = m0->m_pkthdr.len + IEEE80211_CRC_LEN;
2240
2241 /* fill Tx descriptor */
2242 desc->len = htole16(totlen);
2243
2244 desc->flags = ZYD_TX_FLAG_BACKOFF;
2245 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2246 /* multicast frames are not sent at OFDM rates in 802.11b/g */
2247 if (totlen > ic->ic_rtsthreshold) {
2248 desc->flags |= ZYD_TX_FLAG_RTS;
2249 } else if (ZYD_RATE_IS_OFDM(rate) &&
2250 (ic->ic_flags & IEEE80211_F_USEPROT)) {
2251 if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
2252 desc->flags |= ZYD_TX_FLAG_CTS_TO_SELF;
2253 else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
2254 desc->flags |= ZYD_TX_FLAG_RTS;
2255 }
2256 } else
2257 desc->flags |= ZYD_TX_FLAG_MULTICAST;
2258
2259 if ((wh->i_fc[0] &
2260 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
2261 (IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_PS_POLL))
2262 desc->flags |= ZYD_TX_FLAG_TYPE(ZYD_TX_TYPE_PS_POLL);
2263
2264 desc->phy = zyd_plcp_signal(rate);
2265 if (ZYD_RATE_IS_OFDM(rate)) {
2266 desc->phy |= ZYD_TX_PHY_OFDM;
2267 if (ic->ic_curmode == IEEE80211_MODE_11A)
2268 desc->phy |= ZYD_TX_PHY_5GHZ;
2269 } else if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
2270 desc->phy |= ZYD_TX_PHY_SHPREAMBLE;
2271
2272 /* actual transmit length (XXX why +10?) */
2273 pktlen = sizeof(struct zyd_tx_desc) + 10;
2274 if (sc->mac_rev == ZYD_ZD1211)
2275 pktlen += totlen;
2276 desc->pktlen = htole16(pktlen);
2277
2278 desc->plcp_length = (16 * totlen + rate - 1) / rate;
2279 desc->plcp_service = 0;
2280 if (rate == 22) {
2281 const int remainder = (16 * totlen) % 22;
2282 if (remainder != 0 && remainder < 7)
2283 desc->plcp_service |= ZYD_PLCP_LENGEXT;
2284 }
2285
2286 if (sc->sc_drvbpf != NULL) {
2287 struct zyd_tx_radiotap_header *tap = &sc->sc_txtap;
2288
2289 tap->wt_flags = 0;
2290 tap->wt_rate = rate;
2291 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
2292 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
2293
2294 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0, BPF_D_OUT);
2295 }
2296
2297 m_copydata(m0, 0, m0->m_pkthdr.len,
2298 data->buf + sizeof(struct zyd_tx_desc));
2299
2300 DPRINTFN(10, ("%s: sending data frame len=%zu rate=%u xferlen=%u\n",
2301 device_xname(sc->sc_dev), (size_t)m0->m_pkthdr.len, rate, xferlen));
2302
2303 m_freem(m0); /* mbuf no longer needed */
2304
2305 usbd_setup_xfer(data->xfer, data, data->buf, xferlen,
2306 USBD_FORCE_SHORT_XFER, ZYD_TX_TIMEOUT, zyd_txeof);
2307 error = usbd_transfer(data->xfer);
2308 if (error != USBD_IN_PROGRESS && error != 0) {
2309 if_statinc(ifp, if_oerrors);
2310 return EIO;
2311 }
2312 sc->tx_queued++;
2313
2314 return 0;
2315 }
2316
2317 Static void
2318 zyd_start(struct ifnet *ifp)
2319 {
2320 struct zyd_softc *sc = ifp->if_softc;
2321 struct ieee80211com *ic = &sc->sc_ic;
2322 struct ether_header *eh;
2323 struct ieee80211_node *ni;
2324 struct mbuf *m0;
2325
2326 for (;;) {
2327 IF_POLL(&ic->ic_mgtq, m0);
2328 if (m0 != NULL) {
2329 if (sc->tx_queued >= ZYD_TX_LIST_CNT) {
2330 ifp->if_flags |= IFF_OACTIVE;
2331 break;
2332 }
2333 IF_DEQUEUE(&ic->ic_mgtq, m0);
2334
2335 ni = M_GETCTX(m0, struct ieee80211_node *);
2336 M_CLEARCTX(m0);
2337 bpf_mtap3(ic->ic_rawbpf, m0, BPF_D_OUT);
2338 if (zyd_tx_mgt(sc, m0, ni) != 0)
2339 break;
2340 } else {
2341 if (ic->ic_state != IEEE80211_S_RUN)
2342 break;
2343 IFQ_POLL(&ifp->if_snd, m0);
2344 if (m0 == NULL)
2345 break;
2346 if (sc->tx_queued >= ZYD_TX_LIST_CNT) {
2347 ifp->if_flags |= IFF_OACTIVE;
2348 break;
2349 }
2350 IFQ_DEQUEUE(&ifp->if_snd, m0);
2351
2352 if (m0->m_len < sizeof(struct ether_header) &&
2353 !(m0 = m_pullup(m0, sizeof(struct ether_header))))
2354 continue;
2355
2356 eh = mtod(m0, struct ether_header *);
2357 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
2358 if (ni == NULL) {
2359 m_freem(m0);
2360 continue;
2361 }
2362 bpf_mtap(ifp, m0, BPF_D_OUT);
2363 if ((m0 = ieee80211_encap(ic, m0, ni)) == NULL) {
2364 ieee80211_free_node(ni);
2365 if_statinc(ifp, if_oerrors);
2366 continue;
2367 }
2368 bpf_mtap3(ic->ic_rawbpf, m0, BPF_D_OUT);
2369 if (zyd_tx_data(sc, m0, ni) != 0) {
2370 ieee80211_free_node(ni);
2371 if_statinc(ifp, if_oerrors);
2372 break;
2373 }
2374 }
2375
2376 sc->tx_timer = 5;
2377 ifp->if_timer = 1;
2378 }
2379 }
2380
2381 Static void
2382 zyd_watchdog(struct ifnet *ifp)
2383 {
2384 struct zyd_softc *sc = ifp->if_softc;
2385 struct ieee80211com *ic = &sc->sc_ic;
2386
2387 ifp->if_timer = 0;
2388
2389 if (sc->tx_timer > 0) {
2390 if (--sc->tx_timer == 0) {
2391 printf("%s: device timeout\n", device_xname(sc->sc_dev));
2392 /* zyd_init(ifp); XXX needs a process context ? */
2393 if_statinc(ifp, if_oerrors);
2394 return;
2395 }
2396 ifp->if_timer = 1;
2397 }
2398
2399 ieee80211_watchdog(ic);
2400 }
2401
2402 Static int
2403 zyd_ioctl(struct ifnet *ifp, u_long cmd, void *data)
2404 {
2405 struct zyd_softc *sc = ifp->if_softc;
2406 struct ieee80211com *ic = &sc->sc_ic;
2407 int s, error = 0;
2408
2409 s = splnet();
2410
2411 switch (cmd) {
2412 case SIOCSIFFLAGS:
2413 if ((error = ifioctl_common(ifp, cmd, data)) != 0)
2414 break;
2415 /* XXX re-use ether_ioctl() */
2416 switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
2417 case IFF_UP:
2418 zyd_init(ifp);
2419 break;
2420 case IFF_RUNNING:
2421 zyd_stop(ifp, 1);
2422 break;
2423 default:
2424 break;
2425 }
2426 break;
2427
2428 default:
2429 error = ieee80211_ioctl(ic, cmd, data);
2430 }
2431
2432 if (error == ENETRESET) {
2433 if ((ifp->if_flags & (IFF_RUNNING | IFF_UP)) ==
2434 (IFF_RUNNING | IFF_UP))
2435 zyd_init(ifp);
2436 error = 0;
2437 }
2438
2439 splx(s);
2440
2441 return error;
2442 }
2443
2444 Static int
2445 zyd_init(struct ifnet *ifp)
2446 {
2447 struct zyd_softc *sc = ifp->if_softc;
2448 struct ieee80211com *ic = &sc->sc_ic;
2449 int i, error;
2450
2451 zyd_stop(ifp, 0);
2452
2453 IEEE80211_ADDR_COPY(ic->ic_myaddr, CLLADDR(ifp->if_sadl));
2454 DPRINTF(("setting MAC address to %s\n", ether_sprintf(ic->ic_myaddr)));
2455 error = zyd_set_macaddr(sc, ic->ic_myaddr);
2456 if (error != 0)
2457 return error;
2458
2459 /* we'll do software WEP decryption for now */
2460 DPRINTF(("setting encryption type\n"));
2461 error = zyd_write32(sc, ZYD_MAC_ENCRYPTION_TYPE, ZYD_ENC_SNIFFER);
2462 if (error != 0)
2463 return error;
2464
2465 /* promiscuous mode */
2466 (void)zyd_write32(sc, ZYD_MAC_SNIFFER,
2467 (ic->ic_opmode == IEEE80211_M_MONITOR) ? 1 : 0);
2468
2469 (void)zyd_set_rxfilter(sc);
2470
2471 /* switch radio transmitter ON */
2472 (void)zyd_switch_radio(sc, 1);
2473
2474 /* set basic rates */
2475 if (ic->ic_curmode == IEEE80211_MODE_11B)
2476 (void)zyd_write32(sc, ZYD_MAC_BAS_RATE, 0x0003);
2477 else if (ic->ic_curmode == IEEE80211_MODE_11A)
2478 (void)zyd_write32(sc, ZYD_MAC_BAS_RATE, 0x1500);
2479 else /* assumes 802.11b/g */
2480 (void)zyd_write32(sc, ZYD_MAC_BAS_RATE, 0x000f);
2481
2482 /* set mandatory rates */
2483 if (ic->ic_curmode == IEEE80211_MODE_11B)
2484 (void)zyd_write32(sc, ZYD_MAC_MAN_RATE, 0x000f);
2485 else if (ic->ic_curmode == IEEE80211_MODE_11A)
2486 (void)zyd_write32(sc, ZYD_MAC_MAN_RATE, 0x1500);
2487 else /* assumes 802.11b/g */
2488 (void)zyd_write32(sc, ZYD_MAC_MAN_RATE, 0x150f);
2489
2490 /* set default BSS channel */
2491 ic->ic_bss->ni_chan = ic->ic_ibss_chan;
2492 zyd_set_chan(sc, ic->ic_bss->ni_chan);
2493
2494 /* enable interrupts */
2495 (void)zyd_write32(sc, ZYD_CR_INTERRUPT, ZYD_HWINT_MASK);
2496
2497 /*
2498 * Allocate Tx and Rx xfer queues.
2499 */
2500 if ((error = zyd_alloc_tx_list(sc)) != 0) {
2501 printf("%s: could not allocate Tx list\n",
2502 device_xname(sc->sc_dev));
2503 goto fail;
2504 }
2505 if ((error = zyd_alloc_rx_list(sc)) != 0) {
2506 printf("%s: could not allocate Rx list\n",
2507 device_xname(sc->sc_dev));
2508 goto fail;
2509 }
2510
2511 /*
2512 * Start up the receive pipe.
2513 */
2514 for (i = 0; i < ZYD_RX_LIST_CNT; i++) {
2515 struct zyd_rx_data *data = &sc->rx_data[i];
2516
2517 usbd_setup_xfer(data->xfer, data, NULL, ZYX_MAX_RXBUFSZ,
2518 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, zyd_rxeof);
2519 error = usbd_transfer(data->xfer);
2520 if (error != USBD_IN_PROGRESS && error != 0) {
2521 printf("%s: could not queue Rx transfer\n",
2522 device_xname(sc->sc_dev));
2523 goto fail;
2524 }
2525 }
2526
2527 ifp->if_flags &= ~IFF_OACTIVE;
2528 ifp->if_flags |= IFF_RUNNING;
2529
2530 if (ic->ic_opmode == IEEE80211_M_MONITOR)
2531 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2532 else
2533 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2534
2535 return 0;
2536
2537 fail: zyd_stop(ifp, 1);
2538 return error;
2539 }
2540
2541 Static void
2542 zyd_stop(struct ifnet *ifp, int disable)
2543 {
2544 struct zyd_softc *sc = ifp->if_softc;
2545 struct ieee80211com *ic = &sc->sc_ic;
2546
2547 ieee80211_new_state(ic, IEEE80211_S_INIT, -1); /* free all nodes */
2548
2549 sc->tx_timer = 0;
2550 ifp->if_timer = 0;
2551 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2552
2553 /* switch radio transmitter OFF */
2554 (void)zyd_switch_radio(sc, 0);
2555
2556 /* disable Rx */
2557 (void)zyd_write32(sc, ZYD_MAC_RXFILTER, 0);
2558
2559 /* disable interrupts */
2560 (void)zyd_write32(sc, ZYD_CR_INTERRUPT, 0);
2561
2562 usbd_abort_pipe(sc->zyd_ep[ZYD_ENDPT_BIN]);
2563 usbd_abort_pipe(sc->zyd_ep[ZYD_ENDPT_BOUT]);
2564
2565 zyd_free_rx_list(sc);
2566 zyd_free_tx_list(sc);
2567 }
2568
2569 Static int
2570 zyd_loadfirmware(struct zyd_softc *sc, u_char *fw, size_t size)
2571 {
2572 usb_device_request_t req;
2573 uint16_t addr;
2574 uint8_t stat;
2575
2576 DPRINTF(("firmware size=%zu\n", size));
2577
2578 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2579 req.bRequest = ZYD_DOWNLOADREQ;
2580 USETW(req.wIndex, 0);
2581
2582 addr = ZYD_FIRMWARE_START_ADDR;
2583 while (size > 0) {
2584 #if 0
2585 const int mlen = uimin(size, 4096);
2586 #else
2587 /*
2588 * XXXX: When the transfer size is 4096 bytes, it is not
2589 * likely to be able to transfer it.
2590 * The cause is port or machine or chip?
2591 */
2592 const int mlen = uimin(size, 64);
2593 #endif
2594
2595 DPRINTF(("loading firmware block: len=%d, addr=%#x\n", mlen,
2596 addr));
2597
2598 USETW(req.wValue, addr);
2599 USETW(req.wLength, mlen);
2600 if (usbd_do_request(sc->sc_udev, &req, fw) != 0)
2601 return EIO;
2602
2603 addr += mlen / 2;
2604 fw += mlen;
2605 size -= mlen;
2606 }
2607
2608 /* check whether the upload succeeded */
2609 req.bmRequestType = UT_READ_VENDOR_DEVICE;
2610 req.bRequest = ZYD_DOWNLOADSTS;
2611 USETW(req.wValue, 0);
2612 USETW(req.wIndex, 0);
2613 USETW(req.wLength, sizeof(stat));
2614 if (usbd_do_request(sc->sc_udev, &req, &stat) != 0)
2615 return EIO;
2616
2617 return (stat & 0x80) ? EIO : 0;
2618 }
2619
2620 Static void
2621 zyd_iter_func(void *arg, struct ieee80211_node *ni)
2622 {
2623 struct zyd_softc *sc = arg;
2624 struct zyd_node *zn = (struct zyd_node *)ni;
2625
2626 ieee80211_amrr_choose(&sc->amrr, ni, &zn->amn);
2627 }
2628
2629 Static void
2630 zyd_amrr_timeout(void *arg)
2631 {
2632 struct zyd_softc *sc = arg;
2633 struct ieee80211com *ic = &sc->sc_ic;
2634 int s;
2635
2636 s = splnet();
2637 if (ic->ic_opmode == IEEE80211_M_STA)
2638 zyd_iter_func(sc, ic->ic_bss);
2639 else
2640 ieee80211_iterate_nodes(&ic->ic_sta, zyd_iter_func, sc);
2641 splx(s);
2642
2643 callout_reset(&sc->sc_amrr_ch, hz, zyd_amrr_timeout, sc);
2644 }
2645
2646 Static void
2647 zyd_newassoc(struct ieee80211_node *ni, int isnew)
2648 {
2649 struct zyd_softc *sc = ni->ni_ic->ic_ifp->if_softc;
2650 int i;
2651
2652 ieee80211_amrr_node_init(&sc->amrr, &((struct zyd_node *)ni)->amn);
2653
2654 /* set rate to some reasonable initial value */
2655 for (i = ni->ni_rates.rs_nrates - 1;
2656 i > 0 && (ni->ni_rates.rs_rates[i] & IEEE80211_RATE_VAL) > 72;
2657 i--);
2658 ni->ni_txrate = i;
2659 }
2660
2661 static int
2662 zyd_activate(device_t self, enum devact act)
2663 {
2664 struct zyd_softc *sc = device_private(self);
2665
2666 switch (act) {
2667 case DVACT_DEACTIVATE:
2668 if_deactivate(&sc->sc_if);
2669 return 0;
2670 default:
2671 return EOPNOTSUPP;
2672 }
2673 }
2674