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