Sun Sep 23 14:40:29 2012 UTC ()
Don't use a usbd_status as an int, split error into two variables.


(joerg)
diff -r1.31 -r1.32 src/sys/dev/usb/if_zyd.c

cvs diff -r1.31 -r1.32 src/sys/dev/usb/if_zyd.c (switch to unified diff)

--- src/sys/dev/usb/if_zyd.c 2012/09/23 01:08:17 1.31
+++ src/sys/dev/usb/if_zyd.c 2012/09/23 14:40:29 1.32
@@ -1,1831 +1,1832 @@ @@ -1,1831 +1,1832 @@
1/* $OpenBSD: if_zyd.c,v 1.52 2007/02/11 00:08:04 jsg Exp $ */ 1/* $OpenBSD: if_zyd.c,v 1.52 2007/02/11 00:08:04 jsg Exp $ */
2/* $NetBSD: if_zyd.c,v 1.31 2012/09/23 01:08:17 chs Exp $ */ 2/* $NetBSD: if_zyd.c,v 1.32 2012/09/23 14:40:29 joerg Exp $ */
3 3
4/*- 4/*-
5 * Copyright (c) 2006 by Damien Bergamini <damien.bergamini@free.fr> 5 * Copyright (c) 2006 by Damien Bergamini <damien.bergamini@free.fr>
6 * Copyright (c) 2006 by Florian Stoehr <ich@florian-stoehr.de> 6 * Copyright (c) 2006 by Florian Stoehr <ich@florian-stoehr.de>
7 * 7 *
8 * Permission to use, copy, modify, and distribute this software for any 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 9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies. 10 * copyright notice and this permission notice appear in all copies.
11 * 11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 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 17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 */ 19 */
20 20
21/* 21/*
22 * ZyDAS ZD1211/ZD1211B USB WLAN driver. 22 * ZyDAS ZD1211/ZD1211B USB WLAN driver.
23 */ 23 */
24#include <sys/cdefs.h> 24#include <sys/cdefs.h>
25__KERNEL_RCSID(0, "$NetBSD: if_zyd.c,v 1.31 2012/09/23 01:08:17 chs Exp $"); 25__KERNEL_RCSID(0, "$NetBSD: if_zyd.c,v 1.32 2012/09/23 14:40:29 joerg Exp $");
26 26
27 27
28#include <sys/param.h> 28#include <sys/param.h>
29#include <sys/sockio.h> 29#include <sys/sockio.h>
30#include <sys/proc.h> 30#include <sys/proc.h>
31#include <sys/mbuf.h> 31#include <sys/mbuf.h>
32#include <sys/kernel.h> 32#include <sys/kernel.h>
33#include <sys/socket.h> 33#include <sys/socket.h>
34#include <sys/systm.h> 34#include <sys/systm.h>
35#include <sys/malloc.h> 35#include <sys/malloc.h>
36#include <sys/conf.h> 36#include <sys/conf.h>
37#include <sys/device.h> 37#include <sys/device.h>
38 38
39#include <sys/bus.h> 39#include <sys/bus.h>
40#include <machine/endian.h> 40#include <machine/endian.h>
41 41
42#include <net/bpf.h> 42#include <net/bpf.h>
43#include <net/if.h> 43#include <net/if.h>
44#include <net/if_arp.h> 44#include <net/if_arp.h>
45#include <net/if_dl.h> 45#include <net/if_dl.h>
46#include <net/if_ether.h> 46#include <net/if_ether.h>
47#include <net/if_media.h> 47#include <net/if_media.h>
48#include <net/if_types.h> 48#include <net/if_types.h>
49 49
50#include <netinet/in.h> 50#include <netinet/in.h>
51#include <netinet/in_systm.h> 51#include <netinet/in_systm.h>
52#include <netinet/in_var.h> 52#include <netinet/in_var.h>
53#include <netinet/ip.h> 53#include <netinet/ip.h>
54 54
55#include <net80211/ieee80211_netbsd.h> 55#include <net80211/ieee80211_netbsd.h>
56#include <net80211/ieee80211_var.h> 56#include <net80211/ieee80211_var.h>
57#include <net80211/ieee80211_amrr.h> 57#include <net80211/ieee80211_amrr.h>
58#include <net80211/ieee80211_radiotap.h> 58#include <net80211/ieee80211_radiotap.h>
59 59
60#include <dev/firmload.h> 60#include <dev/firmload.h>
61 61
62#include <dev/usb/usb.h> 62#include <dev/usb/usb.h>
63#include <dev/usb/usbdi.h> 63#include <dev/usb/usbdi.h>
64#include <dev/usb/usbdi_util.h> 64#include <dev/usb/usbdi_util.h>
65#include <dev/usb/usbdevs.h> 65#include <dev/usb/usbdevs.h>
66 66
67#include <dev/usb/if_zydreg.h> 67#include <dev/usb/if_zydreg.h>
68 68
69#ifdef USB_DEBUG 69#ifdef USB_DEBUG
70#define ZYD_DEBUG 70#define ZYD_DEBUG
71#endif 71#endif
72 72
73#ifdef ZYD_DEBUG 73#ifdef ZYD_DEBUG
74#define DPRINTF(x) do { if (zyddebug > 0) printf x; } while (0) 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) 75#define DPRINTFN(n, x) do { if (zyddebug > (n)) printf x; } while (0)
76int zyddebug = 0; 76int zyddebug = 0;
77#else 77#else
78#define DPRINTF(x) 78#define DPRINTF(x)
79#define DPRINTFN(n, x) 79#define DPRINTFN(n, x)
80#endif 80#endif
81 81
82static const struct zyd_phy_pair zyd_def_phy[] = ZYD_DEF_PHY; 82static const struct zyd_phy_pair zyd_def_phy[] = ZYD_DEF_PHY;
83static const struct zyd_phy_pair zyd_def_phyB[] = ZYD_DEF_PHYB; 83static const struct zyd_phy_pair zyd_def_phyB[] = ZYD_DEF_PHYB;
84 84
85/* various supported device vendors/products */ 85/* various supported device vendors/products */
86#define ZYD_ZD1211_DEV(v, p) \ 86#define ZYD_ZD1211_DEV(v, p) \
87 { { USB_VENDOR_##v, USB_PRODUCT_##v##_##p }, ZYD_ZD1211 } 87 { { USB_VENDOR_##v, USB_PRODUCT_##v##_##p }, ZYD_ZD1211 }
88#define ZYD_ZD1211B_DEV(v, p) \ 88#define ZYD_ZD1211B_DEV(v, p) \
89 { { USB_VENDOR_##v, USB_PRODUCT_##v##_##p }, ZYD_ZD1211B } 89 { { USB_VENDOR_##v, USB_PRODUCT_##v##_##p }, ZYD_ZD1211B }
90static const struct zyd_type { 90static const struct zyd_type {
91 struct usb_devno dev; 91 struct usb_devno dev;
92 uint8_t rev; 92 uint8_t rev;
93#define ZYD_ZD1211 0 93#define ZYD_ZD1211 0
94#define ZYD_ZD1211B 1 94#define ZYD_ZD1211B 1
95} zyd_devs[] = { 95} zyd_devs[] = {
96 ZYD_ZD1211_DEV(3COM2, 3CRUSB10075), 96 ZYD_ZD1211_DEV(3COM2, 3CRUSB10075),
97 ZYD_ZD1211_DEV(ABOCOM, WL54), 97 ZYD_ZD1211_DEV(ABOCOM, WL54),
98 ZYD_ZD1211_DEV(ASUSTEK, WL159G), 98 ZYD_ZD1211_DEV(ASUSTEK, WL159G),
99 ZYD_ZD1211_DEV(CYBERTAN, TG54USB), 99 ZYD_ZD1211_DEV(CYBERTAN, TG54USB),
100 ZYD_ZD1211_DEV(DRAYTEK, VIGOR550), 100 ZYD_ZD1211_DEV(DRAYTEK, VIGOR550),
101 ZYD_ZD1211_DEV(PLANEX2, GWUS54GD), 101 ZYD_ZD1211_DEV(PLANEX2, GWUS54GD),
102 ZYD_ZD1211_DEV(PLANEX2, GWUS54GZL), 102 ZYD_ZD1211_DEV(PLANEX2, GWUS54GZL),
103 ZYD_ZD1211_DEV(PLANEX3, GWUS54GZ), 103 ZYD_ZD1211_DEV(PLANEX3, GWUS54GZ),
104 ZYD_ZD1211_DEV(PLANEX3, GWUS54MINI), 104 ZYD_ZD1211_DEV(PLANEX3, GWUS54MINI),
105 ZYD_ZD1211_DEV(SAGEM, XG760A), 105 ZYD_ZD1211_DEV(SAGEM, XG760A),
106 ZYD_ZD1211_DEV(SENAO, NUB8301), 106 ZYD_ZD1211_DEV(SENAO, NUB8301),
107 ZYD_ZD1211_DEV(SITECOMEU, WL113), 107 ZYD_ZD1211_DEV(SITECOMEU, WL113),
108 ZYD_ZD1211_DEV(SWEEX, ZD1211), 108 ZYD_ZD1211_DEV(SWEEX, ZD1211),
109 ZYD_ZD1211_DEV(TEKRAM, QUICKWLAN), 109 ZYD_ZD1211_DEV(TEKRAM, QUICKWLAN),
110 ZYD_ZD1211_DEV(TEKRAM, ZD1211_1), 110 ZYD_ZD1211_DEV(TEKRAM, ZD1211_1),
111 ZYD_ZD1211_DEV(TEKRAM, ZD1211_2), 111 ZYD_ZD1211_DEV(TEKRAM, ZD1211_2),
112 ZYD_ZD1211_DEV(TWINMOS, G240), 112 ZYD_ZD1211_DEV(TWINMOS, G240),
113 ZYD_ZD1211_DEV(UMEDIA, ALL0298V2), 113 ZYD_ZD1211_DEV(UMEDIA, ALL0298V2),
114 ZYD_ZD1211_DEV(UMEDIA, TEW429UB_A), 114 ZYD_ZD1211_DEV(UMEDIA, TEW429UB_A),
115 ZYD_ZD1211_DEV(UMEDIA, TEW429UB), 115 ZYD_ZD1211_DEV(UMEDIA, TEW429UB),
116 ZYD_ZD1211_DEV(WISTRONNEWEB, UR055G), 116 ZYD_ZD1211_DEV(WISTRONNEWEB, UR055G),
117 ZYD_ZD1211_DEV(ZCOM, ZD1211), 117 ZYD_ZD1211_DEV(ZCOM, ZD1211),
118 ZYD_ZD1211_DEV(ZYDAS, ZD1211), 118 ZYD_ZD1211_DEV(ZYDAS, ZD1211),
119 ZYD_ZD1211_DEV(ZYXEL, AG225H), 119 ZYD_ZD1211_DEV(ZYXEL, AG225H),
120 ZYD_ZD1211_DEV(ZYXEL, ZYAIRG220), 120 ZYD_ZD1211_DEV(ZYXEL, ZYAIRG220),
121 ZYD_ZD1211_DEV(ZYXEL, G200V2), 121 ZYD_ZD1211_DEV(ZYXEL, G200V2),
122 122
123 ZYD_ZD1211B_DEV(ACCTON, SMCWUSBG), 123 ZYD_ZD1211B_DEV(ACCTON, SMCWUSBG),
124 ZYD_ZD1211B_DEV(ACCTON, WN4501H_LF_IR), 124 ZYD_ZD1211B_DEV(ACCTON, WN4501H_LF_IR),
125 ZYD_ZD1211B_DEV(ACCTON, WUS201), 125 ZYD_ZD1211B_DEV(ACCTON, WUS201),
126 ZYD_ZD1211B_DEV(ACCTON, ZD1211B), 126 ZYD_ZD1211B_DEV(ACCTON, ZD1211B),
127 ZYD_ZD1211B_DEV(ASUSTEK, A9T_WIFI), 127 ZYD_ZD1211B_DEV(ASUSTEK, A9T_WIFI),
128 ZYD_ZD1211B_DEV(BELKIN, F5D7050C), 128 ZYD_ZD1211B_DEV(BELKIN, F5D7050C),
129 ZYD_ZD1211B_DEV(BELKIN, ZD1211B), 129 ZYD_ZD1211B_DEV(BELKIN, ZD1211B),
130 ZYD_ZD1211B_DEV(BEWAN, BWIFI_USB54AR), 130 ZYD_ZD1211B_DEV(BEWAN, BWIFI_USB54AR),
131 ZYD_ZD1211B_DEV(CISCOLINKSYS, WUSBF54G), 131 ZYD_ZD1211B_DEV(CISCOLINKSYS, WUSBF54G),
132 ZYD_ZD1211B_DEV(CYBERTAN, ZD1211B), 132 ZYD_ZD1211B_DEV(CYBERTAN, ZD1211B),
133 ZYD_ZD1211B_DEV(FIBERLINE, WL430U), 133 ZYD_ZD1211B_DEV(FIBERLINE, WL430U),
134 ZYD_ZD1211B_DEV(MELCO, KG54L), 134 ZYD_ZD1211B_DEV(MELCO, KG54L),
135 ZYD_ZD1211B_DEV(PHILIPS, SNU5600), 135 ZYD_ZD1211B_DEV(PHILIPS, SNU5600),
136 ZYD_ZD1211B_DEV(PHILIPS, SNU5630NS05), 136 ZYD_ZD1211B_DEV(PHILIPS, SNU5630NS05),
137 ZYD_ZD1211B_DEV(PLANEX2, GWUS54GXS), 137 ZYD_ZD1211B_DEV(PLANEX2, GWUS54GXS),
138 ZYD_ZD1211B_DEV(SAGEM, XG76NA), 138 ZYD_ZD1211B_DEV(SAGEM, XG76NA),
139 ZYD_ZD1211B_DEV(SITECOMEU, WL603), 139 ZYD_ZD1211B_DEV(SITECOMEU, WL603),
140 ZYD_ZD1211B_DEV(SITECOMEU, ZD1211B), 140 ZYD_ZD1211B_DEV(SITECOMEU, ZD1211B),
141 ZYD_ZD1211B_DEV(SONY, IFU_WLM2), 141 ZYD_ZD1211B_DEV(SONY, IFU_WLM2),
142 ZYD_ZD1211B_DEV(UMEDIA, TEW429UBC1), 142 ZYD_ZD1211B_DEV(UMEDIA, TEW429UBC1),
143 ZYD_ZD1211B_DEV(UNKNOWN1, ZD1211B_1), 143 ZYD_ZD1211B_DEV(UNKNOWN1, ZD1211B_1),
144 ZYD_ZD1211B_DEV(UNKNOWN1, ZD1211B_2), 144 ZYD_ZD1211B_DEV(UNKNOWN1, ZD1211B_2),
145 ZYD_ZD1211B_DEV(UNKNOWN2, ZD1211B), 145 ZYD_ZD1211B_DEV(UNKNOWN2, ZD1211B),
146 ZYD_ZD1211B_DEV(UNKNOWN3, ZD1211B), 146 ZYD_ZD1211B_DEV(UNKNOWN3, ZD1211B),
147 ZYD_ZD1211B_DEV(USR, USR5423), 147 ZYD_ZD1211B_DEV(USR, USR5423),
148 ZYD_ZD1211B_DEV(VTECH, ZD1211B), 148 ZYD_ZD1211B_DEV(VTECH, ZD1211B),
149 ZYD_ZD1211B_DEV(ZCOM, ZD1211B), 149 ZYD_ZD1211B_DEV(ZCOM, ZD1211B),
150 ZYD_ZD1211B_DEV(ZYDAS, ZD1211B), 150 ZYD_ZD1211B_DEV(ZYDAS, ZD1211B),
151 ZYD_ZD1211B_DEV(ZYDAS, ZD1211B_2), 151 ZYD_ZD1211B_DEV(ZYDAS, ZD1211B_2),
152 ZYD_ZD1211B_DEV(ZYXEL, M202), 152 ZYD_ZD1211B_DEV(ZYXEL, M202),
153 ZYD_ZD1211B_DEV(ZYXEL, G220V2), 153 ZYD_ZD1211B_DEV(ZYXEL, G220V2),
154}; 154};
155#define zyd_lookup(v, p) \ 155#define zyd_lookup(v, p) \
156 ((const struct zyd_type *)usb_lookup(zyd_devs, v, p)) 156 ((const struct zyd_type *)usb_lookup(zyd_devs, v, p))
157 157
158int zyd_match(device_t, cfdata_t, void *); 158int zyd_match(device_t, cfdata_t, void *);
159void zyd_attach(device_t, device_t, void *); 159void zyd_attach(device_t, device_t, void *);
160int zyd_detach(device_t, int); 160int zyd_detach(device_t, int);
161int zyd_activate(device_t, enum devact); 161int zyd_activate(device_t, enum devact);
162extern struct cfdriver zyd_cd; 162extern struct cfdriver zyd_cd;
163 163
164CFATTACH_DECL_NEW(zyd, sizeof(struct zyd_softc), zyd_match, 164CFATTACH_DECL_NEW(zyd, sizeof(struct zyd_softc), zyd_match,
165 zyd_attach, zyd_detach, zyd_activate); 165 zyd_attach, zyd_detach, zyd_activate);
166 166
167Static void zyd_attachhook(device_t); 167Static void zyd_attachhook(device_t);
168Static int zyd_complete_attach(struct zyd_softc *); 168Static int zyd_complete_attach(struct zyd_softc *);
169Static int zyd_open_pipes(struct zyd_softc *); 169Static int zyd_open_pipes(struct zyd_softc *);
170Static void zyd_close_pipes(struct zyd_softc *); 170Static void zyd_close_pipes(struct zyd_softc *);
171Static int zyd_alloc_tx_list(struct zyd_softc *); 171Static int zyd_alloc_tx_list(struct zyd_softc *);
172Static void zyd_free_tx_list(struct zyd_softc *); 172Static void zyd_free_tx_list(struct zyd_softc *);
173Static int zyd_alloc_rx_list(struct zyd_softc *); 173Static int zyd_alloc_rx_list(struct zyd_softc *);
174Static void zyd_free_rx_list(struct zyd_softc *); 174Static void zyd_free_rx_list(struct zyd_softc *);
175Static struct ieee80211_node *zyd_node_alloc(struct ieee80211_node_table *); 175Static struct ieee80211_node *zyd_node_alloc(struct ieee80211_node_table *);
176Static int zyd_media_change(struct ifnet *); 176Static int zyd_media_change(struct ifnet *);
177Static void zyd_next_scan(void *); 177Static void zyd_next_scan(void *);
178Static void zyd_task(void *); 178Static void zyd_task(void *);
179Static int zyd_newstate(struct ieee80211com *, enum ieee80211_state, int); 179Static int zyd_newstate(struct ieee80211com *, enum ieee80211_state, int);
180Static int zyd_cmd(struct zyd_softc *, uint16_t, const void *, int, 180Static int zyd_cmd(struct zyd_softc *, uint16_t, const void *, int,
181 void *, int, u_int); 181 void *, int, u_int);
182Static int zyd_read16(struct zyd_softc *, uint16_t, uint16_t *); 182Static int zyd_read16(struct zyd_softc *, uint16_t, uint16_t *);
183Static int zyd_read32(struct zyd_softc *, uint16_t, uint32_t *); 183Static int zyd_read32(struct zyd_softc *, uint16_t, uint32_t *);
184Static int zyd_write16(struct zyd_softc *, uint16_t, uint16_t); 184Static int zyd_write16(struct zyd_softc *, uint16_t, uint16_t);
185Static int zyd_write32(struct zyd_softc *, uint16_t, uint32_t); 185Static int zyd_write32(struct zyd_softc *, uint16_t, uint32_t);
186Static int zyd_rfwrite(struct zyd_softc *, uint32_t); 186Static int zyd_rfwrite(struct zyd_softc *, uint32_t);
187Static void zyd_lock_phy(struct zyd_softc *); 187Static void zyd_lock_phy(struct zyd_softc *);
188Static void zyd_unlock_phy(struct zyd_softc *); 188Static void zyd_unlock_phy(struct zyd_softc *);
189Static int zyd_rfmd_init(struct zyd_rf *); 189Static int zyd_rfmd_init(struct zyd_rf *);
190Static int zyd_rfmd_switch_radio(struct zyd_rf *, int); 190Static int zyd_rfmd_switch_radio(struct zyd_rf *, int);
191Static int zyd_rfmd_set_channel(struct zyd_rf *, uint8_t); 191Static int zyd_rfmd_set_channel(struct zyd_rf *, uint8_t);
192Static int zyd_al2230_init(struct zyd_rf *); 192Static int zyd_al2230_init(struct zyd_rf *);
193Static int zyd_al2230_switch_radio(struct zyd_rf *, int); 193Static int zyd_al2230_switch_radio(struct zyd_rf *, int);
194Static int zyd_al2230_set_channel(struct zyd_rf *, uint8_t); 194Static int zyd_al2230_set_channel(struct zyd_rf *, uint8_t);
195Static int zyd_al2230_init_b(struct zyd_rf *); 195Static int zyd_al2230_init_b(struct zyd_rf *);
196Static int zyd_al7230B_init(struct zyd_rf *); 196Static int zyd_al7230B_init(struct zyd_rf *);
197Static int zyd_al7230B_switch_radio(struct zyd_rf *, int); 197Static int zyd_al7230B_switch_radio(struct zyd_rf *, int);
198Static int zyd_al7230B_set_channel(struct zyd_rf *, uint8_t); 198Static int zyd_al7230B_set_channel(struct zyd_rf *, uint8_t);
199Static int zyd_al2210_init(struct zyd_rf *); 199Static int zyd_al2210_init(struct zyd_rf *);
200Static int zyd_al2210_switch_radio(struct zyd_rf *, int); 200Static int zyd_al2210_switch_radio(struct zyd_rf *, int);
201Static int zyd_al2210_set_channel(struct zyd_rf *, uint8_t); 201Static int zyd_al2210_set_channel(struct zyd_rf *, uint8_t);
202Static int zyd_gct_init(struct zyd_rf *); 202Static int zyd_gct_init(struct zyd_rf *);
203Static int zyd_gct_switch_radio(struct zyd_rf *, int); 203Static int zyd_gct_switch_radio(struct zyd_rf *, int);
204Static int zyd_gct_set_channel(struct zyd_rf *, uint8_t); 204Static int zyd_gct_set_channel(struct zyd_rf *, uint8_t);
205Static int zyd_maxim_init(struct zyd_rf *); 205Static int zyd_maxim_init(struct zyd_rf *);
206Static int zyd_maxim_switch_radio(struct zyd_rf *, int); 206Static int zyd_maxim_switch_radio(struct zyd_rf *, int);
207Static int zyd_maxim_set_channel(struct zyd_rf *, uint8_t); 207Static int zyd_maxim_set_channel(struct zyd_rf *, uint8_t);
208Static int zyd_maxim2_init(struct zyd_rf *); 208Static int zyd_maxim2_init(struct zyd_rf *);
209Static int zyd_maxim2_switch_radio(struct zyd_rf *, int); 209Static int zyd_maxim2_switch_radio(struct zyd_rf *, int);
210Static int zyd_maxim2_set_channel(struct zyd_rf *, uint8_t); 210Static int zyd_maxim2_set_channel(struct zyd_rf *, uint8_t);
211Static int zyd_rf_attach(struct zyd_softc *, uint8_t); 211Static int zyd_rf_attach(struct zyd_softc *, uint8_t);
212Static const char *zyd_rf_name(uint8_t); 212Static const char *zyd_rf_name(uint8_t);
213Static int zyd_hw_init(struct zyd_softc *); 213Static int zyd_hw_init(struct zyd_softc *);
214Static int zyd_read_eeprom(struct zyd_softc *); 214Static int zyd_read_eeprom(struct zyd_softc *);
215Static int zyd_set_macaddr(struct zyd_softc *, const uint8_t *); 215Static int zyd_set_macaddr(struct zyd_softc *, const uint8_t *);
216Static int zyd_set_bssid(struct zyd_softc *, const uint8_t *); 216Static int zyd_set_bssid(struct zyd_softc *, const uint8_t *);
217Static int zyd_switch_radio(struct zyd_softc *, int); 217Static int zyd_switch_radio(struct zyd_softc *, int);
218Static void zyd_set_led(struct zyd_softc *, int, int); 218Static void zyd_set_led(struct zyd_softc *, int, int);
219Static int zyd_set_rxfilter(struct zyd_softc *); 219Static int zyd_set_rxfilter(struct zyd_softc *);
220Static void zyd_set_chan(struct zyd_softc *, struct ieee80211_channel *); 220Static void zyd_set_chan(struct zyd_softc *, struct ieee80211_channel *);
221Static int zyd_set_beacon_interval(struct zyd_softc *, int); 221Static int zyd_set_beacon_interval(struct zyd_softc *, int);
222Static uint8_t zyd_plcp_signal(int); 222Static uint8_t zyd_plcp_signal(int);
223Static void zyd_intr(usbd_xfer_handle, usbd_private_handle, usbd_status); 223Static void zyd_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
224Static void zyd_rx_data(struct zyd_softc *, const uint8_t *, uint16_t); 224Static void zyd_rx_data(struct zyd_softc *, const uint8_t *, uint16_t);
225Static void zyd_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status); 225Static void zyd_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
226Static void zyd_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status); 226Static void zyd_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
227Static int zyd_tx_mgt(struct zyd_softc *, struct mbuf *, 227Static int zyd_tx_mgt(struct zyd_softc *, struct mbuf *,
228 struct ieee80211_node *); 228 struct ieee80211_node *);
229Static int zyd_tx_data(struct zyd_softc *, struct mbuf *, 229Static int zyd_tx_data(struct zyd_softc *, struct mbuf *,
230 struct ieee80211_node *); 230 struct ieee80211_node *);
231Static void zyd_start(struct ifnet *); 231Static void zyd_start(struct ifnet *);
232Static void zyd_watchdog(struct ifnet *); 232Static void zyd_watchdog(struct ifnet *);
233Static int zyd_ioctl(struct ifnet *, u_long, void *); 233Static int zyd_ioctl(struct ifnet *, u_long, void *);
234Static int zyd_init(struct ifnet *); 234Static int zyd_init(struct ifnet *);
235Static void zyd_stop(struct ifnet *, int); 235Static void zyd_stop(struct ifnet *, int);
236Static int zyd_loadfirmware(struct zyd_softc *, u_char *, size_t); 236Static int zyd_loadfirmware(struct zyd_softc *, u_char *, size_t);
237Static void zyd_iter_func(void *, struct ieee80211_node *); 237Static void zyd_iter_func(void *, struct ieee80211_node *);
238Static void zyd_amrr_timeout(void *); 238Static void zyd_amrr_timeout(void *);
239Static void zyd_newassoc(struct ieee80211_node *, int); 239Static void zyd_newassoc(struct ieee80211_node *, int);
240 240
241static const struct ieee80211_rateset zyd_rateset_11b = 241static const struct ieee80211_rateset zyd_rateset_11b =
242 { 4, { 2, 4, 11, 22 } }; 242 { 4, { 2, 4, 11, 22 } };
243 243
244static const struct ieee80211_rateset zyd_rateset_11g = 244static const struct ieee80211_rateset zyd_rateset_11g =
245 { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } }; 245 { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
246 246
247int 247int
248zyd_match(device_t parent, cfdata_t match, void *aux) 248zyd_match(device_t parent, cfdata_t match, void *aux)
249{ 249{
250 struct usb_attach_arg *uaa = aux; 250 struct usb_attach_arg *uaa = aux;
251 251
252 return (zyd_lookup(uaa->vendor, uaa->product) != NULL) ? 252 return (zyd_lookup(uaa->vendor, uaa->product) != NULL) ?
253 UMATCH_VENDOR_PRODUCT : UMATCH_NONE; 253 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
254} 254}
255 255
256Static void 256Static void
257zyd_attachhook(device_t self) 257zyd_attachhook(device_t self)
258{ 258{
259 struct zyd_softc *sc = device_private(self); 259 struct zyd_softc *sc = device_private(self);
260 firmware_handle_t fwh; 260 firmware_handle_t fwh;
261 const char *fwname; 261 const char *fwname;
262 u_char *fw; 262 u_char *fw;
263 size_t size; 263 size_t size;
264 int error; 264 int error;
265 265
266 fwname = (sc->mac_rev == ZYD_ZD1211) ? "zyd-zd1211" : "zyd-zd1211b"; 266 fwname = (sc->mac_rev == ZYD_ZD1211) ? "zyd-zd1211" : "zyd-zd1211b";
267 if ((error = firmware_open("zyd", fwname, &fwh)) != 0) { 267 if ((error = firmware_open("zyd", fwname, &fwh)) != 0) {
268 aprint_error_dev(sc->sc_dev, 268 aprint_error_dev(sc->sc_dev,
269 "failed to open firmware %s (error=%d)\n", fwname, error); 269 "failed to open firmware %s (error=%d)\n", fwname, error);
270 return; 270 return;
271 } 271 }
272 size = firmware_get_size(fwh); 272 size = firmware_get_size(fwh);
273 fw = firmware_malloc(size); 273 fw = firmware_malloc(size);
274 if (fw == NULL) { 274 if (fw == NULL) {
275 aprint_error_dev(sc->sc_dev, 275 aprint_error_dev(sc->sc_dev,
276 "failed to allocate firmware memory\n"); 276 "failed to allocate firmware memory\n");
277 firmware_close(fwh); 277 firmware_close(fwh);
278 return; 278 return;
279 } 279 }
280 error = firmware_read(fwh, 0, fw, size); 280 error = firmware_read(fwh, 0, fw, size);
281 firmware_close(fwh); 281 firmware_close(fwh);
282 if (error != 0) { 282 if (error != 0) {
283 aprint_error_dev(sc->sc_dev, 283 aprint_error_dev(sc->sc_dev,
284 "failed to read firmware (error %d)\n", error); 284 "failed to read firmware (error %d)\n", error);
285 firmware_free(fw, 0); 285 firmware_free(fw, 0);
286 return; 286 return;
287 } 287 }
288 288
289 error = zyd_loadfirmware(sc, fw, size); 289 error = zyd_loadfirmware(sc, fw, size);
290 if (error != 0) { 290 if (error != 0) {
291 aprint_error_dev(sc->sc_dev, 291 aprint_error_dev(sc->sc_dev,
292 "could not load firmware (error=%d)\n", error); 292 "could not load firmware (error=%d)\n", error);
293 firmware_free(fw, 0); 293 firmware_free(fw, 0);
294 return; 294 return;
295 } 295 }
296 296
297 firmware_free(fw, 0); 297 firmware_free(fw, 0);
298 sc->sc_flags |= ZD1211_FWLOADED; 298 sc->sc_flags |= ZD1211_FWLOADED;
299 299
300 /* complete the attach process */ 300 /* complete the attach process */
301 if ((error = zyd_complete_attach(sc)) == 0) 301 if ((error = zyd_complete_attach(sc)) == 0)
302 sc->attached = 1; 302 sc->attached = 1;
303 return; 303 return;
304} 304}
305 305
306void 306void
307zyd_attach(device_t parent, device_t self, void *aux) 307zyd_attach(device_t parent, device_t self, void *aux)
308{ 308{
309 struct zyd_softc *sc = device_private(self); 309 struct zyd_softc *sc = device_private(self);
310 struct usb_attach_arg *uaa = aux; 310 struct usb_attach_arg *uaa = aux;
311 char *devinfop; 311 char *devinfop;
312 usb_device_descriptor_t* ddesc; 312 usb_device_descriptor_t* ddesc;
313 struct ifnet *ifp = &sc->sc_if; 313 struct ifnet *ifp = &sc->sc_if;
314 314
315 sc->sc_dev = self; 315 sc->sc_dev = self;
316 sc->sc_udev = uaa->device; 316 sc->sc_udev = uaa->device;
317 sc->sc_flags = 0; 317 sc->sc_flags = 0;
318 318
319 aprint_naive("\n"); 319 aprint_naive("\n");
320 aprint_normal("\n"); 320 aprint_normal("\n");
321 321
322 devinfop = usbd_devinfo_alloc(uaa->device, 0); 322 devinfop = usbd_devinfo_alloc(uaa->device, 0);
323 aprint_normal_dev(self, "%s\n", devinfop); 323 aprint_normal_dev(self, "%s\n", devinfop);
324 usbd_devinfo_free(devinfop); 324 usbd_devinfo_free(devinfop);
325 325
326 sc->mac_rev = zyd_lookup(uaa->vendor, uaa->product)->rev; 326 sc->mac_rev = zyd_lookup(uaa->vendor, uaa->product)->rev;
327 327
328 ddesc = usbd_get_device_descriptor(sc->sc_udev); 328 ddesc = usbd_get_device_descriptor(sc->sc_udev);
329 if (UGETW(ddesc->bcdDevice) < 0x4330) { 329 if (UGETW(ddesc->bcdDevice) < 0x4330) {
330 aprint_error_dev(self, "device version mismatch: 0x%x " 330 aprint_error_dev(self, "device version mismatch: 0x%x "
331 "(only >= 43.30 supported)\n", UGETW(ddesc->bcdDevice)); 331 "(only >= 43.30 supported)\n", UGETW(ddesc->bcdDevice));
332 return; 332 return;
333 } 333 }
334 334
335 ifp->if_softc = sc; 335 ifp->if_softc = sc;
336 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 336 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
337 ifp->if_init = zyd_init; 337 ifp->if_init = zyd_init;
338 ifp->if_ioctl = zyd_ioctl; 338 ifp->if_ioctl = zyd_ioctl;
339 ifp->if_start = zyd_start; 339 ifp->if_start = zyd_start;
340 ifp->if_watchdog = zyd_watchdog; 340 ifp->if_watchdog = zyd_watchdog;
341 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN); 341 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
342 IFQ_SET_READY(&ifp->if_snd); 342 IFQ_SET_READY(&ifp->if_snd);
343 memcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ); 343 memcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
344 344
345 SIMPLEQ_INIT(&sc->sc_rqh); 345 SIMPLEQ_INIT(&sc->sc_rqh);
346 346
347 /* defer configrations after file system is ready to load firmware */ 347 /* defer configrations after file system is ready to load firmware */
348 config_mountroot(self, zyd_attachhook); 348 config_mountroot(self, zyd_attachhook);
349} 349}
350 350
351Static int 351Static int
352zyd_complete_attach(struct zyd_softc *sc) 352zyd_complete_attach(struct zyd_softc *sc)
353{ 353{
354 struct ieee80211com *ic = &sc->sc_ic; 354 struct ieee80211com *ic = &sc->sc_ic;
355 struct ifnet *ifp = &sc->sc_if; 355 struct ifnet *ifp = &sc->sc_if;
356 usbd_status error; 356 usbd_status error;
357 int i; 357 int i;
358 358
359 usb_init_task(&sc->sc_task, zyd_task, sc); 359 usb_init_task(&sc->sc_task, zyd_task, sc);
360 callout_init(&(sc->sc_scan_ch), 0); 360 callout_init(&(sc->sc_scan_ch), 0);
361 361
362 sc->amrr.amrr_min_success_threshold = 1; 362 sc->amrr.amrr_min_success_threshold = 1;
363 sc->amrr.amrr_max_success_threshold = 10; 363 sc->amrr.amrr_max_success_threshold = 10;
364 callout_init(&sc->sc_amrr_ch, 0); 364 callout_init(&sc->sc_amrr_ch, 0);
365 365
366 error = usbd_set_config_no(sc->sc_udev, ZYD_CONFIG_NO, 1); 366 error = usbd_set_config_no(sc->sc_udev, ZYD_CONFIG_NO, 1);
367 if (error != 0) { 367 if (error != 0) {
368 aprint_error_dev(sc->sc_dev, "setting config no failed\n"); 368 aprint_error_dev(sc->sc_dev, "setting config no failed\n");
369 goto fail; 369 goto fail;
370 } 370 }
371 371
372 error = usbd_device2interface_handle(sc->sc_udev, ZYD_IFACE_INDEX, 372 error = usbd_device2interface_handle(sc->sc_udev, ZYD_IFACE_INDEX,
373 &sc->sc_iface); 373 &sc->sc_iface);
374 if (error != 0) { 374 if (error != 0) {
375 aprint_error_dev(sc->sc_dev, 375 aprint_error_dev(sc->sc_dev,
376 "getting interface handle failed\n"); 376 "getting interface handle failed\n");
377 goto fail; 377 goto fail;
378 } 378 }
379 379
380 if ((error = zyd_open_pipes(sc)) != 0) { 380 if ((error = zyd_open_pipes(sc)) != 0) {
381 aprint_error_dev(sc->sc_dev, "could not open pipes\n"); 381 aprint_error_dev(sc->sc_dev, "could not open pipes\n");
382 goto fail; 382 goto fail;
383 } 383 }
384 384
385 if ((error = zyd_read_eeprom(sc)) != 0) { 385 if ((error = zyd_read_eeprom(sc)) != 0) {
386 aprint_error_dev(sc->sc_dev, "could not read EEPROM\n"); 386 aprint_error_dev(sc->sc_dev, "could not read EEPROM\n");
387 goto fail; 387 goto fail;
388 } 388 }
389 389
390 if ((error = zyd_rf_attach(sc, sc->rf_rev)) != 0) { 390 if ((error = zyd_rf_attach(sc, sc->rf_rev)) != 0) {
391 aprint_error_dev(sc->sc_dev, "could not attach RF\n"); 391 aprint_error_dev(sc->sc_dev, "could not attach RF\n");
392 goto fail; 392 goto fail;
393 } 393 }
394 394
395 if ((error = zyd_hw_init(sc)) != 0) { 395 if ((error = zyd_hw_init(sc)) != 0) {
396 aprint_error_dev(sc->sc_dev, 396 aprint_error_dev(sc->sc_dev,
397 "hardware initialization failed\n"); 397 "hardware initialization failed\n");
398 goto fail; 398 goto fail;
399 } 399 }
400 400
401 aprint_normal_dev(sc->sc_dev, 401 aprint_normal_dev(sc->sc_dev,
402 "HMAC ZD1211%s, FW %02x.%02x, RF %s, PA %x, address %s\n", 402 "HMAC ZD1211%s, FW %02x.%02x, RF %s, PA %x, address %s\n",
403 (sc->mac_rev == ZYD_ZD1211) ? "": "B", 403 (sc->mac_rev == ZYD_ZD1211) ? "": "B",
404 sc->fw_rev >> 8, sc->fw_rev & 0xff, zyd_rf_name(sc->rf_rev), 404 sc->fw_rev >> 8, sc->fw_rev & 0xff, zyd_rf_name(sc->rf_rev),
405 sc->pa_rev, ether_sprintf(ic->ic_myaddr)); 405 sc->pa_rev, ether_sprintf(ic->ic_myaddr));
406 406
407 ic->ic_ifp = ifp; 407 ic->ic_ifp = ifp;
408 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */ 408 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
409 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */ 409 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
410 ic->ic_state = IEEE80211_S_INIT; 410 ic->ic_state = IEEE80211_S_INIT;
411 411
412 /* set device capabilities */ 412 /* set device capabilities */
413 ic->ic_caps = 413 ic->ic_caps =
414 IEEE80211_C_MONITOR | /* monitor mode supported */ 414 IEEE80211_C_MONITOR | /* monitor mode supported */
415 IEEE80211_C_TXPMGT | /* tx power management */ 415 IEEE80211_C_TXPMGT | /* tx power management */
416 IEEE80211_C_SHPREAMBLE | /* short preamble supported */ 416 IEEE80211_C_SHPREAMBLE | /* short preamble supported */
417 IEEE80211_C_WEP; /* s/w WEP */ 417 IEEE80211_C_WEP; /* s/w WEP */
418 418
419 /* set supported .11b and .11g rates */ 419 /* set supported .11b and .11g rates */
420 ic->ic_sup_rates[IEEE80211_MODE_11B] = zyd_rateset_11b; 420 ic->ic_sup_rates[IEEE80211_MODE_11B] = zyd_rateset_11b;
421 ic->ic_sup_rates[IEEE80211_MODE_11G] = zyd_rateset_11g; 421 ic->ic_sup_rates[IEEE80211_MODE_11G] = zyd_rateset_11g;
422 422
423 /* set supported .11b and .11g channels (1 through 14) */ 423 /* set supported .11b and .11g channels (1 through 14) */
424 for (i = 1; i <= 14; i++) { 424 for (i = 1; i <= 14; i++) {
425 ic->ic_channels[i].ic_freq = 425 ic->ic_channels[i].ic_freq =
426 ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ); 426 ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
427 ic->ic_channels[i].ic_flags = 427 ic->ic_channels[i].ic_flags =
428 IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM | 428 IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
429 IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ; 429 IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
430 } 430 }
431 431
432 if_attach(ifp); 432 if_attach(ifp);
433 ieee80211_ifattach(ic); 433 ieee80211_ifattach(ic);
434 ic->ic_node_alloc = zyd_node_alloc; 434 ic->ic_node_alloc = zyd_node_alloc;
435 ic->ic_newassoc = zyd_newassoc; 435 ic->ic_newassoc = zyd_newassoc;
436 436
437 /* override state transition machine */ 437 /* override state transition machine */
438 sc->sc_newstate = ic->ic_newstate; 438 sc->sc_newstate = ic->ic_newstate;
439 ic->ic_newstate = zyd_newstate; 439 ic->ic_newstate = zyd_newstate;
440 ieee80211_media_init(ic, zyd_media_change, ieee80211_media_status); 440 ieee80211_media_init(ic, zyd_media_change, ieee80211_media_status);
441 441
442 bpf_attach2(ifp, DLT_IEEE802_11_RADIO, 442 bpf_attach2(ifp, DLT_IEEE802_11_RADIO,
443 sizeof (struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN, 443 sizeof (struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN,
444 &sc->sc_drvbpf); 444 &sc->sc_drvbpf);
445 445
446 sc->sc_rxtap_len = sizeof sc->sc_rxtapu; 446 sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
447 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len); 447 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
448 sc->sc_rxtap.wr_ihdr.it_present = htole32(ZYD_RX_RADIOTAP_PRESENT); 448 sc->sc_rxtap.wr_ihdr.it_present = htole32(ZYD_RX_RADIOTAP_PRESENT);
449 449
450 sc->sc_txtap_len = sizeof sc->sc_txtapu; 450 sc->sc_txtap_len = sizeof sc->sc_txtapu;
451 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len); 451 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
452 sc->sc_txtap.wt_ihdr.it_present = htole32(ZYD_TX_RADIOTAP_PRESENT); 452 sc->sc_txtap.wt_ihdr.it_present = htole32(ZYD_TX_RADIOTAP_PRESENT);
453 453
454 ieee80211_announce(ic); 454 ieee80211_announce(ic);
455 455
456 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev); 456 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
457 457
458fail: return error; 458fail: return error;
459} 459}
460 460
461int 461int
462zyd_detach(device_t self, int flags) 462zyd_detach(device_t self, int flags)
463{ 463{
464 struct zyd_softc *sc = device_private(self); 464 struct zyd_softc *sc = device_private(self);
465 struct ieee80211com *ic = &sc->sc_ic; 465 struct ieee80211com *ic = &sc->sc_ic;
466 struct ifnet *ifp = &sc->sc_if; 466 struct ifnet *ifp = &sc->sc_if;
467 int s; 467 int s;
468 468
469 if (!sc->attached) 469 if (!sc->attached)
470 return 0; 470 return 0;
471 471
472 s = splusb(); 472 s = splusb();
473 473
474 zyd_stop(ifp, 1); 474 zyd_stop(ifp, 1);
475 usb_rem_task(sc->sc_udev, &sc->sc_task); 475 usb_rem_task(sc->sc_udev, &sc->sc_task);
476 callout_stop(&sc->sc_scan_ch); 476 callout_stop(&sc->sc_scan_ch);
477 callout_stop(&sc->sc_amrr_ch); 477 callout_stop(&sc->sc_amrr_ch);
478 478
479 zyd_close_pipes(sc); 479 zyd_close_pipes(sc);
480 480
481 sc->attached = 0; 481 sc->attached = 0;
482 482
483 bpf_detach(ifp); 483 bpf_detach(ifp);
484 ieee80211_ifdetach(ic); 484 ieee80211_ifdetach(ic);
485 if_detach(ifp); 485 if_detach(ifp);
486 486
487 splx(s); 487 splx(s);
488 488
489 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, 489 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
490 sc->sc_dev); 490 sc->sc_dev);
491 491
492 return 0; 492 return 0;
493} 493}
494 494
495Static int 495Static int
496zyd_open_pipes(struct zyd_softc *sc) 496zyd_open_pipes(struct zyd_softc *sc)
497{ 497{
498 usb_endpoint_descriptor_t *edesc; 498 usb_endpoint_descriptor_t *edesc;
499 int isize; 499 int isize;
500 usbd_status error; 500 usbd_status error;
501 501
502 /* interrupt in */ 502 /* interrupt in */
503 edesc = usbd_get_endpoint_descriptor(sc->sc_iface, 0x83); 503 edesc = usbd_get_endpoint_descriptor(sc->sc_iface, 0x83);
504 if (edesc == NULL) 504 if (edesc == NULL)
505 return EINVAL; 505 return EINVAL;
506 506
507 isize = UGETW(edesc->wMaxPacketSize); 507 isize = UGETW(edesc->wMaxPacketSize);
508 if (isize == 0) /* should not happen */ 508 if (isize == 0) /* should not happen */
509 return EINVAL; 509 return EINVAL;
510 510
511 sc->ibuf = malloc(isize, M_USBDEV, M_NOWAIT); 511 sc->ibuf = malloc(isize, M_USBDEV, M_NOWAIT);
512 if (sc->ibuf == NULL) 512 if (sc->ibuf == NULL)
513 return ENOMEM; 513 return ENOMEM;
514 514
515 error = usbd_open_pipe_intr(sc->sc_iface, 0x83, USBD_SHORT_XFER_OK, 515 error = usbd_open_pipe_intr(sc->sc_iface, 0x83, USBD_SHORT_XFER_OK,
516 &sc->zyd_ep[ZYD_ENDPT_IIN], sc, sc->ibuf, isize, zyd_intr, 516 &sc->zyd_ep[ZYD_ENDPT_IIN], sc, sc->ibuf, isize, zyd_intr,
517 USBD_DEFAULT_INTERVAL); 517 USBD_DEFAULT_INTERVAL);
518 if (error != 0) { 518 if (error != 0) {
519 printf("%s: open rx intr pipe failed: %s\n", 519 printf("%s: open rx intr pipe failed: %s\n",
520 device_xname(sc->sc_dev), usbd_errstr(error)); 520 device_xname(sc->sc_dev), usbd_errstr(error));
521 goto fail; 521 goto fail;
522 } 522 }
523 523
524 /* interrupt out (not necessarily an interrupt pipe) */ 524 /* interrupt out (not necessarily an interrupt pipe) */
525 error = usbd_open_pipe(sc->sc_iface, 0x04, USBD_EXCLUSIVE_USE, 525 error = usbd_open_pipe(sc->sc_iface, 0x04, USBD_EXCLUSIVE_USE,
526 &sc->zyd_ep[ZYD_ENDPT_IOUT]); 526 &sc->zyd_ep[ZYD_ENDPT_IOUT]);
527 if (error != 0) { 527 if (error != 0) {
528 printf("%s: open tx intr pipe failed: %s\n", 528 printf("%s: open tx intr pipe failed: %s\n",
529 device_xname(sc->sc_dev), usbd_errstr(error)); 529 device_xname(sc->sc_dev), usbd_errstr(error));
530 goto fail; 530 goto fail;
531 } 531 }
532 532
533 /* bulk in */ 533 /* bulk in */
534 error = usbd_open_pipe(sc->sc_iface, 0x82, USBD_EXCLUSIVE_USE, 534 error = usbd_open_pipe(sc->sc_iface, 0x82, USBD_EXCLUSIVE_USE,
535 &sc->zyd_ep[ZYD_ENDPT_BIN]); 535 &sc->zyd_ep[ZYD_ENDPT_BIN]);
536 if (error != 0) { 536 if (error != 0) {
537 printf("%s: open rx pipe failed: %s\n", 537 printf("%s: open rx pipe failed: %s\n",
538 device_xname(sc->sc_dev), usbd_errstr(error)); 538 device_xname(sc->sc_dev), usbd_errstr(error));
539 goto fail; 539 goto fail;
540 } 540 }
541 541
542 /* bulk out */ 542 /* bulk out */
543 error = usbd_open_pipe(sc->sc_iface, 0x01, USBD_EXCLUSIVE_USE, 543 error = usbd_open_pipe(sc->sc_iface, 0x01, USBD_EXCLUSIVE_USE,
544 &sc->zyd_ep[ZYD_ENDPT_BOUT]); 544 &sc->zyd_ep[ZYD_ENDPT_BOUT]);
545 if (error != 0) { 545 if (error != 0) {
546 printf("%s: open tx pipe failed: %s\n", 546 printf("%s: open tx pipe failed: %s\n",
547 device_xname(sc->sc_dev), usbd_errstr(error)); 547 device_xname(sc->sc_dev), usbd_errstr(error));
548 goto fail; 548 goto fail;
549 } 549 }
550 550
551 return 0; 551 return 0;
552 552
553fail: zyd_close_pipes(sc); 553fail: zyd_close_pipes(sc);
554 return error; 554 return error;
555} 555}
556 556
557Static void 557Static void
558zyd_close_pipes(struct zyd_softc *sc) 558zyd_close_pipes(struct zyd_softc *sc)
559{ 559{
560 int i; 560 int i;
561 561
562 for (i = 0; i < ZYD_ENDPT_CNT; i++) { 562 for (i = 0; i < ZYD_ENDPT_CNT; i++) {
563 if (sc->zyd_ep[i] != NULL) { 563 if (sc->zyd_ep[i] != NULL) {
564 usbd_abort_pipe(sc->zyd_ep[i]); 564 usbd_abort_pipe(sc->zyd_ep[i]);
565 usbd_close_pipe(sc->zyd_ep[i]); 565 usbd_close_pipe(sc->zyd_ep[i]);
566 sc->zyd_ep[i] = NULL; 566 sc->zyd_ep[i] = NULL;
567 } 567 }
568 } 568 }
569 if (sc->ibuf != NULL) { 569 if (sc->ibuf != NULL) {
570 free(sc->ibuf, M_USBDEV); 570 free(sc->ibuf, M_USBDEV);
571 sc->ibuf = NULL; 571 sc->ibuf = NULL;
572 } 572 }
573} 573}
574 574
575Static int 575Static int
576zyd_alloc_tx_list(struct zyd_softc *sc) 576zyd_alloc_tx_list(struct zyd_softc *sc)
577{ 577{
578 int i, error; 578 int i, error;
579 579
580 sc->tx_queued = 0; 580 sc->tx_queued = 0;
581 581
582 for (i = 0; i < ZYD_TX_LIST_CNT; i++) { 582 for (i = 0; i < ZYD_TX_LIST_CNT; i++) {
583 struct zyd_tx_data *data = &sc->tx_data[i]; 583 struct zyd_tx_data *data = &sc->tx_data[i];
584 584
585 data->sc = sc; /* backpointer for callbacks */ 585 data->sc = sc; /* backpointer for callbacks */
586 586
587 data->xfer = usbd_alloc_xfer(sc->sc_udev); 587 data->xfer = usbd_alloc_xfer(sc->sc_udev);
588 if (data->xfer == NULL) { 588 if (data->xfer == NULL) {
589 printf("%s: could not allocate tx xfer\n", 589 printf("%s: could not allocate tx xfer\n",
590 device_xname(sc->sc_dev)); 590 device_xname(sc->sc_dev));
591 error = ENOMEM; 591 error = ENOMEM;
592 goto fail; 592 goto fail;
593 } 593 }
594 data->buf = usbd_alloc_buffer(data->xfer, ZYD_MAX_TXBUFSZ); 594 data->buf = usbd_alloc_buffer(data->xfer, ZYD_MAX_TXBUFSZ);
595 if (data->buf == NULL) { 595 if (data->buf == NULL) {
596 printf("%s: could not allocate tx buffer\n", 596 printf("%s: could not allocate tx buffer\n",
597 device_xname(sc->sc_dev)); 597 device_xname(sc->sc_dev));
598 error = ENOMEM; 598 error = ENOMEM;
599 goto fail; 599 goto fail;
600 } 600 }
601 601
602 /* clear Tx descriptor */ 602 /* clear Tx descriptor */
603 memset(data->buf, 0, sizeof (struct zyd_tx_desc)); 603 memset(data->buf, 0, sizeof (struct zyd_tx_desc));
604 } 604 }
605 return 0; 605 return 0;
606 606
607fail: zyd_free_tx_list(sc); 607fail: zyd_free_tx_list(sc);
608 return error; 608 return error;
609} 609}
610 610
611Static void 611Static void
612zyd_free_tx_list(struct zyd_softc *sc) 612zyd_free_tx_list(struct zyd_softc *sc)
613{ 613{
614 int i; 614 int i;
615 615
616 for (i = 0; i < ZYD_TX_LIST_CNT; i++) { 616 for (i = 0; i < ZYD_TX_LIST_CNT; i++) {
617 struct zyd_tx_data *data = &sc->tx_data[i]; 617 struct zyd_tx_data *data = &sc->tx_data[i];
618 618
619 if (data->xfer != NULL) { 619 if (data->xfer != NULL) {
620 usbd_free_xfer(data->xfer); 620 usbd_free_xfer(data->xfer);
621 data->xfer = NULL; 621 data->xfer = NULL;
622 } 622 }
623 if (data->ni != NULL) { 623 if (data->ni != NULL) {
624 ieee80211_free_node(data->ni); 624 ieee80211_free_node(data->ni);
625 data->ni = NULL; 625 data->ni = NULL;
626 } 626 }
627 } 627 }
628} 628}
629 629
630Static int 630Static int
631zyd_alloc_rx_list(struct zyd_softc *sc) 631zyd_alloc_rx_list(struct zyd_softc *sc)
632{ 632{
633 int i, error; 633 int i, error;
634 634
635 for (i = 0; i < ZYD_RX_LIST_CNT; i++) { 635 for (i = 0; i < ZYD_RX_LIST_CNT; i++) {
636 struct zyd_rx_data *data = &sc->rx_data[i]; 636 struct zyd_rx_data *data = &sc->rx_data[i];
637 637
638 data->sc = sc; /* backpointer for callbacks */ 638 data->sc = sc; /* backpointer for callbacks */
639 639
640 data->xfer = usbd_alloc_xfer(sc->sc_udev); 640 data->xfer = usbd_alloc_xfer(sc->sc_udev);
641 if (data->xfer == NULL) { 641 if (data->xfer == NULL) {
642 printf("%s: could not allocate rx xfer\n", 642 printf("%s: could not allocate rx xfer\n",
643 device_xname(sc->sc_dev)); 643 device_xname(sc->sc_dev));
644 error = ENOMEM; 644 error = ENOMEM;
645 goto fail; 645 goto fail;
646 } 646 }
647 data->buf = usbd_alloc_buffer(data->xfer, ZYX_MAX_RXBUFSZ); 647 data->buf = usbd_alloc_buffer(data->xfer, ZYX_MAX_RXBUFSZ);
648 if (data->buf == NULL) { 648 if (data->buf == NULL) {
649 printf("%s: could not allocate rx buffer\n", 649 printf("%s: could not allocate rx buffer\n",
650 device_xname(sc->sc_dev)); 650 device_xname(sc->sc_dev));
651 error = ENOMEM; 651 error = ENOMEM;
652 goto fail; 652 goto fail;
653 } 653 }
654 } 654 }
655 return 0; 655 return 0;
656 656
657fail: zyd_free_rx_list(sc); 657fail: zyd_free_rx_list(sc);
658 return error; 658 return error;
659} 659}
660 660
661Static void 661Static void
662zyd_free_rx_list(struct zyd_softc *sc) 662zyd_free_rx_list(struct zyd_softc *sc)
663{ 663{
664 int i; 664 int i;
665 665
666 for (i = 0; i < ZYD_RX_LIST_CNT; i++) { 666 for (i = 0; i < ZYD_RX_LIST_CNT; i++) {
667 struct zyd_rx_data *data = &sc->rx_data[i]; 667 struct zyd_rx_data *data = &sc->rx_data[i];
668 668
669 if (data->xfer != NULL) { 669 if (data->xfer != NULL) {
670 usbd_free_xfer(data->xfer); 670 usbd_free_xfer(data->xfer);
671 data->xfer = NULL; 671 data->xfer = NULL;
672 } 672 }
673 } 673 }
674} 674}
675 675
676/* ARGUSED */ 676/* ARGUSED */
677Static struct ieee80211_node * 677Static struct ieee80211_node *
678zyd_node_alloc(struct ieee80211_node_table *nt __unused) 678zyd_node_alloc(struct ieee80211_node_table *nt __unused)
679{ 679{
680 struct zyd_node *zn; 680 struct zyd_node *zn;
681 681
682 zn = malloc(sizeof (struct zyd_node), M_80211_NODE, M_NOWAIT | M_ZERO); 682 zn = malloc(sizeof (struct zyd_node), M_80211_NODE, M_NOWAIT | M_ZERO);
683 683
684 return &zn->ni; 684 return &zn->ni;
685} 685}
686 686
687Static int 687Static int
688zyd_media_change(struct ifnet *ifp) 688zyd_media_change(struct ifnet *ifp)
689{ 689{
690 int error; 690 int error;
691 691
692 error = ieee80211_media_change(ifp); 692 error = ieee80211_media_change(ifp);
693 if (error != ENETRESET) 693 if (error != ENETRESET)
694 return error; 694 return error;
695 695
696 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING)) 696 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
697 zyd_init(ifp); 697 zyd_init(ifp);
698 698
699 return 0; 699 return 0;
700} 700}
701 701
702/* 702/*
703 * This function is called periodically (every 200ms) during scanning to 703 * This function is called periodically (every 200ms) during scanning to
704 * switch from one channel to another. 704 * switch from one channel to another.
705 */ 705 */
706Static void 706Static void
707zyd_next_scan(void *arg) 707zyd_next_scan(void *arg)
708{ 708{
709 struct zyd_softc *sc = arg; 709 struct zyd_softc *sc = arg;
710 struct ieee80211com *ic = &sc->sc_ic; 710 struct ieee80211com *ic = &sc->sc_ic;
711 711
712 if (ic->ic_state == IEEE80211_S_SCAN) 712 if (ic->ic_state == IEEE80211_S_SCAN)
713 ieee80211_next_scan(ic); 713 ieee80211_next_scan(ic);
714} 714}
715 715
716Static void 716Static void
717zyd_task(void *arg) 717zyd_task(void *arg)
718{ 718{
719 struct zyd_softc *sc = arg; 719 struct zyd_softc *sc = arg;
720 struct ieee80211com *ic = &sc->sc_ic; 720 struct ieee80211com *ic = &sc->sc_ic;
721 enum ieee80211_state ostate; 721 enum ieee80211_state ostate;
722 722
723 ostate = ic->ic_state; 723 ostate = ic->ic_state;
724 724
725 switch (sc->sc_state) { 725 switch (sc->sc_state) {
726 case IEEE80211_S_INIT: 726 case IEEE80211_S_INIT:
727 if (ostate == IEEE80211_S_RUN) { 727 if (ostate == IEEE80211_S_RUN) {
728 /* turn link LED off */ 728 /* turn link LED off */
729 zyd_set_led(sc, ZYD_LED1, 0); 729 zyd_set_led(sc, ZYD_LED1, 0);
730 730
731 /* stop data LED from blinking */ 731 /* stop data LED from blinking */
732 zyd_write32(sc, sc->fwbase + ZYD_FW_LINK_STATUS, 0); 732 zyd_write32(sc, sc->fwbase + ZYD_FW_LINK_STATUS, 0);
733 } 733 }
734 break; 734 break;
735 735
736 case IEEE80211_S_SCAN: 736 case IEEE80211_S_SCAN:
737 zyd_set_chan(sc, ic->ic_curchan); 737 zyd_set_chan(sc, ic->ic_curchan);
738 callout_reset(&sc->sc_scan_ch, hz / 5, zyd_next_scan, sc); 738 callout_reset(&sc->sc_scan_ch, hz / 5, zyd_next_scan, sc);
739 break; 739 break;
740 740
741 case IEEE80211_S_AUTH: 741 case IEEE80211_S_AUTH:
742 case IEEE80211_S_ASSOC: 742 case IEEE80211_S_ASSOC:
743 zyd_set_chan(sc, ic->ic_curchan); 743 zyd_set_chan(sc, ic->ic_curchan);
744 break; 744 break;
745 745
746 case IEEE80211_S_RUN: 746 case IEEE80211_S_RUN:
747 { 747 {
748 struct ieee80211_node *ni = ic->ic_bss; 748 struct ieee80211_node *ni = ic->ic_bss;
749 749
750 zyd_set_chan(sc, ic->ic_curchan); 750 zyd_set_chan(sc, ic->ic_curchan);
751 751
752 if (ic->ic_opmode != IEEE80211_M_MONITOR) { 752 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
753 /* turn link LED on */ 753 /* turn link LED on */
754 zyd_set_led(sc, ZYD_LED1, 1); 754 zyd_set_led(sc, ZYD_LED1, 1);
755 755
756 /* make data LED blink upon Tx */ 756 /* make data LED blink upon Tx */
757 zyd_write32(sc, sc->fwbase + ZYD_FW_LINK_STATUS, 1); 757 zyd_write32(sc, sc->fwbase + ZYD_FW_LINK_STATUS, 1);
758 758
759 zyd_set_bssid(sc, ni->ni_bssid); 759 zyd_set_bssid(sc, ni->ni_bssid);
760 } 760 }
761 761
762 if (ic->ic_opmode == IEEE80211_M_STA) { 762 if (ic->ic_opmode == IEEE80211_M_STA) {
763 /* fake a join to init the tx rate */ 763 /* fake a join to init the tx rate */
764 zyd_newassoc(ni, 1); 764 zyd_newassoc(ni, 1);
765 } 765 }
766 766
767 /* start automatic rate control timer */ 767 /* start automatic rate control timer */
768 if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) 768 if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE)
769 callout_reset(&sc->sc_amrr_ch, hz, zyd_amrr_timeout, sc); 769 callout_reset(&sc->sc_amrr_ch, hz, zyd_amrr_timeout, sc);
770 770
771 break; 771 break;
772 } 772 }
773 } 773 }
774 774
775 sc->sc_newstate(ic, sc->sc_state, -1); 775 sc->sc_newstate(ic, sc->sc_state, -1);
776} 776}
777 777
778Static int 778Static int
779zyd_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg) 779zyd_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
780{ 780{
781 struct zyd_softc *sc = ic->ic_ifp->if_softc; 781 struct zyd_softc *sc = ic->ic_ifp->if_softc;
782 782
783 if (!sc->attached) 783 if (!sc->attached)
784 return ENXIO; 784 return ENXIO;
785 785
786 usb_rem_task(sc->sc_udev, &sc->sc_task); 786 usb_rem_task(sc->sc_udev, &sc->sc_task);
787 callout_stop(&sc->sc_scan_ch); 787 callout_stop(&sc->sc_scan_ch);
788 callout_stop(&sc->sc_amrr_ch); 788 callout_stop(&sc->sc_amrr_ch);
789 789
790 /* do it in a process context */ 790 /* do it in a process context */
791 sc->sc_state = nstate; 791 sc->sc_state = nstate;
792 usb_add_task(sc->sc_udev, &sc->sc_task, USB_TASKQ_DRIVER); 792 usb_add_task(sc->sc_udev, &sc->sc_task, USB_TASKQ_DRIVER);
793 793
794 return 0; 794 return 0;
795} 795}
796 796
797Static int 797Static int
798zyd_cmd(struct zyd_softc *sc, uint16_t code, const void *idata, int ilen, 798zyd_cmd(struct zyd_softc *sc, uint16_t code, const void *idata, int ilen,
799 void *odata, int olen, u_int flags) 799 void *odata, int olen, u_int flags)
800{ 800{
801 usbd_xfer_handle xfer; 801 usbd_xfer_handle xfer;
802 struct zyd_cmd cmd; 802 struct zyd_cmd cmd;
803 struct rq rq; 803 struct rq rq;
804 uint16_t xferflags; 804 uint16_t xferflags;
805 usbd_status error; 805 int error;
 806 usbd_status uerror;
806 int s = 0; 807 int s = 0;
807 808
808 if ((xfer = usbd_alloc_xfer(sc->sc_udev)) == NULL) 809 if ((xfer = usbd_alloc_xfer(sc->sc_udev)) == NULL)
809 return ENOMEM; 810 return ENOMEM;
810 811
811 cmd.code = htole16(code); 812 cmd.code = htole16(code);
812 bcopy(idata, cmd.data, ilen); 813 bcopy(idata, cmd.data, ilen);
813 814
814 xferflags = USBD_FORCE_SHORT_XFER; 815 xferflags = USBD_FORCE_SHORT_XFER;
815 if (!(flags & ZYD_CMD_FLAG_READ)) 816 if (!(flags & ZYD_CMD_FLAG_READ))
816 xferflags |= USBD_SYNCHRONOUS; 817 xferflags |= USBD_SYNCHRONOUS;
817 else { 818 else {
818 s = splusb(); 819 s = splusb();
819 rq.idata = idata; 820 rq.idata = idata;
820 rq.odata = odata; 821 rq.odata = odata;
821 rq.len = olen / sizeof (struct zyd_pair); 822 rq.len = olen / sizeof (struct zyd_pair);
822 SIMPLEQ_INSERT_TAIL(&sc->sc_rqh, &rq, rq); 823 SIMPLEQ_INSERT_TAIL(&sc->sc_rqh, &rq, rq);
823 } 824 }
824 825
825 usbd_setup_xfer(xfer, sc->zyd_ep[ZYD_ENDPT_IOUT], 0, &cmd, 826 usbd_setup_xfer(xfer, sc->zyd_ep[ZYD_ENDPT_IOUT], 0, &cmd,
826 sizeof (uint16_t) + ilen, xferflags, ZYD_INTR_TIMEOUT, NULL); 827 sizeof (uint16_t) + ilen, xferflags, ZYD_INTR_TIMEOUT, NULL);
827 error = usbd_transfer(xfer); 828 uerror = usbd_transfer(xfer);
828 if (error != USBD_IN_PROGRESS && error != 0) { 829 if (uerror != USBD_IN_PROGRESS && uerror != 0) {
829 if (flags & ZYD_CMD_FLAG_READ) 830 if (flags & ZYD_CMD_FLAG_READ)
830 splx(s); 831 splx(s);
831 printf("%s: could not send command (error=%s)\n", 832 printf("%s: could not send command (error=%s)\n",
832 device_xname(sc->sc_dev), usbd_errstr(error)); 833 device_xname(sc->sc_dev), usbd_errstr(uerror));
833 (void)usbd_free_xfer(xfer); 834 (void)usbd_free_xfer(xfer);
834 return EIO; 835 return EIO;
835 } 836 }
836 if (!(flags & ZYD_CMD_FLAG_READ)) { 837 if (!(flags & ZYD_CMD_FLAG_READ)) {
837 (void)usbd_free_xfer(xfer); 838 (void)usbd_free_xfer(xfer);
838 return 0; /* write: don't wait for reply */ 839 return 0; /* write: don't wait for reply */
839 } 840 }
840 /* wait at most one second for command reply */ 841 /* wait at most one second for command reply */
841 error = tsleep(odata, PCATCH, "zydcmd", hz); 842 error = tsleep(odata, PCATCH, "zydcmd", hz);
842 if (error == EWOULDBLOCK) 843 if (error == EWOULDBLOCK)
843 printf("%s: zyd_read sleep timeout\n", device_xname(sc->sc_dev)); 844 printf("%s: zyd_read sleep timeout\n", device_xname(sc->sc_dev));
844 SIMPLEQ_REMOVE(&sc->sc_rqh, &rq, rq, rq); 845 SIMPLEQ_REMOVE(&sc->sc_rqh, &rq, rq, rq);
845 splx(s); 846 splx(s);
846 847
847 (void)usbd_free_xfer(xfer); 848 (void)usbd_free_xfer(xfer);
848 return error; 849 return error;
849} 850}
850 851
851Static int 852Static int
852zyd_read16(struct zyd_softc *sc, uint16_t reg, uint16_t *val) 853zyd_read16(struct zyd_softc *sc, uint16_t reg, uint16_t *val)
853{ 854{
854 struct zyd_pair tmp; 855 struct zyd_pair tmp;
855 int error; 856 int error;
856 857
857 reg = htole16(reg); 858 reg = htole16(reg);
858 error = zyd_cmd(sc, ZYD_CMD_IORD, &reg, sizeof reg, &tmp, sizeof tmp, 859 error = zyd_cmd(sc, ZYD_CMD_IORD, &reg, sizeof reg, &tmp, sizeof tmp,
859 ZYD_CMD_FLAG_READ); 860 ZYD_CMD_FLAG_READ);
860 if (error == 0) 861 if (error == 0)
861 *val = le16toh(tmp.val); 862 *val = le16toh(tmp.val);
862 else 863 else
863 *val = 0; 864 *val = 0;
864 return error; 865 return error;
865} 866}
866 867
867Static int 868Static int
868zyd_read32(struct zyd_softc *sc, uint16_t reg, uint32_t *val) 869zyd_read32(struct zyd_softc *sc, uint16_t reg, uint32_t *val)
869{ 870{
870 struct zyd_pair tmp[2]; 871 struct zyd_pair tmp[2];
871 uint16_t regs[2]; 872 uint16_t regs[2];
872 int error; 873 int error;
873 874
874 regs[0] = htole16(ZYD_REG32_HI(reg)); 875 regs[0] = htole16(ZYD_REG32_HI(reg));
875 regs[1] = htole16(ZYD_REG32_LO(reg)); 876 regs[1] = htole16(ZYD_REG32_LO(reg));
876 error = zyd_cmd(sc, ZYD_CMD_IORD, regs, sizeof regs, tmp, sizeof tmp, 877 error = zyd_cmd(sc, ZYD_CMD_IORD, regs, sizeof regs, tmp, sizeof tmp,
877 ZYD_CMD_FLAG_READ); 878 ZYD_CMD_FLAG_READ);
878 if (error == 0) 879 if (error == 0)
879 *val = le16toh(tmp[0].val) << 16 | le16toh(tmp[1].val); 880 *val = le16toh(tmp[0].val) << 16 | le16toh(tmp[1].val);
880 else 881 else
881 *val = 0; 882 *val = 0;
882 return error; 883 return error;
883} 884}
884 885
885Static int 886Static int
886zyd_write16(struct zyd_softc *sc, uint16_t reg, uint16_t val) 887zyd_write16(struct zyd_softc *sc, uint16_t reg, uint16_t val)
887{ 888{
888 struct zyd_pair pair; 889 struct zyd_pair pair;
889 890
890 pair.reg = htole16(reg); 891 pair.reg = htole16(reg);
891 pair.val = htole16(val); 892 pair.val = htole16(val);
892 893
893 return zyd_cmd(sc, ZYD_CMD_IOWR, &pair, sizeof pair, NULL, 0, 0); 894 return zyd_cmd(sc, ZYD_CMD_IOWR, &pair, sizeof pair, NULL, 0, 0);
894} 895}
895 896
896Static int 897Static int
897zyd_write32(struct zyd_softc *sc, uint16_t reg, uint32_t val) 898zyd_write32(struct zyd_softc *sc, uint16_t reg, uint32_t val)
898{ 899{
899 struct zyd_pair pair[2]; 900 struct zyd_pair pair[2];
900 901
901 pair[0].reg = htole16(ZYD_REG32_HI(reg)); 902 pair[0].reg = htole16(ZYD_REG32_HI(reg));
902 pair[0].val = htole16(val >> 16); 903 pair[0].val = htole16(val >> 16);
903 pair[1].reg = htole16(ZYD_REG32_LO(reg)); 904 pair[1].reg = htole16(ZYD_REG32_LO(reg));
904 pair[1].val = htole16(val & 0xffff); 905 pair[1].val = htole16(val & 0xffff);
905 906
906 return zyd_cmd(sc, ZYD_CMD_IOWR, pair, sizeof pair, NULL, 0, 0); 907 return zyd_cmd(sc, ZYD_CMD_IOWR, pair, sizeof pair, NULL, 0, 0);
907} 908}
908 909
909Static int 910Static int
910zyd_rfwrite(struct zyd_softc *sc, uint32_t val) 911zyd_rfwrite(struct zyd_softc *sc, uint32_t val)
911{ 912{
912 struct zyd_rf *rf = &sc->sc_rf; 913 struct zyd_rf *rf = &sc->sc_rf;
913 struct zyd_rfwrite req; 914 struct zyd_rfwrite req;
914 uint16_t cr203; 915 uint16_t cr203;
915 int i; 916 int i;
916 917
917 (void)zyd_read16(sc, ZYD_CR203, &cr203); 918 (void)zyd_read16(sc, ZYD_CR203, &cr203);
918 cr203 &= ~(ZYD_RF_IF_LE | ZYD_RF_CLK | ZYD_RF_DATA); 919 cr203 &= ~(ZYD_RF_IF_LE | ZYD_RF_CLK | ZYD_RF_DATA);
919 920
920 req.code = htole16(2); 921 req.code = htole16(2);
921 req.width = htole16(rf->width); 922 req.width = htole16(rf->width);
922 for (i = 0; i < rf->width; i++) { 923 for (i = 0; i < rf->width; i++) {
923 req.bit[i] = htole16(cr203); 924 req.bit[i] = htole16(cr203);
924 if (val & (1 << (rf->width - 1 - i))) 925 if (val & (1 << (rf->width - 1 - i)))
925 req.bit[i] |= htole16(ZYD_RF_DATA); 926 req.bit[i] |= htole16(ZYD_RF_DATA);
926 } 927 }
927 return zyd_cmd(sc, ZYD_CMD_RFCFG, &req, 4 + 2 * rf->width, NULL, 0, 0); 928 return zyd_cmd(sc, ZYD_CMD_RFCFG, &req, 4 + 2 * rf->width, NULL, 0, 0);
928} 929}
929 930
930Static void 931Static void
931zyd_lock_phy(struct zyd_softc *sc) 932zyd_lock_phy(struct zyd_softc *sc)
932{ 933{
933 uint32_t tmp; 934 uint32_t tmp;
934 935
935 (void)zyd_read32(sc, ZYD_MAC_MISC, &tmp); 936 (void)zyd_read32(sc, ZYD_MAC_MISC, &tmp);
936 tmp &= ~ZYD_UNLOCK_PHY_REGS; 937 tmp &= ~ZYD_UNLOCK_PHY_REGS;
937 (void)zyd_write32(sc, ZYD_MAC_MISC, tmp); 938 (void)zyd_write32(sc, ZYD_MAC_MISC, tmp);
938} 939}
939 940
940Static void 941Static void
941zyd_unlock_phy(struct zyd_softc *sc) 942zyd_unlock_phy(struct zyd_softc *sc)
942{ 943{
943 uint32_t tmp; 944 uint32_t tmp;
944 945
945 (void)zyd_read32(sc, ZYD_MAC_MISC, &tmp); 946 (void)zyd_read32(sc, ZYD_MAC_MISC, &tmp);
946 tmp |= ZYD_UNLOCK_PHY_REGS; 947 tmp |= ZYD_UNLOCK_PHY_REGS;
947 (void)zyd_write32(sc, ZYD_MAC_MISC, tmp); 948 (void)zyd_write32(sc, ZYD_MAC_MISC, tmp);
948} 949}
949 950
950/* 951/*
951 * RFMD RF methods. 952 * RFMD RF methods.
952 */ 953 */
953Static int 954Static int
954zyd_rfmd_init(struct zyd_rf *rf) 955zyd_rfmd_init(struct zyd_rf *rf)
955{ 956{
956 struct zyd_softc *sc = rf->rf_sc; 957 struct zyd_softc *sc = rf->rf_sc;
957 static const struct zyd_phy_pair phyini[] = ZYD_RFMD_PHY; 958 static const struct zyd_phy_pair phyini[] = ZYD_RFMD_PHY;
958 static const uint32_t rfini[] = ZYD_RFMD_RF; 959 static const uint32_t rfini[] = ZYD_RFMD_RF;
959 int error; 960 int error;
960 size_t i; 961 size_t i;
961 962
962 /* init RF-dependent PHY registers */ 963 /* init RF-dependent PHY registers */
963 for (i = 0; i < __arraycount(phyini); i++) { 964 for (i = 0; i < __arraycount(phyini); i++) {
964 error = zyd_write16(sc, phyini[i].reg, phyini[i].val); 965 error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
965 if (error != 0) 966 if (error != 0)
966 return error; 967 return error;
967 } 968 }
968 969
969 /* init RFMD radio */ 970 /* init RFMD radio */
970 for (i = 0; i < __arraycount(rfini); i++) { 971 for (i = 0; i < __arraycount(rfini); i++) {
971 if ((error = zyd_rfwrite(sc, rfini[i])) != 0) 972 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
972 return error; 973 return error;
973 } 974 }
974 return 0; 975 return 0;
975} 976}
976 977
977Static int 978Static int
978zyd_rfmd_switch_radio(struct zyd_rf *rf, int on) 979zyd_rfmd_switch_radio(struct zyd_rf *rf, int on)
979{ 980{
980 struct zyd_softc *sc = rf->rf_sc; 981 struct zyd_softc *sc = rf->rf_sc;
981 982
982 (void)zyd_write16(sc, ZYD_CR10, on ? 0x89 : 0x15); 983 (void)zyd_write16(sc, ZYD_CR10, on ? 0x89 : 0x15);
983 (void)zyd_write16(sc, ZYD_CR11, on ? 0x00 : 0x81); 984 (void)zyd_write16(sc, ZYD_CR11, on ? 0x00 : 0x81);
984 985
985 return 0; 986 return 0;
986} 987}
987 988
988Static int 989Static int
989zyd_rfmd_set_channel(struct zyd_rf *rf, uint8_t chan) 990zyd_rfmd_set_channel(struct zyd_rf *rf, uint8_t chan)
990{ 991{
991 struct zyd_softc *sc = rf->rf_sc; 992 struct zyd_softc *sc = rf->rf_sc;
992 static const struct { 993 static const struct {
993 uint32_t r1, r2; 994 uint32_t r1, r2;
994 } rfprog[] = ZYD_RFMD_CHANTABLE; 995 } rfprog[] = ZYD_RFMD_CHANTABLE;
995 996
996 (void)zyd_rfwrite(sc, rfprog[chan - 1].r1); 997 (void)zyd_rfwrite(sc, rfprog[chan - 1].r1);
997 (void)zyd_rfwrite(sc, rfprog[chan - 1].r2); 998 (void)zyd_rfwrite(sc, rfprog[chan - 1].r2);
998 999
999 return 0; 1000 return 0;
1000} 1001}
1001 1002
1002/* 1003/*
1003 * AL2230 RF methods. 1004 * AL2230 RF methods.
1004 */ 1005 */
1005Static int 1006Static int
1006zyd_al2230_init(struct zyd_rf *rf) 1007zyd_al2230_init(struct zyd_rf *rf)
1007{ 1008{
1008 struct zyd_softc *sc = rf->rf_sc; 1009 struct zyd_softc *sc = rf->rf_sc;
1009 static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY; 1010 static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY;
1010 static const struct zyd_phy_pair phy2230s[] = ZYD_AL2230S_PHY_INIT; 1011 static const struct zyd_phy_pair phy2230s[] = ZYD_AL2230S_PHY_INIT;
1011 static const uint32_t rfini[] = ZYD_AL2230_RF; 1012 static const uint32_t rfini[] = ZYD_AL2230_RF;
1012 int error; 1013 int error;
1013 size_t i; 1014 size_t i;
1014 1015
1015 /* init RF-dependent PHY registers */ 1016 /* init RF-dependent PHY registers */
1016 for (i = 0; i < __arraycount(phyini); i++) { 1017 for (i = 0; i < __arraycount(phyini); i++) {
1017 error = zyd_write16(sc, phyini[i].reg, phyini[i].val); 1018 error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1018 if (error != 0) 1019 if (error != 0)
1019 return error; 1020 return error;
1020 } 1021 }
1021 1022
1022 if (sc->rf_rev == ZYD_RF_AL2230S) { 1023 if (sc->rf_rev == ZYD_RF_AL2230S) {
1023 for (i = 0; i < __arraycount(phy2230s); i++) { 1024 for (i = 0; i < __arraycount(phy2230s); i++) {
1024 error = zyd_write16(sc, phy2230s[i].reg, 1025 error = zyd_write16(sc, phy2230s[i].reg,
1025 phy2230s[i].val); 1026 phy2230s[i].val);
1026 if (error != 0) 1027 if (error != 0)
1027 return error; 1028 return error;
1028 } 1029 }
1029 } 1030 }
1030 1031
1031 /* init AL2230 radio */ 1032 /* init AL2230 radio */
1032 for (i = 0; i < __arraycount(rfini); i++) { 1033 for (i = 0; i < __arraycount(rfini); i++) {
1033 if ((error = zyd_rfwrite(sc, rfini[i])) != 0) 1034 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1034 return error; 1035 return error;
1035 } 1036 }
1036 return 0; 1037 return 0;
1037} 1038}
1038 1039
1039Static int 1040Static int
1040zyd_al2230_init_b(struct zyd_rf *rf) 1041zyd_al2230_init_b(struct zyd_rf *rf)
1041{ 1042{
1042 struct zyd_softc *sc = rf->rf_sc; 1043 struct zyd_softc *sc = rf->rf_sc;
1043 static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY_B; 1044 static const struct zyd_phy_pair phyini[] = ZYD_AL2230_PHY_B;
1044 static const uint32_t rfini[] = ZYD_AL2230_RF_B; 1045 static const uint32_t rfini[] = ZYD_AL2230_RF_B;
1045 int error; 1046 int error;
1046 size_t i; 1047 size_t i;
1047 1048
1048 /* init RF-dependent PHY registers */ 1049 /* init RF-dependent PHY registers */
1049 for (i = 0; i < __arraycount(phyini); i++) { 1050 for (i = 0; i < __arraycount(phyini); i++) {
1050 error = zyd_write16(sc, phyini[i].reg, phyini[i].val); 1051 error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1051 if (error != 0) 1052 if (error != 0)
1052 return error; 1053 return error;
1053 } 1054 }
1054 1055
1055 /* init AL2230 radio */ 1056 /* init AL2230 radio */
1056 for (i = 0; i < __arraycount(rfini); i++) { 1057 for (i = 0; i < __arraycount(rfini); i++) {
1057 if ((error = zyd_rfwrite(sc, rfini[i])) != 0) 1058 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1058 return error; 1059 return error;
1059 } 1060 }
1060 return 0; 1061 return 0;
1061} 1062}
1062 1063
1063Static int 1064Static int
1064zyd_al2230_switch_radio(struct zyd_rf *rf, int on) 1065zyd_al2230_switch_radio(struct zyd_rf *rf, int on)
1065{ 1066{
1066 struct zyd_softc *sc = rf->rf_sc; 1067 struct zyd_softc *sc = rf->rf_sc;
1067 int on251 = (sc->mac_rev == ZYD_ZD1211) ? 0x3f : 0x7f; 1068 int on251 = (sc->mac_rev == ZYD_ZD1211) ? 0x3f : 0x7f;
1068 1069
1069 (void)zyd_write16(sc, ZYD_CR11, on ? 0x00 : 0x04); 1070 (void)zyd_write16(sc, ZYD_CR11, on ? 0x00 : 0x04);
1070 (void)zyd_write16(sc, ZYD_CR251, on ? on251 : 0x2f); 1071 (void)zyd_write16(sc, ZYD_CR251, on ? on251 : 0x2f);
1071 1072
1072 return 0; 1073 return 0;
1073} 1074}
1074 1075
1075Static int 1076Static int
1076zyd_al2230_set_channel(struct zyd_rf *rf, uint8_t chan) 1077zyd_al2230_set_channel(struct zyd_rf *rf, uint8_t chan)
1077{ 1078{
1078 struct zyd_softc *sc = rf->rf_sc; 1079 struct zyd_softc *sc = rf->rf_sc;
1079 static const struct { 1080 static const struct {
1080 uint32_t r1, r2, r3; 1081 uint32_t r1, r2, r3;
1081 } rfprog[] = ZYD_AL2230_CHANTABLE; 1082 } rfprog[] = ZYD_AL2230_CHANTABLE;
1082 1083
1083 (void)zyd_rfwrite(sc, rfprog[chan - 1].r1); 1084 (void)zyd_rfwrite(sc, rfprog[chan - 1].r1);
1084 (void)zyd_rfwrite(sc, rfprog[chan - 1].r2); 1085 (void)zyd_rfwrite(sc, rfprog[chan - 1].r2);
1085 (void)zyd_rfwrite(sc, rfprog[chan - 1].r3); 1086 (void)zyd_rfwrite(sc, rfprog[chan - 1].r3);
1086 1087
1087 (void)zyd_write16(sc, ZYD_CR138, 0x28); 1088 (void)zyd_write16(sc, ZYD_CR138, 0x28);
1088 (void)zyd_write16(sc, ZYD_CR203, 0x06); 1089 (void)zyd_write16(sc, ZYD_CR203, 0x06);
1089 1090
1090 return 0; 1091 return 0;
1091} 1092}
1092 1093
1093/* 1094/*
1094 * AL7230B RF methods. 1095 * AL7230B RF methods.
1095 */ 1096 */
1096Static int 1097Static int
1097zyd_al7230B_init(struct zyd_rf *rf) 1098zyd_al7230B_init(struct zyd_rf *rf)
1098{ 1099{
1099 struct zyd_softc *sc = rf->rf_sc; 1100 struct zyd_softc *sc = rf->rf_sc;
1100 static const struct zyd_phy_pair phyini_1[] = ZYD_AL7230B_PHY_1; 1101 static const struct zyd_phy_pair phyini_1[] = ZYD_AL7230B_PHY_1;
1101 static const struct zyd_phy_pair phyini_2[] = ZYD_AL7230B_PHY_2; 1102 static const struct zyd_phy_pair phyini_2[] = ZYD_AL7230B_PHY_2;
1102 static const struct zyd_phy_pair phyini_3[] = ZYD_AL7230B_PHY_3; 1103 static const struct zyd_phy_pair phyini_3[] = ZYD_AL7230B_PHY_3;
1103 static const uint32_t rfini_1[] = ZYD_AL7230B_RF_1; 1104 static const uint32_t rfini_1[] = ZYD_AL7230B_RF_1;
1104 static const uint32_t rfini_2[] = ZYD_AL7230B_RF_2; 1105 static const uint32_t rfini_2[] = ZYD_AL7230B_RF_2;
1105 int error; 1106 int error;
1106 size_t i; 1107 size_t i;
1107 1108
1108 /* for AL7230B, PHY and RF need to be initialized in "phases" */ 1109 /* for AL7230B, PHY and RF need to be initialized in "phases" */
1109 1110
1110 /* init RF-dependent PHY registers, part one */ 1111 /* init RF-dependent PHY registers, part one */
1111 for (i = 0; i < __arraycount(phyini_1); i++) { 1112 for (i = 0; i < __arraycount(phyini_1); i++) {
1112 error = zyd_write16(sc, phyini_1[i].reg, phyini_1[i].val); 1113 error = zyd_write16(sc, phyini_1[i].reg, phyini_1[i].val);
1113 if (error != 0) 1114 if (error != 0)
1114 return error; 1115 return error;
1115 } 1116 }
1116 /* init AL7230B radio, part one */ 1117 /* init AL7230B radio, part one */
1117 for (i = 0; i < __arraycount(rfini_1); i++) { 1118 for (i = 0; i < __arraycount(rfini_1); i++) {
1118 if ((error = zyd_rfwrite(sc, rfini_1[i])) != 0) 1119 if ((error = zyd_rfwrite(sc, rfini_1[i])) != 0)
1119 return error; 1120 return error;
1120 } 1121 }
1121 /* init RF-dependent PHY registers, part two */ 1122 /* init RF-dependent PHY registers, part two */
1122 for (i = 0; i < __arraycount(phyini_2); i++) { 1123 for (i = 0; i < __arraycount(phyini_2); i++) {
1123 error = zyd_write16(sc, phyini_2[i].reg, phyini_2[i].val); 1124 error = zyd_write16(sc, phyini_2[i].reg, phyini_2[i].val);
1124 if (error != 0) 1125 if (error != 0)
1125 return error; 1126 return error;
1126 } 1127 }
1127 /* init AL7230B radio, part two */ 1128 /* init AL7230B radio, part two */
1128 for (i = 0; i < __arraycount(rfini_2); i++) { 1129 for (i = 0; i < __arraycount(rfini_2); i++) {
1129 if ((error = zyd_rfwrite(sc, rfini_2[i])) != 0) 1130 if ((error = zyd_rfwrite(sc, rfini_2[i])) != 0)
1130 return error; 1131 return error;
1131 } 1132 }
1132 /* init RF-dependent PHY registers, part three */ 1133 /* init RF-dependent PHY registers, part three */
1133 for (i = 0; i < __arraycount(phyini_3); i++) { 1134 for (i = 0; i < __arraycount(phyini_3); i++) {
1134 error = zyd_write16(sc, phyini_3[i].reg, phyini_3[i].val); 1135 error = zyd_write16(sc, phyini_3[i].reg, phyini_3[i].val);
1135 if (error != 0) 1136 if (error != 0)
1136 return error; 1137 return error;
1137 } 1138 }
1138 1139
1139 return 0; 1140 return 0;
1140} 1141}
1141 1142
1142Static int 1143Static int
1143zyd_al7230B_switch_radio(struct zyd_rf *rf, int on) 1144zyd_al7230B_switch_radio(struct zyd_rf *rf, int on)
1144{ 1145{
1145 struct zyd_softc *sc = rf->rf_sc; 1146 struct zyd_softc *sc = rf->rf_sc;
1146 1147
1147 (void)zyd_write16(sc, ZYD_CR11, on ? 0x00 : 0x04); 1148 (void)zyd_write16(sc, ZYD_CR11, on ? 0x00 : 0x04);
1148 (void)zyd_write16(sc, ZYD_CR251, on ? 0x3f : 0x2f); 1149 (void)zyd_write16(sc, ZYD_CR251, on ? 0x3f : 0x2f);
1149 1150
1150 return 0; 1151 return 0;
1151} 1152}
1152 1153
1153Static int 1154Static int
1154zyd_al7230B_set_channel(struct zyd_rf *rf, uint8_t chan) 1155zyd_al7230B_set_channel(struct zyd_rf *rf, uint8_t chan)
1155{ 1156{
1156 struct zyd_softc *sc = rf->rf_sc; 1157 struct zyd_softc *sc = rf->rf_sc;
1157 static const struct { 1158 static const struct {
1158 uint32_t r1, r2; 1159 uint32_t r1, r2;
1159 } rfprog[] = ZYD_AL7230B_CHANTABLE; 1160 } rfprog[] = ZYD_AL7230B_CHANTABLE;
1160 static const uint32_t rfsc[] = ZYD_AL7230B_RF_SETCHANNEL; 1161 static const uint32_t rfsc[] = ZYD_AL7230B_RF_SETCHANNEL;
1161 int error; 1162 int error;
1162 size_t i; 1163 size_t i;
1163 1164
1164 (void)zyd_write16(sc, ZYD_CR240, 0x57); 1165 (void)zyd_write16(sc, ZYD_CR240, 0x57);
1165 (void)zyd_write16(sc, ZYD_CR251, 0x2f); 1166 (void)zyd_write16(sc, ZYD_CR251, 0x2f);
1166 1167
1167 for (i = 0; i < __arraycount(rfsc); i++) { 1168 for (i = 0; i < __arraycount(rfsc); i++) {
1168 if ((error = zyd_rfwrite(sc, rfsc[i])) != 0) 1169 if ((error = zyd_rfwrite(sc, rfsc[i])) != 0)
1169 return error; 1170 return error;
1170 } 1171 }
1171 1172
1172 (void)zyd_write16(sc, ZYD_CR128, 0x14); 1173 (void)zyd_write16(sc, ZYD_CR128, 0x14);
1173 (void)zyd_write16(sc, ZYD_CR129, 0x12); 1174 (void)zyd_write16(sc, ZYD_CR129, 0x12);
1174 (void)zyd_write16(sc, ZYD_CR130, 0x10); 1175 (void)zyd_write16(sc, ZYD_CR130, 0x10);
1175 (void)zyd_write16(sc, ZYD_CR38, 0x38); 1176 (void)zyd_write16(sc, ZYD_CR38, 0x38);
1176 (void)zyd_write16(sc, ZYD_CR136, 0xdf); 1177 (void)zyd_write16(sc, ZYD_CR136, 0xdf);
1177 1178
1178 (void)zyd_rfwrite(sc, rfprog[chan - 1].r1); 1179 (void)zyd_rfwrite(sc, rfprog[chan - 1].r1);
1179 (void)zyd_rfwrite(sc, rfprog[chan - 1].r2); 1180 (void)zyd_rfwrite(sc, rfprog[chan - 1].r2);
1180 (void)zyd_rfwrite(sc, 0x3c9000); 1181 (void)zyd_rfwrite(sc, 0x3c9000);
1181 1182
1182 (void)zyd_write16(sc, ZYD_CR251, 0x3f); 1183 (void)zyd_write16(sc, ZYD_CR251, 0x3f);
1183 (void)zyd_write16(sc, ZYD_CR203, 0x06); 1184 (void)zyd_write16(sc, ZYD_CR203, 0x06);
1184 (void)zyd_write16(sc, ZYD_CR240, 0x08); 1185 (void)zyd_write16(sc, ZYD_CR240, 0x08);
1185 1186
1186 return 0; 1187 return 0;
1187} 1188}
1188 1189
1189/* 1190/*
1190 * AL2210 RF methods. 1191 * AL2210 RF methods.
1191 */ 1192 */
1192Static int 1193Static int
1193zyd_al2210_init(struct zyd_rf *rf) 1194zyd_al2210_init(struct zyd_rf *rf)
1194{ 1195{
1195 struct zyd_softc *sc = rf->rf_sc; 1196 struct zyd_softc *sc = rf->rf_sc;
1196 static const struct zyd_phy_pair phyini[] = ZYD_AL2210_PHY; 1197 static const struct zyd_phy_pair phyini[] = ZYD_AL2210_PHY;
1197 static const uint32_t rfini[] = ZYD_AL2210_RF; 1198 static const uint32_t rfini[] = ZYD_AL2210_RF;
1198 uint32_t tmp; 1199 uint32_t tmp;
1199 int error; 1200 int error;
1200 size_t i; 1201 size_t i;
1201 1202
1202 (void)zyd_write32(sc, ZYD_CR18, 2); 1203 (void)zyd_write32(sc, ZYD_CR18, 2);
1203 1204
1204 /* init RF-dependent PHY registers */ 1205 /* init RF-dependent PHY registers */
1205 for (i = 0; i < __arraycount(phyini); i++) { 1206 for (i = 0; i < __arraycount(phyini); i++) {
1206 error = zyd_write16(sc, phyini[i].reg, phyini[i].val); 1207 error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1207 if (error != 0) 1208 if (error != 0)
1208 return error; 1209 return error;
1209 } 1210 }
1210 /* init AL2210 radio */ 1211 /* init AL2210 radio */
1211 for (i = 0; i < __arraycount(rfini); i++) { 1212 for (i = 0; i < __arraycount(rfini); i++) {
1212 if ((error = zyd_rfwrite(sc, rfini[i])) != 0) 1213 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1213 return error; 1214 return error;
1214 } 1215 }
1215 (void)zyd_write16(sc, ZYD_CR47, 0x1e); 1216 (void)zyd_write16(sc, ZYD_CR47, 0x1e);
1216 (void)zyd_read32(sc, ZYD_CR_RADIO_PD, &tmp); 1217 (void)zyd_read32(sc, ZYD_CR_RADIO_PD, &tmp);
1217 (void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp & ~1); 1218 (void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp & ~1);
1218 (void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp | 1); 1219 (void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp | 1);
1219 (void)zyd_write32(sc, ZYD_CR_RFCFG, 0x05); 1220 (void)zyd_write32(sc, ZYD_CR_RFCFG, 0x05);
1220 (void)zyd_write32(sc, ZYD_CR_RFCFG, 0x00); 1221 (void)zyd_write32(sc, ZYD_CR_RFCFG, 0x00);
1221 (void)zyd_write16(sc, ZYD_CR47, 0x1e); 1222 (void)zyd_write16(sc, ZYD_CR47, 0x1e);
1222 (void)zyd_write32(sc, ZYD_CR18, 3); 1223 (void)zyd_write32(sc, ZYD_CR18, 3);
1223 1224
1224 return 0; 1225 return 0;
1225} 1226}
1226 1227
1227Static int 1228Static int
1228zyd_al2210_switch_radio(struct zyd_rf *rf, int on) 1229zyd_al2210_switch_radio(struct zyd_rf *rf, int on)
1229{ 1230{
1230 /* vendor driver does nothing for this RF chip */ 1231 /* vendor driver does nothing for this RF chip */
1231 1232
1232 return 0; 1233 return 0;
1233} 1234}
1234 1235
1235Static int 1236Static int
1236zyd_al2210_set_channel(struct zyd_rf *rf, uint8_t chan) 1237zyd_al2210_set_channel(struct zyd_rf *rf, uint8_t chan)
1237{ 1238{
1238 struct zyd_softc *sc = rf->rf_sc; 1239 struct zyd_softc *sc = rf->rf_sc;
1239 static const uint32_t rfprog[] = ZYD_AL2210_CHANTABLE; 1240 static const uint32_t rfprog[] = ZYD_AL2210_CHANTABLE;
1240 uint32_t tmp; 1241 uint32_t tmp;
1241 1242
1242 (void)zyd_write32(sc, ZYD_CR18, 2); 1243 (void)zyd_write32(sc, ZYD_CR18, 2);
1243 (void)zyd_write16(sc, ZYD_CR47, 0x1e); 1244 (void)zyd_write16(sc, ZYD_CR47, 0x1e);
1244 (void)zyd_read32(sc, ZYD_CR_RADIO_PD, &tmp); 1245 (void)zyd_read32(sc, ZYD_CR_RADIO_PD, &tmp);
1245 (void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp & ~1); 1246 (void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp & ~1);
1246 (void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp | 1); 1247 (void)zyd_write32(sc, ZYD_CR_RADIO_PD, tmp | 1);
1247 (void)zyd_write32(sc, ZYD_CR_RFCFG, 0x05); 1248 (void)zyd_write32(sc, ZYD_CR_RFCFG, 0x05);
1248 1249
1249 (void)zyd_write32(sc, ZYD_CR_RFCFG, 0x00); 1250 (void)zyd_write32(sc, ZYD_CR_RFCFG, 0x00);
1250 (void)zyd_write16(sc, ZYD_CR47, 0x1e); 1251 (void)zyd_write16(sc, ZYD_CR47, 0x1e);
1251 1252
1252 /* actually set the channel */ 1253 /* actually set the channel */
1253 (void)zyd_rfwrite(sc, rfprog[chan - 1]); 1254 (void)zyd_rfwrite(sc, rfprog[chan - 1]);
1254 1255
1255 (void)zyd_write32(sc, ZYD_CR18, 3); 1256 (void)zyd_write32(sc, ZYD_CR18, 3);
1256 1257
1257 return 0; 1258 return 0;
1258} 1259}
1259 1260
1260/* 1261/*
1261 * GCT RF methods. 1262 * GCT RF methods.
1262 */ 1263 */
1263Static int 1264Static int
1264zyd_gct_init(struct zyd_rf *rf) 1265zyd_gct_init(struct zyd_rf *rf)
1265{ 1266{
1266 struct zyd_softc *sc = rf->rf_sc; 1267 struct zyd_softc *sc = rf->rf_sc;
1267 static const struct zyd_phy_pair phyini[] = ZYD_GCT_PHY; 1268 static const struct zyd_phy_pair phyini[] = ZYD_GCT_PHY;
1268 static const uint32_t rfini[] = ZYD_GCT_RF; 1269 static const uint32_t rfini[] = ZYD_GCT_RF;
1269 int error; 1270 int error;
1270 size_t i; 1271 size_t i;
1271 1272
1272 /* init RF-dependent PHY registers */ 1273 /* init RF-dependent PHY registers */
1273 for (i = 0; i < __arraycount(phyini); i++) { 1274 for (i = 0; i < __arraycount(phyini); i++) {
1274 error = zyd_write16(sc, phyini[i].reg, phyini[i].val); 1275 error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1275 if (error != 0) 1276 if (error != 0)
1276 return error; 1277 return error;
1277 } 1278 }
1278 /* init cgt radio */ 1279 /* init cgt radio */
1279 for (i = 0; i < __arraycount(rfini); i++) { 1280 for (i = 0; i < __arraycount(rfini); i++) {
1280 if ((error = zyd_rfwrite(sc, rfini[i])) != 0) 1281 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1281 return error; 1282 return error;
1282 } 1283 }
1283 return 0; 1284 return 0;
1284} 1285}
1285 1286
1286Static int 1287Static int
1287zyd_gct_switch_radio(struct zyd_rf *rf, int on) 1288zyd_gct_switch_radio(struct zyd_rf *rf, int on)
1288{ 1289{
1289 /* vendor driver does nothing for this RF chip */ 1290 /* vendor driver does nothing for this RF chip */
1290 1291
1291 return 0; 1292 return 0;
1292} 1293}
1293 1294
1294Static int 1295Static int
1295zyd_gct_set_channel(struct zyd_rf *rf, uint8_t chan) 1296zyd_gct_set_channel(struct zyd_rf *rf, uint8_t chan)
1296{ 1297{
1297 struct zyd_softc *sc = rf->rf_sc; 1298 struct zyd_softc *sc = rf->rf_sc;
1298 static const uint32_t rfprog[] = ZYD_GCT_CHANTABLE; 1299 static const uint32_t rfprog[] = ZYD_GCT_CHANTABLE;
1299 1300
1300 (void)zyd_rfwrite(sc, 0x1c0000); 1301 (void)zyd_rfwrite(sc, 0x1c0000);
1301 (void)zyd_rfwrite(sc, rfprog[chan - 1]); 1302 (void)zyd_rfwrite(sc, rfprog[chan - 1]);
1302 (void)zyd_rfwrite(sc, 0x1c0008); 1303 (void)zyd_rfwrite(sc, 0x1c0008);
1303 1304
1304 return 0; 1305 return 0;
1305} 1306}
1306 1307
1307/* 1308/*
1308 * Maxim RF methods. 1309 * Maxim RF methods.
1309 */ 1310 */
1310Static int 1311Static int
1311zyd_maxim_init(struct zyd_rf *rf) 1312zyd_maxim_init(struct zyd_rf *rf)
1312{ 1313{
1313 struct zyd_softc *sc = rf->rf_sc; 1314 struct zyd_softc *sc = rf->rf_sc;
1314 static const struct zyd_phy_pair phyini[] = ZYD_MAXIM_PHY; 1315 static const struct zyd_phy_pair phyini[] = ZYD_MAXIM_PHY;
1315 static const uint32_t rfini[] = ZYD_MAXIM_RF; 1316 static const uint32_t rfini[] = ZYD_MAXIM_RF;
1316 uint16_t tmp; 1317 uint16_t tmp;
1317 int error; 1318 int error;
1318 size_t i; 1319 size_t i;
1319 1320
1320 /* init RF-dependent PHY registers */ 1321 /* init RF-dependent PHY registers */
1321 for (i = 0; i < __arraycount(phyini); i++) { 1322 for (i = 0; i < __arraycount(phyini); i++) {
1322 error = zyd_write16(sc, phyini[i].reg, phyini[i].val); 1323 error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1323 if (error != 0) 1324 if (error != 0)
1324 return error; 1325 return error;
1325 } 1326 }
1326 (void)zyd_read16(sc, ZYD_CR203, &tmp); 1327 (void)zyd_read16(sc, ZYD_CR203, &tmp);
1327 (void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4)); 1328 (void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4));
1328 1329
1329 /* init maxim radio */ 1330 /* init maxim radio */
1330 for (i = 0; i < __arraycount(rfini); i++) { 1331 for (i = 0; i < __arraycount(rfini); i++) {
1331 if ((error = zyd_rfwrite(sc, rfini[i])) != 0) 1332 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1332 return error; 1333 return error;
1333 } 1334 }
1334 (void)zyd_read16(sc, ZYD_CR203, &tmp); 1335 (void)zyd_read16(sc, ZYD_CR203, &tmp);
1335 (void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4)); 1336 (void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4));
1336 1337
1337 return 0; 1338 return 0;
1338} 1339}
1339 1340
1340Static int 1341Static int
1341zyd_maxim_switch_radio(struct zyd_rf *rf, int on) 1342zyd_maxim_switch_radio(struct zyd_rf *rf, int on)
1342{ 1343{
1343 /* vendor driver does nothing for this RF chip */ 1344 /* vendor driver does nothing for this RF chip */
1344 1345
1345 return 0; 1346 return 0;
1346} 1347}
1347 1348
1348Static int 1349Static int
1349zyd_maxim_set_channel(struct zyd_rf *rf, uint8_t chan) 1350zyd_maxim_set_channel(struct zyd_rf *rf, uint8_t chan)
1350{ 1351{
1351 struct zyd_softc *sc = rf->rf_sc; 1352 struct zyd_softc *sc = rf->rf_sc;
1352 static const struct zyd_phy_pair phyini[] = ZYD_MAXIM_PHY; 1353 static const struct zyd_phy_pair phyini[] = ZYD_MAXIM_PHY;
1353 static const uint32_t rfini[] = ZYD_MAXIM_RF; 1354 static const uint32_t rfini[] = ZYD_MAXIM_RF;
1354 static const struct { 1355 static const struct {
1355 uint32_t r1, r2; 1356 uint32_t r1, r2;
1356 } rfprog[] = ZYD_MAXIM_CHANTABLE; 1357 } rfprog[] = ZYD_MAXIM_CHANTABLE;
1357 uint16_t tmp; 1358 uint16_t tmp;
1358 int error; 1359 int error;
1359 size_t i; 1360 size_t i;
1360 1361
1361 /* 1362 /*
1362 * Do the same as we do when initializing it, except for the channel 1363 * Do the same as we do when initializing it, except for the channel
1363 * values coming from the two channel tables. 1364 * values coming from the two channel tables.
1364 */ 1365 */
1365 1366
1366 /* init RF-dependent PHY registers */ 1367 /* init RF-dependent PHY registers */
1367 for (i = 0; i < __arraycount(phyini); i++) { 1368 for (i = 0; i < __arraycount(phyini); i++) {
1368 error = zyd_write16(sc, phyini[i].reg, phyini[i].val); 1369 error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1369 if (error != 0) 1370 if (error != 0)
1370 return error; 1371 return error;
1371 } 1372 }
1372 (void)zyd_read16(sc, ZYD_CR203, &tmp); 1373 (void)zyd_read16(sc, ZYD_CR203, &tmp);
1373 (void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4)); 1374 (void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4));
1374 1375
1375 /* first two values taken from the chantables */ 1376 /* first two values taken from the chantables */
1376 (void)zyd_rfwrite(sc, rfprog[chan - 1].r1); 1377 (void)zyd_rfwrite(sc, rfprog[chan - 1].r1);
1377 (void)zyd_rfwrite(sc, rfprog[chan - 1].r2); 1378 (void)zyd_rfwrite(sc, rfprog[chan - 1].r2);
1378 1379
1379 /* init maxim radio - skipping the two first values */ 1380 /* init maxim radio - skipping the two first values */
1380 for (i = 2; i < __arraycount(rfini); i++) { 1381 for (i = 2; i < __arraycount(rfini); i++) {
1381 if ((error = zyd_rfwrite(sc, rfini[i])) != 0) 1382 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1382 return error; 1383 return error;
1383 } 1384 }
1384 (void)zyd_read16(sc, ZYD_CR203, &tmp); 1385 (void)zyd_read16(sc, ZYD_CR203, &tmp);
1385 (void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4)); 1386 (void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4));
1386 1387
1387 return 0; 1388 return 0;
1388} 1389}
1389 1390
1390/* 1391/*
1391 * Maxim2 RF methods. 1392 * Maxim2 RF methods.
1392 */ 1393 */
1393Static int 1394Static int
1394zyd_maxim2_init(struct zyd_rf *rf) 1395zyd_maxim2_init(struct zyd_rf *rf)
1395{ 1396{
1396 struct zyd_softc *sc = rf->rf_sc; 1397 struct zyd_softc *sc = rf->rf_sc;
1397 static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY; 1398 static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY;
1398 static const uint32_t rfini[] = ZYD_MAXIM2_RF; 1399 static const uint32_t rfini[] = ZYD_MAXIM2_RF;
1399 uint16_t tmp; 1400 uint16_t tmp;
1400 int error; 1401 int error;
1401 size_t i; 1402 size_t i;
1402 1403
1403 /* init RF-dependent PHY registers */ 1404 /* init RF-dependent PHY registers */
1404 for (i = 0; i < __arraycount(phyini); i++) { 1405 for (i = 0; i < __arraycount(phyini); i++) {
1405 error = zyd_write16(sc, phyini[i].reg, phyini[i].val); 1406 error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1406 if (error != 0) 1407 if (error != 0)
1407 return error; 1408 return error;
1408 } 1409 }
1409 (void)zyd_read16(sc, ZYD_CR203, &tmp); 1410 (void)zyd_read16(sc, ZYD_CR203, &tmp);
1410 (void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4)); 1411 (void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4));
1411 1412
1412 /* init maxim2 radio */ 1413 /* init maxim2 radio */
1413 for (i = 0; i < __arraycount(rfini); i++) { 1414 for (i = 0; i < __arraycount(rfini); i++) {
1414 if ((error = zyd_rfwrite(sc, rfini[i])) != 0) 1415 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1415 return error; 1416 return error;
1416 } 1417 }
1417 (void)zyd_read16(sc, ZYD_CR203, &tmp); 1418 (void)zyd_read16(sc, ZYD_CR203, &tmp);
1418 (void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4)); 1419 (void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4));
1419 1420
1420 return 0; 1421 return 0;
1421} 1422}
1422 1423
1423Static int 1424Static int
1424zyd_maxim2_switch_radio(struct zyd_rf *rf, int on) 1425zyd_maxim2_switch_radio(struct zyd_rf *rf, int on)
1425{ 1426{
1426 /* vendor driver does nothing for this RF chip */ 1427 /* vendor driver does nothing for this RF chip */
1427 1428
1428 return 0; 1429 return 0;
1429} 1430}
1430 1431
1431Static int 1432Static int
1432zyd_maxim2_set_channel(struct zyd_rf *rf, uint8_t chan) 1433zyd_maxim2_set_channel(struct zyd_rf *rf, uint8_t chan)
1433{ 1434{
1434 struct zyd_softc *sc = rf->rf_sc; 1435 struct zyd_softc *sc = rf->rf_sc;
1435 static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY; 1436 static const struct zyd_phy_pair phyini[] = ZYD_MAXIM2_PHY;
1436 static const uint32_t rfini[] = ZYD_MAXIM2_RF; 1437 static const uint32_t rfini[] = ZYD_MAXIM2_RF;
1437 static const struct { 1438 static const struct {
1438 uint32_t r1, r2; 1439 uint32_t r1, r2;
1439 } rfprog[] = ZYD_MAXIM2_CHANTABLE; 1440 } rfprog[] = ZYD_MAXIM2_CHANTABLE;
1440 uint16_t tmp; 1441 uint16_t tmp;
1441 int error; 1442 int error;
1442 size_t i; 1443 size_t i;
1443 1444
1444 /* 1445 /*
1445 * Do the same as we do when initializing it, except for the channel 1446 * Do the same as we do when initializing it, except for the channel
1446 * values coming from the two channel tables. 1447 * values coming from the two channel tables.
1447 */ 1448 */
1448 1449
1449 /* init RF-dependent PHY registers */ 1450 /* init RF-dependent PHY registers */
1450 for (i = 0; i < __arraycount(phyini); i++) { 1451 for (i = 0; i < __arraycount(phyini); i++) {
1451 error = zyd_write16(sc, phyini[i].reg, phyini[i].val); 1452 error = zyd_write16(sc, phyini[i].reg, phyini[i].val);
1452 if (error != 0) 1453 if (error != 0)
1453 return error; 1454 return error;
1454 } 1455 }
1455 (void)zyd_read16(sc, ZYD_CR203, &tmp); 1456 (void)zyd_read16(sc, ZYD_CR203, &tmp);
1456 (void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4)); 1457 (void)zyd_write16(sc, ZYD_CR203, tmp & ~(1 << 4));
1457 1458
1458 /* first two values taken from the chantables */ 1459 /* first two values taken from the chantables */
1459 (void)zyd_rfwrite(sc, rfprog[chan - 1].r1); 1460 (void)zyd_rfwrite(sc, rfprog[chan - 1].r1);
1460 (void)zyd_rfwrite(sc, rfprog[chan - 1].r2); 1461 (void)zyd_rfwrite(sc, rfprog[chan - 1].r2);
1461 1462
1462 /* init maxim2 radio - skipping the two first values */ 1463 /* init maxim2 radio - skipping the two first values */
1463 for (i = 2; i < __arraycount(rfini); i++) { 1464 for (i = 2; i < __arraycount(rfini); i++) {
1464 if ((error = zyd_rfwrite(sc, rfini[i])) != 0) 1465 if ((error = zyd_rfwrite(sc, rfini[i])) != 0)
1465 return error; 1466 return error;
1466 } 1467 }
1467 (void)zyd_read16(sc, ZYD_CR203, &tmp); 1468 (void)zyd_read16(sc, ZYD_CR203, &tmp);
1468 (void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4)); 1469 (void)zyd_write16(sc, ZYD_CR203, tmp | (1 << 4));
1469 1470
1470 return 0; 1471 return 0;
1471} 1472}
1472 1473
1473Static int 1474Static int
1474zyd_rf_attach(struct zyd_softc *sc, uint8_t type) 1475zyd_rf_attach(struct zyd_softc *sc, uint8_t type)
1475{ 1476{
1476 struct zyd_rf *rf = &sc->sc_rf; 1477 struct zyd_rf *rf = &sc->sc_rf;
1477 1478
1478 rf->rf_sc = sc; 1479 rf->rf_sc = sc;
1479 1480
1480 switch (type) { 1481 switch (type) {
1481 case ZYD_RF_RFMD: 1482 case ZYD_RF_RFMD:
1482 rf->init = zyd_rfmd_init; 1483 rf->init = zyd_rfmd_init;
1483 rf->switch_radio = zyd_rfmd_switch_radio; 1484 rf->switch_radio = zyd_rfmd_switch_radio;
1484 rf->set_channel = zyd_rfmd_set_channel; 1485 rf->set_channel = zyd_rfmd_set_channel;
1485 rf->width = 24; /* 24-bit RF values */ 1486 rf->width = 24; /* 24-bit RF values */
1486 break; 1487 break;
1487 case ZYD_RF_AL2230: 1488 case ZYD_RF_AL2230:
1488 case ZYD_RF_AL2230S: 1489 case ZYD_RF_AL2230S:
1489 if (sc->mac_rev == ZYD_ZD1211B) 1490 if (sc->mac_rev == ZYD_ZD1211B)
1490 rf->init = zyd_al2230_init_b; 1491 rf->init = zyd_al2230_init_b;
1491 else 1492 else
1492 rf->init = zyd_al2230_init; 1493 rf->init = zyd_al2230_init;
1493 rf->switch_radio = zyd_al2230_switch_radio; 1494 rf->switch_radio = zyd_al2230_switch_radio;
1494 rf->set_channel = zyd_al2230_set_channel; 1495 rf->set_channel = zyd_al2230_set_channel;
1495 rf->width = 24; /* 24-bit RF values */ 1496 rf->width = 24; /* 24-bit RF values */
1496 break; 1497 break;
1497 case ZYD_RF_AL7230B: 1498 case ZYD_RF_AL7230B:
1498 rf->init = zyd_al7230B_init; 1499 rf->init = zyd_al7230B_init;
1499 rf->switch_radio = zyd_al7230B_switch_radio; 1500 rf->switch_radio = zyd_al7230B_switch_radio;
1500 rf->set_channel = zyd_al7230B_set_channel; 1501 rf->set_channel = zyd_al7230B_set_channel;
1501 rf->width = 24; /* 24-bit RF values */ 1502 rf->width = 24; /* 24-bit RF values */
1502 break; 1503 break;
1503 case ZYD_RF_AL2210: 1504 case ZYD_RF_AL2210:
1504 rf->init = zyd_al2210_init; 1505 rf->init = zyd_al2210_init;
1505 rf->switch_radio = zyd_al2210_switch_radio; 1506 rf->switch_radio = zyd_al2210_switch_radio;
1506 rf->set_channel = zyd_al2210_set_channel; 1507 rf->set_channel = zyd_al2210_set_channel;
1507 rf->width = 24; /* 24-bit RF values */ 1508 rf->width = 24; /* 24-bit RF values */
1508 break; 1509 break;
1509 case ZYD_RF_GCT: 1510 case ZYD_RF_GCT:
1510 rf->init = zyd_gct_init; 1511 rf->init = zyd_gct_init;
1511 rf->switch_radio = zyd_gct_switch_radio; 1512 rf->switch_radio = zyd_gct_switch_radio;
1512 rf->set_channel = zyd_gct_set_channel; 1513 rf->set_channel = zyd_gct_set_channel;
1513 rf->width = 21; /* 21-bit RF values */ 1514 rf->width = 21; /* 21-bit RF values */
1514 break; 1515 break;
1515 case ZYD_RF_MAXIM_NEW: 1516 case ZYD_RF_MAXIM_NEW:
1516 rf->init = zyd_maxim_init; 1517 rf->init = zyd_maxim_init;
1517 rf->switch_radio = zyd_maxim_switch_radio; 1518 rf->switch_radio = zyd_maxim_switch_radio;
1518 rf->set_channel = zyd_maxim_set_channel; 1519 rf->set_channel = zyd_maxim_set_channel;
1519 rf->width = 18; /* 18-bit RF values */ 1520 rf->width = 18; /* 18-bit RF values */
1520 break; 1521 break;
1521 case ZYD_RF_MAXIM_NEW2: 1522 case ZYD_RF_MAXIM_NEW2:
1522 rf->init = zyd_maxim2_init; 1523 rf->init = zyd_maxim2_init;
1523 rf->switch_radio = zyd_maxim2_switch_radio; 1524 rf->switch_radio = zyd_maxim2_switch_radio;
1524 rf->set_channel = zyd_maxim2_set_channel; 1525 rf->set_channel = zyd_maxim2_set_channel;
1525 rf->width = 18; /* 18-bit RF values */ 1526 rf->width = 18; /* 18-bit RF values */
1526 break; 1527 break;
1527 default: 1528 default:
1528 printf("%s: sorry, radio \"%s\" is not supported yet\n", 1529 printf("%s: sorry, radio \"%s\" is not supported yet\n",
1529 device_xname(sc->sc_dev), zyd_rf_name(type)); 1530 device_xname(sc->sc_dev), zyd_rf_name(type));
1530 return EINVAL; 1531 return EINVAL;
1531 } 1532 }
1532 return 0; 1533 return 0;
1533} 1534}
1534 1535
1535Static const char * 1536Static const char *
1536zyd_rf_name(uint8_t type) 1537zyd_rf_name(uint8_t type)
1537{ 1538{
1538 static const char * const zyd_rfs[] = { 1539 static const char * const zyd_rfs[] = {
1539 "unknown", "unknown", "UW2451", "UCHIP", "AL2230", 1540 "unknown", "unknown", "UW2451", "UCHIP", "AL2230",
1540 "AL7230B", "THETA", "AL2210", "MAXIM_NEW", "GCT", 1541 "AL7230B", "THETA", "AL2210", "MAXIM_NEW", "GCT",
1541 "AL2230S", "RALINK", "INTERSIL", "RFMD", "MAXIM_NEW2", 1542 "AL2230S", "RALINK", "INTERSIL", "RFMD", "MAXIM_NEW2",
1542 "PHILIPS" 1543 "PHILIPS"
1543 }; 1544 };
1544 1545
1545 return zyd_rfs[(type > 15) ? 0 : type]; 1546 return zyd_rfs[(type > 15) ? 0 : type];
1546} 1547}
1547 1548
1548Static int 1549Static int
1549zyd_hw_init(struct zyd_softc *sc) 1550zyd_hw_init(struct zyd_softc *sc)
1550{ 1551{
1551 struct zyd_rf *rf = &sc->sc_rf; 1552 struct zyd_rf *rf = &sc->sc_rf;
1552 const struct zyd_phy_pair *phyp; 1553 const struct zyd_phy_pair *phyp;
1553 int error; 1554 int error;
1554 1555
1555 /* specify that the plug and play is finished */ 1556 /* specify that the plug and play is finished */
1556 (void)zyd_write32(sc, ZYD_MAC_AFTER_PNP, 1); 1557 (void)zyd_write32(sc, ZYD_MAC_AFTER_PNP, 1);
1557 1558
1558 (void)zyd_read16(sc, ZYD_FIRMWARE_BASE_ADDR, &sc->fwbase); 1559 (void)zyd_read16(sc, ZYD_FIRMWARE_BASE_ADDR, &sc->fwbase);
1559 DPRINTF(("firmware base address=0x%04x\n", sc->fwbase)); 1560 DPRINTF(("firmware base address=0x%04x\n", sc->fwbase));
1560 1561
1561 /* retrieve firmware revision number */ 1562 /* retrieve firmware revision number */
1562 (void)zyd_read16(sc, sc->fwbase + ZYD_FW_FIRMWARE_REV, &sc->fw_rev); 1563 (void)zyd_read16(sc, sc->fwbase + ZYD_FW_FIRMWARE_REV, &sc->fw_rev);
1563 1564
1564 (void)zyd_write32(sc, ZYD_CR_GPI_EN, 0); 1565 (void)zyd_write32(sc, ZYD_CR_GPI_EN, 0);
1565 (void)zyd_write32(sc, ZYD_MAC_CONT_WIN_LIMIT, 0x7f043f); 1566 (void)zyd_write32(sc, ZYD_MAC_CONT_WIN_LIMIT, 0x7f043f);
1566 1567
1567 /* disable interrupts */ 1568 /* disable interrupts */
1568 (void)zyd_write32(sc, ZYD_CR_INTERRUPT, 0); 1569 (void)zyd_write32(sc, ZYD_CR_INTERRUPT, 0);
1569 1570
1570 /* PHY init */ 1571 /* PHY init */
1571 zyd_lock_phy(sc); 1572 zyd_lock_phy(sc);
1572 phyp = (sc->mac_rev == ZYD_ZD1211B) ? zyd_def_phyB : zyd_def_phy; 1573 phyp = (sc->mac_rev == ZYD_ZD1211B) ? zyd_def_phyB : zyd_def_phy;
1573 for (; phyp->reg != 0; phyp++) { 1574 for (; phyp->reg != 0; phyp++) {
1574 if ((error = zyd_write16(sc, phyp->reg, phyp->val)) != 0) 1575 if ((error = zyd_write16(sc, phyp->reg, phyp->val)) != 0)
1575 goto fail; 1576 goto fail;
1576 } 1577 }
1577 zyd_unlock_phy(sc); 1578 zyd_unlock_phy(sc);
1578 1579
1579 /* HMAC init */ 1580 /* HMAC init */
1580 zyd_write32(sc, ZYD_MAC_ACK_EXT, 0x00000020); 1581 zyd_write32(sc, ZYD_MAC_ACK_EXT, 0x00000020);
1581 zyd_write32(sc, ZYD_CR_ADDA_MBIAS_WT, 0x30000808); 1582 zyd_write32(sc, ZYD_CR_ADDA_MBIAS_WT, 0x30000808);
1582 1583
1583 if (sc->mac_rev == ZYD_ZD1211) { 1584 if (sc->mac_rev == ZYD_ZD1211) {
1584 zyd_write32(sc, ZYD_MAC_RETRY, 0x00000002); 1585 zyd_write32(sc, ZYD_MAC_RETRY, 0x00000002);
1585 } else { 1586 } else {
1586 zyd_write32(sc, ZYD_MAC_RETRY, 0x02020202); 1587 zyd_write32(sc, ZYD_MAC_RETRY, 0x02020202);
1587 zyd_write32(sc, ZYD_MACB_TXPWR_CTL4, 0x007f003f); 1588 zyd_write32(sc, ZYD_MACB_TXPWR_CTL4, 0x007f003f);
1588 zyd_write32(sc, ZYD_MACB_TXPWR_CTL3, 0x007f003f); 1589 zyd_write32(sc, ZYD_MACB_TXPWR_CTL3, 0x007f003f);
1589 zyd_write32(sc, ZYD_MACB_TXPWR_CTL2, 0x003f001f); 1590 zyd_write32(sc, ZYD_MACB_TXPWR_CTL2, 0x003f001f);
1590 zyd_write32(sc, ZYD_MACB_TXPWR_CTL1, 0x001f000f); 1591 zyd_write32(sc, ZYD_MACB_TXPWR_CTL1, 0x001f000f);
1591 zyd_write32(sc, ZYD_MACB_AIFS_CTL1, 0x00280028); 1592 zyd_write32(sc, ZYD_MACB_AIFS_CTL1, 0x00280028);
1592 zyd_write32(sc, ZYD_MACB_AIFS_CTL2, 0x008C003C); 1593 zyd_write32(sc, ZYD_MACB_AIFS_CTL2, 0x008C003C);
1593 zyd_write32(sc, ZYD_MACB_TXOP, 0x01800824); 1594 zyd_write32(sc, ZYD_MACB_TXOP, 0x01800824);
1594 } 1595 }
1595 1596
1596 zyd_write32(sc, ZYD_MAC_SNIFFER, 0x00000000); 1597 zyd_write32(sc, ZYD_MAC_SNIFFER, 0x00000000);
1597 zyd_write32(sc, ZYD_MAC_RXFILTER, 0x00000000); 1598 zyd_write32(sc, ZYD_MAC_RXFILTER, 0x00000000);
1598 zyd_write32(sc, ZYD_MAC_GHTBL, 0x00000000); 1599 zyd_write32(sc, ZYD_MAC_GHTBL, 0x00000000);
1599 zyd_write32(sc, ZYD_MAC_GHTBH, 0x80000000); 1600 zyd_write32(sc, ZYD_MAC_GHTBH, 0x80000000);
1600 zyd_write32(sc, ZYD_MAC_MISC, 0x000000a4); 1601 zyd_write32(sc, ZYD_MAC_MISC, 0x000000a4);
1601 zyd_write32(sc, ZYD_CR_ADDA_PWR_DWN, 0x0000007f); 1602 zyd_write32(sc, ZYD_CR_ADDA_PWR_DWN, 0x0000007f);
1602 zyd_write32(sc, ZYD_MAC_BCNCFG, 0x00f00401); 1603 zyd_write32(sc, ZYD_MAC_BCNCFG, 0x00f00401);
1603 zyd_write32(sc, ZYD_MAC_PHY_DELAY2, 0x00000000); 1604 zyd_write32(sc, ZYD_MAC_PHY_DELAY2, 0x00000000);
1604 zyd_write32(sc, ZYD_MAC_ACK_EXT, 0x00000080); 1605 zyd_write32(sc, ZYD_MAC_ACK_EXT, 0x00000080);
1605 zyd_write32(sc, ZYD_CR_ADDA_PWR_DWN, 0x00000000); 1606 zyd_write32(sc, ZYD_CR_ADDA_PWR_DWN, 0x00000000);
1606 zyd_write32(sc, ZYD_MAC_SIFS_ACK_TIME, 0x00000100); 1607 zyd_write32(sc, ZYD_MAC_SIFS_ACK_TIME, 0x00000100);
1607 zyd_write32(sc, ZYD_MAC_DIFS_EIFS_SIFS, 0x0547c032); 1608 zyd_write32(sc, ZYD_MAC_DIFS_EIFS_SIFS, 0x0547c032);
1608 zyd_write32(sc, ZYD_CR_RX_PE_DELAY, 0x00000070); 1609 zyd_write32(sc, ZYD_CR_RX_PE_DELAY, 0x00000070);
1609 zyd_write32(sc, ZYD_CR_PS_CTRL, 0x10000000); 1610 zyd_write32(sc, ZYD_CR_PS_CTRL, 0x10000000);
1610 zyd_write32(sc, ZYD_MAC_RTSCTSRATE, 0x02030203); 1611 zyd_write32(sc, ZYD_MAC_RTSCTSRATE, 0x02030203);
1611 zyd_write32(sc, ZYD_MAC_RX_THRESHOLD, 0x000c0640); 1612 zyd_write32(sc, ZYD_MAC_RX_THRESHOLD, 0x000c0640);
1612 zyd_write32(sc, ZYD_MAC_BACKOFF_PROTECT, 0x00000114); 1613 zyd_write32(sc, ZYD_MAC_BACKOFF_PROTECT, 0x00000114);
1613 1614
1614 /* RF chip init */ 1615 /* RF chip init */
1615 zyd_lock_phy(sc); 1616 zyd_lock_phy(sc);
1616 error = (*rf->init)(rf); 1617 error = (*rf->init)(rf);
1617 zyd_unlock_phy(sc); 1618 zyd_unlock_phy(sc);
1618 if (error != 0) { 1619 if (error != 0) {
1619 printf("%s: radio initialization failed\n", 1620 printf("%s: radio initialization failed\n",
1620 device_xname(sc->sc_dev)); 1621 device_xname(sc->sc_dev));
1621 goto fail; 1622 goto fail;
1622 } 1623 }
1623 1624
1624 /* init beacon interval to 100ms */ 1625 /* init beacon interval to 100ms */
1625 if ((error = zyd_set_beacon_interval(sc, 100)) != 0) 1626 if ((error = zyd_set_beacon_interval(sc, 100)) != 0)
1626 goto fail; 1627 goto fail;
1627 1628
1628fail: return error; 1629fail: return error;
1629} 1630}
1630 1631
1631Static int 1632Static int
1632zyd_read_eeprom(struct zyd_softc *sc) 1633zyd_read_eeprom(struct zyd_softc *sc)
1633{ 1634{
1634 struct ieee80211com *ic = &sc->sc_ic; 1635 struct ieee80211com *ic = &sc->sc_ic;
1635 uint32_t tmp; 1636 uint32_t tmp;
1636 uint16_t val; 1637 uint16_t val;
1637 int i; 1638 int i;
1638 1639
1639 /* read MAC address */ 1640 /* read MAC address */
1640 (void)zyd_read32(sc, ZYD_EEPROM_MAC_ADDR_P1, &tmp); 1641 (void)zyd_read32(sc, ZYD_EEPROM_MAC_ADDR_P1, &tmp);
1641 ic->ic_myaddr[0] = tmp & 0xff; 1642 ic->ic_myaddr[0] = tmp & 0xff;
1642 ic->ic_myaddr[1] = tmp >> 8; 1643 ic->ic_myaddr[1] = tmp >> 8;
1643 ic->ic_myaddr[2] = tmp >> 16; 1644 ic->ic_myaddr[2] = tmp >> 16;
1644 ic->ic_myaddr[3] = tmp >> 24; 1645 ic->ic_myaddr[3] = tmp >> 24;
1645 (void)zyd_read32(sc, ZYD_EEPROM_MAC_ADDR_P2, &tmp); 1646 (void)zyd_read32(sc, ZYD_EEPROM_MAC_ADDR_P2, &tmp);
1646 ic->ic_myaddr[4] = tmp & 0xff; 1647 ic->ic_myaddr[4] = tmp & 0xff;
1647 ic->ic_myaddr[5] = tmp >> 8; 1648 ic->ic_myaddr[5] = tmp >> 8;
1648 1649
1649 (void)zyd_read32(sc, ZYD_EEPROM_POD, &tmp); 1650 (void)zyd_read32(sc, ZYD_EEPROM_POD, &tmp);
1650 sc->rf_rev = tmp & 0x0f; 1651 sc->rf_rev = tmp & 0x0f;
1651 sc->pa_rev = (tmp >> 16) & 0x0f; 1652 sc->pa_rev = (tmp >> 16) & 0x0f;
1652 1653
1653 /* read regulatory domain (currently unused) */ 1654 /* read regulatory domain (currently unused) */
1654 (void)zyd_read32(sc, ZYD_EEPROM_SUBID, &tmp); 1655 (void)zyd_read32(sc, ZYD_EEPROM_SUBID, &tmp);
1655 sc->regdomain = tmp >> 16; 1656 sc->regdomain = tmp >> 16;
1656 DPRINTF(("regulatory domain %x\n", sc->regdomain)); 1657 DPRINTF(("regulatory domain %x\n", sc->regdomain));
1657 1658
1658 /* read Tx power calibration tables */ 1659 /* read Tx power calibration tables */
1659 for (i = 0; i < 7; i++) { 1660 for (i = 0; i < 7; i++) {
1660 (void)zyd_read16(sc, ZYD_EEPROM_PWR_CAL + i, &val); 1661 (void)zyd_read16(sc, ZYD_EEPROM_PWR_CAL + i, &val);
1661 sc->pwr_cal[i * 2] = val >> 8; 1662 sc->pwr_cal[i * 2] = val >> 8;
1662 sc->pwr_cal[i * 2 + 1] = val & 0xff; 1663 sc->pwr_cal[i * 2 + 1] = val & 0xff;
1663 1664
1664 (void)zyd_read16(sc, ZYD_EEPROM_PWR_INT + i, &val); 1665 (void)zyd_read16(sc, ZYD_EEPROM_PWR_INT + i, &val);
1665 sc->pwr_int[i * 2] = val >> 8; 1666 sc->pwr_int[i * 2] = val >> 8;
1666 sc->pwr_int[i * 2 + 1] = val & 0xff; 1667 sc->pwr_int[i * 2 + 1] = val & 0xff;
1667 1668
1668 (void)zyd_read16(sc, ZYD_EEPROM_36M_CAL + i, &val); 1669 (void)zyd_read16(sc, ZYD_EEPROM_36M_CAL + i, &val);
1669 sc->ofdm36_cal[i * 2] = val >> 8; 1670 sc->ofdm36_cal[i * 2] = val >> 8;
1670 sc->ofdm36_cal[i * 2 + 1] = val & 0xff; 1671 sc->ofdm36_cal[i * 2 + 1] = val & 0xff;
1671 1672
1672 (void)zyd_read16(sc, ZYD_EEPROM_48M_CAL + i, &val); 1673 (void)zyd_read16(sc, ZYD_EEPROM_48M_CAL + i, &val);
1673 sc->ofdm48_cal[i * 2] = val >> 8; 1674 sc->ofdm48_cal[i * 2] = val >> 8;
1674 sc->ofdm48_cal[i * 2 + 1] = val & 0xff; 1675 sc->ofdm48_cal[i * 2 + 1] = val & 0xff;
1675 1676
1676 (void)zyd_read16(sc, ZYD_EEPROM_54M_CAL + i, &val); 1677 (void)zyd_read16(sc, ZYD_EEPROM_54M_CAL + i, &val);
1677 sc->ofdm54_cal[i * 2] = val >> 8; 1678 sc->ofdm54_cal[i * 2] = val >> 8;
1678 sc->ofdm54_cal[i * 2 + 1] = val & 0xff; 1679 sc->ofdm54_cal[i * 2 + 1] = val & 0xff;
1679 } 1680 }
1680 return 0; 1681 return 0;
1681} 1682}
1682 1683
1683Static int 1684Static int
1684zyd_set_macaddr(struct zyd_softc *sc, const uint8_t *addr) 1685zyd_set_macaddr(struct zyd_softc *sc, const uint8_t *addr)
1685{ 1686{
1686 uint32_t tmp; 1687 uint32_t tmp;
1687 1688
1688 tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0]; 1689 tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
1689 (void)zyd_write32(sc, ZYD_MAC_MACADRL, tmp); 1690 (void)zyd_write32(sc, ZYD_MAC_MACADRL, tmp);
1690 1691
1691 tmp = addr[5] << 8 | addr[4]; 1692 tmp = addr[5] << 8 | addr[4];
1692 (void)zyd_write32(sc, ZYD_MAC_MACADRH, tmp); 1693 (void)zyd_write32(sc, ZYD_MAC_MACADRH, tmp);
1693 1694
1694 return 0; 1695 return 0;
1695} 1696}
1696 1697
1697Static int 1698Static int
1698zyd_set_bssid(struct zyd_softc *sc, const uint8_t *addr) 1699zyd_set_bssid(struct zyd_softc *sc, const uint8_t *addr)
1699{ 1700{
1700 uint32_t tmp; 1701 uint32_t tmp;
1701 1702
1702 tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0]; 1703 tmp = addr[3] << 24 | addr[2] << 16 | addr[1] << 8 | addr[0];
1703 (void)zyd_write32(sc, ZYD_MAC_BSSADRL, tmp); 1704 (void)zyd_write32(sc, ZYD_MAC_BSSADRL, tmp);
1704 1705
1705 tmp = addr[5] << 8 | addr[4]; 1706 tmp = addr[5] << 8 | addr[4];
1706 (void)zyd_write32(sc, ZYD_MAC_BSSADRH, tmp); 1707 (void)zyd_write32(sc, ZYD_MAC_BSSADRH, tmp);
1707 1708
1708 return 0; 1709 return 0;
1709} 1710}
1710 1711
1711Static int 1712Static int
1712zyd_switch_radio(struct zyd_softc *sc, int on) 1713zyd_switch_radio(struct zyd_softc *sc, int on)
1713{ 1714{
1714 struct zyd_rf *rf = &sc->sc_rf; 1715 struct zyd_rf *rf = &sc->sc_rf;
1715 int error; 1716 int error;
1716 1717
1717 zyd_lock_phy(sc); 1718 zyd_lock_phy(sc);
1718 error = (*rf->switch_radio)(rf, on); 1719 error = (*rf->switch_radio)(rf, on);
1719 zyd_unlock_phy(sc); 1720 zyd_unlock_phy(sc);
1720 1721
1721 return error; 1722 return error;
1722} 1723}
1723 1724
1724Static void 1725Static void
1725zyd_set_led(struct zyd_softc *sc, int which, int on) 1726zyd_set_led(struct zyd_softc *sc, int which, int on)
1726{ 1727{
1727 uint32_t tmp; 1728 uint32_t tmp;
1728 1729
1729 (void)zyd_read32(sc, ZYD_MAC_TX_PE_CONTROL, &tmp); 1730 (void)zyd_read32(sc, ZYD_MAC_TX_PE_CONTROL, &tmp);
1730 tmp &= ~which; 1731 tmp &= ~which;
1731 if (on) 1732 if (on)
1732 tmp |= which; 1733 tmp |= which;
1733 (void)zyd_write32(sc, ZYD_MAC_TX_PE_CONTROL, tmp); 1734 (void)zyd_write32(sc, ZYD_MAC_TX_PE_CONTROL, tmp);
1734} 1735}
1735 1736
1736Static int 1737Static int
1737zyd_set_rxfilter(struct zyd_softc *sc) 1738zyd_set_rxfilter(struct zyd_softc *sc)
1738{ 1739{
1739 uint32_t rxfilter; 1740 uint32_t rxfilter;
1740 1741
1741 switch (sc->sc_ic.ic_opmode) { 1742 switch (sc->sc_ic.ic_opmode) {
1742 case IEEE80211_M_STA: 1743 case IEEE80211_M_STA:
1743 rxfilter = ZYD_FILTER_BSS; 1744 rxfilter = ZYD_FILTER_BSS;
1744 break; 1745 break;
1745 case IEEE80211_M_IBSS: 1746 case IEEE80211_M_IBSS:
1746 case IEEE80211_M_HOSTAP: 1747 case IEEE80211_M_HOSTAP:
1747 rxfilter = ZYD_FILTER_HOSTAP; 1748 rxfilter = ZYD_FILTER_HOSTAP;
1748 break; 1749 break;
1749 case IEEE80211_M_MONITOR: 1750 case IEEE80211_M_MONITOR:
1750 rxfilter = ZYD_FILTER_MONITOR; 1751 rxfilter = ZYD_FILTER_MONITOR;
1751 break; 1752 break;
1752 default: 1753 default:
1753 /* should not get there */ 1754 /* should not get there */
1754 return EINVAL; 1755 return EINVAL;
1755 } 1756 }
1756 return zyd_write32(sc, ZYD_MAC_RXFILTER, rxfilter); 1757 return zyd_write32(sc, ZYD_MAC_RXFILTER, rxfilter);
1757} 1758}
1758 1759
1759Static void 1760Static void
1760zyd_set_chan(struct zyd_softc *sc, struct ieee80211_channel *c) 1761zyd_set_chan(struct zyd_softc *sc, struct ieee80211_channel *c)
1761{ 1762{
1762 struct ieee80211com *ic = &sc->sc_ic; 1763 struct ieee80211com *ic = &sc->sc_ic;
1763 struct zyd_rf *rf = &sc->sc_rf; 1764 struct zyd_rf *rf = &sc->sc_rf;
1764 u_int chan; 1765 u_int chan;
1765 1766
1766 chan = ieee80211_chan2ieee(ic, c); 1767 chan = ieee80211_chan2ieee(ic, c);
1767 if (chan == 0 || chan == IEEE80211_CHAN_ANY) 1768 if (chan == 0 || chan == IEEE80211_CHAN_ANY)
1768 return; 1769 return;
1769 1770
1770 zyd_lock_phy(sc); 1771 zyd_lock_phy(sc);
1771 1772
1772 (*rf->set_channel)(rf, chan); 1773 (*rf->set_channel)(rf, chan);
1773 1774
1774 /* update Tx power */ 1775 /* update Tx power */
1775 (void)zyd_write32(sc, ZYD_CR31, sc->pwr_int[chan - 1]); 1776 (void)zyd_write32(sc, ZYD_CR31, sc->pwr_int[chan - 1]);
1776 (void)zyd_write32(sc, ZYD_CR68, sc->pwr_cal[chan - 1]); 1777 (void)zyd_write32(sc, ZYD_CR68, sc->pwr_cal[chan - 1]);
1777 1778
1778 if (sc->mac_rev == ZYD_ZD1211B) { 1779 if (sc->mac_rev == ZYD_ZD1211B) {
1779 (void)zyd_write32(sc, ZYD_CR67, sc->ofdm36_cal[chan - 1]); 1780 (void)zyd_write32(sc, ZYD_CR67, sc->ofdm36_cal[chan - 1]);
1780 (void)zyd_write32(sc, ZYD_CR66, sc->ofdm48_cal[chan - 1]); 1781 (void)zyd_write32(sc, ZYD_CR66, sc->ofdm48_cal[chan - 1]);
1781 (void)zyd_write32(sc, ZYD_CR65, sc->ofdm54_cal[chan - 1]); 1782 (void)zyd_write32(sc, ZYD_CR65, sc->ofdm54_cal[chan - 1]);
1782 1783
1783 (void)zyd_write32(sc, ZYD_CR69, 0x28); 1784 (void)zyd_write32(sc, ZYD_CR69, 0x28);
1784 (void)zyd_write32(sc, ZYD_CR69, 0x2a); 1785 (void)zyd_write32(sc, ZYD_CR69, 0x2a);
1785 } 1786 }
1786 1787
1787 zyd_unlock_phy(sc); 1788 zyd_unlock_phy(sc);
1788} 1789}
1789 1790
1790Static int 1791Static int
1791zyd_set_beacon_interval(struct zyd_softc *sc, int bintval) 1792zyd_set_beacon_interval(struct zyd_softc *sc, int bintval)
1792{ 1793{
1793 /* XXX this is probably broken.. */ 1794 /* XXX this is probably broken.. */
1794 (void)zyd_write32(sc, ZYD_CR_ATIM_WND_PERIOD, bintval - 2); 1795 (void)zyd_write32(sc, ZYD_CR_ATIM_WND_PERIOD, bintval - 2);
1795 (void)zyd_write32(sc, ZYD_CR_PRE_TBTT, bintval - 1); 1796 (void)zyd_write32(sc, ZYD_CR_PRE_TBTT, bintval - 1);
1796 (void)zyd_write32(sc, ZYD_CR_BCN_INTERVAL, bintval); 1797 (void)zyd_write32(sc, ZYD_CR_BCN_INTERVAL, bintval);
1797 1798
1798 return 0; 1799 return 0;
1799} 1800}
1800 1801
1801Static uint8_t 1802Static uint8_t
1802zyd_plcp_signal(int rate) 1803zyd_plcp_signal(int rate)
1803{ 1804{
1804 switch (rate) { 1805 switch (rate) {
1805 /* CCK rates (returned values are device-dependent) */ 1806 /* CCK rates (returned values are device-dependent) */
1806 case 2: return 0x0; 1807 case 2: return 0x0;
1807 case 4: return 0x1; 1808 case 4: return 0x1;
1808 case 11: return 0x2; 1809 case 11: return 0x2;
1809 case 22: return 0x3; 1810 case 22: return 0x3;
1810 1811
1811 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */ 1812 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1812 case 12: return 0xb; 1813 case 12: return 0xb;
1813 case 18: return 0xf; 1814 case 18: return 0xf;
1814 case 24: return 0xa; 1815 case 24: return 0xa;
1815 case 36: return 0xe; 1816 case 36: return 0xe;
1816 case 48: return 0x9; 1817 case 48: return 0x9;
1817 case 72: return 0xd; 1818 case 72: return 0xd;
1818 case 96: return 0x8; 1819 case 96: return 0x8;
1819 case 108: return 0xc; 1820 case 108: return 0xc;
1820 1821
1821 /* unsupported rates (should not get there) */ 1822 /* unsupported rates (should not get there) */
1822 default: return 0xff; 1823 default: return 0xff;
1823 } 1824 }
1824} 1825}
1825 1826
1826Static void 1827Static void
1827zyd_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status) 1828zyd_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
1828{ 1829{
1829 struct zyd_softc *sc = (struct zyd_softc *)priv; 1830 struct zyd_softc *sc = (struct zyd_softc *)priv;
1830 struct zyd_cmd *cmd; 1831 struct zyd_cmd *cmd;
1831 uint32_t datalen; 1832 uint32_t datalen;