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