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