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