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