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