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