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