if_athn_usb.c revision 1.3 1 1.3 christos /* $NetBSD: if_athn_usb.c,v 1.3 2013/03/30 14:14:31 christos Exp $ */
2 1.1 christos /* $OpenBSD: if_athn_usb.c,v 1.12 2013/01/14 09:50:31 jsing Exp $ */
3 1.1 christos
4 1.1 christos /*-
5 1.1 christos * Copyright (c) 2011 Damien Bergamini <damien.bergamini (at) free.fr>
6 1.1 christos *
7 1.1 christos * Permission to use, copy, modify, and distribute this software for any
8 1.1 christos * purpose with or without fee is hereby granted, provided that the above
9 1.1 christos * copyright notice and this permission notice appear in all copies.
10 1.1 christos *
11 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 1.1 christos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 1.1 christos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 1.1 christos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.1 christos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 1.1 christos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 1.1 christos * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1.1 christos */
19 1.1 christos
20 1.1 christos /*
21 1.1 christos * USB front-end for Atheros AR9271 and AR7010 chipsets.
22 1.1 christos */
23 1.1 christos
24 1.1 christos #include <sys/cdefs.h>
25 1.3 christos __KERNEL_RCSID(0, "$NetBSD: if_athn_usb.c,v 1.3 2013/03/30 14:14:31 christos Exp $");
26 1.1 christos
27 1.1 christos #ifdef _KERNEL_OPT
28 1.1 christos #include "opt_inet.h"
29 1.1 christos #endif
30 1.1 christos
31 1.1 christos #include <sys/param.h>
32 1.1 christos #include <sys/callout.h>
33 1.1 christos #include <sys/conf.h>
34 1.1 christos #include <sys/device.h>
35 1.1 christos #include <sys/kernel.h>
36 1.1 christos #include <sys/mbuf.h>
37 1.1 christos #include <sys/module.h>
38 1.1 christos #include <sys/proc.h>
39 1.1 christos #include <sys/socket.h>
40 1.1 christos #include <sys/sockio.h>
41 1.1 christos #include <sys/systm.h>
42 1.1 christos
43 1.1 christos #include <sys/bus.h>
44 1.1 christos #include <sys/endian.h>
45 1.1 christos #include <sys/intr.h>
46 1.1 christos
47 1.1 christos #include <net/bpf.h>
48 1.1 christos #include <net/if.h>
49 1.1 christos #include <net/if_arp.h>
50 1.1 christos #include <net/if_dl.h>
51 1.1 christos #include <net/if_ether.h>
52 1.1 christos #include <net/if_media.h>
53 1.1 christos #include <net/if_types.h>
54 1.1 christos
55 1.1 christos #include <netinet/if_inarp.h>
56 1.1 christos #include <netinet/in.h>
57 1.1 christos #include <netinet/in_systm.h>
58 1.1 christos #include <netinet/in_var.h>
59 1.1 christos #include <netinet/ip.h>
60 1.1 christos
61 1.1 christos #include <net80211/ieee80211_var.h>
62 1.1 christos #include <net80211/ieee80211_amrr.h>
63 1.1 christos #include <net80211/ieee80211_radiotap.h>
64 1.1 christos
65 1.1 christos #include <dev/firmload.h>
66 1.1 christos
67 1.1 christos #include <dev/usb/usb.h>
68 1.1 christos #include <dev/usb/usbdevs.h>
69 1.1 christos #include <dev/usb/usbdi.h>
70 1.1 christos #include <dev/usb/usbdi_util.h>
71 1.1 christos
72 1.1 christos #include <dev/ic/athnreg.h>
73 1.1 christos #include <dev/ic/athnvar.h>
74 1.1 christos #include <dev/ic/arn9285.h>
75 1.1 christos #include <dev/usb/if_athn_usb.h>
76 1.1 christos
77 1.1 christos #define ATHN_USB_SOFTC(sc) ((struct athn_usb_softc *)(sc))
78 1.1 christos #define ATHN_USB_NODE(ni) ((struct athn_usb_node *)(ni))
79 1.1 christos
80 1.1 christos #define IS_UP_AND_RUNNING(ifp) \
81 1.1 christos (((ifp)->if_flags & IFF_UP) && ((ifp)->if_flags & IFF_RUNNING))
82 1.1 christos
83 1.1 christos #define athn_usb_wmi_cmd(sc, cmd_id) \
84 1.1 christos athn_usb_wmi_xcmd(sc, cmd_id, NULL, 0, NULL)
85 1.1 christos
86 1.1 christos Static int athn_usb_activate(device_t, enum devact);
87 1.1 christos Static int athn_usb_detach(device_t, int);
88 1.1 christos Static int athn_usb_match(device_t, cfdata_t, void *);
89 1.1 christos Static void athn_usb_attach(device_t, device_t, void *);
90 1.1 christos
91 1.1 christos CFATTACH_DECL_NEW(athn_usb, sizeof(struct athn_usb_softc), athn_usb_match,
92 1.1 christos athn_usb_attach, athn_usb_detach, athn_usb_activate);
93 1.1 christos
94 1.1 christos Static int athn_usb_alloc_rx_list(struct athn_usb_softc *);
95 1.1 christos Static int athn_usb_alloc_tx_cmd(struct athn_usb_softc *);
96 1.1 christos Static int athn_usb_alloc_tx_list(struct athn_usb_softc *);
97 1.1 christos Static void athn_usb_attachhook(device_t);
98 1.1 christos Static void athn_usb_bcneof(usbd_xfer_handle, usbd_private_handle,
99 1.1 christos usbd_status);
100 1.1 christos Static void athn_usb_close_pipes(struct athn_usb_softc *);
101 1.1 christos Static int athn_usb_create_hw_node(struct athn_usb_softc *,
102 1.1 christos struct ar_htc_target_sta *);
103 1.1 christos Static int athn_usb_create_node(struct athn_usb_softc *,
104 1.1 christos struct ieee80211_node *);
105 1.1 christos Static void athn_usb_do_async(struct athn_usb_softc *,
106 1.1 christos void (*)(struct athn_usb_softc *, void *), void *, int);
107 1.1 christos Static void athn_usb_free_rx_list(struct athn_usb_softc *);
108 1.1 christos Static void athn_usb_free_tx_cmd(struct athn_usb_softc *);
109 1.1 christos Static void athn_usb_free_tx_list(struct athn_usb_softc *);
110 1.1 christos Static int athn_usb_htc_connect_svc(struct athn_usb_softc *, uint16_t,
111 1.1 christos uint8_t, uint8_t, uint8_t *);
112 1.1 christos Static int athn_usb_htc_msg(struct athn_usb_softc *, uint16_t, void *,
113 1.1 christos int);
114 1.1 christos Static int athn_usb_htc_setup(struct athn_usb_softc *);
115 1.1 christos Static int athn_usb_init(struct ifnet *);
116 1.1 christos Static void athn_usb_intr(usbd_xfer_handle, usbd_private_handle,
117 1.1 christos usbd_status);
118 1.1 christos Static int athn_usb_ioctl(struct ifnet *, u_long, void *);
119 1.1 christos Static int athn_usb_load_firmware(struct athn_usb_softc *);
120 1.1 christos Static const struct athn_usb_type *
121 1.1 christos athn_usb_lookup(int, int);
122 1.1 christos Static int athn_usb_media_change(struct ifnet *);
123 1.1 christos Static void athn_usb_newassoc(struct ieee80211_node *, int);
124 1.1 christos Static void athn_usb_newassoc_cb(struct athn_usb_softc *, void *);
125 1.1 christos Static int athn_usb_newstate(struct ieee80211com *, enum ieee80211_state,
126 1.1 christos int);
127 1.1 christos Static void athn_usb_newstate_cb(struct athn_usb_softc *, void *);
128 1.1 christos Static void athn_usb_node_cleanup(struct ieee80211_node *);
129 1.1 christos Static void athn_usb_node_cleanup_cb(struct athn_usb_softc *, void *);
130 1.1 christos Static int athn_usb_open_pipes(struct athn_usb_softc *);
131 1.1 christos Static uint32_t athn_usb_read(struct athn_softc *, uint32_t);
132 1.1 christos Static int athn_usb_remove_hw_node(struct athn_usb_softc *, uint8_t *);
133 1.1 christos Static void athn_usb_rx_enable(struct athn_softc *);
134 1.1 christos Static void athn_usb_rx_frame(struct athn_usb_softc *, struct mbuf *);
135 1.1 christos Static void athn_usb_rx_radiotap(struct athn_softc *, struct mbuf *,
136 1.1 christos struct ar_rx_status *);
137 1.3 christos Static void athn_usb_rx_wmi_ctrl(struct athn_usb_softc *, uint8_t *, size_t);
138 1.1 christos Static void athn_usb_rxeof(usbd_xfer_handle, usbd_private_handle,
139 1.1 christos usbd_status);
140 1.1 christos Static void athn_usb_start(struct ifnet *);
141 1.1 christos Static void athn_usb_stop(struct ifnet *);
142 1.1 christos Static void athn_usb_swba(struct athn_usb_softc *);
143 1.1 christos Static int athn_usb_switch_chan(struct athn_softc *,
144 1.1 christos struct ieee80211_channel *, struct ieee80211_channel *);
145 1.1 christos Static void athn_usb_task(void *);
146 1.1 christos Static int athn_usb_tx(struct athn_softc *, struct mbuf *,
147 1.1 christos struct ieee80211_node *, struct athn_usb_tx_data *);
148 1.1 christos Static void athn_usb_txeof(usbd_xfer_handle, usbd_private_handle,
149 1.1 christos usbd_status);
150 1.1 christos Static void athn_usb_updateslot(struct ifnet *);
151 1.1 christos Static void athn_usb_updateslot_cb(struct athn_usb_softc *, void *);
152 1.1 christos Static void athn_usb_wait_async(struct athn_usb_softc *);
153 1.1 christos Static void athn_usb_wait_cmd(struct athn_usb_softc *);
154 1.1 christos Static void athn_usb_wait_msg(struct athn_usb_softc *);
155 1.1 christos Static void athn_usb_wait_wmi(struct athn_usb_softc *);
156 1.1 christos Static void athn_usb_watchdog(struct ifnet *);
157 1.1 christos Static int athn_usb_wmi_xcmd(struct athn_usb_softc *, uint16_t, void *,
158 1.1 christos int, void *);
159 1.1 christos Static void athn_usb_wmieof(usbd_xfer_handle, usbd_private_handle,
160 1.1 christos usbd_status);
161 1.1 christos Static void athn_usb_write(struct athn_softc *, uint32_t, uint32_t);
162 1.1 christos Static void athn_usb_write_barrier(struct athn_softc *);
163 1.1 christos
164 1.1 christos /************************************************************************
165 1.1 christos * unused/notyet declarations
166 1.1 christos */
167 1.1 christos #ifdef unused
168 1.1 christos Static int athn_usb_read_rom(struct athn_softc *);
169 1.1 christos #endif /* unused */
170 1.1 christos
171 1.1 christos #ifdef notyet_edca
172 1.1 christos Static void athn_usb_updateedca(struct ieee80211com *);
173 1.1 christos Static void athn_usb_updateedca_cb(struct athn_usb_softc *, void *);
174 1.1 christos #endif /* notyet_edca */
175 1.1 christos
176 1.1 christos #ifdef notyet
177 1.1 christos Static int athn_usb_ampdu_tx_start(struct ieee80211com *,
178 1.1 christos struct ieee80211_node *, uint8_t);
179 1.1 christos Static void athn_usb_ampdu_tx_start_cb(struct athn_usb_softc *, void *);
180 1.1 christos Static void athn_usb_ampdu_tx_stop(struct ieee80211com *,
181 1.1 christos struct ieee80211_node *, uint8_t);
182 1.1 christos Static void athn_usb_ampdu_tx_stop_cb(struct athn_usb_softc *, void *);
183 1.1 christos Static void athn_usb_delete_key(struct ieee80211com *,
184 1.1 christos struct ieee80211_node *, struct ieee80211_key *);
185 1.1 christos Static void athn_usb_delete_key_cb(struct athn_usb_softc *, void *);
186 1.1 christos Static int athn_usb_set_key(struct ieee80211com *,
187 1.1 christos struct ieee80211_node *, struct ieee80211_key *);
188 1.1 christos Static void athn_usb_set_key_cb(struct athn_usb_softc *, void *);
189 1.1 christos #endif /* notyet */
190 1.1 christos /************************************************************************/
191 1.1 christos
192 1.1 christos struct athn_usb_type {
193 1.1 christos struct usb_devno devno;
194 1.1 christos u_int flags;
195 1.1 christos };
196 1.1 christos
197 1.1 christos Static const struct athn_usb_type *
198 1.1 christos athn_usb_lookup(int vendor, int product)
199 1.1 christos {
200 1.1 christos static const struct athn_usb_type athn_usb_devs[] = {
201 1.1 christos #define _D(v,p,f) \
202 1.1 christos {{ USB_VENDOR_##v, USB_PRODUCT_##p }, ATHN_USB_FLAG_##f }
203 1.1 christos
204 1.1 christos _D( ACCTON, ACCTON_AR9280, AR7010 ),
205 1.1 christos _D( ACTIONTEC, ACTIONTEC_AR9287, AR7010 ),
206 1.1 christos _D( ATHEROS2, ATHEROS2_AR9271_1, NONE ),
207 1.1 christos _D( ATHEROS2, ATHEROS2_AR9271_2, NONE ),
208 1.1 christos _D( ATHEROS2, ATHEROS2_AR9271_3, NONE ),
209 1.1 christos _D( ATHEROS2, ATHEROS2_AR9280, AR7010 ),
210 1.1 christos _D( ATHEROS2, ATHEROS2_AR9287, AR7010 ),
211 1.1 christos _D( AZUREWAVE, AZUREWAVE_AR9271_1, NONE ),
212 1.1 christos _D( AZUREWAVE, AZUREWAVE_AR9271_2, NONE ),
213 1.1 christos _D( AZUREWAVE, AZUREWAVE_AR9271_3, NONE ),
214 1.1 christos _D( AZUREWAVE, AZUREWAVE_AR9271_4, NONE ),
215 1.1 christos _D( AZUREWAVE, AZUREWAVE_AR9271_5, NONE ),
216 1.1 christos _D( AZUREWAVE, AZUREWAVE_AR9271_6, NONE ),
217 1.1 christos _D( DLINK2, DLINK2_AR9271, NONE ),
218 1.1 christos _D( LITEON, LITEON_AR9271, NONE ),
219 1.1 christos _D( NETGEAR, NETGEAR_WNA1100, NONE ),
220 1.1 christos _D( NETGEAR, NETGEAR_WNDA3200, AR7010 ),
221 1.1 christos _D( VIA, VIA_AR9271, NONE )
222 1.1 christos #undef _D
223 1.1 christos };
224 1.1 christos
225 1.1 christos return (const void *)usb_lookup(athn_usb_devs, vendor, product);
226 1.1 christos }
227 1.1 christos
228 1.1 christos Static int
229 1.1 christos athn_usb_match(device_t parent, cfdata_t match, void *aux)
230 1.1 christos {
231 1.1 christos struct usb_attach_arg *uaa = aux;
232 1.1 christos
233 1.1 christos return athn_usb_lookup(uaa->vendor, uaa->product) != NULL ?
234 1.1 christos UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
235 1.1 christos }
236 1.1 christos
237 1.1 christos Static void
238 1.1 christos athn_usb_attach(device_t parent, device_t self, void *aux)
239 1.1 christos {
240 1.1 christos struct athn_usb_softc *usc;
241 1.1 christos struct athn_softc *sc;
242 1.1 christos struct usb_attach_arg *uaa;
243 1.1 christos int error;
244 1.1 christos
245 1.1 christos usc = device_private(self);
246 1.1 christos sc = &usc->usc_sc;
247 1.1 christos uaa = aux;
248 1.1 christos sc->sc_dev = self;
249 1.1 christos usc->usc_udev = uaa->device;
250 1.1 christos
251 1.1 christos aprint_naive("\n");
252 1.1 christos aprint_normal("\n");
253 1.1 christos
254 1.1 christos DPRINTFN(DBG_FN, sc, "\n");
255 1.1 christos
256 1.1 christos usc->usc_athn_attached = 0;
257 1.1 christos usc->usc_flags = athn_usb_lookup(uaa->vendor, uaa->product)->flags;
258 1.1 christos sc->sc_flags |= ATHN_FLAG_USB;
259 1.1 christos #ifdef notyet
260 1.1 christos /* Check if it is a combo WiFi+Bluetooth (WB193) device. */
261 1.1 christos if (strncmp(product, "wb193", 5) == 0)
262 1.1 christos sc->sc_flags |= ATHN_FLAG_BTCOEX3WIRE;
263 1.1 christos #endif
264 1.1 christos
265 1.1 christos sc->sc_ops.read = athn_usb_read;
266 1.1 christos sc->sc_ops.write = athn_usb_write;
267 1.1 christos sc->sc_ops.write_barrier = athn_usb_write_barrier;
268 1.1 christos
269 1.1 christos mutex_init(&usc->usc_task_mtx, MUTEX_DEFAULT, IPL_NET);
270 1.1 christos mutex_init(&usc->usc_tx_mtx, MUTEX_DEFAULT, IPL_NONE);
271 1.1 christos
272 1.1 christos usb_init_task(&usc->usc_task, athn_usb_task, usc, 0);
273 1.1 christos
274 1.1 christos if (usbd_set_config_no(usc->usc_udev, 1, 0) != 0) {
275 1.1 christos aprint_error_dev(sc->sc_dev,
276 1.1 christos "could not set configuration no\n");
277 1.1 christos goto fail;
278 1.1 christos }
279 1.1 christos
280 1.1 christos /* Get the first interface handle. */
281 1.1 christos error = usbd_device2interface_handle(usc->usc_udev, 0, &usc->usc_iface);
282 1.1 christos if (error != 0) {
283 1.1 christos aprint_error_dev(sc->sc_dev,
284 1.1 christos "could not get interface handle\n");
285 1.1 christos goto fail;
286 1.1 christos }
287 1.1 christos
288 1.1 christos if (athn_usb_open_pipes(usc) != 0)
289 1.1 christos goto fail;
290 1.1 christos
291 1.1 christos /* Allocate xfer for firmware commands. */
292 1.1 christos if (athn_usb_alloc_tx_cmd(usc) != 0)
293 1.1 christos goto fail;
294 1.1 christos
295 1.1 christos config_mountroot(self, athn_usb_attachhook);
296 1.1 christos
297 1.1 christos usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, usc->usc_udev, sc->sc_dev);
298 1.1 christos return;
299 1.1 christos
300 1.1 christos fail:
301 1.1 christos athn_usb_free_tx_cmd(usc);
302 1.1 christos athn_usb_close_pipes(usc);
303 1.1 christos usb_rem_task(usc->usc_udev, &usc->usc_task);
304 1.1 christos mutex_destroy(&usc->usc_tx_mtx);
305 1.1 christos mutex_destroy(&usc->usc_task_mtx);
306 1.1 christos }
307 1.1 christos
308 1.1 christos Static void
309 1.1 christos athn_usb_node_cleanup_cb(struct athn_usb_softc *usc, void *arg)
310 1.1 christos {
311 1.1 christos uint8_t sta_index = *(uint8_t *)arg;
312 1.1 christos
313 1.1 christos DPRINTFN(DBG_FN, usc, "\n");
314 1.1 christos DPRINTFN(DBG_NODES, usc, "removing node %u\n", sta_index);
315 1.1 christos athn_usb_remove_hw_node(usc, &sta_index);
316 1.1 christos }
317 1.1 christos
318 1.1 christos Static void
319 1.1 christos athn_usb_node_cleanup(struct ieee80211_node *ni)
320 1.1 christos {
321 1.1 christos struct athn_usb_softc *usc;
322 1.1 christos struct ieee80211com *ic;
323 1.1 christos uint8_t sta_index;
324 1.1 christos
325 1.1 christos usc = ATHN_USB_SOFTC(ni->ni_ic->ic_ifp->if_softc);
326 1.1 christos ic = &ATHN_SOFTC(usc)->sc_ic;
327 1.1 christos
328 1.1 christos DPRINTFN(DBG_FN, usc, "\n");
329 1.1 christos
330 1.1 christos if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
331 1.1 christos sta_index = ATHN_NODE(ni)->sta_index;
332 1.1 christos if (sta_index != 0)
333 1.1 christos athn_usb_do_async(usc, athn_usb_node_cleanup_cb,
334 1.1 christos &sta_index, sizeof(sta_index));
335 1.1 christos }
336 1.1 christos usc->usc_node_cleanup(ni);
337 1.1 christos }
338 1.1 christos
339 1.1 christos Static void
340 1.1 christos athn_usb_attachhook(device_t arg)
341 1.1 christos {
342 1.1 christos struct athn_usb_softc *usc = device_private(arg);
343 1.1 christos struct athn_softc *sc = &usc->usc_sc;
344 1.1 christos struct athn_ops *ops = &sc->sc_ops;
345 1.1 christos struct ieee80211com *ic = &sc->sc_ic;
346 1.1 christos struct ifnet *ifp = &sc->sc_if;
347 1.1 christos size_t i;
348 1.1 christos int s, error;
349 1.1 christos
350 1.1 christos if (usc->usc_dying)
351 1.1 christos return;
352 1.1 christos
353 1.1 christos DPRINTFN(DBG_FN, usc, "\n");
354 1.1 christos
355 1.1 christos /* Load firmware. */
356 1.1 christos error = athn_usb_load_firmware(usc);
357 1.1 christos if (error != 0) {
358 1.1 christos aprint_error_dev(sc->sc_dev,
359 1.1 christos "could not load firmware (%d)\n", error);
360 1.1 christos return;
361 1.1 christos }
362 1.1 christos
363 1.1 christos /* Setup the host transport communication interface. */
364 1.1 christos error = athn_usb_htc_setup(usc);
365 1.1 christos if (error != 0)
366 1.1 christos return;
367 1.1 christos
368 1.1 christos /* We're now ready to attach the bus agnostic driver. */
369 1.1 christos s = splnet();
370 1.1 christos ic->ic_ifp = ifp;
371 1.1 christos ic->ic_updateslot = athn_usb_updateslot;
372 1.1 christos sc->sc_max_aid = AR_USB_MAX_STA; /* Firmware is limited to 8 STA */
373 1.1 christos sc->sc_media_change = athn_usb_media_change;
374 1.1 christos error = athn_attach(sc);
375 1.1 christos if (error != 0) {
376 1.1 christos splx(s);
377 1.1 christos return;
378 1.1 christos }
379 1.1 christos usc->usc_athn_attached = 1;
380 1.1 christos
381 1.1 christos /* Override some operations for USB. */
382 1.1 christos ifp->if_init = athn_usb_init;
383 1.1 christos ifp->if_ioctl = athn_usb_ioctl;
384 1.1 christos ifp->if_start = athn_usb_start;
385 1.1 christos ifp->if_watchdog = athn_usb_watchdog;
386 1.1 christos
387 1.1 christos /* hooks for HostAP association and disassociation */
388 1.1 christos ic->ic_newassoc = athn_usb_newassoc;
389 1.1 christos usc->usc_node_cleanup = ic->ic_node_cleanup;
390 1.1 christos ic->ic_node_cleanup = athn_usb_node_cleanup;
391 1.1 christos
392 1.1 christos #ifdef notyet_edca
393 1.1 christos ic->ic_updateedca = athn_usb_updateedca;
394 1.1 christos #endif
395 1.1 christos #ifdef notyet
396 1.1 christos ic->ic_set_key = athn_usb_set_key;
397 1.1 christos ic->ic_delete_key = athn_usb_delete_key;
398 1.1 christos ic->ic_ampdu_tx_start = athn_usb_ampdu_tx_start;
399 1.1 christos ic->ic_ampdu_tx_stop = athn_usb_ampdu_tx_stop;
400 1.1 christos #endif
401 1.1 christos ic->ic_newstate = athn_usb_newstate;
402 1.1 christos
403 1.1 christos ops->rx_enable = athn_usb_rx_enable;
404 1.1 christos splx(s);
405 1.1 christos
406 1.1 christos /* Reset HW key cache entries. */
407 1.1 christos for (i = 0; i < sc->sc_kc_entries; i++)
408 1.1 christos athn_reset_key(sc, i);
409 1.1 christos
410 1.1 christos ops->enable_antenna_diversity(sc);
411 1.1 christos
412 1.1 christos #ifdef ATHN_BT_COEXISTENCE
413 1.1 christos /* Configure bluetooth coexistence for combo chips. */
414 1.1 christos if (sc->sc_flags & ATHN_FLAG_BTCOEX)
415 1.1 christos athn_btcoex_init(sc);
416 1.1 christos #endif
417 1.1 christos /* Configure LED. */
418 1.1 christos athn_led_init(sc);
419 1.1 christos
420 1.1 christos ieee80211_announce(ic);
421 1.1 christos }
422 1.1 christos
423 1.1 christos Static int
424 1.1 christos athn_usb_detach(device_t self, int flags)
425 1.1 christos {
426 1.1 christos struct athn_usb_softc *usc = device_private(self);
427 1.1 christos struct athn_softc *sc = &usc->usc_sc;
428 1.1 christos int s;
429 1.1 christos
430 1.1 christos DPRINTFN(DBG_FN, usc, "\n");
431 1.1 christos
432 1.1 christos s = splusb();
433 1.1 christos usc->usc_dying = 1;
434 1.1 christos
435 1.1 christos athn_usb_wait_wmi(usc);
436 1.1 christos athn_usb_wait_cmd(usc);
437 1.1 christos athn_usb_wait_msg(usc);
438 1.1 christos athn_usb_wait_async(usc);
439 1.1 christos
440 1.1 christos usb_rem_task(usc->usc_udev, &usc->usc_task);
441 1.1 christos
442 1.1 christos if (usc->usc_athn_attached) {
443 1.1 christos usc->usc_athn_attached = 0;
444 1.1 christos athn_detach(sc);
445 1.1 christos }
446 1.1 christos /* Abort and close Tx/Rx pipes. */
447 1.1 christos athn_usb_close_pipes(usc);
448 1.1 christos splx(s);
449 1.1 christos
450 1.1 christos /* Free Tx/Rx buffers. */
451 1.1 christos athn_usb_free_rx_list(usc);
452 1.1 christos athn_usb_free_tx_list(usc);
453 1.1 christos athn_usb_free_tx_cmd(usc);
454 1.1 christos
455 1.1 christos mutex_destroy(&usc->usc_tx_mtx);
456 1.1 christos mutex_destroy(&usc->usc_task_mtx);
457 1.1 christos
458 1.1 christos usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, usc->usc_udev, sc->sc_dev);
459 1.1 christos return 0;
460 1.1 christos }
461 1.1 christos
462 1.1 christos Static int
463 1.1 christos athn_usb_activate(device_t self, enum devact act)
464 1.1 christos {
465 1.1 christos struct athn_usb_softc *usc = device_private(self);
466 1.1 christos struct athn_softc *sc = &usc->usc_sc;
467 1.1 christos
468 1.1 christos DPRINTFN(DBG_FN, usc, "\n");
469 1.1 christos
470 1.1 christos switch (act) {
471 1.1 christos case DVACT_DEACTIVATE:
472 1.1 christos if_deactivate(sc->sc_ic.ic_ifp);
473 1.1 christos usc->usc_dying = 1;
474 1.1 christos return 0;
475 1.1 christos default:
476 1.1 christos return EOPNOTSUPP;
477 1.1 christos }
478 1.1 christos }
479 1.1 christos
480 1.1 christos Static int
481 1.1 christos athn_usb_open_pipes(struct athn_usb_softc *usc)
482 1.1 christos {
483 1.1 christos usb_endpoint_descriptor_t *ed;
484 1.1 christos int isize, error;
485 1.1 christos
486 1.1 christos DPRINTFN(DBG_FN, usc, "\n");
487 1.1 christos
488 1.1 christos error = usbd_open_pipe(usc->usc_iface, AR_PIPE_TX_DATA, 0,
489 1.1 christos &usc->usc_tx_data_pipe);
490 1.1 christos if (error != 0) {
491 1.1 christos aprint_error_dev(usc->usc_dev,
492 1.1 christos "could not open Tx bulk pipe\n");
493 1.1 christos goto fail;
494 1.1 christos }
495 1.1 christos
496 1.1 christos error = usbd_open_pipe(usc->usc_iface, AR_PIPE_RX_DATA, 0,
497 1.1 christos &usc->usc_rx_data_pipe);
498 1.1 christos if (error != 0) {
499 1.1 christos aprint_error_dev(usc->usc_dev,
500 1.1 christos "could not open Rx bulk pipe\n");
501 1.1 christos goto fail;
502 1.1 christos }
503 1.1 christos
504 1.1 christos ed = usbd_get_endpoint_descriptor(usc->usc_iface, AR_PIPE_RX_INTR);
505 1.1 christos if (ed == NULL) {
506 1.1 christos aprint_error_dev(usc->usc_dev,
507 1.1 christos "could not retrieve Rx intr pipe descriptor\n");
508 1.1 christos goto fail;
509 1.1 christos }
510 1.1 christos isize = UGETW(ed->wMaxPacketSize);
511 1.1 christos if (isize == 0) {
512 1.1 christos aprint_error_dev(usc->usc_dev,
513 1.1 christos "invalid Rx intr pipe descriptor\n");
514 1.1 christos goto fail;
515 1.1 christos }
516 1.1 christos usc->usc_ibuf = malloc(isize, M_USBDEV, M_NOWAIT);
517 1.1 christos if (usc->usc_ibuf == NULL) {
518 1.1 christos aprint_error_dev(usc->usc_dev,
519 1.1 christos "could not allocate Rx intr buffer\n");
520 1.1 christos goto fail;
521 1.1 christos }
522 1.1 christos error = usbd_open_pipe_intr(usc->usc_iface, AR_PIPE_RX_INTR,
523 1.1 christos USBD_SHORT_XFER_OK, &usc->usc_rx_intr_pipe, usc, usc->usc_ibuf, isize,
524 1.1 christos athn_usb_intr, USBD_DEFAULT_INTERVAL);
525 1.1 christos if (error != 0) {
526 1.1 christos aprint_error_dev(usc->usc_dev,
527 1.1 christos "could not open Rx intr pipe\n");
528 1.1 christos goto fail;
529 1.1 christos }
530 1.1 christos error = usbd_open_pipe(usc->usc_iface, AR_PIPE_TX_INTR, 0,
531 1.1 christos &usc->usc_tx_intr_pipe);
532 1.1 christos if (error != 0) {
533 1.1 christos aprint_error_dev(usc->usc_dev,
534 1.1 christos "could not open Tx intr pipe\n");
535 1.1 christos goto fail;
536 1.1 christos }
537 1.1 christos return 0;
538 1.1 christos fail:
539 1.1 christos athn_usb_close_pipes(usc);
540 1.1 christos return error;
541 1.1 christos }
542 1.1 christos
543 1.1 christos static inline void
544 1.1 christos athn_usb_kill_pipe(usbd_pipe_handle *pipeptr)
545 1.1 christos {
546 1.1 christos usbd_pipe_handle pipe;
547 1.1 christos
548 1.1 christos CTASSERT(sizeof(pipe) == sizeof(void *));
549 1.1 christos pipe = atomic_swap_ptr(pipeptr, NULL);
550 1.1 christos if (pipe != NULL) {
551 1.1 christos usbd_abort_pipe(pipe);
552 1.1 christos usbd_close_pipe(pipe);
553 1.1 christos }
554 1.1 christos }
555 1.1 christos
556 1.1 christos Static void
557 1.1 christos athn_usb_close_pipes(struct athn_usb_softc *usc)
558 1.1 christos {
559 1.1 christos uint8_t *ibuf;
560 1.1 christos
561 1.1 christos DPRINTFN(DBG_FN, usc, "\n");
562 1.1 christos
563 1.1 christos athn_usb_kill_pipe(&usc->usc_tx_data_pipe);
564 1.1 christos athn_usb_kill_pipe(&usc->usc_rx_data_pipe);
565 1.1 christos athn_usb_kill_pipe(&usc->usc_tx_intr_pipe);
566 1.1 christos athn_usb_kill_pipe(&usc->usc_rx_intr_pipe);
567 1.1 christos ibuf = atomic_swap_ptr(&usc->usc_ibuf, NULL);
568 1.1 christos if (ibuf != NULL)
569 1.1 christos free(ibuf, M_USBDEV);
570 1.1 christos }
571 1.1 christos
572 1.1 christos Static int
573 1.1 christos athn_usb_alloc_rx_list(struct athn_usb_softc *usc)
574 1.1 christos {
575 1.1 christos struct athn_usb_rx_data *data;
576 1.1 christos size_t i;
577 1.1 christos int error = 0;
578 1.1 christos
579 1.1 christos DPRINTFN(DBG_FN, usc, "\n");
580 1.1 christos
581 1.1 christos for (i = 0; i < ATHN_USB_RX_LIST_COUNT; i++) {
582 1.1 christos data = &usc->usc_rx_data[i];
583 1.1 christos
584 1.1 christos data->sc = usc; /* Backpointer for callbacks. */
585 1.1 christos
586 1.1 christos data->xfer = usbd_alloc_xfer(usc->usc_udev);
587 1.1 christos if (data->xfer == NULL) {
588 1.1 christos aprint_error_dev(usc->usc_dev,
589 1.1 christos "could not allocate xfer\n");
590 1.1 christos error = ENOMEM;
591 1.1 christos break;
592 1.1 christos }
593 1.1 christos data->buf = usbd_alloc_buffer(data->xfer, ATHN_USB_RXBUFSZ);
594 1.1 christos if (data->buf == NULL) {
595 1.1 christos aprint_error_dev(usc->usc_dev,
596 1.1 christos "could not allocate xfer buffer\n");
597 1.1 christos error = ENOMEM;
598 1.1 christos break;
599 1.1 christos }
600 1.1 christos }
601 1.1 christos if (error != 0)
602 1.1 christos athn_usb_free_rx_list(usc);
603 1.1 christos return error;
604 1.1 christos }
605 1.1 christos
606 1.1 christos Static void
607 1.1 christos athn_usb_free_rx_list(struct athn_usb_softc *usc)
608 1.1 christos {
609 1.1 christos usbd_xfer_handle xfer;
610 1.1 christos size_t i;
611 1.1 christos
612 1.1 christos DPRINTFN(DBG_FN, usc, "\n");
613 1.1 christos
614 1.1 christos /* NB: Caller must abort pipe first. */
615 1.1 christos for (i = 0; i < ATHN_USB_RX_LIST_COUNT; i++) {
616 1.1 christos CTASSERT(sizeof(xfer) == sizeof(void *));
617 1.1 christos xfer = atomic_swap_ptr(&usc->usc_rx_data[i].xfer, NULL);
618 1.1 christos if (xfer != NULL)
619 1.1 christos usbd_free_xfer(xfer);
620 1.1 christos }
621 1.1 christos }
622 1.1 christos
623 1.1 christos Static int
624 1.1 christos athn_usb_alloc_tx_list(struct athn_usb_softc *usc)
625 1.1 christos {
626 1.1 christos struct athn_usb_tx_data *data;
627 1.1 christos size_t i;
628 1.1 christos int error = 0;
629 1.1 christos
630 1.1 christos DPRINTFN(DBG_FN, usc, "\n");
631 1.1 christos
632 1.1 christos mutex_enter(&usc->usc_tx_mtx);
633 1.1 christos TAILQ_INIT(&usc->usc_tx_free_list);
634 1.1 christos for (i = 0; i < ATHN_USB_TX_LIST_COUNT; i++) {
635 1.1 christos data = &usc->usc_tx_data[i];
636 1.1 christos
637 1.1 christos data->sc = usc; /* Backpointer for callbacks. */
638 1.1 christos
639 1.1 christos data->xfer = usbd_alloc_xfer(usc->usc_udev);
640 1.1 christos if (data->xfer == NULL) {
641 1.1 christos aprint_error_dev(usc->usc_dev,
642 1.1 christos "could not allocate xfer\n");
643 1.1 christos error = ENOMEM;
644 1.1 christos break;
645 1.1 christos }
646 1.1 christos data->buf = usbd_alloc_buffer(data->xfer, ATHN_USB_TXBUFSZ);
647 1.1 christos if (data->buf == NULL) {
648 1.1 christos aprint_error_dev(usc->usc_dev,
649 1.1 christos "could not allocate xfer buffer\n");
650 1.1 christos error = ENOMEM;
651 1.1 christos break;
652 1.1 christos }
653 1.1 christos /* Append this Tx buffer to our free list. */
654 1.1 christos TAILQ_INSERT_TAIL(&usc->usc_tx_free_list, data, next);
655 1.1 christos }
656 1.1 christos if (error != 0)
657 1.1 christos athn_usb_free_tx_list(usc);
658 1.1 christos mutex_exit(&usc->usc_tx_mtx);
659 1.1 christos return error;
660 1.1 christos }
661 1.1 christos
662 1.1 christos Static void
663 1.1 christos athn_usb_free_tx_list(struct athn_usb_softc *usc)
664 1.1 christos {
665 1.1 christos usbd_xfer_handle xfer;
666 1.1 christos size_t i;
667 1.1 christos
668 1.1 christos DPRINTFN(DBG_FN, usc, "\n");
669 1.1 christos
670 1.1 christos /* NB: Caller must abort pipe first. */
671 1.1 christos for (i = 0; i < ATHN_USB_TX_LIST_COUNT; i++) {
672 1.1 christos CTASSERT(sizeof(xfer) == sizeof(void *));
673 1.1 christos xfer = atomic_swap_ptr(&usc->usc_tx_data[i].xfer, NULL);
674 1.1 christos if (xfer != NULL)
675 1.1 christos usbd_free_xfer(xfer);
676 1.1 christos }
677 1.1 christos }
678 1.1 christos
679 1.1 christos Static int
680 1.1 christos athn_usb_alloc_tx_cmd(struct athn_usb_softc *usc)
681 1.1 christos {
682 1.1 christos struct athn_usb_tx_data *data = &usc->usc_tx_cmd;
683 1.1 christos
684 1.1 christos DPRINTFN(DBG_FN, usc, "\n");
685 1.1 christos
686 1.1 christos data->sc = usc; /* Backpointer for callbacks. */
687 1.1 christos
688 1.1 christos data->xfer = usbd_alloc_xfer(usc->usc_udev);
689 1.1 christos if (data->xfer == NULL) {
690 1.1 christos aprint_error_dev(usc->usc_dev, "could not allocate xfer\n");
691 1.1 christos return ENOMEM;
692 1.1 christos }
693 1.1 christos data->buf = usbd_alloc_buffer(data->xfer, ATHN_USB_TXCMDSZ);
694 1.1 christos if (data->buf == NULL) {
695 1.1 christos aprint_error_dev(usc->usc_dev,
696 1.1 christos "could not allocate xfer buffer\n");
697 1.1 christos return ENOMEM;
698 1.1 christos }
699 1.1 christos return 0;
700 1.1 christos }
701 1.1 christos
702 1.1 christos Static void
703 1.1 christos athn_usb_free_tx_cmd(struct athn_usb_softc *usc)
704 1.1 christos {
705 1.1 christos usbd_xfer_handle xfer;
706 1.1 christos
707 1.1 christos DPRINTFN(DBG_FN, usc, "\n");
708 1.1 christos
709 1.1 christos CTASSERT(sizeof(xfer) == sizeof(void *));
710 1.1 christos xfer = atomic_swap_ptr(&usc->usc_tx_cmd.xfer, NULL);
711 1.1 christos if (xfer != NULL)
712 1.1 christos usbd_free_xfer(xfer);
713 1.1 christos }
714 1.1 christos
715 1.1 christos Static void
716 1.1 christos athn_usb_task(void *arg)
717 1.1 christos {
718 1.1 christos struct athn_usb_softc *usc = arg;
719 1.1 christos struct athn_usb_host_cmd_ring *ring = &usc->usc_cmdq;
720 1.1 christos struct athn_usb_host_cmd *cmd;
721 1.1 christos int s;
722 1.1 christos
723 1.1 christos DPRINTFN(DBG_FN, usc, "\n");
724 1.1 christos
725 1.1 christos /* Process host commands. */
726 1.1 christos s = splusb();
727 1.1 christos mutex_spin_enter(&usc->usc_task_mtx);
728 1.1 christos while (ring->next != ring->cur) {
729 1.1 christos cmd = &ring->cmd[ring->next];
730 1.1 christos mutex_spin_exit(&usc->usc_task_mtx);
731 1.1 christos splx(s);
732 1.1 christos
733 1.1 christos /* Invoke callback. */
734 1.1 christos if (!usc->usc_dying)
735 1.1 christos cmd->cb(usc, cmd->data);
736 1.1 christos
737 1.1 christos s = splusb();
738 1.1 christos mutex_spin_enter(&usc->usc_task_mtx);
739 1.1 christos ring->queued--;
740 1.1 christos ring->next = (ring->next + 1) % ATHN_USB_HOST_CMD_RING_COUNT;
741 1.1 christos }
742 1.1 christos mutex_spin_exit(&usc->usc_task_mtx);
743 1.1 christos wakeup(ring);
744 1.1 christos splx(s);
745 1.1 christos }
746 1.1 christos
747 1.1 christos Static void
748 1.1 christos athn_usb_do_async(struct athn_usb_softc *usc,
749 1.1 christos void (*cb)(struct athn_usb_softc *, void *), void *arg, int len)
750 1.1 christos {
751 1.1 christos struct athn_usb_host_cmd_ring *ring = &usc->usc_cmdq;
752 1.1 christos struct athn_usb_host_cmd *cmd;
753 1.1 christos int s;
754 1.1 christos
755 1.1 christos if (usc->usc_dying)
756 1.1 christos return;
757 1.1 christos
758 1.1 christos DPRINTFN(DBG_FN, usc, "\n");
759 1.1 christos
760 1.1 christos s = splusb();
761 1.1 christos mutex_spin_enter(&usc->usc_task_mtx);
762 1.1 christos cmd = &ring->cmd[ring->cur];
763 1.1 christos cmd->cb = cb;
764 1.1 christos KASSERT(len <= sizeof(cmd->data));
765 1.1 christos memcpy(cmd->data, arg, len);
766 1.1 christos ring->cur = (ring->cur + 1) % ATHN_USB_HOST_CMD_RING_COUNT;
767 1.1 christos
768 1.1 christos /* If there is no pending command already, schedule a task. */
769 1.1 christos if (++ring->queued == 1) {
770 1.1 christos mutex_spin_exit(&usc->usc_task_mtx);
771 1.1 christos usb_add_task(usc->usc_udev, &usc->usc_task, USB_TASKQ_DRIVER);
772 1.1 christos }
773 1.1 christos else
774 1.1 christos mutex_spin_exit(&usc->usc_task_mtx);
775 1.1 christos splx(s);
776 1.1 christos }
777 1.1 christos
778 1.1 christos Static void
779 1.1 christos athn_usb_wait_async(struct athn_usb_softc *usc)
780 1.1 christos {
781 1.1 christos
782 1.1 christos DPRINTFN(DBG_FN, usc, "\n");
783 1.1 christos
784 1.1 christos /* Wait for all queued asynchronous commands to complete. */
785 1.1 christos while (usc->usc_cmdq.queued > 0)
786 1.1 christos tsleep(&usc->usc_cmdq, 0, "cmdq", 0);
787 1.1 christos }
788 1.1 christos
789 1.1 christos Static int
790 1.1 christos athn_usb_load_firmware(struct athn_usb_softc *usc)
791 1.1 christos {
792 1.1 christos struct athn_softc *sc = &usc->usc_sc;
793 1.1 christos firmware_handle_t fwh;
794 1.1 christos usb_device_descriptor_t *dd;
795 1.1 christos usb_device_request_t req;
796 1.1 christos const char *name;
797 1.1 christos u_char *fw, *ptr;
798 1.1 christos size_t size;
799 1.1 christos uint32_t addr;
800 1.1 christos int s, mlen, error;
801 1.1 christos
802 1.1 christos DPRINTFN(DBG_FN, sc, "\n");
803 1.1 christos
804 1.1 christos /* Determine which firmware image to load. */
805 1.1 christos if (usc->usc_flags & ATHN_USB_FLAG_AR7010) {
806 1.1 christos dd = usbd_get_device_descriptor(usc->usc_udev);
807 1.1 christos if (UGETW(dd->bcdDevice) == 0x0202)
808 1.1 christos name = "athn-ar7010-11";
809 1.1 christos else
810 1.1 christos name = "athn-ar7010";
811 1.1 christos }
812 1.1 christos else
813 1.1 christos name = "athn-ar9271";
814 1.1 christos
815 1.1 christos /* Read firmware image from the filesystem. */
816 1.3 christos if ((error = firmware_open("if_athn_usb", name, &fwh)) != 0) {
817 1.1 christos aprint_error_dev(sc->sc_dev,
818 1.1 christos "failed to open firmware file %s (%d)\n", name, error);
819 1.1 christos return error;
820 1.1 christos }
821 1.1 christos size = firmware_get_size(fwh);
822 1.1 christos fw = firmware_malloc(size);
823 1.1 christos if (fw == NULL) {
824 1.1 christos aprint_error_dev(usc->usc_dev,
825 1.1 christos "failed to allocate firmware memory\n");
826 1.1 christos firmware_close(fwh);
827 1.1 christos return ENOMEM;
828 1.1 christos }
829 1.1 christos error = firmware_read(fwh, 0, fw, size);
830 1.1 christos firmware_close(fwh);
831 1.1 christos if (error != 0) {
832 1.1 christos aprint_error_dev(usc->usc_dev,
833 1.1 christos "failed to read firmware (error %d)\n", error);
834 1.1 christos firmware_free(fw, 0);
835 1.1 christos return error;
836 1.1 christos }
837 1.1 christos
838 1.1 christos /* Load firmware image. */
839 1.1 christos ptr = fw;
840 1.1 christos addr = AR9271_FIRMWARE >> 8;
841 1.1 christos req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
842 1.1 christos req.bRequest = AR_FW_DOWNLOAD;
843 1.1 christos USETW(req.wIndex, 0);
844 1.1 christos while (size > 0) {
845 1.1 christos mlen = MIN(size, 4096);
846 1.1 christos
847 1.1 christos USETW(req.wValue, addr);
848 1.1 christos USETW(req.wLength, mlen);
849 1.1 christos error = usbd_do_request(usc->usc_udev, &req, ptr);
850 1.1 christos if (error != 0) {
851 1.1 christos free(fw, M_DEVBUF);
852 1.1 christos return error;
853 1.1 christos }
854 1.1 christos addr += mlen >> 8;
855 1.1 christos ptr += mlen;
856 1.1 christos size -= mlen;
857 1.1 christos }
858 1.1 christos free(fw, M_DEVBUF);
859 1.1 christos
860 1.1 christos /* Start firmware. */
861 1.1 christos if (usc->usc_flags & ATHN_USB_FLAG_AR7010)
862 1.1 christos addr = AR7010_FIRMWARE_TEXT >> 8;
863 1.1 christos else
864 1.1 christos addr = AR9271_FIRMWARE_TEXT >> 8;
865 1.1 christos req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
866 1.1 christos req.bRequest = AR_FW_DOWNLOAD_COMP;
867 1.1 christos USETW(req.wIndex, 0);
868 1.1 christos USETW(req.wValue, addr);
869 1.1 christos USETW(req.wLength, 0);
870 1.1 christos
871 1.1 christos s = splusb();
872 1.1 christos usc->usc_wait_msg_id = AR_HTC_MSG_READY;
873 1.1 christos error = usbd_do_request(usc->usc_udev, &req, NULL);
874 1.1 christos /* Wait at most 1 second for firmware to boot. */
875 1.1 christos if (error == 0 && usc->usc_wait_msg_id != 0)
876 1.1 christos error = tsleep(&usc->usc_wait_msg_id, 0, "athnfw", hz);
877 1.1 christos usc->usc_wait_msg_id = 0;
878 1.1 christos splx(s);
879 1.1 christos return error;
880 1.1 christos }
881 1.1 christos
882 1.1 christos Static int
883 1.1 christos athn_usb_htc_msg(struct athn_usb_softc *usc, uint16_t msg_id, void *buf,
884 1.1 christos int len)
885 1.1 christos {
886 1.1 christos struct athn_usb_tx_data *data = &usc->usc_tx_cmd;
887 1.1 christos struct ar_htc_frame_hdr *htc;
888 1.1 christos struct ar_htc_msg_hdr *msg;
889 1.1 christos
890 1.1 christos if (usc->usc_dying)
891 1.1 christos return USBD_CANCELLED;
892 1.1 christos
893 1.1 christos DPRINTFN(DBG_FN, usc, "\n");
894 1.1 christos
895 1.1 christos htc = (struct ar_htc_frame_hdr *)data->buf;
896 1.1 christos memset(htc, 0, sizeof(*htc));
897 1.1 christos htc->endpoint_id = 0;
898 1.1 christos htc->payload_len = htobe16(sizeof(*msg) + len);
899 1.1 christos
900 1.1 christos msg = (struct ar_htc_msg_hdr *)&htc[1];
901 1.1 christos msg->msg_id = htobe16(msg_id);
902 1.1 christos
903 1.1 christos memcpy(&msg[1], buf, len);
904 1.1 christos
905 1.1 christos usbd_setup_xfer(data->xfer, usc->usc_tx_intr_pipe, NULL, data->buf,
906 1.1 christos sizeof(*htc) + sizeof(*msg) + len,
907 1.1 christos USBD_SHORT_XFER_OK | USBD_NO_COPY, ATHN_USB_CMD_TIMEOUT, NULL);
908 1.1 christos return usbd_sync_transfer(data->xfer);
909 1.1 christos }
910 1.1 christos
911 1.1 christos Static int
912 1.1 christos athn_usb_htc_setup(struct athn_usb_softc *usc)
913 1.1 christos {
914 1.1 christos struct ar_htc_msg_config_pipe cfg;
915 1.1 christos int s, error;
916 1.1 christos
917 1.1 christos /*
918 1.1 christos * Connect WMI services to USB pipes.
919 1.1 christos */
920 1.1 christos error = athn_usb_htc_connect_svc(usc, AR_SVC_WMI_CONTROL,
921 1.1 christos AR_PIPE_TX_INTR, AR_PIPE_RX_INTR, &usc->usc_ep_ctrl);
922 1.1 christos if (error != 0)
923 1.1 christos return error;
924 1.1 christos error = athn_usb_htc_connect_svc(usc, AR_SVC_WMI_BEACON,
925 1.1 christos AR_PIPE_TX_DATA, AR_PIPE_RX_DATA, &usc->usc_ep_bcn);
926 1.1 christos if (error != 0)
927 1.1 christos return error;
928 1.1 christos error = athn_usb_htc_connect_svc(usc, AR_SVC_WMI_CAB,
929 1.1 christos AR_PIPE_TX_DATA, AR_PIPE_RX_DATA, &usc->usc_ep_cab);
930 1.1 christos if (error != 0)
931 1.1 christos return error;
932 1.1 christos error = athn_usb_htc_connect_svc(usc, AR_SVC_WMI_UAPSD,
933 1.1 christos AR_PIPE_TX_DATA, AR_PIPE_RX_DATA, &usc->usc_ep_uapsd);
934 1.1 christos if (error != 0)
935 1.1 christos return error;
936 1.1 christos error = athn_usb_htc_connect_svc(usc, AR_SVC_WMI_MGMT,
937 1.1 christos AR_PIPE_TX_DATA, AR_PIPE_RX_DATA, &usc->usc_ep_mgmt);
938 1.1 christos if (error != 0)
939 1.1 christos return error;
940 1.1 christos error = athn_usb_htc_connect_svc(usc, AR_SVC_WMI_DATA_BE,
941 1.3 christos AR_PIPE_TX_DATA, AR_PIPE_RX_DATA, &usc->usc_ep_data[WME_AC_BE]);
942 1.1 christos if (error != 0)
943 1.1 christos return error;
944 1.1 christos error = athn_usb_htc_connect_svc(usc, AR_SVC_WMI_DATA_BK,
945 1.3 christos AR_PIPE_TX_DATA, AR_PIPE_RX_DATA, &usc->usc_ep_data[WME_AC_BK]);
946 1.1 christos if (error != 0)
947 1.1 christos return error;
948 1.1 christos error = athn_usb_htc_connect_svc(usc, AR_SVC_WMI_DATA_VI,
949 1.3 christos AR_PIPE_TX_DATA, AR_PIPE_RX_DATA, &usc->usc_ep_data[WME_AC_VI]);
950 1.1 christos if (error != 0)
951 1.1 christos return error;
952 1.1 christos error = athn_usb_htc_connect_svc(usc, AR_SVC_WMI_DATA_VO,
953 1.3 christos AR_PIPE_TX_DATA, AR_PIPE_RX_DATA, &usc->usc_ep_data[WME_AC_VO]);
954 1.1 christos if (error != 0)
955 1.1 christos return error;
956 1.1 christos
957 1.1 christos /* Set credits for WLAN Tx pipe. */
958 1.1 christos memset(&cfg, 0, sizeof(cfg));
959 1.1 christos cfg.pipe_id = UE_GET_ADDR(AR_PIPE_TX_DATA);
960 1.1 christos cfg.credits = (usc->usc_flags & ATHN_USB_FLAG_AR7010) ? 45 : 33;
961 1.1 christos
962 1.1 christos s = splusb();
963 1.1 christos
964 1.1 christos usc->usc_wait_msg_id = AR_HTC_MSG_CONF_PIPE_RSP;
965 1.1 christos error = athn_usb_htc_msg(usc, AR_HTC_MSG_CONF_PIPE, &cfg, sizeof(cfg));
966 1.1 christos if (error == 0 && usc->usc_wait_msg_id != 0)
967 1.1 christos error = tsleep(&usc->usc_wait_msg_id, 0, "athnhtc", hz);
968 1.1 christos usc->usc_wait_msg_id = 0;
969 1.1 christos
970 1.1 christos splx(s);
971 1.1 christos
972 1.1 christos if (error != 0) {
973 1.1 christos aprint_error_dev(usc->usc_dev, "could not configure pipe\n");
974 1.1 christos return error;
975 1.1 christos }
976 1.1 christos
977 1.1 christos error = athn_usb_htc_msg(usc, AR_HTC_MSG_SETUP_COMPLETE, NULL, 0);
978 1.1 christos if (error != 0) {
979 1.1 christos aprint_error_dev(usc->usc_dev, "could not complete setup\n");
980 1.1 christos return error;
981 1.1 christos }
982 1.1 christos return 0;
983 1.1 christos }
984 1.1 christos
985 1.1 christos Static int
986 1.1 christos athn_usb_htc_connect_svc(struct athn_usb_softc *usc, uint16_t svc_id,
987 1.1 christos uint8_t ul_pipe, uint8_t dl_pipe, uint8_t *endpoint_id)
988 1.1 christos {
989 1.1 christos struct ar_htc_msg_conn_svc msg;
990 1.1 christos struct ar_htc_msg_conn_svc_rsp rsp;
991 1.1 christos int s, error;
992 1.1 christos
993 1.1 christos DPRINTFN(DBG_FN, usc, "\n");
994 1.1 christos
995 1.1 christos memset(&msg, 0, sizeof(msg));
996 1.1 christos msg.svc_id = htobe16(svc_id);
997 1.1 christos msg.dl_pipeid = UE_GET_ADDR(dl_pipe);
998 1.1 christos msg.ul_pipeid = UE_GET_ADDR(ul_pipe);
999 1.1 christos s = splusb();
1000 1.1 christos
1001 1.1 christos usc->usc_msg_conn_svc_rsp = &rsp;
1002 1.1 christos
1003 1.1 christos usc->usc_wait_msg_id = AR_HTC_MSG_CONN_SVC_RSP;
1004 1.1 christos error = athn_usb_htc_msg(usc, AR_HTC_MSG_CONN_SVC, &msg, sizeof(msg));
1005 1.1 christos if (error == 0 && usc->usc_wait_msg_id != 0)
1006 1.1 christos error = tsleep(&usc->usc_wait_msg_id, 0, "athnhtc", hz);
1007 1.1 christos usc->usc_wait_msg_id = 0;
1008 1.1 christos
1009 1.1 christos splx(s);
1010 1.1 christos if (error != 0) {
1011 1.1 christos aprint_error_dev(usc->usc_dev,
1012 1.1 christos "error waiting for service %d connection\n", svc_id);
1013 1.1 christos return error;
1014 1.1 christos }
1015 1.1 christos if (rsp.status != AR_HTC_SVC_SUCCESS) {
1016 1.1 christos aprint_error_dev(usc->usc_dev,
1017 1.1 christos "service %d connection failed, error %d\n",
1018 1.1 christos svc_id, rsp.status);
1019 1.1 christos return EIO;
1020 1.1 christos }
1021 1.1 christos DPRINTFN(DBG_INIT, usc,
1022 1.1 christos "service %d successfully connected to endpoint %d\n",
1023 1.1 christos svc_id, rsp.endpoint_id);
1024 1.1 christos
1025 1.1 christos /* Return endpoint id. */
1026 1.1 christos *endpoint_id = rsp.endpoint_id;
1027 1.1 christos return 0;
1028 1.1 christos }
1029 1.1 christos
1030 1.1 christos Static void
1031 1.1 christos athn_usb_wait_msg(struct athn_usb_softc *usc)
1032 1.1 christos {
1033 1.1 christos
1034 1.1 christos DPRINTFN(DBG_FN, usc, "\n");
1035 1.1 christos
1036 1.1 christos while (__predict_false(usc->usc_wait_msg_id))
1037 1.1 christos tsleep(&usc->usc_wait_msg_id, 0, "athnmsg", hz);
1038 1.1 christos }
1039 1.1 christos
1040 1.1 christos Static void
1041 1.1 christos athn_usb_wait_cmd(struct athn_usb_softc *usc)
1042 1.1 christos {
1043 1.1 christos
1044 1.1 christos DPRINTFN(DBG_FN, usc, "\n");
1045 1.1 christos
1046 1.1 christos while (__predict_false(usc->usc_wait_cmd_id))
1047 1.1 christos tsleep(&usc->usc_wait_cmd_id, 0, "athncmd", hz);
1048 1.1 christos }
1049 1.1 christos
1050 1.1 christos Static void
1051 1.1 christos athn_usb_wmieof(usbd_xfer_handle xfer, usbd_private_handle priv,
1052 1.1 christos usbd_status status)
1053 1.1 christos {
1054 1.1 christos struct athn_usb_softc *usc = priv;
1055 1.1 christos
1056 1.1 christos DPRINTFN(DBG_FN, usc, "\n");
1057 1.1 christos
1058 1.1 christos if (__predict_false(status == USBD_STALLED))
1059 1.1 christos usbd_clear_endpoint_stall_async(usc->usc_tx_intr_pipe);
1060 1.1 christos
1061 1.1 christos usc->usc_wmi_done = 1;
1062 1.1 christos wakeup(&usc->usc_wmi_done);
1063 1.1 christos }
1064 1.1 christos
1065 1.1 christos Static int
1066 1.1 christos athn_usb_wmi_xcmd(struct athn_usb_softc *usc, uint16_t cmd_id, void *ibuf,
1067 1.1 christos int ilen, void *obuf)
1068 1.1 christos {
1069 1.1 christos struct athn_usb_tx_data *data = &usc->usc_tx_cmd;
1070 1.1 christos struct ar_htc_frame_hdr *htc;
1071 1.1 christos struct ar_wmi_cmd_hdr *wmi;
1072 1.1 christos int s, error;
1073 1.1 christos
1074 1.1 christos if (usc->usc_dying)
1075 1.1 christos return EIO;
1076 1.1 christos
1077 1.1 christos DPRINTFN(DBG_FN, usc, "\n");
1078 1.1 christos
1079 1.1 christos htc = (struct ar_htc_frame_hdr *)data->buf;
1080 1.1 christos memset(htc, 0, sizeof(*htc));
1081 1.1 christos htc->endpoint_id = usc->usc_ep_ctrl;
1082 1.1 christos htc->payload_len = htobe16(sizeof(*wmi) + ilen);
1083 1.1 christos
1084 1.1 christos wmi = (struct ar_wmi_cmd_hdr *)&htc[1];
1085 1.1 christos wmi->cmd_id = htobe16(cmd_id);
1086 1.1 christos usc->usc_wmi_seq_no++;
1087 1.1 christos wmi->seq_no = htobe16(usc->usc_wmi_seq_no);
1088 1.1 christos
1089 1.1 christos memcpy(&wmi[1], ibuf, ilen);
1090 1.1 christos
1091 1.1 christos usbd_setup_xfer(data->xfer, usc->usc_tx_intr_pipe, usc, data->buf,
1092 1.1 christos sizeof(*htc) + sizeof(*wmi) + ilen,
1093 1.1 christos USBD_SHORT_XFER_OK | USBD_NO_COPY, ATHN_USB_CMD_TIMEOUT,
1094 1.1 christos athn_usb_wmieof);
1095 1.1 christos
1096 1.1 christos s = splusb();
1097 1.1 christos usc->usc_wmi_done = 0;
1098 1.1 christos usc->usc_wait_cmd_id = cmd_id;
1099 1.1 christos error = usbd_transfer(data->xfer);
1100 1.1 christos if (__predict_true(error == 0 || error == USBD_IN_PROGRESS)) {
1101 1.1 christos usc->usc_obuf = obuf;
1102 1.1 christos
1103 1.1 christos /* Wait for WMI command to complete. */
1104 1.1 christos error = tsleep(&usc->usc_wait_cmd_id, 0, "athnwmi", hz);
1105 1.1 christos usc->usc_wait_cmd_id = 0;
1106 1.1 christos athn_usb_wait_wmi(usc);
1107 1.1 christos }
1108 1.1 christos splx(s);
1109 1.1 christos return error;
1110 1.1 christos }
1111 1.1 christos
1112 1.1 christos Static void
1113 1.1 christos athn_usb_wait_wmi(struct athn_usb_softc *usc)
1114 1.1 christos {
1115 1.1 christos
1116 1.1 christos DPRINTFN(DBG_FN, usc, "\n");
1117 1.1 christos
1118 1.1 christos while (__predict_false(!usc->usc_wmi_done))
1119 1.1 christos tsleep(&usc->usc_wmi_done, 0, "athnwmi", 0);
1120 1.1 christos }
1121 1.1 christos
1122 1.1 christos #ifdef unused
1123 1.1 christos Static int
1124 1.1 christos athn_usb_read_rom(struct athn_softc *sc)
1125 1.1 christos {
1126 1.1 christos struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
1127 1.1 christos uint32_t addrs[8], vals[8], addr;
1128 1.1 christos uint16_t *eep;
1129 1.1 christos size_t i, j;
1130 1.1 christos int error = 0;
1131 1.1 christos
1132 1.1 christos DPRINTFN(DBG_FN, sc, "\n");
1133 1.1 christos
1134 1.1 christos /* Read EEPROM by blocks of 16 bytes. */
1135 1.1 christos eep = sc->sc_eep;
1136 1.1 christos addr = AR_EEPROM_OFFSET(sc->sc_eep_base);
1137 1.1 christos for (i = 0; i < sc->sc_eep_size / 16; i++) {
1138 1.1 christos for (j = 0; j < 8; j++, addr += 4)
1139 1.1 christos addrs[j] = htobe32(addr);
1140 1.1 christos error = athn_usb_wmi_xcmd(usc, AR_WMI_CMD_REG_READ,
1141 1.1 christos addrs, sizeof(addrs), vals);
1142 1.1 christos if (error != 0)
1143 1.1 christos break;
1144 1.1 christos for (j = 0; j < 8; j++)
1145 1.1 christos *eep++ = be32toh(vals[j]);
1146 1.1 christos }
1147 1.1 christos return error;
1148 1.1 christos }
1149 1.1 christos #endif /* unused */
1150 1.1 christos
1151 1.1 christos Static uint32_t
1152 1.1 christos athn_usb_read(struct athn_softc *sc, uint32_t addr)
1153 1.1 christos {
1154 1.1 christos struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
1155 1.1 christos uint32_t val;
1156 1.1 christos int error;
1157 1.1 christos
1158 1.1 christos if (usc->usc_dying)
1159 1.1 christos return 0;
1160 1.1 christos
1161 1.1 christos DPRINTFN(DBG_FN, sc, "\n");
1162 1.1 christos
1163 1.1 christos /* Flush pending writes for strict consistency. */
1164 1.1 christos athn_usb_write_barrier(sc);
1165 1.1 christos
1166 1.1 christos addr = htobe32(addr);
1167 1.1 christos error = athn_usb_wmi_xcmd(usc, AR_WMI_CMD_REG_READ,
1168 1.1 christos &addr, sizeof(addr), &val);
1169 1.1 christos if (error != 0)
1170 1.1 christos return 0xdeadbeef;
1171 1.1 christos return be32toh(val);
1172 1.1 christos }
1173 1.1 christos
1174 1.1 christos Static void
1175 1.1 christos athn_usb_write(struct athn_softc *sc, uint32_t addr, uint32_t val)
1176 1.1 christos {
1177 1.1 christos struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
1178 1.1 christos
1179 1.1 christos if (usc->usc_dying)
1180 1.1 christos return;
1181 1.1 christos
1182 1.1 christos DPRINTFN(DBG_FN, sc, "\n");
1183 1.1 christos
1184 1.1 christos usc->usc_wbuf[usc->usc_wcount].addr = htobe32(addr);
1185 1.1 christos usc->usc_wbuf[usc->usc_wcount].val = htobe32(val);
1186 1.1 christos if (++usc->usc_wcount == AR_MAX_WRITE_COUNT)
1187 1.1 christos athn_usb_write_barrier(sc);
1188 1.1 christos }
1189 1.1 christos
1190 1.1 christos Static void
1191 1.1 christos athn_usb_write_barrier(struct athn_softc *sc)
1192 1.1 christos {
1193 1.1 christos struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
1194 1.1 christos
1195 1.1 christos if (usc->usc_dying)
1196 1.1 christos goto done;
1197 1.1 christos
1198 1.1 christos DPRINTFN(DBG_FN, sc, "\n");
1199 1.1 christos
1200 1.1 christos if (usc->usc_wcount == 0)
1201 1.1 christos return;
1202 1.1 christos
1203 1.1 christos (void)athn_usb_wmi_xcmd(usc, AR_WMI_CMD_REG_WRITE,
1204 1.1 christos usc->usc_wbuf, usc->usc_wcount * sizeof(usc->usc_wbuf[0]), NULL);
1205 1.1 christos done:
1206 1.1 christos usc->usc_wcount = 0; /* Always flush buffer. */
1207 1.1 christos }
1208 1.1 christos
1209 1.1 christos Static int
1210 1.1 christos athn_usb_media_change(struct ifnet *ifp)
1211 1.1 christos {
1212 1.1 christos struct athn_softc *sc = ifp->if_softc;
1213 1.1 christos struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
1214 1.1 christos int error;
1215 1.1 christos
1216 1.1 christos if (usc->usc_dying)
1217 1.1 christos return EIO;
1218 1.1 christos
1219 1.1 christos DPRINTFN(DBG_FN, sc, "\n");
1220 1.1 christos
1221 1.1 christos error = ieee80211_media_change(ifp);
1222 1.1 christos if (error == ENETRESET && IS_UP_AND_RUNNING(ifp)) {
1223 1.1 christos athn_usb_stop(ifp);
1224 1.1 christos error = athn_usb_init(ifp);
1225 1.1 christos }
1226 1.1 christos return error;
1227 1.1 christos }
1228 1.1 christos
1229 1.1 christos Static int
1230 1.1 christos athn_usb_newstate(struct ieee80211com *ic, enum ieee80211_state nstate,
1231 1.1 christos int arg)
1232 1.1 christos {
1233 1.1 christos struct athn_softc *sc = ic->ic_ifp->if_softc;
1234 1.1 christos struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
1235 1.1 christos struct athn_usb_cmd_newstate cmd;
1236 1.1 christos
1237 1.1 christos DPRINTFN(DBG_FN, sc, "\n");
1238 1.1 christos
1239 1.1 christos /* Do it in a process context. */
1240 1.1 christos cmd.state = nstate;
1241 1.1 christos cmd.arg = arg;
1242 1.1 christos athn_usb_do_async(usc, athn_usb_newstate_cb, &cmd, sizeof(cmd));
1243 1.1 christos return 0;
1244 1.1 christos }
1245 1.1 christos
1246 1.1 christos Static void
1247 1.1 christos athn_usb_newstate_cb(struct athn_usb_softc *usc, void *arg)
1248 1.1 christos {
1249 1.1 christos struct athn_usb_cmd_newstate *cmd = arg;
1250 1.1 christos struct athn_softc *sc = &usc->usc_sc;
1251 1.1 christos struct ieee80211com *ic = &sc->sc_ic;
1252 1.1 christos enum ieee80211_state ostate, nstate;
1253 1.1 christos uint32_t reg, imask;
1254 1.1 christos int s;
1255 1.1 christos
1256 1.1 christos DPRINTFN(DBG_FN, sc, "\n");
1257 1.1 christos
1258 1.1 christos callout_stop(&sc->sc_calib_to);
1259 1.1 christos
1260 1.1 christos s = splnet();
1261 1.1 christos
1262 1.1 christos ostate = ic->ic_state;
1263 1.1 christos nstate = cmd->state;
1264 1.1 christos DPRINTFN(DBG_STM, usc, "newstate %s(%d) -> %s(%d)\n",
1265 1.1 christos ieee80211_state_name[ostate], ostate,
1266 1.1 christos ieee80211_state_name[nstate], nstate);
1267 1.1 christos
1268 1.1 christos if (ostate == IEEE80211_S_RUN) {
1269 1.1 christos uint8_t sta_index;
1270 1.1 christos
1271 1.1 christos sta_index = ATHN_NODE(ic->ic_bss)->sta_index;
1272 1.1 christos DPRINTFN(DBG_NODES, usc, "removing node %u\n", sta_index);
1273 1.1 christos athn_usb_remove_hw_node(usc, &sta_index);
1274 1.1 christos }
1275 1.1 christos
1276 1.1 christos switch (nstate) {
1277 1.1 christos case IEEE80211_S_INIT:
1278 1.1 christos athn_set_led(sc, 0);
1279 1.1 christos break;
1280 1.1 christos case IEEE80211_S_SCAN:
1281 1.1 christos /* Make the LED blink while scanning. */
1282 1.1 christos athn_set_led(sc, !sc->sc_led_state);
1283 1.1 christos (void)athn_usb_switch_chan(sc, ic->ic_curchan, NULL);
1284 1.1 christos if (!usc->usc_dying)
1285 1.1 christos callout_schedule(&sc->sc_scan_to, hz / 5);
1286 1.1 christos break;
1287 1.1 christos case IEEE80211_S_AUTH:
1288 1.1 christos athn_set_led(sc, 0);
1289 1.1 christos athn_usb_switch_chan(sc, ic->ic_curchan, NULL);
1290 1.1 christos break;
1291 1.1 christos case IEEE80211_S_ASSOC:
1292 1.1 christos break;
1293 1.1 christos case IEEE80211_S_RUN:
1294 1.1 christos athn_set_led(sc, 1);
1295 1.1 christos
1296 1.1 christos if (ic->ic_opmode == IEEE80211_M_MONITOR)
1297 1.1 christos break;
1298 1.1 christos
1299 1.1 christos /* Create node entry for our BSS. */
1300 1.1 christos DPRINTFN(DBG_NODES, sc, "create node for AID=0x%x\n",
1301 1.1 christos ic->ic_bss->ni_associd);
1302 1.1 christos athn_usb_create_node(usc, ic->ic_bss); /* XXX: handle error? */
1303 1.1 christos
1304 1.1 christos athn_set_bss(sc, ic->ic_bss);
1305 1.1 christos athn_usb_wmi_cmd(usc, AR_WMI_CMD_DISABLE_INTR);
1306 1.1 christos #ifndef IEEE80211_STA_ONLY
1307 1.1 christos if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
1308 1.1 christos athn_set_hostap_timers(sc);
1309 1.1 christos /* Enable software beacon alert interrupts. */
1310 1.1 christos imask = htobe32(AR_IMR_SWBA);
1311 1.1 christos }
1312 1.1 christos else
1313 1.1 christos #endif
1314 1.1 christos {
1315 1.1 christos athn_set_sta_timers(sc);
1316 1.1 christos /* Enable beacon miss interrupts. */
1317 1.1 christos imask = htobe32(AR_IMR_BMISS);
1318 1.1 christos
1319 1.1 christos /* Stop receiving beacons from other BSS. */
1320 1.1 christos reg = AR_READ(sc, AR_RX_FILTER);
1321 1.1 christos reg = (reg & ~AR_RX_FILTER_BEACON) |
1322 1.1 christos AR_RX_FILTER_MYBEACON;
1323 1.1 christos AR_WRITE(sc, AR_RX_FILTER, reg);
1324 1.1 christos AR_WRITE_BARRIER(sc);
1325 1.1 christos }
1326 1.1 christos athn_usb_wmi_xcmd(usc, AR_WMI_CMD_ENABLE_INTR,
1327 1.1 christos &imask, sizeof(imask), NULL);
1328 1.1 christos break;
1329 1.1 christos }
1330 1.1 christos if (!usc->usc_dying)
1331 1.1 christos (void)sc->sc_newstate(ic, nstate, cmd->arg);
1332 1.1 christos splx(s);
1333 1.1 christos }
1334 1.1 christos
1335 1.1 christos Static void
1336 1.1 christos athn_usb_newassoc(struct ieee80211_node *ni, int isnew)
1337 1.1 christos {
1338 1.1 christos struct ieee80211com *ic = ni->ni_ic;
1339 1.1 christos struct athn_softc *sc = ic->ic_ifp->if_softc;
1340 1.1 christos struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
1341 1.1 christos
1342 1.1 christos DPRINTFN(DBG_FN, sc, "\n");
1343 1.1 christos
1344 1.1 christos if (ic->ic_opmode != IEEE80211_M_HOSTAP || !isnew)
1345 1.1 christos return;
1346 1.1 christos
1347 1.1 christos /* Do it in a process context. */
1348 1.1 christos ieee80211_ref_node(ni);
1349 1.1 christos athn_usb_do_async(usc, athn_usb_newassoc_cb, &ni, sizeof(ni));
1350 1.1 christos }
1351 1.1 christos
1352 1.1 christos Static void
1353 1.1 christos athn_usb_newassoc_cb(struct athn_usb_softc *usc, void *arg)
1354 1.1 christos {
1355 1.1 christos struct ieee80211_node *ni = *(void **)arg;
1356 1.1 christos int s;
1357 1.1 christos
1358 1.1 christos DPRINTFN(DBG_FN, usc, "\n");
1359 1.1 christos
1360 1.1 christos s = splnet();
1361 1.1 christos /* NB: Node may have left before we got scheduled. */
1362 1.1 christos if (ni->ni_associd != 0) {
1363 1.1 christos DPRINTFN(DBG_NODES, usc, "creating node for AID=0x%x\n",
1364 1.1 christos ni->ni_associd);
1365 1.1 christos (void)athn_usb_create_node(usc, ni); /* XXX: handle error? */
1366 1.1 christos }
1367 1.1 christos ieee80211_free_node(ni);
1368 1.1 christos splx(s);
1369 1.1 christos }
1370 1.1 christos
1371 1.1 christos #ifdef notyet
1372 1.1 christos Static int
1373 1.1 christos athn_usb_ampdu_tx_start(struct ieee80211com *ic, struct ieee80211_node *ni,
1374 1.1 christos uint8_t tid)
1375 1.1 christos {
1376 1.1 christos struct athn_softc *sc = ic->ic_ifp->if_softc;
1377 1.1 christos struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
1378 1.1 christos struct athn_node *an = ATHN_NODE(ni);
1379 1.1 christos struct athn_usb_aggr_cmd cmd;
1380 1.1 christos
1381 1.1 christos DPRINTFN(DBG_FN, sc, "\n");
1382 1.1 christos
1383 1.1 christos /* Do it in a process context. */
1384 1.1 christos cmd.sta_index = an->sta_index;
1385 1.1 christos cmd.tid = tid;
1386 1.1 christos athn_usb_do_async(usc, athn_usb_ampdu_tx_start_cb, &cmd, sizeof(cmd));
1387 1.1 christos return 0;
1388 1.1 christos }
1389 1.1 christos
1390 1.1 christos Static void
1391 1.1 christos athn_usb_ampdu_tx_start_cb(struct athn_usb_softc *usc, void *arg)
1392 1.1 christos {
1393 1.1 christos struct athn_usb_aggr_cmd *cmd = arg;
1394 1.1 christos struct ar_htc_target_aggr aggr;
1395 1.1 christos
1396 1.1 christos DPRINTFN(DBG_FN, usc, "\n");
1397 1.1 christos
1398 1.1 christos memset(&aggr, 0, sizeof(aggr));
1399 1.1 christos aggr.sta_index = cmd->sta_index;
1400 1.1 christos aggr.tidno = cmd->tid;
1401 1.1 christos aggr.aggr_enable = 1;
1402 1.1 christos (void)athn_usb_wmi_xcmd(usc, AR_WMI_CMD_TX_AGGR_ENABLE,
1403 1.1 christos &aggr, sizeof(aggr), NULL);
1404 1.1 christos }
1405 1.1 christos
1406 1.1 christos Static void
1407 1.1 christos athn_usb_ampdu_tx_stop(struct ieee80211com *ic, struct ieee80211_node *ni,
1408 1.1 christos uint8_t tid)
1409 1.1 christos {
1410 1.1 christos struct athn_softc *sc = ic->ic_ifp->if_softc;
1411 1.1 christos struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
1412 1.1 christos struct athn_node *an = ATHN_NODE(ni);
1413 1.1 christos struct athn_usb_aggr_cmd cmd;
1414 1.1 christos
1415 1.1 christos DPRINTFN(DBG_FN, sc, "\n");
1416 1.1 christos
1417 1.1 christos /* Do it in a process context. */
1418 1.1 christos cmd.sta_index = an->sta_index;
1419 1.1 christos cmd.tid = tid;
1420 1.1 christos athn_usb_do_async(usc, athn_usb_ampdu_tx_stop_cb, &cmd, sizeof(cmd));
1421 1.1 christos }
1422 1.1 christos
1423 1.1 christos Static void
1424 1.1 christos athn_usb_ampdu_tx_stop_cb(struct athn_usb_softc *usc, void *arg)
1425 1.1 christos {
1426 1.1 christos struct athn_usb_aggr_cmd *cmd = arg;
1427 1.1 christos struct ar_htc_target_aggr aggr;
1428 1.1 christos
1429 1.1 christos DPRINTFN(DBG_FN, usc, "\n");
1430 1.1 christos
1431 1.1 christos memset(&aggr, 0, sizeof(aggr));
1432 1.1 christos aggr.sta_index = cmd->sta_index;
1433 1.1 christos aggr.tidno = cmd->tid;
1434 1.1 christos aggr.aggr_enable = 0;
1435 1.1 christos (void)athn_usb_wmi_xcmd(usc, AR_WMI_CMD_TX_AGGR_ENABLE,
1436 1.1 christos &aggr, sizeof(aggr), NULL);
1437 1.1 christos }
1438 1.1 christos #endif /* notyet */
1439 1.1 christos
1440 1.1 christos Static int
1441 1.1 christos athn_usb_remove_hw_node(struct athn_usb_softc *usc, uint8_t *sta_idx)
1442 1.1 christos {
1443 1.1 christos int error;
1444 1.1 christos
1445 1.1 christos DPRINTFN(DBG_FN, usc, "\n");
1446 1.1 christos
1447 1.1 christos error = athn_usb_wmi_xcmd(usc, AR_WMI_CMD_NODE_REMOVE,
1448 1.1 christos sta_idx, sizeof(*sta_idx), NULL);
1449 1.1 christos
1450 1.1 christos DPRINTFN(DBG_NODES, usc, "node=%u error=%d\n",
1451 1.1 christos *sta_idx, error);
1452 1.1 christos return error;
1453 1.1 christos }
1454 1.1 christos
1455 1.1 christos Static int
1456 1.1 christos athn_usb_create_hw_node(struct athn_usb_softc *usc,
1457 1.1 christos struct ar_htc_target_sta *sta)
1458 1.1 christos {
1459 1.1 christos int error;
1460 1.1 christos
1461 1.1 christos DPRINTFN(DBG_FN, usc, "\n");
1462 1.1 christos
1463 1.1 christos error = athn_usb_wmi_xcmd(usc, AR_WMI_CMD_NODE_CREATE,
1464 1.1 christos sta, sizeof(*sta), NULL);
1465 1.1 christos
1466 1.1 christos DPRINTFN(DBG_NODES, usc, "node=%u error=%d\n",
1467 1.1 christos sta->sta_index, error);
1468 1.1 christos
1469 1.1 christos return error;
1470 1.1 christos }
1471 1.1 christos
1472 1.1 christos Static int
1473 1.1 christos athn_usb_create_node(struct athn_usb_softc *usc, struct ieee80211_node *ni)
1474 1.1 christos {
1475 1.1 christos struct athn_node *an = ATHN_NODE(ni);
1476 1.1 christos struct ar_htc_target_sta sta;
1477 1.1 christos struct ar_htc_target_rate rate;
1478 1.1 christos int error;
1479 1.1 christos
1480 1.1 christos DPRINTFN(DBG_FN | DBG_NODES, usc, "AID=0x%x\n", ni->ni_associd);
1481 1.1 christos
1482 1.1 christos /*
1483 1.1 christos * NB: this is called by ic_newstate and (in HOSTAP mode by)
1484 1.1 christos * ic_newassoc.
1485 1.1 christos *
1486 1.1 christos * The firmware has a limit of 8 nodes. In HOSTAP mode, we
1487 1.1 christos * limit the AID to < 8 and use that value to index the
1488 1.1 christos * firmware node table. Node zero is used for the BSS.
1489 1.1 christos *
1490 1.1 christos * In STA mode, we simply use node 1 for the BSS.
1491 1.1 christos */
1492 1.1 christos if (ATHN_SOFTC(usc)->sc_ic.ic_opmode == IEEE80211_M_HOSTAP)
1493 1.1 christos an->sta_index = IEEE80211_NODE_AID(ni);
1494 1.1 christos else
1495 1.1 christos an->sta_index = 1;
1496 1.1 christos
1497 1.1 christos /* Create node entry on target. */
1498 1.1 christos memset(&sta, 0, sizeof(sta));
1499 1.1 christos IEEE80211_ADDR_COPY(sta.macaddr, ni->ni_macaddr);
1500 1.1 christos IEEE80211_ADDR_COPY(sta.bssid, ni->ni_bssid);
1501 1.1 christos
1502 1.1 christos sta.associd = htobe16(ni->ni_associd);
1503 1.1 christos sta.valid = 1;
1504 1.1 christos sta.sta_index = an->sta_index;
1505 1.1 christos
1506 1.1 christos sta.maxampdu = 0xffff;
1507 1.1 christos #ifndef IEEE80211_NO_HT
1508 1.1 christos if (ni->ni_flags & IEEE80211_NODE_HT)
1509 1.1 christos sta.flags |= htobe16(AR_HTC_STA_HT);
1510 1.1 christos #endif
1511 1.1 christos error = athn_usb_create_hw_node(usc, &sta);
1512 1.1 christos if (error)
1513 1.1 christos return error;
1514 1.1 christos
1515 1.1 christos /* Setup supported rates. */
1516 1.1 christos memset(&rate, 0, sizeof(rate));
1517 1.1 christos rate.sta_index = sta.sta_index;
1518 1.1 christos rate.isnew = 1;
1519 1.1 christos rate.lg_rates.rs_nrates = ni->ni_rates.rs_nrates;
1520 1.1 christos memcpy(rate.lg_rates.rs_rates, ni->ni_rates.rs_rates,
1521 1.1 christos ni->ni_rates.rs_nrates);
1522 1.1 christos
1523 1.1 christos #ifndef IEEE80211_NO_HT
1524 1.1 christos if (ni->ni_flags & IEEE80211_NODE_HT) {
1525 1.1 christos rate.capflags |= htobe32(AR_RC_HT_FLAG);
1526 1.1 christos #ifdef notyet
1527 1.1 christos /* XXX setup HT rates */
1528 1.1 christos if (ni->ni_htcaps & IEEE80211_HTCAP_CBW20_40)
1529 1.1 christos rate.capflags |= htobe32(AR_RC_40_FLAG);
1530 1.1 christos if (ni->ni_htcaps & IEEE80211_HTCAP_SGI40)
1531 1.1 christos rate.capflags |= htobe32(AR_RC_SGI_FLAG);
1532 1.1 christos if (ni->ni_htcaps & IEEE80211_HTCAP_SGI20)
1533 1.1 christos rate.capflags |= htobe32(AR_RC_SGI_FLAG);
1534 1.1 christos #endif
1535 1.1 christos }
1536 1.1 christos #endif
1537 1.1 christos error = athn_usb_wmi_xcmd(usc, AR_WMI_CMD_RC_RATE_UPDATE,
1538 1.1 christos &rate, sizeof(rate), NULL);
1539 1.1 christos return error;
1540 1.1 christos }
1541 1.1 christos
1542 1.1 christos Static void
1543 1.1 christos athn_usb_rx_enable(struct athn_softc *sc)
1544 1.1 christos {
1545 1.1 christos
1546 1.1 christos DPRINTFN(DBG_FN, sc, "\n");
1547 1.1 christos
1548 1.1 christos AR_WRITE(sc, AR_CR, AR_CR_RXE);
1549 1.1 christos AR_WRITE_BARRIER(sc);
1550 1.1 christos }
1551 1.1 christos
1552 1.1 christos Static int
1553 1.1 christos athn_usb_switch_chan(struct athn_softc *sc, struct ieee80211_channel *curchan,
1554 1.1 christos struct ieee80211_channel *extchan)
1555 1.1 christos {
1556 1.1 christos struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
1557 1.1 christos uint16_t mode;
1558 1.1 christos int error;
1559 1.1 christos
1560 1.1 christos DPRINTFN(DBG_FN, sc, "\n");
1561 1.1 christos
1562 1.1 christos /* Disable interrupts. */
1563 1.1 christos error = athn_usb_wmi_cmd(usc, AR_WMI_CMD_DISABLE_INTR);
1564 1.1 christos if (error != 0)
1565 1.1 christos goto reset;
1566 1.1 christos /* Stop all Tx queues. */
1567 1.1 christos error = athn_usb_wmi_cmd(usc, AR_WMI_CMD_DRAIN_TXQ_ALL);
1568 1.1 christos if (error != 0)
1569 1.1 christos goto reset;
1570 1.1 christos /* Stop Rx. */
1571 1.1 christos error = athn_usb_wmi_cmd(usc, AR_WMI_CMD_STOP_RECV);
1572 1.1 christos if (error != 0)
1573 1.1 christos goto reset;
1574 1.1 christos
1575 1.1 christos /* If band or bandwidth changes, we need to do a full reset. */
1576 1.1 christos if (curchan->ic_flags != sc->sc_curchan->ic_flags ||
1577 1.1 christos ((extchan != NULL) ^ (sc->sc_curchanext != NULL))) {
1578 1.1 christos DPRINTFN(DBG_RF, sc, "channel band switch\n");
1579 1.1 christos goto reset;
1580 1.1 christos }
1581 1.1 christos
1582 1.1 christos error = athn_set_chan(sc, curchan, extchan);
1583 1.1 christos if (AR_SREV_9271(sc) && error == 0)
1584 1.1 christos ar9271_load_ani(sc);
1585 1.1 christos if (error != 0) {
1586 1.1 christos reset: /* Error found, try a full reset. */
1587 1.1 christos DPRINTFN(DBG_RF, sc, "needs a full reset\n");
1588 1.1 christos error = athn_hw_reset(sc, curchan, extchan, 0);
1589 1.1 christos if (error != 0) /* Hopeless case. */
1590 1.1 christos return error;
1591 1.1 christos }
1592 1.1 christos
1593 1.1 christos error = athn_usb_wmi_cmd(usc, AR_WMI_CMD_START_RECV);
1594 1.1 christos if (error != 0)
1595 1.1 christos return error;
1596 1.1 christos athn_rx_start(sc);
1597 1.1 christos
1598 1.1 christos mode = htobe16(IEEE80211_IS_CHAN_2GHZ(curchan) ?
1599 1.1 christos AR_HTC_MODE_11NG : AR_HTC_MODE_11NA);
1600 1.1 christos error = athn_usb_wmi_xcmd(usc, AR_WMI_CMD_SET_MODE,
1601 1.1 christos &mode, sizeof(mode), NULL);
1602 1.1 christos if (error != 0)
1603 1.1 christos return error;
1604 1.1 christos
1605 1.1 christos /* Re-enable interrupts. */
1606 1.1 christos error = athn_usb_wmi_cmd(usc, AR_WMI_CMD_ENABLE_INTR);
1607 1.1 christos return error;
1608 1.1 christos }
1609 1.1 christos
1610 1.1 christos #ifdef notyet_edca
1611 1.1 christos Static void
1612 1.1 christos athn_usb_updateedca(struct ieee80211com *ic)
1613 1.1 christos {
1614 1.1 christos struct athn_softc *sc = ic->ic_ifp->if_softc;
1615 1.1 christos struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
1616 1.1 christos
1617 1.1 christos DPRINTFN(DBG_FN, sc, "\n");
1618 1.1 christos
1619 1.1 christos /* Do it in a process context. */
1620 1.1 christos athn_usb_do_async(usc, athn_usb_updateedca_cb, NULL, 0);
1621 1.1 christos }
1622 1.1 christos
1623 1.1 christos Static void
1624 1.1 christos athn_usb_updateedca_cb(struct athn_usb_softc *usc, void *arg)
1625 1.1 christos {
1626 1.1 christos int s;
1627 1.1 christos
1628 1.1 christos DPRINTFN(DBG_FN, usc, "\n");
1629 1.1 christos
1630 1.1 christos s = splnet();
1631 1.1 christos athn_updateedca(&usc->usc_sc.sc_ic);
1632 1.1 christos splx(s);
1633 1.1 christos }
1634 1.1 christos #endif /* notyet_edca */
1635 1.1 christos
1636 1.1 christos Static void
1637 1.1 christos athn_usb_updateslot(struct ifnet *ifp)
1638 1.1 christos {
1639 1.1 christos struct athn_softc *sc = ifp->if_softc;
1640 1.1 christos struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
1641 1.1 christos
1642 1.1 christos DPRINTFN(DBG_FN, sc, "\n");
1643 1.1 christos
1644 1.1 christos /*
1645 1.1 christos * NB: athn_updateslog() needs to be done in a process context
1646 1.1 christos * to avoid being called by ieee80211_reset_erp() inside a
1647 1.1 christos * spinlock held by ieee80211_free_allnodes().
1648 1.1 christos *
1649 1.1 christos * XXX: calling this during the athn_attach() causes
1650 1.1 christos * usb_insert_transfer() to produce a bunch of "not busy"
1651 1.1 christos * messages. Why?
1652 1.1 christos */
1653 1.1 christos if (usc->usc_athn_attached)
1654 1.1 christos athn_usb_do_async(usc, athn_usb_updateslot_cb, NULL, 0);
1655 1.1 christos }
1656 1.1 christos
1657 1.1 christos Static void
1658 1.1 christos athn_usb_updateslot_cb(struct athn_usb_softc *usc, void *arg)
1659 1.1 christos {
1660 1.1 christos int s;
1661 1.1 christos
1662 1.1 christos DPRINTFN(DBG_FN, usc, "\n");
1663 1.1 christos
1664 1.1 christos s = splnet();
1665 1.1 christos athn_updateslot(&usc->usc_sc.sc_if);
1666 1.1 christos splx(s);
1667 1.1 christos }
1668 1.1 christos
1669 1.1 christos #ifdef notyet
1670 1.1 christos Static int
1671 1.1 christos athn_usb_set_key(struct ieee80211com *ic, struct ieee80211_node *ni,
1672 1.1 christos struct ieee80211_key *k)
1673 1.1 christos {
1674 1.1 christos struct athn_softc *sc = ic->ic_ifp->if_softc;
1675 1.1 christos struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
1676 1.1 christos struct ifnet *ifp = &usc->usc_sc.sc_if;
1677 1.1 christos struct athn_usb_cmd_key cmd;
1678 1.1 christos
1679 1.1 christos DPRINTFN(DBG_FN, sc, "\n");
1680 1.1 christos
1681 1.1 christos /* Defer setting of WEP keys until interface is brought up. */
1682 1.1 christos if (!IS_UP_AND_RUNNING(ifp))
1683 1.1 christos return 0;
1684 1.1 christos
1685 1.1 christos /* Do it in a process context. */
1686 1.1 christos cmd.ni = (ni != NULL) ? ieee80211_ref_node(ni) : NULL;
1687 1.1 christos cmd.key = k;
1688 1.1 christos athn_usb_do_async(usc, athn_usb_set_key_cb, &cmd, sizeof(cmd));
1689 1.1 christos return 0;
1690 1.1 christos }
1691 1.1 christos
1692 1.1 christos Static void
1693 1.1 christos athn_usb_set_key_cb(struct athn_usb_softc *usc, void *arg)
1694 1.1 christos {
1695 1.1 christos struct ieee80211com *ic = &usc->usc_sc.sc_ic;
1696 1.1 christos struct athn_usb_cmd_key *cmd = arg;
1697 1.1 christos int s;
1698 1.1 christos
1699 1.1 christos DPRINTFN(DBG_FN, usc, "\n");
1700 1.1 christos
1701 1.1 christos s = splnet();
1702 1.1 christos athn_set_key(ic, cmd->ni, cmd->key);
1703 1.1 christos if (cmd->ni != NULL)
1704 1.1 christos ieee80211_free_node(cmd->ni);
1705 1.1 christos splx(s);
1706 1.1 christos }
1707 1.1 christos
1708 1.1 christos Static void
1709 1.1 christos athn_usb_delete_key(struct ieee80211com *ic, struct ieee80211_node *ni,
1710 1.1 christos struct ieee80211_key *k)
1711 1.1 christos {
1712 1.1 christos struct athn_softc *sc = ic->ic_ifp->if_softc;
1713 1.1 christos struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
1714 1.1 christos struct ifnet *ifp = &usc->usc_sc.sc_if;
1715 1.1 christos struct athn_usb_cmd_key cmd;
1716 1.1 christos
1717 1.1 christos DPRINTFN(DBG_FN, sc, "\n");
1718 1.1 christos
1719 1.1 christos if (!(ifp->if_flags & IFF_RUNNING) ||
1720 1.1 christos ic->ic_state != IEEE80211_S_RUN)
1721 1.1 christos return; /* Nothing to do. */
1722 1.1 christos
1723 1.1 christos /* Do it in a process context. */
1724 1.1 christos cmd.ni = (ni != NULL) ? ieee80211_ref_node(ni) : NULL;
1725 1.1 christos cmd.key = k;
1726 1.1 christos athn_usb_do_async(usc, athn_usb_delete_key_cb, &cmd, sizeof(cmd));
1727 1.1 christos }
1728 1.1 christos
1729 1.1 christos Static void
1730 1.1 christos athn_usb_delete_key_cb(struct athn_usb_softc *usc, void *arg)
1731 1.1 christos {
1732 1.1 christos struct ieee80211com *ic = &usc->usc_sc.sc_ic;
1733 1.1 christos struct athn_usb_cmd_key *cmd = arg;
1734 1.1 christos int s;
1735 1.1 christos
1736 1.1 christos DPRINTFN(DBG_FN, usc, "\n");
1737 1.1 christos
1738 1.1 christos s = splnet();
1739 1.1 christos athn_delete_key(ic, cmd->ni, cmd->key);
1740 1.1 christos if (cmd->ni != NULL)
1741 1.1 christos ieee80211_free_node(cmd->ni);
1742 1.1 christos splx(s);
1743 1.1 christos }
1744 1.1 christos #endif /* notyet */
1745 1.1 christos
1746 1.1 christos #ifndef IEEE80211_STA_ONLY
1747 1.1 christos Static void
1748 1.1 christos athn_usb_bcneof(usbd_xfer_handle xfer, usbd_private_handle priv,
1749 1.1 christos usbd_status status)
1750 1.1 christos {
1751 1.1 christos struct athn_usb_tx_data *data = priv;
1752 1.1 christos struct athn_usb_softc *usc = data->sc;
1753 1.1 christos
1754 1.1 christos DPRINTFN(DBG_FN, usc, "\n");
1755 1.1 christos
1756 1.1 christos if (__predict_false(status == USBD_STALLED))
1757 1.1 christos usbd_clear_endpoint_stall_async(usc->usc_tx_data_pipe);
1758 1.1 christos usc->usc_tx_bcn = data;
1759 1.1 christos }
1760 1.1 christos
1761 1.1 christos /*
1762 1.1 christos * Process Software Beacon Alert interrupts.
1763 1.1 christos */
1764 1.1 christos Static void
1765 1.1 christos athn_usb_swba(struct athn_usb_softc *usc)
1766 1.1 christos {
1767 1.1 christos struct athn_softc *sc = &usc->usc_sc;
1768 1.1 christos struct ieee80211com *ic = &sc->sc_ic;
1769 1.1 christos struct athn_usb_tx_data *data;
1770 1.1 christos struct ieee80211_frame *wh;
1771 1.1 christos struct ieee80211_beacon_offsets bo;
1772 1.1 christos struct ar_stream_hdr *hdr;
1773 1.1 christos struct ar_htc_frame_hdr *htc;
1774 1.1 christos struct ar_tx_bcn *bcn;
1775 1.1 christos struct mbuf *m;
1776 1.1 christos int error;
1777 1.1 christos
1778 1.1 christos if (usc->usc_dying)
1779 1.1 christos return;
1780 1.1 christos
1781 1.1 christos DPRINTFN(DBG_FN, sc, "\n");
1782 1.1 christos
1783 1.1 christos if (ic->ic_dtim_count == 0)
1784 1.1 christos ic->ic_dtim_count = ic->ic_dtim_period - 1;
1785 1.1 christos else
1786 1.1 christos ic->ic_dtim_count--;
1787 1.1 christos
1788 1.1 christos /* Make sure previous beacon has been sent. */
1789 1.1 christos if (usc->usc_tx_bcn == NULL)
1790 1.1 christos return;
1791 1.1 christos data = usc->usc_tx_bcn;
1792 1.1 christos
1793 1.1 christos /* Get new beacon. */
1794 1.1 christos #ifdef ATHN_DEBUG
1795 1.1 christos memset(&bo, 0, sizeof(bo));
1796 1.1 christos #endif
1797 1.1 christos m = ieee80211_beacon_alloc(ic, ic->ic_bss, &bo);
1798 1.1 christos if (__predict_false(m == NULL))
1799 1.1 christos return;
1800 1.1 christos /* Assign sequence number. */
1801 1.1 christos /* XXX: use non-QoS tid? */
1802 1.1 christos wh = mtod(m, struct ieee80211_frame *);
1803 1.1 christos *(uint16_t *)&wh->i_seq[0] =
1804 1.1 christos htole16(ic->ic_bss->ni_txseqs[0] << IEEE80211_SEQ_SEQ_SHIFT);
1805 1.1 christos ic->ic_bss->ni_txseqs[0]++;
1806 1.1 christos
1807 1.1 christos hdr = (struct ar_stream_hdr *)data->buf;
1808 1.1 christos hdr->tag = htole16(AR_USB_TX_STREAM_TAG);
1809 1.1 christos hdr->len = htole16(sizeof(*htc) + sizeof(*bcn) + m->m_pkthdr.len);
1810 1.1 christos
1811 1.1 christos htc = (struct ar_htc_frame_hdr *)&hdr[1];
1812 1.1 christos memset(htc, 0, sizeof(*htc));
1813 1.1 christos htc->endpoint_id = usc->usc_ep_bcn;
1814 1.1 christos htc->payload_len = htobe16(sizeof(*bcn) + m->m_pkthdr.len);
1815 1.1 christos
1816 1.1 christos bcn = (struct ar_tx_bcn *)&htc[1];
1817 1.1 christos memset(bcn, 0, sizeof(*bcn));
1818 1.1 christos bcn->vif_idx = 0;
1819 1.1 christos
1820 1.1 christos m_copydata(m, 0, m->m_pkthdr.len, (void *)&bcn[1]);
1821 1.1 christos
1822 1.1 christos usbd_setup_xfer(data->xfer, usc->usc_tx_data_pipe, data, data->buf,
1823 1.1 christos sizeof(*hdr) + sizeof(*htc) + sizeof(*bcn) + m->m_pkthdr.len,
1824 1.1 christos USBD_SHORT_XFER_OK | USBD_NO_COPY, ATHN_USB_TX_TIMEOUT,
1825 1.1 christos athn_usb_bcneof);
1826 1.1 christos
1827 1.1 christos m_freem(m);
1828 1.1 christos usc->usc_tx_bcn = NULL;
1829 1.1 christos error = usbd_transfer(data->xfer);
1830 1.1 christos if (__predict_false(error != USBD_IN_PROGRESS && error != 0))
1831 1.1 christos usc->usc_tx_bcn = data;
1832 1.1 christos }
1833 1.1 christos #endif
1834 1.1 christos
1835 1.1 christos Static void
1836 1.3 christos athn_usb_rx_wmi_ctrl(struct athn_usb_softc *usc, uint8_t *buf, size_t len)
1837 1.1 christos {
1838 1.1 christos #ifdef ATHN_DEBUG
1839 1.1 christos struct ar_wmi_evt_txrate *txrate;
1840 1.1 christos #endif
1841 1.1 christos struct ar_wmi_cmd_hdr *wmi;
1842 1.1 christos uint16_t cmd_id;
1843 1.1 christos
1844 1.1 christos if (usc->usc_dying)
1845 1.1 christos return;
1846 1.1 christos
1847 1.1 christos DPRINTFN(DBG_FN, usc, "\n");
1848 1.1 christos
1849 1.3 christos if (__predict_false(len < sizeof(*wmi)))
1850 1.1 christos return;
1851 1.1 christos wmi = (struct ar_wmi_cmd_hdr *)buf;
1852 1.1 christos cmd_id = be16toh(wmi->cmd_id);
1853 1.1 christos
1854 1.1 christos if (!(cmd_id & AR_WMI_EVT_FLAG)) {
1855 1.1 christos if (usc->usc_wait_cmd_id != cmd_id)
1856 1.1 christos return; /* Unexpected reply. */
1857 1.1 christos if (usc->usc_obuf != NULL) {
1858 1.1 christos /* Copy answer into caller supplied buffer. */
1859 1.1 christos memcpy(usc->usc_obuf, &wmi[1], len - sizeof(*wmi));
1860 1.1 christos }
1861 1.1 christos /* Notify caller of completion. */
1862 1.1 christos usc->usc_wait_cmd_id = 0;
1863 1.1 christos wakeup(&usc->usc_wait_cmd_id);
1864 1.1 christos return;
1865 1.1 christos }
1866 1.1 christos /*
1867 1.1 christos * XXX: the Linux 2.6 and 3.7.4 kernels differ on the event numbers!
1868 1.1 christos * See the alternate defines in if_athn_usb.h.
1869 1.1 christos */
1870 1.1 christos switch (cmd_id & 0xfff) {
1871 1.1 christos #ifndef IEEE80211_STA_ONLY
1872 1.1 christos case AR_WMI_EVT_SWBA:
1873 1.1 christos athn_usb_swba(usc);
1874 1.1 christos break;
1875 1.1 christos #endif
1876 1.1 christos case AR_WMI_EVT_FATAL:
1877 1.1 christos aprint_error_dev(usc->usc_dev, "fatal firmware error\n");
1878 1.1 christos break;
1879 1.1 christos case AR_WMI_EVT_TXRATE:
1880 1.1 christos #ifdef ATHN_DEBUG
1881 1.1 christos txrate = (struct ar_wmi_evt_txrate *)&wmi[1];
1882 1.1 christos DPRINTFN(DBG_TX, usc, "txrate=%d\n", be32toh(txrate->txrate));
1883 1.1 christos #endif
1884 1.1 christos break;
1885 1.1 christos default:
1886 1.1 christos DPRINTFN(DBG_TX, usc, "WMI event 0x%x (%d) ignored\n", cmd_id, cmd_id);
1887 1.1 christos break;
1888 1.1 christos }
1889 1.1 christos }
1890 1.1 christos
1891 1.1 christos Static void
1892 1.1 christos athn_usb_intr(usbd_xfer_handle xfer, usbd_private_handle priv,
1893 1.1 christos usbd_status status)
1894 1.1 christos {
1895 1.1 christos struct athn_usb_softc *usc = priv;
1896 1.1 christos struct ar_htc_frame_hdr *htc;
1897 1.1 christos struct ar_htc_msg_hdr *msg;
1898 1.1 christos uint8_t *buf = usc->usc_ibuf;
1899 1.1 christos uint16_t msg_id;
1900 1.1 christos int len;
1901 1.1 christos
1902 1.1 christos if (usc->usc_dying)
1903 1.1 christos return;
1904 1.1 christos
1905 1.1 christos DPRINTFN(DBG_FN, usc, "\n");
1906 1.1 christos
1907 1.1 christos if (__predict_false(status != USBD_NORMAL_COMPLETION)) {
1908 1.1 christos DPRINTFN(DBG_INTR, usc, "intr status=%d\n", status);
1909 1.1 christos if (status == USBD_STALLED)
1910 1.1 christos usbd_clear_endpoint_stall_async(usc->usc_rx_intr_pipe);
1911 1.1 christos return;
1912 1.1 christos }
1913 1.1 christos usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
1914 1.1 christos
1915 1.1 christos /* Skip watchdog pattern if present. */
1916 1.1 christos if (len >= 4 && *(uint32_t *)buf == htobe32(0x00c60000)) {
1917 1.1 christos buf += 4;
1918 1.1 christos len -= 4;
1919 1.1 christos }
1920 1.1 christos if (__predict_false(len < (int)sizeof(*htc)))
1921 1.1 christos return;
1922 1.1 christos htc = (struct ar_htc_frame_hdr *)buf;
1923 1.1 christos /* Skip HTC header. */
1924 1.1 christos buf += sizeof(*htc);
1925 1.1 christos len -= sizeof(*htc);
1926 1.1 christos
1927 1.1 christos if (htc->endpoint_id != 0) {
1928 1.1 christos if (__predict_false(htc->endpoint_id != usc->usc_ep_ctrl))
1929 1.1 christos return;
1930 1.1 christos /* Remove trailer if present. */
1931 1.1 christos if (htc->flags & AR_HTC_FLAG_TRAILER) {
1932 1.1 christos if (__predict_false(len < htc->control[0]))
1933 1.1 christos return;
1934 1.1 christos len -= htc->control[0];
1935 1.1 christos }
1936 1.1 christos athn_usb_rx_wmi_ctrl(usc, buf, len);
1937 1.1 christos return;
1938 1.1 christos }
1939 1.1 christos
1940 1.1 christos /*
1941 1.1 christos * Endpoint 0 carries HTC messages.
1942 1.1 christos */
1943 1.1 christos if (__predict_false(len < (int)sizeof(*msg)))
1944 1.1 christos return;
1945 1.1 christos msg = (struct ar_htc_msg_hdr *)buf;
1946 1.1 christos msg_id = be16toh(msg->msg_id);
1947 1.1 christos DPRINTFN(DBG_RX, usc, "Rx HTC message %d\n", msg_id);
1948 1.1 christos switch (msg_id) {
1949 1.1 christos case AR_HTC_MSG_READY:
1950 1.1 christos case AR_HTC_MSG_CONF_PIPE_RSP:
1951 1.1 christos if (usc->usc_wait_msg_id != msg_id)
1952 1.1 christos break;
1953 1.1 christos usc->usc_wait_msg_id = 0;
1954 1.1 christos wakeup(&usc->usc_wait_msg_id);
1955 1.1 christos break;
1956 1.1 christos case AR_HTC_MSG_CONN_SVC_RSP:
1957 1.1 christos if (usc->usc_wait_msg_id != msg_id)
1958 1.1 christos break;
1959 1.1 christos if (usc->usc_msg_conn_svc_rsp != NULL) {
1960 1.1 christos memcpy(usc->usc_msg_conn_svc_rsp, &msg[1],
1961 1.1 christos sizeof(*usc->usc_msg_conn_svc_rsp));
1962 1.1 christos }
1963 1.1 christos usc->usc_wait_msg_id = 0;
1964 1.1 christos wakeup(&usc->usc_wait_msg_id);
1965 1.1 christos break;
1966 1.1 christos default:
1967 1.1 christos DPRINTFN(DBG_RX, usc, "HTC message %d ignored\n", msg_id);
1968 1.1 christos break;
1969 1.1 christos }
1970 1.1 christos }
1971 1.1 christos
1972 1.1 christos Static void
1973 1.1 christos athn_usb_rx_radiotap(struct athn_softc *sc, struct mbuf *m,
1974 1.1 christos struct ar_rx_status *rs)
1975 1.1 christos {
1976 1.1 christos struct athn_rx_radiotap_header *tap = &sc->sc_rxtap;
1977 1.1 christos struct ieee80211com *ic = &sc->sc_ic;
1978 1.1 christos uint8_t rate;
1979 1.1 christos
1980 1.1 christos DPRINTFN(DBG_FN, sc, "\n");
1981 1.1 christos
1982 1.1 christos tap->wr_flags = IEEE80211_RADIOTAP_F_FCS;
1983 1.1 christos tap->wr_tsft = htole64(be64toh(rs->rs_tstamp));
1984 1.1 christos tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
1985 1.1 christos tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
1986 1.1 christos tap->wr_dbm_antsignal = rs->rs_rssi;
1987 1.1 christos /* XXX noise. */
1988 1.1 christos tap->wr_antenna = rs->rs_antenna;
1989 1.1 christos rate = rs->rs_rate;
1990 1.1 christos if (rate & 0x80) { /* HT. */
1991 1.1 christos /* Bit 7 set means HT MCS instead of rate. */
1992 1.1 christos tap->wr_rate = rate;
1993 1.1 christos if (!(rs->rs_flags & AR_RXS_FLAG_GI))
1994 1.1 christos tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
1995 1.1 christos }
1996 1.1 christos else if (rate & 0x10) { /* CCK. */
1997 1.1 christos if (rate & 0x04)
1998 1.1 christos tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
1999 1.1 christos switch (rate & ~0x14) {
2000 1.1 christos case 0xb: tap->wr_rate = 2; break;
2001 1.1 christos case 0xa: tap->wr_rate = 4; break;
2002 1.1 christos case 0x9: tap->wr_rate = 11; break;
2003 1.1 christos case 0x8: tap->wr_rate = 22; break;
2004 1.1 christos default: tap->wr_rate = 0; break;
2005 1.1 christos }
2006 1.1 christos }
2007 1.1 christos else { /* OFDM. */
2008 1.1 christos switch (rate) {
2009 1.1 christos case 0xb: tap->wr_rate = 12; break;
2010 1.1 christos case 0xf: tap->wr_rate = 18; break;
2011 1.1 christos case 0xa: tap->wr_rate = 24; break;
2012 1.1 christos case 0xe: tap->wr_rate = 36; break;
2013 1.1 christos case 0x9: tap->wr_rate = 48; break;
2014 1.1 christos case 0xd: tap->wr_rate = 72; break;
2015 1.1 christos case 0x8: tap->wr_rate = 96; break;
2016 1.1 christos case 0xc: tap->wr_rate = 108; break;
2017 1.1 christos default: tap->wr_rate = 0; break;
2018 1.1 christos }
2019 1.1 christos }
2020 1.1 christos bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m);
2021 1.1 christos }
2022 1.1 christos
2023 1.1 christos Static void
2024 1.1 christos athn_usb_rx_frame(struct athn_usb_softc *usc, struct mbuf *m)
2025 1.1 christos {
2026 1.1 christos struct athn_softc *sc = &usc->usc_sc;
2027 1.1 christos struct ieee80211com *ic = &sc->sc_ic;
2028 1.1 christos struct ifnet *ifp = &sc->sc_if;
2029 1.1 christos struct ieee80211_frame *wh;
2030 1.1 christos struct ieee80211_node *ni;
2031 1.1 christos struct ar_htc_frame_hdr *htc;
2032 1.1 christos struct ar_rx_status *rs;
2033 1.1 christos uint16_t datalen;
2034 1.1 christos int s;
2035 1.1 christos
2036 1.1 christos DPRINTFN(DBG_FN, sc, "\n");
2037 1.1 christos
2038 1.1 christos if (__predict_false(m->m_len < (int)sizeof(*htc)))
2039 1.1 christos goto skip;
2040 1.1 christos htc = mtod(m, struct ar_htc_frame_hdr *);
2041 1.1 christos if (__predict_false(htc->endpoint_id == 0)) {
2042 1.1 christos DPRINTFN(DBG_RX, sc, "bad endpoint %d\n", htc->endpoint_id);
2043 1.1 christos goto skip;
2044 1.1 christos }
2045 1.1 christos if (htc->flags & AR_HTC_FLAG_TRAILER) {
2046 1.1 christos if (m->m_len < htc->control[0])
2047 1.1 christos goto skip;
2048 1.1 christos m_adj(m, -(int)htc->control[0]);
2049 1.1 christos }
2050 1.1 christos m_adj(m, sizeof(*htc)); /* Strip HTC header. */
2051 1.1 christos
2052 1.1 christos if (__predict_false(m->m_len < (int)sizeof(*rs)))
2053 1.1 christos goto skip;
2054 1.1 christos rs = mtod(m, struct ar_rx_status *);
2055 1.1 christos
2056 1.1 christos /* Make sure that payload fits. */
2057 1.1 christos datalen = be16toh(rs->rs_datalen);
2058 1.1 christos if (__predict_false(m->m_len < (int)sizeof(*rs) + datalen))
2059 1.1 christos goto skip;
2060 1.1 christos
2061 1.1 christos /* Ignore runt frames. Let ACKs be seen by bpf */
2062 1.1 christos if (__predict_false(datalen <
2063 1.1 christos sizeof(struct ieee80211_frame_ack) + IEEE80211_CRC_LEN))
2064 1.1 christos goto skip;
2065 1.1 christos
2066 1.1 christos m_adj(m, sizeof(*rs)); /* Strip Rx status. */
2067 1.1 christos m->m_pkthdr.rcvif = ifp;
2068 1.1 christos
2069 1.1 christos s = splnet();
2070 1.1 christos
2071 1.1 christos /* Grab a reference to the source node. */
2072 1.1 christos wh = mtod(m, struct ieee80211_frame *);
2073 1.1 christos ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
2074 1.1 christos
2075 1.1 christos /* Remove any HW padding after the 802.11 header. */
2076 1.1 christos if (!(wh->i_fc[0] & IEEE80211_FC0_TYPE_CTL)) {
2077 1.1 christos u_int hdrlen = ieee80211_anyhdrsize(wh);
2078 1.1 christos if (hdrlen & 3) {
2079 1.1 christos ovbcopy(wh, (uint8_t *)wh + 2, hdrlen);
2080 1.1 christos m_adj(m, 2);
2081 1.1 christos }
2082 1.1 christos }
2083 1.1 christos if (__predict_false(sc->sc_drvbpf != NULL))
2084 1.1 christos athn_usb_rx_radiotap(sc, m, rs);
2085 1.1 christos
2086 1.1 christos /* Trim 802.11 FCS after radiotap. */
2087 1.1 christos m_adj(m, -IEEE80211_CRC_LEN);
2088 1.1 christos
2089 1.1 christos /* Send the frame to the 802.11 layer. */
2090 1.1 christos ieee80211_input(ic, m, ni, rs->rs_rssi + AR_USB_DEFAULT_NF, 0);
2091 1.1 christos
2092 1.1 christos /* Node is no longer needed. */
2093 1.1 christos ieee80211_free_node(ni);
2094 1.1 christos splx(s);
2095 1.1 christos return;
2096 1.1 christos skip:
2097 1.1 christos m_freem(m);
2098 1.1 christos }
2099 1.1 christos
2100 1.1 christos Static void
2101 1.1 christos athn_usb_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv,
2102 1.1 christos usbd_status status)
2103 1.1 christos {
2104 1.1 christos struct athn_usb_rx_data *data = priv;
2105 1.1 christos struct athn_usb_softc *usc = data->sc;
2106 1.1 christos struct athn_usb_rx_stream *stream = &usc->usc_rx_stream;
2107 1.1 christos uint8_t *buf = data->buf;
2108 1.1 christos struct ar_stream_hdr *hdr;
2109 1.1 christos struct mbuf *m;
2110 1.1 christos uint16_t pktlen;
2111 1.1 christos int off, len;
2112 1.1 christos
2113 1.1 christos if (usc->usc_dying)
2114 1.1 christos return;
2115 1.1 christos
2116 1.1 christos DPRINTFN(DBG_FN, usc, "\n");
2117 1.1 christos
2118 1.1 christos if (__predict_false(status != USBD_NORMAL_COMPLETION)) {
2119 1.1 christos DPRINTFN(DBG_RX, usc, "RX status=%d\n", status);
2120 1.1 christos if (status == USBD_STALLED)
2121 1.1 christos usbd_clear_endpoint_stall_async(usc->usc_rx_data_pipe);
2122 1.1 christos if (status != USBD_CANCELLED)
2123 1.1 christos goto resubmit;
2124 1.1 christos return;
2125 1.1 christos }
2126 1.1 christos usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
2127 1.1 christos
2128 1.1 christos if (stream->left > 0) {
2129 1.1 christos if (len >= stream->left) {
2130 1.1 christos /* We have all our pktlen bytes now. */
2131 1.1 christos if (__predict_true(stream->m != NULL)) {
2132 1.1 christos memcpy(mtod(stream->m, uint8_t *) +
2133 1.1 christos stream->moff, buf, stream->left);
2134 1.1 christos athn_usb_rx_frame(usc, stream->m);
2135 1.1 christos stream->m = NULL;
2136 1.1 christos }
2137 1.1 christos /* Next header is 32-bit aligned. */
2138 1.1 christos off = (stream->left + 3) & ~3;
2139 1.1 christos buf += off;
2140 1.1 christos len -= off;
2141 1.1 christos stream->left = 0;
2142 1.1 christos }
2143 1.1 christos else {
2144 1.1 christos /* Still need more bytes, save what we have. */
2145 1.1 christos if (__predict_true(stream->m != NULL)) {
2146 1.1 christos memcpy(mtod(stream->m, uint8_t *) +
2147 1.1 christos stream->moff, buf, len);
2148 1.1 christos stream->moff += len;
2149 1.1 christos }
2150 1.1 christos stream->left -= len;
2151 1.1 christos goto resubmit;
2152 1.1 christos }
2153 1.1 christos }
2154 1.1 christos KASSERT(stream->left == 0);
2155 1.1 christos while (len >= (int)sizeof(*hdr)) {
2156 1.1 christos hdr = (struct ar_stream_hdr *)buf;
2157 1.1 christos if (hdr->tag != htole16(AR_USB_RX_STREAM_TAG)) {
2158 1.1 christos DPRINTFN(DBG_RX, usc, "invalid tag 0x%x\n", hdr->tag);
2159 1.1 christos break;
2160 1.1 christos }
2161 1.1 christos pktlen = le16toh(hdr->len);
2162 1.1 christos buf += sizeof(*hdr);
2163 1.1 christos len -= sizeof(*hdr);
2164 1.1 christos
2165 1.1 christos if (__predict_true(pktlen <= MCLBYTES)) {
2166 1.1 christos /* Allocate an mbuf to store the next pktlen bytes. */
2167 1.1 christos MGETHDR(m, M_DONTWAIT, MT_DATA);
2168 1.1 christos if (__predict_true(m != NULL)) {
2169 1.1 christos m->m_pkthdr.len = m->m_len = pktlen;
2170 1.1 christos if (pktlen > MHLEN) {
2171 1.1 christos MCLGET(m, M_DONTWAIT);
2172 1.1 christos if (!(m->m_flags & M_EXT)) {
2173 1.1 christos m_free(m);
2174 1.1 christos m = NULL;
2175 1.1 christos }
2176 1.1 christos }
2177 1.1 christos }
2178 1.1 christos }
2179 1.1 christos else /* Drop frames larger than MCLBYTES. */
2180 1.1 christos m = NULL;
2181 1.1 christos /*
2182 1.1 christos * NB: m can be NULL, in which case the next pktlen bytes
2183 1.1 christos * will be discarded from the Rx stream.
2184 1.1 christos */
2185 1.1 christos if (pktlen > len) {
2186 1.1 christos /* Need more bytes, save what we have. */
2187 1.1 christos stream->m = m; /* NB: m can be NULL. */
2188 1.1 christos if (__predict_true(stream->m != NULL)) {
2189 1.1 christos memcpy(mtod(stream->m, uint8_t *), buf, len);
2190 1.1 christos stream->moff = len;
2191 1.1 christos }
2192 1.1 christos stream->left = pktlen - len;
2193 1.1 christos goto resubmit;
2194 1.1 christos }
2195 1.1 christos if (__predict_true(m != NULL)) {
2196 1.1 christos /* We have all the pktlen bytes in this xfer. */
2197 1.1 christos memcpy(mtod(m, uint8_t *), buf, pktlen);
2198 1.1 christos athn_usb_rx_frame(usc, m);
2199 1.1 christos }
2200 1.1 christos
2201 1.1 christos /* Next header is 32-bit aligned. */
2202 1.1 christos off = (pktlen + 3) & ~3;
2203 1.1 christos buf += off;
2204 1.1 christos len -= off;
2205 1.1 christos }
2206 1.1 christos
2207 1.1 christos resubmit:
2208 1.1 christos /* Setup a new transfer. */
2209 1.1 christos usbd_setup_xfer(xfer, usc->usc_rx_data_pipe, data, data->buf,
2210 1.1 christos ATHN_USB_RXBUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY,
2211 1.1 christos USBD_NO_TIMEOUT, athn_usb_rxeof);
2212 1.1 christos (void)usbd_transfer(xfer);
2213 1.1 christos }
2214 1.1 christos
2215 1.1 christos Static void
2216 1.1 christos athn_usb_txeof(usbd_xfer_handle xfer, usbd_private_handle priv,
2217 1.1 christos usbd_status status)
2218 1.1 christos {
2219 1.1 christos struct athn_usb_tx_data *data = priv;
2220 1.1 christos struct athn_usb_softc *usc = data->sc;
2221 1.1 christos struct athn_softc *sc = &usc->usc_sc;
2222 1.1 christos struct ifnet *ifp = &sc->sc_if;
2223 1.1 christos int s;
2224 1.1 christos
2225 1.1 christos if (usc->usc_dying)
2226 1.1 christos return;
2227 1.1 christos
2228 1.1 christos DPRINTFN(DBG_FN, usc, "\n");
2229 1.1 christos
2230 1.1 christos s = splnet();
2231 1.1 christos /* Put this Tx buffer back to our free list. */
2232 1.1 christos mutex_enter(&usc->usc_tx_mtx);
2233 1.1 christos TAILQ_INSERT_TAIL(&usc->usc_tx_free_list, data, next);
2234 1.1 christos mutex_exit(&usc->usc_tx_mtx);
2235 1.1 christos
2236 1.1 christos if (__predict_false(status != USBD_NORMAL_COMPLETION)) {
2237 1.1 christos DPRINTFN(DBG_TX, sc, "TX status=%d\n", status);
2238 1.1 christos if (status == USBD_STALLED)
2239 1.1 christos usbd_clear_endpoint_stall_async(usc->usc_tx_data_pipe);
2240 1.1 christos ifp->if_oerrors++;
2241 1.1 christos splx(s);
2242 1.1 christos /* XXX Why return? */
2243 1.1 christos return;
2244 1.1 christos }
2245 1.1 christos sc->sc_tx_timer = 0;
2246 1.1 christos ifp->if_opackets++;
2247 1.1 christos
2248 1.1 christos /* We just released a Tx buffer, notify Tx. */
2249 1.1 christos if (ifp->if_flags & IFF_OACTIVE) {
2250 1.1 christos ifp->if_flags &= ~IFF_OACTIVE;
2251 1.1 christos ifp->if_start(ifp);
2252 1.1 christos }
2253 1.1 christos splx(s);
2254 1.1 christos }
2255 1.1 christos
2256 1.1 christos Static int
2257 1.1 christos athn_usb_tx(struct athn_softc *sc, struct mbuf *m, struct ieee80211_node *ni,
2258 1.1 christos struct athn_usb_tx_data *data)
2259 1.1 christos {
2260 1.1 christos struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
2261 1.1 christos struct athn_node *an = ATHN_NODE(ni);
2262 1.1 christos struct ieee80211com *ic = &sc->sc_ic;
2263 1.1 christos struct ieee80211_frame *wh;
2264 1.1 christos struct ieee80211_key *k = NULL;
2265 1.1 christos struct ar_stream_hdr *hdr;
2266 1.1 christos struct ar_htc_frame_hdr *htc;
2267 1.1 christos struct ar_tx_frame *txf;
2268 1.1 christos struct ar_tx_mgmt *txm;
2269 1.1 christos uint8_t *frm;
2270 1.1 christos uint8_t sta_index, qid, tid;
2271 1.1 christos int error, s, xferlen;
2272 1.1 christos
2273 1.1 christos DPRINTFN(DBG_FN, sc, "\n");
2274 1.1 christos
2275 1.1 christos wh = mtod(m, struct ieee80211_frame *);
2276 1.1 christos if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
2277 1.1 christos k = ieee80211_crypto_encap(ic, ni, m);
2278 1.1 christos if (k == NULL)
2279 1.1 christos return ENOBUFS;
2280 1.1 christos
2281 1.1 christos /* packet header may have moved, reset our local pointer */
2282 1.1 christos wh = mtod(m, struct ieee80211_frame *);
2283 1.1 christos }
2284 1.1 christos #ifdef notyet_edca
2285 1.1 christos if (ieee80211_has_qos(wh)) {
2286 1.1 christos uint16_t qos;
2287 1.1 christos
2288 1.1 christos qos = ieee80211_get_qos(wh);
2289 1.1 christos tid = qos & IEEE80211_QOS_TID;
2290 1.1 christos qid = ieee80211_up_to_ac(ic, tid);
2291 1.1 christos }
2292 1.1 christos else
2293 1.1 christos #endif /* notyet_edca */
2294 1.1 christos {
2295 1.1 christos tid = 0;
2296 1.3 christos qid = WME_AC_BE;
2297 1.1 christos }
2298 1.1 christos
2299 1.1 christos /* XXX Change radiotap Tx header for USB (no txrate). */
2300 1.1 christos if (__predict_false(sc->sc_drvbpf != NULL)) {
2301 1.1 christos struct athn_tx_radiotap_header *tap = &sc->sc_txtap;
2302 1.1 christos
2303 1.1 christos tap->wt_flags = 0;
2304 1.1 christos tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
2305 1.1 christos tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
2306 1.1 christos if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED)
2307 1.1 christos tap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
2308 1.1 christos
2309 1.1 christos bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m);
2310 1.1 christos }
2311 1.1 christos sta_index = an->sta_index;
2312 1.1 christos
2313 1.1 christos /* NB: We don't take advantage of USB Tx stream mode for now. */
2314 1.1 christos hdr = (struct ar_stream_hdr *)data->buf;
2315 1.1 christos hdr->tag = htole16(AR_USB_TX_STREAM_TAG);
2316 1.1 christos
2317 1.1 christos htc = (struct ar_htc_frame_hdr *)&hdr[1];
2318 1.1 christos memset(htc, 0, sizeof(*htc));
2319 1.1 christos if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
2320 1.1 christos IEEE80211_FC0_TYPE_DATA) {
2321 1.1 christos htc->endpoint_id = usc->usc_ep_data[qid];
2322 1.1 christos
2323 1.1 christos txf = (struct ar_tx_frame *)&htc[1];
2324 1.1 christos memset(txf, 0, sizeof(*txf));
2325 1.1 christos txf->data_type = AR_HTC_NORMAL;
2326 1.1 christos txf->node_idx = sta_index;
2327 1.1 christos txf->vif_idx = 0;
2328 1.1 christos txf->tid = tid;
2329 1.1 christos if (m->m_pkthdr.len + IEEE80211_CRC_LEN > ic->ic_rtsthreshold)
2330 1.1 christos txf->flags |= htobe32(AR_HTC_TX_RTSCTS);
2331 1.1 christos else if (ic->ic_flags & IEEE80211_F_USEPROT) {
2332 1.1 christos if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
2333 1.1 christos txf->flags |= htobe32(AR_HTC_TX_CTSONLY);
2334 1.1 christos else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
2335 1.1 christos txf->flags |= htobe32(AR_HTC_TX_RTSCTS);
2336 1.1 christos }
2337 1.1 christos txf->key_idx = 0xff;
2338 1.1 christos frm = (uint8_t *)&txf[1];
2339 1.1 christos }
2340 1.1 christos else {
2341 1.1 christos htc->endpoint_id = usc->usc_ep_mgmt;
2342 1.1 christos
2343 1.1 christos txm = (struct ar_tx_mgmt *)&htc[1];
2344 1.1 christos memset(txm, 0, sizeof(*txm));
2345 1.1 christos txm->node_idx = sta_index;
2346 1.1 christos txm->vif_idx = 0;
2347 1.1 christos txm->key_idx = 0xff;
2348 1.1 christos frm = (uint8_t *)&txm[1];
2349 1.1 christos }
2350 1.1 christos /* Copy payload. */
2351 1.1 christos m_copydata(m, 0, m->m_pkthdr.len, (void *)frm);
2352 1.1 christos frm += m->m_pkthdr.len;
2353 1.1 christos
2354 1.1 christos /* Finalize headers. */
2355 1.1 christos htc->payload_len = htobe16(frm - (uint8_t *)&htc[1]);
2356 1.1 christos hdr->len = htole16(frm - (uint8_t *)&hdr[1]);
2357 1.1 christos xferlen = frm - data->buf;
2358 1.1 christos
2359 1.1 christos s = splnet();
2360 1.1 christos usbd_setup_xfer(data->xfer, usc->usc_tx_data_pipe, data, data->buf,
2361 1.1 christos xferlen, USBD_FORCE_SHORT_XFER | USBD_NO_COPY, ATHN_USB_TX_TIMEOUT,
2362 1.1 christos athn_usb_txeof);
2363 1.1 christos error = usbd_transfer(data->xfer);
2364 1.1 christos if (__predict_false(error != USBD_IN_PROGRESS && error != 0)) {
2365 1.1 christos splx(s);
2366 1.1 christos return error;
2367 1.1 christos }
2368 1.1 christos splx(s);
2369 1.1 christos return 0;
2370 1.1 christos }
2371 1.1 christos
2372 1.1 christos Static void
2373 1.1 christos athn_usb_start(struct ifnet *ifp)
2374 1.1 christos {
2375 1.1 christos struct athn_softc *sc = ifp->if_softc;
2376 1.1 christos struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
2377 1.1 christos struct ieee80211com *ic = &sc->sc_ic;
2378 1.1 christos struct athn_usb_tx_data *data;
2379 1.1 christos struct ether_header *eh;
2380 1.1 christos struct ieee80211_node *ni;
2381 1.1 christos struct mbuf *m;
2382 1.1 christos
2383 1.1 christos if (usc->usc_dying)
2384 1.1 christos return;
2385 1.1 christos
2386 1.1 christos DPRINTFN(DBG_FN, sc, "\n");
2387 1.1 christos
2388 1.1 christos if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
2389 1.1 christos return;
2390 1.1 christos
2391 1.1 christos data = NULL;
2392 1.1 christos for (;;) {
2393 1.1 christos mutex_enter(&usc->usc_tx_mtx);
2394 1.1 christos if (data == NULL && !TAILQ_EMPTY(&usc->usc_tx_free_list)) {
2395 1.1 christos data = TAILQ_FIRST(&usc->usc_tx_free_list);
2396 1.1 christos TAILQ_REMOVE(&usc->usc_tx_free_list, data, next);
2397 1.1 christos }
2398 1.1 christos mutex_exit(&usc->usc_tx_mtx);
2399 1.1 christos
2400 1.1 christos if (data == NULL) {
2401 1.1 christos ifp->if_flags |= IFF_OACTIVE;
2402 1.1 christos return;
2403 1.1 christos }
2404 1.1 christos
2405 1.1 christos /* Send pending management frames first. */
2406 1.1 christos IF_DEQUEUE(&ic->ic_mgtq, m);
2407 1.1 christos if (m != NULL) {
2408 1.1 christos ni = (void *)m->m_pkthdr.rcvif;
2409 1.1 christos m->m_pkthdr.rcvif = NULL;
2410 1.1 christos goto sendit;
2411 1.1 christos }
2412 1.1 christos if (ic->ic_state != IEEE80211_S_RUN)
2413 1.1 christos break;
2414 1.1 christos
2415 1.1 christos /* Encapsulate and send data frames. */
2416 1.1 christos IFQ_DEQUEUE(&ifp->if_snd, m);
2417 1.1 christos if (m == NULL)
2418 1.1 christos break;
2419 1.1 christos
2420 1.1 christos if (m->m_len < (int)sizeof(*eh) &&
2421 1.1 christos (m = m_pullup(m, sizeof(*eh))) == NULL) {
2422 1.1 christos ifp->if_oerrors++;
2423 1.1 christos continue;
2424 1.1 christos }
2425 1.1 christos eh = mtod(m, struct ether_header *);
2426 1.1 christos ni = ieee80211_find_txnode(ic, eh->ether_dhost);
2427 1.1 christos if (ni == NULL) {
2428 1.1 christos m_freem(m);
2429 1.1 christos ifp->if_oerrors++;
2430 1.1 christos continue;
2431 1.1 christos }
2432 1.1 christos
2433 1.1 christos bpf_mtap(ifp, m);
2434 1.1 christos
2435 1.1 christos if ((m = ieee80211_encap(ic, m, ni)) == NULL) {
2436 1.1 christos ieee80211_free_node(ni);
2437 1.1 christos ifp->if_oerrors++;
2438 1.1 christos continue;
2439 1.1 christos }
2440 1.1 christos sendit:
2441 1.1 christos bpf_mtap3(ic->ic_rawbpf, m);
2442 1.1 christos
2443 1.1 christos if (athn_usb_tx(sc, m, ni, data) != 0) {
2444 1.1 christos m_free(m);
2445 1.1 christos ieee80211_free_node(ni);
2446 1.1 christos ifp->if_oerrors++;
2447 1.1 christos continue;
2448 1.1 christos }
2449 1.1 christos data = NULL;
2450 1.1 christos m_free(m);
2451 1.1 christos ieee80211_free_node(ni);
2452 1.1 christos sc->sc_tx_timer = 5;
2453 1.1 christos ifp->if_timer = 1;
2454 1.1 christos }
2455 1.1 christos
2456 1.1 christos /* Return the Tx buffer to the free list */
2457 1.1 christos mutex_enter(&usc->usc_tx_mtx);
2458 1.1 christos TAILQ_INSERT_TAIL(&usc->usc_tx_free_list, data, next);
2459 1.1 christos mutex_exit(&usc->usc_tx_mtx);
2460 1.1 christos }
2461 1.1 christos
2462 1.1 christos Static void
2463 1.1 christos athn_usb_watchdog(struct ifnet *ifp)
2464 1.1 christos {
2465 1.1 christos struct athn_softc *sc = ifp->if_softc;
2466 1.1 christos
2467 1.1 christos DPRINTFN(DBG_FN, sc, "\n");
2468 1.1 christos
2469 1.1 christos ifp->if_timer = 0;
2470 1.1 christos
2471 1.1 christos if (sc->sc_tx_timer > 0) {
2472 1.1 christos if (--sc->sc_tx_timer == 0) {
2473 1.1 christos aprint_error_dev(sc->sc_dev, "device timeout\n");
2474 1.1 christos /* athn_usb_init(ifp); XXX needs a process context! */
2475 1.1 christos ifp->if_oerrors++;
2476 1.1 christos return;
2477 1.1 christos }
2478 1.1 christos ifp->if_timer = 1;
2479 1.1 christos }
2480 1.1 christos ieee80211_watchdog(&sc->sc_ic);
2481 1.1 christos }
2482 1.1 christos
2483 1.1 christos Static int
2484 1.1 christos athn_usb_ioctl(struct ifnet *ifp, u_long cmd, void *data)
2485 1.1 christos {
2486 1.1 christos struct athn_softc *sc = ifp->if_softc;
2487 1.1 christos struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
2488 1.1 christos struct ieee80211com *ic = &sc->sc_ic;
2489 1.1 christos int s, error = 0;
2490 1.1 christos
2491 1.1 christos if (usc->usc_dying)
2492 1.1 christos return EIO;
2493 1.1 christos
2494 1.1 christos DPRINTFN(DBG_FN, sc, "cmd=0x%08lx\n", cmd);
2495 1.1 christos
2496 1.1 christos s = splnet();
2497 1.1 christos
2498 1.1 christos switch (cmd) {
2499 1.1 christos case SIOCSIFFLAGS:
2500 1.1 christos if ((error = ifioctl_common(ifp, cmd, data)) != 0)
2501 1.1 christos break;
2502 1.1 christos
2503 1.1 christos switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
2504 1.1 christos case IFF_UP | IFF_RUNNING:
2505 1.1 christos break;
2506 1.1 christos case IFF_UP:
2507 1.1 christos error = athn_usb_init(ifp);
2508 1.1 christos break;
2509 1.1 christos case IFF_RUNNING:
2510 1.1 christos athn_usb_stop(ifp);
2511 1.1 christos break;
2512 1.1 christos case 0:
2513 1.1 christos default:
2514 1.1 christos break;
2515 1.1 christos }
2516 1.1 christos break;
2517 1.1 christos
2518 1.1 christos case SIOCADDMULTI:
2519 1.1 christos case SIOCDELMULTI:
2520 1.1 christos if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
2521 1.1 christos /* setup multicast filter, etc */
2522 1.1 christos error = 0;
2523 1.1 christos }
2524 1.1 christos break;
2525 1.1 christos
2526 1.1 christos case SIOCS80211CHANNEL:
2527 1.1 christos error = ieee80211_ioctl(ic, cmd, data);
2528 1.1 christos if (error == ENETRESET &&
2529 1.1 christos ic->ic_opmode == IEEE80211_M_MONITOR) {
2530 1.1 christos if (IS_UP_AND_RUNNING(ifp))
2531 1.1 christos athn_usb_switch_chan(sc, ic->ic_curchan, NULL);
2532 1.1 christos error = 0;
2533 1.1 christos }
2534 1.1 christos break;
2535 1.1 christos
2536 1.1 christos default:
2537 1.1 christos error = ieee80211_ioctl(ic, cmd, data);
2538 1.1 christos break;
2539 1.1 christos }
2540 1.1 christos if (error == ENETRESET) {
2541 1.1 christos error = 0;
2542 1.1 christos if (IS_UP_AND_RUNNING(ifp) &&
2543 1.1 christos ic->ic_roaming != IEEE80211_ROAMING_MANUAL) {
2544 1.1 christos athn_usb_stop(ifp);
2545 1.1 christos error = athn_usb_init(ifp);
2546 1.1 christos }
2547 1.1 christos }
2548 1.1 christos splx(s);
2549 1.1 christos return error;
2550 1.1 christos }
2551 1.1 christos
2552 1.1 christos Static int
2553 1.1 christos athn_usb_init(struct ifnet *ifp)
2554 1.1 christos {
2555 1.1 christos struct athn_softc *sc = ifp->if_softc;
2556 1.1 christos struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
2557 1.1 christos struct athn_ops *ops = &sc->sc_ops;
2558 1.1 christos struct ieee80211com *ic = &sc->sc_ic;
2559 1.1 christos struct ieee80211_channel *curchan, *extchan;
2560 1.1 christos struct athn_usb_rx_data *data;
2561 1.1 christos struct ar_htc_target_vif hvif;
2562 1.1 christos struct ar_htc_target_sta sta;
2563 1.1 christos struct ar_htc_cap_target hic;
2564 1.1 christos uint16_t mode;
2565 1.1 christos size_t i;
2566 1.1 christos int error;
2567 1.1 christos
2568 1.1 christos if (usc->usc_dying)
2569 1.1 christos return USBD_CANCELLED;
2570 1.1 christos
2571 1.1 christos DPRINTFN(DBG_FN, sc, "\n");
2572 1.1 christos
2573 1.1 christos /* Init host async commands ring. */
2574 1.1 christos mutex_spin_enter(&usc->usc_task_mtx);
2575 1.1 christos usc->usc_cmdq.cur = usc->usc_cmdq.next = usc->usc_cmdq.queued = 0;
2576 1.1 christos mutex_spin_exit(&usc->usc_task_mtx);
2577 1.1 christos
2578 1.1 christos /* Allocate Tx/Rx buffers. */
2579 1.1 christos error = athn_usb_alloc_rx_list(usc);
2580 1.1 christos if (error != 0)
2581 1.1 christos goto fail;
2582 1.1 christos error = athn_usb_alloc_tx_list(usc);
2583 1.1 christos if (error != 0)
2584 1.1 christos goto fail;
2585 1.1 christos /* Steal one buffer for beacons. */
2586 1.1 christos mutex_enter(&usc->usc_tx_mtx);
2587 1.1 christos usc->usc_tx_bcn = TAILQ_FIRST(&usc->usc_tx_free_list);
2588 1.1 christos TAILQ_REMOVE(&usc->usc_tx_free_list, usc->usc_tx_bcn, next);
2589 1.1 christos mutex_exit(&usc->usc_tx_mtx);
2590 1.1 christos
2591 1.1 christos curchan = ic->ic_curchan;
2592 1.1 christos extchan = NULL;
2593 1.1 christos
2594 1.1 christos /* In case a new MAC address has been configured. */
2595 1.1 christos IEEE80211_ADDR_COPY(ic->ic_myaddr, CLLADDR(ifp->if_sadl));
2596 1.1 christos
2597 1.1 christos error = athn_set_power_awake(sc);
2598 1.1 christos if (error != 0)
2599 1.1 christos goto fail;
2600 1.1 christos
2601 1.1 christos error = athn_usb_wmi_cmd(usc, AR_WMI_CMD_FLUSH_RECV);
2602 1.1 christos if (error != 0)
2603 1.1 christos goto fail;
2604 1.1 christos
2605 1.1 christos error = athn_hw_reset(sc, curchan, extchan, 1);
2606 1.1 christos if (error != 0)
2607 1.1 christos goto fail;
2608 1.1 christos
2609 1.1 christos ops->set_txpower(sc, curchan, extchan);
2610 1.1 christos
2611 1.1 christos mode = htobe16(IEEE80211_IS_CHAN_2GHZ(curchan) ?
2612 1.1 christos AR_HTC_MODE_11NG : AR_HTC_MODE_11NA);
2613 1.1 christos error = athn_usb_wmi_xcmd(usc, AR_WMI_CMD_SET_MODE,
2614 1.1 christos &mode, sizeof(mode), NULL);
2615 1.1 christos if (error != 0)
2616 1.1 christos goto fail;
2617 1.1 christos
2618 1.1 christos error = athn_usb_wmi_cmd(usc, AR_WMI_CMD_ATH_INIT);
2619 1.1 christos if (error != 0)
2620 1.1 christos goto fail;
2621 1.1 christos
2622 1.1 christos error = athn_usb_wmi_cmd(usc, AR_WMI_CMD_START_RECV);
2623 1.1 christos if (error != 0)
2624 1.1 christos goto fail;
2625 1.1 christos
2626 1.1 christos athn_rx_start(sc);
2627 1.1 christos
2628 1.1 christos /* Create main interface on target. */
2629 1.1 christos memset(&hvif, 0, sizeof(hvif));
2630 1.1 christos hvif.index = 0;
2631 1.1 christos IEEE80211_ADDR_COPY(hvif.myaddr, ic->ic_myaddr);
2632 1.1 christos switch (ic->ic_opmode) {
2633 1.1 christos case IEEE80211_M_STA:
2634 1.1 christos hvif.opmode = htobe32(AR_HTC_M_STA);
2635 1.1 christos break;
2636 1.1 christos case IEEE80211_M_MONITOR:
2637 1.1 christos hvif.opmode = htobe32(AR_HTC_M_MONITOR);
2638 1.1 christos break;
2639 1.1 christos #ifndef IEEE80211_STA_ONLY
2640 1.1 christos case IEEE80211_M_IBSS:
2641 1.1 christos hvif.opmode = htobe32(AR_HTC_M_IBSS);
2642 1.1 christos break;
2643 1.1 christos case IEEE80211_M_AHDEMO:
2644 1.1 christos hvif.opmode = htobe32(AR_HTC_M_AHDEMO);
2645 1.1 christos break;
2646 1.1 christos case IEEE80211_M_HOSTAP:
2647 1.1 christos hvif.opmode = htobe32(AR_HTC_M_HOSTAP);
2648 1.1 christos break;
2649 1.1 christos #endif
2650 1.1 christos }
2651 1.1 christos hvif.rtsthreshold = htobe16(ic->ic_rtsthreshold);
2652 1.1 christos DPRINTFN(DBG_INIT, sc, "creating VAP\n");
2653 1.1 christos error = athn_usb_wmi_xcmd(usc, AR_WMI_CMD_VAP_CREATE,
2654 1.1 christos &hvif, sizeof(hvif), NULL);
2655 1.1 christos if (error != 0)
2656 1.1 christos goto fail;
2657 1.1 christos
2658 1.1 christos /* Create a fake node to send management frames before assoc. */
2659 1.1 christos memset(&sta, 0, sizeof(sta));
2660 1.1 christos IEEE80211_ADDR_COPY(sta.macaddr, ic->ic_myaddr);
2661 1.1 christos sta.sta_index = 0;
2662 1.1 christos sta.is_vif_sta = 1;
2663 1.1 christos sta.vif_index = hvif.index;
2664 1.1 christos sta.maxampdu = 0xffff;
2665 1.1 christos
2666 1.1 christos DPRINTFN(DBG_INIT | DBG_NODES, sc, "creating default node %u\n",
2667 1.1 christos sta.sta_index);
2668 1.1 christos error = athn_usb_create_hw_node(usc, &sta);
2669 1.1 christos if (error != 0)
2670 1.1 christos goto fail;
2671 1.1 christos
2672 1.1 christos /* Update target capabilities. */
2673 1.1 christos memset(&hic, 0, sizeof(hic));
2674 1.1 christos hic.flags = htobe32(0x400c2400);
2675 1.1 christos hic.flags_ext = htobe32(0x00106080);
2676 1.1 christos hic.ampdu_limit = htobe32(0x0000ffff);
2677 1.1 christos hic.ampdu_subframes = 20;
2678 1.1 christos hic.protmode = 1; /* XXX */
2679 1.1 christos hic.lg_txchainmask = sc->sc_txchainmask;
2680 1.1 christos hic.ht_txchainmask = sc->sc_txchainmask;
2681 1.1 christos DPRINTFN(DBG_INIT, sc, "updating target configuration\n");
2682 1.1 christos error = athn_usb_wmi_xcmd(usc, AR_WMI_CMD_TARGET_IC_UPDATE,
2683 1.1 christos &hic, sizeof(hic), NULL);
2684 1.1 christos if (error != 0)
2685 1.1 christos goto fail;
2686 1.1 christos
2687 1.1 christos /* Queue Rx xfers. */
2688 1.1 christos for (i = 0; i < ATHN_USB_RX_LIST_COUNT; i++) {
2689 1.1 christos data = &usc->usc_rx_data[i];
2690 1.1 christos
2691 1.1 christos usbd_setup_xfer(data->xfer, usc->usc_rx_data_pipe, data, data->buf,
2692 1.1 christos ATHN_USB_RXBUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY,
2693 1.1 christos USBD_NO_TIMEOUT, athn_usb_rxeof);
2694 1.1 christos error = usbd_transfer(data->xfer);
2695 1.1 christos if (error != 0 && error != USBD_IN_PROGRESS)
2696 1.1 christos goto fail;
2697 1.1 christos }
2698 1.1 christos /* We're ready to go. */
2699 1.1 christos ifp->if_flags &= ~IFF_OACTIVE;
2700 1.1 christos ifp->if_flags |= IFF_RUNNING;
2701 1.1 christos
2702 1.1 christos #ifdef notyet
2703 1.1 christos if (ic->ic_flags & IEEE80211_F_WEPON) {
2704 1.1 christos /* Install WEP keys. */
2705 1.1 christos for (i = 0; i < IEEE80211_WEP_NKID; i++)
2706 1.1 christos athn_usb_set_key(ic, NULL, &ic->ic_nw_keys[i]);
2707 1.1 christos }
2708 1.1 christos #endif
2709 1.1 christos if (ic->ic_opmode == IEEE80211_M_HOSTAP)
2710 1.1 christos ic->ic_max_aid = AR_USB_MAX_STA; /* Firmware is limited to 8 STA */
2711 1.1 christos else
2712 1.1 christos ic->ic_max_aid = sc->sc_max_aid;
2713 1.1 christos
2714 1.1 christos if (ic->ic_opmode == IEEE80211_M_MONITOR)
2715 1.1 christos ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2716 1.1 christos else
2717 1.1 christos ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2718 1.1 christos athn_usb_wait_async(usc);
2719 1.1 christos return 0;
2720 1.1 christos fail:
2721 1.1 christos athn_usb_stop(ifp);
2722 1.1 christos return error;
2723 1.1 christos }
2724 1.1 christos
2725 1.1 christos Static void
2726 1.1 christos athn_usb_stop(struct ifnet *ifp)
2727 1.1 christos {
2728 1.1 christos struct athn_softc *sc = ifp->if_softc;
2729 1.1 christos struct athn_usb_softc *usc = ATHN_USB_SOFTC(sc);
2730 1.1 christos struct ieee80211com *ic = &sc->sc_ic;
2731 1.1 christos struct ar_htc_target_vif hvif;
2732 1.1 christos struct mbuf *m;
2733 1.1 christos uint8_t sta_index;
2734 1.1 christos int s, error;
2735 1.1 christos
2736 1.1 christos DPRINTFN(DBG_FN, sc, "\n");
2737 1.1 christos
2738 1.1 christos s = splusb();
2739 1.1 christos ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2740 1.1 christos athn_usb_wait_async(usc);
2741 1.1 christos splx(s);
2742 1.1 christos
2743 1.1 christos sc->sc_tx_timer = 0;
2744 1.1 christos ifp->if_timer = 0;
2745 1.1 christos ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2746 1.1 christos
2747 1.1 christos callout_stop(&sc->sc_scan_to);
2748 1.1 christos callout_stop(&sc->sc_calib_to);
2749 1.1 christos
2750 1.1 christos /* Abort Tx/Rx. */
2751 1.1 christos usbd_abort_pipe(usc->usc_tx_data_pipe);
2752 1.1 christos usbd_abort_pipe(usc->usc_rx_data_pipe);
2753 1.1 christos
2754 1.1 christos /* Free Tx/Rx buffers. */
2755 1.1 christos athn_usb_free_tx_list(usc);
2756 1.1 christos athn_usb_free_rx_list(usc);
2757 1.1 christos
2758 1.1 christos /* Flush Rx stream. */
2759 1.1 christos CTASSERT(sizeof(m) == sizeof(void *));
2760 1.1 christos m = atomic_swap_ptr(&usc->usc_rx_stream.m, NULL);
2761 1.1 christos m_freem(m);
2762 1.1 christos usc->usc_rx_stream.left = 0;
2763 1.1 christos
2764 1.1 christos /* Remove main interface. */
2765 1.1 christos memset(&hvif, 0, sizeof(hvif));
2766 1.1 christos hvif.index = 0;
2767 1.1 christos IEEE80211_ADDR_COPY(hvif.myaddr, ic->ic_myaddr);
2768 1.1 christos (void)athn_usb_wmi_xcmd(usc, AR_WMI_CMD_VAP_REMOVE,
2769 1.1 christos &hvif, sizeof(hvif), NULL);
2770 1.1 christos
2771 1.1 christos /* Remove default node. */
2772 1.1 christos sta_index = 0;
2773 1.1 christos DPRINTFN(DBG_NODES, usc, "removing node %u\n", sta_index);
2774 1.1 christos error = athn_usb_remove_hw_node(usc, &sta_index);
2775 1.1 christos
2776 1.1 christos (void)athn_usb_wmi_cmd(usc, AR_WMI_CMD_DISABLE_INTR);
2777 1.1 christos (void)athn_usb_wmi_cmd(usc, AR_WMI_CMD_DRAIN_TXQ_ALL);
2778 1.1 christos (void)athn_usb_wmi_cmd(usc, AR_WMI_CMD_STOP_RECV);
2779 1.1 christos
2780 1.1 christos athn_reset(sc, 0);
2781 1.1 christos athn_init_pll(sc, NULL);
2782 1.1 christos athn_set_power_awake(sc);
2783 1.1 christos athn_reset(sc, 1);
2784 1.1 christos athn_init_pll(sc, NULL);
2785 1.1 christos athn_set_power_sleep(sc);
2786 1.1 christos }
2787 1.1 christos
2788 1.1 christos MODULE(MODULE_CLASS_DRIVER, if_athn_usb, "bpf");
2789 1.1 christos
2790 1.1 christos #ifdef _MODULE
2791 1.1 christos #include "ioconf.c"
2792 1.1 christos #endif
2793 1.1 christos
2794 1.1 christos static int
2795 1.1 christos if_athn_usb_modcmd(modcmd_t cmd, void *aux)
2796 1.1 christos {
2797 1.1 christos int error = 0;
2798 1.1 christos
2799 1.1 christos switch (cmd) {
2800 1.1 christos case MODULE_CMD_INIT:
2801 1.1 christos #ifdef _MODULE
2802 1.1 christos error = config_init_component(cfdriver_ioconf_if_athn_usb,
2803 1.1 christos cfattach_ioconf_if_athn_usb, cfdata_ioconf_if_athn_usb);
2804 1.1 christos #endif
2805 1.1 christos return error;
2806 1.1 christos case MODULE_CMD_FINI:
2807 1.1 christos #ifdef _MODULE
2808 1.1 christos error = config_fini_component(cfdriver_ioconf_if_athn_usb,
2809 1.1 christos cfattach_ioconf_if_athn_usb, cfdata_ioconf_if_athn_usb);
2810 1.1 christos #endif
2811 1.1 christos return error;
2812 1.1 christos default:
2813 1.1 christos return ENOTTY;
2814 1.1 christos }
2815 1.1 christos }
2816