]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/usb/wlan/if_zyd.c
Import tzdata 2018e
[FreeBSD/FreeBSD.git] / sys / dev / usb / wlan / 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 #include <sys/cdefs.h>
23 __FBSDID("$FreeBSD$");
24
25 /*
26  * ZyDAS ZD1211/ZD1211B USB WLAN driver.
27  */
28
29 #include "opt_wlan.h"
30
31 #include <sys/param.h>
32 #include <sys/sockio.h>
33 #include <sys/sysctl.h>
34 #include <sys/lock.h>
35 #include <sys/mutex.h>
36 #include <sys/condvar.h>
37 #include <sys/mbuf.h>
38 #include <sys/kernel.h>
39 #include <sys/socket.h>
40 #include <sys/systm.h>
41 #include <sys/malloc.h>
42 #include <sys/module.h>
43 #include <sys/bus.h>
44 #include <sys/endian.h>
45 #include <sys/kdb.h>
46
47 #include <machine/bus.h>
48 #include <machine/resource.h>
49 #include <sys/rman.h>
50
51 #include <net/bpf.h>
52 #include <net/if.h>
53 #include <net/if_var.h>
54 #include <net/if_arp.h>
55 #include <net/ethernet.h>
56 #include <net/if_dl.h>
57 #include <net/if_media.h>
58 #include <net/if_types.h>
59
60 #ifdef INET
61 #include <netinet/in.h>
62 #include <netinet/in_systm.h>
63 #include <netinet/in_var.h>
64 #include <netinet/if_ether.h>
65 #include <netinet/ip.h>
66 #endif
67
68 #include <net80211/ieee80211_var.h>
69 #include <net80211/ieee80211_regdomain.h>
70 #include <net80211/ieee80211_radiotap.h>
71 #include <net80211/ieee80211_ratectl.h>
72
73 #include <dev/usb/usb.h>
74 #include <dev/usb/usbdi.h>
75 #include <dev/usb/usbdi_util.h>
76 #include "usbdevs.h"
77
78 #include <dev/usb/wlan/if_zydreg.h>
79 #include <dev/usb/wlan/if_zydfw.h>
80
81 #ifdef USB_DEBUG
82 static int zyd_debug = 0;
83
84 static SYSCTL_NODE(_hw_usb, OID_AUTO, zyd, CTLFLAG_RW, 0, "USB zyd");
85 SYSCTL_INT(_hw_usb_zyd, OID_AUTO, debug, CTLFLAG_RWTUN, &zyd_debug, 0,
86     "zyd debug level");
87
88 enum {
89         ZYD_DEBUG_XMIT          = 0x00000001,   /* basic xmit operation */
90         ZYD_DEBUG_RECV          = 0x00000002,   /* basic recv operation */
91         ZYD_DEBUG_RESET         = 0x00000004,   /* reset processing */
92         ZYD_DEBUG_INIT          = 0x00000008,   /* device init */
93         ZYD_DEBUG_TX_PROC       = 0x00000010,   /* tx ISR proc */
94         ZYD_DEBUG_RX_PROC       = 0x00000020,   /* rx ISR proc */
95         ZYD_DEBUG_STATE         = 0x00000040,   /* 802.11 state transitions */
96         ZYD_DEBUG_STAT          = 0x00000080,   /* statistic */
97         ZYD_DEBUG_FW            = 0x00000100,   /* firmware */
98         ZYD_DEBUG_CMD           = 0x00000200,   /* fw commands */
99         ZYD_DEBUG_ANY           = 0xffffffff
100 };
101 #define DPRINTF(sc, m, fmt, ...) do {                           \
102         if (zyd_debug & (m))                                    \
103                 printf("%s: " fmt, __func__, ## __VA_ARGS__);   \
104 } while (0)
105 #else
106 #define DPRINTF(sc, m, fmt, ...) do {                           \
107         (void) sc;                                              \
108 } while (0)
109 #endif
110
111 #define zyd_do_request(sc,req,data) \
112     usbd_do_request_flags((sc)->sc_udev, &(sc)->sc_mtx, req, data, 0, NULL, 5000)
113
114 static device_probe_t zyd_match;
115 static device_attach_t zyd_attach;
116 static device_detach_t zyd_detach;
117
118 static usb_callback_t zyd_intr_read_callback;
119 static usb_callback_t zyd_intr_write_callback;
120 static usb_callback_t zyd_bulk_read_callback;
121 static usb_callback_t zyd_bulk_write_callback;
122
123 static struct ieee80211vap *zyd_vap_create(struct ieee80211com *,
124                     const char [IFNAMSIZ], int, enum ieee80211_opmode, int,
125                     const uint8_t [IEEE80211_ADDR_LEN],
126                     const uint8_t [IEEE80211_ADDR_LEN]);
127 static void     zyd_vap_delete(struct ieee80211vap *);
128 static void     zyd_tx_free(struct zyd_tx_data *, int);
129 static void     zyd_setup_tx_list(struct zyd_softc *);
130 static void     zyd_unsetup_tx_list(struct zyd_softc *);
131 static int      zyd_newstate(struct ieee80211vap *, enum ieee80211_state, int);
132 static int      zyd_cmd(struct zyd_softc *, uint16_t, const void *, int,
133                     void *, int, int);
134 static int      zyd_read16(struct zyd_softc *, uint16_t, uint16_t *);
135 static int      zyd_read32(struct zyd_softc *, uint16_t, uint32_t *);
136 static int      zyd_write16(struct zyd_softc *, uint16_t, uint16_t);
137 static int      zyd_write32(struct zyd_softc *, uint16_t, uint32_t);
138 static int      zyd_rfwrite(struct zyd_softc *, uint32_t);
139 static int      zyd_lock_phy(struct zyd_softc *);
140 static int      zyd_unlock_phy(struct zyd_softc *);
141 static int      zyd_rf_attach(struct zyd_softc *, uint8_t);
142 static const char *zyd_rf_name(uint8_t);
143 static int      zyd_hw_init(struct zyd_softc *);
144 static int      zyd_read_pod(struct zyd_softc *);
145 static int      zyd_read_eeprom(struct zyd_softc *);
146 static int      zyd_get_macaddr(struct zyd_softc *);
147 static int      zyd_set_macaddr(struct zyd_softc *, const uint8_t *);
148 static int      zyd_set_bssid(struct zyd_softc *, const uint8_t *);
149 static int      zyd_switch_radio(struct zyd_softc *, int);
150 static int      zyd_set_led(struct zyd_softc *, int, int);
151 static void     zyd_set_multi(struct zyd_softc *);
152 static void     zyd_update_mcast(struct ieee80211com *);
153 static int      zyd_set_rxfilter(struct zyd_softc *);
154 static void     zyd_set_chan(struct zyd_softc *, struct ieee80211_channel *);
155 static int      zyd_set_beacon_interval(struct zyd_softc *, int);
156 static void     zyd_rx_data(struct usb_xfer *, int, uint16_t);
157 static int      zyd_tx_start(struct zyd_softc *, struct mbuf *,
158                     struct ieee80211_node *);
159 static int      zyd_transmit(struct ieee80211com *, struct mbuf *);
160 static void     zyd_start(struct zyd_softc *);
161 static int      zyd_raw_xmit(struct ieee80211_node *, struct mbuf *,
162                     const struct ieee80211_bpf_params *);
163 static void     zyd_parent(struct ieee80211com *);
164 static void     zyd_init_locked(struct zyd_softc *);
165 static void     zyd_stop(struct zyd_softc *);
166 static int      zyd_loadfirmware(struct zyd_softc *);
167 static void     zyd_scan_start(struct ieee80211com *);
168 static void     zyd_scan_end(struct ieee80211com *);
169 static void     zyd_getradiocaps(struct ieee80211com *, int, int *,
170                     struct ieee80211_channel[]);
171 static void     zyd_set_channel(struct ieee80211com *);
172 static int      zyd_rfmd_init(struct zyd_rf *);
173 static int      zyd_rfmd_switch_radio(struct zyd_rf *, int);
174 static int      zyd_rfmd_set_channel(struct zyd_rf *, uint8_t);
175 static int      zyd_al2230_init(struct zyd_rf *);
176 static int      zyd_al2230_switch_radio(struct zyd_rf *, int);
177 static int      zyd_al2230_set_channel(struct zyd_rf *, uint8_t);
178 static int      zyd_al2230_set_channel_b(struct zyd_rf *, uint8_t);
179 static int      zyd_al2230_init_b(struct zyd_rf *);
180 static int      zyd_al7230B_init(struct zyd_rf *);
181 static int      zyd_al7230B_switch_radio(struct zyd_rf *, int);
182 static int      zyd_al7230B_set_channel(struct zyd_rf *, uint8_t);
183 static int      zyd_al2210_init(struct zyd_rf *);
184 static int      zyd_al2210_switch_radio(struct zyd_rf *, int);
185 static int      zyd_al2210_set_channel(struct zyd_rf *, uint8_t);
186 static int      zyd_gct_init(struct zyd_rf *);
187 static int      zyd_gct_switch_radio(struct zyd_rf *, int);
188 static int      zyd_gct_set_channel(struct zyd_rf *, uint8_t);
189 static int      zyd_gct_mode(struct zyd_rf *);
190 static int      zyd_gct_set_channel_synth(struct zyd_rf *, int, int);
191 static int      zyd_gct_write(struct zyd_rf *, uint16_t);
192 static int      zyd_gct_txgain(struct zyd_rf *, uint8_t);
193 static int      zyd_maxim2_init(struct zyd_rf *);
194 static int      zyd_maxim2_switch_radio(struct zyd_rf *, int);
195 static int      zyd_maxim2_set_channel(struct zyd_rf *, uint8_t);
196
197 static const struct zyd_phy_pair zyd_def_phy[] = ZYD_DEF_PHY;
198 static const struct zyd_phy_pair zyd_def_phyB[] = ZYD_DEF_PHYB;
199
200 /* various supported device vendors/products */
201 #define ZYD_ZD1211      0
202 #define ZYD_ZD1211B     1
203
204 #define ZYD_ZD1211_DEV(v,p)     \
205         { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, ZYD_ZD1211) }
206 #define ZYD_ZD1211B_DEV(v,p)    \
207         { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, ZYD_ZD1211B) }
208 static const STRUCT_USB_HOST_ID zyd_devs[] = {
209         /* ZYD_ZD1211 */
210         ZYD_ZD1211_DEV(3COM2, 3CRUSB10075),
211         ZYD_ZD1211_DEV(ABOCOM, WL54),
212         ZYD_ZD1211_DEV(ASUS, WL159G),
213         ZYD_ZD1211_DEV(CYBERTAN, TG54USB),
214         ZYD_ZD1211_DEV(DRAYTEK, VIGOR550),
215         ZYD_ZD1211_DEV(PLANEX2, GWUS54GD),
216         ZYD_ZD1211_DEV(PLANEX2, GWUS54GZL),
217         ZYD_ZD1211_DEV(PLANEX3, GWUS54GZ),
218         ZYD_ZD1211_DEV(PLANEX3, GWUS54MINI),
219         ZYD_ZD1211_DEV(SAGEM, XG760A),
220         ZYD_ZD1211_DEV(SENAO, NUB8301),
221         ZYD_ZD1211_DEV(SITECOMEU, WL113),
222         ZYD_ZD1211_DEV(SWEEX, ZD1211),
223         ZYD_ZD1211_DEV(TEKRAM, QUICKWLAN),
224         ZYD_ZD1211_DEV(TEKRAM, ZD1211_1),
225         ZYD_ZD1211_DEV(TEKRAM, ZD1211_2),
226         ZYD_ZD1211_DEV(TWINMOS, G240),
227         ZYD_ZD1211_DEV(UMEDIA, ALL0298V2),
228         ZYD_ZD1211_DEV(UMEDIA, TEW429UB_A),
229         ZYD_ZD1211_DEV(UMEDIA, TEW429UB),
230         ZYD_ZD1211_DEV(WISTRONNEWEB, UR055G),
231         ZYD_ZD1211_DEV(ZCOM, ZD1211),
232         ZYD_ZD1211_DEV(ZYDAS, ZD1211),
233         ZYD_ZD1211_DEV(ZYXEL, AG225H),
234         ZYD_ZD1211_DEV(ZYXEL, ZYAIRG220),
235         ZYD_ZD1211_DEV(ZYXEL, G200V2),
236         /* ZYD_ZD1211B */
237         ZYD_ZD1211B_DEV(ACCTON, SMCWUSBG_NF),
238         ZYD_ZD1211B_DEV(ACCTON, SMCWUSBG),
239         ZYD_ZD1211B_DEV(ACCTON, ZD1211B),
240         ZYD_ZD1211B_DEV(ASUS, A9T_WIFI),
241         ZYD_ZD1211B_DEV(BELKIN, F5D7050_V4000),
242         ZYD_ZD1211B_DEV(BELKIN, ZD1211B),
243         ZYD_ZD1211B_DEV(CISCOLINKSYS, WUSBF54G),
244         ZYD_ZD1211B_DEV(FIBERLINE, WL430U),
245         ZYD_ZD1211B_DEV(MELCO, KG54L),
246         ZYD_ZD1211B_DEV(PHILIPS, SNU5600),
247         ZYD_ZD1211B_DEV(PLANEX2, GW_US54GXS),
248         ZYD_ZD1211B_DEV(SAGEM, XG76NA),
249         ZYD_ZD1211B_DEV(SITECOMEU, ZD1211B),
250         ZYD_ZD1211B_DEV(UMEDIA, TEW429UBC1),
251         ZYD_ZD1211B_DEV(USR, USR5423),
252         ZYD_ZD1211B_DEV(VTECH, ZD1211B),
253         ZYD_ZD1211B_DEV(ZCOM, ZD1211B),
254         ZYD_ZD1211B_DEV(ZYDAS, ZD1211B),
255         ZYD_ZD1211B_DEV(ZYXEL, M202),
256         ZYD_ZD1211B_DEV(ZYXEL, G202),
257         ZYD_ZD1211B_DEV(ZYXEL, G220V2)
258 };
259
260 static const struct usb_config zyd_config[ZYD_N_TRANSFER] = {
261         [ZYD_BULK_WR] = {
262                 .type = UE_BULK,
263                 .endpoint = UE_ADDR_ANY,
264                 .direction = UE_DIR_OUT,
265                 .bufsize = ZYD_MAX_TXBUFSZ,
266                 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
267                 .callback = zyd_bulk_write_callback,
268                 .ep_index = 0,
269                 .timeout = 10000,       /* 10 seconds */
270         },
271         [ZYD_BULK_RD] = {
272                 .type = UE_BULK,
273                 .endpoint = UE_ADDR_ANY,
274                 .direction = UE_DIR_IN,
275                 .bufsize = ZYX_MAX_RXBUFSZ,
276                 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
277                 .callback = zyd_bulk_read_callback,
278                 .ep_index = 0,
279         },
280         [ZYD_INTR_WR] = {
281                 .type = UE_BULK_INTR,
282                 .endpoint = UE_ADDR_ANY,
283                 .direction = UE_DIR_OUT,
284                 .bufsize = sizeof(struct zyd_cmd),
285                 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
286                 .callback = zyd_intr_write_callback,
287                 .timeout = 1000,        /* 1 second */
288                 .ep_index = 1,
289         },
290         [ZYD_INTR_RD] = {
291                 .type = UE_INTERRUPT,
292                 .endpoint = UE_ADDR_ANY,
293                 .direction = UE_DIR_IN,
294                 .bufsize = sizeof(struct zyd_cmd),
295                 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
296                 .callback = zyd_intr_read_callback,
297         },
298 };
299 #define zyd_read16_m(sc, val, data)     do {                            \
300         error = zyd_read16(sc, val, data);                              \
301         if (error != 0)                                                 \
302                 goto fail;                                              \
303 } while (0)
304 #define zyd_write16_m(sc, val, data)    do {                            \
305         error = zyd_write16(sc, val, data);                             \
306         if (error != 0)                                                 \
307                 goto fail;                                              \
308 } while (0)
309 #define zyd_read32_m(sc, val, data)     do {                            \
310         error = zyd_read32(sc, val, data);                              \
311         if (error != 0)                                                 \
312                 goto fail;                                              \
313 } while (0)
314 #define zyd_write32_m(sc, val, data)    do {                            \
315         error = zyd_write32(sc, val, data);                             \
316         if (error != 0)                                                 \
317                 goto fail;                                              \
318 } while (0)
319
320 static int
321 zyd_match(device_t dev)
322 {
323         struct usb_attach_arg *uaa = device_get_ivars(dev);
324
325         if (uaa->usb_mode != USB_MODE_HOST)
326                 return (ENXIO);
327         if (uaa->info.bConfigIndex != ZYD_CONFIG_INDEX)
328                 return (ENXIO);
329         if (uaa->info.bIfaceIndex != ZYD_IFACE_INDEX)
330                 return (ENXIO);
331
332         return (usbd_lookup_id_by_uaa(zyd_devs, sizeof(zyd_devs), uaa));
333 }
334
335 static int
336 zyd_attach(device_t dev)
337 {
338         struct usb_attach_arg *uaa = device_get_ivars(dev);
339         struct zyd_softc *sc = device_get_softc(dev);
340         struct ieee80211com *ic = &sc->sc_ic;
341         uint8_t iface_index;
342         int error;
343
344         if (uaa->info.bcdDevice < 0x4330) {
345                 device_printf(dev, "device version mismatch: 0x%X "
346                     "(only >= 43.30 supported)\n",
347                     uaa->info.bcdDevice);
348                 return (EINVAL);
349         }
350
351         device_set_usb_desc(dev);
352         sc->sc_dev = dev;
353         sc->sc_udev = uaa->device;
354         sc->sc_macrev = USB_GET_DRIVER_INFO(uaa);
355
356         mtx_init(&sc->sc_mtx, device_get_nameunit(sc->sc_dev),
357             MTX_NETWORK_LOCK, MTX_DEF);
358         STAILQ_INIT(&sc->sc_rqh);
359         mbufq_init(&sc->sc_snd, ifqmaxlen);
360
361         iface_index = ZYD_IFACE_INDEX;
362         error = usbd_transfer_setup(uaa->device,
363             &iface_index, sc->sc_xfer, zyd_config,
364             ZYD_N_TRANSFER, sc, &sc->sc_mtx);
365         if (error) {
366                 device_printf(dev, "could not allocate USB transfers, "
367                     "err=%s\n", usbd_errstr(error));
368                 goto detach;
369         }
370
371         ZYD_LOCK(sc);
372         if ((error = zyd_get_macaddr(sc)) != 0) {
373                 device_printf(sc->sc_dev, "could not read EEPROM\n");
374                 ZYD_UNLOCK(sc);
375                 goto detach;
376         }
377         ZYD_UNLOCK(sc);
378
379         ic->ic_softc = sc;
380         ic->ic_name = device_get_nameunit(dev);
381         ic->ic_phytype = IEEE80211_T_OFDM;      /* not only, but not used */
382         ic->ic_opmode = IEEE80211_M_STA;
383
384         /* set device capabilities */
385         ic->ic_caps =
386                   IEEE80211_C_STA               /* station mode */
387                 | IEEE80211_C_MONITOR           /* monitor mode */
388                 | IEEE80211_C_SHPREAMBLE        /* short preamble supported */
389                 | IEEE80211_C_SHSLOT            /* short slot time supported */
390                 | IEEE80211_C_BGSCAN            /* capable of bg scanning */
391                 | IEEE80211_C_WPA               /* 802.11i */
392                 ;
393
394         zyd_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
395             ic->ic_channels);
396
397         ieee80211_ifattach(ic);
398         ic->ic_raw_xmit = zyd_raw_xmit;
399         ic->ic_scan_start = zyd_scan_start;
400         ic->ic_scan_end = zyd_scan_end;
401         ic->ic_getradiocaps = zyd_getradiocaps;
402         ic->ic_set_channel = zyd_set_channel;
403         ic->ic_vap_create = zyd_vap_create;
404         ic->ic_vap_delete = zyd_vap_delete;
405         ic->ic_update_mcast = zyd_update_mcast;
406         ic->ic_update_promisc = zyd_update_mcast;
407         ic->ic_parent = zyd_parent;
408         ic->ic_transmit = zyd_transmit;
409
410         ieee80211_radiotap_attach(ic,
411             &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
412                 ZYD_TX_RADIOTAP_PRESENT,
413             &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
414                 ZYD_RX_RADIOTAP_PRESENT);
415
416         if (bootverbose)
417                 ieee80211_announce(ic);
418
419         return (0);
420
421 detach:
422         zyd_detach(dev);
423         return (ENXIO);                 /* failure */
424 }
425
426 static void
427 zyd_drain_mbufq(struct zyd_softc *sc)
428 {
429         struct mbuf *m;
430         struct ieee80211_node *ni;
431
432         ZYD_LOCK_ASSERT(sc, MA_OWNED);
433         while ((m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
434                 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
435                 m->m_pkthdr.rcvif = NULL;
436                 ieee80211_free_node(ni);
437                 m_freem(m);
438         }
439 }
440
441
442 static int
443 zyd_detach(device_t dev)
444 {
445         struct zyd_softc *sc = device_get_softc(dev);
446         struct ieee80211com *ic = &sc->sc_ic;
447         unsigned int x;
448
449         /*
450          * Prevent further allocations from RX/TX data
451          * lists and ioctls:
452          */
453         ZYD_LOCK(sc);
454         sc->sc_flags |= ZYD_FLAG_DETACHED;
455         zyd_drain_mbufq(sc);
456         STAILQ_INIT(&sc->tx_q);
457         STAILQ_INIT(&sc->tx_free);
458         ZYD_UNLOCK(sc);
459
460         /* drain USB transfers */
461         for (x = 0; x != ZYD_N_TRANSFER; x++)
462                 usbd_transfer_drain(sc->sc_xfer[x]);
463
464         /* free TX list, if any */
465         ZYD_LOCK(sc);
466         zyd_unsetup_tx_list(sc);
467         ZYD_UNLOCK(sc);
468
469         /* free USB transfers and some data buffers */
470         usbd_transfer_unsetup(sc->sc_xfer, ZYD_N_TRANSFER);
471
472         if (ic->ic_softc == sc)
473                 ieee80211_ifdetach(ic);
474         mtx_destroy(&sc->sc_mtx);
475
476         return (0);
477 }
478
479 static struct ieee80211vap *
480 zyd_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit,
481     enum ieee80211_opmode opmode, int flags,
482     const uint8_t bssid[IEEE80211_ADDR_LEN],
483     const uint8_t mac[IEEE80211_ADDR_LEN])
484 {
485         struct zyd_vap *zvp;
486         struct ieee80211vap *vap;
487
488         if (!TAILQ_EMPTY(&ic->ic_vaps))         /* only one at a time */
489                 return (NULL);
490         zvp = malloc(sizeof(struct zyd_vap), M_80211_VAP, M_WAITOK | M_ZERO);
491         vap = &zvp->vap;
492
493         /* enable s/w bmiss handling for sta mode */
494         if (ieee80211_vap_setup(ic, vap, name, unit, opmode,
495             flags | IEEE80211_CLONE_NOBEACONS, bssid) != 0) {
496                 /* out of memory */
497                 free(zvp, M_80211_VAP);
498                 return (NULL);
499         }
500
501         /* override state transition machine */
502         zvp->newstate = vap->iv_newstate;
503         vap->iv_newstate = zyd_newstate;
504
505         ieee80211_ratectl_init(vap);
506         ieee80211_ratectl_setinterval(vap, 1000 /* 1 sec */);
507
508         /* complete setup */
509         ieee80211_vap_attach(vap, ieee80211_media_change,
510             ieee80211_media_status, mac);
511         ic->ic_opmode = opmode;
512         return (vap);
513 }
514
515 static void
516 zyd_vap_delete(struct ieee80211vap *vap)
517 {
518         struct zyd_vap *zvp = ZYD_VAP(vap);
519
520         ieee80211_ratectl_deinit(vap);
521         ieee80211_vap_detach(vap);
522         free(zvp, M_80211_VAP);
523 }
524
525 static void
526 zyd_tx_free(struct zyd_tx_data *data, int txerr)
527 {
528         struct zyd_softc *sc = data->sc;
529
530         if (data->m != NULL) {
531                 ieee80211_tx_complete(data->ni, data->m, txerr);
532                 data->m = NULL;
533                 data->ni = NULL;
534         }
535         STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
536         sc->tx_nfree++;
537 }
538
539 static void
540 zyd_setup_tx_list(struct zyd_softc *sc)
541 {
542         struct zyd_tx_data *data;
543         int i;
544
545         sc->tx_nfree = 0;
546         STAILQ_INIT(&sc->tx_q);
547         STAILQ_INIT(&sc->tx_free);
548
549         for (i = 0; i < ZYD_TX_LIST_CNT; i++) {
550                 data = &sc->tx_data[i];
551
552                 data->sc = sc;
553                 STAILQ_INSERT_TAIL(&sc->tx_free, data, next);
554                 sc->tx_nfree++;
555         }
556 }
557
558 static void
559 zyd_unsetup_tx_list(struct zyd_softc *sc)
560 {
561         struct zyd_tx_data *data;
562         int i;
563
564         /* make sure any subsequent use of the queues will fail */
565         sc->tx_nfree = 0;
566         STAILQ_INIT(&sc->tx_q);
567         STAILQ_INIT(&sc->tx_free);
568
569         /* free up all node references and mbufs */
570         for (i = 0; i < ZYD_TX_LIST_CNT; i++) {
571                 data = &sc->tx_data[i];
572
573                 if (data->m != NULL) {
574                         m_freem(data->m);
575                         data->m = NULL;
576                 }
577                 if (data->ni != NULL) {
578                         ieee80211_free_node(data->ni);
579                         data->ni = NULL;
580                 }
581         }
582 }
583
584 static int
585 zyd_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
586 {
587         struct zyd_vap *zvp = ZYD_VAP(vap);
588         struct ieee80211com *ic = vap->iv_ic;
589         struct zyd_softc *sc = ic->ic_softc;
590         int error;
591
592         DPRINTF(sc, ZYD_DEBUG_STATE, "%s: %s -> %s\n", __func__,
593             ieee80211_state_name[vap->iv_state],
594             ieee80211_state_name[nstate]);
595
596         IEEE80211_UNLOCK(ic);
597         ZYD_LOCK(sc);
598         switch (nstate) {
599         case IEEE80211_S_AUTH:
600                 zyd_set_chan(sc, ic->ic_curchan);
601                 break;
602         case IEEE80211_S_RUN:
603                 if (vap->iv_opmode == IEEE80211_M_MONITOR)
604                         break;
605
606                 /* turn link LED on */
607                 error = zyd_set_led(sc, ZYD_LED1, 1);
608                 if (error != 0)
609                         break;
610
611                 /* make data LED blink upon Tx */
612                 zyd_write32_m(sc, sc->sc_fwbase + ZYD_FW_LINK_STATUS, 1);
613
614                 IEEE80211_ADDR_COPY(sc->sc_bssid, vap->iv_bss->ni_bssid);
615                 zyd_set_bssid(sc, sc->sc_bssid);
616                 break;
617         default:
618                 break;
619         }
620 fail:
621         ZYD_UNLOCK(sc);
622         IEEE80211_LOCK(ic);
623         return (zvp->newstate(vap, nstate, arg));
624 }
625
626 /*
627  * Callback handler for interrupt transfer
628  */
629 static void
630 zyd_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
631 {
632         struct zyd_softc *sc = usbd_xfer_softc(xfer);
633         struct ieee80211com *ic = &sc->sc_ic;
634         struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
635         struct ieee80211_node *ni;
636         struct zyd_cmd *cmd = &sc->sc_ibuf;
637         struct usb_page_cache *pc;
638         int datalen;
639         int actlen;
640
641         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
642
643         switch (USB_GET_STATE(xfer)) {
644         case USB_ST_TRANSFERRED:
645                 pc = usbd_xfer_get_frame(xfer, 0);
646                 usbd_copy_out(pc, 0, cmd, sizeof(*cmd));
647
648                 switch (le16toh(cmd->code)) {
649                 case ZYD_NOTIF_RETRYSTATUS:
650                 {
651                         struct zyd_notif_retry *retry =
652                             (struct zyd_notif_retry *)cmd->data;
653                         uint16_t count = le16toh(retry->count);
654
655                         DPRINTF(sc, ZYD_DEBUG_TX_PROC,
656                             "retry intr: rate=0x%x addr=%s count=%d (0x%x)\n",
657                             le16toh(retry->rate), ether_sprintf(retry->macaddr),
658                             count & 0xff, count);
659
660                         /*
661                          * Find the node to which the packet was sent and
662                          * update its retry statistics.  In BSS mode, this node
663                          * is the AP we're associated to so no lookup is
664                          * actually needed.
665                          */
666                         ni = ieee80211_find_txnode(vap, retry->macaddr);
667                         if (ni != NULL) {
668                                 struct ieee80211_ratectl_tx_status *txs =
669                                     &sc->sc_txs;
670                                 int retrycnt = count & 0xff;
671
672                                 txs->flags =
673                                     IEEE80211_RATECTL_STATUS_LONG_RETRY;
674                                 txs->long_retries = retrycnt;
675                                 if (count & 0x100) {
676                                         txs->status =
677                                             IEEE80211_RATECTL_TX_FAIL_LONG;
678                                 } else {
679                                         txs->status =
680                                             IEEE80211_RATECTL_TX_SUCCESS;
681                                 }
682
683
684                                 ieee80211_ratectl_tx_complete(ni, txs);
685                                 ieee80211_free_node(ni);
686                         }
687                         if (count & 0x100)
688                                 /* too many retries */
689                                 if_inc_counter(vap->iv_ifp, IFCOUNTER_OERRORS,
690                                     1);
691                         break;
692                 }
693                 case ZYD_NOTIF_IORD:
694                 {
695                         struct zyd_rq *rqp;
696
697                         if (le16toh(*(uint16_t *)cmd->data) == ZYD_CR_INTERRUPT)
698                                 break;  /* HMAC interrupt */
699
700                         datalen = actlen - sizeof(cmd->code);
701                         datalen -= 2;   /* XXX: padding? */
702
703                         STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) {
704                                 int i;
705                                 int count;
706
707                                 if (rqp->olen != datalen)
708                                         continue;
709                                 count = rqp->olen / sizeof(struct zyd_pair);
710                                 for (i = 0; i < count; i++) {
711                                         if (*(((const uint16_t *)rqp->idata) + i) !=
712                                             (((struct zyd_pair *)cmd->data) + i)->reg)
713                                                 break;
714                                 }
715                                 if (i != count)
716                                         continue;
717                                 /* copy answer into caller-supplied buffer */
718                                 memcpy(rqp->odata, cmd->data, rqp->olen);
719                                 DPRINTF(sc, ZYD_DEBUG_CMD,
720                                     "command %p complete, data = %*D \n",
721                                     rqp, rqp->olen, (char *)rqp->odata, ":");
722                                 wakeup(rqp);    /* wakeup caller */
723                                 break;
724                         }
725                         if (rqp == NULL) {
726                                 device_printf(sc->sc_dev,
727                                     "unexpected IORD notification %*D\n",
728                                     datalen, cmd->data, ":");
729                         }
730                         break;
731                 }
732                 default:
733                         device_printf(sc->sc_dev, "unknown notification %x\n",
734                             le16toh(cmd->code));
735                 }
736
737                 /* FALLTHROUGH */
738         case USB_ST_SETUP:
739 tr_setup:
740                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
741                 usbd_transfer_submit(xfer);
742                 break;
743
744         default:                        /* Error */
745                 DPRINTF(sc, ZYD_DEBUG_CMD, "error = %s\n",
746                     usbd_errstr(error));
747
748                 if (error != USB_ERR_CANCELLED) {
749                         /* try to clear stall first */
750                         usbd_xfer_set_stall(xfer);
751                         goto tr_setup;
752                 }
753                 break;
754         }
755 }
756
757 static void
758 zyd_intr_write_callback(struct usb_xfer *xfer, usb_error_t error)
759 {
760         struct zyd_softc *sc = usbd_xfer_softc(xfer);
761         struct zyd_rq *rqp, *cmd;
762         struct usb_page_cache *pc;
763
764         switch (USB_GET_STATE(xfer)) {
765         case USB_ST_TRANSFERRED:
766                 cmd = usbd_xfer_get_priv(xfer);
767                 DPRINTF(sc, ZYD_DEBUG_CMD, "command %p transferred\n", cmd);
768                 STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) {
769                         /* Ensure the cached rq pointer is still valid */
770                         if (rqp == cmd &&
771                             (rqp->flags & ZYD_CMD_FLAG_READ) == 0)
772                                 wakeup(rqp);    /* wakeup caller */
773                 }
774
775                 /* FALLTHROUGH */
776         case USB_ST_SETUP:
777 tr_setup:
778                 STAILQ_FOREACH(rqp, &sc->sc_rqh, rq) {
779                         if (rqp->flags & ZYD_CMD_FLAG_SENT)
780                                 continue;
781
782                         pc = usbd_xfer_get_frame(xfer, 0);
783                         usbd_copy_in(pc, 0, rqp->cmd, rqp->ilen);
784
785                         usbd_xfer_set_frame_len(xfer, 0, rqp->ilen);
786                         usbd_xfer_set_priv(xfer, rqp);
787                         rqp->flags |= ZYD_CMD_FLAG_SENT;
788                         usbd_transfer_submit(xfer);
789                         break;
790                 }
791                 break;
792
793         default:                        /* Error */
794                 DPRINTF(sc, ZYD_DEBUG_ANY, "error = %s\n",
795                     usbd_errstr(error));
796
797                 if (error != USB_ERR_CANCELLED) {
798                         /* try to clear stall first */
799                         usbd_xfer_set_stall(xfer);
800                         goto tr_setup;
801                 }
802                 break;
803         }
804 }
805
806 static int
807 zyd_cmd(struct zyd_softc *sc, uint16_t code, const void *idata, int ilen,
808     void *odata, int olen, int flags)
809 {
810         struct zyd_cmd cmd;
811         struct zyd_rq rq;
812         int error;
813
814         if (ilen > (int)sizeof(cmd.data))
815                 return (EINVAL);
816
817         cmd.code = htole16(code);
818         memcpy(cmd.data, idata, ilen);
819         DPRINTF(sc, ZYD_DEBUG_CMD, "sending cmd %p = %*D\n",
820             &rq, ilen, idata, ":");
821
822         rq.cmd = &cmd;
823         rq.idata = idata;
824         rq.odata = odata;
825         rq.ilen = sizeof(uint16_t) + ilen;
826         rq.olen = olen;
827         rq.flags = flags;
828         STAILQ_INSERT_TAIL(&sc->sc_rqh, &rq, rq);
829         usbd_transfer_start(sc->sc_xfer[ZYD_INTR_RD]);
830         usbd_transfer_start(sc->sc_xfer[ZYD_INTR_WR]);
831
832         /* wait at most one second for command reply */
833         error = mtx_sleep(&rq, &sc->sc_mtx, 0 , "zydcmd", hz);
834         if (error)
835                 device_printf(sc->sc_dev, "command timeout\n");
836         STAILQ_REMOVE(&sc->sc_rqh, &rq, zyd_rq, rq);
837         DPRINTF(sc, ZYD_DEBUG_CMD, "finsihed cmd %p, error = %d \n",
838             &rq, error);
839
840         return (error);
841 }
842
843 static int
844 zyd_read16(struct zyd_softc *sc, uint16_t reg, uint16_t *val)
845 {
846         struct zyd_pair tmp;
847         int error;
848
849         reg = htole16(reg);
850         error = zyd_cmd(sc, ZYD_CMD_IORD, &reg, sizeof(reg), &tmp, sizeof(tmp),
851             ZYD_CMD_FLAG_READ);
852         if (error == 0)
853                 *val = le16toh(tmp.val);
854         return (error);
855 }
856
857 static int
858 zyd_read32(struct zyd_softc *sc, uint16_t reg, uint32_t *val)
859 {
860         struct zyd_pair tmp[2];
861         uint16_t regs[2];
862         int error;
863
864         regs[0] = htole16(ZYD_REG32_HI(reg));
865         regs[1] = htole16(ZYD_REG32_LO(reg));
866         error = zyd_cmd(sc, ZYD_CMD_IORD, regs, sizeof(regs), tmp, sizeof(tmp),
867             ZYD_CMD_FLAG_READ);
868         if (error == 0)
869                 *val = le16toh(tmp[0].val) << 16 | le16toh(tmp[1].val);
870         return (error);
871 }
872
873 static int
874 zyd_write16(struct zyd_softc *sc, uint16_t reg, uint16_t val)
875 {
876         struct zyd_pair pair;
877
878         pair.reg = htole16(reg);
879         pair.val = htole16(val);
880
881         return zyd_cmd(sc, ZYD_CMD_IOWR, &pair, sizeof(pair), NULL, 0, 0);
882 }
883
884 static int
885 zyd_write32(struct zyd_softc *sc, uint16_t reg, uint32_t val)
886 {
887         struct zyd_pair pair[2];
888
889         pair[0].reg = htole16(ZYD_REG32_HI(reg));
890         pair[0].val = htole16(val >> 16);
891         pair[1].reg = htole16(ZYD_REG32_LO(reg));
892         pair[1].val = htole16(val & 0xffff);
893
894         return zyd_cmd(sc, ZYD_CMD_IOWR, pair, sizeof(pair), NULL, 0, 0);
895 }
896
897 static int
898 zyd_rfwrite(struct zyd_softc *sc, uint32_t val)
899 {
900         struct zyd_rf *rf = &sc->sc_rf;
901         struct zyd_rfwrite_cmd req;
902         uint16_t cr203;
903         int error, i;
904
905         zyd_read16_m(sc, ZYD_CR203, &cr203);
906         cr203 &= ~(ZYD_RF_IF_LE | ZYD_RF_CLK | ZYD_RF_DATA);
907
908         req.code  = htole16(2);
909         req.width = htole16(rf->width);
910         for (i = 0; i < rf->width; i++) {
911                 req.bit[i] = htole16(cr203);
912                 if (val & (1 << (rf->width - 1 - i)))
913                         req.bit[i] |= htole16(ZYD_RF_DATA);
914         }
915         error = zyd_cmd(sc, ZYD_CMD_RFCFG, &req, 4 + 2 * rf->width, NULL, 0, 0);
916 fail:
917         return (error);
918 }
919
920 static int
921 zyd_rfwrite_cr(struct zyd_softc *sc, uint32_t val)
922 {
923         int error;
924
925         zyd_write16_m(sc, ZYD_CR244, (val >> 16) & 0xff);
926         zyd_write16_m(sc, ZYD_CR243, (val >>  8) & 0xff);
927         zyd_write16_m(sc, ZYD_CR242, (val >>  0) & 0xff);
928 fail:
929         return (error);
930 }
931
932 static int
933 zyd_lock_phy(struct zyd_softc *sc)
934 {
935         int error;
936         uint32_t tmp;
937
938         zyd_read32_m(sc, ZYD_MAC_MISC, &tmp);
939         tmp &= ~ZYD_UNLOCK_PHY_REGS;
940         zyd_write32_m(sc, ZYD_MAC_MISC, tmp);
941 fail:
942         return (error);
943 }
944
945 static int
946 zyd_unlock_phy(struct zyd_softc *sc)
947 {
948         int error;
949         uint32_t tmp;
950
951         zyd_read32_m(sc, ZYD_MAC_MISC, &tmp);
952         tmp |= ZYD_UNLOCK_PHY_REGS;
953         zyd_write32_m(sc, ZYD_MAC_MISC, tmp);
954 fail:
955         return (error);
956 }
957
958 /*
959  * RFMD RF methods.
960  */
961 static int
962 zyd_rfmd_init(struct zyd_rf *rf)
963 {
964         struct zyd_softc *sc = rf->rf_sc;
965         static const struct zyd_phy_pair phyini[] = ZYD_RFMD_PHY;
966         static const uint32_t rfini[] = ZYD_RFMD_RF;
967         int i, error;
968
969         /* init RF-dependent PHY registers */
970         for (i = 0; i < nitems(phyini); i++) {
971                 zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
972         }
973
974         /* init RFMD radio */
975         for (i = 0; i < nitems(rfini); i++) {
976                 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
977                         return (error);
978         }
979 fail:
980         return (error);
981 }
982
983 static int
984 zyd_rfmd_switch_radio(struct zyd_rf *rf, int on)
985 {
986         int error;
987         struct zyd_softc *sc = rf->rf_sc;
988
989         zyd_write16_m(sc, ZYD_CR10, on ? 0x89 : 0x15);
990         zyd_write16_m(sc, ZYD_CR11, on ? 0x00 : 0x81);
991 fail:
992         return (error);
993 }
994
995 static int
996 zyd_rfmd_set_channel(struct zyd_rf *rf, uint8_t chan)
997 {
998         int error;
999         struct zyd_softc *sc = rf->rf_sc;
1000         static const struct {
1001                 uint32_t        r1, r2;
1002         } rfprog[] = ZYD_RFMD_CHANTABLE;
1003
1004         error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
1005         if (error != 0)
1006                 goto fail;
1007         error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
1008         if (error != 0)
1009                 goto fail;
1010
1011 fail:
1012         return (error);
1013 }
1014
1015 /*
1016  * AL2230 RF methods.
1017  */
1018 static int
1019 zyd_al2230_init(struct zyd_rf *rf)
1020 {
1021         struct zyd_softc *sc = rf->rf_sc;
1022         static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY;
1023         static const struct zyd_phy_pair phy2230s[] = ZYD_AL2230S_PHY_INIT;
1024         static const struct zyd_phy_pair phypll[] = {
1025                 { ZYD_CR251, 0x2f }, { ZYD_CR251, 0x3f },
1026                 { ZYD_CR138, 0x28 }, { ZYD_CR203, 0x06 }
1027         };
1028         static const uint32_t rfini1[] = ZYD_AL2230_RF_PART1;
1029         static const uint32_t rfini2[] = ZYD_AL2230_RF_PART2;
1030         static const uint32_t rfini3[] = ZYD_AL2230_RF_PART3;
1031         int i, error;
1032
1033         /* init RF-dependent PHY registers */
1034         for (i = 0; i < nitems(phyini); i++)
1035                 zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1036
1037         if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0) {
1038                 for (i = 0; i < nitems(phy2230s); i++)
1039                         zyd_write16_m(sc, phy2230s[i].reg, phy2230s[i].val);
1040         }
1041
1042         /* init AL2230 radio */
1043         for (i = 0; i < nitems(rfini1); i++) {
1044                 error = zyd_rfwrite(sc, rfini1[i]);
1045                 if (error != 0)
1046                         goto fail;
1047         }
1048
1049         if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0)
1050                 error = zyd_rfwrite(sc, 0x000824);
1051         else
1052                 error = zyd_rfwrite(sc, 0x0005a4);
1053         if (error != 0)
1054                 goto fail;
1055
1056         for (i = 0; i < nitems(rfini2); i++) {
1057                 error = zyd_rfwrite(sc, rfini2[i]);
1058                 if (error != 0)
1059                         goto fail;
1060         }
1061
1062         for (i = 0; i < nitems(phypll); i++)
1063                 zyd_write16_m(sc, phypll[i].reg, phypll[i].val);
1064
1065         for (i = 0; i < nitems(rfini3); i++) {
1066                 error = zyd_rfwrite(sc, rfini3[i]);
1067                 if (error != 0)
1068                         goto fail;
1069         }
1070 fail:
1071         return (error);
1072 }
1073
1074 static int
1075 zyd_al2230_fini(struct zyd_rf *rf)
1076 {
1077         int error, i;
1078         struct zyd_softc *sc = rf->rf_sc;
1079         static const struct zyd_phy_pair phy[] = ZYD_AL2230_PHY_FINI_PART1;
1080
1081         for (i = 0; i < nitems(phy); i++)
1082                 zyd_write16_m(sc, phy[i].reg, phy[i].val);
1083
1084         if (sc->sc_newphy != 0)
1085                 zyd_write16_m(sc, ZYD_CR9, 0xe1);
1086
1087         zyd_write16_m(sc, ZYD_CR203, 0x6);
1088 fail:
1089         return (error);
1090 }
1091
1092 static int
1093 zyd_al2230_init_b(struct zyd_rf *rf)
1094 {
1095         struct zyd_softc *sc = rf->rf_sc;
1096         static const struct zyd_phy_pair phy1[] = ZYD_AL2230_PHY_PART1;
1097         static const struct zyd_phy_pair phy2[] = ZYD_AL2230_PHY_PART2;
1098         static const struct zyd_phy_pair phy3[] = ZYD_AL2230_PHY_PART3;
1099         static const struct zyd_phy_pair phy2230s[] = ZYD_AL2230S_PHY_INIT;
1100         static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY_B;
1101         static const uint32_t rfini_part1[] = ZYD_AL2230_RF_B_PART1;
1102         static const uint32_t rfini_part2[] = ZYD_AL2230_RF_B_PART2;
1103         static const uint32_t rfini_part3[] = ZYD_AL2230_RF_B_PART3;
1104         static const uint32_t zyd_al2230_chtable[][3] = ZYD_AL2230_CHANTABLE;
1105         int i, error;
1106
1107         for (i = 0; i < nitems(phy1); i++)
1108                 zyd_write16_m(sc, phy1[i].reg, phy1[i].val);
1109
1110         /* init RF-dependent PHY registers */
1111         for (i = 0; i < nitems(phyini); i++)
1112                 zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1113
1114         if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0) {
1115                 for (i = 0; i < nitems(phy2230s); i++)
1116                         zyd_write16_m(sc, phy2230s[i].reg, phy2230s[i].val);
1117         }
1118
1119         for (i = 0; i < 3; i++) {
1120                 error = zyd_rfwrite_cr(sc, zyd_al2230_chtable[0][i]);
1121                 if (error != 0)
1122                         return (error);
1123         }
1124
1125         for (i = 0; i < nitems(rfini_part1); i++) {
1126                 error = zyd_rfwrite_cr(sc, rfini_part1[i]);
1127                 if (error != 0)
1128                         return (error);
1129         }
1130
1131         if (sc->sc_rfrev == ZYD_RF_AL2230S || sc->sc_al2230s != 0)
1132                 error = zyd_rfwrite(sc, 0x241000);
1133         else
1134                 error = zyd_rfwrite(sc, 0x25a000);
1135         if (error != 0)
1136                 goto fail;
1137
1138         for (i = 0; i < nitems(rfini_part2); i++) {
1139                 error = zyd_rfwrite_cr(sc, rfini_part2[i]);
1140                 if (error != 0)
1141                         return (error);
1142         }
1143
1144         for (i = 0; i < nitems(phy2); i++)
1145                 zyd_write16_m(sc, phy2[i].reg, phy2[i].val);
1146
1147         for (i = 0; i < nitems(rfini_part3); i++) {
1148                 error = zyd_rfwrite_cr(sc, rfini_part3[i]);
1149                 if (error != 0)
1150                         return (error);
1151         }
1152
1153         for (i = 0; i < nitems(phy3); i++)
1154                 zyd_write16_m(sc, phy3[i].reg, phy3[i].val);
1155
1156         error = zyd_al2230_fini(rf);
1157 fail:
1158         return (error);
1159 }
1160
1161 static int
1162 zyd_al2230_switch_radio(struct zyd_rf *rf, int on)
1163 {
1164         struct zyd_softc *sc = rf->rf_sc;
1165         int error, on251 = (sc->sc_macrev == ZYD_ZD1211) ? 0x3f : 0x7f;
1166
1167         zyd_write16_m(sc, ZYD_CR11,  on ? 0x00 : 0x04);
1168         zyd_write16_m(sc, ZYD_CR251, on ? on251 : 0x2f);
1169 fail:
1170         return (error);
1171 }
1172
1173 static int
1174 zyd_al2230_set_channel(struct zyd_rf *rf, uint8_t chan)
1175 {
1176         int error, i;
1177         struct zyd_softc *sc = rf->rf_sc;
1178         static const struct zyd_phy_pair phy1[] = {
1179                 { ZYD_CR138, 0x28 }, { ZYD_CR203, 0x06 },
1180         };
1181         static const struct {
1182                 uint32_t        r1, r2, r3;
1183         } rfprog[] = ZYD_AL2230_CHANTABLE;
1184
1185         error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
1186         if (error != 0)
1187                 goto fail;
1188         error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
1189         if (error != 0)
1190                 goto fail;
1191         error = zyd_rfwrite(sc, rfprog[chan - 1].r3);
1192         if (error != 0)
1193                 goto fail;
1194
1195         for (i = 0; i < nitems(phy1); i++)
1196                 zyd_write16_m(sc, phy1[i].reg, phy1[i].val);
1197 fail:
1198         return (error);
1199 }
1200
1201 static int
1202 zyd_al2230_set_channel_b(struct zyd_rf *rf, uint8_t chan)
1203 {
1204         int error, i;
1205         struct zyd_softc *sc = rf->rf_sc;
1206         static const struct zyd_phy_pair phy1[] = ZYD_AL2230_PHY_PART1;
1207         static const struct {
1208                 uint32_t        r1, r2, r3;
1209         } rfprog[] = ZYD_AL2230_CHANTABLE_B;
1210
1211         for (i = 0; i < nitems(phy1); i++)
1212                 zyd_write16_m(sc, phy1[i].reg, phy1[i].val);
1213
1214         error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r1);
1215         if (error != 0)
1216                 goto fail;
1217         error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r2);
1218         if (error != 0)
1219                 goto fail;
1220         error = zyd_rfwrite_cr(sc, rfprog[chan - 1].r3);
1221         if (error != 0)
1222                 goto fail;
1223         error = zyd_al2230_fini(rf);
1224 fail:
1225         return (error);
1226 }
1227
1228 #define ZYD_AL2230_PHY_BANDEDGE6                                        \
1229 {                                                                       \
1230         { ZYD_CR128, 0x14 }, { ZYD_CR129, 0x12 }, { ZYD_CR130, 0x10 },  \
1231         { ZYD_CR47,  0x1e }                                             \
1232 }
1233
1234 static int
1235 zyd_al2230_bandedge6(struct zyd_rf *rf, struct ieee80211_channel *c)
1236 {
1237         int error = 0, i;
1238         struct zyd_softc *sc = rf->rf_sc;
1239         struct ieee80211com *ic = &sc->sc_ic;
1240         struct zyd_phy_pair r[] = ZYD_AL2230_PHY_BANDEDGE6;
1241         int chan = ieee80211_chan2ieee(ic, c);
1242
1243         if (chan == 1 || chan == 11)
1244                 r[0].val = 0x12;
1245         
1246         for (i = 0; i < nitems(r); i++)
1247                 zyd_write16_m(sc, r[i].reg, r[i].val);
1248 fail:
1249         return (error);
1250 }
1251
1252 /*
1253  * AL7230B RF methods.
1254  */
1255 static int
1256 zyd_al7230B_init(struct zyd_rf *rf)
1257 {
1258         struct zyd_softc *sc = rf->rf_sc;
1259         static const struct zyd_phy_pair phyini_1[] = ZYD_AL7230B_PHY_1;
1260         static const struct zyd_phy_pair phyini_2[] = ZYD_AL7230B_PHY_2;
1261         static const struct zyd_phy_pair phyini_3[] = ZYD_AL7230B_PHY_3;
1262         static const uint32_t rfini_1[] = ZYD_AL7230B_RF_1;
1263         static const uint32_t rfini_2[] = ZYD_AL7230B_RF_2;
1264         int i, error;
1265
1266         /* for AL7230B, PHY and RF need to be initialized in "phases" */
1267
1268         /* init RF-dependent PHY registers, part one */
1269         for (i = 0; i < nitems(phyini_1); i++)
1270                 zyd_write16_m(sc, phyini_1[i].reg, phyini_1[i].val);
1271
1272         /* init AL7230B radio, part one */
1273         for (i = 0; i < nitems(rfini_1); i++) {
1274                 if ((error = zyd_rfwrite(sc, rfini_1[i])) != 0)
1275                         return (error);
1276         }
1277         /* init RF-dependent PHY registers, part two */
1278         for (i = 0; i < nitems(phyini_2); i++)
1279                 zyd_write16_m(sc, phyini_2[i].reg, phyini_2[i].val);
1280
1281         /* init AL7230B radio, part two */
1282         for (i = 0; i < nitems(rfini_2); i++) {
1283                 if ((error = zyd_rfwrite(sc, rfini_2[i])) != 0)
1284                         return (error);
1285         }
1286         /* init RF-dependent PHY registers, part three */
1287         for (i = 0; i < nitems(phyini_3); i++)
1288                 zyd_write16_m(sc, phyini_3[i].reg, phyini_3[i].val);
1289 fail:
1290         return (error);
1291 }
1292
1293 static int
1294 zyd_al7230B_switch_radio(struct zyd_rf *rf, int on)
1295 {
1296         int error;
1297         struct zyd_softc *sc = rf->rf_sc;
1298
1299         zyd_write16_m(sc, ZYD_CR11,  on ? 0x00 : 0x04);
1300         zyd_write16_m(sc, ZYD_CR251, on ? 0x3f : 0x2f);
1301 fail:
1302         return (error);
1303 }
1304
1305 static int
1306 zyd_al7230B_set_channel(struct zyd_rf *rf, uint8_t chan)
1307 {
1308         struct zyd_softc *sc = rf->rf_sc;
1309         static const struct {
1310                 uint32_t        r1, r2;
1311         } rfprog[] = ZYD_AL7230B_CHANTABLE;
1312         static const uint32_t rfsc[] = ZYD_AL7230B_RF_SETCHANNEL;
1313         int i, error;
1314
1315         zyd_write16_m(sc, ZYD_CR240, 0x57);
1316         zyd_write16_m(sc, ZYD_CR251, 0x2f);
1317
1318         for (i = 0; i < nitems(rfsc); i++) {
1319                 if ((error = zyd_rfwrite(sc, rfsc[i])) != 0)
1320                         return (error);
1321         }
1322
1323         zyd_write16_m(sc, ZYD_CR128, 0x14);
1324         zyd_write16_m(sc, ZYD_CR129, 0x12);
1325         zyd_write16_m(sc, ZYD_CR130, 0x10);
1326         zyd_write16_m(sc, ZYD_CR38,  0x38);
1327         zyd_write16_m(sc, ZYD_CR136, 0xdf);
1328
1329         error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
1330         if (error != 0)
1331                 goto fail;
1332         error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
1333         if (error != 0)
1334                 goto fail;
1335         error = zyd_rfwrite(sc, 0x3c9000);
1336         if (error != 0)
1337                 goto fail;
1338
1339         zyd_write16_m(sc, ZYD_CR251, 0x3f);
1340         zyd_write16_m(sc, ZYD_CR203, 0x06);
1341         zyd_write16_m(sc, ZYD_CR240, 0x08);
1342 fail:
1343         return (error);
1344 }
1345
1346 /*
1347  * AL2210 RF methods.
1348  */
1349 static int
1350 zyd_al2210_init(struct zyd_rf *rf)
1351 {
1352         struct zyd_softc *sc = rf->rf_sc;
1353         static const struct zyd_phy_pair phyini[] = ZYD_AL2210_PHY;
1354         static const uint32_t rfini[] = ZYD_AL2210_RF;
1355         uint32_t tmp;
1356         int i, error;
1357
1358         zyd_write32_m(sc, ZYD_CR18, 2);
1359
1360         /* init RF-dependent PHY registers */
1361         for (i = 0; i < nitems(phyini); i++)
1362                 zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1363
1364         /* init AL2210 radio */
1365         for (i = 0; i < nitems(rfini); i++) {
1366                 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1367                         return (error);
1368         }
1369         zyd_write16_m(sc, ZYD_CR47, 0x1e);
1370         zyd_read32_m(sc, ZYD_CR_RADIO_PD, &tmp);
1371         zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp & ~1);
1372         zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp | 1);
1373         zyd_write32_m(sc, ZYD_CR_RFCFG, 0x05);
1374         zyd_write32_m(sc, ZYD_CR_RFCFG, 0x00);
1375         zyd_write16_m(sc, ZYD_CR47, 0x1e);
1376         zyd_write32_m(sc, ZYD_CR18, 3);
1377 fail:
1378         return (error);
1379 }
1380
1381 static int
1382 zyd_al2210_switch_radio(struct zyd_rf *rf, int on)
1383 {
1384         /* vendor driver does nothing for this RF chip */
1385
1386         return (0);
1387 }
1388
1389 static int
1390 zyd_al2210_set_channel(struct zyd_rf *rf, uint8_t chan)
1391 {
1392         int error;
1393         struct zyd_softc *sc = rf->rf_sc;
1394         static const uint32_t rfprog[] = ZYD_AL2210_CHANTABLE;
1395         uint32_t tmp;
1396
1397         zyd_write32_m(sc, ZYD_CR18, 2);
1398         zyd_write16_m(sc, ZYD_CR47, 0x1e);
1399         zyd_read32_m(sc, ZYD_CR_RADIO_PD, &tmp);
1400         zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp & ~1);
1401         zyd_write32_m(sc, ZYD_CR_RADIO_PD, tmp | 1);
1402         zyd_write32_m(sc, ZYD_CR_RFCFG, 0x05);
1403         zyd_write32_m(sc, ZYD_CR_RFCFG, 0x00);
1404         zyd_write16_m(sc, ZYD_CR47, 0x1e);
1405
1406         /* actually set the channel */
1407         error = zyd_rfwrite(sc, rfprog[chan - 1]);
1408         if (error != 0)
1409                 goto fail;
1410
1411         zyd_write32_m(sc, ZYD_CR18, 3);
1412 fail:
1413         return (error);
1414 }
1415
1416 /*
1417  * GCT RF methods.
1418  */
1419 static int
1420 zyd_gct_init(struct zyd_rf *rf)
1421 {
1422 #define ZYD_GCT_INTR_REG        0x85c1
1423         struct zyd_softc *sc = rf->rf_sc;
1424         static const struct zyd_phy_pair phyini[] = ZYD_GCT_PHY;
1425         static const uint32_t rfini[] = ZYD_GCT_RF;
1426         static const uint16_t vco[11][7] = ZYD_GCT_VCO;
1427         int i, idx = -1, error;
1428         uint16_t data;
1429
1430         /* init RF-dependent PHY registers */
1431         for (i = 0; i < nitems(phyini); i++)
1432                 zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1433
1434         /* init cgt radio */
1435         for (i = 0; i < nitems(rfini); i++) {
1436                 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1437                         return (error);
1438         }
1439
1440         error = zyd_gct_mode(rf);
1441         if (error != 0)
1442                 return (error);
1443
1444         for (i = 0; i < (int)(nitems(vco) - 1); i++) {
1445                 error = zyd_gct_set_channel_synth(rf, 1, 0);
1446                 if (error != 0)
1447                         goto fail;
1448                 error = zyd_gct_write(rf, vco[i][0]);
1449                 if (error != 0)
1450                         goto fail;
1451                 zyd_write16_m(sc, ZYD_GCT_INTR_REG, 0xf);
1452                 zyd_read16_m(sc, ZYD_GCT_INTR_REG, &data);
1453                 if ((data & 0xf) == 0) {
1454                         idx = i;
1455                         break;
1456                 }
1457         }
1458         if (idx == -1) {
1459                 error = zyd_gct_set_channel_synth(rf, 1, 1);
1460                 if (error != 0)
1461                         goto fail;
1462                 error = zyd_gct_write(rf, 0x6662);
1463                 if (error != 0)
1464                         goto fail;
1465         }
1466
1467         rf->idx = idx;
1468         zyd_write16_m(sc, ZYD_CR203, 0x6);
1469 fail:
1470         return (error);
1471 #undef ZYD_GCT_INTR_REG
1472 }
1473
1474 static int
1475 zyd_gct_mode(struct zyd_rf *rf)
1476 {
1477         struct zyd_softc *sc = rf->rf_sc;
1478         static const uint32_t mode[] = {
1479                 0x25f98, 0x25f9a, 0x25f94, 0x27fd4
1480         };
1481         int i, error;
1482
1483         for (i = 0; i < nitems(mode); i++) {
1484                 if ((error = zyd_rfwrite(sc, mode[i])) != 0)
1485                         break;
1486         }
1487         return (error);
1488 }
1489
1490 static int
1491 zyd_gct_set_channel_synth(struct zyd_rf *rf, int chan, int acal)
1492 {
1493         int error, idx = chan - 1;
1494         struct zyd_softc *sc = rf->rf_sc;
1495         static uint32_t acal_synth[] = ZYD_GCT_CHANNEL_ACAL;
1496         static uint32_t std_synth[] = ZYD_GCT_CHANNEL_STD;
1497         static uint32_t div_synth[] = ZYD_GCT_CHANNEL_DIV;
1498
1499         error = zyd_rfwrite(sc,
1500             (acal == 1) ? acal_synth[idx] : std_synth[idx]);
1501         if (error != 0)
1502                 return (error);
1503         return zyd_rfwrite(sc, div_synth[idx]);
1504 }
1505
1506 static int
1507 zyd_gct_write(struct zyd_rf *rf, uint16_t value)
1508 {
1509         struct zyd_softc *sc = rf->rf_sc;
1510
1511         return zyd_rfwrite(sc, 0x300000 | 0x40000 | value);
1512 }
1513
1514 static int
1515 zyd_gct_switch_radio(struct zyd_rf *rf, int on)
1516 {
1517         int error;
1518         struct zyd_softc *sc = rf->rf_sc;
1519
1520         error = zyd_rfwrite(sc, on ? 0x25f94 : 0x25f90);
1521         if (error != 0)
1522                 return (error);
1523
1524         zyd_write16_m(sc, ZYD_CR11, on ? 0x00 : 0x04);
1525         zyd_write16_m(sc, ZYD_CR251,
1526             on ? ((sc->sc_macrev == ZYD_ZD1211B) ? 0x7f : 0x3f) : 0x2f);
1527 fail:
1528         return (error);
1529 }
1530
1531 static int
1532 zyd_gct_set_channel(struct zyd_rf *rf, uint8_t chan)
1533 {
1534         int error, i;
1535         struct zyd_softc *sc = rf->rf_sc;
1536         static const struct zyd_phy_pair cmd[] = {
1537                 { ZYD_CR80, 0x30 }, { ZYD_CR81, 0x30 }, { ZYD_CR79, 0x58 },
1538                 { ZYD_CR12, 0xf0 }, { ZYD_CR77, 0x1b }, { ZYD_CR78, 0x58 },
1539         };
1540         static const uint16_t vco[11][7] = ZYD_GCT_VCO;
1541
1542         error = zyd_gct_set_channel_synth(rf, chan, 0);
1543         if (error != 0)
1544                 goto fail;
1545         error = zyd_gct_write(rf, (rf->idx == -1) ? 0x6662 :
1546             vco[rf->idx][((chan - 1) / 2)]);
1547         if (error != 0)
1548                 goto fail;
1549         error = zyd_gct_mode(rf);
1550         if (error != 0)
1551                 return (error);
1552         for (i = 0; i < nitems(cmd); i++)
1553                 zyd_write16_m(sc, cmd[i].reg, cmd[i].val);
1554         error = zyd_gct_txgain(rf, chan);
1555         if (error != 0)
1556                 return (error);
1557         zyd_write16_m(sc, ZYD_CR203, 0x6);
1558 fail:
1559         return (error);
1560 }
1561
1562 static int
1563 zyd_gct_txgain(struct zyd_rf *rf, uint8_t chan)
1564 {
1565         struct zyd_softc *sc = rf->rf_sc;
1566         static uint32_t txgain[] = ZYD_GCT_TXGAIN;
1567         uint8_t idx = sc->sc_pwrint[chan - 1];
1568
1569         if (idx >= nitems(txgain)) {
1570                 device_printf(sc->sc_dev, "could not set TX gain (%d %#x)\n",
1571                     chan, idx);
1572                 return 0;
1573         }
1574
1575         return zyd_rfwrite(sc, 0x700000 | txgain[idx]);
1576 }
1577
1578 /*
1579  * Maxim2 RF methods.
1580  */
1581 static int
1582 zyd_maxim2_init(struct zyd_rf *rf)
1583 {
1584         struct zyd_softc *sc = rf->rf_sc;
1585         static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY;
1586         static const uint32_t rfini[] = ZYD_MAXIM2_RF;
1587         uint16_t tmp;
1588         int i, error;
1589
1590         /* init RF-dependent PHY registers */
1591         for (i = 0; i < nitems(phyini); i++)
1592                 zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1593
1594         zyd_read16_m(sc, ZYD_CR203, &tmp);
1595         zyd_write16_m(sc, ZYD_CR203, tmp & ~(1 << 4));
1596
1597         /* init maxim2 radio */
1598         for (i = 0; i < nitems(rfini); i++) {
1599                 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1600                         return (error);
1601         }
1602         zyd_read16_m(sc, ZYD_CR203, &tmp);
1603         zyd_write16_m(sc, ZYD_CR203, tmp | (1 << 4));
1604 fail:
1605         return (error);
1606 }
1607
1608 static int
1609 zyd_maxim2_switch_radio(struct zyd_rf *rf, int on)
1610 {
1611
1612         /* vendor driver does nothing for this RF chip */
1613         return (0);
1614 }
1615
1616 static int
1617 zyd_maxim2_set_channel(struct zyd_rf *rf, uint8_t chan)
1618 {
1619         struct zyd_softc *sc = rf->rf_sc;
1620         static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY;
1621         static const uint32_t rfini[] = ZYD_MAXIM2_RF;
1622         static const struct {
1623                 uint32_t        r1, r2;
1624         } rfprog[] = ZYD_MAXIM2_CHANTABLE;
1625         uint16_t tmp;
1626         int i, error;
1627
1628         /*
1629          * Do the same as we do when initializing it, except for the channel
1630          * values coming from the two channel tables.
1631          */
1632
1633         /* init RF-dependent PHY registers */
1634         for (i = 0; i < nitems(phyini); i++)
1635                 zyd_write16_m(sc, phyini[i].reg, phyini[i].val);
1636
1637         zyd_read16_m(sc, ZYD_CR203, &tmp);
1638         zyd_write16_m(sc, ZYD_CR203, tmp & ~(1 << 4));
1639
1640         /* first two values taken from the chantables */
1641         error = zyd_rfwrite(sc, rfprog[chan - 1].r1);
1642         if (error != 0)
1643                 goto fail;
1644         error = zyd_rfwrite(sc, rfprog[chan - 1].r2);
1645         if (error != 0)
1646                 goto fail;
1647
1648         /* init maxim2 radio - skipping the two first values */
1649         for (i = 2; i < nitems(rfini); i++) {
1650                 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1651                         return (error);
1652         }
1653         zyd_read16_m(sc, ZYD_CR203, &tmp);
1654         zyd_write16_m(sc, ZYD_CR203, tmp | (1 << 4));
1655 fail:
1656         return (error);
1657 }
1658
1659 static int
1660 zyd_rf_attach(struct zyd_softc *sc, uint8_t type)
1661 {
1662         struct zyd_rf *rf = &sc->sc_rf;
1663
1664         rf->rf_sc = sc;
1665         rf->update_pwr = 1;
1666
1667         switch (type) {
1668         case ZYD_RF_RFMD:
1669                 rf->init         = zyd_rfmd_init;
1670                 rf->switch_radio = zyd_rfmd_switch_radio;
1671                 rf->set_channel  = zyd_rfmd_set_channel;
1672                 rf->width        = 24;  /* 24-bit RF values */
1673                 break;
1674         case ZYD_RF_AL2230:
1675         case ZYD_RF_AL2230S:
1676                 if (sc->sc_macrev == ZYD_ZD1211B) {
1677                         rf->init = zyd_al2230_init_b;
1678                         rf->set_channel = zyd_al2230_set_channel_b;
1679                 } else {
1680                         rf->init = zyd_al2230_init;
1681                         rf->set_channel = zyd_al2230_set_channel;
1682                 }
1683                 rf->switch_radio = zyd_al2230_switch_radio;
1684                 rf->bandedge6    = zyd_al2230_bandedge6;
1685                 rf->width        = 24;  /* 24-bit RF values */
1686                 break;
1687         case ZYD_RF_AL7230B:
1688                 rf->init         = zyd_al7230B_init;
1689                 rf->switch_radio = zyd_al7230B_switch_radio;
1690                 rf->set_channel  = zyd_al7230B_set_channel;
1691                 rf->width        = 24;  /* 24-bit RF values */
1692                 break;
1693         case ZYD_RF_AL2210:
1694                 rf->init         = zyd_al2210_init;
1695                 rf->switch_radio = zyd_al2210_switch_radio;
1696                 rf->set_channel  = zyd_al2210_set_channel;
1697                 rf->width        = 24;  /* 24-bit RF values */
1698                 break;
1699         case ZYD_RF_MAXIM_NEW:
1700         case ZYD_RF_GCT:
1701                 rf->init         = zyd_gct_init;
1702                 rf->switch_radio = zyd_gct_switch_radio;
1703                 rf->set_channel  = zyd_gct_set_channel;
1704                 rf->width        = 24;  /* 24-bit RF values */
1705                 rf->update_pwr   = 0;
1706                 break;
1707         case ZYD_RF_MAXIM_NEW2:
1708                 rf->init         = zyd_maxim2_init;
1709                 rf->switch_radio = zyd_maxim2_switch_radio;
1710                 rf->set_channel  = zyd_maxim2_set_channel;
1711                 rf->width        = 18;  /* 18-bit RF values */
1712                 break;
1713         default:
1714                 device_printf(sc->sc_dev,
1715                     "sorry, radio \"%s\" is not supported yet\n",
1716                     zyd_rf_name(type));
1717                 return (EINVAL);
1718         }
1719         return (0);
1720 }
1721
1722 static const char *
1723 zyd_rf_name(uint8_t type)
1724 {
1725         static const char * const zyd_rfs[] = {
1726                 "unknown", "unknown", "UW2451",   "UCHIP",     "AL2230",
1727                 "AL7230B", "THETA",   "AL2210",   "MAXIM_NEW", "GCT",
1728                 "AL2230S",  "RALINK",  "INTERSIL", "RFMD",      "MAXIM_NEW2",
1729                 "PHILIPS"
1730         };
1731
1732         return zyd_rfs[(type > 15) ? 0 : type];
1733 }
1734
1735 static int
1736 zyd_hw_init(struct zyd_softc *sc)
1737 {
1738         int error;
1739         const struct zyd_phy_pair *phyp;
1740         struct zyd_rf *rf = &sc->sc_rf;
1741         uint16_t val;
1742
1743         /* specify that the plug and play is finished */
1744         zyd_write32_m(sc, ZYD_MAC_AFTER_PNP, 1);
1745         zyd_read16_m(sc, ZYD_FIRMWARE_BASE_ADDR, &sc->sc_fwbase);
1746         DPRINTF(sc, ZYD_DEBUG_FW, "firmware base address=0x%04x\n",
1747             sc->sc_fwbase);
1748
1749         /* retrieve firmware revision number */
1750         zyd_read16_m(sc, sc->sc_fwbase + ZYD_FW_FIRMWARE_REV, &sc->sc_fwrev);
1751         zyd_write32_m(sc, ZYD_CR_GPI_EN, 0);
1752         zyd_write32_m(sc, ZYD_MAC_CONT_WIN_LIMIT, 0x7f043f);
1753         /* set mandatory rates - XXX assumes 802.11b/g */
1754         zyd_write32_m(sc, ZYD_MAC_MAN_RATE, 0x150f);
1755
1756         /* disable interrupts */
1757         zyd_write32_m(sc, ZYD_CR_INTERRUPT, 0);
1758
1759         if ((error = zyd_read_pod(sc)) != 0) {
1760                 device_printf(sc->sc_dev, "could not read EEPROM\n");
1761                 goto fail;
1762         }
1763
1764         /* PHY init (resetting) */
1765         error = zyd_lock_phy(sc);
1766         if (error != 0)
1767                 goto fail;
1768         phyp = (sc->sc_macrev == ZYD_ZD1211B) ? zyd_def_phyB : zyd_def_phy;
1769         for (; phyp->reg != 0; phyp++)
1770                 zyd_write16_m(sc, phyp->reg, phyp->val);
1771         if (sc->sc_macrev == ZYD_ZD1211 && sc->sc_fix_cr157 != 0) {
1772                 zyd_read16_m(sc, ZYD_EEPROM_PHY_REG, &val);
1773                 zyd_write32_m(sc, ZYD_CR157, val >> 8);
1774         }
1775         error = zyd_unlock_phy(sc);
1776         if (error != 0)
1777                 goto fail;
1778
1779         /* HMAC init */
1780         zyd_write32_m(sc, ZYD_MAC_ACK_EXT, 0x00000020);
1781         zyd_write32_m(sc, ZYD_CR_ADDA_MBIAS_WT, 0x30000808);
1782         zyd_write32_m(sc, ZYD_MAC_SNIFFER, 0x00000000);
1783         zyd_write32_m(sc, ZYD_MAC_RXFILTER, 0x00000000);
1784         zyd_write32_m(sc, ZYD_MAC_GHTBL, 0x00000000);
1785         zyd_write32_m(sc, ZYD_MAC_GHTBH, 0x80000000);
1786         zyd_write32_m(sc, ZYD_MAC_MISC, 0x000000a4);
1787         zyd_write32_m(sc, ZYD_CR_ADDA_PWR_DWN, 0x0000007f);
1788         zyd_write32_m(sc, ZYD_MAC_BCNCFG, 0x00f00401);
1789         zyd_write32_m(sc, ZYD_MAC_PHY_DELAY2, 0x00000000);
1790         zyd_write32_m(sc, ZYD_MAC_ACK_EXT, 0x00000080);
1791         zyd_write32_m(sc, ZYD_CR_ADDA_PWR_DWN, 0x00000000);
1792         zyd_write32_m(sc, ZYD_MAC_SIFS_ACK_TIME, 0x00000100);
1793         zyd_write32_m(sc, ZYD_CR_RX_PE_DELAY, 0x00000070);
1794         zyd_write32_m(sc, ZYD_CR_PS_CTRL, 0x10000000);
1795         zyd_write32_m(sc, ZYD_MAC_RTSCTSRATE, 0x02030203);
1796         zyd_write32_m(sc, ZYD_MAC_AFTER_PNP, 1);
1797         zyd_write32_m(sc, ZYD_MAC_BACKOFF_PROTECT, 0x00000114);
1798         zyd_write32_m(sc, ZYD_MAC_DIFS_EIFS_SIFS, 0x0a47c032);
1799         zyd_write32_m(sc, ZYD_MAC_CAM_MODE, 0x3);
1800
1801         if (sc->sc_macrev == ZYD_ZD1211) {
1802                 zyd_write32_m(sc, ZYD_MAC_RETRY, 0x00000002);
1803                 zyd_write32_m(sc, ZYD_MAC_RX_THRESHOLD, 0x000c0640);
1804         } else {
1805                 zyd_write32_m(sc, ZYD_MACB_MAX_RETRY, 0x02020202);
1806                 zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL4, 0x007f003f);
1807                 zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL3, 0x007f003f);
1808                 zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL2, 0x003f001f);
1809                 zyd_write32_m(sc, ZYD_MACB_TXPWR_CTL1, 0x001f000f);
1810                 zyd_write32_m(sc, ZYD_MACB_AIFS_CTL1, 0x00280028);
1811                 zyd_write32_m(sc, ZYD_MACB_AIFS_CTL2, 0x008C003C);
1812                 zyd_write32_m(sc, ZYD_MACB_TXOP, 0x01800824);
1813                 zyd_write32_m(sc, ZYD_MAC_RX_THRESHOLD, 0x000c0eff);
1814         }
1815
1816         /* init beacon interval to 100ms */
1817         if ((error = zyd_set_beacon_interval(sc, 100)) != 0)
1818                 goto fail;
1819
1820         if ((error = zyd_rf_attach(sc, sc->sc_rfrev)) != 0) {
1821                 device_printf(sc->sc_dev, "could not attach RF, rev 0x%x\n",
1822                     sc->sc_rfrev);
1823                 goto fail;
1824         }
1825
1826         /* RF chip init */
1827         error = zyd_lock_phy(sc);
1828         if (error != 0)
1829                 goto fail;
1830         error = (*rf->init)(rf);
1831         if (error != 0) {
1832                 device_printf(sc->sc_dev,
1833                     "radio initialization failed, error %d\n", error);
1834                 goto fail;
1835         }
1836         error = zyd_unlock_phy(sc);
1837         if (error != 0)
1838                 goto fail;
1839
1840         if ((error = zyd_read_eeprom(sc)) != 0) {
1841                 device_printf(sc->sc_dev, "could not read EEPROM\n");
1842                 goto fail;
1843         }
1844
1845 fail:   return (error);
1846 }
1847
1848 static int
1849 zyd_read_pod(struct zyd_softc *sc)
1850 {
1851         int error;
1852         uint32_t tmp;
1853
1854         zyd_read32_m(sc, ZYD_EEPROM_POD, &tmp);
1855         sc->sc_rfrev     = tmp & 0x0f;
1856         sc->sc_ledtype   = (tmp >>  4) & 0x01;
1857         sc->sc_al2230s   = (tmp >>  7) & 0x01;
1858         sc->sc_cckgain   = (tmp >>  8) & 0x01;
1859         sc->sc_fix_cr157 = (tmp >> 13) & 0x01;
1860         sc->sc_parev     = (tmp >> 16) & 0x0f;
1861         sc->sc_bandedge6 = (tmp >> 21) & 0x01;
1862         sc->sc_newphy    = (tmp >> 31) & 0x01;
1863         sc->sc_txled     = ((tmp & (1 << 24)) && (tmp & (1 << 29))) ? 0 : 1;
1864 fail:
1865         return (error);
1866 }
1867
1868 static int
1869 zyd_read_eeprom(struct zyd_softc *sc)
1870 {
1871         uint16_t val;
1872         int error, i;
1873
1874         /* read Tx power calibration tables */
1875         for (i = 0; i < 7; i++) {
1876                 zyd_read16_m(sc, ZYD_EEPROM_PWR_CAL + i, &val);
1877                 sc->sc_pwrcal[i * 2] = val >> 8;
1878                 sc->sc_pwrcal[i * 2 + 1] = val & 0xff;
1879                 zyd_read16_m(sc, ZYD_EEPROM_PWR_INT + i, &val);
1880                 sc->sc_pwrint[i * 2] = val >> 8;
1881                 sc->sc_pwrint[i * 2 + 1] = val & 0xff;
1882                 zyd_read16_m(sc, ZYD_EEPROM_36M_CAL + i, &val);
1883                 sc->sc_ofdm36_cal[i * 2] = val >> 8;
1884                 sc->sc_ofdm36_cal[i * 2 + 1] = val & 0xff;
1885                 zyd_read16_m(sc, ZYD_EEPROM_48M_CAL + i, &val);
1886                 sc->sc_ofdm48_cal[i * 2] = val >> 8;
1887                 sc->sc_ofdm48_cal[i * 2 + 1] = val & 0xff;
1888                 zyd_read16_m(sc, ZYD_EEPROM_54M_CAL + i, &val);
1889                 sc->sc_ofdm54_cal[i * 2] = val >> 8;
1890                 sc->sc_ofdm54_cal[i * 2 + 1] = val & 0xff;
1891         }
1892 fail:
1893         return (error);
1894 }
1895
1896 static int
1897 zyd_get_macaddr(struct zyd_softc *sc)
1898 {
1899         struct usb_device_request req;
1900         usb_error_t error;
1901
1902         req.bmRequestType = UT_READ_VENDOR_DEVICE;
1903         req.bRequest = ZYD_READFWDATAREQ;
1904         USETW(req.wValue, ZYD_EEPROM_MAC_ADDR_P1);
1905         USETW(req.wIndex, 0);
1906         USETW(req.wLength, IEEE80211_ADDR_LEN);
1907
1908         error = zyd_do_request(sc, &req, sc->sc_ic.ic_macaddr);
1909         if (error != 0) {
1910                 device_printf(sc->sc_dev, "could not read EEPROM: %s\n",
1911                     usbd_errstr(error));
1912         }
1913
1914         return (error);
1915 }
1916
1917 static int
1918 zyd_set_macaddr(struct zyd_softc *sc, const uint8_t *addr)
1919 {
1920         int error;
1921         uint32_t tmp;
1922
1923         tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
1924         zyd_write32_m(sc, ZYD_MAC_MACADRL, tmp);
1925         tmp = addr[5] << 8 | addr[4];
1926         zyd_write32_m(sc, ZYD_MAC_MACADRH, tmp);
1927 fail:
1928         return (error);
1929 }
1930
1931 static int
1932 zyd_set_bssid(struct zyd_softc *sc, const uint8_t *addr)
1933 {
1934         int error;
1935         uint32_t tmp;
1936
1937         tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
1938         zyd_write32_m(sc, ZYD_MAC_BSSADRL, tmp);
1939         tmp = addr[5] << 8 | addr[4];
1940         zyd_write32_m(sc, ZYD_MAC_BSSADRH, tmp);
1941 fail:
1942         return (error);
1943 }
1944
1945 static int
1946 zyd_switch_radio(struct zyd_softc *sc, int on)
1947 {
1948         struct zyd_rf *rf = &sc->sc_rf;
1949         int error;
1950
1951         error = zyd_lock_phy(sc);
1952         if (error != 0)
1953                 goto fail;
1954         error = (*rf->switch_radio)(rf, on);
1955         if (error != 0)
1956                 goto fail;
1957         error = zyd_unlock_phy(sc);
1958 fail:
1959         return (error);
1960 }
1961
1962 static int
1963 zyd_set_led(struct zyd_softc *sc, int which, int on)
1964 {
1965         int error;
1966         uint32_t tmp;
1967
1968         zyd_read32_m(sc, ZYD_MAC_TX_PE_CONTROL, &tmp);
1969         tmp &= ~which;
1970         if (on)
1971                 tmp |= which;
1972         zyd_write32_m(sc, ZYD_MAC_TX_PE_CONTROL, tmp);
1973 fail:
1974         return (error);
1975 }
1976
1977 static void
1978 zyd_set_multi(struct zyd_softc *sc)
1979 {
1980         struct ieee80211com *ic = &sc->sc_ic;
1981         uint32_t low, high;
1982         int error;
1983
1984         if ((sc->sc_flags & ZYD_FLAG_RUNNING) == 0)
1985                 return;
1986
1987         low = 0x00000000;
1988         high = 0x80000000;
1989
1990         if (ic->ic_opmode == IEEE80211_M_MONITOR || ic->ic_allmulti > 0 ||
1991             ic->ic_promisc > 0) {
1992                 low = 0xffffffff;
1993                 high = 0xffffffff;
1994         } else {
1995                 struct ieee80211vap *vap;
1996                 struct ifnet *ifp;
1997                 struct ifmultiaddr *ifma;
1998                 uint8_t v;
1999
2000                 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
2001                         ifp = vap->iv_ifp;
2002                         if_maddr_rlock(ifp);
2003                         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2004                                 if (ifma->ifma_addr->sa_family != AF_LINK)
2005                                         continue;
2006                                 v = ((uint8_t *)LLADDR((struct sockaddr_dl *)
2007                                     ifma->ifma_addr))[5] >> 2;
2008                                 if (v < 32)
2009                                         low |= 1 << v;
2010                                 else
2011                                         high |= 1 << (v - 32);
2012                         }
2013                         if_maddr_runlock(ifp);
2014                 }
2015         }
2016
2017         /* reprogram multicast global hash table */
2018         zyd_write32_m(sc, ZYD_MAC_GHTBL, low);
2019         zyd_write32_m(sc, ZYD_MAC_GHTBH, high);
2020 fail:
2021         if (error != 0)
2022                 device_printf(sc->sc_dev,
2023                     "could not set multicast hash table\n");
2024 }
2025
2026 static void
2027 zyd_update_mcast(struct ieee80211com *ic)
2028 {
2029         struct zyd_softc *sc = ic->ic_softc;
2030
2031         ZYD_LOCK(sc);
2032         zyd_set_multi(sc);
2033         ZYD_UNLOCK(sc);
2034 }
2035
2036 static int
2037 zyd_set_rxfilter(struct zyd_softc *sc)
2038 {
2039         struct ieee80211com *ic = &sc->sc_ic;
2040         uint32_t rxfilter;
2041
2042         switch (ic->ic_opmode) {
2043         case IEEE80211_M_STA:
2044                 rxfilter = ZYD_FILTER_BSS;
2045                 break;
2046         case IEEE80211_M_IBSS:
2047         case IEEE80211_M_HOSTAP:
2048                 rxfilter = ZYD_FILTER_HOSTAP;
2049                 break;
2050         case IEEE80211_M_MONITOR:
2051                 rxfilter = ZYD_FILTER_MONITOR;
2052                 break;
2053         default:
2054                 /* should not get there */
2055                 return (EINVAL);
2056         }
2057         return zyd_write32(sc, ZYD_MAC_RXFILTER, rxfilter);
2058 }
2059
2060 static void
2061 zyd_set_chan(struct zyd_softc *sc, struct ieee80211_channel *c)
2062 {
2063         int error;
2064         struct ieee80211com *ic = &sc->sc_ic;
2065         struct zyd_rf *rf = &sc->sc_rf;
2066         uint32_t tmp;
2067         int chan;
2068
2069         chan = ieee80211_chan2ieee(ic, c);
2070         if (chan == 0 || chan == IEEE80211_CHAN_ANY) {
2071                 /* XXX should NEVER happen */
2072                 device_printf(sc->sc_dev,
2073                     "%s: invalid channel %x\n", __func__, chan);
2074                 return;
2075         }
2076
2077         error = zyd_lock_phy(sc);
2078         if (error != 0)
2079                 goto fail;
2080
2081         error = (*rf->set_channel)(rf, chan);
2082         if (error != 0)
2083                 goto fail;
2084
2085         if (rf->update_pwr) {
2086                 /* update Tx power */
2087                 zyd_write16_m(sc, ZYD_CR31, sc->sc_pwrint[chan - 1]);
2088
2089                 if (sc->sc_macrev == ZYD_ZD1211B) {
2090                         zyd_write16_m(sc, ZYD_CR67,
2091                             sc->sc_ofdm36_cal[chan - 1]);
2092                         zyd_write16_m(sc, ZYD_CR66,
2093                             sc->sc_ofdm48_cal[chan - 1]);
2094                         zyd_write16_m(sc, ZYD_CR65,
2095                             sc->sc_ofdm54_cal[chan - 1]);
2096                         zyd_write16_m(sc, ZYD_CR68, sc->sc_pwrcal[chan - 1]);
2097                         zyd_write16_m(sc, ZYD_CR69, 0x28);
2098                         zyd_write16_m(sc, ZYD_CR69, 0x2a);
2099                 }
2100         }
2101         if (sc->sc_cckgain) {
2102                 /* set CCK baseband gain from EEPROM */
2103                 if (zyd_read32(sc, ZYD_EEPROM_PHY_REG, &tmp) == 0)
2104                         zyd_write16_m(sc, ZYD_CR47, tmp & 0xff);
2105         }
2106         if (sc->sc_bandedge6 && rf->bandedge6 != NULL) {
2107                 error = (*rf->bandedge6)(rf, c);
2108                 if (error != 0)
2109                         goto fail;
2110         }
2111         zyd_write32_m(sc, ZYD_CR_CONFIG_PHILIPS, 0);
2112
2113         error = zyd_unlock_phy(sc);
2114         if (error != 0)
2115                 goto fail;
2116
2117         sc->sc_rxtap.wr_chan_freq = sc->sc_txtap.wt_chan_freq =
2118             htole16(c->ic_freq);
2119         sc->sc_rxtap.wr_chan_flags = sc->sc_txtap.wt_chan_flags =
2120             htole16(c->ic_flags);
2121 fail:
2122         return;
2123 }
2124
2125 static int
2126 zyd_set_beacon_interval(struct zyd_softc *sc, int bintval)
2127 {
2128         int error;
2129         uint32_t val;
2130
2131         zyd_read32_m(sc, ZYD_CR_ATIM_WND_PERIOD, &val);
2132         sc->sc_atim_wnd = val;
2133         zyd_read32_m(sc, ZYD_CR_PRE_TBTT, &val);
2134         sc->sc_pre_tbtt = val;
2135         sc->sc_bcn_int = bintval;
2136
2137         if (sc->sc_bcn_int <= 5)
2138                 sc->sc_bcn_int = 5;
2139         if (sc->sc_pre_tbtt < 4 || sc->sc_pre_tbtt >= sc->sc_bcn_int)
2140                 sc->sc_pre_tbtt = sc->sc_bcn_int - 1;
2141         if (sc->sc_atim_wnd >= sc->sc_pre_tbtt)
2142                 sc->sc_atim_wnd = sc->sc_pre_tbtt - 1;
2143
2144         zyd_write32_m(sc, ZYD_CR_ATIM_WND_PERIOD, sc->sc_atim_wnd);
2145         zyd_write32_m(sc, ZYD_CR_PRE_TBTT, sc->sc_pre_tbtt);
2146         zyd_write32_m(sc, ZYD_CR_BCN_INTERVAL, sc->sc_bcn_int);
2147 fail:
2148         return (error);
2149 }
2150
2151 static void
2152 zyd_rx_data(struct usb_xfer *xfer, int offset, uint16_t len)
2153 {
2154         struct zyd_softc *sc = usbd_xfer_softc(xfer);
2155         struct ieee80211com *ic = &sc->sc_ic;
2156         struct zyd_plcphdr plcp;
2157         struct zyd_rx_stat stat;
2158         struct usb_page_cache *pc;
2159         struct mbuf *m;
2160         int rlen, rssi;
2161
2162         if (len < ZYD_MIN_FRAGSZ) {
2163                 DPRINTF(sc, ZYD_DEBUG_RECV, "%s: frame too short (length=%d)\n",
2164                     device_get_nameunit(sc->sc_dev), len);
2165                 counter_u64_add(ic->ic_ierrors, 1);
2166                 return;
2167         }
2168         pc = usbd_xfer_get_frame(xfer, 0);
2169         usbd_copy_out(pc, offset, &plcp, sizeof(plcp));
2170         usbd_copy_out(pc, offset + len - sizeof(stat), &stat, sizeof(stat));
2171
2172         if (stat.flags & ZYD_RX_ERROR) {
2173                 DPRINTF(sc, ZYD_DEBUG_RECV,
2174                     "%s: RX status indicated error (%x)\n",
2175                     device_get_nameunit(sc->sc_dev), stat.flags);
2176                 counter_u64_add(ic->ic_ierrors, 1);
2177                 return;
2178         }
2179
2180         /* compute actual frame length */
2181         rlen = len - sizeof(struct zyd_plcphdr) -
2182             sizeof(struct zyd_rx_stat) - IEEE80211_CRC_LEN;
2183
2184         /* allocate a mbuf to store the frame */
2185         if (rlen > (int)MCLBYTES) {
2186                 DPRINTF(sc, ZYD_DEBUG_RECV, "%s: frame too long (length=%d)\n",
2187                     device_get_nameunit(sc->sc_dev), rlen);
2188                 counter_u64_add(ic->ic_ierrors, 1);
2189                 return;
2190         } else if (rlen > (int)MHLEN)
2191                 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2192         else
2193                 m = m_gethdr(M_NOWAIT, MT_DATA);
2194         if (m == NULL) {
2195                 DPRINTF(sc, ZYD_DEBUG_RECV, "%s: could not allocate rx mbuf\n",
2196                     device_get_nameunit(sc->sc_dev));
2197                 counter_u64_add(ic->ic_ierrors, 1);
2198                 return;
2199         }
2200         m->m_pkthdr.len = m->m_len = rlen;
2201         usbd_copy_out(pc, offset + sizeof(plcp), mtod(m, uint8_t *), rlen);
2202
2203         if (ieee80211_radiotap_active(ic)) {
2204                 struct zyd_rx_radiotap_header *tap = &sc->sc_rxtap;
2205
2206                 tap->wr_flags = 0;
2207                 if (stat.flags & (ZYD_RX_BADCRC16 | ZYD_RX_BADCRC32))
2208                         tap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
2209                 /* XXX toss, no way to express errors */
2210                 if (stat.flags & ZYD_RX_DECRYPTERR)
2211                         tap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
2212                 tap->wr_rate = ieee80211_plcp2rate(plcp.signal,
2213                     (stat.flags & ZYD_RX_OFDM) ?
2214                         IEEE80211_T_OFDM : IEEE80211_T_CCK);
2215                 tap->wr_antsignal = stat.rssi + -95;
2216                 tap->wr_antnoise = -95; /* XXX */
2217         }
2218         rssi = (stat.rssi > 63) ? 127 : 2 * stat.rssi;
2219
2220         sc->sc_rx_data[sc->sc_rx_count].rssi = rssi;
2221         sc->sc_rx_data[sc->sc_rx_count].m = m;
2222         sc->sc_rx_count++;
2223 }
2224
2225 static void
2226 zyd_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
2227 {
2228         struct zyd_softc *sc = usbd_xfer_softc(xfer);
2229         struct ieee80211com *ic = &sc->sc_ic;
2230         struct ieee80211_node *ni;
2231         struct zyd_rx_desc desc;
2232         struct mbuf *m;
2233         struct usb_page_cache *pc;
2234         uint32_t offset;
2235         uint8_t rssi;
2236         int8_t nf;
2237         int i;
2238         int actlen;
2239
2240         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
2241
2242         sc->sc_rx_count = 0;
2243         switch (USB_GET_STATE(xfer)) {
2244         case USB_ST_TRANSFERRED:
2245                 pc = usbd_xfer_get_frame(xfer, 0);
2246                 usbd_copy_out(pc, actlen - sizeof(desc), &desc, sizeof(desc));
2247
2248                 offset = 0;
2249                 if (UGETW(desc.tag) == ZYD_TAG_MULTIFRAME) {
2250                         DPRINTF(sc, ZYD_DEBUG_RECV,
2251                             "%s: received multi-frame transfer\n", __func__);
2252
2253                         for (i = 0; i < ZYD_MAX_RXFRAMECNT; i++) {
2254                                 uint16_t len16 = UGETW(desc.len[i]);
2255
2256                                 if (len16 == 0 || len16 > actlen)
2257                                         break;
2258
2259                                 zyd_rx_data(xfer, offset, len16);
2260
2261                                 /* next frame is aligned on a 32-bit boundary */
2262                                 len16 = (len16 + 3) & ~3;
2263                                 offset += len16;
2264                                 if (len16 > actlen)
2265                                         break;
2266                                 actlen -= len16;
2267                         }
2268                 } else {
2269                         DPRINTF(sc, ZYD_DEBUG_RECV,
2270                             "%s: received single-frame transfer\n", __func__);
2271
2272                         zyd_rx_data(xfer, 0, actlen);
2273                 }
2274                 /* FALLTHROUGH */
2275         case USB_ST_SETUP:
2276 tr_setup:
2277                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
2278                 usbd_transfer_submit(xfer);
2279
2280                 /*
2281                  * At the end of a USB callback it is always safe to unlock
2282                  * the private mutex of a device! That is why we do the
2283                  * "ieee80211_input" here, and not some lines up!
2284                  */
2285                 ZYD_UNLOCK(sc);
2286                 for (i = 0; i < sc->sc_rx_count; i++) {
2287                         rssi = sc->sc_rx_data[i].rssi;
2288                         m = sc->sc_rx_data[i].m;
2289                         sc->sc_rx_data[i].m = NULL;
2290
2291                         nf = -95;       /* XXX */
2292
2293                         ni = ieee80211_find_rxnode(ic,
2294                             mtod(m, struct ieee80211_frame_min *));
2295                         if (ni != NULL) {
2296                                 (void)ieee80211_input(ni, m, rssi, nf);
2297                                 ieee80211_free_node(ni);
2298                         } else
2299                                 (void)ieee80211_input_all(ic, m, rssi, nf);
2300                 }
2301                 ZYD_LOCK(sc);
2302                 zyd_start(sc);
2303                 break;
2304
2305         default:                        /* Error */
2306                 DPRINTF(sc, ZYD_DEBUG_ANY, "frame error: %s\n", usbd_errstr(error));
2307
2308                 if (error != USB_ERR_CANCELLED) {
2309                         /* try to clear stall first */
2310                         usbd_xfer_set_stall(xfer);
2311                         goto tr_setup;
2312                 }
2313                 break;
2314         }
2315 }
2316
2317 static uint8_t
2318 zyd_plcp_signal(struct zyd_softc *sc, int rate)
2319 {
2320         switch (rate) {
2321         /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
2322         case 12:
2323                 return (0xb);
2324         case 18:
2325                 return (0xf);
2326         case 24:
2327                 return (0xa);
2328         case 36:
2329                 return (0xe);
2330         case 48:
2331                 return (0x9);
2332         case 72:
2333                 return (0xd);
2334         case 96:
2335                 return (0x8);
2336         case 108:
2337                 return (0xc);
2338         /* CCK rates (NB: not IEEE std, device-specific) */
2339         case 2:
2340                 return (0x0);
2341         case 4:
2342                 return (0x1);
2343         case 11:
2344                 return (0x2);
2345         case 22:
2346                 return (0x3);
2347         }
2348
2349         device_printf(sc->sc_dev, "unsupported rate %d\n", rate);
2350         return (0x0);
2351 }
2352
2353 static void
2354 zyd_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
2355 {
2356         struct zyd_softc *sc = usbd_xfer_softc(xfer);
2357         struct ieee80211vap *vap;
2358         struct zyd_tx_data *data;
2359         struct mbuf *m;
2360         struct usb_page_cache *pc;
2361         int actlen;
2362
2363         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
2364
2365         switch (USB_GET_STATE(xfer)) {
2366         case USB_ST_TRANSFERRED:
2367                 DPRINTF(sc, ZYD_DEBUG_ANY, "transfer complete, %u bytes\n",
2368                     actlen);
2369
2370                 /* free resources */
2371                 data = usbd_xfer_get_priv(xfer);
2372                 zyd_tx_free(data, 0);
2373                 usbd_xfer_set_priv(xfer, NULL);
2374
2375                 /* FALLTHROUGH */
2376         case USB_ST_SETUP:
2377 tr_setup:
2378                 data = STAILQ_FIRST(&sc->tx_q);
2379                 if (data) {
2380                         STAILQ_REMOVE_HEAD(&sc->tx_q, next);
2381                         m = data->m;
2382
2383                         if (m->m_pkthdr.len > (int)ZYD_MAX_TXBUFSZ) {
2384                                 DPRINTF(sc, ZYD_DEBUG_ANY, "data overflow, %u bytes\n",
2385                                     m->m_pkthdr.len);
2386                                 m->m_pkthdr.len = ZYD_MAX_TXBUFSZ;
2387                         }
2388                         pc = usbd_xfer_get_frame(xfer, 0);
2389                         usbd_copy_in(pc, 0, &data->desc, ZYD_TX_DESC_SIZE);
2390                         usbd_m_copy_in(pc, ZYD_TX_DESC_SIZE, m, 0,
2391                             m->m_pkthdr.len);
2392
2393                         vap = data->ni->ni_vap;
2394                         if (ieee80211_radiotap_active_vap(vap)) {
2395                                 struct zyd_tx_radiotap_header *tap = &sc->sc_txtap;
2396
2397                                 tap->wt_flags = 0;
2398                                 tap->wt_rate = data->rate;
2399
2400                                 ieee80211_radiotap_tx(vap, m);
2401                         }
2402
2403                         usbd_xfer_set_frame_len(xfer, 0, ZYD_TX_DESC_SIZE + m->m_pkthdr.len);
2404                         usbd_xfer_set_priv(xfer, data);
2405                         usbd_transfer_submit(xfer);
2406                 }
2407                 zyd_start(sc);
2408                 break;
2409
2410         default:                        /* Error */
2411                 DPRINTF(sc, ZYD_DEBUG_ANY, "transfer error, %s\n",
2412                     usbd_errstr(error));
2413
2414                 counter_u64_add(sc->sc_ic.ic_oerrors, 1);
2415                 data = usbd_xfer_get_priv(xfer);
2416                 usbd_xfer_set_priv(xfer, NULL);
2417                 if (data != NULL)
2418                         zyd_tx_free(data, error);
2419
2420                 if (error != USB_ERR_CANCELLED) {
2421                         if (error == USB_ERR_TIMEOUT)
2422                                 device_printf(sc->sc_dev, "device timeout\n");
2423
2424                         /*
2425                          * Try to clear stall first, also if other
2426                          * errors occur, hence clearing stall
2427                          * introduces a 50 ms delay:
2428                          */
2429                         usbd_xfer_set_stall(xfer);
2430                         goto tr_setup;
2431                 }
2432                 break;
2433         }
2434 }
2435
2436 static int
2437 zyd_tx_start(struct zyd_softc *sc, struct mbuf *m0, struct ieee80211_node *ni)
2438 {
2439         struct ieee80211vap *vap = ni->ni_vap;
2440         struct ieee80211com *ic = ni->ni_ic;
2441         struct zyd_tx_desc *desc;
2442         struct zyd_tx_data *data;
2443         struct ieee80211_frame *wh;
2444         const struct ieee80211_txparam *tp = ni->ni_txparms;
2445         struct ieee80211_key *k;
2446         int rate, totlen, type, ismcast;
2447         static const uint8_t ratediv[] = ZYD_TX_RATEDIV;
2448         uint8_t phy;
2449         uint16_t pktlen;
2450         uint32_t bits;
2451
2452         wh = mtod(m0, struct ieee80211_frame *);
2453         data = STAILQ_FIRST(&sc->tx_free);
2454         STAILQ_REMOVE_HEAD(&sc->tx_free, next);
2455         sc->tx_nfree--;
2456
2457         ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
2458         type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
2459
2460         if (type == IEEE80211_FC0_TYPE_MGT ||
2461             type == IEEE80211_FC0_TYPE_CTL ||
2462             (m0->m_flags & M_EAPOL) != 0) {
2463                 rate = tp->mgmtrate;
2464         } else {
2465                 /* for data frames */
2466                 if (ismcast)
2467                         rate = tp->mcastrate;
2468                 else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
2469                         rate = tp->ucastrate;
2470                 else {
2471                         (void) ieee80211_ratectl_rate(ni, NULL, 0);
2472                         rate = ni->ni_txrate;
2473                 }
2474         }
2475
2476         if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
2477                 k = ieee80211_crypto_encap(ni, m0);
2478                 if (k == NULL) {
2479                         return (ENOBUFS);
2480                 }
2481                 /* packet header may have moved, reset our local pointer */
2482                 wh = mtod(m0, struct ieee80211_frame *);
2483         }
2484
2485         data->ni = ni;
2486         data->m = m0;
2487         data->rate = rate;
2488
2489         /* fill Tx descriptor */
2490         desc = &data->desc;
2491         phy = zyd_plcp_signal(sc, rate);
2492         desc->phy = phy;
2493         if (ZYD_RATE_IS_OFDM(rate)) {
2494                 desc->phy |= ZYD_TX_PHY_OFDM;
2495                 if (IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan))
2496                         desc->phy |= ZYD_TX_PHY_5GHZ;
2497         } else if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
2498                 desc->phy |= ZYD_TX_PHY_SHPREAMBLE;
2499
2500         totlen = m0->m_pkthdr.len + IEEE80211_CRC_LEN;
2501         desc->len = htole16(totlen);
2502
2503         desc->flags = ZYD_TX_FLAG_BACKOFF;
2504         if (!ismcast) {
2505                 /* multicast frames are not sent at OFDM rates in 802.11b/g */
2506                 if (totlen > vap->iv_rtsthreshold) {
2507                         desc->flags |= ZYD_TX_FLAG_RTS;
2508                 } else if (ZYD_RATE_IS_OFDM(rate) &&
2509                     (ic->ic_flags & IEEE80211_F_USEPROT)) {
2510                         if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
2511                                 desc->flags |= ZYD_TX_FLAG_CTS_TO_SELF;
2512                         else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
2513                                 desc->flags |= ZYD_TX_FLAG_RTS;
2514                 }
2515         } else
2516                 desc->flags |= ZYD_TX_FLAG_MULTICAST;
2517         if ((wh->i_fc[0] &
2518             (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
2519             (IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_PS_POLL))
2520                 desc->flags |= ZYD_TX_FLAG_TYPE(ZYD_TX_TYPE_PS_POLL);
2521
2522         /* actual transmit length (XXX why +10?) */
2523         pktlen = ZYD_TX_DESC_SIZE + 10;
2524         if (sc->sc_macrev == ZYD_ZD1211)
2525                 pktlen += totlen;
2526         desc->pktlen = htole16(pktlen);
2527
2528         bits = (rate == 11) ? (totlen * 16) + 10 :
2529             ((rate == 22) ? (totlen * 8) + 10 : (totlen * 8));
2530         desc->plcp_length = htole16(bits / ratediv[phy]);
2531         desc->plcp_service = 0;
2532         if (rate == 22 && (bits % 11) > 0 && (bits % 11) <= 3)
2533                 desc->plcp_service |= ZYD_PLCP_LENGEXT;
2534         desc->nextlen = 0;
2535
2536         if (ieee80211_radiotap_active_vap(vap)) {
2537                 struct zyd_tx_radiotap_header *tap = &sc->sc_txtap;
2538
2539                 tap->wt_flags = 0;
2540                 tap->wt_rate = rate;
2541
2542                 ieee80211_radiotap_tx(vap, m0);
2543         }
2544
2545         DPRINTF(sc, ZYD_DEBUG_XMIT,
2546             "%s: sending data frame len=%zu rate=%u\n",
2547             device_get_nameunit(sc->sc_dev), (size_t)m0->m_pkthdr.len,
2548                 rate);
2549
2550         STAILQ_INSERT_TAIL(&sc->tx_q, data, next);
2551         usbd_transfer_start(sc->sc_xfer[ZYD_BULK_WR]);
2552
2553         return (0);
2554 }
2555
2556 static int
2557 zyd_transmit(struct ieee80211com *ic, struct mbuf *m)
2558 {
2559         struct zyd_softc *sc = ic->ic_softc;
2560         int error;
2561
2562         ZYD_LOCK(sc);
2563         if ((sc->sc_flags & ZYD_FLAG_RUNNING) == 0) {
2564                 ZYD_UNLOCK(sc);
2565                 return (ENXIO);
2566         }
2567         error = mbufq_enqueue(&sc->sc_snd, m);
2568         if (error) {
2569                 ZYD_UNLOCK(sc);
2570                 return (error);
2571         }
2572         zyd_start(sc);
2573         ZYD_UNLOCK(sc);
2574
2575         return (0);
2576 }
2577
2578 static void
2579 zyd_start(struct zyd_softc *sc)
2580 {
2581         struct ieee80211_node *ni;
2582         struct mbuf *m;
2583
2584         ZYD_LOCK_ASSERT(sc, MA_OWNED);
2585
2586         while (sc->tx_nfree > 0 && (m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
2587                 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
2588                 if (zyd_tx_start(sc, m, ni) != 0) {
2589                         m_freem(m);
2590                         if_inc_counter(ni->ni_vap->iv_ifp,
2591                             IFCOUNTER_OERRORS, 1);
2592                         ieee80211_free_node(ni);
2593                         break;
2594                 }
2595         }
2596 }
2597
2598 static int
2599 zyd_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2600         const struct ieee80211_bpf_params *params)
2601 {
2602         struct ieee80211com *ic = ni->ni_ic;
2603         struct zyd_softc *sc = ic->ic_softc;
2604
2605         ZYD_LOCK(sc);
2606         /* prevent management frames from being sent if we're not ready */
2607         if (!(sc->sc_flags & ZYD_FLAG_RUNNING)) {
2608                 ZYD_UNLOCK(sc);
2609                 m_freem(m);
2610                 return (ENETDOWN);
2611         }
2612         if (sc->tx_nfree == 0) {
2613                 ZYD_UNLOCK(sc);
2614                 m_freem(m);
2615                 return (ENOBUFS);               /* XXX */
2616         }
2617
2618         /*
2619          * Legacy path; interpret frame contents to decide
2620          * precisely how to send the frame.
2621          * XXX raw path
2622          */
2623         if (zyd_tx_start(sc, m, ni) != 0) {
2624                 ZYD_UNLOCK(sc);
2625                 m_freem(m);
2626                 return (EIO);
2627         }
2628         ZYD_UNLOCK(sc);
2629         return (0);
2630 }
2631
2632 static void
2633 zyd_parent(struct ieee80211com *ic)
2634 {
2635         struct zyd_softc *sc = ic->ic_softc;
2636         int startall = 0;
2637
2638         ZYD_LOCK(sc);
2639         if (sc->sc_flags & ZYD_FLAG_DETACHED) {
2640                 ZYD_UNLOCK(sc);
2641                 return;
2642         }
2643         if (ic->ic_nrunning > 0) {
2644                 if ((sc->sc_flags & ZYD_FLAG_RUNNING) == 0) {
2645                         zyd_init_locked(sc);
2646                         startall = 1;
2647                 } else
2648                         zyd_set_multi(sc);
2649         } else if (sc->sc_flags & ZYD_FLAG_RUNNING)
2650                 zyd_stop(sc);
2651         ZYD_UNLOCK(sc);
2652         if (startall)
2653                 ieee80211_start_all(ic);
2654 }
2655
2656 static void
2657 zyd_init_locked(struct zyd_softc *sc)
2658 {
2659         struct ieee80211com *ic = &sc->sc_ic;
2660         struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2661         struct usb_config_descriptor *cd;
2662         int error;
2663         uint32_t val;
2664
2665         ZYD_LOCK_ASSERT(sc, MA_OWNED);
2666
2667         if (!(sc->sc_flags & ZYD_FLAG_INITONCE)) {
2668                 error = zyd_loadfirmware(sc);
2669                 if (error != 0) {
2670                         device_printf(sc->sc_dev,
2671                             "could not load firmware (error=%d)\n", error);
2672                         goto fail;
2673                 }
2674
2675                 /* reset device */
2676                 cd = usbd_get_config_descriptor(sc->sc_udev);
2677                 error = usbd_req_set_config(sc->sc_udev, &sc->sc_mtx,
2678                     cd->bConfigurationValue);
2679                 if (error)
2680                         device_printf(sc->sc_dev, "reset failed, continuing\n");
2681
2682                 error = zyd_hw_init(sc);
2683                 if (error) {
2684                         device_printf(sc->sc_dev,
2685                             "hardware initialization failed\n");
2686                         goto fail;
2687                 }
2688
2689                 device_printf(sc->sc_dev,
2690                     "HMAC ZD1211%s, FW %02x.%02x, RF %s S%x, PA%x LED %x "
2691                     "BE%x NP%x Gain%x F%x\n",
2692                     (sc->sc_macrev == ZYD_ZD1211) ? "": "B",
2693                     sc->sc_fwrev >> 8, sc->sc_fwrev & 0xff,
2694                     zyd_rf_name(sc->sc_rfrev), sc->sc_al2230s, sc->sc_parev,
2695                     sc->sc_ledtype, sc->sc_bandedge6, sc->sc_newphy,
2696                     sc->sc_cckgain, sc->sc_fix_cr157);
2697
2698                 /* read regulatory domain (currently unused) */
2699                 zyd_read32_m(sc, ZYD_EEPROM_SUBID, &val);
2700                 sc->sc_regdomain = val >> 16;
2701                 DPRINTF(sc, ZYD_DEBUG_INIT, "regulatory domain %x\n",
2702                     sc->sc_regdomain);
2703
2704                 /* we'll do software WEP decryption for now */
2705                 DPRINTF(sc, ZYD_DEBUG_INIT, "%s: setting encryption type\n",
2706                     __func__);
2707                 zyd_write32_m(sc, ZYD_MAC_ENCRYPTION_TYPE, ZYD_ENC_SNIFFER);
2708
2709                 sc->sc_flags |= ZYD_FLAG_INITONCE;
2710         }
2711
2712         if (sc->sc_flags & ZYD_FLAG_RUNNING)
2713                 zyd_stop(sc);
2714
2715         DPRINTF(sc, ZYD_DEBUG_INIT, "setting MAC address to %6D\n",
2716             vap ? vap->iv_myaddr : ic->ic_macaddr, ":");
2717         error = zyd_set_macaddr(sc, vap ? vap->iv_myaddr : ic->ic_macaddr);
2718         if (error != 0)
2719                 return;
2720
2721         /* set basic rates */
2722         if (ic->ic_curmode == IEEE80211_MODE_11B)
2723                 zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0x0003);
2724         else if (ic->ic_curmode == IEEE80211_MODE_11A)
2725                 zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0x1500);
2726         else    /* assumes 802.11b/g */
2727                 zyd_write32_m(sc, ZYD_MAC_BAS_RATE, 0xff0f);
2728
2729         /* promiscuous mode */
2730         zyd_write32_m(sc, ZYD_MAC_SNIFFER, 0);
2731         /* multicast setup */
2732         zyd_set_multi(sc);
2733         /* set RX filter  */
2734         error = zyd_set_rxfilter(sc);
2735         if (error != 0)
2736                 goto fail;
2737
2738         /* switch radio transmitter ON */
2739         error = zyd_switch_radio(sc, 1);
2740         if (error != 0)
2741                 goto fail;
2742         /* set default BSS channel */
2743         zyd_set_chan(sc, ic->ic_curchan);
2744
2745         /*
2746          * Allocate Tx and Rx xfer queues.
2747          */
2748         zyd_setup_tx_list(sc);
2749
2750         /* enable interrupts */
2751         zyd_write32_m(sc, ZYD_CR_INTERRUPT, ZYD_HWINT_MASK);
2752
2753         sc->sc_flags |= ZYD_FLAG_RUNNING;
2754         usbd_xfer_set_stall(sc->sc_xfer[ZYD_BULK_WR]);
2755         usbd_transfer_start(sc->sc_xfer[ZYD_BULK_RD]);
2756         usbd_transfer_start(sc->sc_xfer[ZYD_INTR_RD]);
2757
2758         return;
2759
2760 fail:   zyd_stop(sc);
2761         return;
2762 }
2763
2764 static void
2765 zyd_stop(struct zyd_softc *sc)
2766 {
2767         int error;
2768
2769         ZYD_LOCK_ASSERT(sc, MA_OWNED);
2770
2771         sc->sc_flags &= ~ZYD_FLAG_RUNNING;
2772         zyd_drain_mbufq(sc);
2773
2774         /*
2775          * Drain all the transfers, if not already drained:
2776          */
2777         ZYD_UNLOCK(sc);
2778         usbd_transfer_drain(sc->sc_xfer[ZYD_BULK_WR]);
2779         usbd_transfer_drain(sc->sc_xfer[ZYD_BULK_RD]);
2780         ZYD_LOCK(sc);
2781
2782         zyd_unsetup_tx_list(sc);
2783
2784         /* Stop now if the device was never set up */
2785         if (!(sc->sc_flags & ZYD_FLAG_INITONCE))
2786                 return;
2787
2788         /* switch radio transmitter OFF */
2789         error = zyd_switch_radio(sc, 0);
2790         if (error != 0)
2791                 goto fail;
2792         /* disable Rx */
2793         zyd_write32_m(sc, ZYD_MAC_RXFILTER, 0);
2794         /* disable interrupts */
2795         zyd_write32_m(sc, ZYD_CR_INTERRUPT, 0);
2796
2797 fail:
2798         return;
2799 }
2800
2801 static int
2802 zyd_loadfirmware(struct zyd_softc *sc)
2803 {
2804         struct usb_device_request req;
2805         size_t size;
2806         u_char *fw;
2807         uint8_t stat;
2808         uint16_t addr;
2809
2810         if (sc->sc_flags & ZYD_FLAG_FWLOADED)
2811                 return (0);
2812
2813         if (sc->sc_macrev == ZYD_ZD1211) {
2814                 fw = (u_char *)zd1211_firmware;
2815                 size = sizeof(zd1211_firmware);
2816         } else {
2817                 fw = (u_char *)zd1211b_firmware;
2818                 size = sizeof(zd1211b_firmware);
2819         }
2820
2821         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
2822         req.bRequest = ZYD_DOWNLOADREQ;
2823         USETW(req.wIndex, 0);
2824
2825         addr = ZYD_FIRMWARE_START_ADDR;
2826         while (size > 0) {
2827                 /*
2828                  * When the transfer size is 4096 bytes, it is not
2829                  * likely to be able to transfer it.
2830                  * The cause is port or machine or chip?
2831                  */
2832                 const int mlen = min(size, 64);
2833
2834                 DPRINTF(sc, ZYD_DEBUG_FW,
2835                     "loading firmware block: len=%d, addr=0x%x\n", mlen, addr);
2836
2837                 USETW(req.wValue, addr);
2838                 USETW(req.wLength, mlen);
2839                 if (zyd_do_request(sc, &req, fw) != 0)
2840                         return (EIO);
2841
2842                 addr += mlen / 2;
2843                 fw   += mlen;
2844                 size -= mlen;
2845         }
2846
2847         /* check whether the upload succeeded */
2848         req.bmRequestType = UT_READ_VENDOR_DEVICE;
2849         req.bRequest = ZYD_DOWNLOADSTS;
2850         USETW(req.wValue, 0);
2851         USETW(req.wIndex, 0);
2852         USETW(req.wLength, sizeof(stat));
2853         if (zyd_do_request(sc, &req, &stat) != 0)
2854                 return (EIO);
2855
2856         sc->sc_flags |= ZYD_FLAG_FWLOADED;
2857
2858         return (stat & 0x80) ? (EIO) : (0);
2859 }
2860
2861 static void
2862 zyd_scan_start(struct ieee80211com *ic)
2863 {
2864         struct zyd_softc *sc = ic->ic_softc;
2865
2866         ZYD_LOCK(sc);
2867         /* want broadcast address while scanning */
2868         zyd_set_bssid(sc, ieee80211broadcastaddr);
2869         ZYD_UNLOCK(sc);
2870 }
2871
2872 static void
2873 zyd_scan_end(struct ieee80211com *ic)
2874 {
2875         struct zyd_softc *sc = ic->ic_softc;
2876
2877         ZYD_LOCK(sc);
2878         /* restore previous bssid */
2879         zyd_set_bssid(sc, sc->sc_bssid);
2880         ZYD_UNLOCK(sc);
2881 }
2882
2883 static void
2884 zyd_getradiocaps(struct ieee80211com *ic,
2885     int maxchans, int *nchans, struct ieee80211_channel chans[])
2886 {
2887         uint8_t bands[IEEE80211_MODE_BYTES];
2888
2889         memset(bands, 0, sizeof(bands));
2890         setbit(bands, IEEE80211_MODE_11B);
2891         setbit(bands, IEEE80211_MODE_11G);
2892         ieee80211_add_channel_list_2ghz(chans, maxchans, nchans,
2893             zyd_chan_2ghz, nitems(zyd_chan_2ghz), bands, 0);
2894 }
2895
2896 static void
2897 zyd_set_channel(struct ieee80211com *ic)
2898 {
2899         struct zyd_softc *sc = ic->ic_softc;
2900
2901         ZYD_LOCK(sc);
2902         zyd_set_chan(sc, ic->ic_curchan);
2903         ZYD_UNLOCK(sc);
2904 }
2905
2906 static device_method_t zyd_methods[] = {
2907         /* Device interface */
2908         DEVMETHOD(device_probe, zyd_match),
2909         DEVMETHOD(device_attach, zyd_attach),
2910         DEVMETHOD(device_detach, zyd_detach),
2911         DEVMETHOD_END
2912 };
2913
2914 static driver_t zyd_driver = {
2915         .name = "zyd",
2916         .methods = zyd_methods,
2917         .size = sizeof(struct zyd_softc)
2918 };
2919
2920 static devclass_t zyd_devclass;
2921
2922 DRIVER_MODULE(zyd, uhub, zyd_driver, zyd_devclass, NULL, 0);
2923 MODULE_DEPEND(zyd, usb, 1, 1, 1);
2924 MODULE_DEPEND(zyd, wlan, 1, 1, 1);
2925 MODULE_VERSION(zyd, 1);
2926 USB_PNP_HOST_INFO(zyd_devs);